JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / list / plugin.js
index 0cd962f..bc28e51 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -12,6 +12,10 @@ 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
+\r
        CKEDITOR.plugins.list = {\r
                /*\r
                 * Convert a DOM list tree into a data structure that is easier to\r
@@ -34,6 +38,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
@@ -79,57 +87,97 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                currentIndex = baseIndex,\r
                                indentLevel = Math.max( listArray[ baseIndex ].indent, 0 ),\r
                                currentListItem = null,\r
+                               orgDir,\r
                                paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
-                       while ( true )\r
+                       while ( 1 )\r
                        {\r
                                var item = listArray[ currentIndex ];\r
+\r
+                               orgDir = item.element.getDirection( 1 );\r
+\r
                                if ( item.indent == indentLevel )\r
                                {\r
                                        if ( !rootNode || listArray[ currentIndex ].parent.getName() != rootNode.getName() )\r
                                        {\r
-                                               rootNode = listArray[ currentIndex ].parent.clone( false, true );\r
+                                               rootNode = listArray[ currentIndex ].parent.clone( false, 1 );\r
+                                               dir && rootNode.setAttribute( 'dir', dir );\r
                                                retval.append( rootNode );\r
                                        }\r
-                                       currentListItem = rootNode.append( item.element.clone( false, true ) );\r
+                                       currentListItem = rootNode.append( item.element.clone( 0, 1 ) );\r
+\r
+                                       if ( orgDir != rootNode.getDirection( 1 ) )\r
+                                               currentListItem.setAttribute( 'dir', orgDir );\r
+                                       else\r
+                                               currentListItem.removeAttribute( 'dir' );\r
+\r
                                        for ( var i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
                                        currentIndex++;\r
                                }\r
                                else if ( item.indent == Math.max( indentLevel, 0 ) + 1 )\r
                                {\r
-                                       var listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode );\r
+                                       // Maintain original direction (#6861).\r
+                                       var currDir = listArray[ currentIndex - 1 ].element.getDirection( 1 ),\r
+                                               listData = CKEDITOR.plugins.list.arrayToList( listArray, null, currentIndex, paragraphMode,\r
+                                               currDir != orgDir ? orgDir: null );\r
+\r
+                                       // If the next block is an <li> with another list tree as the first\r
+                                       // child, we'll need to append a filler (<br>/NBSP) or the list item\r
+                                       // wouldn't be editable. (#6724)\r
+                                       if ( !currentListItem.getChildCount() && CKEDITOR.env.ie && !( doc.$.documentMode > 7 ))\r
+                                               currentListItem.append( doc.createText( '\xa0' ) );\r
                                        currentListItem.append( listData.listNode );\r
                                        currentIndex = listData.nextIndex;\r
                                }\r
                                else if ( item.indent == -1 && !baseIndex && item.grandparent )\r
                                {\r
-                                       currentListItem;\r
                                        if ( listNodeNames[ item.grandparent.getName() ] )\r
                                                currentListItem = item.element.clone( false, true );\r
                                        else\r
                                        {\r
-                                               // Create completely new blocks here, attributes are dropped.\r
-                                               if ( dir || ( paragraphMode != CKEDITOR.ENTER_BR && item.grandparent.getName() != 'td' ) )\r
+                                               // Create completely new blocks here.\r
+                                               if ( dir || item.element.hasAttributes() || paragraphMode != CKEDITOR.ENTER_BR )\r
                                                {\r
                                                        currentListItem = doc.createElement( paragraphName );\r
-                                                       if ( dir )\r
-                                                               currentListItem.setAttribute( 'dir', dir );\r
+                                                       item.element.copyAttributes( currentListItem, { type:1, value:1 } );\r
+\r
+                                                       // There might be a case where there are no attributes in the element after all\r
+                                                       // (i.e. when "type" or "value" are the only attributes set). In this case, if enterMode = BR,\r
+                                                       // the current item should be a fragment.\r
+                                                       if ( !dir && paragraphMode == CKEDITOR.ENTER_BR && !currentListItem.hasAttributes() )\r
+                                                               currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
                                                }\r
                                                else\r
                                                        currentListItem = new CKEDITOR.dom.documentFragment( doc );\r
                                        }\r
 \r
+                                       if ( currentListItem.type == CKEDITOR.NODE_ELEMENT )\r
+                                       {\r
+                                               if ( item.grandparent.getDirection( 1 ) != orgDir )\r
+                                                       currentListItem.setAttribute( 'dir', orgDir );\r
+                                               else\r
+                                                       currentListItem.removeAttribute( 'dir' );\r
+                                       }\r
+\r
                                        for ( i = 0 ; i < item.contents.length ; i++ )\r
-                                               currentListItem.append( item.contents[i].clone( true, true ) );\r
+                                               currentListItem.append( item.contents[i].clone( 1, 1 ) );\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
+                                               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 ( !( last = currentListItem.getLast( nonEmpty )\r
+                                                       && last.type == CKEDITOR.NODE_ELEMENT\r
+                                                       && last.getName() in CKEDITOR.dtd.$block ) )\r
+                                               {\r
+                                                       currentListItem.append( doc.createElement( 'br' ) );\r
+                                               }\r
                                        }\r
 \r
                                        if ( currentListItem.type == CKEDITOR.NODE_ELEMENT &&\r
@@ -176,30 +224,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 \r
-       function setState( editor, state )\r
-       {\r
-               editor.getCommand( this.name ).setState( state );\r
-       }\r
-\r
        function onSelectionChange( evt )\r
        {\r
+               if ( evt.editor.readOnly )\r
+                       return null;\r
+\r
                var path = evt.data.path,\r
                        blockLimit = path.blockLimit,\r
                        elements = path.elements,\r
-                       element;\r
+                       element,\r
+                       i;\r
 \r
                // Grouping should only happen under blockLimit.(#3940).\r
-               for ( var i = 0 ; i < elements.length && ( element = elements[ i ] )\r
+               for ( i = 0 ; i < elements.length && ( element = elements[ i ] )\r
                          && !element.equals( blockLimit ); i++ )\r
                {\r
-                       if ( listNodeNames[ elements[i].getName() ] )\r
-                       {\r
-                               return setState.call( this, evt.editor,\r
-                                               this.type == elements[i].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
-                       }\r
+                       if ( listNodeNames[ elements[ i ].getName() ] )\r
+                               return this.setState( this.type == elements[ i ].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
                }\r
 \r
-               return setState.call( this, evt.editor, CKEDITOR.TRISTATE_OFF );\r
+               return this.setState( CKEDITOR.TRISTATE_OFF );\r
        }\r
 \r
        function changeListType( editor, groupObj, database, listsCreated )\r
@@ -268,6 +312,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( var i = 0 ; i < contents.length ; i++ )\r
                        commonParent = commonParent.getCommonAncestor( contents[i].getParent() );\r
 \r
+               var useComputedState = editor.config.useComputedState,\r
+                       listDir, explicitDirection;\r
+\r
+               useComputedState = useComputedState === undefined || useComputedState;\r
+\r
                // We want to insert things that are in the same tree level only, so calculate the contents again\r
                // by expanding the selected blocks to the same tree level.\r
                for ( i = 0 ; i < contents.length ; i++ )\r
@@ -279,6 +328,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                if ( parentNode.equals( commonParent ) )\r
                                {\r
                                        listContents.push( contentNode );\r
+\r
+                                       // Determine the lists's direction.\r
+                                       if ( !explicitDirection && contentNode.getDirection() )\r
+                                               explicitDirection = 1;\r
+\r
+                                       var itemDir = contentNode.getDirection( useComputedState );\r
+\r
+                                       if ( listDir !== null )\r
+                                       {\r
+                                               // If at least one LI have a different direction than current listDir, we can't have listDir.\r
+                                               if ( listDir && listDir != itemDir )\r
+                                                       listDir = null;\r
+                                               else\r
+                                                       listDir = itemDir;\r
+                                       }\r
+\r
                                        break;\r
                                }\r
                                contentNode = parentNode;\r
@@ -290,39 +355,39 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                // Insert the list to the DOM tree.\r
                var insertAnchor = listContents[ listContents.length - 1 ].getNext(),\r
-                       listNode = doc.createElement( this.type ),\r
-                       dir;\r
+                       listNode = doc.createElement( this.type );\r
 \r
                listsCreated.push( listNode );\r
+\r
+               var contentBlock, listItem;\r
+\r
                while ( listContents.length )\r
                {\r
-                       var contentBlock = listContents.shift(),\r
-                               listItem = doc.createElement( 'li' );\r
+                       contentBlock = listContents.shift();\r
+                       listItem = doc.createElement( 'li' );\r
 \r
-                       // Preserve heading structure when converting to list item. (#5271)\r
-                       if ( headerTagRegex.test( contentBlock.getName() ) )\r
+                       // Preserve preformat block and heading structure when converting to list item. (#5335) (#5271)\r
+                       if ( contentBlock.is( 'pre' ) || headerTagRegex.test( contentBlock.getName() ) )\r
                                contentBlock.appendTo( listItem );\r
                        else\r
                        {\r
-                               if ( contentBlock.hasAttribute( 'dir' ) )\r
+                               contentBlock.copyAttributes( listItem );\r
+                               // Remove direction attribute after it was merged into list root. (#7657)\r
+                               if ( listDir && contentBlock.getDirection() )\r
                                {\r
-                                       dir = dir || contentBlock.getAttribute( 'dir' );\r
-                                       contentBlock.removeAttribute( 'dir' );\r
+                                       listItem.removeStyle( 'direction' );\r
+                                       listItem.removeAttribute( 'dir' );\r
                                }\r
-                               contentBlock.copyAttributes( listItem );\r
                                contentBlock.moveChildren( listItem );\r
                                contentBlock.remove();\r
                        }\r
 \r
                        listItem.appendTo( listNode );\r
-\r
-                       // Append a bogus BR to force the LI to render at full height\r
-                       if ( !CKEDITOR.env.ie )\r
-                               listItem.appendBogus();\r
                }\r
 \r
-               if ( dir )\r
-                       listNode.setAttribute( 'dir', dir );\r
+               // Apply list root dir only if it has been explicitly declared.\r
+               if ( listDir && explicitDirection )\r
+                       listNode.setAttribute( 'dir', listDir );\r
 \r
                if ( insertAnchor )\r
                        listNode.insertBefore( insertAnchor );\r
@@ -402,9 +467,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        listCommand.prototype = {\r
                exec : function( editor )\r
                {\r
-                       editor.focus();\r
-\r
                        var doc = editor.document,\r
+                               config = editor.config,\r
                                selection = editor.getSelection(),\r
                                ranges = selection && selection.getRanges( true );\r
 \r
@@ -418,21 +482,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.range( doc ) ];\r
-                                       // IE exception on inserting anything when anchor inside <br>.\r
-                                       if ( paragraph.is( 'br' ) )\r
-                                       {\r
-                                               ranges[ 0 ].setStartBefore( paragraph );\r
-                                               ranges[ 0 ].setEndAfter( paragraph );\r
-                                       }\r
-                                       else\r
-                                               ranges[ 0 ].selectNodeContents( paragraph );\r
+                                       config.enterMode == CKEDITOR.ENTER_BR ?\r
+                                               body.appendBogus() :\r
+                                               ranges[ 0 ].fixBlock( 1, config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\r
+\r
                                        selection.selectRanges( ranges );\r
                                }\r
                                // Maybe a single range there enclosing the whole list,\r
@@ -443,9 +498,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                enclosedNode = range && range.getEnclosedNode();\r
                                        if ( enclosedNode && enclosedNode.is\r
                                                && this.type == enclosedNode.getName() )\r
-                                       {\r
-                                               setState.call( this, editor, CKEDITOR.TRISTATE_ON );\r
-                                       }\r
+                                                       this.setState( CKEDITOR.TRISTATE_ON );\r
                                }\r
                        }\r
 \r
@@ -487,7 +540,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                pathElements = path.elements,\r
                                                pathElementsCount = pathElements.length,\r
                                                listNode = null,\r
-                                               processedFlag = false,\r
+                                               processedFlag = 0,\r
                                                blockLimit = path.blockLimit,\r
                                                element;\r
 \r
@@ -513,7 +566,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                listGroups.push( groupObj );\r
                                                                CKEDITOR.dom.element.setMarker( database, element, 'list_group_object', groupObj );\r
                                                        }\r
-                                                       processedFlag = true;\r
+                                                       processedFlag = 1;\r
                                                        break;\r
                                                }\r
                                        }\r
@@ -566,10 +619,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                sibling.remove();\r
                                                // Move children order by merge direction.(#3820)\r
-                                               sibling.moveChildren( listNode, rtl ? true : false );\r
+                                               sibling.moveChildren( listNode, rtl );\r
                                        }\r
                                } )();\r
-                               mergeSibling( true );\r
+                               mergeSibling( 1 );\r
                        }\r
 \r
                        // Clean up, restore selection and update toolbar button states.\r
@@ -646,10 +699,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                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