Ticket #4548: 4548.patch

File 4548.patch, 3.5 KB (added by Garry Yao, 15 years ago)
  • _source/plugins/fakecomment/plugin.js

     
     1/*
     2Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     3For licensing, see LICENSE.html or http://ckeditor.com/license
     4*/
     5
     6/**
     7 * @file Comment place holder plugin which create a fake object to represent HTML
     8 * comments in certain patterns.
     9 */
     10
     11CKEDITOR.plugins.add( 'fakecomment',
     12{
     13        requires  : [ 'htmldataprocessor','fakeobjects' ],
     14        init : function( editor )
     15        {
     16                // Add the style that renders our placeholder.
     17                editor.addCss(
     18                        'img.cke_comment' +
     19                        '{' +
     20                                'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.gif' ) + ');' +
     21                            '-webkit-background-size: 20px 20px;' +
     22                            '-o-background-size: 20px 20px;' +
     23//                          '-moz-background-size: 20px 20px;' +
     24                                'background-position: center center;' +
     25                                'background-repeat: no-repeat;' +
     26                                'width: 20px;' +
     27                                'vertical-align: middle;' +
     28                                'border: #999999 1px dotted;' +
     29                        '}' );
     30        },
     31
     32        // There's currently no gurantee on registration order, this make sure
     33        // the registration will come after the default ones in 'htmldataprocessor'.
     34        afterInit : function( editor )
     35        {
     36                var dataProcessor = editor.dataProcessor,
     37                        dataFilter = dataProcessor.dataFilter,
     38                        htmlFilter = dataProcessor.htmlFilter,
     39                        commentFilters = htmlFilter._.comment,
     40                        filter = commentFilters.filter || commentFilters[ 0 ],
     41                        config = editor.config,
     42                        fakeComments = config.fakeComments,
     43                        fakeCommentName = config.fakeCommentName || 'cke:comment';
     44
     45                if( ! ( fakeComments && fakeComments.length ) )
     46                        return;
     47
     48                // Create fake objects for all fake comments.
     49                dataFilter.addRules(
     50                        {
     51                                comment : function( value )
     52                                {
     53                                        var data = filter( value ), fakeWrapper, fakeElement;
     54
     55                                        // Instance of CKEDITOR.htmlParser.cdata is expected.
     56                                        if ( data.value )
     57                                        {
     58                                                for ( var i = 0; i < fakeComments.length; i++ )
     59                                                {
     60                                                        // Is it actually a comment and contains the desired values?
     61                                                        data.value.replace( /<!--([\s\S]*?)-->/, function( match, content )
     62                                                        {
     63                                                                if( content.match( fakeComments[ i ] ) )
     64                                                                {
     65                                                                        fakeWrapper = new CKEDITOR.htmlParser.element( fakeCommentName, {} );
     66                                                                        fakeWrapper.add( data );
     67                                                                        fakeElement = editor.createFakeParserElement( fakeWrapper, 'cke_comment', fakeCommentName, false );
     68                                                                }
     69                                                        } );
     70
     71                                                        if ( fakeElement )
     72                                                                return fakeElement;
     73                                                }
     74                                        }
     75                                        return value;
     76                                }
     77                        } );
     78
     79                // Reveal the comment in output data.
     80                var rule = { elements : {} };
     81                rule.elements[ fakeCommentName ] = function( element )
     82                {
     83                        // Drop this wrapper element.
     84                        delete element.name;
     85                };
     86                htmlFilter.addRules( rule );
     87        }
     88} );
     89
     90/**
     91 * A list of comment content value patterns which will be converted to fake element.
     92 * @type Array
     93 * @default Empty
     94 * @example
     95 * config.fakeComments = [ /^break$/ ];
     96 */
     97//CKEDITOR.config.fakeComments = [];
     98
     99/**
     100 * Display name of the fake comment in editor.
     101 * @type String
     102 * @default 'cke:comment'
     103 * @example
     104 * config.fakeCommentName = 'comment';
     105 */
     106//CKEDITOR.config.fakeCommentName = 'cke:comment';
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy