2 Copyright (c) 2003-2011, 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[ 'data-cke-real-element-type' ] || name || '';
\r
39 // Reveal the real semantic of our internal custom tag name (#6639).
\r
40 var internalTag = tagName.match( /^cke:(.*)/ );
\r
41 internalTag && ( tagName = internalTag[ 1 ] );
\r
43 var dtd = CKEDITOR.dtd,
\r
44 isBlockLike = !!( dtd.$nonBodyContent[ tagName ]
\r
45 || dtd.$block[ tagName ]
\r
46 || dtd.$listItem[ tagName ]
\r
47 || dtd.$tableContent[ tagName ]
\r
48 || dtd.$nonEditable[ tagName ]
\r
49 || tagName == 'br' ),
\r
50 isEmpty = !!dtd.$empty[ name ];
\r
52 this.isEmpty = isEmpty;
\r
53 this.isUnknown = !dtd[ name ];
\r
58 isBlockLike : isBlockLike,
\r
59 hasInlineStarted : isEmpty || !isBlockLike
\r
65 // Used to sort attribute entries in an array, where the first element of
\r
66 // each object is the attribute name.
\r
67 var sortAttribs = function( a, b )
\r
71 return a < b ? -1 : a > b ? 1 : 0;
\r
74 CKEDITOR.htmlParser.element.prototype =
\r
77 * The node type. This is a constant value set to {@link CKEDITOR.NODE_ELEMENT}.
\r
81 type : CKEDITOR.NODE_ELEMENT,
\r
84 * Adds a node to the element children list.
\r
85 * @param {Object} node The node to be added. It can be any of of the
\r
86 * following types: {@link CKEDITOR.htmlParser.element},
\r
87 * {@link CKEDITOR.htmlParser.text} and
\r
88 * {@link CKEDITOR.htmlParser.comment}.
\r
92 add : CKEDITOR.htmlParser.fragment.prototype.add,
\r
95 * Clone this element.
\r
96 * @returns {CKEDITOR.htmlParser.element} The element clone.
\r
101 return new CKEDITOR.htmlParser.element( this.name, this.attributes );
\r
105 * Writes the element HTML to a CKEDITOR.htmlWriter.
\r
106 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
\r
109 writeHtml : function( writer, filter )
\r
111 var attributes = this.attributes;
\r
113 // Ignore cke: prefixes when writing HTML.
\r
114 var element = this,
\r
115 writeName = element.name,
\r
116 a, newAttrName, value;
\r
118 var isChildrenFiltered;
\r
121 * Providing an option for bottom-up filtering order ( element
\r
122 * children to be pre-filtered before the element itself ).
\r
124 element.filterChildren = function()
\r
126 if ( !isChildrenFiltered )
\r
128 var writer = new CKEDITOR.htmlParser.basicWriter();
\r
129 CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.call( element, writer, filter );
\r
130 element.children = new CKEDITOR.htmlParser.fragment.fromHtml( writer.getHtml(), 0, element.clone() ).children;
\r
131 isChildrenFiltered = 1;
\r
139 if ( !( writeName = filter.onElementName( writeName ) ) )
\r
142 element.name = writeName;
\r
144 if ( !( element = filter.onElement( element ) ) )
\r
147 element.parent = this.parent;
\r
149 if ( element.name == writeName )
\r
152 // If the element has been replaced with something of a
\r
153 // different type, then make the replacement write itself.
\r
154 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
156 element.writeHtml( writer, filter );
\r
160 writeName = element.name;
\r
162 // This indicate that the element has been dropped by
\r
163 // filter but not the children.
\r
166 this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
\r
171 // The element may have been changed, so update the local
\r
173 attributes = element.attributes;
\r
176 // Open element tag.
\r
177 writer.openTag( writeName, attributes );
\r
179 // Copy all attributes to an array.
\r
180 var attribsArray = [];
\r
181 // Iterate over the attributes twice since filters may alter
\r
182 // other attributes.
\r
183 for ( var i = 0 ; i < 2; i++ )
\r
185 for ( a in attributes )
\r
188 value = attributes[ a ];
\r
190 attribsArray.push( [ a, value ] );
\r
195 if ( !( newAttrName = filter.onAttributeName( a ) ) )
\r
197 delete attributes[ a ];
\r
200 else if ( newAttrName != a )
\r
202 delete attributes[ a ];
\r
211 if ( ( value = filter.onAttribute( element, newAttrName, value ) ) === false )
\r
212 delete attributes[ newAttrName ];
\r
214 attributes [ newAttrName ] = value;
\r
219 // Sort the attributes by name.
\r
220 if ( writer.sortAttributes )
\r
221 attribsArray.sort( sortAttribs );
\r
223 // Send the attributes.
\r
224 var len = attribsArray.length;
\r
225 for ( i = 0 ; i < len ; i++ )
\r
227 var attrib = attribsArray[ i ];
\r
228 writer.attribute( attrib[0], attrib[1] );
\r
232 writer.openTagClose( writeName, element.isEmpty );
\r
234 if ( !element.isEmpty )
\r
236 this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
\r
237 // Close the element.
\r
238 writer.closeTag( writeName );
\r
242 writeChildrenHtml : function( writer, filter )
\r
245 CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.apply( this, arguments );
\r