JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / bidi / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\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
14 \r
15         function onSelectionChange( e )\r
16         {\r
17                 setToolbarStates( e );\r
18                 handleMixedDirContent( e );\r
19         }\r
20 \r
21         function setToolbarStates( evt )\r
22         {\r
23                 var editor = evt.editor,\r
24                         path = evt.data.path;\r
25                 var useComputedState = editor.config.useComputedState,\r
26                         selectedElement;\r
27 \r
28                 useComputedState = useComputedState === undefined || useComputedState;\r
29 \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
33 \r
34                 selectedElement = selectedElement || path.block || path.blockLimit;\r
35 \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
39 \r
40                 if ( !selectedElement )\r
41                         return;\r
42 \r
43                 var selectionDir = useComputedState ?\r
44                         selectedElement.getComputedStyle( 'direction' ) :\r
45                         selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );\r
46 \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
49         }\r
50 \r
51         function handleMixedDirContent( evt )\r
52         {\r
53                 var editor = evt.editor,\r
54                         directionNode = evt.data.path.block || evt.data.path.blockLimit;\r
55 \r
56                 editor.fire( 'contentDirChanged', directionNode ? directionNode.getComputedStyle( 'direction' ) : editor.lang.dir );\r
57         }\r
58 \r
59         /**\r
60          * Returns element with possibility of applying the direction.\r
61          * @param node\r
62          */\r
63         function getElementForDirection( node )\r
64         {\r
65                 while ( node && !( node.getName() in allGuardElements || node.is( 'body' ) ) )\r
66                 {\r
67                         var parent = node.getParent();\r
68                         if ( !parent )\r
69                                 break;\r
70 \r
71                         node = parent;\r
72                 }\r
73 \r
74                 return node;\r
75         }\r
76 \r
77         function switchDir( element, dir, editor, database )\r
78         {\r
79                 if ( element.isReadOnly() )\r
80                         return;\r
81 \r
82                 // Mark this element as processed by switchDir.\r
83                 CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );\r
84 \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
88                 {\r
89                         if ( parent.getCustomData( 'bidi_processed' ) )\r
90                         {\r
91                                 // Ancestor style must dominate.\r
92                                 element.removeStyle( 'direction' );\r
93                                 element.removeAttribute( 'dir' );\r
94                                 return;\r
95                         }\r
96                 }\r
97 \r
98                 var useComputedState = ( 'useComputedState' in editor.config ) ? editor.config.useComputedState : 1;\r
99 \r
100                 var elementDir = useComputedState ? element.getComputedStyle( 'direction' )\r
101                         : element.getStyle( 'direction' ) || element.hasAttribute( 'dir' );\r
102 \r
103                 // Stop if direction is same as present.\r
104                 if ( elementDir == dir )\r
105                         return;\r
106 \r
107                 // Clear direction on this element.\r
108                 element.removeStyle( 'direction' );\r
109 \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
113                 {\r
114                         element.removeAttribute( 'dir' );\r
115                         if ( dir != element.getComputedStyle( 'direction' ) )\r
116                                 element.setAttribute( 'dir', dir );\r
117                 }\r
118                 else\r
119                         // Set new direction for this element.\r
120                         element.setAttribute( 'dir', dir );\r
121 \r
122                 editor.forceNextSelectionCheck();\r
123 \r
124                 return;\r
125         }\r
126 \r
127         function getFullySelected( range, elements, enterMode )\r
128         {\r
129                 var ancestor = range.getCommonAncestor( false, true );\r
130 \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
135 \r
136                 if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )\r
137                                 && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )\r
138                 {\r
139                         var parent;\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
144                                 ancestor = parent;\r
145 \r
146                         return ancestor.type == CKEDITOR.NODE_ELEMENT\r
147                                         && ( ancestor.getName() in elements )\r
148                                         && ancestor;\r
149                 }\r
150         }\r
151 \r
152         function bidiCommand( dir )\r
153         {\r
154                 return function( editor )\r
155                 {\r
156                         var selection = editor.getSelection(),\r
157                                 enterMode = editor.config.enterMode,\r
158                                 ranges = selection.getRanges();\r
159 \r
160                         if ( ranges && ranges.length )\r
161                         {\r
162                                 var database = {};\r
163 \r
164                                 // Creates bookmarks for selection, as we may split some blocks.\r
165                                 var bookmarks = selection.createBookmarks();\r
166 \r
167                                 var rangeIterator = ranges.createIterator(),\r
168                                         range,\r
169                                         i = 0;\r
170 \r
171                                 while ( ( range = rangeIterator.getNextRange( 1 ) ) )\r
172                                 {\r
173                                         // Apply do directly selected elements from guardElements.\r
174                                         var selectedElement = range.getEnclosedNode();\r
175 \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
179                                                 )\r
180                                                 selectedElement = getFullySelected( range, guardElements, enterMode );\r
181 \r
182                                         selectedElement && switchDir( selectedElement, dir, editor, database );\r
183 \r
184                                         var iterator,\r
185                                                 block;\r
186 \r
187                                         // Walker searching for guardElements.\r
188                                         var walker = new CKEDITOR.dom.walker( range );\r
189 \r
190                                         var start = bookmarks[ i ].startNode,\r
191                                                 end = bookmarks[ i++ ].endNode;\r
192 \r
193                                         walker.evaluator = function( node )\r
194                                         {\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
203                                         };\r
204 \r
205                                         while ( ( block = walker.next() ) )\r
206                                                 switchDir( block, dir, editor, database );\r
207 \r
208                                         iterator = range.createIterator();\r
209                                         iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
210 \r
211                                         while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )\r
212                                                 switchDir( block, dir, editor, database );\r
213                                         }\r
214 \r
215                                 CKEDITOR.dom.element.clearAllMarkers( database );\r
216 \r
217                                 editor.forceNextSelectionCheck();\r
218                                 // Restore selection position.\r
219                                 selection.selectBookmarks( bookmarks );\r
220 \r
221                                 editor.focus();\r
222                         }\r
223                 };\r
224         }\r
225 \r
226         CKEDITOR.plugins.add( 'bidi',\r
227         {\r
228                 requires : [ 'styles', 'button' ],\r
229 \r
230                 init : function( editor )\r
231                 {\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
235                         {\r
236                                 editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );\r
237 \r
238                                 editor.ui.addButton( buttonName,\r
239                                         {\r
240                                                 label : buttonLabel,\r
241                                                 command : commandName\r
242                                         });\r
243                         };\r
244 \r
245                         var lang = editor.lang.bidi;\r
246 \r
247                         addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );\r
248                         addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );\r
249 \r
250                         editor.on( 'selectionChange', onSelectionChange );\r
251                         editor.on( 'contentDom', function()\r
252                         {\r
253                                 editor.document.on( 'dirChanged', function( evt )\r
254                                 {\r
255                                         editor.fire( 'dirChanged',\r
256                                                 {\r
257                                                         node : evt.data,\r
258                                                         dir : evt.data.getDirection( 1 )\r
259                                                 } );\r
260                                 });\r
261                         });\r
262                 }\r
263         });\r
264 \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
269         {\r
270                 var html = el.getDocument().getBody().getParent();\r
271                 while ( el )\r
272                 {\r
273                         if ( el.equals( html ) )\r
274                                 return false;\r
275                         el = el.getParent();\r
276                 }\r
277                 return true;\r
278         }\r
279         function dirChangeNotifier( org )\r
280         {\r
281                 var isAttribute = org == elementProto.setAttribute,\r
282                         isRemoveAttribute = org == elementProto.removeAttribute,\r
283                         dirStyleRegexp = /\bdirection\s*:\s*(.*?)\s*(:?$|;)/;\r
284 \r
285                 return function( name, val )\r
286                 {\r
287                         if ( !this.getDocument().equals( CKEDITOR.document ) )\r
288                         {\r
289                                 var orgDir;\r
290                                 if ( ( name == ( isAttribute || isRemoveAttribute ? 'dir' : 'direction' ) ||\r
291                                          name == 'style' && ( isRemoveAttribute || dirStyleRegexp.test( val ) ) ) && !isOffline( this ) )\r
292                                 {\r
293                                         orgDir = this.getDirection( 1 );\r
294                                         var retval = org.apply( this, arguments );\r
295                                         if ( orgDir != this.getDirection( 1 ) )\r
296                                         {\r
297                                                 this.getDocument().fire( 'dirChanged', this );\r
298                                                 return retval;\r
299                                         }\r
300                                 }\r
301                         }\r
302 \r
303                         return org.apply( this, arguments );\r
304                 };\r
305         }\r
306 \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
311 })();\r
312 \r
313 /**\r
314  * Fired when the language direction of an element is changed\r
315  * @name CKEDITOR.editor#dirChanged\r
316  * @event\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
320  */\r
321 \r
322 /**\r
323  * Fired when the language direction in the specific cursor position is changed\r
324  * @name CKEDITOR.editor#contentDirChanged\r
325  * @event\r
326  * @param {String} eventData The direction in the current position.\r
327  */\r