[2862] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
|
---|
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | (function()
|
---|
| 7 | {
|
---|
| 8 | CKEDITOR.htmlParser.filter = CKEDITOR.tools.createClass(
|
---|
| 9 | {
|
---|
| 10 | $ : function( rules )
|
---|
| 11 | {
|
---|
| 12 | this._ =
|
---|
| 13 | {
|
---|
| 14 | elementNames : [],
|
---|
| 15 | attributeNames : [],
|
---|
| 16 | elements : { $length : 0 },
|
---|
| 17 | attributes : { $length : 0 }
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | if ( rules )
|
---|
| 21 | this.addRules( rules, 10 );
|
---|
| 22 | },
|
---|
| 23 |
|
---|
| 24 | proto :
|
---|
| 25 | {
|
---|
| 26 | addRules : function( rules, priority )
|
---|
| 27 | {
|
---|
| 28 | if ( typeof priority != 'number' )
|
---|
| 29 | priority = 10;
|
---|
| 30 |
|
---|
| 31 | // Add the elementNames.
|
---|
| 32 | addItemsToList( this._.elementNames, rules.elementNames, priority );
|
---|
| 33 |
|
---|
| 34 | // Add the attributeNames.
|
---|
| 35 | addItemsToList( this._.attributeNames, rules.attributeNames, priority );
|
---|
| 36 |
|
---|
| 37 | // Add the elements.
|
---|
| 38 | addNamedItems( this._.elements, rules.elements, priority );
|
---|
| 39 |
|
---|
| 40 | // Add the attributes.
|
---|
| 41 | addNamedItems( this._.attributes, rules.attributes, priority );
|
---|
| 42 |
|
---|
| 43 | // Add the text.
|
---|
| 44 | this._.text = transformNamedItem( this._.text, rules.text, priority ) || this._.text;
|
---|
| 45 |
|
---|
| 46 | // Add the comment.
|
---|
| 47 | this._.comment = transformNamedItem( this._.comment, rules.comment, priority ) || this._.comment;
|
---|
| 48 |
|
---|
| 49 | // Add root fragment.
|
---|
| 50 | this._.root = transformNamedItem( this._.root, rules.root, priority ) || this._.root;
|
---|
| 51 | },
|
---|
| 52 |
|
---|
| 53 | onElementName : function( name )
|
---|
| 54 | {
|
---|
| 55 | return filterName( name, this._.elementNames );
|
---|
| 56 | },
|
---|
| 57 |
|
---|
| 58 | onAttributeName : function( name )
|
---|
| 59 | {
|
---|
| 60 | return filterName( name, this._.attributeNames );
|
---|
| 61 | },
|
---|
| 62 |
|
---|
| 63 | onText : function( text )
|
---|
| 64 | {
|
---|
| 65 | var textFilter = this._.text;
|
---|
| 66 | return textFilter ? textFilter.filter( text ) : text;
|
---|
| 67 | },
|
---|
| 68 |
|
---|
| 69 | onComment : function( commentText, comment )
|
---|
| 70 | {
|
---|
| 71 | var textFilter = this._.comment;
|
---|
| 72 | return textFilter ? textFilter.filter( commentText, comment ) : commentText;
|
---|
| 73 | },
|
---|
| 74 |
|
---|
| 75 | onFragment : function( element )
|
---|
| 76 | {
|
---|
| 77 | var rootFilter = this._.root;
|
---|
| 78 | return rootFilter ? rootFilter.filter( element ) : element;
|
---|
| 79 | },
|
---|
| 80 |
|
---|
| 81 | onElement : function( element )
|
---|
| 82 | {
|
---|
| 83 | // We must apply filters set to the specific element name as
|
---|
| 84 | // well as those set to the generic $ name. So, add both to an
|
---|
| 85 | // array and process them in a small loop.
|
---|
| 86 | var filters = [ this._.elements[ '^' ], this._.elements[ element.name ], this._.elements.$ ],
|
---|
| 87 | filter, ret;
|
---|
| 88 |
|
---|
| 89 | for ( var i = 0 ; i < 3 ; i++ )
|
---|
| 90 | {
|
---|
| 91 | filter = filters[ i ];
|
---|
| 92 | if ( filter )
|
---|
| 93 | {
|
---|
| 94 | ret = filter.filter( element, this );
|
---|
| 95 |
|
---|
| 96 | if ( ret === false )
|
---|
| 97 | return null;
|
---|
| 98 |
|
---|
| 99 | if ( ret && ret != element )
|
---|
| 100 | return this.onNode( ret );
|
---|
| 101 |
|
---|
| 102 | // The non-root element has been dismissed by one of the filters.
|
---|
| 103 | if ( element.parent && !element.name )
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return element;
|
---|
| 109 | },
|
---|
| 110 |
|
---|
| 111 | onNode : function( node )
|
---|
| 112 | {
|
---|
| 113 | var type = node.type;
|
---|
| 114 |
|
---|
| 115 | return type == CKEDITOR.NODE_ELEMENT ? this.onElement( node ) :
|
---|
| 116 | type == CKEDITOR.NODE_TEXT ? new CKEDITOR.htmlParser.text( this.onText( node.value ) ) :
|
---|
| 117 | type == CKEDITOR.NODE_COMMENT ? new CKEDITOR.htmlParser.comment( this.onComment( node.value ) ):
|
---|
| 118 | null;
|
---|
| 119 | },
|
---|
| 120 |
|
---|
| 121 | onAttribute : function( element, name, value )
|
---|
| 122 | {
|
---|
| 123 | var filter = this._.attributes[ name ];
|
---|
| 124 |
|
---|
| 125 | if ( filter )
|
---|
| 126 | {
|
---|
| 127 | var ret = filter.filter( value, element, this );
|
---|
| 128 |
|
---|
| 129 | if ( ret === false )
|
---|
| 130 | return false;
|
---|
| 131 |
|
---|
| 132 | if ( typeof ret != 'undefined' )
|
---|
| 133 | return ret;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return value;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | });
|
---|
| 140 |
|
---|
| 141 | function filterName( name, filters )
|
---|
| 142 | {
|
---|
| 143 | for ( var i = 0 ; name && i < filters.length ; i++ )
|
---|
| 144 | {
|
---|
| 145 | var filter = filters[ i ];
|
---|
| 146 | name = name.replace( filter[ 0 ], filter[ 1 ] );
|
---|
| 147 | }
|
---|
| 148 | return name;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | function addItemsToList( list, items, priority )
|
---|
| 152 | {
|
---|
| 153 | if ( typeof items == 'function' )
|
---|
| 154 | items = [ items ];
|
---|
| 155 |
|
---|
| 156 | var i, j,
|
---|
| 157 | listLength = list.length,
|
---|
| 158 | itemsLength = items && items.length;
|
---|
| 159 |
|
---|
| 160 | if ( itemsLength )
|
---|
| 161 | {
|
---|
| 162 | // Find the index to insert the items at.
|
---|
| 163 | for ( i = 0 ; i < listLength && list[ i ].pri < priority ; i++ )
|
---|
| 164 | { /*jsl:pass*/ }
|
---|
| 165 |
|
---|
| 166 | // Add all new items to the list at the specific index.
|
---|
| 167 | for ( j = itemsLength - 1 ; j >= 0 ; j-- )
|
---|
| 168 | {
|
---|
| 169 | var item = items[ j ];
|
---|
| 170 | if ( item )
|
---|
| 171 | {
|
---|
| 172 | item.pri = priority;
|
---|
| 173 | list.splice( i, 0, item );
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | function addNamedItems( hashTable, items, priority )
|
---|
| 180 | {
|
---|
| 181 | if ( items )
|
---|
| 182 | {
|
---|
| 183 | for ( var name in items )
|
---|
| 184 | if ( ! ( name in Object.prototype ) )
|
---|
| 185 | {
|
---|
| 186 | var current = hashTable[ name ];
|
---|
| 187 |
|
---|
| 188 | hashTable[ name ] =
|
---|
| 189 | transformNamedItem(
|
---|
| 190 | current,
|
---|
| 191 | items[ name ],
|
---|
| 192 | priority );
|
---|
| 193 |
|
---|
| 194 | if ( !current )
|
---|
| 195 | hashTable.$length++;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | function transformNamedItem( current, item, priority )
|
---|
| 201 | {
|
---|
| 202 | if ( item )
|
---|
| 203 | {
|
---|
| 204 | item.pri = priority;
|
---|
| 205 |
|
---|
| 206 | if ( current )
|
---|
| 207 | {
|
---|
| 208 | // If the current item is not an Array, transform it.
|
---|
| 209 | if ( !current.splice )
|
---|
| 210 | {
|
---|
| 211 | if ( current.pri > priority )
|
---|
| 212 | current = [ item, current ];
|
---|
| 213 | else
|
---|
| 214 | current = [ current, item ];
|
---|
| 215 |
|
---|
| 216 | current.filter = callItems;
|
---|
| 217 | }
|
---|
| 218 | else
|
---|
| 219 | addItemsToList( current, item, priority );
|
---|
| 220 |
|
---|
| 221 | return current;
|
---|
| 222 | }
|
---|
| 223 | else
|
---|
| 224 | {
|
---|
| 225 | item.filter = item;
|
---|
| 226 | return item;
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | function callItems( currentEntry )
|
---|
| 232 | {
|
---|
| 233 | var isObject = ( typeof currentEntry == 'object' );
|
---|
| 234 |
|
---|
| 235 | for ( var i = 0 ; i < this.length ; i++ )
|
---|
| 236 | {
|
---|
| 237 | var item = this[ i ],
|
---|
| 238 | ret = item.apply( window, arguments );
|
---|
| 239 |
|
---|
| 240 | if ( typeof ret != 'undefined' )
|
---|
| 241 | {
|
---|
| 242 | if ( ret === false )
|
---|
| 243 | return false;
|
---|
| 244 |
|
---|
| 245 | if ( isObject && ret != currentEntry )
|
---|
| 246 | return ret;
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | return null;
|
---|
| 251 | }
|
---|
| 252 | })();
|
---|
| 253 |
|
---|
| 254 | // "entities" plugin
|
---|
| 255 | /*
|
---|
| 256 | {
|
---|
| 257 | text : function( text )
|
---|
| 258 | {
|
---|
| 259 | // TODO : Process entities.
|
---|
| 260 | return text.toUpperCase();
|
---|
| 261 | }
|
---|
| 262 | };
|
---|
| 263 | */
|
---|