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			var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__Flash', oCloned ) ;
147			oImg.setAttribute( '_fckflash', 'true', 0 ) ;
148
149			FCKFlashProcessor.RefreshView( oImg, oEmbed ) ;
150
151			oEmbed.parentNode.insertBefore( oImg, oEmbed ) ;
152			oEmbed.parentNode.removeChild( oEmbed ) ;
153		}
154	}
155}
156
157FCKFlashProcessor.RefreshView = function( placeHolderImage, originalEmbed )
158{
159	if ( originalEmbed.getAttribute( 'width' ) > 0 )
160		placeHolderImage.style.width = FCKTools.ConvertHtmlSizeToStyle( originalEmbed.getAttribute( 'width' ) ) ;
161
162	if ( originalEmbed.getAttribute( 'height' ) > 0 )
163		placeHolderImage.style.height = FCKTools.ConvertHtmlSizeToStyle( originalEmbed.getAttribute( 'height' ) ) ;
164}
165
166FCK.GetRealElement = function( fakeElement )
167{
168	var e = FCKTempBin.Elements[ fakeElement.getAttribute('_fckrealelement') ] ;
169
170	if ( fakeElement.getAttribute('_fckflash') )
171	{
172		if ( fakeElement.style.width.length > 0 )
173				e.width = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.width ) ;
174
175		if ( fakeElement.style.height.length > 0 )
176				e.height = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.height ) ;
177	}
178
179	return e ;
180}
181
182// HR Processor.
183// This is a IE only (tricky) thing. We protect all HR tags before loading them
184// (see FCK.ProtectTags). Here we put the HRs back.
185if ( FCKBrowserInfo.IsIE )
186{
187	FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
188	{
189		var aHRs = document.getElementsByTagName( 'HR' ) ;
190
191		var eHR ;
192		var i = aHRs.length - 1 ;
193		while ( i >= 0 && ( eHR = aHRs[i--] ) )
194		{
195			// Create the replacement HR.
196			var newHR = document.createElement( 'hr' ) ;
197			newHR.mergeAttributes( eHR, true ) ;
198
199			// We must insert the new one after it. insertBefore will not work in all cases.
200			FCKDomTools.InsertAfterNode( eHR, newHR ) ;
201
202			eHR.parentNode.removeChild( eHR ) ;
203		}
204	}
205}
206
207// INPUT:hidden Processor.
208FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
209{
210	var aInputs = document.getElementsByTagName( 'INPUT' ) ;
211
212	var oInput ;
213	var i = aInputs.length - 1 ;
214	while ( i >= 0 && ( oInput = aInputs[i--] ) )
215	{
216		if ( oInput.type == 'hidden' )
217		{
218			var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__InputHidden', oInput.cloneNode(true) ) ;
219			oImg.setAttribute( '_fckinputhidden', 'true', 0 ) ;
220
221			oInput.parentNode.insertBefore( oImg, oInput ) ;
222			oInput.parentNode.removeChild( oInput ) ;
223		}
224	}
225}