X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fhtmlparser%2Ffragment.js;h=8c3ad5d3b960f98564078c2e139cfc93b6280f91;hb=refs%2Ftags%2Fv3.5.3;hp=7912569d05a86e5740713b79cc4238acd5dd152e;hpb=9afde8772159bd3436f1f5b7862960307710ae5a;p=ckeditor.git diff --git a/_source/core/htmlparser/fragment.js b/_source/core/htmlparser/fragment.js index 7912569..8c3ad5d 100644 --- a/_source/core/htmlparser/fragment.js +++ b/_source/core/htmlparser/fragment.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 */ @@ -37,38 +37,35 @@ CKEDITOR.htmlParser.fragment = function() (function() { - // Elements which the end tag is marked as optional in the HTML 4.01 DTD - // (expect empty elements). - var optionalClose = {colgroup:1,dd:1,dt:1,li:1,option:1,p:1,td:1,tfoot:1,th:1,thead:1,tr:1}; - // Block-level elements whose internal structure should be respected during // parser fixing. - var nonBreakingBlocks = CKEDITOR.tools.extend( - {table:1,ul:1,ol:1,dl:1}, - CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl ), - listBlocks = CKEDITOR.dtd.$list, listItems = CKEDITOR.dtd.$listItem; + var nonBreakingBlocks = CKEDITOR.tools.extend( { table:1,ul:1,ol:1,dl:1 }, CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl ); + + var listBlocks = { ol:1, ul:1 }; + + // Dtd of the fragment element, basically it accept anything except for intermediate structure, e.g. orphan
element, spaces should be touched differently. - inPre = false, - returnPoint; + inPre = false; function checkPending( newTagName ) { @@ -108,19 +105,37 @@ CKEDITOR.htmlParser.fragment = function() } } - function sendPendingBRs( brsToIgnore ) + function sendPendingBRs() { - while ( pendingBRs.length - ( brsToIgnore || 0 ) > 0 ) + while ( pendingBRs.length ) currentNode.add( pendingBRs.shift() ); } - function addElement( element, target, enforceCurrent ) + /* + * Beside of simply append specified element to target, this function also takes + * care of other dirty lifts like forcing block in body, trimming spaces at + * the block boundaries etc. + * + * @param {Element} element The element to be added as the last child of {@link target}. + * @param {Element} target The parent element to relieve the new node. + * @param {Boolean} [moveCurrent=false] Don't change the "currentNode" global unless + * there's a return point node specified on the element, otherwise move current onto {@link target} node. + */ + function addElement( element, target, moveCurrent ) { + // Ignore any element that has already been added. + if ( element.previous !== undefined ) + return; + target = target || currentNode || fragment; - // If the target is the fragment and this element can't go inside + // Current element might be mangled by fix body below, + // save it for restore later. + var savedCurrent = currentNode; + + // If the target is the fragment and this inline element can't go inside // body (if fixForBody). - if ( fixForBody && !target.type ) + if ( fixForBody && ( !target.type || target.name == 'body' ) ) { var elementName, realElementName; if ( element.attributes @@ -129,21 +144,15 @@ CKEDITOR.htmlParser.fragment = function() elementName = realElementName; else elementName = element.name; - if ( elementName - && !( elementName in CKEDITOR.dtd.$body ) - && !( elementName in CKEDITOR.dtd.$nonBodyContent ) ) - { - var savedCurrent = currentNode; + if ( elementName && !( elementName in CKEDITOR.dtd.$body || elementName == 'body' || element.isOrphan ) ) + { // Create ain the fragment. currentNode = target; parser.onTagOpen( fixForBody, {} ); // The new target now is the
. - target = currentNode; - - if ( enforceCurrent ) - currentNode = savedCurrent; + element.returnPoint = target = currentNode; } } @@ -171,9 +180,11 @@ CKEDITOR.htmlParser.fragment = function() currentNode = element.returnPoint; delete element.returnPoint; } + else + currentNode = moveCurrent ? target : savedCurrent; } - parser.onTagOpen = function( tagName, attributes, selfClosing ) + parser.onTagOpen = function( tagName, attributes, selfClosing, optionalClose ) { var element = new CKEDITOR.htmlParser.element( tagName, attributes ); @@ -182,6 +193,8 @@ CKEDITOR.htmlParser.fragment = function() if ( element.isUnknown && selfClosing ) element.isEmpty = true; + element.isOptionalClose = optionalClose; + // This is a tag to be removed if empty, so do not add it immediately. if ( CKEDITOR.dtd.$removeEmpty[ tagName ] ) { @@ -202,89 +215,77 @@ CKEDITOR.htmlParser.fragment = function() return; } - 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 ( currentDtd // Fragment could receive any elements. - && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] ) + while( 1 ) { + var currentName = currentNode.name; - var reApply = false, - addPoint; // New position to start adding nodes. + var currentDtd = currentName ? ( CKEDITOR.dtd[ currentName ] + || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) ) + : rootDtd; - // Fixing malformed nested lists by moving it into a previous list item. (#3828) - if ( tagName in listBlocks - && currentName in listBlocks ) + // If the element cannot be child of the current element. + if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] ) { - var children = currentNode.children, - lastChild = children[ children.length - 1 ]; + // Current node doesn't have a close tag, time for a close + // as this element isn't fit in. (#7497) + if ( currentNode.isOptionalClose ) + parser.onTagClose( currentName ); + // Fixing malformed nested lists by moving it into a previous list item. (#3828) + else if ( tagName in listBlocks + && currentName in listBlocks ) + { + var children = currentNode.children, + lastChild = children[ children.length - 1 ]; - // Establish the list item if it's not existed. - if ( !( lastChild && lastChild.name in listItems ) ) - addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode ); + // Establish the list item if it's not existed. + if ( !( lastChild && lastChild.name == 'li' ) ) + 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 - // parent. This situation usually happens with
,
td1 | td2 |