X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Flist%2Fplugin.js;h=152be3c4f6b68ced9ae851a509f46ef656d07ac8;hb=2f22c0c38f17e75be5541089076885442aaa2377;hp=b0d5e8cf63103dd8f58b52da3e361eb19b99c780;hpb=c9fdde67e6384bd5a66adc2b3bba5c4ce9db56c7;p=ckeditor.git diff --git a/_source/plugins/list/plugin.js b/_source/plugins/list/plugin.js index b0d5e8c..152be3c 100644 --- a/_source/plugins/list/plugin.js +++ b/_source/plugins/list/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -12,6 +12,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var listNodeNames = { ol : 1, ul : 1 }, emptyTextRegex = /^[\n\r\t ]*$/; + var whitespaces = CKEDITOR.dom.walker.whitespaces(), + bookmarks = CKEDITOR.dom.walker.bookmark(), + nonEmpty = function( node ){ return !( whitespaces( node ) || bookmarks( node ) );}, + blockBogus = CKEDITOR.dom.walker.bogus(); + + function cleanUpDirection( element ) + { + var dir, parent, parentDir; + if ( ( dir = element.getDirection() ) ) + { + parent = element.getParent(); + while ( parent && !( parentDir = parent.getDirection() ) ) + parent = parent.getParent(); + + if ( dir == parentDir ) + element.removeAttribute( 'dir' ); + } + } + CKEDITOR.plugins.list = { /* * Convert a DOM list tree into a data structure that is easier to @@ -34,6 +53,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var listItem = listNode.getChild( i ); + // Fixing malformed nested lists by moving it into a previous list item. (#6236) + if( listItem.type == CKEDITOR.NODE_ELEMENT && listItem.getName() in CKEDITOR.dtd.$list ) + CKEDITOR.plugins.list.listToArray( listItem, database, baseArray, baseIndentLevel + 1 ); + // It may be a text node or some funny stuff. if ( listItem.$.nodeName.toLowerCase() != 'li' ) continue; @@ -73,76 +96,122 @@ For licensing, see LICENSE.html or http://ckeditor.com/license baseIndex = 0; if ( !listArray || listArray.length < baseIndex + 1 ) return null; - var doc = listArray[ baseIndex ].parent.getDocument(), + var i, + doc = listArray[ baseIndex ].parent.getDocument(), retval = new CKEDITOR.dom.documentFragment( doc ), rootNode = null, currentIndex = baseIndex, indentLevel = Math.max( listArray[ baseIndex ].indent, 0 ), currentListItem = null, + orgDir, + block, paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' ); - while ( true ) + while ( 1 ) { var item = listArray[ currentIndex ]; + + orgDir = item.element.getDirection( 1 ); + if ( item.indent == indentLevel ) { if ( !rootNode || listArray[ currentIndex ].parent.getName() != rootNode.getName() ) { - rootNode = listArray[ currentIndex ].parent.clone( false, true ); + rootNode = listArray[ currentIndex ].parent.clone( false, 1 ); + dir && rootNode.setAttribute( 'dir', dir ); retval.append( rootNode ); } - currentListItem = rootNode.append( item.element.clone( false, true ) ); - for ( var i = 0 ; i < item.contents.length ; i++ ) - currentListItem.append( item.contents[i].clone( true, true ) ); + currentListItem = rootNode.append( item.element.clone( 0, 1 ) ); + + if ( orgDir != rootNode.getDirection( 1 ) ) + currentListItem.setAttribute( 'dir', orgDir ); + + for ( i = 0 ; i < item.contents.length ; i++ ) + currentListItem.append( item.contents[i].clone( 1, 1 ) ); currentIndex++; } else if ( item.indent == Math.max( indentLevel, 0 ) + 1 ) { - var listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode ); + // Maintain original direction (#6861). + var currDir = listArray[ currentIndex - 1 ].element.getDirection( 1 ), + listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode, + currDir != orgDir ? orgDir: null ); + + // If the next block is an
  • with another list tree as the first + // child, we'll need to append a filler (
    /NBSP) or the list item + // wouldn't be editable. (#6724) + if ( !currentListItem.getChildCount() && CKEDITOR.env.ie && !( doc.$.documentMode > 7 )) + currentListItem.append( doc.createText( '\xa0' ) ); currentListItem.append( listData.listNode ); currentIndex = listData.nextIndex; } else if ( item.indent == -1 && !baseIndex && item.grandparent ) { - currentListItem; if ( listNodeNames[ item.grandparent.getName() ] ) currentListItem = item.element.clone( false, true ); else + currentListItem = new CKEDITOR.dom.documentFragment( doc ); + + // Migrate all children to the new container, + // apply the proper text direction. + var dirLoose = item.grandparent.getDirection( 1 ) != orgDir, + needsBlock = currentListItem.type == CKEDITOR.NODE_DOCUMENT_FRAGMENT && + paragraphMode != CKEDITOR.ENTER_BR, + li = item.element, + className = li.getAttribute( 'class' ), + style = li.getAttribute( 'style' ); + + var child, count = item.contents.length; + for ( i = 0 ; i < count; i++ ) { - // Create completely new blocks here, attributes are dropped. - if ( dir || ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' ) ) + child = item.contents[ i ]; + + if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() ) { - currentListItem = doc.createElement( paragraphName ); - if ( dir ) - currentListItem.setAttribute( 'dir', dir ); + // Apply direction on content blocks. + if ( dirLoose && !child.getDirection() ) + child.setAttribute( 'dir', orgDir ); + + // Merge into child styles. + style && child.setAttribute( 'style', + style.replace( /([^;])$/, '$1;') + + ( child.getAttribute( 'style' ) || '' ) ); + + className && child.addClass( className ); } - else - currentListItem = new CKEDITOR.dom.documentFragment( doc ); - } + else if ( dirLoose || needsBlock || style || className ) + { + // Establish new block to hold text direction and styles. + if ( !block ) + { + block = doc.createElement( paragraphName ); + dirLoose && block.setAttribute( 'dir', orgDir ); + } - for ( i = 0 ; i < item.contents.length ; i++ ) - currentListItem.append( item.contents[i].clone( true, true ) ); + // Copy over styles to new block; + style && block.setAttribute( 'style', style ); + className && block.setAttribute( 'class', className ); + + block.append( child.clone( 1, 1 ) ); + } + + currentListItem.append( block || child.clone( 1, 1 ) ); + } if ( currentListItem.type == CKEDITOR.NODE_DOCUMENT_FRAGMENT && currentIndex != listArray.length - 1 ) { - if ( currentListItem.getLast() - && currentListItem.getLast().type == CKEDITOR.NODE_ELEMENT - && currentListItem.getLast().getAttribute( 'type' ) == '_moz' ) - currentListItem.getLast().remove(); - currentListItem.appendBogus(); - } + var last = currentListItem.getLast(); + if ( last && last.type == CKEDITOR.NODE_ELEMENT + && last.getAttribute( 'type' ) == '_moz' ) + { + last.remove(); + } - if ( currentListItem.type == CKEDITOR.NODE_ELEMENT && - currentListItem.getName() == paragraphName && - currentListItem.$.firstChild ) - { - currentListItem.trim(); - var firstChild = currentListItem.getFirst(); - if ( firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.isBlockBoundary() ) + if ( !( last = currentListItem.getLast( nonEmpty ) + && last.type == CKEDITOR.NODE_ELEMENT + && last.getName() in CKEDITOR.dtd.$block ) ) { - var tmp = new CKEDITOR.dom.documentFragment( doc ); - currentListItem.moveChildren( tmp ); - currentListItem = tmp; + currentListItem.append( doc.createElement( 'br' ) ); } } @@ -156,18 +225,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license else return null; + block = null; + if ( listArray.length <= currentIndex || Math.max( listArray[ currentIndex ].indent, 0 ) < indentLevel ) break; } - // Clear marker attributes for the new list tree made of cloned nodes, if any. if ( database ) { - var currentNode = retval.getFirst(); + var currentNode = retval.getFirst(), + listRoot = listArray[ 0 ].parent; + while ( currentNode ) { if ( currentNode.type == CKEDITOR.NODE_ELEMENT ) + { + // Clear marker attributes for the new list tree made of cloned nodes, if any. CKEDITOR.dom.element.clearMarkers( database, currentNode ); + + // Clear redundant direction attribute specified on list items. + if ( currentNode.getName() in CKEDITOR.dtd.$listItem ) + cleanUpDirection( currentNode ); + } + currentNode = currentNode.getNextSourceNode(); } } @@ -176,30 +256,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }; - function setState( editor, state ) - { - editor.getCommand( this.name ).setState( state ); - } - function onSelectionChange( evt ) { + if ( evt.editor.readOnly ) + return null; + var path = evt.data.path, blockLimit = path.blockLimit, elements = path.elements, - element; + element, + i; // Grouping should only happen under blockLimit.(#3940). - for ( var i = 0 ; i < elements.length && ( element = elements[ i ] ) + for ( i = 0 ; i < elements.length && ( element = elements[ i ] ) && !element.equals( blockLimit ); i++ ) { - if ( listNodeNames[ elements[i].getName() ] ) - { - return setState.call( this, evt.editor, - this.type == elements[i].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF ); - } + if ( listNodeNames[ elements[ i ].getName() ] ) + return this.setState( this.type == elements[ i ].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF ); } - return setState.call( this, evt.editor, CKEDITOR.TRISTATE_OFF ); + return this.setState( CKEDITOR.TRISTATE_OFF ); } function changeListType( editor, groupObj, database, listsCreated ) @@ -268,6 +344,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license for ( var i = 0 ; i < contents.length ; i++ ) commonParent = commonParent.getCommonAncestor( contents[i].getParent() ); + var useComputedState = editor.config.useComputedState, + listDir, explicitDirection; + + useComputedState = useComputedState === undefined || useComputedState; + // We want to insert things that are in the same tree level only, so calculate the contents again // by expanding the selected blocks to the same tree level. for ( i = 0 ; i < contents.length ; i++ ) @@ -279,6 +360,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( parentNode.equals( commonParent ) ) { listContents.push( contentNode ); + + // Determine the lists's direction. + if ( !explicitDirection && contentNode.getDirection() ) + explicitDirection = 1; + + var itemDir = contentNode.getDirection( useComputedState ); + + if ( listDir !== null ) + { + // If at least one LI have a different direction than current listDir, we can't have listDir. + if ( listDir && listDir != itemDir ) + listDir = null; + else + listDir = itemDir; + } + break; } contentNode = parentNode; @@ -290,39 +387,39 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Insert the list to the DOM tree. var insertAnchor = listContents[ listContents.length - 1 ].getNext(), - listNode = doc.createElement( this.type ), - dir; + listNode = doc.createElement( this.type ); listsCreated.push( listNode ); + + var contentBlock, listItem; + while ( listContents.length ) { - var contentBlock = listContents.shift(), - listItem = doc.createElement( 'li' ); + contentBlock = listContents.shift(); + listItem = doc.createElement( 'li' ); // Preserve preformat block and heading structure when converting to list item. (#5335) (#5271) if ( contentBlock.is( 'pre' ) || headerTagRegex.test( contentBlock.getName() ) ) contentBlock.appendTo( listItem ); else { - if ( contentBlock.hasAttribute( 'dir' ) ) + contentBlock.copyAttributes( listItem ); + // Remove direction attribute after it was merged into list root. (#7657) + if ( listDir && contentBlock.getDirection() ) { - dir = dir || contentBlock.getAttribute( 'dir' ); - contentBlock.removeAttribute( 'dir' ); + listItem.removeStyle( 'direction' ); + listItem.removeAttribute( 'dir' ); } - contentBlock.copyAttributes( listItem ); contentBlock.moveChildren( listItem ); contentBlock.remove(); - - // Append a bogus BR to force the LI to render at full height - if ( !CKEDITOR.env.ie ) - listItem.appendBogus(); } listItem.appendTo( listNode ); } - if ( dir ) - listNode.setAttribute( 'dir', dir ); + // Apply list root dir only if it has been explicitly declared. + if ( listDir && explicitDirection ) + listNode.setAttribute( 'dir', listDir ); if ( insertAnchor ) listNode.insertBefore( insertAnchor ); @@ -399,12 +496,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this.type = type; } + var elementType = CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT ); + // Merge list items with direction preserved. (#7448) + function mergeListItems( from, into, refNode, toHead ) + { + var child, itemDir; + while ( ( child = from.getFirst( elementType ) ) ) + { + if ( ( itemDir = child.getDirection( 1 ) ) !== into.getDirection( 1 ) ) + child.setAttribute( 'dir', itemDir ); + + child.remove(); + + refNode ? + child[ toHead ? 'insertBefore' : 'insertAfter' ]( refNode ) : + into.append( child, toHead ); + } + } + listCommand.prototype = { exec : function( editor ) { - editor.focus(); - var doc = editor.document, + config = editor.config, selection = editor.getSelection(), ranges = selection && selection.getRanges( true ); @@ -418,21 +532,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( this.state == CKEDITOR.TRISTATE_OFF ) { var body = doc.getBody(); - body.trim(); - if ( !body.getFirst() ) + if ( !body.getFirst( nonEmpty ) ) { - var paragraph = doc.createElement( editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' : - ( editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'br' ) ); - paragraph.appendTo( body ); - ranges = new CKEDITOR.dom.rangeList( [ new CKEDITOR.dom.range( doc ) ] ); - // IE exception on inserting anything when anchor inside
    . - if ( paragraph.is( 'br' ) ) - { - ranges[ 0 ].setStartBefore( paragraph ); - ranges[ 0 ].setEndAfter( paragraph ); - } - else - ranges[ 0 ].selectNodeContents( paragraph ); + config.enterMode == CKEDITOR.ENTER_BR ? + body.appendBogus() : + ranges[ 0 ].fixBlock( 1, config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ); + selection.selectRanges( ranges ); } // Maybe a single range there enclosing the whole list, @@ -443,9 +548,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license enclosedNode = range && range.getEnclosedNode(); if ( enclosedNode && enclosedNode.is && this.type == enclosedNode.getName() ) - { - setState.call( this, editor, CKEDITOR.TRISTATE_ON ); - } + this.setState( CKEDITOR.TRISTATE_ON ); } } @@ -487,7 +590,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license pathElements = path.elements, pathElementsCount = pathElements.length, listNode = null, - processedFlag = false, + processedFlag = 0, blockLimit = path.blockLimit, element; @@ -513,7 +616,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license listGroups.push( groupObj ); CKEDITOR.dom.element.setMarker( database, element, 'list_group_object', groupObj ); } - processedFlag = true; + processedFlag = 1; break; } } @@ -552,24 +655,27 @@ For licensing, see LICENSE.html or http://ckeditor.com/license removeList.call( this, editor, groupObj, database ); } - // For all new lists created, merge adjacent, same type lists. + // For all new lists created, merge into adjacent, same type lists. for ( i = 0 ; i < listsCreated.length ; i++ ) { listNode = listsCreated[i]; var mergeSibling, listCommand = this; - ( mergeSibling = function( rtl ){ + ( mergeSibling = function( rtl ) + { var sibling = listNode[ rtl ? 'getPrevious' : 'getNext' ]( CKEDITOR.dom.walker.whitespaces( true ) ); if ( sibling && sibling.getName && sibling.getName() == listCommand.type ) { - sibling.remove(); // Move children order by merge direction.(#3820) - sibling.moveChildren( listNode, rtl ? true : false ); + mergeListItems( listNode, sibling, null, !rtl ); + + listNode.remove(); + listNode = sibling; } } )(); - mergeSibling( true ); + mergeSibling( 1 ); } // Clean up, restore selection and update toolbar button states. @@ -641,15 +747,103 @@ For licensing, see LICENSE.html or http://ckeditor.com/license for ( i in dtd.$listItem ) defaultListHtmlFilterRules.elements[ i ] = getExtendNestedListFilter( true ); + // Check if node is block element that recieves text. + function isTextBlock( node ) + { + return node.type == CKEDITOR.NODE_ELEMENT && + ( node.getName() in CKEDITOR.dtd.$block || + node.getName() in CKEDITOR.dtd.$listItem ) && + CKEDITOR.dtd[ node.getName() ][ '#' ]; + } + + // Merge the visual line content at the cursor range into the block. + function joinNextLineToCursor( editor, cursor, nextCursor ) + { + editor.fire( 'saveSnapshot' ); + + // Merge with previous block's content. + nextCursor.enlarge( CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ); + var frag = nextCursor.extractContents(); + + cursor.trim( false, true ); + + // Kill original bogus; + var currentPath = new CKEDITOR.dom.elementPath( cursor.startContainer ); + var currentLi = currentPath.lastElement.getAscendant( 'li', 1 ); + + var bogus = currentPath.block.getBogus(); + bogus && bogus.remove(); + + // Kill the tail br in extracted. + var last = frag.getLast(); + if ( last && last.type == CKEDITOR.NODE_ELEMENT && last.is( 'br' ) ) + last.remove(); + + // Insert fragment at the range position. + var nextNode = cursor.startContainer.getChild( cursor.startOffset ); + if ( nextNode ) + frag.insertBefore( nextNode ); + else + cursor.startContainer.append( frag ); + + var nextPath = new CKEDITOR.dom.elementPath( nextCursor.startContainer ); + var nextLi = nextCursor.startContainer.getAscendant( 'li', 1 ); + + // Move the sub list nested in the next list item. + if ( nextLi ) + { + var sublist = getSubList( nextLi ); + if ( sublist ) + { + // If next line is in the sub list of the current list item. + if ( currentLi.contains( nextLi ) ) + { + mergeListItems( sublist, nextLi.getParent(), nextLi ); + sublist.remove(); + } + // Migrate the sub list to current list item. + else + currentLi.append( sublist ); + } + } + + + if ( nextCursor.checkStartOfBlock() && + nextCursor.checkEndOfBlock() ) + { + var nextBlock = nextPath.block, + parentBlock = nextBlock.getParent(); + + nextBlock.remove(); + + // Remove if the path block container is now empty, e.g. li. + if ( parentBlock && + !parentBlock.getFirst( nonEmpty ) && + !parentBlock.equals( nextPath.blockLimit ) ) + { + parentBlock.remove(); + } + } + + // Make fresh selection. + cursor.select(); + + editor.fire( 'saveSnapshot' ); + } + + function getSubList( li ) + { + var last = li.getLast( nonEmpty ); + return last && last.type == CKEDITOR.NODE_ELEMENT && last.getName() in listNodeNames ? last : null; + } + CKEDITOR.plugins.add( 'list', { init : function( editor ) { // Register commands. - var numberedListCommand = new listCommand( 'numberedlist', 'ol' ), - bulletedListCommand = new listCommand( 'bulletedlist', 'ul' ); - editor.addCommand( 'numberedlist', numberedListCommand ); - editor.addCommand( 'bulletedlist', bulletedListCommand ); + var numberedListCommand = editor.addCommand( 'numberedlist', new listCommand( 'numberedlist', 'ol' ) ), + bulletedListCommand = editor.addCommand( 'bulletedlist', new listCommand( 'bulletedlist', 'ul' ) ); // Register the toolbar button. editor.ui.addButton( 'NumberedList', @@ -666,6 +860,96 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Register the state changing handlers. editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, numberedListCommand ) ); editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, bulletedListCommand ) ); + + // [IE8] Fix "backspace" after list and "del" at the end of list item. (#8248) + if ( CKEDITOR.env.ie8Compat ) + { + editor.on( 'key', function( evt ) + { + var key = evt.data.keyCode; + + // DEl/BACKSPACE + if ( editor.mode == 'wysiwyg' && key in { 8 : 1, 46 : 1 } ) + { + var sel = editor.getSelection(), + range = sel.getRanges()[ 0 ]; + + if ( !range.collapsed ) + return; + + var isBackspace = key == 8; + var body = editor.document.getBody(); + var walker = new CKEDITOR.dom.walker( range.clone() ); + walker.evaluator = function( node ) { return nonEmpty( node ) && !blockBogus( node ); }; + + var cursor = range.clone(); + + if ( isBackspace ) + { + walker.range.setStartAt( body, CKEDITOR.POSITION_AFTER_START ); + walker.range.setEnd( range.startContainer, range.startOffset ); + + var previous = walker.previous(); + + // Check if cursor collapsed right behind of a list. + if ( previous && + previous.type == CKEDITOR.NODE_ELEMENT && + previous.getName() in listNodeNames ) + { + walker.range.selectNodeContents( previous ); + walker.reset(); + walker.evaluator = isTextBlock; + + // Place cursor at the end of previous block. + cursor.moveToElementEditEnd( walker.lastForward() ); + joinNextLineToCursor( editor, cursor, range ); + evt.cancel(); + } + } + else + { + var li = range.startContainer.getAscendant( 'li', 1 ); + if ( li ) + { + walker.range.setEndAt( body, CKEDITOR.POSITION_BEFORE_END ); + + var last = li.getLast( nonEmpty ); + var block = last && isTextBlock( last ) ? last : li; + + // Indicate cursor at the visual end of an list item. + var isAtEnd = 0; + + var next = walker.next(); + + // When list item contains a sub list. + if ( next && next.type == CKEDITOR.NODE_ELEMENT && + next.getName() in listNodeNames + && next.equals( last ) ) + { + isAtEnd = 1; + + // Move to the first item in sub list. + next = walker.next(); + } + // Right at the end of list item. + else if ( range.checkBoundaryOfElement( block, CKEDITOR.END ) ) + isAtEnd = 1; + + + if ( isAtEnd && next ) + { + // Put cursor range there. + var nextLine = range.clone(); + nextLine.moveToElementEditStart( next ); + + joinNextLineToCursor( editor, cursor, nextLine ); + evt.cancel(); + } + } + } + } + } ); + } }, afterInit : function ( editor )