import rt 3.8.7
[freeside.git] / rt / share / html / NoAuth / RichText / FCKeditor / editor / _source / internals / fck_ie.js
1 /*\r
2  * FCKeditor - The text editor for Internet - http://www.fckeditor.net\r
3  * Copyright (C) 2003-2009 Frederico Caldeira Knabben\r
4  *\r
5  * == BEGIN LICENSE ==\r
6  *\r
7  * Licensed under the terms of any of the following licenses at your\r
8  * choice:\r
9  *\r
10  *  - GNU General Public License Version 2 or later (the "GPL")\r
11  *    http://www.gnu.org/licenses/gpl.html\r
12  *\r
13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")\r
14  *    http://www.gnu.org/licenses/lgpl.html\r
15  *\r
16  *  - Mozilla Public License Version 1.1 or later (the "MPL")\r
17  *    http://www.mozilla.org/MPL/MPL-1.1.html\r
18  *\r
19  * == END LICENSE ==\r
20  *\r
21  * Creation and initialization of the "FCK" object. This is the main\r
22  * object that represents an editor instance.\r
23  * (IE specific implementations)\r
24  */\r
25 \r
26 FCK.Description = "FCKeditor for Internet Explorer 5.5+" ;\r
27 \r
28 FCK._GetBehaviorsStyle = function()\r
29 {\r
30         if ( !FCK._BehaviorsStyle )\r
31         {\r
32                 var sBasePath = FCKConfig.BasePath ;\r
33                 var sTableBehavior = '' ;\r
34                 var sStyle ;\r
35 \r
36                 // The behaviors should be pointed using the BasePath to avoid security\r
37                 // errors when using a different BaseHref.\r
38                 sStyle = '<style type="text/css" _fcktemp="true">' ;\r
39 \r
40                 if ( FCKConfig.ShowBorders )\r
41                         sTableBehavior = 'url(' + sBasePath + 'css/behaviors/showtableborders.htc)' ;\r
42 \r
43                 // Disable resize handlers.\r
44                 sStyle += 'INPUT,TEXTAREA,SELECT,.FCK__Anchor,.FCK__PageBreak,.FCK__InputHidden' ;\r
45 \r
46                 if ( FCKConfig.DisableObjectResizing )\r
47                 {\r
48                         sStyle += ',IMG' ;\r
49                         sTableBehavior += ' url(' + sBasePath + 'css/behaviors/disablehandles.htc)' ;\r
50                 }\r
51 \r
52                 sStyle += ' { behavior: url(' + sBasePath + 'css/behaviors/disablehandles.htc) ; }' ;\r
53 \r
54                 if ( sTableBehavior.length > 0 )\r
55                         sStyle += 'TABLE { behavior: ' + sTableBehavior + ' ; }' ;\r
56 \r
57                 sStyle += '</style>' ;\r
58                 FCK._BehaviorsStyle = sStyle ;\r
59         }\r
60 \r
61         return FCK._BehaviorsStyle ;\r
62 }\r
63 \r
64 function Doc_OnMouseUp()\r
65 {\r
66         if ( FCK.EditorWindow.event.srcElement.tagName == 'HTML' )\r
67         {\r
68                 FCK.Focus() ;\r
69                 FCK.EditorWindow.event.cancelBubble     = true ;\r
70                 FCK.EditorWindow.event.returnValue      = false ;\r
71         }\r
72 }\r
73 \r
74 function Doc_OnPaste()\r
75 {\r
76         var body = FCK.EditorDocument.body ;\r
77 \r
78         body.detachEvent( 'onpaste', Doc_OnPaste ) ;\r
79 \r
80         var ret = FCK.Paste( !FCKConfig.ForcePasteAsPlainText && !FCKConfig.AutoDetectPasteFromWord ) ;\r
81 \r
82         body.attachEvent( 'onpaste', Doc_OnPaste ) ;\r
83 \r
84         return ret ;\r
85 }\r
86 \r
87 function Doc_OnDblClick()\r
88 {\r
89         FCK.OnDoubleClick( FCK.EditorWindow.event.srcElement ) ;\r
90         FCK.EditorWindow.event.cancelBubble = true ;\r
91 }\r
92 \r
93 function Doc_OnSelectionChange()\r
94 {\r
95         // Don't fire the event if no document is loaded.\r
96         if ( !FCK.IsSelectionChangeLocked && FCK.EditorDocument )\r
97                 FCK.Events.FireEvent( "OnSelectionChange" ) ;\r
98 }\r
99 \r
100 function Doc_OnDrop()\r
101 {\r
102         if ( FCK.MouseDownFlag )\r
103         {\r
104                 FCK.MouseDownFlag = false ;\r
105                 return ;\r
106         }\r
107 \r
108         if ( FCKConfig.ForcePasteAsPlainText )\r
109         {\r
110                 var evt = FCK.EditorWindow.event ;\r
111 \r
112                 if ( FCK._CheckIsPastingEnabled() || FCKConfig.ShowDropDialog )\r
113                         FCK.PasteAsPlainText( evt.dataTransfer.getData( 'Text' ) ) ;\r
114 \r
115                 evt.returnValue = false ;\r
116                 evt.cancelBubble = true ;\r
117         }\r
118 }\r
119 \r
120 FCK.InitializeBehaviors = function( dontReturn )\r
121 {\r
122         // Set the focus to the editable area when clicking in the document area.\r
123         // TODO: The cursor must be positioned at the end.\r
124         this.EditorDocument.attachEvent( 'onmouseup', Doc_OnMouseUp ) ;\r
125 \r
126         // Intercept pasting operations\r
127         this.EditorDocument.body.attachEvent( 'onpaste', Doc_OnPaste ) ;\r
128 \r
129         // Intercept drop operations\r
130         this.EditorDocument.body.attachEvent( 'ondrop', Doc_OnDrop ) ;\r
131 \r
132         // Reset the context menu.\r
133         FCK.ContextMenu._InnerContextMenu.AttachToElement( FCK.EditorDocument.body ) ;\r
134 \r
135         this.EditorDocument.attachEvent("onkeydown", FCK._KeyDownListener ) ;\r
136 \r
137         this.EditorDocument.attachEvent("ondblclick", Doc_OnDblClick ) ;\r
138 \r
139         this.EditorDocument.attachEvent("onbeforedeactivate", function(){ FCKSelection.Save() ; } ) ;\r
140 \r
141         // Catch cursor selection changes.\r
142         this.EditorDocument.attachEvent("onselectionchange", Doc_OnSelectionChange ) ;\r
143 \r
144         FCKTools.AddEventListener( FCK.EditorDocument, 'mousedown', Doc_OnMouseDown ) ;\r
145 }\r
146 \r
147 FCK.InsertHtml = function( html )\r
148 {\r
149         html = FCKConfig.ProtectedSource.Protect( html ) ;\r
150         html = FCK.ProtectEvents( html ) ;\r
151         html = FCK.ProtectUrls( html ) ;\r
152         html = FCK.ProtectTags( html ) ;\r
153 \r
154 //      FCK.Focus() ;\r
155         FCKSelection.Restore() ;\r
156         FCK.EditorWindow.focus() ;\r
157 \r
158         FCKUndo.SaveUndoStep() ;\r
159 \r
160         // Gets the actual selection.\r
161         var oSel = FCKSelection.GetSelection() ;\r
162 \r
163         // Deletes the actual selection contents.\r
164         if ( oSel.type.toLowerCase() == 'control' )\r
165                 oSel.clear() ;\r
166 \r
167         // Using the following trick, any comment in the beginning of the HTML will\r
168         // be preserved.\r
169         html = '<span id="__fakeFCKRemove__" style="display:none;">fakeFCKRemove</span>' + html ;\r
170 \r
171         // Insert the HTML.\r
172         oSel.createRange().pasteHTML( html ) ;\r
173 \r
174         // Remove the fake node\r
175         FCK.EditorDocument.getElementById('__fakeFCKRemove__').removeNode( true ) ;\r
176 \r
177         FCKDocumentProcessor.Process( FCK.EditorDocument ) ;\r
178 \r
179         // For some strange reason the SaveUndoStep() call doesn't activate the undo button at the first InsertHtml() call.\r
180         this.Events.FireEvent( "OnSelectionChange" ) ;\r
181 }\r
182 \r
183 FCK.SetInnerHtml = function( html )             // IE Only\r
184 {\r
185         var oDoc = FCK.EditorDocument ;\r
186         // Using the following trick, any comment in the beginning of the HTML will\r
187         // be preserved.\r
188         oDoc.body.innerHTML = '<div id="__fakeFCKRemove__">&nbsp;</div>' + html ;\r
189         oDoc.getElementById('__fakeFCKRemove__').removeNode( true ) ;\r
190 }\r
191 \r
192 function FCK_PreloadImages()\r
193 {\r
194         var oPreloader = new FCKImagePreloader() ;\r
195 \r
196         // Add the configured images.\r
197         oPreloader.AddImages( FCKConfig.PreloadImages ) ;\r
198 \r
199         // Add the skin icons strip.\r
200         oPreloader.AddImages( FCKConfig.SkinPath + 'fck_strip.gif' ) ;\r
201 \r
202         oPreloader.OnComplete = LoadToolbarSetup ;\r
203         oPreloader.Start() ;\r
204 }\r
205 \r
206 // Disable the context menu in the editor (outside the editing area).\r
207 function Document_OnContextMenu()\r
208 {\r
209         return ( event.srcElement._FCKShowContextMenu == true ) ;\r
210 }\r
211 document.oncontextmenu = Document_OnContextMenu ;\r
212 \r
213 function FCK_Cleanup()\r
214 {\r
215         this.LinkedField = null ;\r
216         this.EditorWindow = null ;\r
217         this.EditorDocument = null ;\r
218 }\r
219 \r
220 FCK._ExecPaste = function()\r
221 {\r
222         // As we call ExecuteNamedCommand('Paste'), it would enter in a loop. So, let's use a semaphore.\r
223         if ( FCK._PasteIsRunning )\r
224                 return true ;\r
225 \r
226         if ( FCKConfig.ForcePasteAsPlainText )\r
227         {\r
228                 FCK.PasteAsPlainText() ;\r
229                 return false ;\r
230         }\r
231 \r
232         var sHTML = FCK._CheckIsPastingEnabled( true ) ;\r
233 \r
234         if ( sHTML === false )\r
235                 FCKTools.RunFunction( FCKDialog.OpenDialog, FCKDialog, ['FCKDialog_Paste', FCKLang.Paste, 'dialog/fck_paste.html', 400, 330, 'Security'] ) ;\r
236         else\r
237         {\r
238                 if ( FCKConfig.AutoDetectPasteFromWord && sHTML.length > 0 )\r
239                 {\r
240                         var re = /<\w[^>]*(( class="?MsoNormal"?)|(="mso-))/gi ;\r
241                         if ( re.test( sHTML ) )\r
242                         {\r
243                                 if ( confirm( FCKLang.PasteWordConfirm ) )\r
244                                 {\r
245                                         FCK.PasteFromWord() ;\r
246                                         return false ;\r
247                                 }\r
248                         }\r
249                 }\r
250 \r
251                 // Instead of inserting the retrieved HTML, let's leave the OS work for us,\r
252                 // by calling FCK.ExecuteNamedCommand( 'Paste' ). It could give better results.\r
253 \r
254                 // Enable the semaphore to avoid a loop.\r
255                 FCK._PasteIsRunning = true ;\r
256 \r
257                 FCK.ExecuteNamedCommand( 'Paste' ) ;\r
258 \r
259                 // Removes the semaphore.\r
260                 delete FCK._PasteIsRunning ;\r
261         }\r
262 \r
263         // Let's always make a custom implementation (return false), otherwise\r
264         // the new Keyboard Handler may conflict with this code, and the CTRL+V code\r
265         // could result in a simple "V" being pasted.\r
266         return false ;\r
267 }\r
268 \r
269 FCK.PasteAsPlainText = function( forceText )\r
270 {\r
271         if ( !FCK._CheckIsPastingEnabled() )\r
272         {\r
273                 FCKDialog.OpenDialog( 'FCKDialog_Paste', FCKLang.PasteAsText, 'dialog/fck_paste.html', 400, 330, 'PlainText' ) ;\r
274                 return ;\r
275         }\r
276 \r
277         // Get the data available in the clipboard in text format.\r
278         var sText = null ;\r
279         if ( ! forceText )\r
280                 sText = clipboardData.getData("Text") ;\r
281         else\r
282                 sText = forceText ;\r
283 \r
284         if ( sText && sText.length > 0 )\r
285         {\r
286                 // Replace the carriage returns with <BR>\r
287                 sText = FCKTools.HTMLEncode( sText ) ;\r
288                 sText = FCKTools.ProcessLineBreaks( window, FCKConfig, sText ) ;\r
289 \r
290                 var closeTagIndex = sText.search( '</p>' ) ;\r
291                 var startTagIndex = sText.search( '<p>' ) ;\r
292 \r
293                 if ( ( closeTagIndex != -1 && startTagIndex != -1 && closeTagIndex < startTagIndex )\r
294                                 || ( closeTagIndex != -1 && startTagIndex == -1 ) )\r
295                 {\r
296                         var prefix = sText.substr( 0, closeTagIndex ) ;\r
297                         sText = sText.substr( closeTagIndex + 4 ) ;\r
298                         this.InsertHtml( prefix ) ;\r
299                 }\r
300 \r
301                 // Insert the resulting data in the editor.\r
302                 FCKUndo.SaveLocked = true ;\r
303                 this.InsertHtml( sText ) ;\r
304                 FCKUndo.SaveLocked = false ;\r
305         }\r
306 }\r
307 \r
308 FCK._CheckIsPastingEnabled = function( returnContents )\r
309 {\r
310         // The following seams to be the only reliable way to check is script\r
311         // pasting operations are enabled in the security settings of IE6 and IE7.\r
312         // It adds a little bit of overhead to the check, but so far that's the\r
313         // only way, mainly because of IE7.\r
314 \r
315         FCK._PasteIsEnabled = false ;\r
316 \r
317         document.body.attachEvent( 'onpaste', FCK_CheckPasting_Listener ) ;\r
318 \r
319         // The execCommand in GetClipboardHTML will fire the "onpaste", only if the\r
320         // security settings are enabled.\r
321         var oReturn = FCK.GetClipboardHTML() ;\r
322 \r
323         document.body.detachEvent( 'onpaste', FCK_CheckPasting_Listener ) ;\r
324 \r
325         if ( FCK._PasteIsEnabled )\r
326         {\r
327                 if ( !returnContents )\r
328                         oReturn = true ;\r
329         }\r
330         else\r
331                 oReturn = false ;\r
332 \r
333         delete FCK._PasteIsEnabled ;\r
334 \r
335         return oReturn ;\r
336 }\r
337 \r
338 function FCK_CheckPasting_Listener()\r
339 {\r
340         FCK._PasteIsEnabled = true ;\r
341 }\r
342 \r
343 FCK.GetClipboardHTML = function()\r
344 {\r
345         var oDiv = document.getElementById( '___FCKHiddenDiv' ) ;\r
346 \r
347         if ( !oDiv )\r
348         {\r
349                 oDiv = document.createElement( 'DIV' ) ;\r
350                 oDiv.id = '___FCKHiddenDiv' ;\r
351 \r
352                 var oDivStyle = oDiv.style ;\r
353                 oDivStyle.position              = 'absolute' ;\r
354                 oDivStyle.visibility    = oDivStyle.overflow    = 'hidden' ;\r
355                 oDivStyle.width                 = oDivStyle.height              = 1 ;\r
356 \r
357                 document.body.appendChild( oDiv ) ;\r
358         }\r
359 \r
360         oDiv.innerHTML = '' ;\r
361 \r
362         var oTextRange = document.body.createTextRange() ;\r
363         oTextRange.moveToElementText( oDiv ) ;\r
364         oTextRange.execCommand( 'Paste' ) ;\r
365 \r
366         var sData = oDiv.innerHTML ;\r
367         oDiv.innerHTML = '' ;\r
368 \r
369         return sData ;\r
370 }\r
371 \r
372 FCK.CreateLink = function( url, noUndo )\r
373 {\r
374         // Creates the array that will be returned. It contains one or more created links (see #220).\r
375         var aCreatedLinks = new Array() ;\r
376 \r
377         // Remove any existing link in the selection.\r
378         FCK.ExecuteNamedCommand( 'Unlink', null, false, !!noUndo ) ;\r
379 \r
380         if ( url.length > 0 )\r
381         {\r
382                 // If there are several images, and you try to link each one, all the images get inside the link:\r
383                 // <img><img> -> <a><img></a><img> -> <a><img><img></a> due to the call to 'CreateLink' (bug in IE)\r
384                 if (FCKSelection.GetType() == 'Control')\r
385                 {\r
386                         // Create a link\r
387                         var oLink = this.EditorDocument.createElement( 'A' ) ;\r
388                         oLink.href = url ;\r
389 \r
390                         // Get the selected object\r
391                         var oControl = FCKSelection.GetSelectedElement() ;\r
392                         // Put the link just before the object\r
393                         oControl.parentNode.insertBefore(oLink, oControl) ;\r
394                         // Move the object inside the link\r
395                         oControl.parentNode.removeChild( oControl ) ;\r
396                         oLink.appendChild( oControl ) ;\r
397 \r
398                         return [ oLink ] ;\r
399                 }\r
400 \r
401                 // Generate a temporary name for the link.\r
402                 var sTempUrl = 'javascript:void(0);/*' + ( new Date().getTime() ) + '*/' ;\r
403 \r
404                 // Use the internal "CreateLink" command to create the link.\r
405                 FCK.ExecuteNamedCommand( 'CreateLink', sTempUrl, false, !!noUndo ) ;\r
406 \r
407                 // Look for the just create link.\r
408                 var oLinks = this.EditorDocument.links ;\r
409 \r
410                 for ( i = 0 ; i < oLinks.length ; i++ )\r
411                 {\r
412                         var oLink = oLinks[i] ;\r
413 \r
414                         // Check it this a newly created link.\r
415                         // getAttribute must be used. oLink.url may cause problems with IE7 (#555).\r
416                         if ( oLink.getAttribute( 'href', 2 ) == sTempUrl )\r
417                         {\r
418                                 var sInnerHtml = oLink.innerHTML ;      // Save the innerHTML (IE changes it if it is like an URL).\r
419                                 oLink.href = url ;\r
420                                 oLink.innerHTML = sInnerHtml ;          // Restore the innerHTML.\r
421 \r
422                                 // If the last child is a <br> move it outside the link or it\r
423                                 // will be too easy to select this link again #388.\r
424                                 var oLastChild = oLink.lastChild ;\r
425                                 if ( oLastChild && oLastChild.nodeName == 'BR' )\r
426                                 {\r
427                                         // Move the BR after the link.\r
428                                         FCKDomTools.InsertAfterNode( oLink, oLink.removeChild( oLastChild ) ) ;\r
429                                 }\r
430 \r
431                                 aCreatedLinks.push( oLink ) ;\r
432                         }\r
433                 }\r
434         }\r
435 \r
436         return aCreatedLinks ;\r
437 }\r
438 \r
439 function _FCK_RemoveDisabledAtt()\r
440 {\r
441         this.removeAttribute( 'disabled' ) ;\r
442 }\r
443 \r
444 function Doc_OnMouseDown( evt )\r
445 {\r
446         var e = evt.srcElement ;\r
447 \r
448         // Radio buttons and checkboxes should not be allowed to be triggered in IE\r
449         // in editable mode. Otherwise the whole browser window may be locked by\r
450         // the buttons. (#1782)\r
451         if ( e.nodeName.IEquals( 'input' ) && e.type.IEquals( ['radio', 'checkbox'] ) && !e.disabled )\r
452         {\r
453                 e.disabled = true ;\r
454                 FCKTools.SetTimeout( _FCK_RemoveDisabledAtt, 1, e ) ;\r
455         }\r
456 }\r