JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / list / plugin.js
index 856a430..bc28e51 100644 (file)
@@ -38,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
@@ -83,10 +87,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
+                               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
@@ -96,13 +104,22 @@ 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
+                                       else\r
+                                               currentListItem.removeAttribute( 'dir' );\r
+\r
                                        for ( var 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
@@ -114,7 +131,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\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
@@ -124,9 +140,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
-                                                       var 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
@@ -138,6 +151,14 @@ 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
+                                               else\r
+                                                       currentListItem.removeAttribute( 'dir' );\r
+                                       }\r
+\r
                                        for ( i = 0 ; i < item.contents.length ; i++ )\r
                                                currentListItem.append( item.contents[i].clone( 1, 1 ) );\r
 \r
@@ -205,6 +226,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
@@ -215,8 +239,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
@@ -347,14 +371,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
@@ -444,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
@@ -460,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.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