X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fhtmlparser%2Ffragment.js;h=bfa5cc57925c31bcd800e086e33f45a154e07f5c;hb=refs%2Ftags%2Fv3.6.1;hp=7f6c6df8620ac53b09fd51d66096c3bff6e43f82;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/core/htmlparser/fragment.js b/_source/core/htmlparser/fragment.js index 7f6c6df..bfa5cc5 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-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -37,39 +37,43 @@ 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 ); + 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 ); + + // IE < 8 don't output the close tag on definition list items. (#6975) + var optionalCloseTags = CKEDITOR.env.ie && CKEDITOR.env.version < 8 ? { dd : 1, dt :1 } : {}; + + 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 ) { + var pendingBRsSent; + if ( pendingInline.length > 0 ) { for ( var i = 0 ; i < pendingInline.length ; i++ ) @@ -81,6 +85,12 @@ CKEDITOR.htmlParser.fragment = function() if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) ) { + if ( !pendingBRsSent ) + { + sendPendingBRs(); + pendingBRsSent = 1; + } + // Get a clone for the pending element. pendingElement = pendingElement.clone(); @@ -98,34 +108,54 @@ CKEDITOR.htmlParser.fragment = function() } } - function addElement( element, target, enforceCurrent ) + function sendPendingBRs() + { + while ( pendingBRs.length ) + currentNode.add( pendingBRs.shift() ); + } + + /* + * 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 && ( realElementName = - element.attributes[ '_cke_real_element_type' ] ) ) + element.attributes[ 'data-cke-real-element-type' ] ) ) elementName = realElementName; else elementName = element.name; - if ( !( elementName in CKEDITOR.dtd.$body ) ) - { - 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; } } @@ -153,9 +183,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 ); @@ -164,6 +196,9 @@ CKEDITOR.htmlParser.fragment = function() if ( element.isUnknown && selfClosing ) element.isEmpty = true; + // Check for optional closed elements, including browser quirks and manually opened blocks. + element.isOptionalClose = tagName in optionalCloseTags || optionalClose; + // This is a tag to be removed if empty, so do not add it immediately. if ( CKEDITOR.dtd.$removeEmpty[ tagName ] ) { @@ -178,66 +213,83 @@ CKEDITOR.htmlParser.fragment = function() return; } - var currentName = currentNode.name, - currentDtd = ( currentName && CKEDITOR.dtd[ currentName ] ) || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ); + if ( tagName == 'br' ) + { + pendingBRs.push( element ); + return; + } - // If the element cannot be child of the current element. - if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] ) + while( 1 ) { - // If this is the fragment node, just ignore this tag and add - // its children. - if ( !currentName ) - return; + var currentName = currentNode.name; - var reApply = false; + var currentDtd = currentName ? ( CKEDITOR.dtd[ currentName ] + || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) ) + : rootDtd; - // 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 |
. - if ( !currentNode._.hasInlineStarted && !inPre ) + // Trim empty spaces at beginning of text contents except. + if ( ( !currentNode._.hasInlineStarted || pendingBRs.length ) && !inPre ) { text = CKEDITOR.tools.ltrim( text ); @@ -326,10 +373,15 @@ CKEDITOR.htmlParser.fragment = function() return; } + sendPendingBRs(); checkPending(); - if ( fixForBody && !currentNode.type ) - this.onTagOpen( fixForBody, {} ); + if ( fixForBody + && ( !currentNode.type || currentNode.name == 'body' ) + && CKEDITOR.tools.trim( text ) ) + { + this.onTagOpen( fixForBody, {}, 0, 1 ); + } // Shrinking consequential spaces into one single for all elements // text contents. @@ -346,28 +398,20 @@ CKEDITOR.htmlParser.fragment = function() parser.onComment = function( comment ) { + sendPendingBRs(); + checkPending(); currentNode.add( new CKEDITOR.htmlParser.comment( comment ) ); }; // Parse it. parser.parse( fragmentHtml ); - // Close all pending nodes. - while ( currentNode.type ) - { - var parent = currentNode.parent, - node = currentNode; + // Send all pending BRs except one, which we consider a unwanted bogus. (#5293) + sendPendingBRs( !CKEDITOR.env.ie && 1 ); - if ( fixForBody && !parent.type && !CKEDITOR.dtd.$body[ node.name ] ) - { - currentNode = parent; - parser.onTagOpen( fixForBody, {} ); - parent = currentNode; - } - - parent.add( node ); - currentNode = parent; - } + // Close all pending nodes, make sure return point is properly restored. + while ( currentNode != fragment ) + addElement( currentNode, currentNode.parent, 1 ); return fragment; }; @@ -380,13 +424,14 @@ CKEDITOR.htmlParser.fragment = function() * following types: {@link CKEDITOR.htmlParser.element}, * {@link CKEDITOR.htmlParser.text} and * {@link CKEDITOR.htmlParser.comment}. + * @param {Number} [index] From where the insertion happens. * @example */ - add : function( node ) + add : function( node, index ) { - var len = this.children.length, - previous = len > 0 && this.children[ len - 1 ] || null; + isNaN( index ) && ( index = this.children.length ); + var previous = index > 0 ? this.children[ index - 1 ] : null; if ( previous ) { // If the block to be appended is following text, trim spaces at @@ -411,7 +456,7 @@ CKEDITOR.htmlParser.fragment = function() node.previous = previous; node.parent = this; - this.children.push( node ); + this.children.splice( index, 0, node ); this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike ); }, @@ -427,7 +472,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 ); } };