X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fcore%2Fhtmlparser%2Ffragment.js;h=6c7686c228eb81ec803292dc597992a9bfabe414;hp=66bb3be0e9f8d848f29e8744e630a5aca630a400;hb=941b0a9ba4e673e292510d80a5a86806994b8ea6;hpb=7cd80714081a8ffdf4a1a8d2c72f120ed5ef3d6d diff --git a/_source/core/htmlparser/fragment.js b/_source/core/htmlparser/fragment.js index 66bb3be..6c7686c 100644 --- a/_source/core/htmlparser/fragment.js +++ b/_source/core/htmlparser/fragment.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -114,7 +114,9 @@ CKEDITOR.htmlParser.fragment = function() elementName = realElementName; else elementName = element.name; - if ( !( elementName in CKEDITOR.dtd.$body ) ) + if ( elementName + && !( elementName in CKEDITOR.dtd.$body ) + && !( elementName in CKEDITOR.dtd.$nonBodyContent ) ) { var savedCurrent = currentNode; @@ -179,32 +181,32 @@ CKEDITOR.htmlParser.fragment = function() return; } - var currentName = currentNode.name, - currentDtd = ( currentName && CKEDITOR.dtd[ currentName ] ) || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ); + var currentName = currentNode.name; + + var currentDtd = currentName + && ( CKEDITOR.dtd[ currentName ] + || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) ); // If the element cannot be child of the current element. - if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] ) + if ( currentDtd // Fragment could receive any elements. + && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] ) { - // If this is the fragment node, just ignore this tag and add - // its children. - if ( !currentName ) - return; var reApply = false, addPoint; // New position to start adding nodes. - // Fixing malformed nested lists(#3828). + // Fixing malformed nested lists by moving it into a previous list item. (#3828) if( tagName in listBlocks && currentName in listBlocks ) { var children = currentNode.children, lastChild = children[ children.length - 1 ]; - // Move inner list into to previous list item if any. - if( lastChild && lastChild.name in listItems ) - returnPoint = currentNode, addPoint = lastChild; - // Move inner list outside in the worst case. - else - addElement( currentNode, currentNode.parent ); + + // Establish the list item if it's not existed. + if ( !( lastChild && lastChild.name in listItems ) ) + addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode ); + + returnPoint = currentNode, addPoint = lastChild; } // If the element name is the same as the current element name, // then just close the current one and append the new one to the @@ -276,14 +278,16 @@ CKEDITOR.htmlParser.fragment = function() } var pendingAdd = [], + newPendingInline = [], candidate = currentNode; while ( candidate.type && candidate.name != tagName ) { - // If this is an inline element, add it to the pending list, so - // it will continue after the closing tag. + // If this is an inline element, add it to the pending list, if we're + // really closing one of the parents element later, they will continue + // after it. if ( !candidate._.isBlockLike ) - pendingInline.unshift( candidate ); + newPendingInline.unshift( candidate ); // This node should be added to it's parent at this point. But, // it should happen only if the closing tag is really closing @@ -313,7 +317,12 @@ CKEDITOR.htmlParser.fragment = function() // addElement changed the currentNode. if ( candidate == currentNode ) currentNode = currentNode.parent; + + pendingInline = pendingInline.concat( newPendingInline ); } + + if( tagName == 'body' ) + fixForBody = false; }; parser.onText = function( text ) @@ -329,8 +338,12 @@ CKEDITOR.htmlParser.fragment = function() checkPending(); - if ( fixForBody && !currentNode.type ) + if ( fixForBody + && ( !currentNode.type || currentNode.name == 'body' ) + && CKEDITOR.tools.trim( text ) ) + { this.onTagOpen( fixForBody, {} ); + } // Shrinking consequential spaces into one single for all elements // text contents. @@ -359,7 +372,9 @@ CKEDITOR.htmlParser.fragment = function() var parent = currentNode.parent, node = currentNode; - if ( fixForBody && !parent.type && !CKEDITOR.dtd.$body[ node.name ] ) + if ( fixForBody + && ( !parent.type || parent.name == 'body' ) + && !CKEDITOR.dtd.$body[ node.name ] ) { currentNode = parent; parser.onTagOpen( fixForBody, {} ); @@ -428,7 +443,25 @@ CKEDITOR.htmlParser.fragment = function() */ writeHtml : function( writer, filter ) { - for ( var i = 0, len = this.children.length ; i < len ; i++ ) + var isChildrenFiltered; + this.filterChildren = function() + { + var writer = new CKEDITOR.htmlParser.basicWriter(); + this.writeChildrenHtml.call( this, writer, filter, true ); + var html = writer.getHtml(); + this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children; + isChildrenFiltered = 1; + }; + + // Filtering the root fragment before anything else. + !this.name && filter && filter.onFragment( this ); + + this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter ); + }, + + writeChildrenHtml : function( writer, filter ) + { + for ( var i = 0 ; i < this.children.length ; i++ ) this.children[i].writeHtml( writer, filter ); } };