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 * Defines the FCKXHtml object, responsible for the XHTML operations.
22 * IE specific.
23 */
24
25FCKXHtml._GetMainXmlString = function()
26{
27	return this.MainNode.xml ;
28}
29
30FCKXHtml._AppendAttributes = function( xmlNode, htmlNode, node, nodeName )
31{
32	var aAttributes = htmlNode.attributes ;
33
34	for ( var n = 0 ; n < aAttributes.length ; n++ )
35	{
36		var oAttribute = aAttributes[n] ;
37
38		if ( oAttribute.specified )
39		{
40			var sAttName = oAttribute.nodeName.toLowerCase() ;
41			var sAttValue ;
42
43			// Ignore any attribute starting with "_fck".
44			if ( sAttName.StartsWith( '_fck' ) )
45				continue ;
46			// The following must be done because of a bug on IE regarding the style
47			// attribute. It returns "null" for the nodeValue.
48			else if ( sAttName == 'style' )
49			{
50				var data = FCKTools.ProtectFormStyles( htmlNode ) ;
51				sAttValue = htmlNode.style.cssText.replace( FCKRegexLib.StyleProperties, FCKTools.ToLowerCase ) ;
52				FCKTools.RestoreFormStyles( htmlNode, data ) ;
53			}
54			// There are two cases when the oAttribute.nodeValue must be used:
55			//		- for the "class" attribute
56			//		- for events attributes (on IE only).
57			else if ( sAttName == 'class' )
58			{
59				sAttValue = oAttribute.nodeValue.replace( FCKRegexLib.FCK_Class, '' ) ;
60				if ( sAttValue.length == 0 )
61					continue ;
62			}
63			else if ( sAttName.indexOf('on') == 0 )
64				sAttValue = oAttribute.nodeValue ;
65			else if ( nodeName == 'body' && sAttName == 'contenteditable' )
66				continue ;
67			// XHTML doens't support attribute minimization like "CHECKED". It must be transformed to checked="checked".
68			else if ( oAttribute.nodeValue === true )
69				sAttValue = sAttName ;
70			else
71			{
72				// We must use getAttribute to get it exactly as it is defined.
73				// There are some rare cases that IE throws an error here, so we must try/catch.
74				try
75				{
76					sAttValue = htmlNode.getAttribute( sAttName, 2 ) ;
77				}
78				catch (e) {}
79			}
80			this._AppendAttribute( node, sAttName, sAttValue || oAttribute.nodeValue ) ;
81		}
82	}
83}
84
85FCKXHtml.TagProcessors['meta'] = function( node, htmlNode )
86{
87	var oHttpEquiv = node.attributes.getNamedItem( 'http-equiv' ) ;
88
89	if ( oHttpEquiv == null || oHttpEquiv.value.length == 0 )
90	{
91		// Get the http-equiv value from the outerHTML.
92		var sHttpEquiv = htmlNode.outerHTML.match( FCKRegexLib.MetaHttpEquiv ) ;
93
94		if ( sHttpEquiv )
95		{
96			sHttpEquiv = sHttpEquiv[1] ;
97			FCKXHtml._AppendAttribute( node, 'http-equiv', sHttpEquiv ) ;
98		}
99	}
100
101	return node ;
102}
103
104// IE automatically changes <FONT> tags to <FONT size=+0>.
105FCKXHtml.TagProcessors['font'] = function( node, htmlNode )
106{
107	if ( node.attributes.length == 0 )
108		node = FCKXHtml.XML.createDocumentFragment() ;
109
110	node = FCKXHtml._AppendChildNodes( node, htmlNode ) ;
111
112	return node ;
113}
114
115// IE doens't see the value attribute as an attribute for the <INPUT> tag.
116FCKXHtml.TagProcessors['input'] = function( node, htmlNode )
117{
118	if ( htmlNode.name )
119		FCKXHtml._AppendAttribute( node, 'name', htmlNode.name ) ;
120
121	if ( htmlNode.value && !node.attributes.getNamedItem( 'value' ) )
122		FCKXHtml._AppendAttribute( node, 'value', htmlNode.value ) ;
123
124	if ( !node.attributes.getNamedItem( 'type' ) )
125		FCKXHtml._AppendAttribute( node, 'type', 'text' ) ;
126
127	return node ;
128}
129
130// IE ignores the "SELECTED" attribute so we must add it manually.
131FCKXHtml.TagProcessors['option'] = function( node, htmlNode )
132{
133	if ( htmlNode.selected && !node.attributes.getNamedItem( 'selected' ) )
134		FCKXHtml._AppendAttribute( node, 'selected', 'selected' ) ;
135
136	node = FCKXHtml._AppendChildNodes( node, htmlNode ) ;
137
138	return node ;
139}
140
141// IE ignores the "COORDS" and "SHAPE" attribute so we must add it manually.
142FCKXHtml.TagProcessors['area'] = function( node, htmlNode )
143{
144	if ( ! node.attributes.getNamedItem( 'coords' ) )
145	{
146		var sCoords = htmlNode.getAttribute( 'coords', 2 ) ;
147		if ( sCoords && sCoords != '0,0,0' )
148			FCKXHtml._AppendAttribute( node, 'coords', sCoords ) ;
149	}
150
151	if ( ! node.attributes.getNamedItem( 'shape' ) )
152	{
153		var sShape = htmlNode.getAttribute( 'shape', 2 ) ;
154		if ( sShape && sShape.length > 0 )
155			FCKXHtml._AppendAttribute( node, 'shape', sShape.toLowerCase() ) ;
156	}
157
158	return node ;
159}
160
161FCKXHtml.TagProcessors['label'] = function( node, htmlNode )
162{
163	if ( htmlNode.htmlFor.length > 0 )
164		FCKXHtml._AppendAttribute( node, 'for', htmlNode.htmlFor ) ;
165
166	node = FCKXHtml._AppendChildNodes( node, htmlNode ) ;
167
168	return node ;
169}
170
171FCKXHtml.TagProcessors['form'] = function( node, htmlNode )
172{
173	if ( htmlNode.acceptCharset && htmlNode.acceptCharset.length > 0 && htmlNode.acceptCharset != 'UNKNOWN' )
174		FCKXHtml._AppendAttribute( node, 'accept-charset', htmlNode.acceptCharset ) ;
175
176	// IE has a bug and htmlNode.attributes['name'].specified=false if there is
177	// no element with id="name" inside the form (#360 and SF-BUG-1155726).
178	var nameAtt = htmlNode.attributes['name'] ;
179
180	if ( nameAtt && nameAtt.value.length > 0 )
181		FCKXHtml._AppendAttribute( node, 'name', nameAtt.value ) ;
182
183	node = FCKXHtml._AppendChildNodes( node, htmlNode, true ) ;
184
185	return node ;
186}
187
188// IE doens't hold the name attribute as an attribute for the <TEXTAREA> and <SELECT> tags.
189FCKXHtml.TagProcessors['textarea'] = FCKXHtml.TagProcessors['select'] = function( node, htmlNode )
190{
191	if ( htmlNode.name )
192		FCKXHtml._AppendAttribute( node, 'name', htmlNode.name ) ;
193
194	node = FCKXHtml._AppendChildNodes( node, htmlNode ) ;
195
196	return node ;
197}
198
199// On very rare cases, IE is loosing the "align" attribute for DIV. (right align and apply bulleted list)
200FCKXHtml.TagProcessors['div'] = function( node, htmlNode )
201{
202	if ( htmlNode.align.length > 0 )
203		FCKXHtml._AppendAttribute( node, 'align', htmlNode.align ) ;
204
205	node = FCKXHtml._AppendChildNodes( node, htmlNode, true ) ;
206
207	return node ;
208}
209