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
14 function trimFillers( block, fromSource )
\r
16 // If the current node is a block, and if we're converting from source or
\r
17 // we're not in IE then search for and remove any tailing BR node.
\r
19 // Also, any at the end of blocks are fillers, remove them as well.
\r
21 var children = block.children;
\r
22 var lastChild = children[ children.length - 1 ];
\r
25 if ( ( fromSource || !CKEDITOR.env.ie ) && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' )
\r
27 if ( lastChild.type == CKEDITOR.NODE_TEXT && tailNbspRegex.test( lastChild.value ) )
\r
32 function blockNeedsExtension( block )
\r
34 if ( block.children.length < 1 )
\r
37 var lastChild = block.children[ block.children.length - 1 ];
\r
38 return lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br';
\r
41 function extendBlockForDisplay( block )
\r
43 trimFillers( block, true );
\r
45 if ( blockNeedsExtension( block ) )
\r
47 if ( CKEDITOR.env.ie )
\r
48 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
50 block.add( new CKEDITOR.htmlParser.element( 'br', {} ) );
\r
54 function extendBlockForOutput( block )
\r
56 trimFillers( block );
\r
58 if ( blockNeedsExtension( block ) )
\r
59 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
62 var dtd = CKEDITOR.dtd;
\r
64 // Find out the list of block-like tags that can contain <br>.
\r
65 var blockLikeTags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent );
\r
66 for ( var i in blockLikeTags )
\r
68 if ( ! ( 'br' in dtd[i] ) )
\r
69 delete blockLikeTags[i];
\r
71 // We just avoid filler in <pre> right now.
\r
72 // TODO: Support filler for <pre>, line break is also occupy line height.
\r
73 delete blockLikeTags.pre;
\r
74 var defaultDataFilterRules =
\r
78 // Event attributes (onXYZ) must not be directly set. They can become
\r
79 // active in the editing area (IE|WebKit).
\r
80 [ ( /^on/ ), '_cke_pa_on' ]
\r
84 var defaultDataBlockFilterRules = { elements : {} };
\r
86 for ( i in blockLikeTags )
\r
87 defaultDataBlockFilterRules.elements[ i ] = extendBlockForDisplay;
\r
89 var defaultHtmlFilterRules =
\r
93 // Remove the "cke:" namespace prefix.
\r
94 [ ( /^cke:/ ), '' ],
\r
96 // Ignore <?xml:namespace> tags.
\r
97 [ ( /^\?xml:namespace$/ ), '' ]
\r
102 // Attributes saved for changes and protected attributes.
\r
103 [ ( /^_cke_(saved|pa)_/ ), '' ],
\r
105 // All "_cke" attributes are to be ignored.
\r
106 [ ( /^_cke.*/ ), '' ]
\r
111 $ : function( element )
\r
113 // Remove duplicated attributes - #3789.
\r
114 var attribs = element.attributes;
\r
118 var attributeNames = [ 'name', 'href', 'src' ],
\r
119 savedAttributeName;
\r
120 for ( var i = 0 ; i < attributeNames.length ; i++ )
\r
122 savedAttributeName = '_cke_saved_' + attributeNames[ i ];
\r
123 savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] );
\r
128 embed : function( element )
\r
130 var parent = element.parent;
\r
132 // If the <embed> is child of a <object>, copy the width
\r
133 // and height attributes from it.
\r
134 if ( parent && parent.name == 'object' )
\r
136 var parentWidth = parent.attributes.width,
\r
137 parentHeight = parent.attributes.height;
\r
138 parentWidth && ( element.attributes.width = parentWidth );
\r
139 parentHeight && ( element.attributes.height = parentHeight );
\r
142 // Restore param elements into self-closing.
\r
143 param : function( param )
\r
145 param.children = [];
\r
146 param.isEmpty = true;
\r
150 // Remove empty link but not empty anchor.(#3829)
\r
151 a : function( element )
\r
153 if ( !( element.children.length ||
\r
154 element.attributes.name ||
\r
155 element.attributes._cke_saved_name ) )
\r
164 'class' : function( value, element )
\r
166 // Remove all class names starting with "cke_".
\r
167 return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;
\r
171 comment : function( contents )
\r
173 if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )
\r
174 return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents.substr( protectedSourceMarker.length ) ) );
\r
180 var defaultHtmlBlockFilterRules = { elements : {} };
\r
182 for ( i in blockLikeTags )
\r
183 defaultHtmlBlockFilterRules.elements[ i ] = extendBlockForOutput;
\r
185 if ( CKEDITOR.env.ie )
\r
187 // IE outputs style attribute in capital letters. We should convert
\r
188 // them back to lower case.
\r
189 defaultHtmlFilterRules.attributes.style = function( value, element )
\r
191 return value.toLowerCase();
\r
195 var protectAttributeRegex = /<(?:a|area|img|input).*?\s((?:href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))/gi;
\r
197 function protectAttributes( html )
\r
199 return html.replace( protectAttributeRegex, '$& _cke_saved_$1' );
\r
202 var protectStyleTagsRegex = /<(style)(?=[ >])[^>]*>[^<]*<\/\1>/gi;
\r
203 var encodedTagsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;
\r
204 var protectElementNamesRegex = /(<\/?)((?:object|embed|param).*?>)/gi;
\r
205 var protectSelfClosingRegex = /<cke:param(.*?)\/>/gi;
\r
207 function protectStyleTagsMatch( match )
\r
209 return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';
\r
212 function protectStyleTags( html )
\r
214 return html.replace( protectStyleTagsRegex, protectStyleTagsMatch );
\r
216 function protectElementsNames( html )
\r
218 return html.replace( protectElementNamesRegex, '$1cke:$2');
\r
220 function protectSelfClosingElements( html )
\r
222 return html.replace( protectSelfClosingRegex, '<cke:param$1></cke:param>' );
\r
225 function unprotectEncodedTagsMatch( match, encoded )
\r
227 return decodeURIComponent( encoded );
\r
230 function unprotectEncodedTags( html )
\r
232 return html.replace( encodedTagsRegex, unprotectEncodedTagsMatch );
\r
235 function protectSource( data, protectRegexes )
\r
237 var protectedHtml = [],
\r
238 tempRegex = /<\!--\{cke_temp\}(\d*?)-->/g;
\r
241 // First of any other protection, we must protect all comments
\r
242 // to avoid loosing them (of course, IE related).
\r
243 (/<!--[\s\S]*?-->/g),
\r
245 // Script tags will also be forced to be protected, otherwise
\r
246 // IE will execute them.
\r
247 /<script[\s\S]*?<\/script>/gi,
\r
249 // <noscript> tags (get lost in IE and messed up in FF).
\r
250 /<noscript[\s\S]*?<\/noscript>/gi
\r
252 .concat( protectRegexes );
\r
254 for ( var i = 0 ; i < regexes.length ; i++ )
\r
256 data = data.replace( regexes[i], function( match )
\r
258 match = match.replace( tempRegex, // There could be protected source inside another one. (#3869).
\r
261 return protectedHtml[ id ];
\r
264 return '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';
\r
267 data = data.replace( tempRegex, function( $, id )
\r
269 return '<!--' + protectedSourceMarker +
\r
270 encodeURIComponent( protectedHtml[ id ] ).replace( /--/g, '%2D%2D' ) +
\r
277 CKEDITOR.plugins.add( 'htmldataprocessor',
\r
279 requires : [ 'htmlwriter' ],
\r
281 init : function( editor )
\r
283 var dataProcessor = editor.dataProcessor = new CKEDITOR.htmlDataProcessor( editor );
\r
285 dataProcessor.writer.forceSimpleAmpersand = editor.config.forceSimpleAmpersand;
\r
287 dataProcessor.dataFilter.addRules( defaultDataFilterRules );
\r
288 dataProcessor.dataFilter.addRules( defaultDataBlockFilterRules );
\r
289 dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules );
\r
290 dataProcessor.htmlFilter.addRules( defaultHtmlBlockFilterRules );
\r
294 CKEDITOR.htmlDataProcessor = function( editor )
\r
296 this.editor = editor;
\r
298 this.writer = new CKEDITOR.htmlWriter();
\r
299 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
300 this.htmlFilter = new CKEDITOR.htmlParser.filter();
\r
303 CKEDITOR.htmlDataProcessor.prototype =
\r
305 toHtml : function( data, fixForBody )
\r
307 // The source data is already HTML, but we need to clean
\r
308 // it up and apply the filter.
\r
310 data = protectSource( data, this.editor.config.protectedSource );
\r
312 // Before anything, we must protect the URL attributes as the
\r
313 // browser may changing them when setting the innerHTML later in
\r
315 data = protectAttributes( data );
\r
317 // IE remvoes style tags from innerHTML. (#3710).
\r
318 if ( CKEDITOR.env.ie )
\r
319 data = protectStyleTags( data );
\r
321 // Certain elements has problem to go through DOM operation, protect
\r
322 // them by prefixing 'cke' namespace.(#3591)
\r
323 data = protectElementsNames( data );
\r
325 // All none-IE browsers ignore self-closed custom elements,
\r
326 // protecting them into open-close.(#3591)
\r
327 data = protectSelfClosingElements( data );
\r
329 // Call the browser to help us fixing a possibly invalid HTML
\r
331 var div = document.createElement( 'div' );
\r
332 // Add fake character to workaround IE comments bug. (#3801)
\r
333 div.innerHTML = 'a' + data;
\r
334 data = div.innerHTML.substr( 1 );
\r
336 if ( CKEDITOR.env.ie )
\r
337 data = unprotectEncodedTags( data );
\r
339 // Now use our parser to make further fixes to the structure, as
\r
340 // well as apply the filter.
\r
341 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, fixForBody ),
\r
342 writer = new CKEDITOR.htmlParser.basicWriter();
\r
344 fragment.writeHtml( writer, this.dataFilter );
\r
346 return writer.getHtml( true );
\r
349 toDataFormat : function( html, fixForBody )
\r
351 var writer = this.writer,
\r
352 fragment = CKEDITOR.htmlParser.fragment.fromHtml( html, fixForBody );
\r
356 fragment.writeHtml( writer, this.htmlFilter );
\r
358 return writer.getHtml( true );
\r
364 * Whether to force using "&" instead of "&amp;" in elements attributes
\r
365 * values. It's not recommended to change this setting for compliance with the
\r
366 * W3C XHTML 1.0 standards
\r
367 * (<a href="http://www.w3.org/TR/xhtml1/#C_12">C.12, XHTML 1.0</a>).
\r
371 * config.forceSimpleAmpersand = false;
\r
373 CKEDITOR.config.forceSimpleAmpersand = false;
\r