JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.6.1
[ckeditor.git] / _source / plugins / list / plugin.js
index 0b61c2d..3beb9fb 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -12,6 +12,36 @@ 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
+       // 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
@@ -34,6 +64,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,83 +107,127 @@ 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
-                               var item = listArray[ currentIndex ];\r
+                               var item = listArray[ currentIndex ],\r
+                                       itemGrandParent = item.grandparent;\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, 1 );\r
+                                               dir && rootNode.setAttribute( 'dir', dir );\r
                                                retval.append( rootNode );\r
                                        }\r
                                        currentListItem = rootNode.append( item.element.clone( 0, 1 ) );\r
-                                       for ( var i = 0 ; i < item.contents.length ; i++ )\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
+                               else if ( item.indent == -1 && !baseIndex && itemGrandParent )\r
                                {\r
-                                       currentListItem;\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 = 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
-                                               // Create completely new blocks here.\r
-                                               if ( dir || item.element.hasAttributes() ||\r
-                                                       ( 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
-                                                       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
+                                                       // Apply direction on content blocks.\r
+                                                       if ( dirLoose && !child.getDirection() )\r
+                                                               child.setAttribute( 'dir', orgDir );\r
+\r
+                                                       inheirtInlineStyles( li, child );\r
+\r
+                                                       className && child.addClass( className );\r
                                                }\r
-                                               else\r
-                                                       currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
-                                       }\r
+                                               else if ( needsBlock )\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( 1, 1 ) );\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
@@ -163,18 +241,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
@@ -183,30 +272,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
@@ -230,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
@@ -334,14 +429,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                contentBlock.appendTo( listItem );\r
                        else\r
                        {\r
-                               // Remove DIR attribute if it was merged into list root.\r
+                               contentBlock.copyAttributes( listItem );\r
+                               // Remove direction attribute after it was merged into list root. (#7657)\r
                                if ( listDir && contentBlock.getDirection() )\r
                                {\r
-                                       contentBlock.removeStyle( 'direction' );\r
-                                       contentBlock.removeAttribute( 'dir' );\r
+                                       listItem.removeStyle( 'direction' );\r
+                                       listItem.removeAttribute( 'dir' );\r
                                }\r
-\r
-                               contentBlock.copyAttributes( listItem );\r
                                contentBlock.moveChildren( listItem );\r
                                contentBlock.remove();\r
                        }\r
