2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
\r
3 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
\r
5 * == BEGIN LICENSE ==
\r
7 * Licensed under the terms of any of the following licenses at your
\r
10 * - GNU General Public License Version 2 or later (the "GPL")
\r
11 * http://www.gnu.org/licenses/gpl.html
\r
13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
\r
14 * http://www.gnu.org/licenses/lgpl.html
\r
16 * - Mozilla Public License Version 1.1 or later (the "MPL")
\r
17 * http://www.mozilla.org/MPL/MPL-1.1.html
\r
21 * Defines the FCKXHtml object, responsible for the XHTML operations.
\r
24 var FCKXHtml = new Object() ;
\r
26 FCKXHtml.CurrentJobNum = 0 ;
\r
28 FCKXHtml.GetXHTML = function( node, includeNode, format )
\r
30 FCKDomTools.CheckAndRemovePaddingNode( FCKTools.GetElementDocument( node ), FCKConfig.EnterMode ) ;
\r
31 FCKXHtmlEntities.Initialize() ;
\r
33 // Set the correct entity to use for empty blocks.
\r
34 this._NbspEntity = ( FCKConfig.ProcessHTMLEntities? 'nbsp' : '#160' ) ;
\r
36 // Save the current IsDirty state. The XHTML processor may change the
\r
37 // original HTML, dirtying it.
\r
38 var bIsDirty = FCK.IsDirty() ;
\r
40 // Special blocks are blocks of content that remain untouched during the
\r
41 // process. It is used for SCRIPTs and STYLEs.
\r
42 FCKXHtml.SpecialBlocks = new Array() ;
\r
44 // Create the XML DOMDocument object.
\r
45 this.XML = FCKTools.CreateXmlObject( 'DOMDocument' ) ;
\r
47 // Add a root element that holds all child nodes.
\r
48 this.MainNode = this.XML.appendChild( this.XML.createElement( 'xhtml' ) ) ;
\r
50 FCKXHtml.CurrentJobNum++ ;
\r
52 // var dTimer = new Date() ;
\r
55 this._AppendNode( this.MainNode, node ) ;
\r
57 this._AppendChildNodes( this.MainNode, node, false ) ;
\r
59 // Get the resulting XHTML as a string.
\r
60 var sXHTML = this._GetMainXmlString() ;
\r
62 // alert( 'Time: ' + ( ( ( new Date() ) - dTimer ) ) + ' ms' ) ;
\r
66 // Safari adds xmlns="http://www.w3.org/1999/xhtml" to the root node (#963)
\r
67 if ( FCKBrowserInfo.IsSafari )
\r
68 sXHTML = sXHTML.replace( /^<xhtml.*?>/, '<xhtml>' ) ;
\r
70 // Strip the "XHTML" root node.
\r
71 sXHTML = sXHTML.substr( 7, sXHTML.length - 15 ).Trim() ;
\r
73 // According to the doctype set the proper end for self-closing tags
\r
75 // XHTML: Add a space, like <br/> -> <br />
\r
76 if (FCKConfig.DocType.length > 0 && FCKRegexLib.HtmlDocType.test( FCKConfig.DocType ) )
\r
77 sXHTML = sXHTML.replace( FCKRegexLib.SpaceNoClose, '>');
\r
79 sXHTML = sXHTML.replace( FCKRegexLib.SpaceNoClose, ' />');
\r
81 if ( FCKConfig.ForceSimpleAmpersand )
\r
82 sXHTML = sXHTML.replace( FCKRegexLib.ForceSimpleAmpersand, '&' ) ;
\r
85 sXHTML = FCKCodeFormatter.Format( sXHTML ) ;
\r
87 // Now we put back the SpecialBlocks contents.
\r
88 for ( var i = 0 ; i < FCKXHtml.SpecialBlocks.length ; i++ )
\r
90 var oRegex = new RegExp( '___FCKsi___' + i ) ;
\r
91 sXHTML = sXHTML.replace( oRegex, FCKXHtml.SpecialBlocks[i] ) ;
\r
94 // Replace entities marker with the ampersand.
\r
95 sXHTML = sXHTML.replace( FCKRegexLib.GeckoEntitiesMarker, '&' ) ;
\r
97 // Restore the IsDirty state if it was not dirty.
\r
99 FCK.ResetIsDirty() ;
\r
101 FCKDomTools.EnforcePaddingNode( FCKTools.GetElementDocument( node ), FCKConfig.EnterMode ) ;
\r
105 FCKXHtml._AppendAttribute = function( xmlNode, attributeName, attributeValue )
\r
109 if ( attributeValue == undefined || attributeValue == null )
\r
110 attributeValue = '' ;
\r
111 else if ( attributeValue.replace )
\r
113 if ( FCKConfig.ForceSimpleAmpersand )
\r
114 attributeValue = attributeValue.replace( /&/g, '___FCKAmp___' ) ;
\r
116 // Entities must be replaced in the attribute values.
\r
117 attributeValue = attributeValue.replace( FCKXHtmlEntities.EntitiesRegex, FCKXHtml_GetEntity ) ;
\r
120 // Create the attribute.
\r
121 var oXmlAtt = this.XML.createAttribute( attributeName ) ;
\r
122 oXmlAtt.value = attributeValue ;
\r
124 // Set the attribute in the node.
\r
125 xmlNode.attributes.setNamedItem( oXmlAtt ) ;
\r
131 FCKXHtml._AppendChildNodes = function( xmlNode, htmlNode, isBlockElement )
\r
133 var oNode = htmlNode.firstChild ;
\r
137 this._AppendNode( xmlNode, oNode ) ;
\r
138 oNode = oNode.nextSibling ;
\r
141 // Trim block elements. This is also needed to avoid Firefox leaving extra
\r
142 // BRs at the end of them.
\r
143 if ( isBlockElement && htmlNode.tagName && htmlNode.tagName.toLowerCase() != 'pre' )
\r
145 FCKDomTools.TrimNode( xmlNode ) ;
\r
147 if ( FCKConfig.FillEmptyBlocks )
\r
149 var lastChild = xmlNode.lastChild ;
\r
150 if ( lastChild && lastChild.nodeType == 1 && lastChild.nodeName == 'br' )
\r
151 this._AppendEntity( xmlNode, this._NbspEntity ) ;
\r
155 // If the resulting node is empty.
\r
156 if ( xmlNode.childNodes.length == 0 )
\r
158 if ( isBlockElement && FCKConfig.FillEmptyBlocks )
\r
160 this._AppendEntity( xmlNode, this._NbspEntity ) ;
\r
164 var sNodeName = xmlNode.nodeName ;
\r
166 // Some inline elements are required to have something inside (span, strong, etc...).
\r
167 if ( FCKListsLib.InlineChildReqElements[ sNodeName ] )
\r
170 // We can't use short representation of empty elements that are not marked
\r
171 // as empty in th XHTML DTD.
\r
172 if ( !FCKListsLib.EmptyElements[ sNodeName ] )
\r
173 xmlNode.appendChild( this.XML.createTextNode('') ) ;
\r
179 FCKXHtml._AppendNode = function( xmlNode, htmlNode )
\r
184 switch ( htmlNode.nodeType )
\r
188 // If we detect a <br> inside a <pre> in Gecko, turn it into a line break instead.
\r
189 // This is a workaround for the Gecko bug here: https://bugzilla.mozilla.org/show_bug.cgi?id=92921
\r
190 if ( FCKBrowserInfo.IsGecko
\r
191 && htmlNode.tagName.toLowerCase() == 'br'
\r
192 && htmlNode.parentNode.tagName.toLowerCase() == 'pre' )
\r
195 if ( htmlNode == htmlNode.parentNode.firstChild )
\r
197 return FCKXHtml._AppendNode( xmlNode, this.XML.createTextNode( val ) ) ;
\r
200 // Here we found an element that is not the real element, but a
\r
201 // fake one (like the Flash placeholder image), so we must get the real one.
\r
202 if ( htmlNode.getAttribute('_fckfakelement') )
\r
203 return FCKXHtml._AppendNode( xmlNode, FCK.GetRealElement( htmlNode ) ) ;
\r
205 // Ignore bogus BR nodes in the DOM.
\r
206 if ( FCKBrowserInfo.IsGecko &&
\r
207 ( htmlNode.hasAttribute('_moz_editor_bogus_node') || htmlNode.getAttribute( 'type' ) == '_moz' ) )
\r
209 if ( htmlNode.nextSibling )
\r
213 htmlNode.removeAttribute( '_moz_editor_bogus_node' ) ;
\r
214 htmlNode.removeAttribute( 'type' ) ;
\r
218 // This is for elements that are instrumental to FCKeditor and
\r
219 // must be removed from the final HTML.
\r
220 if ( htmlNode.getAttribute('_fcktemp') )
\r
223 // Get the element name.
\r
224 var sNodeName = htmlNode.tagName.toLowerCase() ;
\r
226 if ( FCKBrowserInfo.IsIE )
\r
228 // IE doens't include the scope name in the nodeName. So, add the namespace.
\r
229 if ( htmlNode.scopeName && htmlNode.scopeName != 'HTML' && htmlNode.scopeName != 'FCK' )
\r
230 sNodeName = htmlNode.scopeName.toLowerCase() + ':' + sNodeName ;
\r
234 if ( sNodeName.StartsWith( 'fck:' ) )
\r
235 sNodeName = sNodeName.Remove( 0,4 ) ;
\r
238 // Check if the node name is valid, otherwise ignore this tag.
\r
239 // If the nodeName starts with a slash, it is a orphan closing tag.
\r
240 // On some strange cases, the nodeName is empty, even if the node exists.
\r
241 if ( !FCKRegexLib.ElementName.test( sNodeName ) )
\r
244 // The already processed nodes must be marked to avoid then to be duplicated (bad formatted HTML).
\r
245 // So here, the "mark" is checked... if the element is Ok, then mark it.
\r
246 if ( htmlNode._fckxhtmljob && htmlNode._fckxhtmljob == FCKXHtml.CurrentJobNum )
\r
249 var oNode = this.XML.createElement( sNodeName ) ;
\r
251 // Add all attributes.
\r
252 FCKXHtml._AppendAttributes( xmlNode, htmlNode, oNode, sNodeName ) ;
\r
254 htmlNode._fckxhtmljob = FCKXHtml.CurrentJobNum ;
\r
256 // Tag specific processing.
\r
257 var oTagProcessor = FCKXHtml.TagProcessors[ sNodeName ] ;
\r
259 if ( oTagProcessor )
\r
260 oNode = oTagProcessor( oNode, htmlNode, xmlNode ) ;
\r
262 oNode = this._AppendChildNodes( oNode, htmlNode, Boolean( FCKListsLib.NonEmptyBlockElements[ sNodeName ] ) ) ;
\r
267 xmlNode.appendChild( oNode ) ;
\r
273 if ( htmlNode.parentNode && htmlNode.parentNode.nodeName.IEquals( 'pre' ) )
\r
274 return this._AppendTextNode( xmlNode, htmlNode.nodeValue ) ;
\r
275 return this._AppendTextNode( xmlNode, htmlNode.nodeValue.ReplaceNewLineChars(' ') ) ;
\r
279 // IE catches the <!DOTYPE ... > as a comment, but it has no
\r
280 // innerHTML, so we can catch it, and ignore it.
\r
281 if ( FCKBrowserInfo.IsIE && !htmlNode.innerHTML )
\r
284 try { xmlNode.appendChild( this.XML.createComment( htmlNode.nodeValue ) ) ; }
\r
285 catch (e) { /* Do nothing... probably this is a wrong format comment. */ }
\r
288 // Unknown Node type.
\r
290 xmlNode.appendChild( this.XML.createComment( "Element not supported - Type: " + htmlNode.nodeType + " Name: " + htmlNode.nodeName ) ) ;
\r
296 // Append an item to the SpecialBlocks array and returns the tag to be used.
\r
297 FCKXHtml._AppendSpecialItem = function( item )
\r
299 return '___FCKsi___' + ( FCKXHtml.SpecialBlocks.push( item ) - 1 ) ;
\r
302 FCKXHtml._AppendEntity = function( xmlNode, entity )
\r
304 xmlNode.appendChild( this.XML.createTextNode( '#?-:' + entity + ';' ) ) ;
\r
307 FCKXHtml._AppendTextNode = function( targetNode, textValue )
\r
309 var bHadText = textValue.length > 0 ;
\r
311 targetNode.appendChild( this.XML.createTextNode( textValue.replace( FCKXHtmlEntities.EntitiesRegex, FCKXHtml_GetEntity ) ) ) ;
\r
315 // Retrieves a entity (internal format) for a given character.
\r
316 function FCKXHtml_GetEntity( character )
\r
318 // We cannot simply place the entities in the text, because the XML parser
\r
319 // will translate & to &. So we use a temporary marker which is replaced
\r
320 // in the end of the processing.
\r
321 var sEntity = FCKXHtmlEntities.Entities[ character ] || ( '#' + character.charCodeAt(0) ) ;
\r
322 return '#?-:' + sEntity + ';' ;
\r
325 // An object that hold tag specific operations.
\r
326 FCKXHtml.TagProcessors =
\r
328 a : function( node, htmlNode )
\r
330 // Firefox may create empty tags when deleting the selection in some special cases (SF-BUG 1556878).
\r
331 if ( htmlNode.innerHTML.Trim().length == 0 && !htmlNode.name )
\r
334 var sSavedUrl = htmlNode.getAttribute( '_fcksavedurl' ) ;
\r
335 if ( sSavedUrl != null )
\r
336 FCKXHtml._AppendAttribute( node, 'href', sSavedUrl ) ;
\r
339 // Anchors with content has been marked with an additional class, now we must remove it.
\r
340 if ( FCKBrowserInfo.IsIE )
\r
342 // Buggy IE, doesn't copy the name of changed anchors.
\r
343 if ( htmlNode.name )
\r
344 FCKXHtml._AppendAttribute( node, 'name', htmlNode.name ) ;
\r
347 node = FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;
\r
352 area : function( node, htmlNode )
\r
354 var sSavedUrl = htmlNode.getAttribute( '_fcksavedurl' ) ;
\r
355 if ( sSavedUrl != null )
\r
356 FCKXHtml._AppendAttribute( node, 'href', sSavedUrl ) ;
\r
358 // IE ignores the "COORDS" and "SHAPE" attribute so we must add it manually.
\r
359 if ( FCKBrowserInfo.IsIE )
\r
361 if ( ! node.attributes.getNamedItem( 'coords' ) )
\r
363 var sCoords = htmlNode.getAttribute( 'coords', 2 ) ;
\r
364 if ( sCoords && sCoords != '0,0,0' )
\r
365 FCKXHtml._AppendAttribute( node, 'coords', sCoords ) ;
\r
368 if ( ! node.attributes.getNamedItem( 'shape' ) )
\r
370 var sShape = htmlNode.getAttribute( 'shape', 2 ) ;
\r
371 if ( sShape && sShape.length > 0 )
\r
372 FCKXHtml._AppendAttribute( node, 'shape', sShape.toLowerCase() ) ;
\r
379 body : function( node, htmlNode )
\r
381 node = FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;
\r
382 // Remove spellchecker attributes added for Firefox when converting to HTML code (Bug #1351).
\r
383 node.removeAttribute( 'spellcheck' ) ;
\r
387 // IE loses contents of iframes, and Gecko does give it back HtmlEncoded
\r
388 // Note: Opera does lose the content and doesn't provide it in the innerHTML string
\r
389 iframe : function( node, htmlNode )
\r
391 var sHtml = htmlNode.innerHTML ;
\r
393 // Gecko does give back the encoded html
\r
394 if ( FCKBrowserInfo.IsGecko )
\r
395 sHtml = FCKTools.HTMLDecode( sHtml );
\r
397 // Remove the saved urls here as the data won't be processed as nodes
\r
398 sHtml = sHtml.replace( /\s_fcksavedurl="[^"]*"/g, '' ) ;
\r
400 node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( sHtml ) ) ) ;
\r
405 img : function( node, htmlNode )
\r
407 // The "ALT" attribute is required in XHTML.
\r
408 if ( ! node.attributes.getNamedItem( 'alt' ) )
\r
409 FCKXHtml._AppendAttribute( node, 'alt', '' ) ;
\r
411 var sSavedUrl = htmlNode.getAttribute( '_fcksavedurl' ) ;
\r
412 if ( sSavedUrl != null )
\r
413 FCKXHtml._AppendAttribute( node, 'src', sSavedUrl ) ;
\r
415 // Bug #768 : If the width and height are defined inline CSS,
\r
416 // don't define it again in the HTML attributes.
\r
417 if ( htmlNode.style.width )
\r
418 node.removeAttribute( 'width' ) ;
\r
419 if ( htmlNode.style.height )
\r
420 node.removeAttribute( 'height' ) ;
\r
425 // Fix orphaned <li> nodes (Bug #503).
\r
426 li : function( node, htmlNode, targetNode )
\r
428 // If the XML parent node is already a <ul> or <ol>, then add the <li> as usual.
\r
429 if ( targetNode.nodeName.IEquals( ['ul', 'ol'] ) )
\r
430 return FCKXHtml._AppendChildNodes( node, htmlNode, true ) ;
\r
432 var newTarget = FCKXHtml.XML.createElement( 'ul' ) ;
\r
434 // Reset the _fckxhtmljob so the HTML node is processed again.
\r
435 htmlNode._fckxhtmljob = null ;
\r
437 // Loop through all sibling LIs, adding them to the <ul>.
\r
440 FCKXHtml._AppendNode( newTarget, htmlNode ) ;
\r
442 // Look for the next element following this <li>.
\r
445 htmlNode = FCKDomTools.GetNextSibling( htmlNode ) ;
\r
447 } while ( htmlNode && htmlNode.nodeType == 3 && htmlNode.nodeValue.Trim().length == 0 )
\r
449 } while ( htmlNode && htmlNode.nodeName.toLowerCase() == 'li' )
\r
454 // Fix nested <ul> and <ol>.
\r
455 ol : function( node, htmlNode, targetNode )
\r
457 if ( htmlNode.innerHTML.Trim().length == 0 )
\r
460 var ePSibling = targetNode.lastChild ;
\r
462 if ( ePSibling && ePSibling.nodeType == 3 )
\r
463 ePSibling = ePSibling.previousSibling ;
\r
465 if ( ePSibling && ePSibling.nodeName.toUpperCase() == 'LI' )
\r
467 htmlNode._fckxhtmljob = null ;
\r
468 FCKXHtml._AppendNode( ePSibling, htmlNode ) ;
\r
472 node = FCKXHtml._AppendChildNodes( node, htmlNode ) ;
\r
477 pre : function ( node, htmlNode )
\r
479 var firstChild = htmlNode.firstChild ;
\r
481 if ( firstChild && firstChild.nodeType == 3 )
\r
482 node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( '\r\n' ) ) ) ;
\r
484 FCKXHtml._AppendChildNodes( node, htmlNode, true ) ;
\r
489 script : function( node, htmlNode )
\r
491 // The "TYPE" attribute is required in XHTML.
\r
492 if ( ! node.attributes.getNamedItem( 'type' ) )
\r
493 FCKXHtml._AppendAttribute( node, 'type', 'text/javascript' ) ;
\r
495 node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( htmlNode.text ) ) ) ;
\r
500 span : function( node, htmlNode )
\r
502 // Firefox may create empty tags when deleting the selection in some special cases (SF-BUG 1084404).
\r
503 if ( htmlNode.innerHTML.length == 0 )
\r
506 node = FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;
\r
511 style : function( node, htmlNode )
\r
513 // The "TYPE" attribute is required in XHTML.
\r
514 if ( ! node.attributes.getNamedItem( 'type' ) )
\r
515 FCKXHtml._AppendAttribute( node, 'type', 'text/css' ) ;
\r
517 var cssText = htmlNode.innerHTML ;
\r
518 if ( FCKBrowserInfo.IsIE ) // Bug #403 : IE always appends a \r\n to the beginning of StyleNode.innerHTML
\r
519 cssText = cssText.replace( /^(\r\n|\n|\r)/, '' ) ;
\r
521 node.appendChild( FCKXHtml.XML.createTextNode( FCKXHtml._AppendSpecialItem( cssText ) ) ) ;
\r
526 title : function( node, htmlNode )
\r
528 node.appendChild( FCKXHtml.XML.createTextNode( FCK.EditorDocument.title ) ) ;
\r
534 FCKXHtml.TagProcessors.ul = FCKXHtml.TagProcessors.ol ;
\r