source: extensions/FCKEditor/editor/_source/internals/fck_ie.js @ 3295

Last change on this file since 3295 was 3295, checked in by patdenice, 15 years ago

New extension added:
FCK Editor (2.0.a)

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