/*\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
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
if ( !baseArray )\r
baseArray = [];\r
\r
- // Iterate over all list items to get their contents and look for inner lists.\r
+ // Iterate over all list items to and look for inner lists.\r
for ( var i = 0, count = listNode.getChildCount() ; i < count ; i++ )\r
{\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
- var itemObj = { 'parent' : listNode, indent : baseIndentLevel, contents : [] };\r
+\r
+ var itemObj = { 'parent' : listNode, indent : baseIndentLevel, element : listItem, contents : [] };\r
if ( !grandparentNode )\r
{\r
itemObj.grandparent = listNode.getParent();\r
CKEDITOR.dom.element.setMarker( database, listItem, 'listarray_index', baseArray.length );\r
baseArray.push( itemObj );\r
\r
- for ( var j = 0, itemChildCount = listItem.getChildCount() ; j < itemChildCount ; j++ )\r
+ for ( var j = 0, itemChildCount = listItem.getChildCount(), child; j < itemChildCount ; j++ )\r
{\r
- var child = listItem.getChild( j );\r
+ child = listItem.getChild( j );\r
if ( child.type == CKEDITOR.NODE_ELEMENT && listNodeNames[ child.getName() ] )\r
// Note the recursion here, it pushes inner list items with\r
// +1 indentation in the correct order.\r
},\r
\r
// Convert our internal representation of a list back to a DOM forest.\r
- arrayToList : function( listArray, database, baseIndex, paragraphMode )\r
+ arrayToList : function( listArray, database, baseIndex, paragraphMode, dir )\r
{\r
if ( !baseIndex )\r
baseIndex = 0;\r
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
+ 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, true );\r
+ rootNode = listArray[ currentIndex ].parent.clone( false, 1 );\r
+ dir && rootNode.setAttribute( 'dir', dir );\r
retval.append( rootNode );\r
}\r
- currentListItem = rootNode.append( doc.createElement( 'li' ) );\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
+ else if ( item.indent == -1 && !baseIndex && itemGrandParent )\r
{\r
- currentListItem;\r
- if ( listNodeNames[ item.grandparent.getName() ] )\r
- currentListItem = doc.createElement( 'li' );\r
- else\r
+ if ( listNodeNames[ itemGrandParent.getName() ] )\r
{\r
- if ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' )\r
- currentListItem = doc.createElement( paragraphName );\r
- else\r
- currentListItem = new CKEDITOR.dom.documentFragment( doc );\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
+ child = item.contents[ i ];\r
\r
- for ( i = 0 ; i < item.contents.length ; i++ )\r
- currentListItem.append( item.contents[i].clone( true, true ) );\r
+ if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )\r
+ {\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 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
+ // 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
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
}\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
CKEDITOR.dom.element.setMarker( database, itemNode, 'list_item_processed', true );\r
}\r
\r
- var fakeParent = groupObj.root.getDocument().createElement( this.type );\r
+ var root = groupObj.root,\r
+ 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
newList.listNode.replace( groupObj.root );\r
}\r
\r
+ var headerTagRegex = /^h[1-6]$/;\r
+\r
function createList( editor, groupObj, listsCreated )\r
{\r
var contents = groupObj.contents,\r
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
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
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.moveChildren( listItem );\r
- contentBlock.remove();\r
- listItem.appendTo( listNode );\r
+ contentBlock = listContents.shift();\r
+ listItem = doc.createElement( 'li' );\r
\r
- // Append a bogus BR to force the LI to render at full height\r
- if ( !CKEDITOR.env.ie )\r
- listItem.appendBogus();\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
+ contentBlock.copyAttributes( listItem );\r
+ // Remove direction attribute after it was merged into list root. (#7657)\r
+ if ( listDir && contentBlock.getDirection() )\r
+ {\r
+ listItem.removeStyle( 'direction' );\r
+ listItem.removeAttribute( 'dir' );\r
+ }\r
+ contentBlock.moveChildren( listItem );\r
+ contentBlock.remove();\r
+ }\r
+\r
+ listItem.appendTo( listNode );\r
}\r
+\r
+ // Apply list root dir only if it has been explicitly declared.\r
+ if ( listDir && explicitDirection )\r
+ listNode.setAttribute( 'dir', listDir );\r
+\r
if ( insertAnchor )\r
listNode.insertBefore( insertAnchor );\r
else\r
}\r
}\r
\r
- var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode );\r
+ var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode,\r
+ groupObj.root.getAttribute( 'dir' ) );\r
\r
// Compensate <br> before/after the list node if the surrounds are non-blocks.(#3836)\r
var docFragment = newList.listNode, boundaryNode, siblingNode;\r
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();\r
+ ranges = selection && selection.getRanges( true );\r
\r
// There should be at least one selected range.\r
if ( !ranges || ranges.length < 1 )\r
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.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
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
// Group the blocks up because there are many cases where multiple lists have to be created,\r
// or multiple lists have to be cancelled.\r
var listGroups = [],\r
- database = {};\r
+ database = {},\r
+ rangeIterator = ranges.createIterator(),\r
+ index = 0;\r
\r
- while ( ranges.length > 0 )\r
+ while ( ( range = rangeIterator.getNextRange() ) && ++index )\r
{\r
- range = ranges.shift();\r
-\r
var boundaryNodes = range.getBoundaryNodes(),\r
startNode = boundaryNodes.startNode,\r
endNode = boundaryNodes.endNode;\r
\r
while ( ( block = iterator.getNextParagraph() ) )\r
{\r
+ // Avoid duplicate blocks get processed across ranges.\r
+ if( block.getCustomData( 'list_block' ) )\r
+ continue;\r
+ else\r
+ CKEDITOR.dom.element.setMarker( database, block, 'list_block', 1 );\r
+\r
var path = new CKEDITOR.dom.elementPath( block ),\r
pathElements = path.elements,\r
pathElementsCount = pathElements.length,\r
listNode = null,\r
- processedFlag = false,\r
+ processedFlag = 0,\r
blockLimit = path.blockLimit,\r
element;\r
\r
// no longer be valid. Since paragraphs after the list\r
// should belong to a different group of paragraphs before\r
// the list. (Bug #1309)\r
- blockLimit.removeCustomData( 'list_group_object' );\r
+ blockLimit.removeCustomData( 'list_group_object_' + index );\r
\r
var groupObj = element.getCustomData( 'list_group_object' );\r
if ( groupObj )\r
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
if ( processedFlag )\r
continue;\r
\r
- // No list ancestor? Group by block limit.\r
+ // No list ancestor? Group by block limit, but don't mix contents from different ranges.\r
var root = blockLimit;\r
- if ( root.getCustomData( 'list_group_object' ) )\r
- root.getCustomData( 'list_group_object' ).contents.push( block );\r
+ if ( root.getCustomData( 'list_group_object_' + index ) )\r
+ root.getCustomData( 'list_group_object_' + index ).contents.push( block );\r
else\r
{\r
groupObj = { root : root, contents : [ block ] };\r
- CKEDITOR.dom.element.setMarker( database, root, 'list_group_object', groupObj );\r
+ CKEDITOR.dom.element.setMarker( database, root, 'list_group_object_' + index, groupObj );\r
listGroups.push( groupObj );\r
}\r
}\r
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 ? true : false );\r
- }\r
- } )();\r
- mergeSibling( true );\r
- }\r
+ mergeListSiblings( listsCreated[ i ] );\r
\r
// Clean up, restore selection and update toolbar button states.\r
CKEDITOR.dom.element.clearAllMarkers( database );\r
}\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 ]*(?: |\xa0)$/;\r
\r
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
// 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