JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.1
[ckeditor.git] / _source / plugins / htmldataprocessor / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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         // Regex to scan for   at the end of blocks, which are actually placeholders.\r
9         // Safari transforms the   to \xa0. (#4172)\r
10         var tailNbspRegex = /^[\t\r\n ]*(?: |\xa0)$/;\r
11 \r
12         var protectedSourceMarker = '{cke_protected}';\r
13 \r
14         // Return the last non-space child node of the block (#4344).\r
15         function lastNoneSpaceChild( block )\r
16         {\r
17                 var lastIndex = block.children.length,\r
18                         last = block.children[ lastIndex - 1 ];\r
19                 while (  last && last.type == CKEDITOR.NODE_TEXT && !CKEDITOR.tools.trim( last.value ) )\r
20                         last = block.children[ --lastIndex ];\r
21                 return last;\r
22         }\r
23 \r
24         function trimFillers( block, fromSource )\r
25         {\r
26                 // If the current node is a block, and if we're converting from source or\r
27                 // we're not in IE then search for and remove any tailing BR node.\r
28                 //\r
29                 // Also, any   at the end of blocks are fillers, remove them as well.\r
30                 // (#2886)\r
31                 var children = block.children, lastChild = lastNoneSpaceChild( block );\r
32                 if ( lastChild )\r
33                 {\r
34                         if ( ( fromSource || !CKEDITOR.env.ie ) && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' )\r
35                                 children.pop();\r
36                         if ( lastChild.type == CKEDITOR.NODE_TEXT && tailNbspRegex.test( lastChild.value ) )\r
37                                 children.pop();\r
38                 }\r
39         }\r
40 \r
41         function blockNeedsExtension( block )\r
42         {\r
43                 var lastChild = lastNoneSpaceChild( block );\r
44 \r
45                 return !lastChild\r
46                         || lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br'\r
47                         // Some of the controls in form needs extension too,\r
48                         // to move cursor at the end of the form. (#4791)\r
49                         || block.name == 'form' && lastChild.name == 'input';\r
50         }\r
51 \r
52         function extendBlockForDisplay( block )\r
53         {\r
54                 trimFillers( block, true );\r
55 \r
56                 if ( blockNeedsExtension( block ) )\r
57                 {\r
58                         if ( CKEDITOR.env.ie )\r
59                                 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );\r
60                         else\r
61                                 block.add( new CKEDITOR.htmlParser.element( 'br', {} ) );\r
62                 }\r
63         }\r
64 \r
65         function extendBlockForOutput( block )\r
66         {\r
67                 trimFillers( block );\r
68 \r
69                 if ( blockNeedsExtension( block ) )\r
70                         block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );\r
71         }\r
72 \r
73         var dtd = CKEDITOR.dtd;\r
74 \r
75         // Find out the list of block-like tags that can contain <br>.\r
76         var blockLikeTags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent );\r
77         for ( var i in blockLikeTags )\r
78         {\r
79                 if ( ! ( 'br' in dtd[i] ) )\r
80                         delete blockLikeTags[i];\r
81         }\r
82         // We just avoid filler in <pre> right now.\r
83         // TODO: Support filler for <pre>, line break is also occupy line height.\r
84         delete blockLikeTags.pre;\r
85         var defaultDataFilterRules =\r
86         {\r
87                 attributeNames :\r
88                 [\r
89                         // Event attributes (onXYZ) must not be directly set. They can become\r
90                         // active in the editing area (IE|WebKit).\r
91                         [ ( /^on/ ), '_cke_pa_on' ]\r
92                 ]\r
93         };\r
94 \r
95         var defaultDataBlockFilterRules = { elements : {} };\r
96 \r
97         for ( i in blockLikeTags )\r
98                 defaultDataBlockFilterRules.elements[ i ] = extendBlockForDisplay;\r
99 \r
100         var defaultHtmlFilterRules =\r
101                 {\r
102                         elementNames :\r
103                         [\r
104                                 // Remove the "cke:" namespace prefix.\r
105                                 [ ( /^cke:/ ), '' ],\r
106 \r
107                                 // Ignore <?xml:namespace> tags.\r
108                                 [ ( /^\?xml:namespace$/ ), '' ]\r
109                         ],\r
110 \r
111                         attributeNames :\r
112                         [\r
113                                 // Attributes saved for changes and protected attributes.\r
114                                 [ ( /^_cke_(saved|pa)_/ ), '' ],\r
115 \r
116                                 // All "_cke" attributes are to be ignored.\r
117                                 [ ( /^_cke.*/ ), '' ],\r
118 \r
119                                 [ 'hidefocus', '' ]\r
120                         ],\r
121 \r
122                         elements :\r
123                         {\r
124                                 $ : function( element )\r
125                                 {\r
126                                         var attribs = element.attributes;\r
127 \r
128                                         if ( attribs )\r
129                                         {\r
130                                                 // Elements marked as temporary are to be ignored.\r
131                                                 if ( attribs.cke_temp )\r
132                                                         return false;\r
133 \r
134                                                 // Remove duplicated attributes - #3789.\r
135                                                 var attributeNames = [ 'name', 'href', 'src' ],\r
136                                                         savedAttributeName;\r
137                                                 for ( var i = 0 ; i < attributeNames.length ; i++ )\r
138                                                 {\r
139                                                         savedAttributeName = '_cke_saved_' + attributeNames[ i ];\r
140                                                         savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] );\r
141                                                 }\r
142                                         }\r
143 \r
144                                         return element;\r
145                                 },\r
146 \r
147                                 embed : function( element )\r
148                                 {\r
149                                         var parent = element.parent;\r
150 \r
151                                         // If the <embed> is child of a <object>, copy the width\r
152                                         // and height attributes from it.\r
153                                         if ( parent && parent.name == 'object' )\r
154                                         {\r
155                                                 var parentWidth = parent.attributes.width,\r
156                                                         parentHeight = parent.attributes.height;\r
157                                                 parentWidth && ( element.attributes.width = parentWidth );\r
158                                                 parentHeight && ( element.attributes.height = parentHeight );\r
159                                         }\r
160                                 },\r
161                                 // Restore param elements into self-closing.\r
162                                 param : function( param )\r
163                                 {\r
164                                         param.children = [];\r
165                                         param.isEmpty = true;\r
166                                         return param;\r
167                                 },\r
168 \r
169                                 // Remove empty link but not empty anchor.(#3829)\r
170                                 a : function( element )\r
171                                 {\r
172                                         if ( !( element.children.length ||\r
173                                                         element.attributes.name ||\r
174                                                         element.attributes._cke_saved_name ) )\r
175                                         {\r
176                                                 return false;\r
177                                         }\r
178                                 },\r
179 \r
180                                 body : function( element )\r
181                                 {\r
182                                         delete element.attributes.spellcheck;\r
183                                         delete element.attributes.contenteditable;\r
184                                 },\r
185 \r
186                                 style : function( element )\r
187                                 {\r
188                                         var child = element.children[ 0 ];\r
189                                         child && child.value && ( child.value = CKEDITOR.tools.trim( child.value ));\r
190 \r
191                                         if ( !element.attributes.type )\r
192                                                 element.attributes.type = 'text/css';\r
193                                 },\r
194 \r
195                                 title : function( element )\r
196                                 {\r
197                                         element.children[ 0 ].value = element.attributes[ '_cke_title' ];\r
198                                 }\r
199                         },\r
200 \r
201                         attributes :\r
202                         {\r
203                                 'class' : function( value, element )\r
204                                 {\r
205                                         // Remove all class names starting with "cke_".\r
206                                         return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;\r
207                                 }\r
208                         },\r
209 \r
210                         comment : function( contents )\r
211                         {\r
212                                 // If this is a comment for protected source.\r
213                                 if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )\r
214                                 {\r
215                                         // Remove the extra marker for real comments from it.\r
216                                         if ( contents.substr( protectedSourceMarker.length, 3 ) == '{C}' )\r
217                                                 contents = contents.substr( protectedSourceMarker.length + 3 );\r
218                                         else\r
219                                                 contents = contents.substr( protectedSourceMarker.length );\r
220 \r
221                                         return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents ) );\r
222                                 }\r
223 \r
224                                 return contents;\r
225                         }\r
226                 };\r
227 \r
228         var defaultHtmlBlockFilterRules = { elements : {} };\r
229 \r
230         for ( i in blockLikeTags )\r
231                 defaultHtmlBlockFilterRules.elements[ i ] = extendBlockForOutput;\r
232 \r
233         if ( CKEDITOR.env.ie )\r
234         {\r
235                 // IE outputs style attribute in capital letters. We should convert\r
236                 // them back to lower case.\r
237                 defaultHtmlFilterRules.attributes.style = function( value, element )\r
238                 {\r
239                         return value.toLowerCase();\r
240                 };\r
241         }\r
242 \r
243         var protectAttributeRegex = /<(?:a|area|img|input)[\s\S]*?\s((?:href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))/gi;\r
244 \r
245         var protectElementsRegex = /(?:<style(?=[ >])[^>]*>[\s\S]*<\/style>)|(?:<(:?link|meta|base)[^>]*>)/gi,\r
246                 encodedElementsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;\r
247 \r
248         var protectElementNamesRegex = /(<\/?)((?:object|embed|param|html|body|head|title)[^>]*>)/gi,\r
249                 unprotectElementNamesRegex = /(<\/?)cke:((?:html|body|head|title)[^>]*>)/gi;\r
250 \r
251         var protectSelfClosingRegex = /<cke:(param|embed)([^>]*?)\/?>(?!\s*<\/cke:\1)/gi;\r
252 \r
253         function protectAttributes( html )\r
254         {\r
255                 return html.replace( protectAttributeRegex, '$& _cke_saved_$1' );\r
256         }\r
257 \r
258         function protectElements( html )\r
259         {\r
260                 return html.replace( protectElementsRegex, function( match )\r
261                         {\r
262                                 return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';\r
263                         });\r
264         }\r
265 \r
266         function unprotectElements( html )\r
267         {\r
268                 return html.replace( encodedElementsRegex, function( match, encoded )\r
269                         {\r
270                                 return decodeURIComponent( encoded );\r
271                         });\r
272         }\r
273 \r
274         function protectElementsNames( html )\r
275         {\r
276                 return html.replace( protectElementNamesRegex, '$1cke:$2');\r
277         }\r
278 \r
279         function unprotectElementNames( html )\r
280         {\r
281                 return html.replace( unprotectElementNamesRegex, '$1$2' );\r
282         }\r
283 \r
284         function protectSelfClosingElements( html )\r
285         {\r
286                 return html.replace( protectSelfClosingRegex, '<cke:$1$2></cke:$1>' );\r
287         }\r
288 \r
289         function protectRealComments( html )\r
290         {\r
291                 return html.replace( /<!--(?!{cke_protected})[\s\S]+?-->/g, function( match )\r
292                         {\r
293                                 return '<!--' + protectedSourceMarker +\r
294                                                 '{C}' +\r
295                                                 encodeURIComponent( match ).replace( /--/g, '%2D%2D' ) +\r
296                                                 '-->';\r
297                         });\r
298         }\r
299 \r
300         function unprotectRealComments( html )\r
301         {\r
302                 return html.replace( /<!--\{cke_protected\}\{C\}([\s\S]+?)-->/g, function( match, data )\r
303                         {\r
304                                 return decodeURIComponent( data );\r
305                         });\r
306         }\r
307 \r
308         function protectSource( data, protectRegexes )\r
309         {\r
310                 var protectedHtml = [],\r
311                         tempRegex = /<\!--\{cke_temp(comment)?\}(\d*?)-->/g;\r
312 \r
313                 var regexes =\r
314                         [\r
315                                 // Script tags will also be forced to be protected, otherwise\r
316                                 // IE will execute them.\r
317                                 ( /<script[\s\S]*?<\/script>/gi ),\r
318 \r
319                                 // <noscript> tags (get lost in IE and messed up in FF).\r
320                                 /<noscript[\s\S]*?<\/noscript>/gi\r
321                         ]\r
322                         .concat( protectRegexes );\r
323 \r
324                 // First of any other protection, we must protect all comments\r
325                 // to avoid loosing them (of course, IE related).\r
326                 // Note that we use a different tag for comments, as we need to\r
327                 // transform them when applying filters.\r
328                 data = data.replace( (/<!--[\s\S]*?-->/g), function( match )\r
329                         {\r
330                                 return  '<!--{cke_tempcomment}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
331                         });\r
332 \r
333                 for ( var i = 0 ; i < regexes.length ; i++ )\r
334                 {\r
335                         data = data.replace( regexes[i], function( match )\r
336                                 {\r
337                                         match = match.replace( tempRegex,               // There could be protected source inside another one. (#3869).\r
338                                                 function( $, isComment, id )\r
339                                                 {\r
340                                                         return protectedHtml[ id ];\r
341                                                 }\r
342                                         );\r
343                                         return  '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';\r
344                                 });\r
345                 }\r
346                 data = data.replace( tempRegex, function( $, isComment, id )\r
347                         {\r
348                                 return '<!--' + protectedSourceMarker +\r
349                                                 ( isComment ? '{C}' : '' ) +\r
350                                                 encodeURIComponent( protectedHtml[ id ] ).replace( /--/g, '%2D%2D' ) +\r
351                                                 '-->';\r
352                         }\r
353                 );\r
354                 return data;\r
355         }\r
356 \r
357         CKEDITOR.plugins.add( 'htmldataprocessor',\r
358         {\r
359                 requires : [ 'htmlwriter' ],\r
360 \r
361                 init : function( editor )\r
362                 {\r
363                         var dataProcessor = editor.dataProcessor = new CKEDITOR.htmlDataProcessor( editor );\r
364 \r
365                         dataProcessor.writer.forceSimpleAmpersand = editor.config.forceSimpleAmpersand;\r
366 \r
367                         dataProcessor.dataFilter.addRules( defaultDataFilterRules );\r
368                         dataProcessor.dataFilter.addRules( defaultDataBlockFilterRules );\r
369                         dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules );\r
370                         dataProcessor.htmlFilter.addRules( defaultHtmlBlockFilterRules );\r
371                 }\r
372         });\r
373 \r
374         CKEDITOR.htmlDataProcessor = function( editor )\r
375         {\r
376                 this.editor = editor;\r
377 \r
378                 this.writer = new CKEDITOR.htmlWriter();\r
379                 this.dataFilter = new CKEDITOR.htmlParser.filter();\r
380                 this.htmlFilter = new CKEDITOR.htmlParser.filter();\r
381         };\r
382 \r
383         CKEDITOR.htmlDataProcessor.prototype =\r
384         {\r
385                 toHtml : function( data, fixForBody )\r
386                 {\r
387                         // The source data is already HTML, but we need to clean\r
388                         // it up and apply the filter.\r
389 \r
390                         data = protectSource( data, this.editor.config.protectedSource );\r
391 \r
392                         // Before anything, we must protect the URL attributes as the\r
393                         // browser may changing them when setting the innerHTML later in\r
394                         // the code.\r
395                         data = protectAttributes( data );\r
396 \r
397                         // Protect elements than can't be set inside a DIV. E.g. IE removes\r
398                         // style tags from innerHTML. (#3710)\r
399                         data = protectElements( data );\r
400 \r
401                         // Certain elements has problem to go through DOM operation, protect\r
402                         // them by prefixing 'cke' namespace. (#3591)\r
403                         data = protectElementsNames( data );\r
404 \r
405                         // All none-IE browsers ignore self-closed custom elements,\r
406                         // protecting them into open-close. (#3591)\r
407                         data = protectSelfClosingElements( data );\r
408 \r
409                         // Call the browser to help us fixing a possibly invalid HTML\r
410                         // structure.\r
411                         var div = new CKEDITOR.dom.element( 'div' );\r
412                         // Add fake character to workaround IE comments bug. (#3801)\r
413                         div.setHtml( 'a' + data );\r
414                         data = div.getHtml().substr( 1 );\r
415 \r
416                         // Unprotect "some" of the protected elements at this point.\r
417                         data = unprotectElementNames( data );\r
418 \r
419                         data = unprotectElements( data );\r
420 \r
421                         // Restore the comments that have been protected, in this way they\r
422                         // can be properly filtered.\r
423                         data = unprotectRealComments( data );\r
424 \r
425                         // Now use our parser to make further fixes to the structure, as\r
426                         // well as apply the filter.\r
427                         var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, fixForBody ),\r
428                                 writer = new CKEDITOR.htmlParser.basicWriter();\r
429 \r
430                         fragment.writeHtml( writer, this.dataFilter );\r
431                         data = writer.getHtml( true );\r
432 \r
433                         // Protect the real comments again.\r
434                         data = protectRealComments( data );\r
435 \r
436                         return data;\r
437                 },\r
438 \r
439                 toDataFormat : function( html, fixForBody )\r
440                 {\r
441                         var writer = this.writer,\r
442                                 fragment = CKEDITOR.htmlParser.fragment.fromHtml( html, fixForBody );\r
443 \r
444                         writer.reset();\r
445 \r
446                         fragment.writeHtml( writer, this.htmlFilter );\r
447 \r
448                         return writer.getHtml( true );\r
449                 }\r
450         };\r
451 })();\r
452 \r
453 /**\r
454  * Whether to force using "&" instead of "&amp;amp;" in elements attributes\r
455  * values. It's not recommended to change this setting for compliance with the\r
456  * W3C XHTML 1.0 standards\r
457  * (<a href="http://www.w3.org/TR/xhtml1/#C_12">C.12, XHTML 1.0</a>).\r
458  * @type Boolean\r
459  * @default false\r
460  * @example\r
461  * config.forceSimpleAmpersand = false;\r
462  */\r
463 CKEDITOR.config.forceSimpleAmpersand = false;\r