X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fhtmldataprocessor%2Fplugin.js;h=ecf87ed45212310349d1f2696abad5e95bdd39b4;hb=2f22c0c38f17e75be5541089076885442aaa2377;hp=8a17828c4a45a0a9ef7bccc1e983b3cb32f414d0;hpb=f8fc585c18d287eb325c575596d183122486b641;p=ckeditor.git diff --git a/_source/plugins/htmldataprocessor/plugin.js b/_source/plugins/htmldataprocessor/plugin.js index 8a17828..ecf87ed 100644 --- a/_source/plugins/htmldataprocessor/plugin.js +++ b/_source/plugins/htmldataprocessor/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -38,40 +38,51 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } } - function blockNeedsExtension( block ) + function blockNeedsExtension( block, fromSource, extendEmptyBlock ) { + if( !fromSource && ( !extendEmptyBlock || + typeof extendEmptyBlock == 'function' && ( extendEmptyBlock( block ) === false ) ) ) + return false; + + // 1. For IE version >=8, empty blocks are displayed correctly themself in wysiwiyg; + // 2. For the rest, at least table cell and list item need no filler space. + // (#6248) + if ( fromSource && CKEDITOR.env.ie && + ( document.documentMode > 7 + || block.name in CKEDITOR.dtd.tr + || block.name in CKEDITOR.dtd.$listItem ) ) + return false; + var lastChild = lastNoneSpaceChild( block ); - return !lastChild - || lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' - // Some of the controls in form needs extension too, - // to move cursor at the end of the form. (#4791) - || block.name == 'form' && lastChild.name == 'input'; + return !lastChild || lastChild && + ( lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.name == 'br' + // Some of the controls in form needs extension too, + // to move cursor at the end of the form. (#4791) + || block.name == 'form' && lastChild.name == 'input' ); } - function extendBlockForDisplay( block ) + function getBlockExtension( isOutput, emptyBlockFiller ) { - trimFillers( block, true ); - - if ( blockNeedsExtension( block ) ) + return function( node ) { - if ( CKEDITOR.env.ie ) - block.add( new CKEDITOR.htmlParser.text( '\xa0' ) ); - else - block.add( new CKEDITOR.htmlParser.element( 'br', {} ) ); - } - } + trimFillers( node, !isOutput ); - function extendBlockForOutput( block ) - { - trimFillers( block ); - - if ( blockNeedsExtension( block ) ) - block.add( new CKEDITOR.htmlParser.text( '\xa0' ) ); + if ( blockNeedsExtension( node, !isOutput, emptyBlockFiller ) ) + { + if ( isOutput || CKEDITOR.env.ie ) + node.add( new CKEDITOR.htmlParser.text( '\xa0' ) ); + else + node.add( new CKEDITOR.htmlParser.element( 'br', {} ) ); + } + }; } var dtd = CKEDITOR.dtd; + // Define orders of table elements. + var tableOrder = [ 'caption', 'colgroup', 'col', 'thead', 'tfoot', 'tbody' ]; + // Find out the list of block-like tags that can contain
. var blockLikeTags = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ); for ( var i in blockLikeTags ) @@ -89,14 +100,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license [ // Event attributes (onXYZ) must not be directly set. They can become // active in the editing area (IE|WebKit). - [ ( /^on/ ), '_cke_pa_on' ] + [ ( /^on/ ), 'data-cke-pa-on' ] ] }; var defaultDataBlockFilterRules = { elements : {} }; for ( i in blockLikeTags ) - defaultDataBlockFilterRules.elements[ i ] = extendBlockForDisplay; + defaultDataBlockFilterRules.elements[ i ] = getBlockExtension(); var defaultHtmlFilterRules = { @@ -112,10 +123,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license attributeNames : [ // Attributes saved for changes and protected attributes. - [ ( /^_cke_(saved|pa)_/ ), '' ], + [ ( /^data-cke-(saved|pa)-/ ), '' ], - // All "_cke" attributes are to be ignored. - [ ( /^_cke.*/ ), '' ], + // All "data-cke-" attributes are to be ignored. + [ ( /^data-cke-.*/ ), '' ], [ 'hidefocus', '' ] ], @@ -129,7 +140,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( attribs ) { // Elements marked as temporary are to be ignored. - if ( attribs.cke_temp ) + if ( attribs[ 'data-cke-temp' ] ) return false; // Remove duplicated attributes - #3789. @@ -137,7 +148,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license savedAttributeName; for ( var i = 0 ; i < attributeNames.length ; i++ ) { - savedAttributeName = '_cke_saved_' + attributeNames[ i ]; + savedAttributeName = 'data-cke-saved-' + attributeNames[ i ]; savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] ); } } @@ -145,6 +156,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return element; }, + // The contents of table should be in correct order (#4809). + table : function( element ) + { + var children = element.children; + children.sort( function ( node1, node2 ) + { + return node1.type == CKEDITOR.NODE_ELEMENT && node2.type == node1.type ? + CKEDITOR.tools.indexOf( tableOrder, node1.name ) > CKEDITOR.tools.indexOf( tableOrder, node2.name ) ? 1 : -1 : 0; + } ); + }, + embed : function( element ) { var parent = element.parent; @@ -172,12 +194,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { if ( !( element.children.length || element.attributes.name || - element.attributes._cke_saved_name ) ) + element.attributes[ 'data-cke-saved-name' ] ) ) { return false; } }, + // Remove dummy span in webkit. + span: function( element ) + { + if ( element.attributes[ 'class' ] == 'Apple-style-span' ) + delete element.name; + }, + + // Empty
 in IE is reported with filler node ( ).
+				pre : function( element ) { CKEDITOR.env.ie && trimFillers( element ); },
+
+				html : function( element )
+				{
+					delete element.attributes.contenteditable;
+					delete element.attributes[ 'class' ];
+				},
+
 				body : function( element )
 				{
 					delete element.attributes.spellcheck;
@@ -195,7 +233,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 
 				title : function( element )
 				{
-					element.children[ 0 ].value = element.attributes[ '_cke_title' ];
+					var titleText = element.children[ 0 ];
+					titleText && ( titleText.value = element.attributes[ 'data-cke-title' ] || '' );
 				}
 			},
 
@@ -206,48 +245,41 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 					// Remove all class names starting with "cke_".
 					return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false;
 				}
