JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / list / plugin.js
index ad79153..152be3c 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -14,7 +14,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        var whitespaces = CKEDITOR.dom.walker.whitespaces(),\r
                bookmarks = CKEDITOR.dom.walker.bookmark(),\r
-               nonEmpty = function( node ){ return !( whitespaces( node ) || bookmarks( node ) ); };\r
+               nonEmpty = function( node ){ return !( whitespaces( node ) || bookmarks( node ) );},\r
+               blockBogus = CKEDITOR.dom.walker.bogus();\r
 \r
        function cleanUpDirection( element )\r
        {\r
@@ -95,13 +96,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                baseIndex = 0;\r
                        if ( !listArray || listArray.length < baseIndex + 1 )\r
                                return null;\r
-                       var doc = listArray[ baseIndex ].parent.getDocument(),\r
+                       var i,\r
+                               doc = listArray[ baseIndex ].parent.getDocument(),\r
                                retval = new CKEDITOR.dom.documentFragment( doc ),\r
                                rootNode = null,\r
                                currentIndex = baseIndex,\r
                                indentLevel = Math.max( listArray[ baseIndex ].indent, 0 ),\r
                                currentListItem = null,\r
                                orgDir,\r
+                               block,\r
                                paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
                        while ( 1 )\r
                        {\r
@@ -122,7 +125,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        if ( orgDir != rootNode.getDirection( 1 ) )\r
                                                currentListItem.setAttribute( 'dir', orgDir );\r
 \r
-                                       for ( var i = 0 ; i < item.contents.length ; i++ )\r
+                                       for ( i = 0 ; i < item.contents.length ; i++ )\r
                                                currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
                                        currentIndex++;\r
                                }\r
@@ -146,31 +149,53 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        if ( listNodeNames[ item.grandparent.getName() ] )\r
                                                currentListItem = item.element.clone( false, true );\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
+                                               li = item.element,\r
+                                               className = li.getAttribute( 'class' ),\r
+                                               style = li.getAttribute( 'style' );\r
+\r
+                                       var child, count = item.contents.length;\r
+                                       for ( i = 0 ; i < count; i++ )\r
                                        {\r
-                                               // Create completely new blocks here.\r
-                                               if ( dir || item.element.hasAttributes() || paragraphMode != CKEDITOR.ENTER_BR )\r
+                                               child = item.contents[ i ];\r
+\r
+                                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )\r
                                                {\r
-                                                       currentListItem = doc.createElement( paragraphName );\r
-                                                       item.element.copyAttributes( currentListItem, { type:1, value:1 } );\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
+                                                       // Apply direction on content blocks.\r
+                                                       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
+\r
+                                                       className && child.addClass( className );\r
                                                }\r
-                                               else\r
-                                                       currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
-                                       }\r
+                                               else if ( dirLoose || needsBlock || style || className )\r
+                                               {\r
+                                                       // Establish new block to hold text direction and styles.\r
+                                                       if ( !block )\r
+                                                       {\r
+                                                               block = doc.createElement( paragraphName );\r
+                                                               dirLoose && block.setAttribute( 'dir', orgDir );\r
+                                                       }\r
 \r
-                                       if ( currentListItem.type == CKEDITOR.NODE_ELEMENT )\r
-                                       {\r
-                                               if ( item.grandparent.getDirection( 1 ) != orgDir )\r
-                                                       currentListItem.setAttribute( 'dir', orgDir );\r
-                                       }\r
+                                                       // Copy over styles to new block;\r
+                                                       style && block.setAttribute( 'style', style );\r
+                                                       className && block.setAttribute( 'class', className );\r
 \r
-                                       for ( i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
+                                                       block.append( child.clone( 1, 1 ) );\r
+                                               }\r
+\r
+                                               currentListItem.append( block || child.clone( 1, 1 ) );\r
+                                       }\r
 \r
                                        if ( currentListItem.type == CKEDITOR.NODE_DOCUMENT_FRAGMENT\r
                                                 && currentIndex != listArray.length - 1 )\r
@@ -190,20 +215,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                }\r
                                        }\r
 \r
-                                       if ( currentListItem.type == CKEDITOR.NODE_ELEMENT &&\r
-                                                       currentListItem.getName() == paragraphName &&\r
-                                                       currentListItem.$.firstChild )\r
-                                       {\r
-                                               currentListItem.trim();\r
-                                               var firstChild = currentListItem.getFirst();\r
-                                               if ( firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.isBlockBoundary() )\r
-                                               {\r
-                                                       var tmp = new CKEDITOR.dom.documentFragment( doc );\r
-                                                       currentListItem.moveChildren( tmp );\r
-                                                       currentListItem = tmp;\r
-                                               }\r
-                                       }\r
-\r
                                        var currentListItemName = currentListItem.$.nodeName.toLowerCase();\r
                                        if ( !CKEDITOR.env.ie && ( currentListItemName == 'div' || currentListItemName == 'p' ) )\r
                                                currentListItem.appendBogus();\r
