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 // 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
12 var protectedSourceMarker = '{cke_protected}';
\r
15 // Return the last non-space child node of the block (#4344).
\r
16 function lastNoneSpaceChild( block )
\r
18 var lastIndex = block.children.length,
\r
19 last = block.children[ lastIndex - 1 ];
\r
20 while( last && last.type == CKEDITOR.NODE_TEXT && !CKEDITOR.tools.trim( last.value ) )
\r
21 last = block.children[ --lastIndex ];
\r
25 function trimFillers( block, fromSource )
\r
27 // If the current node is a block, and if we're converting from source or
\r
28 // we're not in IE then search for and remove any tailing BR node.
\r
30 // Also, any at the end of blocks are fillers, remove them as well.
\r
32 var children = block.children, lastChild = lastNoneSpaceChild( block );
\r
35 if ( ( fromSource || !CKEDITOR.env.ie ) && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' )
\r
37 if ( lastChild.type == CKEDITOR.NODE_TEXT && tailNbspRegex.test( lastChild.value ) )
\r
42 function blockNeedsExtension( block )
\r
44 var lastChild = lastNoneSpaceChild( block );
\r
45 return !lastChild || lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br';
\r
48 function extendBlockForDisplay( block )
\r
50 trimFillers( block, true );
\r
52 if ( blockNeedsExtension( block ) )
\r
54 if ( CKEDITOR.env.ie )
\r
55 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
57 block.add( new CKEDITOR.htmlParser.element( 'br', {} ) );
\r
61 function extendBlockForOutput( block )
\r
63 trimFillers( block );
\r
65 if ( blockNeedsExtension( block ) )
\r
66 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
69 var dtd = CKEDITOR.dtd;
\r
71 // Find out the list of block-like tags that can contain <br>.
\r
72 var blockLikeTags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent );
\r
73 for ( var i in blockLikeTags )
\r
75 if ( ! ( 'br' in dtd[i] ) )
\r
76 delete blockLikeTags[i];
\r
78 // We just avoid filler in <pre> right now.
\r
79 // TODO: Support filler for <pre>, line break is also occupy line height.
\r
80 delete blockLikeTags.pre;
\r
81 var defaultDataFilterRules =
\r
85 // Event attributes (onXYZ) must not be directly set. They can become
\r
86 // active in the editing area (IE|WebKit).
\r
87 [ ( /^on/ ), '_cke_pa_on' ]
\r
91 var defaultDataBlockFilterRules = { elements : {} };
\r
93 for ( i in blockLikeTags )
\r
94 defaultDataBlockFilterRules.elements[ i ] = extendBlockForDisplay;
\r
96 var defaultHtmlFilterRules =
\r
100 // Remove the "cke:" namespace prefix.
\r
101 [ ( /^cke:/ ), '' ],
\r
103 // Ignore <?xml:namespace> tags.
\r
104 [ ( /^\?xml:namespace$/ ), '' ]
\r
109 // Attributes saved for changes and protected attributes.
\r
110 [ ( /^_cke_(saved|pa)_/ ), '' ],
\r
112 // All "_cke" attributes are to be ignored.
\r
113 [ ( /^_cke.*/ ), '' ]
\r
118 $ : function( element )
\r
120 // Remove duplicated attributes - #3789.
\r
121 var attribs = element.attributes;
\r
125 var attributeNames = [ 'name', 'href', 'src' ],
\r
126 savedAttributeName;
\r
127 for ( var i = 0 ; i < attributeNames.length ; i++ )
\r
129 savedAttributeName = '_cke_saved_' + attributeNames[ i ];
\r
130 savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] );
\r
135 embed : function( element )
\r
137 var parent = element.parent;
\r
139 // If the <embed> is child of a <object>, copy the width
\r
140 // and height attributes from it.
\r
141 if ( parent && parent.name == 'object' )
\r
143 var parentWidth = parent.attributes.width,
\r
144 parentHeight = parent.attributes.height;
\r
145 parentWidth && ( element.attributes.width = parentWidth );
\r
146 parentHeight && ( element.attributes.height = parentHeight );
\r
149 // Restore param elements into self-closing.
\r
150 param : function( param )
\r
152 param.children = [];
\r
153 param.isEmpty = true;
\r
157 // Remove empty link but not empty anchor.(#3829)
\r
158 a : function( element )
\r
160 if ( !( element.children.length ||
\r
161 element.attributes.name ||
\r
162 element.attributes._cke_saved_name ) )
\r
171 'class' : function( value, element )
\r
173 // Remove all class names starting with "cke_".
\r
174 return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;
\r
178 comment : function( contents )
\r
180 if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )
\r
181 return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents.substr( protectedSourceMarker.length ) ) );
\r
187 var defaultHtmlBlockFilterRules = { elements : {} };
\r
189 for ( i in blockLikeTags )
\r
190 defaultHtmlBlockFilterRules.elements[ i ] = extendBlockForOutput;
\r
192 if ( CKEDITOR.env.ie )
\r
194 // IE outputs style attribute in capital letters. We should convert
\r
195 // them back to lower case.
\r
196 defaultHtmlFilterRules.attributes.style = function( value, element )
\r
198 return value.toLowerCase();
\r
202 var protectAttributeRegex = /<(?:a|area|img|input).*?\s((?:href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))/gi;
\r
204 function protectAttributes( html )
\r
206 return html.replace( protectAttributeRegex, '$& _cke_saved_$1' );
\r
209 var protectStyleTagsRegex = /<(style)(?=[ >])[^>]*>[^<]*<\/\1>/gi;
\r
210 var encodedTagsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;
\r
211 var protectElementNamesRegex = /(<\/?)((?:object|embed|param).*?>)/gi;
\r
212 var protectSelfClosingRegex = /<cke:param(.*?)\/>/gi;
\r
214 function protectStyleTagsMatch( match )
\r
216 return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';
\r
219 function protectStyleTags( html )
\r
221 return html.replace( protectStyleTagsRegex, protectStyleTagsMatch );
\r
223 function protectElementsNames( html )
\r
225 return html.replace( protectElementNamesRegex, '$1cke:$2');
\r
227 function protectSelfClosingElements( html )
\r
229 return html.replace( protectSelfClosingRegex, '<cke:param$1></cke:param>' );
\r
232 function unprotectEncodedTagsMatch( match, encoded )
\r
234 return decodeURIComponent( encoded );
\r
237 function unprotectEncodedTags( html )
\r
239 return html.replace( encodedTagsRegex, unprotectEncodedTagsMatch );
\r
242 function protectSource( data, protectRegexes )
\r
244 var protectedHtml = [],
\r
245 tempRegex = /<\!--\{cke_temp\}(\d*?)-->/g;
\r
248 // First of any other protection, we must protect all comments
\r
249 // to avoid loosing them (of course, IE related).
\r
250 (/<!--[\s\S]*?-->/g),
\r
252 // Script tags will also be forced to be protected, otherwise
\r
253 // IE will execute them.
\r
254 /<script[\s\S]*?<\/script>/gi,
\r
256 // <noscript> tags (get lost in IE and messed up in FF).
\r
257 /<noscript[\s\S]*?<\/noscript>/gi
\r
259 .concat( protectRegexes );
\r
261 for ( var i = 0 ; i < regexes.length ; i++ )
\r
263 data = data.replace( regexes[i], function( match )
\r
265 match = match.replace( tempRegex, // There could be protected source inside another one. (#3869).
\r
268 return protectedHtml[ id ];
\r
271 return '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';
\r
274 data = data.replace( tempRegex, function( $, id )
\r
276 return '<!--' + protectedSourceMarker +
\r
277 encodeURIComponent( protectedHtml[ id ] ).replace( /--/g, '%2D%2D' ) +
\r
284 CKEDITOR.plugins.add( 'htmldataprocessor',
\r
286 requires : [ 'htmlwriter' ],
\r
288 init : function( editor )
\r
290 var dataProcessor = editor.dataProcessor = new CKEDITOR.htmlDataProcessor( editor );
\r
292 dataProcessor.writer.forceSimpleAmpersand = editor.config.forceSimpleAmpersand;
\r
294 dataProcessor.dataFilter.addRules( defaultDataFilterRules );
\r
295 dataProcessor.dataFilter.addRules( defaultDataBlockFilterRules );
\r
296 dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules );
\r
297 dataProcessor.htmlFilter.addRules( defaultHtmlBlockFilterRules );
\r
301 CKEDITOR.htmlDataProcessor = function( editor )
\r
303 this.editor = editor;
\r
305 this.writer = new CKEDITOR.htmlWriter();
\r
306 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
307 this.htmlFilter = new CKEDITOR.htmlParser.filter();
\r
310 CKEDITOR.htmlDataProcessor.prototype =
\r
312 toHtml : function( data, fixForBody )
\r
314 // The source data is already HTML, but we need to clean
\r
315 // it up and apply the filter.
\r
317 data = protectSource( data, this.editor.config.protectedSource );
\r
319 // Before anything, we must protect the URL attributes as the
\r
320 // browser may changing them when setting the innerHTML later in
\r
322 data = protectAttributes( data );
\r
324 // IE remvoes style tags from innerHTML. (#3710).
\r
325 if ( CKEDITOR.env.ie )
\r
326 data = protectStyleTags( data );
\r
328 // Certain elements has problem to go through DOM operation, protect
\r
329 // them by prefixing 'cke' namespace.(#3591)
\r
330 data = protectElementsNames( data );
\r
332 // All none-IE browsers ignore self-closed custom elements,
\r
333 // protecting them into open-close.(#3591)
\r
334 data = protectSelfClosingElements( data );
\r
336 // Call the browser to help us fixing a possibly invalid HTML
\r
338 var div = document.createElement( 'div' );
\r
339 // Add fake character to workaround IE comments bug. (#3801)
\r
340 div.innerHTML = 'a' + data;
\r
341 data = div.innerHTML.substr( 1 );
\r
343 if ( CKEDITOR.env.ie )
\r
344 data = unprotectEncodedTags( data );
\r
346 // Now use our parser to make further fixes to the structure, as
\r
347 // well as apply the filter.
\r
348 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, fixForBody ),
\r
349 writer = new CKEDITOR.htmlParser.basicWriter();
\r
351 fragment.writeHtml( writer, this.dataFilter );
\r
353 return writer.getHtml( true );
\r
356 toDataFormat : function( html, fixForBody )
\r
358 var writer = this.writer,
\r
359 fragment = CKEDITOR.htmlParser.fragment.fromHtml( html, fixForBody );
\r
363 fragment.writeHtml( writer, this.htmlFilter );
\r
365 return writer.getHtml( true );
\r
371 * Whether to force using "&" instead of "&amp;" in elements attributes
\r
372 * values. It's not recommended to change this setting for compliance with the
\r
373 * W3C XHTML 1.0 standards
\r
374 * (<a href="http://www.w3.org/TR/xhtml1/#C_12">C.12, XHTML 1.0</a>).
\r
378 * config.forceSimpleAmpersand = false;
\r
380 CKEDITOR.config.forceSimpleAmpersand = false;
\r