JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.1
[ckeditor.git] / _source / plugins / list / plugin.js
index 45744e1..b0d5e8c 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
@@ -29,7 +29,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( !baseArray )\r
                                baseArray = [];\r
 \r
-                       // Iterate over all list items to get their contents and look for inner lists.\r
+                       // Iterate over all list items to and look for inner lists.\r
                        for ( var i = 0, count = listNode.getChildCount() ; i < count ; i++ )\r
                        {\r
                                var listItem = listNode.getChild( i );\r
@@ -37,7 +37,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // It may be a text node or some funny stuff.\r
                                if ( listItem.$.nodeName.toLowerCase() != 'li' )\r
                                        continue;\r
-                               var itemObj = { 'parent' : listNode, indent : baseIndentLevel, contents : [] };\r
+\r
+                               var itemObj = { 'parent' : listNode, indent : baseIndentLevel, element : listItem, contents : [] };\r
                                if ( !grandparentNode )\r
                                {\r
                                        itemObj.grandparent = listNode.getParent();\r
@@ -51,9 +52,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        CKEDITOR.dom.element.setMarker( database, listItem, 'listarray_index', baseArray.length );\r
                                baseArray.push( itemObj );\r
 \r
-                               for ( var j = 0, itemChildCount = listItem.getChildCount() ; j < itemChildCount ; j++ )\r
+                               for ( var j = 0, itemChildCount = listItem.getChildCount(), child; j < itemChildCount ; j++ )\r
                                {\r
-                                       var child = listItem.getChild( j );\r
+                                       child = listItem.getChild( j );\r
                                        if ( child.type == CKEDITOR.NODE_ELEMENT && listNodeNames[ child.getName() ] )\r
                                                // Note the recursion here, it pushes inner list items with\r
                                                // +1 indentation in the correct order.\r
@@ -66,7 +67,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                // Convert our internal representation of a list back to a DOM forest.\r
-               arrayToList : function( listArray, database, baseIndex, paragraphMode )\r
+               arrayToList : function( listArray, database, baseIndex, paragraphMode, dir )\r
                {\r
                        if ( !baseIndex )\r
                                baseIndex = 0;\r
@@ -89,7 +90,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                rootNode = listArray[ currentIndex ].parent.clone( false, true );\r
                                                retval.append( rootNode );\r
                                        }\r
-                                       currentListItem = rootNode.append( doc.createElement( 'li' ) );\r
+                                       currentListItem = rootNode.append( item.element.clone( false, true ) );\r
                                        for ( var i = 0 ; i < item.contents.length ; i++ )\r
                                                currentListItem.append( item.contents[i].clone( true, true ) );\r
                                        currentIndex++;\r
@@ -104,11 +105,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                {\r
                                        currentListItem;\r
                                        if ( listNodeNames[ item.grandparent.getName() ] )\r
-                                               currentListItem = doc.createElement( 'li' );\r
+                                               currentListItem = item.element.clone( false, true );\r
                                        else\r
                                        {\r
-                                               if ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' )\r
+                                               // Create completely new blocks here, attributes are dropped.\r
+                                               if ( dir || ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' ) )\r
+                                               {\r
                                                        currentListItem = doc.createElement( paragraphName );\r
+                                                       if ( dir )\r
+                                                               currentListItem.setAttribute( 'dir', dir );\r
+                                               }\r
                                                else\r
                                                        currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
                                        }\r
@@ -216,7 +222,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        CKEDITOR.dom.element.setMarker( database, itemNode, 'list_item_processed', true );\r
                }\r
 \r
-               var fakeParent = groupObj.root.getDocument().createElement( this.type );\r
+               var root = groupObj.root,\r
+                       fakeParent = root.getDocument().createElement( this.type );\r
+               // Copy all attributes, except from 'start' and 'type'.\r
+               root.copyAttributes( fakeParent, { start : 1, type : 1 } );\r
+               // The list-style-type property should be ignored.\r
+               fakeParent.removeStyle( 'list-style-type' );\r
+\r
                for ( i = 0 ; i < selectedListItems.length ; i++ )\r
                {\r
                        var listIndex = selectedListItems[i].getCustomData( 'listarray_index' );\r
@@ -232,6 +244,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                newList.listNode.replace( groupObj.root );\r
        }\r
 \r
+       var headerTagRegex = /^h[1-6]$/;\r
+\r
        function createList( editor, groupObj, listsCreated )\r
        {\r
                var contents = groupObj.contents,\r
@@ -276,21 +290,40 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                // Insert the list to the DOM tree.\r
                var insertAnchor = listContents[ listContents.length - 1 ].getNext(),\r
-                       listNode = doc.createElement( this.type );\r
+                       listNode = doc.createElement( this.type ),\r
+                       dir;\r
 \r
                listsCreated.push( listNode );\r
                while ( listContents.length )\r
                {\r
                        var contentBlock = listContents.shift(),\r
                                listItem = doc.createElement( 'li' );\r
-                       contentBlock.moveChildren( listItem );\r
-                       contentBlock.remove();\r
-                       listItem.appendTo( listNode );\r
 \r
-                       // Append a bogus BR to force the LI to render at full height\r
-                       if ( !CKEDITOR.env.ie )\r
-                               listItem.appendBogus();\r
+                       // Preserve preformat block and heading structure when converting to list item. (#5335) (#5271)\r
+                       if ( contentBlock.is( 'pre' ) || headerTagRegex.test( contentBlock.getName() ) )\r
+                               contentBlock.appendTo( listItem );\r
+                       else\r
+                       {\r
+                               if ( contentBlock.hasAttribute( 'dir' ) )\r
+                               {\r
+                                       dir = dir || contentBlock.getAttribute( 'dir' );\r
+                                       contentBlock.removeAttribute( 'dir' );\r
+                               }\r
+                               contentBlock.copyAttributes( listItem );\r
+                               contentBlock.moveChildren( listItem );\r
+                               contentBlock.remove();\r
+\r
+                               // Append a bogus BR to force the LI to render at full height\r
+                               if ( !CKEDITOR.env.ie )\r
+                                       listItem.appendBogus();\r
+                       }\r
+\r
+                       listItem.appendTo( listNode );\r
                }\r
+\r
+               if ( dir )\r
+                       listNode.setAttribute( 'dir', dir );\r
+\r
                if ( insertAnchor )\r
                        listNode.insertBefore( insertAnchor );\r
                else\r
@@ -340,7 +373,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                }\r
 \r
-               var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode );\r
+               var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode,\r
+                       groupObj.root.getAttribute( 'dir' ) );\r
 \r
                // Compensate <br> before/after the list node if the surrounds are non-blocks.(#3836)\r
                var docFragment = newList.listNode, boundaryNode, siblingNode;\r
@@ -356,7 +390,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                compensateBrs( true );\r
                compensateBrs();\r
 \r
-               var rootParent = groupObj.root.getParent();\r
                docFragment.replace( groupObj.root );\r
        }\r
 \r
@@ -373,7 +406,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        var doc = editor.document,\r
                                selection = editor.getSelection(),\r
-                               ranges = selection && selection.getRanges();\r
+                               ranges = selection && selection.getRanges( true );\r
 \r
                        // There should be at least one selected range.\r
                        if ( !ranges || ranges.length < 1 )\r
@@ -391,7 +424,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        var paragraph = doc.createElement( editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' :\r
                                                        ( editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'br' ) );\r
                                        paragraph.appendTo( body );\r
-                                       ranges = [ new CKEDITOR.dom.range( doc ) ];\r
+                                       ranges = new CKEDITOR.dom.rangeList( [ new CKEDITOR.dom.range( doc ) ] );\r
                                        // IE exception on inserting anything when anchor inside <br>.\r
                                        if ( paragraph.is( 'br' ) )\r
                                        {\r
@@ -421,12 +454,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // Group the blocks up because there are many cases where multiple lists have to be created,\r
                        // or multiple lists have to be cancelled.\r
                        var listGroups = [],\r
-                               database = {};\r
+                               database = {},\r
+                               rangeIterator = ranges.createIterator(),\r
+                               index = 0;\r
 \r
-                       while ( ranges.length > 0 )\r
+                       while ( ( range = rangeIterator.getNextRange() ) && ++index )\r
                        {\r
-                               range = ranges.shift();\r
-\r
                                var boundaryNodes = range.getBoundaryNodes(),\r
                                        startNode = boundaryNodes.startNode,\r
                                        endNode = boundaryNodes.endNode;\r
@@ -444,24 +477,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                while ( ( block = iterator.getNextParagraph() ) )\r
                                {\r
+                                       // Avoid duplicate blocks get processed across ranges.\r
+                                       if( block.getCustomData( 'list_block' ) )\r
+                                               continue;\r
+                                       else\r
+                                               CKEDITOR.dom.element.setMarker( database, block, 'list_block', 1 );\r
+\r
                                        var path = new CKEDITOR.dom.elementPath( block ),\r
+                                               pathElements = path.elements,\r
+                                               pathElementsCount = pathElements.length,\r
                                                listNode = null,\r
                                                processedFlag = false,\r
                                                blockLimit = path.blockLimit,\r
                                                element;\r
 \r
                                        // First, try to group by a list ancestor.\r
-                                       for ( var i = 0 ; i < path.elements.length &&\r
-                                                 ( element = path.elements[ i ] ) && !element.equals( blockLimit ); i++ )\r
+                                       for ( var i = pathElementsCount - 1; i >= 0 && ( element = pathElements[ i ] ); i-- )\r
                                        {\r
-                                               if ( listNodeNames[ element.getName() ] )\r
+                                               if ( listNodeNames[ element.getName() ]\r
+                                                        && blockLimit.contains( element ) )     // Don't leak outside block limit (#3940).\r
                                                {\r
                                                        // If we've encountered a list inside a block limit\r
                                                        // The last group object of the block limit element should\r
                                                        // no longer be valid. Since paragraphs after the list\r
                                                        // should belong to a different group of paragraphs before\r
                                                        // the list. (Bug #1309)\r
-                                                       blockLimit.removeCustomData( 'list_group_object' );\r
+                                                       blockLimit.removeCustomData( 'list_group_object_' + index );\r
 \r
                                                        var groupObj = element.getCustomData( 'list_group_object' );\r
                                                        if ( groupObj )\r
@@ -480,14 +521,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        if ( processedFlag )\r
                                                continue;\r
 \r
-                                       // No list ancestor? Group by block limit.\r
+                                       // No list ancestor? Group by block limit, but don't mix contents from different ranges.\r
                                        var root = blockLimit;\r
-                                       if ( root.getCustomData( 'list_group_object' ) )\r
-                                               root.getCustomData( 'list_group_object' ).contents.push( block );\r
+                                       if ( root.getCustomData( 'list_group_object_' + index ) )\r
+                                               root.getCustomData( 'list_group_object_' + index ).contents.push( block );\r
                                        else\r
                                        {\r
                                                groupObj = { root : root, contents : [ block ] };\r
-                                               CKEDITOR.dom.element.setMarker( database, root, 'list_group_object', groupObj );\r
+                                               CKEDITOR.dom.element.setMarker( database, root, 'list_group_object_' + index, groupObj );\r
                                                listGroups.push( groupObj );\r
                                        }\r
                                }\r
@@ -521,7 +562,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        var sibling = listNode[ rtl ?\r
                                                'getPrevious' : 'getNext' ]( CKEDITOR.dom.walker.whitespaces( true ) );\r
                                        if ( sibling && sibling.getName &&\r
-                                            sibling.getName() == listCommand.type )\r
+                                                sibling.getName() == listCommand.type )\r
                                        {\r
                                                sibling.remove();\r
                                                // Move children order by merge direction.(#3820)\r
@@ -538,6 +579,68 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 \r
+       var dtd = CKEDITOR.dtd;\r
+       var tailNbspRegex = /[\t\r\n ]*(?:&nbsp;|\xa0)$/;\r
+\r
+       function indexOfFirstChildElement( element, tagNameList )\r
+       {\r
+               var child,\r
+                       children = element.children,\r
+                       length = children.length;\r
+\r
+               for ( var i = 0 ; i < length ; i++ )\r
+               {\r
+                       child = children[ i ];\r
+                       if ( child.name && ( child.name in tagNameList ) )\r
+                               return i;\r
+               }\r
+\r
+               return length;\r
+       }\r
+\r
+       function getExtendNestedListFilter( isHtmlFilter )\r
+       {\r
+               // An element filter function that corrects nested list start in an empty\r
+               // list item for better displaying/outputting. (#3165)\r
+               return function( listItem )\r
+               {\r
+                       var children = listItem.children,\r
+                               firstNestedListIndex = indexOfFirstChildElement( listItem, dtd.$list ),\r
+                               firstNestedList = children[ firstNestedListIndex ],\r
+                               nodeBefore = firstNestedList && firstNestedList.previous,\r
+                               tailNbspmatch;\r
+\r
+                       if ( nodeBefore\r
+                               && ( nodeBefore.name && nodeBefore.name == 'br'\r
+                                       || nodeBefore.value && ( tailNbspmatch = nodeBefore.value.match( tailNbspRegex ) ) ) )\r
+                       {\r
+                               var fillerNode = nodeBefore;\r
+\r
+                               // Always use 'nbsp' as filler node if we found a nested list appear\r
+                               // in front of a list item.\r
+                               if ( !( tailNbspmatch && tailNbspmatch.index ) && fillerNode == children[ 0 ] )\r
+                                       children[ 0 ] = ( isHtmlFilter || CKEDITOR.env.ie ) ?\r
+                                                        new CKEDITOR.htmlParser.text( '\xa0' ) :\r
+                                                                        new CKEDITOR.htmlParser.element( 'br', {} );\r
+\r
+                               // Otherwise the filler is not needed anymore.\r
+                               else if ( fillerNode.name == 'br' )\r
+                                       children.splice( firstNestedListIndex - 1, 1 );\r
+                               else\r
+                                       fillerNode.value = fillerNode.value.replace( tailNbspRegex, '' );\r
+                       }\r
+\r
+               };\r
+       }\r
+\r
+       var defaultListDataFilterRules = { elements : {} };\r
+       for ( var i in dtd.$listItem )\r
+               defaultListDataFilterRules.elements[ i ] = getExtendNestedListFilter();\r
+\r
+       var defaultListHtmlFilterRules = { elements : {} };\r
+       for ( i in dtd.$listItem )\r
+               defaultListHtmlFilterRules.elements[ i ] = getExtendNestedListFilter( true );\r
+\r
        CKEDITOR.plugins.add( 'list',\r
        {\r
                init : function( editor )\r
@@ -565,6 +668,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, bulletedListCommand ) );\r
                },\r
 \r
+               afterInit : function ( editor )\r
+               {\r
+                       var dataProcessor = editor.dataProcessor;\r
+                       if ( dataProcessor )\r
+                       {\r
+                               dataProcessor.dataFilter.addRules( defaultListDataFilterRules );\r
+                               dataProcessor.htmlFilter.addRules( defaultListHtmlFilterRules );\r
+                       }\r
+               },\r
+\r
                requires : [ 'domiterator' ]\r
        } );\r
 })();\r