/*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
For licensing, see LICENSE.html or http://ckeditor.com/license\r
*/\r
\r
{\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
selectedElement = selectedElement || path.block || path.blockLimit;\r
\r
// If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.\r
- selectedElement.is( 'body' ) &&\r
- ( selectedElement = editor.getSelection().getRanges()[ 0 ].getEnclosedNode() );\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
+ if ( !selectedElement )\r
return;\r
\r
var selectionDir = useComputedState ?\r
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
\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
// Ancestor style must dominate.\r
element.removeStyle( 'direction' );\r
element.removeAttribute( 'dir' );\r
- return null;\r
+ return;\r
}\r
}\r
\r
\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
// 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
)\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
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
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
* @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