1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 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.FullBasePath ;
33		var sTableBehavior = '' ;
34		var sStyle ;
35
36		// The behaviors should be pointed using the FullBasePath 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	FCK.Events.FireEvent( "OnSelectionChange" ) ;
96}
97
98function Doc_OnDrop()
99{
100	if ( FCK.MouseDownFlag )
101	{
102		FCK.MouseDownFlag = false ;
103		return ;
104	}
105	var evt = FCK.EditorWindow.event ;
106	if ( FCKConfig.ForcePasteAsPlainText )
107	{
108		if ( FCK._CheckIsPastingEnabled() || FCKConfig.ShowDropDialog )
109			FCK.PasteAsPlainText( evt.dataTransfer.getData( 'Text' ) ) ;
110	}
111	else
112	{
113		if ( FCKConfig.ShowDropDialog )
114			FCKTools.RunFunction( FCKDialog.OpenDialog,
115				FCKDialog, ['FCKDialog_Paste', FCKLang.Paste, 'dialog/fck_paste.html', 400, 330, 'Security'] ) ;
116	}
117	evt.returnValue = false ;
118	evt.cancelBubble = true ;
119}
120
121FCK.InitializeBehaviors = function( dontReturn )
122{
123	// Set the focus to the editable area when clicking in the document area.
124	// TODO: The cursor must be positioned at the end.
125	this.EditorDocument.attachEvent( 'onmouseup', Doc_OnMouseUp ) ;
126
127	// Intercept pasting operations
128	this.EditorDocument.body.attachEvent( 'onpaste', Doc_OnPaste ) ;
129
130	// Intercept drop operations
131	this.EditorDocument.body.attachEvent( 'ondrop', Doc_OnDrop ) ;
132
133	// Reset the context menu.
134	FCK.ContextMenu._InnerContextMenu.AttachToElement( FCK.EditorDocument.body ) ;
135
136	this.EditorDocument.attachEvent("onkeydown", FCK._KeyDownListener ) ;
137
138	this.EditorDocument.attachEvent("ondblclick", Doc_OnDblClick ) ;
139
140	// Catch cursor selection changes.
141	this.EditorDocument.attachEvent("onselectionchange", Doc_OnSelectionChange ) ;
142}
143
144FCK.InsertHtml = function( html )
145{
146	html = FCKConfig.ProtectedSource.Protect( html ) ;
147	html = FCK.ProtectEvents( html ) ;
148	html = FCK.ProtectUrls( html ) ;
149	html = FCK.ProtectTags( html ) ;
150
151//	FCK.Focus() ;
152	FCK.EditorWindow.focus() ;
153
154	FCKUndo.SaveUndoStep() ;
155
156	// Gets the actual selection.
157	var oSel = FCK.EditorDocument.selection ;
158
159	// Deletes the actual selection contents.
160	if ( oSel.type.toLowerCase() == 'control' )
161		oSel.clear() ;
162
163	// Using the following trick, any comment in the beginning of the HTML will
164	// be preserved.
165	html = '<span id="__fakeFCKRemove__">&nbsp;</span>' + html ;
166
167	// Insert the HTML.
168	oSel.createRange().pasteHTML( html ) ;
169
170	// Remove the fake node
171	FCK.EditorDocument.getElementById('__fakeFCKRemove__').removeNode( true ) ;
172
173	FCKDocumentProcessor.Process( FCK.EditorDocument ) ;
174
175	// For some strange reason the SaveUndoStep() call doesn't activate the undo button at the first InsertHtml() call.
176	this.Events.FireEvent( "OnSelectionChange" ) ;
177}
178
179FCK.SetInnerHtml = function( html )		// IE Only
180{
181	var oDoc = FCK.EditorDocument ;
182	// Using the following trick, any comment in the beginning of the HTML will
183	// be preserved.
184	oDoc.body.innerHTML = '<div id="__fakeFCKRemove__">&nbsp;</div>' + html ;
185	oDoc.getElementById('__fakeFCKRemove__').removeNode( true ) ;
186}
187
188function FCK_PreloadImages()
189{
190	var oPreloader = new FCKImagePreloader() ;
191
192	// Add the configured images.
193	oPreloader.AddImages( FCKConfig.PreloadImages ) ;
194
195	// Add the skin icons strip.
196	oPreloader.AddImages( FCKConfig.SkinPath + 'fck_strip.gif' ) ;
197
198	oPreloader.OnComplete = LoadToolbarSetup ;
199	oPreloader.Start() ;
200}
201
202// Disable the context menu in the editor (outside the editing area).
203function Document_OnContextMenu()
204{
205	return ( event.srcElement._FCKShowContextMenu == true ) ;
206}
207document.oncontextmenu = Document_OnContextMenu ;
208
209function FCK_Cleanup()
210{
211	this.LinkedField = null ;
212	this.EditorWindow = null ;
213	this.EditorDocument = null ;
214}
215
216FCK._ExecPaste = function()
217{
218	// As we call ExecuteNamedCommand('Paste'), it would enter in a loop. So, let's use a semaphore.
219	if ( FCK._PasteIsRunning )
220		return true ;
221
222	if ( FCKConfig.ForcePasteAsPlainText )
223	{
224		FCK.PasteAsPlainText() ;
225		return false ;
226	}
227
228	var sHTML = FCK._CheckIsPastingEnabled( true ) ;
229
230	if ( sHTML === false )
231		FCKTools.RunFunction( FCKDialog.OpenDialog, FCKDialog, ['FCKDialog_Paste', FCKLang.Paste, 'dialog/fck_paste.html', 400, 330, 'Security'] ) ;
232	else
233	{
234		if ( FCKConfig.AutoDetectPasteFromWord && sHTML.length > 0 )
235		{
236			var re = /<\w[^>]*(( class="?MsoNormal"?)|(="mso-))/gi ;
237			if ( re.test( sHTML ) )
238			{
239				if ( confirm( FCKLang.PasteWordConfirm ) )
240				{
241					FCK.PasteFromWord() ;
242					return false ;
243				}
244			}
245		}
246
247		// Instead of inserting the retrieved HTML, let's leave the OS work for us,
248		// by calling FCK.ExecuteNamedCommand( 'Paste' ). It could give better results.
249
250		// Enable the semaphore to avoid a loop.
251		FCK._PasteIsRunning = true ;
252
253		FCK.ExecuteNamedCommand( 'Paste' ) ;
254
255		// Removes the semaphore.
256		delete FCK._PasteIsRunning ;
257	}
258
259	// Let's always make a custom implementation (return false), otherwise
260	// the new Keyboard Handler may conflict with this code, and the CTRL+V code
261	// could result in a simple "V" being pasted.
262	return false ;
263}
264
265FCK.PasteAsPlainText = function( forceText )
266{
267	if ( !FCK._CheckIsPastingEnabled() )
268	{
269		FCKDialog.OpenDialog( 'FCKDialog_Paste', FCKLang.PasteAsText, 'dialog/fck_paste.html', 400, 330, 'PlainText' ) ;
270		return ;
271	}
272
273	// Get the data available in the clipboard in text format.
274	var sText = null ;
275	if ( ! forceText )
276		sText = clipboardData.getData("Text") ;
277	else
278		sText = forceText ;
279
280	if ( sText && sText.length > 0 )
281	{
282		// Replace the carriage returns with <BR>
283		sText = FCKTools.HTMLEncode( sText ) ;
284		sText = FCKTools.ProcessLineBreaks( window, FCKConfig, sText ) ;
285
286		// Insert the resulting data in the editor.
287		this.InsertHtml( sText ) ;
288	}
289}
290
291FCK._CheckIsPastingEnabled = function( returnContents )
292{
293	// The following seams to be the only reliable way to check is script
294	// pasting operations are enabled in the security settings of IE6 and IE7.
295	// It adds a little bit of overhead to the check, but so far that's the
296	// only way, mainly because of IE7.
297
298	FCK._PasteIsEnabled = false ;
299
300	document.body.attachEvent( 'onpaste', FCK_CheckPasting_Listener ) ;
301
302	// The execCommand in GetClipboardHTML will fire the "onpaste", only if the
303	// security settings are enabled.
304	var oReturn = FCK.GetClipboardHTML() ;
305
306	document.body.detachEvent( 'onpaste', FCK_CheckPasting_Listener ) ;
307
308	if ( FCK._PasteIsEnabled )
309	{
310		if ( !returnContents )
311			oReturn = true ;
312	}
313	else
314		oReturn = false ;
315
316	delete FCK._PasteIsEnabled ;
317
318	return oReturn ;
319}
320
321function FCK_CheckPasting_Listener()
322{
323	FCK._PasteIsEnabled = true ;
324}
325
326FCK.GetClipboardHTML = function()
327{
328	var oDiv = document.getElementById( '___FCKHiddenDiv' ) ;
329
330	if ( !oDiv )
331	{
332		oDiv = document.createElement( 'DIV' ) ;
333		oDiv.id = '___FCKHiddenDiv' ;
334
335		var oDivStyle = oDiv.style ;
336		oDivStyle.position		= 'absolute' ;
337		oDivStyle.visibility	= oDivStyle.overflow	= 'hidden' ;
338		oDivStyle.width			= oDivStyle.height		= 1 ;
339
340		document.body.appendChild( oDiv ) ;
341	}
342
343	oDiv.innerHTML = '' ;
344
345	var oTextRange = document.body.createTextRange() ;
346	oTextRange.moveToElementText( oDiv ) ;
347	oTextRange.execCommand( 'Paste' ) ;
348
349	var sData = oDiv.innerHTML ;
350	oDiv.innerHTML = '' ;
351
352	return sData ;
353}
354
355FCK.CreateLink = function( url, noUndo )
356{
357	// Creates the array that will be returned. It contains one or more created links (see #220).
358	var aCreatedLinks = new Array() ;
359
360	// Remove any existing link in the selection.
361	FCK.ExecuteNamedCommand( 'Unlink', null, false, !!noUndo ) ;
362
363	if ( url.length > 0 )
364	{
365		// If there are several images, and you try to link each one, all the images get inside the link:
366		// <img><img> -> <a><img></a><img> -> <a><img><img></a> due to the call to 'CreateLink' (bug in IE)
367		if (FCKSelection.GetType() == 'Control')
368		{
369			// Create a link
370			var oLink = this.EditorDocument.createElement( 'A' ) ;
371			oLink.href = url ;
372
373			// Get the selected object
374			var oControl = FCKSelection.GetSelectedElement() ;
375			// Put the link just before the object
376			oControl.parentNode.insertBefore(oLink, oControl) ;
377			// Move the object inside the link
378			oControl.parentNode.removeChild( oControl ) ;
379			oLink.appendChild( oControl ) ;
380
381			return [ oLink ] ;
382		}
383
384		// Generate a temporary name for the link.
385		var sTempUrl = 'javascript:void(0);/*' + ( new Date().getTime() ) + '*/' ;
386
387		// Use the internal "CreateLink" command to create the link.
388		FCK.ExecuteNamedCommand( 'CreateLink', sTempUrl, false, !!noUndo ) ;
389
390		// Look for the just create link.
391		var oLinks = this.EditorDocument.links ;
392
393		for ( i = 0 ; i < oLinks.length ; i++ )
394		{
395			var oLink = oLinks[i] ;
396
397			// Check it this a newly created link.
398			// getAttribute must be used. oLink.url may cause problems with IE7 (#555).
399			if ( oLink.getAttribute( 'href', 2 ) == sTempUrl )
400			{
401				var sInnerHtml = oLink.innerHTML ;	// Save the innerHTML (IE changes it if it is like an URL).
402				oLink.href = url ;
403				oLink.innerHTML = sInnerHtml ;		// Restore the innerHTML.
404
405				// If the last child is a <br> move it outside the link or it
406				// will be too easy to select this link again #388.
407				var oLastChild = oLink.lastChild ;
408				if ( oLastChild && oLastChild.nodeName == 'BR' )
409				{
410					// Move the BR after the link.
411					FCKDomTools.InsertAfterNode( oLink, oLink.removeChild( oLastChild ) ) ;
412				}
413
414				aCreatedLinks.push( oLink ) ;
415			}
416		}
417	}
418
419	return aCreatedLinks ;
420}
421