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