JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / bidi / plugin.js
index 830e2d4..a1ae6c8 100644 (file)
@@ -51,13 +51,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 = 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
@@ -80,6 +76,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
@@ -92,7 +91,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
@@ -103,10 +102,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
@@ -123,21 +119,9 @@ 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
-               {\r
-                       editor.fire( 'dirChanged',\r
-                               {\r
-                                       node : element,\r
-                                       dir : dir\r
-                               } );\r
-               }\r
-\r
                editor.forceNextSelectionCheck();\r
 \r
-               return null;\r
+               return;\r
        }\r
 \r
        function getFullySelected( range, elements, enterMode )\r
@@ -195,8 +179,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                )\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
@@ -226,7 +209,7 @@ 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
+                                               switchDir( block, dir, editor, database );\r
                                        }\r
 \r
                                CKEDITOR.dom.element.clearAllMarkers( database );\r
@@ -265,9 +248,66 @@ 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
@@ -278,3 +318,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
  * @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