JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / bidi / plugin.js
index 761e16b..2233ee1 100644 (file)
@@ -1,11 +1,11 @@
 /*\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
 (function()\r
 {\r
-       var guardElements = { table:1, tbody: 1, ul:1, ol:1, blockquote:1, div:1, tr: 1 },\r
+       var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 },\r
                directSelectionGuardElements = {},\r
                // All guard elements which can have a direction applied on them.\r
                allGuardElements = {};\r
@@ -22,6 +22,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\r
                var editor = evt.editor,\r
                        path = evt.data.path;\r
+\r
+               if ( editor.readOnly )\r
+                       return;\r
+\r
                var useComputedState = editor.config.useComputedState,\r
                        selectedElement;\r
 \r
@@ -33,7 +37,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                selectedElement = selectedElement || path.block || path.blockLimit;\r
 \r
-               if ( !selectedElement || selectedElement.getName() == 'body' )\r
+               // If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.\r
+               if ( selectedElement.is( 'body' ) )\r
+               {\r
+                       var enclosedNode = editor.getSelection().getRanges()[ 0 ].getEnclosedNode();\r
+                       enclosedNode && enclosedNode.type == CKEDITOR.NODE_ELEMENT && ( selectedElement = enclosedNode );\r
+               }\r
+\r
+               if ( !selectedElement  )\r
                        return;\r
 \r
                var selectionDir = useComputedState ?\r
@@ -47,13 +58,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function handleMixedDirContent( evt )\r
        {\r
                var editor = evt.editor,\r
-                       chromeRoot = editor.container.getChild( 1 ),\r
-                       directionNode = getElementForDirection( evt.data.path.lastElement );\r
+                       directionNode = evt.data.path.block || evt.data.path.blockLimit;\r
 \r
-               if ( directionNode && editor.lang.dir != directionNode.getComputedStyle( 'direction' ) )\r
-                       chromeRoot.addClass( 'cke_mixed_dir_content' );\r
-               else\r
-                       chromeRoot.removeClass( 'cke_mixed_dir_content' );\r
+               editor.fire( 'contentDirChanged', directionNode ? directionNode.getComputedStyle( 'direction' ) : editor.lang.dir );\r
        }\r
 \r
        /**\r
@@ -76,6 +83,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        function switchDir( element, dir, editor, database )\r
        {\r
+               if ( element.isReadOnly() )\r
+                       return;\r
+\r
                // Mark this element as processed by switchDir.\r
                CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );\r
 \r
@@ -88,7 +98,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // Ancestor style must dominate.\r
                                element.removeStyle( 'direction' );\r
                                element.removeAttribute( 'dir' );\r
-                               return null;\r
+                               return;\r
                        }\r
                }\r
 \r
@@ -99,10 +109,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                // Stop if direction is same as present.\r
                if ( elementDir == dir )\r
-                       return null;\r
-\r
-               // Reuse computedState if we already have it.\r
-               var dirBefore = useComputedState ? elementDir : element.getComputedStyle( 'direction' );\r
+                       return;\r
 \r
                // Clear direction on this element.\r
                element.removeStyle( 'direction' );\r
@@ -119,22 +126,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // Set new direction for this element.\r
                        element.setAttribute( 'dir', dir );\r
 \r
-               // If the element direction changed, we need to switch the margins of\r
-               // the element and all its children, so it will get really reflected\r
-               // like a mirror. (#5910)\r
-               if ( dir != dirBefore )\r
-                       editor.fire( 'dirChanged', element );\r
-\r
                editor.forceNextSelectionCheck();\r
 \r
-               return null;\r
+               return;\r
        }\r
 \r
-       function getFullySelected( range, elements )\r
+       function getFullySelected( range, elements, enterMode )\r
        {\r
                var ancestor = range.getCommonAncestor( false, true );\r
 \r
-               range.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
+               range = range.clone();\r
+               range.enlarge( enterMode == CKEDITOR.ENTER_BR ?\r
+                               CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS\r
+                               : CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
 \r
                if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )\r
                                && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )\r
@@ -143,8 +147,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        while ( ancestor && ancestor.type == CKEDITOR.NODE_ELEMENT\r
                                        && ( parent = ancestor.getParent() )\r
                                        && parent.getChildCount() == 1\r
-                                       && ( !( ancestor.getName() in elements ) || ( parent.getName() in elements ) )\r
-                                       )\r
+                                       && !( ancestor.getName() in elements ) )\r
                                ancestor = parent;\r
 \r
                        return ancestor.type == CKEDITOR.NODE_ELEMENT\r
@@ -181,10 +184,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        if ( !selectedElement || selectedElement\r
                                                        && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )\r
                                                )\r
-                                               selectedElement = getFullySelected( range, guardElements );\r
+                                               selectedElement = getFullySelected( range, guardElements, enterMode );\r
 \r
-                                       if ( selectedElement && !selectedElement.isReadOnly() )\r
-                                               switchDir( selectedElement, dir, editor, database );\r
+                                       selectedElement && switchDir( selectedElement, dir, editor, database );\r
 \r
                                        var iterator,\r
                                                block;\r
@@ -199,7 +201,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                return !! ( node.type == CKEDITOR.NODE_ELEMENT\r
                                                                && node.getName() in guardElements\r
-                                                               && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ) ? 'p' : 'div'\r
+                                                               && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )\r
                                                                        && node.getParent().type == CKEDITOR.NODE_ELEMENT\r
                                                                        && node.getParent().getName() == 'blockquote' )\r
                                                                // Element must be fully included in the range as well. (#6485).\r
@@ -214,8 +216,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
 \r
                                        while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )\r
-                                               !block.isReadOnly() && switchDir( block, dir, editor, database );\r
-                               }\r
+                                               switchDir( block, dir, editor, database );\r
+                                       }\r
 \r
                                CKEDITOR.dom.element.clearAllMarkers( database );\r
 \r
@@ -253,7 +255,80 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );\r
 \r
                        editor.on( 'selectionChange', onSelectionChange );\r
+                       editor.on( 'contentDom', function()\r
+                       {\r
+                               editor.document.on( 'dirChanged', function( evt )\r
+                               {\r
+                                       editor.fire( 'dirChanged',\r
+                                               {\r
+                                                       node : evt.data,\r
+                                                       dir : evt.data.getDirection( 1 )\r
+                                               } );\r
+                               });\r
+                       });\r
                }\r
        });\r
 \r
+       // If the element direction changed, we need to switch the margins of\r
+       // the element and all its children, so it will get really reflected\r
+       // like a mirror. (#5910)\r
+       function isOffline( el )\r
+       {\r
+               var html = el.getDocument().getBody().getParent();\r
+               while ( el )\r
+               {\r
+                       if ( el.equals( html ) )\r
+                               return false;\r
+                       el = el.getParent();\r
+               }\r
+               return true;\r
+       }\r
+       function dirChangeNotifier( org )\r
+       {\r
+               var isAttribute = org == elementProto.setAttribute,\r
+                       isRemoveAttribute = org == elementProto.removeAttribute,\r
+                       dirStyleRegexp = /\bdirection\s*:\s*(.*?)\s*(:?$|;)/;\r
+\r
+               return function( name, val )\r
+               {\r
+                       if ( !this.getDocument().equals( CKEDITOR.document ) )\r
+                       {\r
+                               var orgDir;\r
+                               if ( ( name == ( isAttribute || isRemoveAttribute ? 'dir' : 'direction' ) ||\r
+                                        name == 'style' && ( isRemoveAttribute || dirStyleRegexp.test( val ) ) ) && !isOffline( this ) )\r
+                               {\r
+                                       orgDir = this.getDirection( 1 );\r
+                                       var retval = org.apply( this, arguments );\r
+                                       if ( orgDir != this.getDirection( 1 ) )\r
+                                       {\r
+                                               this.getDocument().fire( 'dirChanged', this );\r
+                                               return retval;\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       return org.apply( this, arguments );\r
+               };\r
+       }\r
+\r
+       var elementProto = CKEDITOR.dom.element.prototype,\r
+               methods = [ 'setStyle', 'removeStyle', 'setAttribute', 'removeAttribute' ];\r
+       for ( var i = 0; i < methods.length; i++ )\r
+               elementProto[ methods[ i ] ] = CKEDITOR.tools.override( elementProto[ methods [ i ] ], dirChangeNotifier );\r
 })();\r
+\r
+/**\r
+ * Fired when the language direction of an element is changed\r
+ * @name CKEDITOR.editor#dirChanged\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @param {Object} eventData.node The element that is being changed.\r
+ * @param {String} eventData.dir The new direction.\r
+ */\r
+\r
+/**\r
+ * Fired when the language direction in the specific cursor position is changed\r
+ * @name CKEDITOR.editor#contentDirChanged\r
+ * @event\r
+ * @param {String} eventData The direction in the current position.\r
+ */\r