@@ -214,6 +225,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                else\r
                                        return null;\r
 \r
+                               block = null;\r
+\r
                                if ( listArray.length <= currentIndex || Math.max( listArray[ currentIndex ].indent, 0 ) < indentLevel )\r
                                        break;\r
                        }\r
@@ -483,19 +496,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                this.type = type;\r
        }\r
 \r
-       // Move direction attribute from root to list items.\r
-       function dirToListItems( list )\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
-               var dir = list.getDirection();\r
-               if ( dir )\r
+               var child, itemDir;\r
+               while ( ( child = from.getFirst( elementType ) ) )\r
                {\r
-                       for ( var i = 0, children = list.getChildren(), child; child = children.getItem( i ), i < children.count(); i++ )\r
-                       {\r
-                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.is( 'li' ) && !child.getDirection() )\r
-                                       child.setAttribute( 'dir', dir );\r
-                       }\r
+                       if ( ( itemDir = child.getDirection( 1 ) ) !== into.getDirection( 1 ) )\r
+                               child.setAttribute( 'dir', itemDir );\r
 \r
-                       list.removeAttribute( 'dir' );\r
+                       child.remove();\r
+\r
+                       refNode ?\r
+                               child[ toHead ? 'insertBefore' : 'insertAfter' ]( refNode ) :\r
+                               into.append( child, toHead  );\r
                }\r
        }\r
 \r
@@ -640,26 +655,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        removeList.call( this, editor, groupObj, database );\r
                        }\r
 \r
-                       // For all new lists created, merge adjacent, same type lists.\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
+                               ( 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
-\r
-                                               // In case to be merged lists have difference directions. (#7448)\r
-                                               if ( sibling.getDirection( 1 ) != listNode.getDirection( 1 ) )\r
-                                                       dirToListItems( listNode.getDirection() ? listNode : sibling );\r
-\r
-                                               sibling.remove();\r
                                                // Move children order by merge direction.(#3820)\r
-                                               sibling.moveChildren( listNode, rtl );\r
+                                               mergeListItems( listNode, sibling, null, !rtl );\r
+\r
+                                               listNode.remove();\r
+                                               listNode = sibling;\r
                                        }\r
                                } )();\r
                                mergeSibling( 1 );\r
@@ -734,6 +747,96 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        for ( i in dtd.$listItem )\r
                defaultListHtmlFilterRules.elements[ i ] = getExtendNestedListFilter( true );\r
 \r
