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