JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.2
[ckeditor.git] / _source / plugins / list / plugin.js
index b0676d0..ad79153 100644 (file)
@@ -16,6 +16,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                bookmarks = CKEDITOR.dom.walker.bookmark(),\r
                nonEmpty = function( node ){ return !( whitespaces( node ) || bookmarks( node ) ); };\r
 \r
+       function cleanUpDirection( element )\r
+       {\r
+               var dir, parent, parentDir;\r
+               if ( ( dir = element.getDirection() ) )\r
+               {\r
+                       parent = element.getParent();\r
+                       while ( parent && !( parentDir = parent.getDirection() ) )\r
+                               parent = parent.getParent();\r
+\r
+                       if ( dir == parentDir )\r
+                               element.removeAttribute( 'dir' );\r
+               }\r
+       }\r
+\r
        CKEDITOR.plugins.list = {\r
                /*\r
                 * Convert a DOM list tree into a data structure that is easier to\r
@@ -87,11 +101,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                currentIndex = baseIndex,\r
                                indentLevel = Math.max( listArray[ baseIndex ].indent, 0 ),\r
                                currentListItem = null,\r
-                               itemDir,\r
+                               orgDir,\r
                                paragraphName = ( paragraphMode == CKEDITOR.ENTER_P ? 'p' : 'div' );\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
@@ -101,6 +118,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                retval.append( rootNode );\r
                                        }\r
                                        currentListItem = rootNode.append( item.element.clone( 0, 1 ) );\r
+\r
+                                       if ( orgDir != rootNode.getDirection( 1 ) )\r
+                                               currentListItem.setAttribute( 'dir', orgDir );\r
+\r
                                        for ( var i = 0 ; i < item.contents.length ; i++ )\r
                                                currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
                                        currentIndex++;\r
@@ -108,8 +129,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                else if ( item.indent == Math.max( indentLevel, 0 ) + 1 )\r
                                {\r
                                        // Maintain original direction (#6861).\r
-                                       var orgDir = item.element.getDirection( 1 ),\r
-                                               currDir = listArray[ currentIndex - 1 ].element.getDirection( 1 ),\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
@@ -124,12 +144,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                else if ( item.indent == -1 && !baseIndex && item.grandparent )\r
                                {\r
                                        if ( listNodeNames[ item.grandparent.getName() ] )\r
-                                       {\r
                                                currentListItem = item.element.clone( false, true );\r
-                                               itemDir = item.element.getDirection( 1 );\r
-                                               item.grandparent.getDirection( 1 ) != itemDir &&\r
-                                                       currentListItem.setAttribute( 'dir', itemDir );\r
-                                       }\r
                                        else\r
                                        {\r
                                                // Create completely new blocks here.\r
@@ -137,9 +152,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                {\r
                                                        currentListItem = doc.createElement( paragraphName );\r
                                                        item.element.copyAttributes( currentListItem, { type:1, value:1 } );\r
-                                                       itemDir = item.element.getDirection() || dir;\r
-                                                       itemDir &&\r
-                                                               currentListItem.setAttribute( 'dir', itemDir );\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
@@ -151,6 +163,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        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
+                                       }\r
+\r
                                        for ( i = 0 ; i < item.contents.length ; i++ )\r
                                                currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
 \r
@@ -200,14 +218,23 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        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
@@ -218,6 +245,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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
@@ -228,8 +258,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( i = 0 ; i < elements.length && ( element = elements[ i ] )\r
                          && !element.equals( blockLimit ); i++ )\r
                {\r
-                       if ( listNodeNames[ elements[i].getName() ] )\r
-                               return this.setState( this.type == elements[i].getName() ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\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 this.setState( CKEDITOR.TRISTATE_OFF );\r
@@ -360,14 +390,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
@@ -454,6 +483,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                this.type = type;\r
        }\r
 \r
+       // Move direction attribute from root to list items.\r
+       function dirToListItems( list )\r
+       {\r
+               var dir = list.getDirection();\r
+               if ( dir )\r
+               {\r
+                       for ( var i = 0, children = list.getChildren(), child; child = children.getItem( i ), i < children.count(); i++ )\r
+                       {\r
+                               if ( child.type == CKEDITOR.NODE_ELEMENT && child.is( 'li' ) && !child.getDirection() )\r
+                                       child.setAttribute( 'dir', dir );\r
+                       }\r
+\r
+                       list.removeAttribute( 'dir' );\r
+               }\r
+       }\r
+\r
        listCommand.prototype = {\r
                exec : function( editor )\r
                {\r
@@ -607,6 +652,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        if ( sibling && sibling.getName &&\r
                                                 sibling.getName() == listCommand.type )\r
                                        {\r
+\r
+                                               // In case to be merged lists have difference directions. (#7448)\r
+                                               if ( sibling.getDirection( 1 ) != listNode.getDirection( 1 ) )\r
+                                                       dirToListItems( listNode.getDirection() ? listNode : sibling );\r
+\r
                                                sibling.remove();\r
                                                // Move children order by merge direction.(#3820)\r
                                                sibling.moveChildren( listNode, rtl );\r