-			},
-
-			comment : function( contents )
-			{
-				// If this is a comment for protected source.
-				if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker )
-				{
-					// Remove the extra marker for real comments from it.
-					if ( contents.substr( protectedSourceMarker.length, 3 ) == '{C}' )
-						contents = contents.substr( protectedSourceMarker.length + 3 );
-					else
-						contents = contents.substr( protectedSourceMarker.length );
-
-					return new CKEDITOR.htmlParser.cdata( decodeURIComponent( contents ) );
-				}
-
-				return contents;
 			}
 		};
 
-	var defaultHtmlBlockFilterRules = { elements : {} };
-
-	for ( i in blockLikeTags )
-		defaultHtmlBlockFilterRules.elements[ i ] = extendBlockForOutput;
-
 	if ( CKEDITOR.env.ie )
 	{
 		// IE outputs style attribute in capital letters. We should convert
-		// them back to lower case.
+		// them back to lower case, while not hurting the values (#5930)
 		defaultHtmlFilterRules.attributes.style = function( value, element )
 		{
-			return value.toLowerCase();
+			return value.replace( /(^|;)([^\:]+)/g, function( match )
+				{
+					return match.toLowerCase();
+				});
 		};
 	}
 
 	function protectReadOnly( element )
 	{
-		element.attributes.contenteditable = "false";
+		var attrs = element.attributes;
+
+		// We should flag that the element was locked by our code so
+		// it'll be editable by the editor functions (#6046).
+		if ( attrs.contenteditable != "false" )
+			attrs[ 'data-cke-editable' ] = attrs.contenteditable ? 'true' : 1;
+
+		attrs.contenteditable = "false";
 	}
 	function unprotectReadyOnly( element )
 	{
-		delete element.attributes.contenteditable;
+		var attrs = element.attributes;
+		switch( attrs[ 'data-cke-editable' ] )
+		{
+			case 'true':	attrs.contenteditable = 'true';	break;
+			case '1':		delete attrs.contenteditable;	break;
+		}
 	}
 	// Disable form elements editing mode provided by some browers. (#5746)
 	for ( i in { input : 1, textarea : 1 } )
