JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / htmlparser / fragment.js
index 7f6c6df..6c7686c 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -45,7 +45,8 @@ CKEDITOR.htmlParser.fragment = function()
        // parser fixing.\r
        var nonBreakingBlocks = CKEDITOR.tools.extend(\r
                        {table:1,ul:1,ol:1,dl:1},\r
-                       CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl );\r
+                       CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl ),\r
+               listBlocks = CKEDITOR.dtd.$list, listItems = CKEDITOR.dtd.$listItem;\r
 \r
        /**\r
         * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.\r
@@ -113,7 +114,9 @@ CKEDITOR.htmlParser.fragment = function()
                                        elementName = realElementName;\r
                                else\r
                                        elementName =  element.name;\r
-                               if ( !( elementName in CKEDITOR.dtd.$body ) )\r
+                               if ( elementName\r
+                                               && !( elementName in CKEDITOR.dtd.$body )\r
+                                               && !( elementName in CKEDITOR.dtd.$nonBodyContent )  )\r
                                {\r
                                        var savedCurrent = currentNode;\r
 \r
@@ -178,24 +181,38 @@ CKEDITOR.htmlParser.fragment = function()
                                return;\r
                        }\r
 \r
-                       var currentName = currentNode.name,\r
-                               currentDtd = ( currentName && CKEDITOR.dtd[ currentName ] ) || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span );\r
+                       var currentName = currentNode.name;\r
+\r
+                       var currentDtd = currentName\r
+                               && ( CKEDITOR.dtd[ currentName ]\r
+                                       || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) );\r
 \r
                        // If the element cannot be child of the current element.\r
-                       if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )\r
+                       if ( currentDtd   // Fragment could receive any elements.\r
+                                && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )\r
                        {\r
-                               // If this is the fragment node, just ignore this tag and add\r
-                               // its children.\r
-                               if ( !currentName )\r
-                                       return;\r
 \r
-                               var reApply = false;\r
+                               var reApply = false,\r
+                                       addPoint;   // New position to start adding nodes.\r
+\r
+                               // Fixing malformed nested lists by moving it into a previous list item. (#3828)\r
+                               if( tagName in listBlocks\r
+                                       && currentName in listBlocks )\r
+                               {\r
+                                       var children = currentNode.children,\r
+                                               lastChild = children[ children.length - 1 ];\r
+\r
+                                       // Establish the list item if it's not existed.\r
+                                       if ( !( lastChild && lastChild.name in listItems ) )\r
+                                               addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );\r
 \r
+                                       returnPoint = currentNode, addPoint = lastChild;\r
+                               }\r
                                // If the element name is the same as the current element name,\r
                                // then just close the current one and append the new one to the\r
                                // parent. This situation usually happens with <p>, <li>, <dt> and\r
                                // <dd>, specially in IE. Do not enter in this if block in this case.\r
-                               if ( tagName == currentName )\r
+                               else if ( tagName == currentName )\r
                                {\r
                                        addElement( currentNode, currentNode.parent );\r
                                }\r
@@ -222,9 +239,11 @@ CKEDITOR.htmlParser.fragment = function()
                                        reApply = true;\r
                                }\r
 \r
-                               // In any of the above cases, we'll be adding, or trying to\r
-                               // add it to the parent.\r
-                               currentNode = currentNode.returnPoint || currentNode.parent;\r
+                               if( addPoint )\r
+                                       currentNode = addPoint;\r
+                               // Try adding it to the return point, or the parent element.\r
+                               else\r
+                                       currentNode = currentNode.returnPoint || currentNode.parent;\r
 \r
                                if ( reApply )\r
                                {\r
@@ -247,22 +266,28 @@ CKEDITOR.htmlParser.fragment = function()
 \r
                parser.onTagClose = function( tagName )\r
                {\r
-                       var index = 0,\r
-                               pendingAdd = [],\r
+                       // Check if there is any pending tag to be closed.\r
+                       for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )\r
+                       {\r
+                               // If found, just remove it from the list.\r
+                               if ( tagName == pendingInline[ i ].name )\r
+                               {\r
+                                       pendingInline.splice( i, 1 );\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
+                       var pendingAdd = [],\r
+                               newPendingInline = [],\r
                                candidate = currentNode;\r
 \r
                        while ( candidate.type && candidate.name != tagName )\r
                        {\r
-                               // If this is an inline element, add it to the pending list, so\r
-                               // it will continue after the closing tag.\r
+                               // If this is an inline element, add it to the pending list, if we're\r
+                               // really closing one of the parents element later, they will continue\r
+                               // after it.\r
                                if ( !candidate._.isBlockLike )\r
-                               {\r
-                                       pendingInline.unshift( candidate );\r
-\r
-                                       // Increase the index, so it will not get checked again in\r
-                                       // the pending list check that follows.\r
-                                       index++;\r
-                               }\r
+                                       newPendingInline.unshift( candidate );\r
 \r
                                // This node should be added to it's parent at this point. But,\r
                                // it should happen only if the closing tag is really closing\r
@@ -275,7 +300,7 @@ CKEDITOR.htmlParser.fragment = function()
                        if ( candidate.type )\r
                        {\r
                                // Add all elements that have been found in the above loop.\r
-                               for ( var i = 0 ; i < pendingAdd.length ; i++ )\r
+                               for ( i = 0 ; i < pendingAdd.length ; i++ )\r
                                {\r
                                        var node = pendingAdd[ i ];\r
                                        addElement( node, node.parent );\r
@@ -292,27 +317,12 @@ CKEDITOR.htmlParser.fragment = function()
                                // addElement changed the currentNode.\r
                                if ( candidate == currentNode )\r
                                        currentNode = currentNode.parent;\r
-                       }\r
-                       // The tag is not actually closing anything, thus we need invalidate\r
-                       // the pending elements.(#3862)\r
-                       else\r
-                       {\r
-                               pendingInline.splice( 0, index );\r
-                               index = 0;\r
-                       }\r
 \r
-                       // Check if there is any pending tag to be closed.\r
-                       for ( ; index < pendingInline.length ; index++ )\r
-                       {\r
-                               // If found, just remove it from the list.\r
-                               if ( tagName == pendingInline[ index ].name )\r
-                               {\r
-                                       pendingInline.splice( index, 1 );\r
-\r
-                                       // Decrease the index so we continue from the next one.\r
-                                       index--;\r
-                               }\r
+                               pendingInline = pendingInline.concat( newPendingInline );\r
                        }\r
+\r
+                       if( tagName == 'body' )\r
+                               fixForBody = false;\r
                };\r
 \r
                parser.onText = function( text )\r
@@ -328,8 +338,12 @@ CKEDITOR.htmlParser.fragment = function()
 \r
                        checkPending();\r
 \r
-                       if ( fixForBody && !currentNode.type )\r
+                       if ( fixForBody\r
+                                && ( !currentNode.type || currentNode.name == 'body' )\r
+                                && CKEDITOR.tools.trim( text ) )\r
+                       {\r
                                this.onTagOpen( fixForBody, {} );\r
+                       }\r
 \r
                        // Shrinking consequential spaces into one single for all elements\r
                        // text contents.\r
@@ -358,7 +372,9 @@ CKEDITOR.htmlParser.fragment = function()
                        var parent = currentNode.parent,\r
                                node = currentNode;\r
 \r
-                       if ( fixForBody && !parent.type && !CKEDITOR.dtd.$body[ node.name ] )\r
+                       if ( fixForBody\r
+                                && ( !parent.type || parent.name == 'body' )\r
+                                && !CKEDITOR.dtd.$body[ node.name ] )\r
                        {\r
                                currentNode = parent;\r
                                parser.onTagOpen( fixForBody, {} );\r
@@ -427,7 +443,25 @@ CKEDITOR.htmlParser.fragment = function()
                 */\r
                writeHtml : function( writer, filter )\r
                {\r
-                       for ( var i = 0, len = this.children.length ; i < len ; i++ )\r
+                       var isChildrenFiltered;\r
+                       this.filterChildren = function()\r
+                       {\r
+                               var writer = new CKEDITOR.htmlParser.basicWriter();\r
+                               this.writeChildrenHtml.call( this, writer, filter, true );\r
+                               var html = writer.getHtml();\r
+                               this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;\r
+                               isChildrenFiltered = 1;\r
+                       };\r
+\r
+                       // Filtering the root fragment before anything else.\r
+                       !this.name && filter && filter.onFragment( this );\r
+\r
+                       this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );\r
+               },\r
+\r
+               writeChildrenHtml : function( writer, filter )\r
+               {\r
+                       for ( var i = 0 ; i < this.children.length ; i++ )\r
                                this.children[i].writeHtml( writer, filter );\r
                }\r
        };\r