1 | /*
|
---|
2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
|
---|
3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
4 | */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * A lightweight representation of an HTML element.
|
---|
8 | * @param {String} name The element name.
|
---|
9 | * @param {Object} attributes And object holding all attributes defined for
|
---|
10 | * this element.
|
---|
11 | * @constructor
|
---|
12 | * @example
|
---|
13 | */
|
---|
14 | CKEDITOR.htmlParser.element = function( name, attributes )
|
---|
15 | {
|
---|
16 | /**
|
---|
17 | * The element name.
|
---|
18 | * @type String
|
---|
19 | * @example
|
---|
20 | */
|
---|
21 | this.name = name;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Holds the attributes defined for this element.
|
---|
25 | * @type Object
|
---|
26 | * @example
|
---|
27 | */
|
---|
28 | this.attributes = attributes || ( attributes = {} );
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The nodes that are direct children of this element.
|
---|
32 | * @type Array
|
---|
33 | * @example
|
---|
34 | */
|
---|
35 | this.children = [];
|
---|
36 |
|
---|
37 | var tagName = attributes._cke_real_element_type || name;
|
---|
38 |
|
---|
39 | var dtd = CKEDITOR.dtd,
|
---|
40 | isBlockLike = !!( dtd.$nonBodyContent[ tagName ] || dtd.$block[ tagName ] || dtd.$listItem[ tagName ] || dtd.$tableContent[ tagName ] || dtd.$nonEditable[ tagName ] || tagName == 'br' ),
|
---|
41 | isEmpty = !!dtd.$empty[ name ];
|
---|
42 |
|
---|
43 | this.isEmpty = isEmpty;
|
---|
44 | this.isUnknown = !dtd[ name ];
|
---|
45 |
|
---|
46 | /** @private */
|
---|
47 | this._ =
|
---|
48 | {
|
---|
49 | isBlockLike : isBlockLike,
|
---|
50 | hasInlineStarted : isEmpty || !isBlockLike
|
---|
51 | };
|
---|
52 | };
|
---|
53 |
|
---|
54 | (function()
|
---|
55 | {
|
---|
56 | // Used to sort attribute entries in an array, where the first element of
|
---|
57 | // each object is the attribute name.
|
---|
58 | var sortAttribs = function( a, b )
|
---|
59 | {
|
---|
60 | a = a[0];
|
---|
61 | b = b[0];
|
---|
62 | return a < b ? -1 : a > b ? 1 : 0;
|
---|
63 | };
|
---|
64 |
|
---|
65 | CKEDITOR.htmlParser.element.prototype =
|
---|
66 | {
|
---|
67 | /**
|
---|
68 | * The node type. This is a constant value set to {@link CKEDITOR.NODE_ELEMENT}.
|
---|
69 | * @type Number
|
---|
70 | * @example
|
---|
71 | */
|
---|
72 | type : CKEDITOR.NODE_ELEMENT,
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Adds a node to the element children list.
|
---|
76 | * @param {Object} node The node to be added. It can be any of of the
|
---|
77 | * following types: {@link CKEDITOR.htmlParser.element},
|
---|
78 | * {@link CKEDITOR.htmlParser.text} and
|
---|
79 | * {@link CKEDITOR.htmlParser.comment}.
|
---|
80 | * @function
|
---|
81 | * @example
|
---|
82 | */
|
---|
83 | add : CKEDITOR.htmlParser.fragment.prototype.add,
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Clone this element.
|
---|
87 | * @returns {CKEDITOR.htmlParser.element} The element clone.
|
---|
88 | * @example
|
---|
89 | */
|
---|
90 | clone : function()
|
---|
91 | {
|
---|
92 | return new CKEDITOR.htmlParser.element( this.name, this.attributes );
|
---|
93 | },
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Writes the element HTML to a CKEDITOR.htmlWriter.
|
---|
97 | * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
|
---|
98 | * @example
|
---|
99 | */
|
---|
100 | writeHtml : function( writer, filter )
|
---|
101 | {
|
---|
102 | var attributes = this.attributes;
|
---|
103 |
|
---|
104 | // Ignore cke: prefixes when writing HTML.
|
---|
105 | var element = this,
|
---|
106 | writeName = element.name,
|
---|
107 | a, newAttrName, value;
|
---|
108 |
|
---|
109 | var isChildrenFiltered;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Providing an option for bottom-up filtering order ( element
|
---|
113 | * children to be pre-filtered before the element itself ).
|
---|
114 | */
|
---|
115 | element.filterChildren = function()
|
---|
116 | {
|
---|
117 | if ( !isChildrenFiltered )
|
---|
118 | {
|
---|
119 | var writer = new CKEDITOR.htmlParser.basicWriter();
|
---|
120 | CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.call( element, writer, filter );
|
---|
121 | element.children = new CKEDITOR.htmlParser.fragment.fromHtml( writer.getHtml() ).children;
|
---|
122 | isChildrenFiltered = 1;
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | if ( filter )
|
---|
127 | {
|
---|
128 | while ( true )
|
---|
129 | {
|
---|
130 | if ( !( writeName = filter.onElementName( writeName ) ) )
|
---|
131 | return;
|
---|
132 |
|
---|
133 | element.name = writeName;
|
---|
134 |
|
---|
135 | if ( !( element = filter.onElement( element ) ) )
|
---|
136 | return;
|
---|
137 |
|
---|
138 | element.parent = this.parent;
|
---|
139 |
|
---|
140 | if ( element.name == writeName )
|
---|
141 | break;
|
---|
142 |
|
---|
143 | // If the element has been replaced with something of a
|
---|
144 | // different type, then make the replacement write itself.
|
---|
145 | if ( element.type != CKEDITOR.NODE_ELEMENT )
|
---|
146 | {
|
---|
147 | element.writeHtml( writer, filter );
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | writeName = element.name;
|
---|
152 |
|
---|
153 | // This indicate that the element has been dropped by
|
---|
154 | // filter but not the children.
|
---|
155 | if ( !writeName )
|
---|
156 | {
|
---|
157 | this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
|
---|
158 | return;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | // The element may have been changed, so update the local
|
---|
163 | // references.
|
---|
164 | attributes = element.attributes;
|
---|
165 | }
|
---|
166 |
|
---|
167 | // Open element tag.
|
---|
168 | writer.openTag( writeName, attributes );
|
---|
169 |
|
---|
170 | // Copy all attributes to an array.
|
---|
171 | var attribsArray = [];
|
---|
172 | // Iterate over the attributes twice since filters may alter
|
---|
173 | // other attributes.
|
---|
174 | for ( var i = 0 ; i < 2; i++ )
|
---|
175 | {
|
---|
176 | for ( a in attributes )
|
---|
177 | if ( ! ( a in Object.prototype ) )
|
---|
178 | {
|
---|
179 | newAttrName = a;
|
---|
180 | value = attributes[ a ];
|
---|
181 | if ( i == 1 )
|
---|
182 | attribsArray.push( [ a, value ] );
|
---|
183 | else if ( filter )
|
---|
184 | {
|
---|
185 | while ( true )
|
---|
186 | {
|
---|
187 | if ( !( newAttrName = filter.onAttributeName( a ) ) )
|
---|
188 | {
|
---|
189 | delete attributes[ a ];
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | else if ( newAttrName != a )
|
---|
193 | {
|
---|
194 | delete attributes[ a ];
|
---|
195 | a = newAttrName;
|
---|
196 | continue;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | if ( newAttrName )
|
---|
202 | {
|
---|
203 | if ( ( value = filter.onAttribute( element, newAttrName, value ) ) === false )
|
---|
204 | delete attributes[ newAttrName ];
|
---|
205 | else
|
---|
206 | attributes [ newAttrName ] = value;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | // Sort the attributes by name.
|
---|
212 | if ( writer.sortAttributes )
|
---|
213 | attribsArray.sort( sortAttribs );
|
---|
214 |
|
---|
215 | // Send the attributes.
|
---|
216 | var len = attribsArray.length;
|
---|
217 | for ( i = 0 ; i < len ; i++ )
|
---|
218 | {
|
---|
219 | var attrib = attribsArray[ i ];
|
---|
220 | writer.attribute( attrib[0], attrib[1] );
|
---|
221 | }
|
---|
222 |
|
---|
223 | // Close the tag.
|
---|
224 | writer.openTagClose( writeName, element.isEmpty );
|
---|
225 |
|
---|
226 | if ( !element.isEmpty )
|
---|
227 | {
|
---|
228 | this.writeChildrenHtml.call( element, writer, isChildrenFiltered ? null : filter );
|
---|
229 | // Close the element.
|
---|
230 | writer.closeTag( writeName );
|
---|
231 | }
|
---|
232 | },
|
---|
233 |
|
---|
234 | writeChildrenHtml : function( writer, filter )
|
---|
235 | {
|
---|
236 | // Send children.
|
---|
237 | CKEDITOR.htmlParser.fragment.prototype.writeChildrenHtml.apply( this, arguments );
|
---|
238 |
|
---|
239 | }
|
---|
240 | };
|
---|
241 | })();
|
---|