+       // Check if node is block element that recieves text.\r
+       function isTextBlock( node )\r
+       {\r
+               return node.type == CKEDITOR.NODE_ELEMENT &&\r
+                          ( node.getName() in CKEDITOR.dtd.$block ||\r
+                                node.getName() in CKEDITOR.dtd.$listItem ) &&\r
+                          CKEDITOR.dtd[ node.getName() ][ '#' ];\r
+       }\r
+\r
+       // Merge the visual line content at the cursor range into the block.\r
+       function joinNextLineToCursor( editor, cursor, nextCursor )\r
+       {\r
+               editor.fire( 'saveSnapshot' );\r
+\r
+               // Merge with previous block's content.\r
+               nextCursor.enlarge( CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS );\r
+               var frag = nextCursor.extractContents();\r
+\r
+               cursor.trim( false, true );\r
+\r
+               // Kill original bogus;\r
+               var currentPath = new CKEDITOR.dom.elementPath( cursor.startContainer );\r
+               var currentLi = currentPath.lastElement.getAscendant( 'li', 1 );\r
+\r
+               var bogus = currentPath.block.getBogus();\r
+               bogus && bogus.remove();\r
+\r
+               // Kill the tail br in extracted.\r
+               var last = frag.getLast();\r
+               if ( last && last.type == CKEDITOR.NODE_ELEMENT && last.is( 'br' ) )\r
+                       last.remove();\r
+\r
+               // Insert fragment at the range position.\r
+               var nextNode = cursor.startContainer.getChild( cursor.startOffset );\r
+               if ( nextNode )\r
+                       frag.insertBefore( nextNode );\r
+               else\r
+                       cursor.startContainer.append( frag );\r
+\r
+               var nextPath = new CKEDITOR.dom.elementPath( nextCursor.startContainer );\r
+               var nextLi = nextCursor.startContainer.getAscendant( 'li', 1 );\r
+\r
+               // Move the sub list nested in the next list item.\r
+               if ( nextLi )\r
+               {\r
+                       var sublist = getSubList( nextLi );\r
+                       if ( sublist )\r
+                       {\r
+                               // 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
+                                       sublist.remove();\r
+                               }\r
+                               // Migrate the sub list to current list item.\r
+                               else\r
+                                       currentLi.append( sublist );\r
+                       }\r
+               }\r
+\r
+\r
+               if ( nextCursor.checkStartOfBlock() &&\r
+                        nextCursor.checkEndOfBlock() )\r
+               {\r
+                       var nextBlock = nextPath.block,\r
+                               parentBlock = nextBlock.getParent();\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
+                       {\r
+                               parentBlock.remove();\r
+                       }\r
+               }\r
+\r
+               // Make fresh selection.\r
+               cursor.select();\r
+\r
+               editor.fire( 'saveSnapshot' );\r
+       }\r
+\r
+       function getSubList( li )\r
+       {\r
+               var last = li.getLast( nonEmpty );\r
+               return last && last.type == CKEDITOR.NODE_ELEMENT && last.getName() in listNodeNames ? last : null;\r
+       }\r
+\r
        CKEDITOR.plugins.add( 'list',\r
        {\r
                init : function( editor )\r
@@ -757,6 +860,96 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // Register the state changing handlers.\r
                        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
+                               editor.on( 'key', function( evt )\r
+                               {\r
+                                       var key = evt.data.keyCode;\r
+\r
+                                       // DEl/BACKSPACE\r
+                                       if ( editor.mode == 'wysiwyg' && key in { 8 : 1, 46 : 1 } )\r
+                                       {\r
+                                               var sel = editor.getSelection(),\r
+                                               range = sel.getRanges()[ 0 ];\r
+\r
+                                               if ( !range.collapsed )\r
+                                                       return;\r
+\r
+                                               var isBackspace = key == 8;\r
+                                               var body = editor.document.getBody();\r
+                                               var walker = new CKEDITOR.dom.walker( range.clone() );\r
+                                               walker.evaluator = function( node ) { return nonEmpty( node ) && !blockBogus( node ); };\r
+\r
+                                               var cursor = range.clone();\r
+\r
+                                               if ( isBackspace )\r
+                                               {\r
+                                                       walker.range.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
+                                                       walker.range.setEnd( range.startContainer, range.startOffset );\r
+\r
+                                                       var previous = walker.previous();\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
+                                                       {\r
+                                                               walker.range.selectNodeContents( previous );\r
+                                                               walker.reset();\r
+                                                               walker.evaluator = isTextBlock;\r
+\r
+                                                               // Place cursor at the end of previous block.\r
+                                                               cursor.moveToElementEditEnd( walker.lastForward() );\r
+                                                               joinNextLineToCursor( editor, cursor, range );\r
+                                                               evt.cancel();\r
+                                                       }\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       var li = range.startContainer.getAscendant( 'li', 1 );\r
+                                                       if ( li )\r
+                                                       {\r
+                                                               walker.range.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );\r
+\r
+                                                               var last = li.getLast( nonEmpty );\r
+                                                               var block = last && isTextBlock( last ) ? last : li;\r
+\r
+                                                               // Indicate cursor at the visual end of an list item.\r
+                                                               var isAtEnd = 0;\r
+\r
+                                                               var next = walker.next();\r
+\r
+                                                               // When list item contains a sub list.\r
+                                                               if ( next && next.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                                        next.getName() in listNodeNames\r
+                                                                        && next.equals( last ) )\r
+                                                               {\r
+                                                                       isAtEnd = 1;\r
+\r
+                                                                       // Move to the first item in sub list.\r
+                                                                       next = walker.next();\r
+                                                               }\r
+                                                               // Right at the end of list item.\r
+                                                               else if ( range.checkBoundaryOfElement( block, CKEDITOR.END ) )\r
+                                                                       isAtEnd = 1;\r
+\r
+\r
+                                                               if ( isAtEnd && next )\r
+                                                               {\r
+                                                                       // Put cursor range there.\r
+                                                                       var nextLine = range.clone();\r
+                                                                       nextLine.moveToElementEditStart( next );\r
+\r
+                                                                       joinNextLineToCursor( editor, cursor, nextLine );\r
+                                                                       evt.cancel();\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               } );\r
+                       }\r
                },\r
 \r
                afterInit : function ( editor )\r