@@ -256,7 +288,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 		defaultHtmlFilterRules.elements[ i ] = unprotectReadyOnly;
 	}
 
-	var protectAttributeRegex = /<(?:a|area|img|input)[\s\S]*?\s((?:href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+)))/gi;
+	var protectElementRegex = /<(a|area|img|input)\b([^>]*)>/gi,
+		protectAttributeRegex = /\b(on\w+|href|src|name)\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[^ "'>]+))/gi;
 
 	var protectElementsRegex = /(?:])[^>]*>[\s\S]*<\/style>)|(?:<(:?link|meta|base)[^>]*>)/gi,
 		encodedElementsRegex = /([^<]*)<\/cke:encoded>/gi;
@@ -268,7 +301,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 
 	function protectAttributes( html )
 	{
-		return html.replace( protectAttributeRegex, '$& _cke_saved_$1' );
+		return html.replace( protectElementRegex, function( element, tag, attributes )
+		{
+			return '<' +  tag + attributes.replace( protectAttributeRegex, function( fullAttr, attrName )
+			{
+				// Avoid corrupting the inline event attributes (#7243).
+				// We should not rewrite the existed protected attributes, e.g. clipboard content from editor. (#5218)
+				if ( !( /^on/ ).test( attrName ) && attributes.indexOf( 'data-cke-saved-' + attrName ) == -1 )
+					return ' data-cke-saved-' + fullAttr + ' data-cke-' + CKEDITOR.rnd + '-' + fullAttr;
+
+				return fullAttr;
+			}) + '>';
+		});
 	}
 
 	function protectElements( html )
@@ -302,6 +346,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 		return html.replace( protectSelfClosingRegex, '' );
 	}
 
+	function protectPreFormatted( html )
+	{
+		return html.replace( /(]*>)(\r\n|\n)/g, '$1$2$2' );
+	}
+
 	function protectRealComments( html )
 	{
 		return html.replace( //g, function( match )
@@ -321,9 +370,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 			});
 	}
 
-	function protectSource( data, protectRegexes )
+	function unprotectSource( html, editor )
+	{
+		var store = editor._.dataStore;
+
+		return html.replace( //g, function( match, data )
+			{
+				return decodeURIComponent( data );
+			}).replace( /\{cke_protected_(\d+)\}/g, function( match, id )
+			{
+				return store && store[ id ] || '';
+			});
+	}
+
+	function protectSource( data, editor )
 	{
 		var protectedHtml = [],
+			protectRegexes = editor.config.protectedSource,
+			store = editor._.dataStore || ( editor._.dataStore = { id : 1 } ),
 			tempRegex = /<\!--\{cke_temp(comment)?\}(\d*?)-->/g;
 
 		var regexes =
@@ -356,7 +420,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 							return protectedHtml[ id ];
 						}
 					);
-					return  '';
+
+					// Avoid protecting over protected, e.g. /\{.*?\}/
+					return ( /cke_temp(comment)?/ ).test( match ) ? match
+						: '';
 				});
 		}
 		data = data.replace( tempRegex,	function( $, isComment, id )
@@ -367,7 +434,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 						'-->';
 			}
 		);
