2 Copyright (c) 2003-2010, 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 // Return the last non-space child node of the block (#4344).
\r
15 function lastNoneSpaceChild( block )
\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
24 function trimFillers( block, fromSource )
\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
29 // Also, any at the end of blocks are fillers, remove them as well.
\r
31 var children = block.children, lastChild = lastNoneSpaceChild( block );
\r
34 if ( ( fromSource || !CKEDITOR.env.ie ) && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' )
\r
36 if ( lastChild.type == CKEDITOR.NODE_TEXT && tailNbspRegex.test( lastChild.value ) )
\r
41 function blockNeedsExtension( block )
\r
43 var lastChild = lastNoneSpaceChild( block );
\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
52 function extendBlockForDisplay( block )
\r
54 trimFillers( block, true );
\r
56 if ( blockNeedsExtension( block ) )
\r
58 if ( CKEDITOR.env.ie )
\r
59 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
61 block.add( new CKEDITOR.htmlParser.element( 'br', {} ) );
\r
65 function extendBlockForOutput( block )
\r
67 trimFillers( block );
\r
69 if ( blockNeedsExtension( block ) )
\r
70 block.add( new CKEDITOR.htmlParser.text( '\xa0' ) );
\r
73 var dtd = CKEDITOR.dtd;
\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
79 if ( ! ( 'br' in dtd[i] ) )
\r
80 delete blockLikeTags[i];
\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
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
96 var defaultDataBlockFilterRules = { elements : {} };
\r
98 for ( i in blockLikeTags )
\r
99 defaultDataBlockFilterRules.elements[ i ] = extendBlockForDisplay;
\r
101 var defaultHtmlFilterRules =
\r
105 // Remove the "cke:" namespace prefix.
\r
106 [ ( /^cke:/ ), '' ],
\r
108 // Ignore <?xml:namespace> tags.
\r
109 [ ( /^\?xml:namespace$/ ), '' ]
\r
114 // Attributes saved for changes and protected attributes.
\r
115 [ ( /^_cke_(saved|pa)_/ ), '' ],
\r
117 // All "_cke" attributes are to be ignored.
\r
118 [ ( /^_cke.*/ ), '' ],
\r
120 [ 'hidefocus', '' ]
\r
125 $ : function( element )
\r
127 var attribs = element.attributes;
\r
131 // Elements marked as temporary are to be ignored.
\r
132 if ( attribs.cke_temp )
\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
140 savedAttributeName = '_cke_saved_' + attributeNames[ i ];
\r
141 savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] );
\r
148 embed : function( element )
\r
150 var parent = element.parent;
\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
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
162 // Restore param elements into self-closing.
\r
163 param : function( param )
\r
165 param.children = [];
\r
166 param.isEmpty = true;
\r
170 // Remove empty link but not empty anchor.(#3829)
\r
171 a : function( element )
\r
173 if ( !( element.children.length ||
\r
174 element.attributes.name ||
\r
175 element.attributes._cke_saved_name ) )
\r
181 html : function( element )
\r
183 delete element.attributes.contenteditable;
\r
184 delete element.attributes[ 'class' ];
\r
187 body : function( element )
\r
189 delete element.attributes.spellcheck;
\r
190 delete element.attributes.contenteditable;
\r
193 style : function( element )
\r
195 var child = element.children[ 0 ];
\r
196 child && child.value && ( child.value = CKEDITOR.tools.trim( child.value ));
\r
198 if ( !element.attributes.type )
\r
199 element.attributes.type = 'text/css';
\r
202 title : function( element )
\r
204 var titleText = element.children[ 0 ];
\r
205 titleText && ( titleText.value = element.attributes[ '_cke_title' ] || '' );
\r
211 'class' : function( value, element )
\r
213 // Remove all class names starting with "cke_".
\r
214 return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;
\r
218 comment : function( contents )
\r
220 // If this is a comment for protected source.
\r
221 if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )
\r
223 // Remove the extra marker for real comments from it.
\r
224 if ( contents.substr( protectedSourceMarker.length, 3 ) == '{C}' )
\r
225 contents = contents.substr( protectedSourceMarker.length + 3 );
\r
227 contents = contents.substr( protectedSourceMarker.length );
\r
229 return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents ) );
\r
236 var defaultHtmlBlockFilterRules = { elements : {} };
\r
238 for ( i in blockLikeTags )
\r
239 defaultHtmlBlockFilterRules.elements[ i ] = extendBlockForOutput;
\r
241 if ( CKEDITOR.env.ie )
\r
243 // IE outputs style attribute in capital letters. We should convert
\r
244 // them back to lower case.
\r
245 defaultHtmlFilterRules.attributes.style = function( value, element )
\r
247 return value.toLowerCase();
\r
251 function protectReadOnly( element )
\r
253 element.attributes.contenteditable = "false";
\r
255 function unprotectReadyOnly( element )
\r
257 delete element.attributes.contenteditable;
\r
259 // Disable form elements editing mode provided by some browers. (#5746)
\r
260 for ( i in { input : 1, textarea : 1 } )
\r
262 defaultDataFilterRules.elements[ i ] = protectReadOnly;
\r
263 defaultHtmlFilterRules.elements[ i ] = unprotectReadyOnly;
\r
266 var protectAttributeRegex = /<((?:a|area|img|input)[\s\S]*?\s)((href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))([^>]*)>/gi,
\r
267 findSavedSrcRegex = /\s_cke_saved_src\s*=/;
\r
269 var protectElementsRegex = /(?:<style(?=[ >])[^>]*>[\s\S]*<\/style>)|(?:<(:?link|meta|base)[^>]*>)/gi,
\r
270 encodedElementsRegex = /<cke:encoded>([^<]*)<\/cke:encoded>/gi;
\r
272 var protectElementNamesRegex = /(<\/?)((?:object|embed|param|html|body|head|title)[^>]*>)/gi,
\r
273 unprotectElementNamesRegex = /(<\/?)cke:((?:html|body|head|title)[^>]*>)/gi;
\r
275 var protectSelfClosingRegex = /<cke:(param|embed)([^>]*?)\/?>(?!\s*<\/cke:\1)/gi;
\r
277 function protectAttributes( html )
\r
279 return html.replace( protectAttributeRegex, function( tag, beginning, fullAttr, attrName, end )
\r
281 // We should not rewrite the _cke_saved_src attribute (#5218)
\r
282 if ( attrName == 'src' && findSavedSrcRegex.test( tag ) )
\r
285 return '<' + beginning + fullAttr + ' _cke_saved_' + fullAttr + end + '>';
\r
289 function protectElements( html )
\r
291 return html.replace( protectElementsRegex, function( match )
\r
293 return '<cke:encoded>' + encodeURIComponent( match ) + '</cke:encoded>';
\r
297 function unprotectElements( html )
\r
299 return html.replace( encodedElementsRegex, function( match, encoded )
\r
301 return decodeURIComponent( encoded );
\r
305 function protectElementsNames( html )
\r
307 return html.replace( protectElementNamesRegex, '$1cke:$2');
\r
310 function unprotectElementNames( html )
\r
312 return html.replace( unprotectElementNamesRegex, '$1$2' );
\r
315 function protectSelfClosingElements( html )
\r
317 return html.replace( protectSelfClosingRegex, '<cke:$1$2></cke:$1>' );
\r
320 function protectPreFormatted( html )
\r
322 return html.replace( /(<pre\b[^>]*>)(\r\n|\n)/g, '$1$2$2' );
\r
325 function protectRealComments( html )
\r
327 return html.replace( /<!--(?!{cke_protected})[\s\S]+?-->/g, function( match )
\r
329 return '<!--' + protectedSourceMarker +
\r
331 encodeURIComponent( match ).replace( /--/g, '%2D%2D' ) +
\r
336 function unprotectRealComments( html )
\r
338 return html.replace( /<!--\{cke_protected\}\{C\}([\s\S]+?)-->/g, function( match, data )
\r
340 return decodeURIComponent( data );
\r
344 function protectSource( data, protectRegexes )
\r
346 var protectedHtml = [],
\r
347 tempRegex = /<\!--\{cke_temp(comment)?\}(\d*?)-->/g;
\r
351 // Script tags will also be forced to be protected, otherwise
\r
352 // IE will execute them.
\r
353 ( /<script[\s\S]*?<\/script>/gi ),
\r
355 // <noscript> tags (get lost in IE and messed up in FF).
\r
356 /<noscript[\s\S]*?<\/noscript>/gi
\r
358 .concat( protectRegexes );
\r
360 // First of any other protection, we must protect all comments
\r
361 // to avoid loosing them (of course, IE related).
\r
362 // Note that we use a different tag for comments, as we need to
\r
363 // transform them when applying filters.
\r
364 data = data.replace( (/<!--[\s\S]*?-->/g), function( match )
\r
366 return '<!--{cke_tempcomment}' + ( protectedHtml.push( match ) - 1 ) + '-->';
\r
369 for ( var i = 0 ; i < regexes.length ; i++ )
\r
371 data = data.replace( regexes[i], function( match )
\r
373 match = match.replace( tempRegex, // There could be protected source inside another one. (#3869).
\r
374 function( $, isComment, id )
\r
376 return protectedHtml[ id ];
\r
379 return '<!--{cke_temp}' + ( protectedHtml.push( match ) - 1 ) + '-->';
\r
382 data = data.replace( tempRegex, function( $, isComment, id )
\r
384 return '<!--' + protectedSourceMarker +
\r
385 ( isComment ? '{C}' : '' ) +
\r
386 encodeURIComponent( protectedHtml[ id ] ).replace( /--/g, '%2D%2D' ) +
\r
393 CKEDITOR.plugins.add( 'htmldataprocessor',
\r
395 requires : [ 'htmlwriter' ],
\r
397 init : function( editor )
\r
399 var dataProcessor = editor.dataProcessor = new CKEDITOR.htmlDataProcessor( editor );
\r
401 dataProcessor.writer.forceSimpleAmpersand = editor.config.forceSimpleAmpersand;
\r
403 dataProcessor.dataFilter.addRules( defaultDataFilterRules );
\r
404 dataProcessor.dataFilter.addRules( defaultDataBlockFilterRules );
\r
405 dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules );
\r
406 dataProcessor.htmlFilter.addRules( defaultHtmlBlockFilterRules );
\r
410 CKEDITOR.htmlDataProcessor = function( editor )
\r
412 this.editor = editor;
\r
414 this.writer = new CKEDITOR.htmlWriter();
\r
415 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
416 this.htmlFilter = new CKEDITOR.htmlParser.filter();
\r
419 CKEDITOR.htmlDataProcessor.prototype =
\r
421 toHtml : function( data, fixForBody )
\r
423 // The source data is already HTML, but we need to clean
\r
424 // it up and apply the filter.
\r
426 data = protectSource( data, this.editor.config.protectedSource );
\r
428 // Before anything, we must protect the URL attributes as the
\r
429 // browser may changing them when setting the innerHTML later in
\r
431 data = protectAttributes( data );
\r
433 // Protect elements than can't be set inside a DIV. E.g. IE removes
\r
434 // style tags from innerHTML. (#3710)
\r
435 data = protectElements( data );
\r
437 // Certain elements has problem to go through DOM operation, protect
\r
438 // them by prefixing 'cke' namespace. (#3591)
\r
439 data = protectElementsNames( data );
\r
441 // All none-IE browsers ignore self-closed custom elements,
\r
442 // protecting them into open-close. (#3591)
\r
443 data = protectSelfClosingElements( data );
\r
445 // Compensate one leading line break after <pre> open as browsers
\r
446 // eat it up. (#5789)
\r
447 data = protectPreFormatted( data );
\r
449 // Call the browser to help us fixing a possibly invalid HTML
\r
451 var div = new CKEDITOR.dom.element( 'div' );
\r
452 // Add fake character to workaround IE comments bug. (#3801)
\r
453 div.setHtml( 'a' + data );
\r
454 data = div.getHtml().substr( 1 );
\r
456 // Unprotect "some" of the protected elements at this point.
\r
457 data = unprotectElementNames( data );
\r
459 data = unprotectElements( data );
\r
461 // Restore the comments that have been protected, in this way they
\r
462 // can be properly filtered.
\r
463 data = unprotectRealComments( data );
\r
465 // Now use our parser to make further fixes to the structure, as
\r
466 // well as apply the filter.
\r
467 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, fixForBody ),
\r
468 writer = new CKEDITOR.htmlParser.basicWriter();
\r
470 fragment.writeHtml( writer, this.dataFilter );
\r
471 data = writer.getHtml( true );
\r
473 // Protect the real comments again.
\r
474 data = protectRealComments( data );
\r
479 toDataFormat : function( html, fixForBody )
\r
481 var writer = this.writer,
\r
482 fragment = CKEDITOR.htmlParser.fragment.fromHtml( html, fixForBody );
\r
486 fragment.writeHtml( writer, this.htmlFilter );
\r
488 return writer.getHtml( true );
\r
494 * Whether to force using "&" instead of "&amp;" in elements attributes
\r
495 * values. It's not recommended to change this setting for compliance with the
\r
496 * W3C XHTML 1.0 standards
\r
497 * (<a href="http://www.w3.org/TR/xhtml1/#C_12">C.12, XHTML 1.0</a>).
\r
501 * config.forceSimpleAmpersand = false;
\r