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 * Advanced document processors.
22 */
23
24var FCKDocumentProcessor = new Object() ;
25FCKDocumentProcessor._Items = new Array() ;
26
27FCKDocumentProcessor.AppendNew = function()
28{
29	var oNewItem = new Object() ;
30	this._Items.AddItem( oNewItem ) ;
31	return oNewItem ;
32}
33
34FCKDocumentProcessor.Process = function( document )
35{
36	var oProcessor, i = 0 ;
37	while( ( oProcessor = this._Items[i++] ) )
38		oProcessor.ProcessDocument( document ) ;
39}
40
41var FCKDocumentProcessor_CreateFakeImage = function( fakeClass, realElement )
42{
43	var oImg = FCK.EditorDocument.createElement( 'IMG' ) ;
44	oImg.className = fakeClass ;
45	oImg.src = FCKConfig.FullBasePath + 'images/spacer.gif' ;
46	oImg.setAttribute( '_fckfakelement', 'true', 0 ) ;
47	oImg.setAttribute( '_fckrealelement', FCKTempBin.AddElement( realElement ), 0 ) ;
48	return oImg ;
49}
50
51// Link Anchors
52if ( FCKBrowserInfo.IsIE || FCKBrowserInfo.IsOpera )
53{
54	var FCKAnchorsProcessor = FCKDocumentProcessor.AppendNew() ;
55	FCKAnchorsProcessor.ProcessDocument = function( document )
56	{
57		var aLinks = document.getElementsByTagName( 'A' ) ;
58
59		var oLink ;
60		var i = aLinks.length - 1 ;
61		while ( i >= 0 && ( oLink = aLinks[i--] ) )
62		{
63			// If it is anchor. Doesn't matter if it's also a link (even better: we show that it's both a link and an anchor)
64			if ( oLink.name.length > 0 )
65			{
66				//if the anchor has some content then we just add a temporary class
67				if ( oLink.innerHTML != '' )
68				{
69					if ( FCKBrowserInfo.IsIE )
70						oLink.className += ' FCK__AnchorC' ;
71				}
72				else
73				{
74					var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__Anchor', oLink.cloneNode(true) ) ;
75					oImg.setAttribute( '_fckanchor', 'true', 0 ) ;
76
77					oLink.parentNode.insertBefore( oImg, oLink ) ;
78					oLink.parentNode.removeChild( oLink ) ;
79				}
80			}
81		}
82	}
83}
84
85// Page Breaks
86var FCKPageBreaksProcessor = FCKDocumentProcessor.AppendNew() ;
87FCKPageBreaksProcessor.ProcessDocument = function( document )
88{
89	var aDIVs = document.getElementsByTagName( 'DIV' ) ;
90
91	var eDIV ;
92	var i = aDIVs.length - 1 ;
93	while ( i >= 0 && ( eDIV = aDIVs[i--] ) )
94	{
95		if ( eDIV.style.pageBreakAfter == 'always' && eDIV.childNodes.length == 1 && eDIV.childNodes[0].style && eDIV.childNodes[0].style.display == 'none' )
96		{
97			var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', eDIV.cloneNode(true) ) ;
98
99			eDIV.parentNode.insertBefore( oFakeImage, eDIV ) ;
100			eDIV.parentNode.removeChild( eDIV ) ;
101		}
102	}
103/*
104	var aCenters = document.getElementsByTagName( 'CENTER' ) ;
105
106	var oCenter ;
107	var i = aCenters.length - 1 ;
108	while ( i >= 0 && ( oCenter = aCenters[i--] ) )
109	{
110		if ( oCenter.style.pageBreakAfter == 'always' && oCenter.innerHTML.Trim().length == 0 )
111		{
112			var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', oCenter.cloneNode(true) ) ;
113
114			oCenter.parentNode.insertBefore( oFakeImage, oCenter ) ;
115			oCenter.parentNode.removeChild( oCenter ) ;
116		}
117	}
118*/
119}
120
121// Flash Embeds.
122var FCKFlashProcessor = FCKDocumentProcessor.AppendNew() ;
123FCKFlashProcessor.ProcessDocument = function( document )
124{
125	/*
126	Sample code:
127	This is some <embed src="/UserFiles/Flash/Yellow_Runners.swf"></embed><strong>sample text</strong>. You are&nbsp;<a name="fred"></a> using <a href="http://www.fckeditor.net/">FCKeditor</a>.
128	*/
129
130	var aEmbeds = document.getElementsByTagName( 'EMBED' ) ;
131
132	var oEmbed ;
133	var i = aEmbeds.length - 1 ;
134	while ( i >= 0 && ( oEmbed = aEmbeds[i--] ) )
135	{
136		// IE doesn't return the type attribute with oEmbed.type or oEmbed.getAttribute("type")
137		// But it turns out that after accessing it then it doesn't gets copied later
138		var oType = oEmbed.attributes[ 'type' ] ;
139
140		// Check the extension and the type. Now it should be enough with just the type
141		// Opera doesn't return oEmbed.src so oEmbed.src.EndsWith will fail
142		if ( (oEmbed.src && oEmbed.src.EndsWith( '.swf', true )) || ( oType && oType.nodeValue == 'application/x-shockwave-flash' ) )
143		{
144			var oCloned = oEmbed.cloneNode( true ) ;
145
146			// On IE, some properties are not getting clonned properly, so we
147			// must fix it. Thanks to Alfonso Martinez.
148			if ( FCKBrowserInfo.IsIE )
149			{
150				var aAttributes = [ 'scale', 'play', 'loop', 'menu', 'wmode', 'quality' ] ;
151				for ( var iAtt = 0 ; iAtt < aAttributes.length ; iAtt++ )
152				{
153					var oAtt = oEmbed.getAttribute( aAttributes[iAtt] ) ;
154					if ( oAtt ) oCloned.setAttribute( aAttributes[iAtt], oAtt ) ;
155				}
156				// It magically gets lost after reading it in oType
157				oCloned.setAttribute( 'type', oType.nodeValue ) ;
158			}
159
160			var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__Flash', oCloned ) ;
161			oImg.setAttribute( '_fckflash', 'true', 0 ) ;
162
163			FCKFlashProcessor.RefreshView( oImg, oEmbed ) ;
164
165			oEmbed.parentNode.insertBefore( oImg, oEmbed ) ;
166			oEmbed.parentNode.removeChild( oEmbed ) ;
167
168//			oEmbed.setAttribute( '_fcktemp', 'true', 0) ;
169//			oEmbed.style.display = 'none' ;
170//			oEmbed.hidden = true ;
171		}
172	}
173}
174
175FCKFlashProcessor.RefreshView = function( placholderImage, originalEmbed )
176{
177	if ( originalEmbed.width > 0 )
178		placholderImage.style.width = FCKTools.ConvertHtmlSizeToStyle( originalEmbed.width ) ;
179
180	if ( originalEmbed.height > 0 )
181		placholderImage.style.height = FCKTools.ConvertHtmlSizeToStyle( originalEmbed.height ) ;
182}
183
184FCK.GetRealElement = function( fakeElement )
185{
186	var e = FCKTempBin.Elements[ fakeElement.getAttribute('_fckrealelement') ] ;
187
188	if ( fakeElement.getAttribute('_fckflash') )
189	{
190		if ( fakeElement.style.width.length > 0 )
191				e.width = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.width ) ;
192
193		if ( fakeElement.style.height.length > 0 )
194				e.height = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.height ) ;
195	}
196
197	return e ;
198}
199
200// HR Processor.
201// This is a IE only (tricky) thing. We protect all HR tags before loading them
202// (see FCK.ProtectTags). Here we put the HRs back.
203if ( FCKBrowserInfo.IsIE )
204{
205	FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
206	{
207		var aHRs = document.getElementsByTagName( 'HR' ) ;
208
209		var eHR ;
210		var i = aHRs.length - 1 ;
211		while ( i >= 0 && ( eHR = aHRs[i--] ) )
212		{
213			// Create the replacement HR.
214			var newHR = document.createElement( 'hr' ) ;
215			newHR.mergeAttributes( eHR, true ) ;
216
217			// We must insert the new one after it. insertBefore will not work in all cases.
218			FCKDomTools.InsertAfterNode( eHR, newHR ) ;
219
220			eHR.parentNode.removeChild( eHR ) ;
221		}
222	}
223}
224
225// INPUT:hidden Processor.
226FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
227{
228	var aInputs = document.getElementsByTagName( 'INPUT' ) ;
229
230	var oInput ;
231	var i = aInputs.length - 1 ;
232	while ( i >= 0 && ( oInput = aInputs[i--] ) )
233	{
234		if ( oInput.type == 'hidden' )
235		{
236			var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__InputHidden', oInput.cloneNode(true) ) ;
237			oImg.setAttribute( '_fckinputhidden', 'true', 0 ) ;
238
239			oInput.parentNode.insertBefore( oImg, oInput ) ;
240			oInput.parentNode.removeChild( oInput ) ;
241		}
242	}
243}