X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Fstyles%2Fplugin.js;h=40dc6d18b85420f3e53ae3ae57940b31bf96a5b0;hp=eade43b36e2ce5532d35b403e8c77e9d495a615e;hb=48b1db88210b4160dce439c6e3e32e14af8c106b;hpb=9afde8772159bd3436f1f5b7862960307710ae5a diff --git a/_source/plugins/styles/plugin.js b/_source/plugins/styles/plugin.js index eade43b..40dc6d1 100644 --- a/_source/plugins/styles/plugin.js +++ b/_source/plugins/styles/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -92,6 +92,8 @@ CKEDITOR.STYLE_OBJECT = 3; var semicolonFixRegex = /\s*(?:;\s*|$)/; + var notBookmark = CKEDITOR.dom.walker.bookmark( 0, 1 ); + CKEDITOR.style = function( styleDefinition, variablesValues ) { if ( variablesValues ) @@ -147,6 +149,8 @@ CKEDITOR.STYLE_OBJECT = 3; return ( this.removeFromRange = this.type == CKEDITOR.STYLE_INLINE ? removeInlineStyle + : this.type == CKEDITOR.STYLE_BLOCK ? + removeBlockStyle : this.type == CKEDITOR.STYLE_OBJECT ? removeObjectStyle : null ).call( this, range ); @@ -365,7 +369,7 @@ CKEDITOR.STYLE_OBJECT = 3; // Gets the parent element which blocks the styling for an element. This // can be done through read-only elements (contenteditable=false) or - // elements with the "data-cke-nostyle" attribute. + // elements with the "data-nostyle" attribute. function getUnstylableParent( element ) { var unstylable, @@ -376,7 +380,7 @@ CKEDITOR.STYLE_OBJECT = 3; if ( element.getName() == 'body' ) break; - if ( element.getAttribute( 'data-cke-nostyle' ) ) + if ( element.getAttribute( 'data-nostyle' ) ) unstylable = element; else if ( !editable ) { @@ -426,7 +430,7 @@ CKEDITOR.STYLE_OBJECT = 3; var dtd = CKEDITOR.dtd[ elementName ] || ( isUnknownElement = true, CKEDITOR.dtd.span ); // Expand the range. - range.enlarge( CKEDITOR.ENLARGE_ELEMENT ); + range.enlarge( CKEDITOR.ENLARGE_ELEMENT, 1 ); range.trim(); // Get the first node to be processed and the last, which concludes the @@ -471,7 +475,7 @@ CKEDITOR.STYLE_OBJECT = 3; var nodeType = currentNode.type; var nodeName = nodeType == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null; var nodeIsReadonly = nodeName && ( currentNode.getAttribute( 'contentEditable' ) == 'false' ); - var nodeIsNoStyle = nodeName && currentNode.getAttribute( 'data-cke-nostyle' ); + var nodeIsNoStyle = nodeName && currentNode.getAttribute( 'data-nostyle' ); if ( nodeName && currentNode.data( 'cke-bookmark' ) ) { @@ -514,8 +518,8 @@ CKEDITOR.STYLE_OBJECT = 3; // This node is about to be included completelly, but, // if this is the last node in its parent, we must also // check if the parent itself can be added completelly - // to the range. - while ( !includedNode.$.nextSibling + // to the range, otherwise apply the style immediately. + while ( ( applyStyle = !includedNode.getNext( notBookmark ) ) && ( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] ) && ( parentNode.getPosition( firstNode ) | CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED ) == ( CKEDITOR.POSITION_FOLLOWING + CKEDITOR.POSITION_IDENTICAL + CKEDITOR.POSITION_IS_CONTAINED ) && ( !def.childRule || def.childRule( parentNode ) ) ) @@ -525,11 +529,6 @@ CKEDITOR.STYLE_OBJECT = 3; styleRange.setEndAfter( includedNode ); - // If the included node still is the last node in its - // parent, it means that the parent can't be included - // in this style DTD, so apply the style immediately. - if ( !includedNode.$.nextSibling ) - applyStyle = true; } } else @@ -658,7 +657,7 @@ CKEDITOR.STYLE_OBJECT = 3; * Make sure our range has included all "collpased" parent inline nodes so * that our operation logic can be simpler. */ - range.enlarge( CKEDITOR.ENLARGE_ELEMENT ); + range.enlarge( CKEDITOR.ENLARGE_ELEMENT, 1 ); var bookmark = range.createBookmark(), startNode = bookmark.startNode; @@ -884,13 +883,55 @@ CKEDITOR.STYLE_OBJECT = 3; range.moveToBookmark( bookmark ); } + function removeBlockStyle( range ) + { + // Serializible bookmarks is needed here since + // elements may be merged. + var bookmark = range.createBookmark( 1 ); + + var iterator = range.createIterator(); + iterator.enforceRealBlocks = true; + iterator.enlargeBr = this._.enterMode != CKEDITOR.ENTER_BR; + + var block; + while ( ( block = iterator.getNextParagraph() ) ) + { + if ( this.checkElementRemovable( block ) ) + { + //
 get special treatment.
+				if ( block.is( 'pre' ) )
+				{
+					var newBlock = this._.enterMode == CKEDITOR.ENTER_BR ?
+								null : range.document.createElement(
+									this._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );
+
+					newBlock && block.copyAttributes( newBlock );
+					replaceBlock( block, newBlock );
+				}
+				else
+					 removeFromElement( this, block, 1 );
+			}
+		}
+
+		range.moveToBookmark( bookmark );
+	}
+
 	// Replace the original block with new one, with special treatment
 	// for 
 blocks to make sure content format is well preserved, and merging/splitting adjacent
 	// when necessary.(#3188)
 	function replaceBlock( block, newBlock )
 	{
-		var newBlockIsPre	= newBlock.is( 'pre' );
-		var blockIsPre		= block.is( 'pre' );
+		// Block is to be removed, create a temp element to
+		// save contents.
+		var removeBlock = !newBlock;
+		if ( removeBlock )
+		{
+			newBlock = block.getDocument().createElement( 'div' );
+			block.copyAttributes( newBlock );
+		}
+
+		var newBlockIsPre	= newBlock && newBlock.is( 'pre' );
+		var blockIsPre	= block.is( 'pre' );
 
 		var isToPre	= newBlockIsPre && !blockIsPre;
 		var isFromPre	= !newBlockIsPre && blockIsPre;
@@ -899,7 +940,8 @@ CKEDITOR.STYLE_OBJECT = 3;
 			newBlock = toPre( block, newBlock );
 		else if ( isFromPre )
 			// Split big 
 into pieces before start to convert.
-			newBlock = fromPres( splitIntoPres( block ), newBlock );
+			newBlock = fromPres( removeBlock ?
+						[ block.getHtml() ] : splitIntoPres( block ), newBlock );
 		else
 			block.moveChildren( newBlock );
 
@@ -910,9 +952,11 @@ CKEDITOR.STYLE_OBJECT = 3;
 			// Merge previous 
 blocks.
 			mergePre( newBlock );
 		}
