JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[ckeditor.git] / _source / plugins / list / plugin.js
index be74702..3a47c55 100644 (file)
@@ -12,6 +12,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        var listNodeNames = { ol : 1, ul : 1 },\r
                emptyTextRegex = /^[\n\r\t ]*$/;\r
 \r
+       var whitespaces = CKEDITOR.dom.walker.whitespaces(),\r
+               bookmarks = CKEDITOR.dom.walker.bookmark(),\r
+               nonEmpty = function( node ){ return !( whitespaces( node ) || bookmarks( node ) ); };\r
+\r
        CKEDITOR.plugins.list = {\r
                /*\r
                 * Convert a DOM list tree into a data structure that is easier to\r
@@ -67,7 +71,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
@@ -80,19 +84,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                indentLevel = Math.max( listArray[ baseIndex ].indent, 0 ),\r
                                currentListItem = null,\r
                                paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
-                       while ( true )\r
+                       while ( 1 )\r
                        {\r
                                var item = listArray[ currentIndex ];\r
                                if ( item.indent == indentLevel )\r
                                {\r
                                        if ( !rootNode || listArray[ currentIndex ].parent.getName() != rootNode.getName() )\r
                                        {\r
-                                               rootNode = listArray[ currentIndex ].parent.clone( false, true );\r
+                                               rootNode = listArray[ currentIndex ].parent.clone( false, 1 );\r
+                                               dir && rootNode.setAttribute( 'dir', dir );\r
                                                retval.append( rootNode );\r
                                        }\r
-                                       currentListItem = rootNode.append( item.element.clone( false, true ) );\r
+                                       currentListItem = rootNode.append( item.element.clone( 0, 1 ) );\r
                                        for ( var i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
                                        currentIndex++;\r
                                }\r
                                else if ( item.indent == Math.max( indentLevel, 0 ) + 1 )\r
@@ -108,24 +113,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                currentListItem = item.element.clone( false, true );\r
                                        else\r
                                        {\r
-                                               // Create completely new blocks here, attributes are dropped.\r
-                                               if ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' )\r
+                                               // Create completely new blocks here.\r
+                                               if ( dir || item.element.hasAttributes() ||\r
+                                                       ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' ) )\r
+                                               {\r
                                                        currentListItem = doc.createElement( paragraphName );\r
+                                                       item.element.copyAttributes( currentListItem, { type:1, value:1 } );\r
+                                                       dir && currentListItem.setAttribute( 'dir', dir );\r
+\r
+                                                       // There might be a case where there are no attributes in the element after all\r
+                                                       // (i.e. when "type" or "value" are the only attributes set). In this case, if enterMode = BR,\r
+                                                       // the current item should be a fragment.\r
+                                                       if ( !dir && paragraphMode == CKEDITOR.ENTER_BR && !currentListItem.hasAttributes() )\r
+                                                               currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
+                                               }\r
                                                else\r
                                                        currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
                                        }\r
 \r
                                        for ( i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
 \r
                                        if ( currentListItem.type == CKEDITOR.NODE_DOCUMENT_FRAGMENT\r
                                                 && currentIndex != listArray.length - 1 )\r
                                        {\r
-                                               if ( currentListItem.getLast()\r
-                                                               && currentListItem.getLast().type == CKEDITOR.NODE_ELEMENT\r
-                                                               && currentListItem.getLast().getAttribute( 'type' ) == '_moz' )\r
-                                                       currentListItem.getLast().remove();\r
-                                               currentListItem.appendBogus();\r
+                                               var last = currentListItem.getLast();\r
+                                               if ( last && last.type == CKEDITOR.NODE_ELEMENT\r
+                                                               && last.getAttribute( 'type' ) == '_moz' )\r
+                                               {\r
+                                                       last.remove();\r
+                                               }\r
+\r
+                                               if ( !( last = currentListItem.getLast( nonEmpty )\r
+                                                       && last.type == CKEDITOR.NODE_ELEMENT\r
+                                                       && last.getName() in CKEDITOR.dtd.$block ) )\r
+                                               {\r
+                                                       currentListItem.append( doc.createElement( 'br' ) );\r
+                                               }\r
                                        }\r
 \r
                                        if ( currentListItem.type == CKEDITOR.NODE_ELEMENT &&\r
@@ -218,7 +242,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
@@ -258,6 +288,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( var i = 0 ; i < contents.length ; i++ )\r
                        commonParent = commonParent.getCommonAncestor( contents[i].getParent() );\r
 \r
+               var useComputedState = editor.config.useComputedState,\r
+                       listDir, explicitDirection;\r
+\r
+               useComputedState = useComputedState === undefined || useComputedState;\r
+\r
                // We want to insert things that are in the same tree level only, so calculate the contents again\r
                // by expanding the selected blocks to the same tree level.\r
                for ( i = 0 ; i < contents.length ; i++ )\r
@@ -269,6 +304,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                if ( parentNode.equals( commonParent ) )\r
                                {\r
                                        listContents.push( contentNode );\r
+\r
+                                       // Determine the lists's direction.\r
+                                       if ( !explicitDirection && contentNode.getDirection() )\r
+                                               explicitDirection = 1;\r
+\r
+                                       var itemDir = contentNode.getDirection( useComputedState );\r
+\r
+                                       if ( listDir !== null )\r
+                                       {\r
+                                               // If at least one LI have a different direction than current listDir, we can't have listDir.\r
+                                               if ( listDir && listDir != itemDir )\r
+                                                       listDir = null;\r
+                                               else\r
+                                                       listDir = itemDir;\r
+                                       }\r
+\r
                                        break;\r
                                }\r
                                contentNode = parentNode;\r
@@ -283,27 +334,38 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        listNode = doc.createElement( this.type );\r
 \r
                listsCreated.push( listNode );\r
+\r
+               var contentBlock, listItem;\r
+\r
                while ( listContents.length )\r
                {\r
-                       var contentBlock = listContents.shift(),\r
-                               listItem = doc.createElement( 'li' );\r
+                       contentBlock = listContents.shift();\r
+                       listItem = doc.createElement( 'li' );\r
 \r
-                       // Preserve heading structure when converting to list item. (#5271)\r
-                       if ( headerTagRegex.test( contentBlock.getName() ) )\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
+                               // Remove DIR attribute if it was merged into list root.\r
+                               if ( listDir && contentBlock.getDirection() )\r
+                               {\r
+                                       contentBlock.removeStyle( 'direction' );\r
+                                       contentBlock.removeAttribute( 'dir' );\r
+                               }\r
+\r
                                contentBlock.copyAttributes( listItem );\r
                                contentBlock.moveChildren( listItem );\r
                                contentBlock.remove();\r
                        }\r
 \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
                }\r
+\r
+               // Apply list root dir only if it has been explicitly declared.\r
+               if ( listDir && explicitDirection )\r
+                       listNode.setAttribute( 'dir', listDir );\r
+\r
                if ( insertAnchor )\r
                        listNode.insertBefore( insertAnchor );\r
                else\r
@@ -353,7 +415,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
@@ -385,7 +448,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
@@ -403,7 +466,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
@@ -422,9 +485,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                enclosedNode = range && range.getEnclosedNode();\r
                                        if ( enclosedNode && enclosedNode.is\r
                                                && this.type == enclosedNode.getName() )\r
-                                       {\r
-                                               setState.call( this, editor, CKEDITOR.TRISTATE_ON );\r
-                                       }\r
+                                                       setState.call( this, editor, CKEDITOR.TRISTATE_ON );\r
                                }\r
                        }\r
 \r
@@ -433,12 +494,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
@@ -456,11 +517,17 @@ 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
+                                               processedFlag = 0,\r
                                                blockLimit = path.blockLimit,\r
                                                element;\r
 \r
@@ -475,7 +542,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // 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
@@ -486,7 +553,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                listGroups.push( groupObj );\r
                                                                CKEDITOR.dom.element.setMarker( database, element, 'list_group_object', groupObj );\r
                                                        }\r
-                                                       processedFlag = true;\r
+                                                       processedFlag = 1;\r
                                                        break;\r
                                                }\r
                                        }\r
@@ -494,14 +561,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
@@ -535,14 +602,14 @@ 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
-                                               sibling.moveChildren( listNode, rtl ? true : false );\r
+                                               sibling.moveChildren( listNode, rtl );\r
                                        }\r
                                } )();\r
-                               mergeSibling( true );\r
+                               mergeSibling( 1 );\r
                        }\r
 \r
                        // Clean up, restore selection and update toolbar button states.\r