2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 },
\r
9 directSelectionGuardElements = {},
\r
10 // All guard elements which can have a direction applied on them.
\r
11 allGuardElements = {};
\r
12 CKEDITOR.tools.extend( directSelectionGuardElements, guardElements, { tr:1, p:1, div:1, li:1 } );
\r
13 CKEDITOR.tools.extend( allGuardElements, directSelectionGuardElements, { td:1 } );
\r
15 function onSelectionChange( e )
\r
17 setToolbarStates( e );
\r
18 handleMixedDirContent( e );
\r
21 function setToolbarStates( evt )
\r
23 var editor = evt.editor,
\r
24 path = evt.data.path;
\r
25 var useComputedState = editor.config.useComputedState,
\r
28 useComputedState = useComputedState === undefined || useComputedState;
\r
30 // We can use computedState provided by the browser or traverse parents manually.
\r
31 if ( !useComputedState )
\r
32 selectedElement = getElementForDirection( path.lastElement );
\r
34 selectedElement = selectedElement || path.block || path.blockLimit;
\r
36 // If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.
\r
37 selectedElement.is( 'body' ) &&
\r
38 ( selectedElement = editor.getSelection().getRanges()[ 0 ].getEnclosedNode() );
\r
40 if ( !selectedElement )
\r
43 var selectionDir = useComputedState ?
\r
44 selectedElement.getComputedStyle( 'direction' ) :
\r
45 selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );
\r
47 editor.getCommand( 'bidirtl' ).setState( selectionDir == 'rtl' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
48 editor.getCommand( 'bidiltr' ).setState( selectionDir == 'ltr' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
51 function handleMixedDirContent( evt )
\r
53 var editor = evt.editor,
\r
54 directionNode = evt.data.path.block || evt.data.path.blockLimit;
\r
56 editor.fire( 'contentDirChanged', directionNode ? directionNode.getComputedStyle( 'direction' ) : editor.lang.dir );
\r
60 * Returns element with possibility of applying the direction.
\r
63 function getElementForDirection( node )
\r
65 while ( node && !( node.getName() in allGuardElements || node.is( 'body' ) ) )
\r
67 var parent = node.getParent();
\r
77 function switchDir( element, dir, editor, database )
\r
79 if ( element.isReadOnly() )
\r
82 // Mark this element as processed by switchDir.
\r
83 CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );
\r
85 // Check whether one of the ancestors has already been styled.
\r
86 var parent = element;
\r
87 while ( ( parent = parent.getParent() ) && !parent.is( 'body' ) )
\r
89 if ( parent.getCustomData( 'bidi_processed' ) )
\r
91 // Ancestor style must dominate.
\r
92 element.removeStyle( 'direction' );
\r
93 element.removeAttribute( 'dir' );
\r
98 var useComputedState = ( 'useComputedState' in editor.config ) ? editor.config.useComputedState : 1;
\r
100 var elementDir = useComputedState ? element.getComputedStyle( 'direction' )
\r
101 : element.getStyle( 'direction' ) || element.hasAttribute( 'dir' );
\r
103 // Stop if direction is same as present.
\r
104 if ( elementDir == dir )
\r
107 // Clear direction on this element.
\r
108 element.removeStyle( 'direction' );
\r
110 // Do the second check when computed state is ON, to check
\r
111 // if we need to apply explicit direction on this element.
\r
112 if ( useComputedState )
\r
114 element.removeAttribute( 'dir' );
\r
115 if ( dir != element.getComputedStyle( 'direction' ) )
\r
116 element.setAttribute( 'dir', dir );
\r
119 // Set new direction for this element.
\r
120 element.setAttribute( 'dir', dir );
\r
122 editor.forceNextSelectionCheck();
\r
127 function getFullySelected( range, elements, enterMode )
\r
129 var ancestor = range.getCommonAncestor( false, true );
\r
131 range = range.clone();
\r
132 range.enlarge( enterMode == CKEDITOR.ENTER_BR ?
\r
133 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS
\r
134 : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
\r
136 if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )
\r
137 && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )
\r
140 while ( ancestor && ancestor.type == CKEDITOR.NODE_ELEMENT
\r
141 && ( parent = ancestor.getParent() )
\r
142 && parent.getChildCount() == 1
\r
143 && !( ancestor.getName() in elements ) )
\r
146 return ancestor.type == CKEDITOR.NODE_ELEMENT
\r
147 && ( ancestor.getName() in elements )
\r
152 function bidiCommand( dir )
\r
154 return function( editor )
\r
156 var selection = editor.getSelection(),
\r
157 enterMode = editor.config.enterMode,
\r
158 ranges = selection.getRanges();
\r
160 if ( ranges && ranges.length )
\r
164 // Creates bookmarks for selection, as we may split some blocks.
\r
165 var bookmarks = selection.createBookmarks();
\r
167 var rangeIterator = ranges.createIterator(),
\r
171 while ( ( range = rangeIterator.getNextRange( 1 ) ) )
\r
173 // Apply do directly selected elements from guardElements.
\r
174 var selectedElement = range.getEnclosedNode();
\r
176 // If this is not our element of interest, apply to fully selected elements from guardElements.
\r
177 if ( !selectedElement || selectedElement
\r
178 && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )
\r
180 selectedElement = getFullySelected( range, guardElements, enterMode );
\r
182 selectedElement && switchDir( selectedElement, dir, editor, database );
\r
187 // Walker searching for guardElements.
\r
188 var walker = new CKEDITOR.dom.walker( range );
\r
190 var start = bookmarks[ i ].startNode,
\r
191 end = bookmarks[ i++ ].endNode;
\r
193 walker.evaluator = function( node )
\r
195 return !! ( node.type == CKEDITOR.NODE_ELEMENT
\r
196 && node.getName() in guardElements
\r
197 && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )
\r
198 && node.getParent().type == CKEDITOR.NODE_ELEMENT
\r
199 && node.getParent().getName() == 'blockquote' )
\r
200 // Element must be fully included in the range as well. (#6485).
\r
201 && node.getPosition( start ) & CKEDITOR.POSITION_FOLLOWING
\r
202 && ( ( node.getPosition( end ) & CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_CONTAINS ) == CKEDITOR.POSITION_PRECEDING ) );
\r
205 while ( ( block = walker.next() ) )
\r
206 switchDir( block, dir, editor, database );
\r
208 iterator = range.createIterator();
\r
209 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
211 while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
\r
212 switchDir( block, dir, editor, database );
\r
215 CKEDITOR.dom.element.clearAllMarkers( database );
\r
217 editor.forceNextSelectionCheck();
\r
218 // Restore selection position.
\r
219 selection.selectBookmarks( bookmarks );
\r
226 CKEDITOR.plugins.add( 'bidi',
\r
228 requires : [ 'styles', 'button' ],
\r
230 init : function( editor )
\r
232 // All buttons use the same code to register. So, to avoid
\r
233 // duplications, let's use this tool function.
\r
234 var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec )
\r
236 editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );
\r
238 editor.ui.addButton( buttonName,
\r
240 label : buttonLabel,
\r
241 command : commandName
\r
245 var lang = editor.lang.bidi;
\r
247 addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );
\r
248 addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );
\r
250 editor.on( 'selectionChange', onSelectionChange );
\r
251 editor.on( 'contentDom', function()
\r
253 editor.document.on( 'dirChanged', function( evt )
\r
255 editor.fire( 'dirChanged',
\r
258 dir : evt.data.getDirection( 1 )
\r
265 // If the element direction changed, we need to switch the margins of
\r
266 // the element and all its children, so it will get really reflected
\r
267 // like a mirror. (#5910)
\r
268 function isOffline( el )
\r
270 var html = el.getDocument().getBody().getParent();
\r
273 if ( el.equals( html ) )
\r
275 el = el.getParent();
\r
279 function dirChangeNotifier( org )
\r
281 var isAttribute = org == elementProto.setAttribute,
\r
282 isRemoveAttribute = org == elementProto.removeAttribute,
\r
283 dirStyleRegexp = /\bdirection\s*:\s*(.*?)\s*(:?$|;)/;
\r
285 return function( name, val )
\r
287 if ( !this.getDocument().equals( CKEDITOR.document ) )
\r
290 if ( ( name == ( isAttribute || isRemoveAttribute ? 'dir' : 'direction' ) ||
\r
291 name == 'style' && ( isRemoveAttribute || dirStyleRegexp.test( val ) ) ) && !isOffline( this ) )
\r
293 orgDir = this.getDirection( 1 );
\r
294 var retval = org.apply( this, arguments );
\r
295 if ( orgDir != this.getDirection( 1 ) )
\r
297 this.getDocument().fire( 'dirChanged', this );
\r
303 return org.apply( this, arguments );
\r
307 var elementProto = CKEDITOR.dom.element.prototype,
\r
308 methods = [ 'setStyle', 'removeStyle', 'setAttribute', 'removeAttribute' ];
\r
309 for ( var i = 0; i < methods.length; i++ )
\r
310 elementProto[ methods[ i ] ] = CKEDITOR.tools.override( elementProto[ methods [ i ] ], dirChangeNotifier );
\r
314 * Fired when the language direction of an element is changed
\r
315 * @name CKEDITOR.editor#dirChanged
\r
317 * @param {CKEDITOR.editor} editor This editor instance.
\r
318 * @param {Object} eventData.node The element that is being changed.
\r
319 * @param {String} eventData.dir The new direction.
\r
323 * Fired when the language direction in the specific cursor position is changed
\r
324 * @name CKEDITOR.editor#contentDirChanged
\r
326 * @param {String} eventData The direction in the current position.
\r