JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / list / plugin.js
index b0d5e8c..152be3c 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, 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
@@ -12,6 +12,25 @@ 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
+               blockBogus = CKEDITOR.dom.walker.bogus();\r
+\r
+       function cleanUpDirection( element )\r
+       {\r
+               var dir, parent, parentDir;\r
+               if ( ( dir = element.getDirection() ) )\r
+               {\r
+                       parent = element.getParent();\r
+                       while ( parent && !( parentDir = parent.getDirection() ) )\r
+                               parent = parent.getParent();\r
+\r
+                       if ( dir == parentDir )\r
+                               element.removeAttribute( 'dir' );\r
+               }\r
+       }\r
+\r
        CKEDITOR.plugins.list = {\r
                /*\r
                 * Convert a DOM list tree into a data structure that is easier to\r
@@ -34,6 +53,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                var listItem = listNode.getChild( i );\r
 \r
+                               // Fixing malformed nested lists by moving it into a previous list item. (#6236)\r
+                               if( listItem.type == CKEDITOR.NODE_ELEMENT && listItem.getName() in CKEDITOR.dtd.$list )\r
+                                       CKEDITOR.plugins.list.listToArray( listItem, database, baseArray, baseIndentLevel + 1 );\r
+\r
                                // It may be a text node or some funny stuff.\r
                                if ( listItem.$.nodeName.toLowerCase() != 'li' )\r
                                        continue;\r
@@ -73,76 +96,122 @@ 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 ( true )\r
+                       while ( 1 )\r
                        {\r
                                var item = listArray[ currentIndex ];\r
+\r
+                               orgDir = item.element.getDirection( 1 );\r
+\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
-                                       for ( var i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                       currentListItem = rootNode.append( item.element.clone( 0, 1 ) );\r
+\r
+                                       if ( orgDir != rootNode.getDirection( 1 ) )\r
+                                               currentListItem.setAttribute( 'dir', orgDir );\r
+\r
+                                       for ( i = 0 ; i < item.contents.length ; i++ )\r
+                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
                                        currentIndex++;\r
                                }\r
                                else if ( item.indent == Math.max( indentLevel, 0 ) + 1 )\r
                                {\r
-                                       var listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode );\r
+                                       // Maintain original direction (#6861).\r
+                                       var currDir = listArray[ currentIndex - 1 ].element.getDirection( 1 ),\r
+                                               listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode,\r
+                                               currDir != orgDir ? orgDir: null );\r
+\r
+                                       // If the next block is an <li> with another list tree as the first\r
+                                       // child, we'll need to append a filler (<br>/NBSP) or the list item\r
+                                       // wouldn't be editable. (#6724)\r
+                                       if ( !currentListItem.getChildCount() && CKEDITOR.env.ie && !( doc.$.documentMode > 7 ))\r
+                                               currentListItem.append( doc.createText( '\xa0' ) );\r
                                        currentListItem.append( listData.listNode );\r
                                        currentIndex = listData.nextIndex;\r
                                }\r
                                else if ( item.indent == -1 && !baseIndex && item.grandparent )\r
                                {\r
-                                       currentListItem;\r
                                        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, attributes are dropped.\r
-                                               if ( dir || ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' ) )\r
+                                               child = item.contents[ i ];\r
+\r
+                                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )\r
                                                {\r
-                                                       currentListItem = doc.createElement( paragraphName );\r
-                                                       if ( dir )\r
-                                                               currentListItem.setAttribute( 'dir', dir );\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
-                                       for ( i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                                       // Copy over styles to new block;\r
+                                                       style && block.setAttribute( 'style', style );\r
+                                                       className && block.setAttribute( 'class', className );\r
+\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
                                        {\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
-                                       }\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 ( 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
+                                               if ( !( last = currentListItem.getLast( nonEmpty )\r
+                                                       && last.type == CKEDITOR.NODE_ELEMENT\r
+                                                       && last.getName() in CKEDITOR.dtd.$block ) )\r
                                                {\r
-                                                       var tmp = new CKEDITOR.dom.documentFragment( doc );\r
-                                                       currentListItem.moveChildren( tmp );\r
-                                                       currentListItem = tmp;\r
+                                                       currentListItem.append( doc.createElement( 'br' ) );\r
                                                }\r
                                        }\r
 \r
@@ -156,18 +225,29 @@ 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
 \r
-                       // Clear marker attributes for the new list tree made of cloned nodes, if any.\r
                        if ( database )\r
                        {\r
-                               var currentNode = retval.getFirst();\r
+                               var currentNode = retval.getFirst(),\r
+                                       listRoot = listArray[ 0 ].parent;\r
+\r
                                while ( currentNode )\r
                                {\r
                                        if ( currentNode.type == CKEDITOR.NODE_ELEMENT )\r
+                                       {\r
+                                               // Clear marker attributes for the new list tree made of cloned nodes, if any.\r
                                                CKEDITOR.dom.element.clearMarkers( database, currentNode );\r
+\r
+                                               // Clear redundant direction attribute specified on list items.\r
+                                               if ( currentNode.getName() in CKEDITOR.dtd.$listItem )\r
+                                                       cleanUpDirection( currentNode );\r
+                                       }\r
+\r
                                        currentNode = currentNode.getNextSourceNode();\r
                                }\r
                        }\r
@@ -176,30 +256,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 \r
-       function setState( editor, state )\r
-       {\r
-               editor.getCommand( this.name ).setState( state );\r
-       }\r
-\r
        function onSelectionChange( evt )\r
        {\r
+               if ( evt.editor.readOnly )\r
+                       return null;\r
+\r
                var path = evt.data.path,\r
                        blockLimit = path.blockLimit,\r
                        elements = path.elements,\r
-                       element;\r
+                       element,\r
+                       i;\r
 \r
                // Grouping should only happen under blockLimit.(#3940).\r
-               for ( var i = 0 ; i < elements.length && ( element = elements[ i ] )\r
+               for ( i = 0 ; i < elements.length && ( element = elements[ i ] )\r
                          && !element.equals( blockLimit ); i++ )\r
                {\r
-                       if ( listNodeNames[ elements[i].getName() ] )\r
-                       {\r
-                               return setState.call( this, evt.editor,\r
-                                               this.type == elements[i].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
-                       }\r
+                       if ( listNodeNames[ elements[ i ].getName() ] )\r
+                               return this.setState( this.type == elements[ i ].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
                }\r
 \r
-               return setState.call( this, evt.editor, CKEDITOR.TRISTATE_OFF );\r
+               return this.setState( CKEDITOR.TRISTATE_OFF );\r
        }\r
 \r
        function changeListType( editor, groupObj, database, listsCreated )\r
@@ -268,6 +344,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
@@ -279,6 +360,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
@@ -290,39 +387,39 @@ 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
-                       dir;\r
+                       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 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
+                               contentBlock.copyAttributes( listItem );\r
+                               // Remove direction attribute after it was merged into list root. (#7657)\r
+                               if ( listDir && contentBlock.getDirection() )\r
                                {\r
-                                       dir = dir || contentBlock.getAttribute( 'dir' );\r
-                                       contentBlock.removeAttribute( 'dir' );\r
+                                       listItem.removeStyle( 'direction' );\r
+                                       listItem.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
+               // 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
@@ -399,12 +496,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                this.type = type;\r
        }\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
+               var child, itemDir;\r
+               while ( ( child = from.getFirst( elementType ) ) )\r
+               {\r
+                       if ( ( itemDir = child.getDirection( 1 ) ) !== into.getDirection( 1 ) )\r
+                               child.setAttribute( 'dir', itemDir );\r
+\r
+                       child.remove();\r
+\r
+                       refNode ?\r
+                               child[ toHead ? 'insertBefore' : 'insertAfter' ]( refNode ) :\r
+                               into.append( child, toHead  );\r
+               }\r
+       }\r
+\r
        listCommand.prototype = {\r
                exec : function( editor )\r
                {\r
-                       editor.focus();\r
-\r
                        var doc = editor.document,\r
+                               config = editor.config,\r
                                selection = editor.getSelection(),\r
                                ranges = selection && selection.getRanges( true );\r
 \r
@@ -418,21 +532,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( this.state == CKEDITOR.TRISTATE_OFF )\r
                        {\r
                                var body = doc.getBody();\r
-                               body.trim();\r
-                               if ( !body.getFirst() )\r
+                               if ( !body.getFirst( nonEmpty ) )\r
                                {\r
-                                       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.rangeList( [ new CKEDITOR.dom.range( doc ) ] );\r
-                                       // IE exception on inserting anything when anchor inside <br>.\r
-                                       if ( paragraph.is( 'br' ) )\r
-                                       {\r
-                                               ranges[ 0 ].setStartBefore( paragraph );\r
-                                               ranges[ 0 ].setEndAfter( paragraph );\r
-                                       }\r
-                                       else\r
-                                               ranges[ 0 ].selectNodeContents( paragraph );\r
+                                       config.enterMode == CKEDITOR.ENTER_BR ?\r
+                                               body.appendBogus() :\r
+                                               ranges[ 0 ].fixBlock( 1, config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
+\r
                                        selection.selectRanges( ranges );\r
                                }\r
                                // Maybe a single range there enclosing the whole list,\r
@@ -443,9 +548,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
+                                                       this.setState( CKEDITOR.TRISTATE_ON );\r
                                }\r
                        }\r
 \r
@@ -487,7 +590,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                pathElements = path.elements,\r
                                                pathElementsCount = pathElements.length,\r
                                                listNode = null,\r
-                                               processedFlag = false,\r
+                                               processedFlag = 0,\r
                                                blockLimit = path.blockLimit,\r
                                                element;\r
 \r
@@ -513,7 +616,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
@@ -552,24 +655,27 @@ 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
-                                               sibling.remove();\r
                                                // Move children order by merge direction.(#3820)\r
-                                               sibling.moveChildren( listNode, rtl ? true : false );\r
+                                               mergeListItems( listNode, sibling, null, !rtl );\r
+\r
+                                               listNode.remove();\r
+                                               listNode = sibling;\r
                                        }\r
                                } )();\r
-                               mergeSibling( true );\r
+                               mergeSibling( 1 );\r
                        }\r
 \r
                        // Clean up, restore selection and update toolbar button states.\r
@@ -641,15 +747,103 @@ 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
                {\r
                        // Register commands.\r
-                       var numberedListCommand = new listCommand( 'numberedlist', 'ol' ),\r
-                               bulletedListCommand = new listCommand( 'bulletedlist', 'ul' );\r
-                       editor.addCommand( 'numberedlist', numberedListCommand );\r
-                       editor.addCommand( 'bulletedlist', bulletedListCommand );\r
+                       var numberedListCommand = editor.addCommand( 'numberedlist', new listCommand( 'numberedlist', 'ol' ) ),\r
+                               bulletedListCommand = editor.addCommand( 'bulletedlist', new listCommand( 'bulletedlist', 'ul' ) );\r
 \r
                        // Register the toolbar button.\r
                        editor.ui.addButton( 'NumberedList',\r
@@ -666,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