2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 CKEDITOR.htmlParser.filter = CKEDITOR.tools.createClass(
\r
10 $ : function( rules )
\r
15 attributeNames : [],
\r
16 elements : { $length : 0 },
\r
17 attributes : { $length : 0 }
\r
21 this.addRules( rules, 10 );
\r
26 addRules : function( rules, priority )
\r
28 if ( typeof priority != 'number' )
\r
31 // Add the elementNames.
\r
32 addItemsToList( this._.elementNames, rules.elementNames, priority );
\r
34 // Add the attributeNames.
\r
35 addItemsToList( this._.attributeNames, rules.attributeNames, priority );
\r
37 // Add the elements.
\r
38 addNamedItems( this._.elements, rules.elements, priority );
\r
40 // Add the attributes.
\r
41 addNamedItems( this._.attributes, rules.attributes, priority );
\r
44 this._.text = transformNamedItem( this._.text, rules.text, priority ) || this._.text;
\r
47 this._.comment = transformNamedItem( this._.comment, rules.comment, priority ) || this._.comment;
\r
50 onElementName : function( name )
\r
52 return filterName( name, this._.elementNames );
\r
55 onAttributeName : function( name )
\r
57 return filterName( name, this._.attributeNames );
\r
60 onText : function( text )
\r
62 var textFilter = this._.text;
\r
63 return textFilter ? textFilter.filter( text ) : text;
\r
66 onComment : function( commentText )
\r
68 var textFilter = this._.comment;
\r
69 return textFilter ? textFilter.filter( commentText ) : commentText;
\r
72 onElement : function( element )
\r
74 // We must apply filters set to the specific element name as
\r
75 // well as those set to the generic $ name. So, add both to an
\r
76 // array and process them in a small loop.
\r
77 var filters = [ this._.elements[ element.name ], this._.elements.$ ],
\r
80 for ( var i = 0 ; i < 2 ; i++ )
\r
82 filter = filters[ i ];
\r
85 ret = filter.filter( element, this );
\r
87 if ( ret === false )
\r
90 if ( ret && ret != element )
\r
91 return this.onElement( ret );
\r
98 onAttribute : function( element, name, value )
\r
100 var filter = this._.attributes[ name ];
\r
104 var ret = filter.filter( value, element, this );
\r
106 if ( ret === false )
\r
109 if ( typeof ret != 'undefined' )
\r
118 function filterName( name, filters )
\r
120 for ( var i = 0 ; name && i < filters.length ; i++ )
\r
122 var filter = filters[ i ];
\r
123 name = name.replace( filter[ 0 ], filter[ 1 ] );
\r
128 function addItemsToList( list, items, priority )
\r
130 if( typeof items == 'function' )
\r
134 listLength = list.length,
\r
135 itemsLength = items && items.length;
\r
139 // Find the index to insert the items at.
\r
140 for ( i = 0 ; i < listLength && list[ i ].pri < priority ; i++ )
\r
143 // Add all new items to the list at the specific index.
\r
144 for ( j = itemsLength - 1 ; j >= 0 ; j-- )
\r
146 var item = items[ j ];
\r
147 item.pri = priority;
\r
148 list.splice( i, 0, item );
\r
153 function addNamedItems( hashTable, items, priority )
\r
157 for ( var name in items )
\r
159 var current = hashTable[ name ];
\r
161 hashTable[ name ] =
\r
162 transformNamedItem(
\r
168 hashTable.$length++;
\r
173 function transformNamedItem( current, item, priority )
\r
177 item.pri = priority;
\r
181 // If the current item is not an Array, transform it.
\r
182 if ( !current.splice )
\r
184 if ( current.pri > priority )
\r
185 current = [ item, current ];
\r
187 current = [ current, item ];
\r
189 current.filter = callItems;
\r
192 addItemsToList( current, item, priority );
\r
198 item.filter = item;
\r
204 function callItems( currentEntry )
\r
206 var isObject = ( typeof currentEntry == 'object' );
\r
208 for ( var i = 0 ; i < this.length ; i++ )
\r
210 var item = this[ i ],
\r
211 ret = item.apply( window, arguments );
\r
213 if ( typeof ret != 'undefined' )
\r
215 if ( ret === false )
\r
218 if ( isObject && ret != currentEntry )
\r
227 // "entities" plugin
\r
230 text : function( text )
\r
232 // TODO : Process entities.
\r
233 return text.toUpperCase();
\r