source: extensions/FCKEditor/editor/_source/internals/fckdocumentprocessor.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: 8.1 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 * 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.push( oNewItem ) ;
31        return oNewItem ;
32}
33
34FCKDocumentProcessor.Process = function( document )
35{
36        var bIsDirty = FCK.IsDirty() ;
37        var oProcessor, i = 0 ;
38        while( ( oProcessor = this._Items[i++] ) )
39                oProcessor.ProcessDocument( document ) ;
40        if ( !bIsDirty )
41                FCK.ResetIsDirty() ;
42}
43
44var FCKDocumentProcessor_CreateFakeImage = function( fakeClass, realElement )
45{
46        var oImg = FCKTools.GetElementDocument( realElement ).createElement( 'IMG' ) ;
47        oImg.className = fakeClass ;
48        oImg.src = FCKConfig.BasePath + 'images/spacer.gif' ;
49        oImg.setAttribute( '_fckfakelement', 'true', 0 ) ;
50        oImg.setAttribute( '_fckrealelement', FCKTempBin.AddElement( realElement ), 0 ) ;
51        return oImg ;
52}
53
54// Link Anchors
55if ( FCKBrowserInfo.IsIE || FCKBrowserInfo.IsOpera )
56{
57        var FCKAnchorsProcessor = FCKDocumentProcessor.AppendNew() ;
58        FCKAnchorsProcessor.ProcessDocument = function( document )
59        {
60                var aLinks = document.getElementsByTagName( 'A' ) ;
61
62                var oLink ;
63                var i = aLinks.length - 1 ;
64                while ( i >= 0 && ( oLink = aLinks[i--] ) )
65                {
66                        // 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)
67                        if ( oLink.name.length > 0 )
68                        {
69                                //if the anchor has some content then we just add a temporary class
70                                if ( oLink.innerHTML !== '' )
71                                {
72                                        if ( FCKBrowserInfo.IsIE )
73                                                oLink.className += ' FCK__AnchorC' ;
74                                }
75                                else
76                                {
77                                        var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__Anchor', oLink.cloneNode(true) ) ;
78                                        oImg.setAttribute( '_fckanchor', 'true', 0 ) ;
79
80                                        oLink.parentNode.insertBefore( oImg, oLink ) ;
81                                        oLink.parentNode.removeChild( oLink ) ;
82                                }
83                        }
84                }
85        }
86}
87
88// Page Breaks
89var FCKPageBreaksProcessor = FCKDocumentProcessor.AppendNew() ;
90FCKPageBreaksProcessor.ProcessDocument = function( document )
91{
92        var aDIVs = document.getElementsByTagName( 'DIV' ) ;
93
94        var eDIV ;
95        var i = aDIVs.length - 1 ;
96        while ( i >= 0 && ( eDIV = aDIVs[i--] ) )
97        {
98                if ( eDIV.style.pageBreakAfter == 'always' && eDIV.childNodes.length == 1 && eDIV.childNodes[0].style && eDIV.childNodes[0].style.display == 'none' )
99                {
100                        var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', eDIV.cloneNode(true) ) ;
101
102                        eDIV.parentNode.insertBefore( oFakeImage, eDIV ) ;
103                        eDIV.parentNode.removeChild( eDIV ) ;
104                }
105        }
106/*
107        var aCenters = document.getElementsByTagName( 'CENTER' ) ;
108
109        var oCenter ;
110        var i = aCenters.length - 1 ;
111        while ( i >= 0 && ( oCenter = aCenters[i--] ) )
112        {
113                if ( oCenter.style.pageBreakAfter == 'always' && oCenter.innerHTML.Trim().length == 0 )
114                {
115                        var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', oCenter.cloneNode(true) ) ;
116
117                        oCenter.parentNode.insertBefore( oFakeImage, oCenter ) ;
118                        oCenter.parentNode.removeChild( oCenter ) ;
119                }
120        }
121*/
122}
123
124// EMBED and OBJECT tags.
125var FCKEmbedAndObjectProcessor = (function()
126{
127        var customProcessors = [] ;
128
129        var processElement = function( el )
130        {
131                var clone = el.cloneNode( true ) ;
132                var replaceElement ;
133                var fakeImg = replaceElement = FCKDocumentProcessor_CreateFakeImage( 'FCK__UnknownObject', clone ) ;
134                FCKEmbedAndObjectProcessor.RefreshView( fakeImg, el ) ;
135
136                for ( var i = 0 ; i < customProcessors.length ; i++ )
137                        replaceElement = customProcessors[i]( el, replaceElement ) || replaceElement ;
138
139                if ( replaceElement != fakeImg )
140                        FCKTempBin.RemoveElement( fakeImg.getAttribute( '_fckrealelement' ) ) ;
141
142                el.parentNode.replaceChild( replaceElement, el ) ;
143        }
144
145        var processElementsByName = function( elementName, doc )
146        {
147                var aObjects = doc.getElementsByTagName( elementName );
148                for ( var i = aObjects.length - 1 ; i >= 0 ; i-- )
149                        processElement( aObjects[i] ) ;
150        }
151
152        var processObjectAndEmbed = function( doc )
153        {
154                processElementsByName( 'object', doc );
155                processElementsByName( 'embed', doc );
156        }
157
158        return FCKTools.Merge( FCKDocumentProcessor.AppendNew(),
159                       {
160                                ProcessDocument : function( doc )
161                                {
162                                        // Firefox 3 would sometimes throw an unknown exception while accessing EMBEDs and OBJECTs
163                                        // without the setTimeout().
164                                        if ( FCKBrowserInfo.IsGecko )
165                                                FCKTools.RunFunction( processObjectAndEmbed, this, [ doc ] ) ;
166                                        else
167                                                processObjectAndEmbed( doc ) ;
168                                },
169
170                                RefreshView : function( placeHolder, original )
171                                {
172                                        if ( original.getAttribute( 'width' ) > 0 )
173                                                placeHolder.style.width = FCKTools.ConvertHtmlSizeToStyle( original.getAttribute( 'width' ) ) ;
174
175                                        if ( original.getAttribute( 'height' ) > 0 )
176                                                placeHolder.style.height = FCKTools.ConvertHtmlSizeToStyle( original.getAttribute( 'height' ) ) ;
177                                },
178
179                                AddCustomHandler : function( func )
180                                {
181                                        customProcessors.push( func ) ;
182                                }
183                        } ) ;
184} )() ;
185
186FCK.GetRealElement = function( fakeElement )
187{
188        var e = FCKTempBin.Elements[ fakeElement.getAttribute('_fckrealelement') ] ;
189
190        if ( fakeElement.getAttribute('_fckflash') )
191        {
192                if ( fakeElement.style.width.length > 0 )
193                                e.width = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.width ) ;
194
195                if ( fakeElement.style.height.length > 0 )
196                                e.height = FCKTools.ConvertStyleSizeToHtml( fakeElement.style.height ) ;
197        }
198
199        return e ;
200}
201
202// HR Processor.
203// This is a IE only (tricky) thing. We protect all HR tags before loading them
204// (see FCK.ProtectTags). Here we put the HRs back.
205if ( FCKBrowserInfo.IsIE )
206{
207        FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
208        {
209                var aHRs = document.getElementsByTagName( 'HR' ) ;
210
211                var eHR ;
212                var i = aHRs.length - 1 ;
213                while ( i >= 0 && ( eHR = aHRs[i--] ) )
214                {
215                        // Create the replacement HR.
216                        var newHR = document.createElement( 'hr' ) ;
217                        newHR.mergeAttributes( eHR, true ) ;
218
219                        // We must insert the new one after it. insertBefore will not work in all cases.
220                        FCKDomTools.InsertAfterNode( eHR, newHR ) ;
221
222                        eHR.parentNode.removeChild( eHR ) ;
223                }
224        }
225}
226
227// INPUT:hidden Processor.
228FCKDocumentProcessor.AppendNew().ProcessDocument = function( document )
229{
230        var aInputs = document.getElementsByTagName( 'INPUT' ) ;
231
232        var oInput ;
233        var i = aInputs.length - 1 ;
234        while ( i >= 0 && ( oInput = aInputs[i--] ) )
235        {
236                if ( oInput.type == 'hidden' )
237                {
238                        var oImg = FCKDocumentProcessor_CreateFakeImage( 'FCK__InputHidden', oInput.cloneNode(true) ) ;
239                        oImg.setAttribute( '_fckinputhidden', 'true', 0 ) ;
240
241                        oInput.parentNode.insertBefore( oImg, oInput ) ;
242                        oInput.parentNode.removeChild( oInput ) ;
243                }
244        }
245}
246
247// Flash handler.
248FCKEmbedAndObjectProcessor.AddCustomHandler( function( el, fakeImg )
249        {
250                if ( ! ( el.nodeName.IEquals( 'embed' ) && ( el.type == 'application/x-shockwave-flash' || /\.swf($|#|\?)/i.test( el.src ) ) ) )
251                        return ;
252                fakeImg.className = 'FCK__Flash' ;
253                fakeImg.setAttribute( '_fckflash', 'true', 0 );
254        } ) ;
255
256// Buggy <span class="Apple-style-span"> tags added by Safari.
257if ( FCKBrowserInfo.IsSafari )
258{
259        FCKDocumentProcessor.AppendNew().ProcessDocument = function( doc )
260        {
261                var spans = doc.getElementsByClassName ?
262                        doc.getElementsByClassName( 'Apple-style-span' ) :
263                        Array.prototype.filter.call(
264                                        doc.getElementsByTagName( 'span' ),
265                                        function( item ){ return item.className == 'Apple-style-span' ; }
266                                        ) ;
267                for ( var i = spans.length - 1 ; i >= 0 ; i-- )
268                        FCKDomTools.RemoveNode( spans[i], true ) ;
269        }
270}
Note: See TracBrowser for help on using the repository browser.