2 Copyright (c) 2003-2010, 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 || ( attributes = {} );
\r
31 * The nodes that are direct children of this element.
\r
37 var tagName = attributes._cke_real_element_type || name;
\r
39 var dtd = CKEDITOR.dtd,
\r
40 isBlockLike = !!( dtd.$nonBodyContent[ tagName ] || dtd.$block[ tagName ] || dtd.$listItem[ tagName ] || dtd.$tableContent[ tagName ] || dtd.$nonEditable[ tagName ] || tagName == 'br' ),
\r
41 isEmpty = !!dtd.$empty[ name ];
\r
43 this.isEmpty = isEmpty;
\r
44 this.isUnknown = !dtd[ name ];
\r
49 isBlockLike : isBlockLike,
\r
50 hasInlineStarted : isEmpty || !isBlockLike
\r
56 // Used to sort attribute entries in an array, where the first element of
\r
57 // each object is the attribute name.
\r
58 var sortAttribs = function( a, b )
\r
62 return a < b ? -1 : a > b ? 1 : 0;
\r
65 CKEDITOR.htmlParser.element.prototype =
\r
68 * The node type. This is a constant value set to {@link CKEDITOR.NODE_ELEMENT}.
\r
72 type : CKEDITOR.NODE_ELEMENT,
\r
75 * Adds a node to the element children list.
\r
76 * @param {Object} node The node to be added. It can be any of of the
\r
77 * following types: {@link CKEDITOR.htmlParser.element},
\r
78 * {@link CKEDITOR.htmlParser.text} and
\r
79 * {@link CKEDITOR.htmlParser.comment}.
\r
83 add : CKEDITOR.htmlParser.fragment.prototype.add,
\r
86 * Clone this element.
\r
87 * @returns {CKEDITOR.htmlParser.element} The element clone.
\r
92 return new CKEDITOR.htmlParser.element( this.name, this.attributes );
\r
96 * Writes the element HTML to a CKEDITOR.htmlWriter.
\r
97 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
\r
100 writeHtml : function( writer, filter )
\r
102 var attributes = this.attributes;
\r
104 // Ignore cke: prefixes when writing HTML.
\r
105 var element = this,
\r
106 writeName = element.name,
\r
107 a, newAttrName, value;
\r
109 var isChildrenFiltered;
\r
112 * Providing an option for bottom-up filtering order ( element
\r
113 * children to be pre-filtered before the element itself ).
\r
115 element.filterChildren = function()
\r
117 if ( !isChildrenFiltered )
\r
119 var writer = new CKEDITOR.htmlParser.basicWriter();
\r
120 CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.call( element, writer, filter );
\r
121 element.children = new CKEDITOR.htmlParser.fragment.fromHtml( writer.getHtml() ).children;
\r
122 isChildrenFiltered = 1;
\r
130 if ( !( writeName = filter.onElementName( writeName ) ) )
\r
133 element.name = writeName;
\r
135 if ( !( element = filter.onElement( element ) ) )
\r
138 element.parent = this.parent;
\r
140 if ( element.name == writeName )
\r
143 // If the element has been replaced with something of a
\r
144 // different type, then make the replacement write itself.
\r
145 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
147 element.writeHtml( writer, filter );
\r
151 writeName = element.name;
\r
153 // This indicate that the element has been dropped by
\r
154 // filter but not the children.
\r
157 this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
\r
162 // The element may have been changed, so update the local
\r
164 attributes = element.attributes;
\r
167 // Open element tag.
\r
168 writer.openTag( writeName, attributes );
\r
170 // Copy all attributes to an array.
\r
171 var attribsArray = [];
\r
172 // Iterate over the attributes twice since filters may alter
\r
173 // other attributes.
\r
174 for ( var i = 0 ; i < 2; i++ )
\r
176 for ( a in attributes )
\r
179 value = attributes[ a ];
\r
181 attribsArray.push( [ a, value ] );
\r
186 if ( !( newAttrName = filter.onAttributeName( a ) ) )
\r
188 delete attributes[ a ];
\r
191 else if ( newAttrName != a )
\r
193 delete attributes[ a ];
\r
202 if ( ( value = filter.onAttribute( element, newAttrName, value ) ) === false )
\r
203 delete attributes[ newAttrName ];
\r
205 attributes [ newAttrName ] = value;
\r
210 // Sort the attributes by name.
\r
211 if ( writer.sortAttributes )
\r
212 attribsArray.sort( sortAttribs );
\r
214 // Send the attributes.
\r
215 var len = attribsArray.length;
\r
216 for ( i = 0 ; i < len ; i++ )
\r
218 var attrib = attribsArray[ i ];
\r
219 writer.attribute( attrib[0], attrib[1] );
\r
223 writer.openTagClose( writeName, element.isEmpty );
\r
225 if ( !element.isEmpty )
\r
227 this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
\r
228 // Close the element.
\r
229 writer.closeTag( writeName );
\r
233 writeChildrenHtml : function( writer, filter )
\r
236 CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.apply( this, arguments );
\r