JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / htmlparser / filter.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         CKEDITOR.htmlParser.filter = CKEDITOR.tools.createClass(\r
9         {\r
10                 $ : function( rules )\r
11                 {\r
12                         this._ =\r
13                         {\r
14                                 elementNames : [],\r
15                                 attributeNames : [],\r
16                                 elements : { $length : 0 },\r
17                                 attributes : { $length : 0 }\r
18                         };\r
19 \r
20                         if ( rules )\r
21                                 this.addRules( rules, 10 );\r
22                 },\r
23 \r
24                 proto :\r
25                 {\r
26                         addRules : function( rules, priority )\r
27                         {\r
28                                 if ( typeof priority != 'number' )\r
29                                         priority = 10;\r
30 \r
31                                 // Add the elementNames.\r
32                                 addItemsToList( this._.elementNames, rules.elementNames, priority );\r
33 \r
34                                 // Add the attributeNames.\r
35                                 addItemsToList( this._.attributeNames, rules.attributeNames, priority );\r
36 \r
37                                 // Add the elements.\r
38                                 addNamedItems( this._.elements, rules.elements, priority );\r
39 \r
40                                 // Add the attributes.\r
41                                 addNamedItems( this._.attributes, rules.attributes, priority );\r
42 \r
43                                 // Add the text.\r
44                                 this._.text = transformNamedItem( this._.text, rules.text, priority ) || this._.text;\r
45 \r
46                                 // Add the comment.\r
47                                 this._.comment = transformNamedItem( this._.comment, rules.comment, priority ) || this._.comment;\r
48                         },\r
49 \r
50                         onElementName : function( name )\r
51                         {\r
52                                 return filterName( name, this._.elementNames );\r
53                         },\r
54 \r
55                         onAttributeName : function( name )\r
56                         {\r
57                                 return filterName( name, this._.attributeNames );\r
58                         },\r
59 \r
60                         onText : function( text )\r
61                         {\r
62                                 var textFilter = this._.text;\r
63                                 return textFilter ? textFilter.filter( text ) : text;\r
64                         },\r
65 \r
66                         onComment : function( commentText )\r
67                         {\r
68                                 var textFilter = this._.comment;\r
69                                 return textFilter ? textFilter.filter( commentText ) : commentText;\r
70                         },\r
71 \r
72                         onElement : function( element )\r
73                         {\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
78                                         filter, ret;\r
79 \r
80                                 for ( var i = 0 ; i < 2 ; i++ )\r
81                                 {\r
82                                         filter = filters[ i ];\r
83                                         if ( filter )\r
84                                         {\r
85                                                 ret = filter.filter( element, this );\r
86 \r
87                                                 if ( ret === false )\r
88                                                         return null;\r
89 \r
90                                                 if ( ret && ret != element )\r
91                                                         return this.onElement( ret );\r
92                                         }\r
93                                 }\r
94 \r
95                                 return element;\r
96                         },\r
97 \r
98                         onAttribute : function( element, name, value )\r
99                         {\r
100                                 var filter = this._.attributes[ name ];\r
101 \r
102                                 if ( filter )\r
103                                 {\r
104                                         var ret = filter.filter( value, element, this );\r
105 \r
106                                         if ( ret === false )\r
107                                                 return false;\r
108 \r
109                                         if ( typeof ret != 'undefined' )\r
110                                                 return ret;\r
111                                 }\r
112 \r
113                                 return value;\r
114                         }\r
115                 }\r
116         });\r
117 \r
118         function filterName( name, filters )\r
119         {\r
120                 for ( var i = 0 ; name && i < filters.length ; i++ )\r
121                 {\r
122                         var filter = filters[ i ];\r
123                         name = name.replace( filter[ 0 ], filter[ 1 ] );\r
124                 }\r
125                 return name;\r
126         }\r
127 \r
128         function addItemsToList( list, items, priority )\r
129         {\r
130                 var i, j,\r
131                         listLength = list.length,\r
132                         itemsLength = items && items.length;\r
133 \r
134                 if ( itemsLength )\r
135                 {\r
136                         // Find the index to insert the items at.\r
137                         for ( i = 0 ; i < listLength && list[ i ].pri < priority ; i++ )\r
138                         { /*jsl:pass*/ }\r
139 \r
140                         // Add all new items to the list at the specific index.\r
141                         for ( j = itemsLength - 1 ; j >= 0 ; j-- )\r
142                         {\r
143                                 var item = items[ j ];\r
144                                 item.pri = priority;\r
145                                 list.splice( i, 0, item );\r
146                         }\r
147                 }\r
148         }\r
149 \r
150         function addNamedItems( hashTable, items, priority )\r
151         {\r
152                 if ( items )\r
153                 {\r
154                         for ( var name in items )\r
155                         {\r
156                                 var current = hashTable[ name ];\r
157 \r
158                                 hashTable[ name ] =\r
159                                         transformNamedItem(\r
160                                                 current,\r
161                                                 items[ name ],\r
162                                                 priority );\r
163 \r
164                                 if ( !current )\r
165                                         hashTable.$length++;\r
166                         }\r
167                 }\r
168         }\r
169 \r
170         function transformNamedItem( current, item, priority )\r
171         {\r
172                 if ( item )\r
173                 {\r
174                         item.pri = priority;\r
175 \r
176                         if ( current )\r
177                         {\r
178                                 // If the current item is not an Array, transform it.\r
179                                 if ( !current.splice )\r
180                                 {\r
181                                         if ( current.pri > priority )\r
182                                                 current = [ item, current ];\r
183                                         else\r
184                                                 current = [ current, item ];\r
185 \r
186                                         current.filter = callItems;\r
187                                 }\r
188                                 else\r
189                                         addItemsToList( current, item, priority );\r
190 \r
191                                 return current;\r
192                         }\r
193                         else\r
194                         {\r
195                                 item.filter = item;\r
196                                 return item;\r
197                         }\r
198                 }\r
199         }\r
200 \r
201         function callItems( currentEntry )\r
202         {\r
203                 var isObject = ( typeof currentEntry == 'object' );\r
204 \r
205                 for ( var i = 0 ; i < this.length ; i++ )\r
206                 {\r
207                         var item = this[ i ],\r
208                                 ret = item.apply( window, arguments );\r
209 \r
210                         if ( typeof ret != 'undefined' )\r
211                         {\r
212                                 if ( ret === false )\r
213                                         return false;\r
214 \r
215                                 if ( isObject && ret != currentEntry )\r
216                                         return ret;\r
217                         }\r
218                 }\r
219 \r
220                 return null;\r
221         }\r
222 })();\r
223 \r
224 // "entities" plugin\r
225 /*\r
226 {\r
227         text : function( text )\r
228         {\r
229                 // TODO : Process entities.\r
230                 return text.toUpperCase();\r
231         }\r
232 };\r
233 */\r