2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * A lightweight representation of an HTML element.
\r
8 * @param {String} name The element name.
\r
9 * @param {Object} attributes And object holding all attributes defined for
\r
14 CKEDITOR.htmlParser.element = function( name, attributes )
\r
24 * Holds the attributes defined for this element.
\r
28 this.attributes = attributes;
\r
31 * The nodes that are direct children of this element.
\r
37 var dtd = CKEDITOR.dtd,
\r
38 isBlockLike = !!( dtd.$block[ name ] || dtd.$listItem[ name ] || dtd.$tableContent[ name ] || dtd.$nonEditable[ name ] || name == 'br' ),
\r
39 isEmpty = !!dtd.$empty[ name ];
\r
41 this.isEmpty = isEmpty;
\r
42 this.isUnknown = !dtd[ name ];
\r
47 isBlockLike : isBlockLike,
\r
48 hasInlineStarted : isEmpty || !isBlockLike
\r
54 // Used to sort attribute entries in an array, where the first element of
\r
55 // each object is the attribute name.
\r
56 var sortAttribs = function( a, b )
\r
60 return a < b ? -1 : a > b ? 1 : 0;
\r
63 CKEDITOR.htmlParser.element.prototype =
\r
66 * The node type. This is a constant value set to {@link CKEDITOR.NODE_ELEMENT}.
\r
70 type : CKEDITOR.NODE_ELEMENT,
\r
73 * Adds a node to the element children list.
\r
74 * @param {Object} node The node to be added. It can be any of of the
\r
75 * following types: {@link CKEDITOR.htmlParser.element},
\r
76 * {@link CKEDITOR.htmlParser.text} and
\r
77 * {@link CKEDITOR.htmlParser.comment}.
\r
81 add : CKEDITOR.htmlParser.fragment.prototype.add,
\r
84 * Clone this element.
\r
85 * @returns {CKEDITOR.htmlParser.element} The element clone.
\r
90 return new CKEDITOR.htmlParser.element( this.name, this.attributes );
\r
94 * Writes the element HTML to a CKEDITOR.htmlWriter.
\r
95 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
\r
98 writeHtml : function( writer, filter )
\r
100 var attributes = this.attributes;
\r
102 // The "_cke_replacedata" indicates that this element is replacing
\r
103 // a data snippet, which should be outputted as is.
\r
104 if ( attributes._cke_replacedata )
\r
106 writer.write( attributes._cke_replacedata );
\r
110 // Ignore cke: prefixes when writing HTML.
\r
111 var element = this,
\r
112 writeName = element.name,
\r
119 if ( !( writeName = filter.onElementName( writeName ) ) )
\r
122 element.name = writeName;
\r
124 if ( !( element = filter.onElement( element ) ) )
\r
127 if ( element.name == writeName )
\r
130 writeName = element.name;
\r
131 if ( !writeName ) // Send children.
\r
133 CKEDITOR.htmlParser.fragment.prototype.writeHtml.apply( element, arguments );
\r
138 // The element may have been changed, so update the local
\r
140 attributes = element.attributes;
\r
143 // Open element tag.
\r
144 writer.openTag( writeName, attributes );
\r
146 if ( writer.sortAttributes )
\r
148 // Copy all attributes to an array.
\r
149 var attribsArray = [];
\r
150 for ( a in attributes )
\r
152 value = attributes[ a ];
\r
154 if ( filter && ( !( a = filter.onAttributeName( a ) ) || ( value = filter.onAttribute( element, a, value ) ) === false ) )
\r
157 attribsArray.push( [ a, value ] );
\r
160 // Sort the attributes by name.
\r
161 attribsArray.sort( sortAttribs );
\r
163 // Send the attributes.
\r
164 for ( var i = 0, len = attribsArray.length ; i < len ; i++ )
\r
166 var attrib = attribsArray[ i ];
\r
167 writer.attribute( attrib[0], attrib[1] );
\r
172 for ( a in attributes )
\r
174 value = attributes[ a ];
\r
176 if ( filter && ( !( a = filter.onAttributeName( a ) ) || ( value = filter.onAttribute( element, a, value ) ) === false ) )
\r
179 writer.attribute( a, value );
\r
184 writer.openTagClose( writeName, element.isEmpty );
\r
186 if ( !element.isEmpty )
\r
189 CKEDITOR.htmlParser.fragment.prototype.writeHtml.apply( element, arguments );
\r
191 // Close the element.
\r
192 writer.closeTag( writeName );
\r