-		return data;
+
+		// Different protection pattern is used for those that
+		// live in attributes to avoid from being HTML encoded.
+		return data.replace( /(['"]).*?\1/g, function ( match )
+		{
+			return match.replace( //g, function( match, data )
+			{
+				store[ store.id ] = decodeURIComponent( data );
+				return '{cke_protected_'+ ( store.id++ )  + '}';
+			});
+		});
 	}
 
 	CKEDITOR.plugins.add( 'htmldataprocessor',
@@ -383,7 +460,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 			dataProcessor.dataFilter.addRules( defaultDataFilterRules );
 			dataProcessor.dataFilter.addRules( defaultDataBlockFilterRules );
 			dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules );
+
+			var defaultHtmlBlockFilterRules = { elements : {} };
+			for ( i in blockLikeTags )
+				defaultHtmlBlockFilterRules.elements[ i ] = getBlockExtension( true, editor.config.fillEmptyBlocks );
+
 			dataProcessor.htmlFilter.addRules( defaultHtmlBlockFilterRules );
+		},
+
+		onLoad : function()
+		{
+			! ( 'fillEmptyBlocks' in CKEDITOR.config ) && ( CKEDITOR.config.fillEmptyBlocks = 1 );
 		}
 	});
 
@@ -403,7 +490,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 			// The source data is already HTML, but we need to clean
 			// it up and apply the filter.
 
-			data = protectSource( data, this.editor.config.protectedSource );
+			data = protectSource( data, this.editor );
 
 			// Before anything, we must protect the URL attributes as the
 			// browser may changing them when setting the innerHTML later in
@@ -422,13 +509,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 			// protecting them into open-close. (#3591)
 			data = protectSelfClosingElements( data );
 
+			// Compensate one leading line break after 
 open as browsers
+			// eat it up. (#5789)
+			data = protectPreFormatted( data );
+
 			// Call the browser to help us fixing a possibly invalid HTML
 			// structure.
 			var div = new CKEDITOR.dom.element( 'div' );
+
 			// Add fake character to workaround IE comments bug. (#3801)
 			div.setHtml( 'a' + data );
 			data = div.getHtml().substr( 1 );
 
+			// Restore shortly protected attribute names.
+			data = data.replace( new RegExp( ' data-cke-' + CKEDITOR.rnd + '-', 'ig' ), ' ' );
+
 			// Unprotect "some" of the protected elements at this point.
 			data = unprotectElementNames( data );
 
@@ -461,19 +556,45 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 
 			fragment.writeHtml( writer, this.htmlFilter );
 
-			return writer.getHtml( true );
+			var data = writer.getHtml( true );
+
+			// Restore those non-HTML protected source. (#4475,#4880)
+			data = unprotectRealComments( data );
+			data = unprotectSource( data, this.editor );
+
+			return data;
 		}
 	};
 })();
 
 /**
  * Whether to force using "&" instead of "&amp;" in elements attributes
- * values. It's not recommended to change this setting for compliance with the
- * W3C XHTML 1.0 standards
- * (C.12, XHTML 1.0).
+ * values, it's not recommended to change this setting for compliance with the
+ * W3C XHTML 1.0 standards (C.12, XHTML 1.0).
+ * @name CKEDITOR.config.forceSimpleAmpersand
+ * @name CKEDITOR.config.forceSimpleAmpersand
  * @type Boolean
  * @default false
  * @example
  * config.forceSimpleAmpersand = false;
  */
-CKEDITOR.config.forceSimpleAmpersand = false;
+
+/**
+ * Whether a filler text (non-breaking space entity -  ) will be inserted into empty block elements in HTML output,
+ * this is used to render block elements properly with line-height; When a function is instead specified,
+ * it'll be passed a {@link CKEDITOR.htmlParser.element} to decide whether adding the filler text
+ * by expecting a boolean return value.
+ * @name CKEDITOR.config.fillEmptyBlocks
+ * @since 3.5
+ * @type Boolean
+ * @default true
+ * @example
+ * config.fillEmptyBlocks = false;	// Prevent filler nodes in all empty blocks.
+ *
+ * // Prevent filler node only in float cleaners.
+ * config.fillEmptyBlocks = function( element )
+ * {
+ * 	if ( element.attributes[ 'class' ].indexOf ( 'clear-both' ) != -1 )
+ * 		return false;
+ * }
+ */