@@ -428,12 +522,30 @@ 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
+\r
+       // Merge child nodes with direction preserved. (#7448)\r
+       function mergeChildren( from, into, refNode, forward )\r
+       {\r
+               var child, itemDir;\r
+               while ( ( child = from[ forward ? 'getLast' : '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[ forward ? 'insertBefore' : 'insertAfter' ]( refNode ) :\r
+                               into.append( child, forward  );\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
@@ -447,21 +559,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
@@ -472,7 +575,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
-                                                       setState.call( this, editor, CKEDITOR.TRISTATE_ON );\r
+                                                       this.setState( CKEDITOR.TRISTATE_ON );\r
                                }\r
                        }\r
 \r
@@ -579,25 +682,9 @@ 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
-\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 );\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
@@ -606,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
@@ -668,15 +775,130 @@ 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
+       // Join visually two block lines.\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
+               var bm = cursor.createBookmark();\r
+\r
+               // Kill original bogus;\r
+               var currentPath = new CKEDITOR.dom.elementPath( cursor.startContainer ),\r
+                               pathBlock = currentPath.block,\r
+                               currentBlock = currentPath.lastElement.getAscendant( 'li', 1 ) || pathBlock,\r
+                               nextPath = new CKEDITOR.dom.elementPath( nextCursor.startContainer ),\r
+                               nextLi = nextPath.contains( CKEDITOR.dtd.$listItem ),\r
+                               nextList = nextPath.contains( CKEDITOR.dtd.$list ),\r
+                               last;\r
+\r
+               // Remove bogus node the current block/pseudo block.\r
+               if ( pathBlock )\r
+               {\r
+                       var bogus = pathBlock.getBogus();\r
+                       bogus && bogus.remove();\r
+               }\r
+               else if ( nextList )\r
+               {\r
+                       last = nextList.getPrevious( nonEmpty );\r
+                       if ( last && blockBogus( last ) )\r
+                               last.remove();\r
+               }\r
+\r
+               // Kill the tail br in extracted.\r
+               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
+               // 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 ( currentBlock.contains( nextLi ) )\r
+                               {\r
+                                       mergeChildren( sublist, nextLi.getParent(), nextLi );\r
+                                       sublist.remove();\r
+                               }\r
+                               // Migrate the sub list to current list item.\r
+                               else\r
+                                       currentBlock.append( sublist );\r
+                       }\r
+               }\r
+\r
+               // Remove any remaining empty path blocks at next line after merging.\r
+               while ( nextCursor.checkStartOfBlock() &&\r
+                        nextCursor.checkEndOfBlock() )\r
+               {\r
+                       nextPath = new CKEDITOR.dom.elementPath( nextCursor.startContainer );\r
+                       var nextBlock = nextPath.block, parent;\r
+\r
+                       // Check if also to remove empty list.\r
+                       if ( nextBlock.is( 'li' ) )\r
+                       {\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
+       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
@@ -693,6 +915,202 @@ 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
+                               // Handled backspace/del key to join list items. (#8248,#9080)\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 path = new CKEDITOR.dom.elementPath( range.startContainer );\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
+                                               // Backspace/Del behavior at the start/end of table is handled in core.\r
+                                               walker.guard = function( node, isOut ) { return !( isOut && node.type == CKEDITOR.NODE_ELEMENT && node.is( 'table' ) ); };\r
+\r
+                                               var cursor = range.clone();\r
+\r
+                                               if ( isBackspace )\r
+                                               {\r
+                                                       var previous, joinWith;\r
+\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.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
+                                                               walker.range.setEnd( range.startContainer, range.startOffset );\r
+                                                               previous = walker.previous();\r
+\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
+                                                       else\r
+                                                       {\r
+                                                               var list = path.contains( listNodeNames ), li;\r
+                                                               // Backspace pressed at the start of list outdents the first list item. (#9129)\r
+                                                               if ( list && range.checkBoundaryOfElement( list, CKEDITOR.START ) )\r
+                                                               {\r
+                                                                       li = list.getFirst( nonEmpty );\r
+\r
+                                                                       if ( range.checkBoundaryOfElement( li, CKEDITOR.START ) )\r
+                                                                       {\r
+                                                                               previous = list.getPrevious( nonEmpty );\r
+\r
+                                                                               // Only if the list item contains a sub list, do nothing but\r
+                                                                               // simply move cursor backward one character.\r
+                                                                               if ( getSubList( li ) )\r
+                                                                               {\r
+                                                                                       if ( previous ) {\r
+                                                                                               range.moveToElementEditEnd( previous );\r
+                                                                                               range.select();\r
+                                                                                       }\r
+\r
+                                                                                       evt.cancel();\r
+                                                                               }\r
+                                                                               else\r
+                                                                               {\r
+                                                                                       editor.execCommand( 'outdent' );\r
+                                                                                       evt.cancel();\r
+                                                                               }\r
+                                                                       }\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       var next, nextLine;\r
+                                                       li = range.startContainer.getAscendant( 'li', 1 );\r
+\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
+                                                               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
+                                                                       nextLine = range.clone();\r
+                                                                       nextLine.moveToElementEditStart( next );\r
+\r
+                                                                       joinNextLineToCursor( editor, cursor, nextLine );\r
+                                                                       evt.cancel();\r
+                                                               }\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               // Handle Del key pressed before the list.\r
+                                                               walker.range.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );\r
+                                                               next = walker.next();\r
+\r
+                                                               if ( next && next.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                                    next.getName() in listNodeNames )\r
+                                                               {\r
+                                                                       // The start <li>\r
+                                                                       next = next.getFirst( nonEmpty );\r
+\r
+                                                                       // Simply remove the current empty block, move cursor to the\r
+                                                                       // subsequent list.\r
+                                                                       if ( path.block &&\r
+                                                                            range.checkStartOfBlock() &&\r
+                                                                            range.checkEndOfBlock() )\r
+                                                                       {\r
+                                                                               path.block.remove();\r
+                                                                               range.moveToElementEditStart( next );\r
+                                                                               range.select();\r
+                                                                               evt.cancel();\r
+                                                                       }\r
+\r
+                                                                       // Preventing the default (merge behavior), but simply move\r
+                                                                       // the cursor one character forward if subsequent list item\r
+                                                                       // contains sub list.\r
+                                                                       else if ( getSubList( next )  )\r
+                                                                       {\r
+                                                                               range.moveToElementEditStart( next );\r
+                                                                               range.select();\r
+                                                                               evt.cancel();\r
+                                                                       }\r
+                                                                       // Merge the first list item with the current line.\r
+                                                                       else\r
+                                                                       {\r
+                                                                               nextLine = range.clone();\r
+                                                                               nextLine.moveToElementEditStart( next );\r
+                                                                               joinNextLineToCursor( editor, cursor, nextLine );\r
+                                                                               evt.cancel();\r
+                                                                       }\r
+                                                               }\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
                afterInit : function ( editor )\r