+		else if ( removeBlock )
+			removeNoAttribsElement( newBlock );
 	}
 
-	var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( true );
+	var nonWhitespaces = CKEDITOR.dom.walker.whitespaces( 1 );
 	/**
 	 * Merge a 
 block with a previous sibling if available.
 	 */
@@ -986,7 +1030,10 @@ CKEDITOR.STYLE_OBJECT = 3;
 	 */
 	function fromPres( preHtmls, newBlock )
 	{
-		var docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );
+		var docFrag;
+		if ( preHtmls.length > 1 )
+			docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );
+
 		for ( var i = 0 ; i < preHtmls.length ; i++ )
 		{
 			var blockHtml = preHtmls[ i ];
@@ -1016,11 +1063,17 @@ CKEDITOR.STYLE_OBJECT = 3;
 						return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ' ;
 					} ) ;
 
-			var newBlockClone = newBlock.clone();
-			newBlockClone.setHtml(  blockHtml );
-			docFrag.append( newBlockClone );
+			if ( docFrag )
+			{
+				var newBlockClone = newBlock.clone();
+				newBlockClone.setHtml(  blockHtml );
+				docFrag.append( newBlockClone );
+			}
+			else
+				newBlock.setHtml( blockHtml );
 		}
-		return docFrag;
+
+		return docFrag || newBlock;
 	}
 
 	/**
@@ -1028,6 +1081,9 @@ CKEDITOR.STYLE_OBJECT = 3;
 	 */
 	function toPre( block, newBlock )
 	{
+		var bogus = block.getBogus();
+		bogus && bogus.remove();
+
 		// First trim the block content.
 		var preHtml = block.getHtml();
 
@@ -1050,6 +1106,7 @@ CKEDITOR.STYLE_OBJECT = 3;
 			var temp = block.getDocument().createElement( 'div' );
 			temp.append( newBlock );
 			newBlock.$.outerHTML =  '
' + preHtml + '
'; + newBlock.copyAttributes( temp.getFirst() ); newBlock = temp.getFirst().remove(); } else @@ -1089,7 +1146,12 @@ CKEDITOR.STYLE_OBJECT = 3; element.removeStyle( styleName ); } - removeEmpty && removeNoAttribsElement( element ); + if ( removeEmpty ) + { + !CKEDITOR.dtd.$block[ element.getName() ] || style._.enterMode == CKEDITOR.ENTER_BR && !element.hasAttributes() ? + removeNoAttribsElement( element ) : + element.renameNode( style._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ); + } } // Removes a style from inside an element. @@ -1166,21 +1228,37 @@ CKEDITOR.STYLE_OBJECT = 3; // leaving its children. if ( !element.hasAttributes() ) { - // Removing elements may open points where merging is possible, - // so let's cache the first and last nodes for later checking. - var firstChild = element.getFirst(); - var lastChild = element.getLast(); + if ( CKEDITOR.dtd.$block[ element.getName() ] ) + { + var previous = element.getPrevious( nonWhitespaces ), + next = element.getNext( nonWhitespaces ); - element.remove( true ); + if ( previous && ( previous.type == CKEDITOR.NODE_TEXT || !previous.isBlockBoundary( { br : 1 } ) ) ) + element.append( 'br', 1 ); + if ( next && ( next.type == CKEDITOR.NODE_TEXT || !next.isBlockBoundary( { br : 1 } ) ) ) + element.append( 'br' ); - if ( firstChild ) + element.remove( true ); + } + else { - // Check the cached nodes for merging. - firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings(); + // Removing elements may open points where merging is possible, + // so let's cache the first and last nodes for later checking. + var firstChild = element.getFirst(); + var lastChild = element.getLast(); + + element.remove( true ); + + if ( firstChild ) + { + // Check the cached nodes for merging. + firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings(); + + if ( lastChild && !firstChild.equals( lastChild ) + && lastChild.type == CKEDITOR.NODE_ELEMENT ) + lastChild.mergeSiblings(); + } - if ( lastChild && !firstChild.equals( lastChild ) - && lastChild.type == CKEDITOR.NODE_ELEMENT ) - lastChild.mergeSiblings(); } } } @@ -1204,7 +1282,15 @@ CKEDITOR.STYLE_OBJECT = 3; if ( element ) element.copyAttributes( el ); - return setupElement( el, style ); + el = setupElement( el, style ); + + // Avoid ID duplication. + if ( targetDocument.getCustomData( 'doc_processing_style' ) && el.hasAttribute( 'id' ) ) + el.removeAttribute( 'id' ); + else + targetDocument.setCustomData( 'doc_processing_style', 1 ); + + return el; } function setupElement( el, style ) @@ -1421,8 +1507,6 @@ CKEDITOR.STYLE_OBJECT = 3; function applyStyle( document, remove ) { var selection = document.getSelection(), - // Bookmark the range so we can re-select it after processing. - bookmarks = selection.createBookmarks( 1 ), ranges = selection.getRanges(), func = remove ? this.removeFromRange : this.applyToRange, range; @@ -1431,13 +1515,9 @@ CKEDITOR.STYLE_OBJECT = 3; while ( ( range = iterator.getNextRange() ) ) func.call( this, range ); - if ( bookmarks.length == 1 && bookmarks[0].collapsed ) - { - selection.selectRanges( ranges ); - document.getById( bookmarks[ 0 ].startNode ).remove(); - } - else - selection.selectBookmarks( bookmarks ); + selection.selectRanges( ranges ); + + document.removeCustomData( 'doc_processing_style' ); } })();