2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
8 var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 },
9 directSelectionGuardElements = {},
10 // All guard elements which can have a direction applied on them.
11 allGuardElements = {};
12 CKEDITOR.tools.extend( directSelectionGuardElements, guardElements, { tr:1, p:1, div:1, li:1 } );
13 CKEDITOR.tools.extend( allGuardElements, directSelectionGuardElements, { td:1 } );
15 function onSelectionChange( e )
17 setToolbarStates( e );
18 handleMixedDirContent( e );
21 function setToolbarStates( evt )
23 var editor = evt.editor,
25 var useComputedState = editor.config.useComputedState,
28 useComputedState = useComputedState === undefined || useComputedState;
30 // We can use computedState provided by the browser or traverse parents manually.
31 if ( !useComputedState )
32 selectedElement = getElementForDirection( path.lastElement );
34 selectedElement = selectedElement || path.block || path.blockLimit;
36 // If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.
37 selectedElement.is( 'body' ) &&
38 ( selectedElement = editor.getSelection().getRanges()[ 0 ].getEnclosedNode() );
40 if ( !selectedElement )
43 var selectionDir = useComputedState ?
44 selectedElement.getComputedStyle( 'direction' ) :
45 selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );
47 editor.getCommand( 'bidirtl' ).setState( selectionDir == 'rtl' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
48 editor.getCommand( 'bidiltr' ).setState( selectionDir == 'ltr' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
51 function handleMixedDirContent( evt )
53 var editor = evt.editor,
54 chromeRoot = editor.container.getChild( 1 ),
55 directionNode = evt.data.path.block || evt.data.path.blockLimit;
57 if ( directionNode && editor.lang.dir != directionNode.getComputedStyle( 'direction' ) )
58 chromeRoot.addClass( 'cke_mixed_dir_content' );
60 chromeRoot.removeClass( 'cke_mixed_dir_content' );
64 * Returns element with possibility of applying the direction.
67 function getElementForDirection( node )
69 while ( node && !( node.getName() in allGuardElements || node.is( 'body' ) ) )
71 var parent = node.getParent();
81 function switchDir( element, dir, editor, database )
83 // Mark this element as processed by switchDir.
84 CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );
86 // Check whether one of the ancestors has already been styled.
88 while ( ( parent = parent.getParent() ) && !parent.is( 'body' ) )
90 if ( parent.getCustomData( 'bidi_processed' ) )
92 // Ancestor style must dominate.
93 element.removeStyle( 'direction' );
94 element.removeAttribute( 'dir' );
99 var useComputedState = ( 'useComputedState' in editor.config ) ? editor.config.useComputedState : 1;
101 var elementDir = useComputedState ? element.getComputedStyle( 'direction' )
102 : element.getStyle( 'direction' ) || element.hasAttribute( 'dir' );
104 // Stop if direction is same as present.
105 if ( elementDir == dir )
108 // Reuse computedState if we already have it.
109 var dirBefore = useComputedState ? elementDir : element.getComputedStyle( 'direction' );
111 // Clear direction on this element.
112 element.removeStyle( 'direction' );
114 // Do the second check when computed state is ON, to check
115 // if we need to apply explicit direction on this element.
116 if ( useComputedState )
118 element.removeAttribute( 'dir' );
119 if ( dir != element.getComputedStyle( 'direction' ) )
120 element.setAttribute( 'dir', dir );
123 // Set new direction for this element.
124 element.setAttribute( 'dir', dir );
126 // If the element direction changed, we need to switch the margins of
127 // the element and all its children, so it will get really reflected
128 // like a mirror. (#5910)
129 if ( dir != dirBefore )
131 editor.fire( 'dirChanged',
138 editor.forceNextSelectionCheck();
143 function getFullySelected( range, elements, enterMode )
145 var ancestor = range.getCommonAncestor( false, true );
147 range = range.clone();
148 range.enlarge( enterMode == CKEDITOR.ENTER_BR ?
149 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS
150 : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
152 if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )
153 && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )
156 while ( ancestor && ancestor.type == CKEDITOR.NODE_ELEMENT
157 && ( parent = ancestor.getParent() )
158 && parent.getChildCount() == 1
159 && !( ancestor.getName() in elements ) )
162 return ancestor.type == CKEDITOR.NODE_ELEMENT
163 && ( ancestor.getName() in elements )
168 function bidiCommand( dir )
170 return function( editor )
172 var selection = editor.getSelection(),
173 enterMode = editor.config.enterMode,
174 ranges = selection.getRanges();
176 if ( ranges && ranges.length )
180 // Creates bookmarks for selection, as we may split some blocks.
181 var bookmarks = selection.createBookmarks();
183 var rangeIterator = ranges.createIterator(),
187 while ( ( range = rangeIterator.getNextRange( 1 ) ) )
189 // Apply do directly selected elements from guardElements.
190 var selectedElement = range.getEnclosedNode();
192 // If this is not our element of interest, apply to fully selected elements from guardElements.
193 if ( !selectedElement || selectedElement
194 && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )
196 selectedElement = getFullySelected( range, guardElements, enterMode );
198 if ( selectedElement && !selectedElement.isReadOnly() )
199 switchDir( selectedElement, dir, editor, database );
204 // Walker searching for guardElements.
205 var walker = new CKEDITOR.dom.walker( range );
207 var start = bookmarks[ i ].startNode,
208 end = bookmarks[ i++ ].endNode;
210 walker.evaluator = function( node )
212 return !! ( node.type == CKEDITOR.NODE_ELEMENT
213 && node.getName() in guardElements
214 && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )
215 && node.getParent().type == CKEDITOR.NODE_ELEMENT
216 && node.getParent().getName() == 'blockquote' )
217 // Element must be fully included in the range as well. (#6485).
218 && node.getPosition( start ) & CKEDITOR.POSITION_FOLLOWING
219 && ( ( node.getPosition( end ) & CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_CONTAINS ) == CKEDITOR.POSITION_PRECEDING ) );
222 while ( ( block = walker.next() ) )
223 switchDir( block, dir, editor, database );
225 iterator = range.createIterator();
226 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
228 while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
229 !block.isReadOnly() && switchDir( block, dir, editor, database );
232 CKEDITOR.dom.element.clearAllMarkers( database );
234 editor.forceNextSelectionCheck();
235 // Restore selection position.
236 selection.selectBookmarks( bookmarks );
243 CKEDITOR.plugins.add( 'bidi',
245 requires : [ 'styles', 'button' ],
247 init : function( editor )
249 // All buttons use the same code to register. So, to avoid
250 // duplications, let's use this tool function.
251 var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec )
253 editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );
255 editor.ui.addButton( buttonName,
258 command : commandName
262 var lang = editor.lang.bidi;
264 addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );
265 addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );
267 editor.on( 'selectionChange', onSelectionChange );
274 * Fired when the language direction of an element is changed
275 * @name CKEDITOR.editor#dirChanged
277 * @param {CKEDITOR.editor} editor This editor instance.
278 * @param {Object} eventData.node The element that is being changed.
279 * @param {String} eventData.dir The new direction.