JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.4
[ckeditor.git] / _source / plugins / list / plugin.js
index 152be3c..373f914 100644 (file)
@@ -31,6 +31,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
+       // Inheirt inline styles from another element.\r
+       function inheirtInlineStyles( parent, el )\r
+       {\r
+               var style = parent.getAttribute( 'style' );\r
+\r
+               // Put parent styles before child styles.\r
+               style && el.setAttribute( 'style',\r
+                       style.replace( /([^;])$/, '$1;' ) +\r
+                       ( el.getAttribute( 'style' ) || '' ) );\r
+       }\r
+\r
        CKEDITOR.plugins.list = {\r
                /*\r
                 * Convert a DOM list tree into a data structure that is easier to\r
@@ -108,7 +119,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
                        while ( 1 )\r
                        {\r
-                               var item = listArray[ currentIndex ];\r
+                               var item = listArray[ currentIndex ],\r
+                                       itemGrandParent = item.grandparent;\r
 \r
                                orgDir = item.element.getDirection( 1 );\r
 \r
@@ -144,22 +156,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        currentListItem.append( listData.listNode );\r
                                        currentIndex = listData.nextIndex;\r
                                }\r
-                               else if ( item.indent == -1 && !baseIndex && item.grandparent )\r
+                               else if ( item.indent == -1 && !baseIndex && itemGrandParent )\r
                                {\r
-                                       if ( listNodeNames[ item.grandparent.getName() ] )\r
+                                       if ( listNodeNames[ itemGrandParent.getName() ] )\r
+                                       {\r
                                                currentListItem = item.element.clone( false, true );\r
+                                               if ( orgDir != itemGrandParent.getDirection( 1 ) )\r
+                                                       currentListItem.setAttribute( 'dir', orgDir );\r
+                                       }\r
                                        else\r
                                                currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
 \r
                                        // Migrate all children to the new container,\r
                                        // apply the proper text direction.\r
-                                       var dirLoose = item.grandparent.getDirection( 1 ) != orgDir,\r
-                                               needsBlock = currentListItem.type == CKEDITOR.NODE_DOCUMENT_FRAGMENT &&\r
-                                                                        paragraphMode != CKEDITOR.ENTER_BR,\r
+                                       var dirLoose = itemGrandParent.getDirection( 1 ) != orgDir,\r
                                                li = item.element,\r
                                                className = li.getAttribute( 'class' ),\r
                                                style = li.getAttribute( 'style' );\r
 \r
+                                       var needsBlock = currentListItem.type ==\r
+                                                        CKEDITOR.NODE_DOCUMENT_FRAGMENT &&\r
+                                                        ( paragraphMode != CKEDITOR.ENTER_BR || dirLoose ||\r
+                                                          style || className );\r
+\r
                                        var child, count = item.contents.length;\r
                                        for ( i = 0 ; i < count; i++ )\r
                                        {\r
@@ -171,14 +190,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        if ( dirLoose && !child.getDirection() )\r
                                                                child.setAttribute( 'dir', orgDir );\r
 \r
-                                                       // Merge into child styles.\r
-                                                       style && child.setAttribute( 'style',\r
-                                                                                style.replace( /([^;])$/, '$1;') +\r
-                                                                                ( child.getAttribute( 'style' ) || '' ) );\r
+                                                       inheirtInlineStyles( li, child );\r
 \r
                                                        className && child.addClass( className );\r
                                                }\r
-                                               else if ( dirLoose || needsBlock || style || className )\r
+                                               else if ( needsBlock )\r
                                                {\r
                                                        // Establish new block to hold text direction and styles.\r
                                                        if ( !block )\r
@@ -299,17 +315,27 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
 \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
+                       doc = root.getDocument(),\r
+                       listNode,\r
+                       newListNode;\r
 \r
                for ( i = 0 ; i < selectedListItems.length ; i++ )\r
                {\r
                        var listIndex = selectedListItems[i].getCustomData( 'listarray_index' );\r
-                       listArray[listIndex].parent = fakeParent;\r
+                       listNode = listArray[ listIndex ].parent;\r
+\r
+                       // Switch to new list node for this particular item.\r
+                       if ( !listNode.is( this.type ) )\r
+                       {\r
+                               newListNode = doc.createElement( this.type );\r
+                               // Copy all attributes, except from 'start' and 'type'.\r
+                               listNode.copyAttributes( newListNode, { start : 1, type : 1 } );\r
+                               // The list-style-type property should be ignored.\r
+                               newListNode.removeStyle( 'list-style-type' );\r
+                               listArray[ listIndex ].parent = newListNode;\r
+                       }\r
                }\r
+\r
                var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode );\r
                var child, length = newList.listNode.getChildCount();\r
                for ( i = 0 ; i < length && ( child = newList.listNode.getChild( i ) ) ; i++ )\r
@@ -497,11 +523,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        }\r
 \r
        var elementType = CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT );\r
-       // Merge list items with direction preserved. (#7448)\r
-       function mergeListItems( from, into, refNode, toHead )\r
+\r
+       // Merge child nodes with direction preserved. (#7448)\r
+       function mergeChildren( from, into, refNode, forward )\r
        {\r
                var child, itemDir;\r
-               while ( ( child = from.getFirst( elementType ) ) )\r
+               while ( ( child = from[ forward ? 'getLast' : 'getFirst' ]( elementType ) ) )\r
                {\r
                        if ( ( itemDir = child.getDirection( 1 ) ) !== into.getDirection( 1 ) )\r
                                child.setAttribute( 'dir', itemDir );\r
@@ -509,8 +536,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        child.remove();\r
 \r
                        refNode ?\r
-                               child[ toHead ? 'insertBefore' : 'insertAfter' ]( refNode ) :\r
-                               into.append( child, toHead  );\r
+                               child[ forward ? 'insertBefore' : 'insertAfter' ]( refNode ) :\r
+                               into.append( child, forward  );\r
                }\r
        }\r
 \r
@@ -657,26 +684,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        // For all new lists created, merge into adjacent, same type lists.\r
                        for ( i = 0 ; i < listsCreated.length ; i++ )\r
-                       {\r
-                               listNode = listsCreated[i];\r
-                               var mergeSibling, listCommand = this;\r
-                               ( mergeSibling = function( rtl )\r
-                               {\r
-\r
-                                       var sibling = listNode[ rtl ?\r
-                                               'getPrevious' : 'getNext' ]( CKEDITOR.dom.walker.whitespaces( true ) );\r
-                                       if ( sibling && sibling.getName &&\r
-                                                sibling.getName() == listCommand.type )\r
-                                       {\r
-                                               // Move children order by merge direction.(#3820)\r
-                                               mergeListItems( listNode, sibling, null, !rtl );\r
-\r
-                                               listNode.remove();\r
-                                               listNode = sibling;\r
-                                       }\r
-                               } )();\r
-                               mergeSibling( 1 );\r
-                       }\r
+                               mergeListSiblings( listsCreated[ i ] );\r
 \r
                        // Clean up, restore selection and update toolbar button states.\r
                        CKEDITOR.dom.element.clearAllMarkers( database );\r
@@ -685,6 +693,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 \r
+       // Merge list adjacent, of same type lists.\r
+       function mergeListSiblings( listNode )\r
+       {\r
+               var mergeSibling;\r
+               ( mergeSibling = function( rtl )\r
+               {\r
+                       var sibling = listNode[ rtl ? 'getPrevious' : 'getNext' ]( nonEmpty );\r
+                       if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT &&\r
+                            sibling.is( listNode.getName() ) )\r
+                       {\r
+                               // Move children order by merge direction.(#3820)\r
+                               mergeChildren( listNode, sibling, null, !rtl );\r
+\r
+                               listNode.remove();\r
+                               listNode = sibling;\r
+                       }\r
+               } )();\r
+               mergeSibling( 1 );\r
+       }\r
+\r
        var dtd = CKEDITOR.dtd;\r
        var tailNbspRegex = /[\t\r\n ]*(?:&nbsp;|\xa0)$/;\r
 \r
@@ -766,6 +794,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var frag = nextCursor.extractContents();\r
 \r
                cursor.trim( false, true );\r
+               var bm = cursor.createBookmark();\r
 \r
                // Kill original bogus;\r
                var currentPath = new CKEDITOR.dom.elementPath( cursor.startContainer );\r
@@ -798,7 +827,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // If next line is in the sub list of the current list item.\r
                                if ( currentLi.contains( nextLi ) )\r
                                {\r
-                                       mergeListItems( sublist, nextLi.getParent(), nextLi );\r
+                                       mergeChildren( sublist, nextLi.getParent(), nextLi );\r
                                        sublist.remove();\r
                                }\r
                                // Migrate the sub list to current list item.\r
@@ -807,27 +836,41 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                }\r
 \r
-\r
-               if ( nextCursor.checkStartOfBlock() &&\r
+               // Remove any remaining empty path blocks at next line after merging.\r
+               while ( nextCursor.checkStartOfBlock() &&\r
                         nextCursor.checkEndOfBlock() )\r
                {\r
-                       var nextBlock = nextPath.block,\r
-                               parentBlock = nextBlock.getParent();\r
+                       nextPath = new CKEDITOR.dom.elementPath( nextCursor.startContainer );\r
+                       var nextBlock = nextPath.block, parent;\r
 \r
-                       nextBlock.remove();\r
-\r
-                       // Remove if the path block container is now empty, e.g. li.\r
-                       if ( parentBlock &&\r
-                                !parentBlock.getFirst( nonEmpty ) &&\r
-                                !parentBlock.equals( nextPath.blockLimit ) )\r
+                       // Check if also to remove empty list.\r
+                       if ( nextBlock.is( 'li' ) )\r
                        {\r
-                               parentBlock.remove();\r
+                               parent = nextBlock.getParent();\r
+                               if ( nextBlock.equals( parent.getLast( nonEmpty ) )\r
+                                               && nextBlock.equals( parent.getFirst( nonEmpty ) ) )\r
+                                       nextBlock = parent;\r
                        }\r
+\r
+                       nextCursor.moveToPosition( nextBlock, CKEDITOR.POSITION_BEFORE_START );\r
+                       nextBlock.remove();\r
                }\r
 \r
+               // Check if need to further merge with the list resides after the merged block. (#9080)\r
+               var walkerRng = nextCursor.clone(), body = editor.document.getBody();\r
+               walkerRng.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );\r
+               var walker = new CKEDITOR.dom.walker( walkerRng );\r
+               walker.evaluator = function( node ) { return nonEmpty( node ) && !blockBogus( node ); };\r
+               var next = walker.next();\r
+               if ( next && next.type == CKEDITOR.NODE_ELEMENT && next.getName() in CKEDITOR.dtd.$list )\r
+                       mergeListSiblings( next );\r
+\r
+               cursor.moveToBookmark( bm );\r
+\r
                // Make fresh selection.\r
                cursor.select();\r
 \r
+               editor.selectionChange( 1 );\r
                editor.fire( 'saveSnapshot' );\r
        }\r
 \r
@@ -861,9 +904,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, numberedListCommand ) );\r
                        editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, bulletedListCommand ) );\r
 \r
-                       // [IE8] Fix "backspace" after list and "del" at the end of list item. (#8248)\r
-                       if ( CKEDITOR.env.ie8Compat )\r
-                       {\r
+                               // Handled backspace/del key to join list items. (#8248,#9080)\r
                                editor.on( 'key', function( evt )\r
                                {\r
                                        var key = evt.data.keyCode;\r
@@ -886,22 +927,49 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                if ( isBackspace )\r
                                                {\r
-                                                       walker.range.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
-                                                       walker.range.setEnd( range.startContainer, range.startOffset );\r
+                                                       var previous, joinWith;\r
 \r
-                                                       var previous = walker.previous();\r
+                                                       var path = new CKEDITOR.dom.elementPath( range.startContainer );\r
 \r
-                                                       // Check if cursor collapsed right behind of a list.\r
-                                                       if ( previous &&\r
-                                                                previous.type == CKEDITOR.NODE_ELEMENT &&\r
-                                                                previous.getName() in listNodeNames )\r
+                                                       // Join a sub list's first line, with the previous visual line in parent.\r
+                                                       if ( ( previous = path.contains( listNodeNames ) ) &&\r
+                                                            range.checkBoundaryOfElement( previous, CKEDITOR.START ) &&\r
+                                                            ( previous = previous.getParent() ) && previous.is( 'li' ) &&\r
+                                                            ( previous = getSubList( previous ) ) )\r
+                                                       {\r
+                                                               joinWith = previous;\r
+                                                               previous = previous.getPrevious( nonEmpty );\r
+                                                               // Place cursor before the nested list.\r
+                                                               cursor.moveToPosition(\r
+                                                                       previous && blockBogus( previous ) ? previous : joinWith,\r
+                                                                       CKEDITOR.POSITION_BEFORE_START );\r
+                                                       }\r
+                                                       // Join any line following a list, with the last visual line of the list.\r
+                                                       else\r
                                                        {\r
-                                                               walker.range.selectNodeContents( previous );\r
-                                                               walker.reset();\r
-                                                               walker.evaluator = isTextBlock;\r
+                                                               walker.range.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
+                                                               walker.range.setEnd( range.startContainer, range.startOffset );\r
+                                                               previous = walker.previous();\r
 \r
-                                                               // Place cursor at the end of previous block.\r
-                                                               cursor.moveToElementEditEnd( walker.lastForward() );\r
+                                                               if ( previous && previous.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                                  ( previous.getName() in listNodeNames || previous.is( 'li' ) ) )\r
+                                                               {\r
+                                                                       if ( !previous.is( 'li' ) )\r
+                                                                       {\r
+                                                                               walker.range.selectNodeContents( previous );\r
+                                                                               walker.reset();\r
+                                                                               walker.evaluator = isTextBlock;\r
+                                                                               previous = walker.previous();\r
+                                                                       }\r
+\r
+                                                                       joinWith = previous;\r
+                                                                       // Place cursor at the end of previous block.\r
+                                                                       cursor.moveToElementEditEnd( joinWith );\r
+                                                               }\r
+                                                       }\r
+\r
+                                                       if ( joinWith )\r
+                                                       {\r
                                                                joinNextLineToCursor( editor, cursor, range );\r
                                                                evt.cancel();\r
                                                        }\r
@@ -947,9 +1015,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                        }\r
                                                }\r
+\r
+                                               // The backspace/del could potentially put cursor at a bad position,\r
+                                               // being it handled or not, check immediately the selection to have it fixed.\r
+                                               setTimeout( function() { editor.selectionChange( 1 ); } );\r
                                        }\r
                                } );\r
-                       }\r
                },\r
 \r
                afterInit : function ( editor )\r