2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * A lightweight representation of an HTML DOM structure.
11 CKEDITOR.htmlParser.fragment = function()
14 * The nodes contained in the root of this fragment.
17 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
18 * alert( fragment.children.length ); "2"
23 * Get the fragment parent. Should always be null.
34 hasInlineStarted : false
40 // Block-level elements whose internal structure should be respected during
42 var nonBreakingBlocks = CKEDITOR.tools.extend( { table:1,ul:1,ol:1,dl:1 }, CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl );
44 // IE < 8 don't output the close tag on definition list items. (#6975)
45 var optionalCloseTags = CKEDITOR.env.ie && CKEDITOR.env.version < 8 ? { dd : 1, dt :1 } : {};
47 var listBlocks = { ol:1, ul:1 };
49 // Dtd of the fragment element, basically it accept anything except for intermediate structure, e.g. orphan <li>.
50 var rootDtd = CKEDITOR.tools.extend( {}, { html: 1 }, CKEDITOR.dtd.html, CKEDITOR.dtd.body, CKEDITOR.dtd.head, { style:1,script:1 } );
52 function isRemoveEmpty( node )
54 // Empty link is to be removed when empty but not anchor. (#7894)
55 return node.name == 'a' && node.attributes.href
56 || CKEDITOR.dtd.$removeEmpty[ node.name ];
60 * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.
61 * @param {String} fragmentHtml The HTML to be parsed, filling the fragment.
62 * @param {Number} [fixForBody=false] Wrap body with specified element if needed.
63 * @param {CKEDITOR.htmlParser.element} contextNode Parse the html as the content of this element.
64 * @returns CKEDITOR.htmlParser.fragment The fragment created.
66 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
67 * alert( fragment.children[0].name ); "b"
68 * alert( fragment.children[1].value ); " Text"
70 CKEDITOR.htmlParser.fragment.fromHtml = function( fragmentHtml, fixForBody, contextNode )
72 var parser = new CKEDITOR.htmlParser(),
73 fragment = contextNode || new CKEDITOR.htmlParser.fragment(),
76 currentNode = fragment,
77 // Indicate we're inside a <textarea> element, spaces should be touched differently.
79 // Indicate we're inside a <pre> element, spaces should be touched differently.
82 function checkPending( newTagName )
86 if ( pendingInline.length > 0 )
88 for ( var i = 0 ; i < pendingInline.length ; i++ )
90 var pendingElement = pendingInline[ i ],
91 pendingName = pendingElement.name,
92 pendingDtd = CKEDITOR.dtd[ pendingName ],
93 currentDtd = currentNode.name && CKEDITOR.dtd[ currentNode.name ];
95 if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) )
97 if ( !pendingBRsSent )
103 // Get a clone for the pending element.
104 pendingElement = pendingElement.clone();
106 // Add it to the current node and make it the current,
107 // so the new element will be added inside of it.
108 pendingElement.parent = currentNode;
109 currentNode = pendingElement;
111 // Remove the pending element (back the index by one
112 // to properly process the next entry).
113 pendingInline.splice( i, 1 );
118 // Some element of the same type cannot be nested, flat them,
119 // e.g. <a href="#">foo<a href="#">bar</a></a>. (#7894)
120 if ( pendingName == currentNode.name )
121 addElement( currentNode, currentNode.parent, 1 ), i--;
127 function sendPendingBRs()
129 while ( pendingBRs.length )
130 currentNode.add( pendingBRs.shift() );
134 * Beside of simply append specified element to target, this function also takes
135 * care of other dirty lifts like forcing block in body, trimming spaces at
136 * the block boundaries etc.
138 * @param {Element} element The element to be added as the last child of {@link target}.
139 * @param {Element} target The parent element to relieve the new node.
140 * @param {Boolean} [moveCurrent=false] Don't change the "currentNode" global unless
141 * there's a return point node specified on the element, otherwise move current onto {@link target} node.
143 function addElement( element, target, moveCurrent )
145 // Ignore any element that has already been added.
146 if ( element.previous !== undefined )
149 target = target || currentNode || fragment;
151 // Current element might be mangled by fix body below,
152 // save it for restore later.
153 var savedCurrent = currentNode;
155 // If the target is the fragment and this inline element can't go inside
156 // body (if fixForBody).
157 if ( fixForBody && ( !target.type || target.name == 'body' ) )
159 var elementName, realElementName;
160 if ( element.attributes
161 && ( realElementName =
162 element.attributes[ 'data-cke-real-element-type' ] ) )
163 elementName = realElementName;
165 elementName = element.name;
167 if ( elementName && !( elementName in CKEDITOR.dtd.$body || elementName == 'body' || element.isOrphan ) )
169 // Create a <p> in the fragment.
170 currentNode = target;
171 parser.onTagOpen( fixForBody, {} );
173 // The new target now is the <p>.
174 element.returnPoint = target = currentNode;
178 // Rtrim empty spaces on block end boundary. (#3585)
179 if ( element._.isBlockLike
180 && element.name != 'pre' && element.name != 'textarea' )
183 var length = element.children.length,
184 lastChild = element.children[ length - 1 ],
186 if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )
188 if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )
189 element.children.length = length -1;
191 lastChild.value = text;
195 target.add( element );
197 if ( element.returnPoint )
199 currentNode = element.returnPoint;
200 delete element.returnPoint;
203 currentNode = moveCurrent ? target : savedCurrent;
206 parser.onTagOpen = function( tagName, attributes, selfClosing, optionalClose )
208 var element = new CKEDITOR.htmlParser.element( tagName, attributes );
210 // "isEmpty" will be always "false" for unknown elements, so we
211 // must force it if the parser has identified it as a selfClosing tag.
212 if ( element.isUnknown && selfClosing )
213 element.isEmpty = true;
215 // Check for optional closed elements, including browser quirks and manually opened blocks.
216 element.isOptionalClose = tagName in optionalCloseTags || optionalClose;
218 // This is a tag to be removed if empty, so do not add it immediately.
219 if ( isRemoveEmpty( element ) )
221 pendingInline.push( element );
224 else if ( tagName == 'pre' )
226 else if ( tagName == 'br' && inPre )
228 currentNode.add( new CKEDITOR.htmlParser.text( '\n' ) );
231 else if ( tagName == 'textarea' )
234 if ( tagName == 'br' )
236 pendingBRs.push( element );
242 var currentName = currentNode.name;
244 var currentDtd = currentName ? ( CKEDITOR.dtd[ currentName ]
245 || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) )
248 // If the element cannot be child of the current element.
249 if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )
251 // Current node doesn't have a close tag, time for a close
252 // as this element isn't fit in. (#7497)
253 if ( currentNode.isOptionalClose )
254 parser.onTagClose( currentName );
255 // Fixing malformed nested lists by moving it into a previous list item. (#3828)
256 else if ( tagName in listBlocks
257 && currentName in listBlocks )
259 var children = currentNode.children,
260 lastChild = children[ children.length - 1 ];
262 // Establish the list item if it's not existed.
263 if ( !( lastChild && lastChild.name == 'li' ) )
264 addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );
266 !element.returnPoint && ( element.returnPoint = currentNode );
267 currentNode = lastChild;
269 // Establish new list root for orphan list items.
270 else if ( tagName in CKEDITOR.dtd.$listItem && currentName != tagName )
271 parser.onTagOpen( tagName == 'li' ? 'ul' : 'dl', {}, 0, 1 );
272 // We're inside a structural block like table and list, AND the incoming element
273 // is not of the same type (e.g. <td>td1<td>td2</td>), we simply add this new one before it,
274 // and most importantly, return back to here once this element is added,
275 // e.g. <table><tr><td>td1</td><p>p1</p><td>td2</td></tr></table>
276 else if ( currentName in nonBreakingBlocks && currentName != tagName )
278 !element.returnPoint && ( element.returnPoint = currentNode );
279 currentNode = currentNode.parent;
283 // The current element is an inline element, which
284 // need to be continued even after the close, so put
285 // it in the pending list.
286 if ( currentName in CKEDITOR.dtd.$inline )
287 pendingInline.unshift( currentNode );
289 // The most common case where we just need to close the
290 // current one and append the new one to the parent.
291 if ( currentNode.parent )
292 addElement( currentNode, currentNode.parent, 1 );
293 // We've tried our best to fix the embarrassment here, while
294 // this element still doesn't find it's parent, mark it as
295 // orphan and show our tolerance to it.
298 element.isOrphan = 1;
307 checkPending( tagName );
310 element.parent = currentNode;
312 if ( element.isEmpty )
313 addElement( element );
315 currentNode = element;
318 parser.onTagClose = function( tagName )
320 // Check if there is any pending tag to be closed.
321 for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )
323 // If found, just remove it from the list.
324 if ( tagName == pendingInline[ i ].name )
326 pendingInline.splice( i, 1 );
332 newPendingInline = [],
333 candidate = currentNode;
335 while ( candidate != fragment && candidate.name != tagName )
337 // If this is an inline element, add it to the pending list, if we're
338 // really closing one of the parents element later, they will continue
340 if ( !candidate._.isBlockLike )
341 newPendingInline.unshift( candidate );
343 // This node should be added to it's parent at this point. But,
344 // it should happen only if the closing tag is really closing
345 // one of the nodes. So, for now, we just cache it.
346 pendingAdd.push( candidate );
348 // Make sure return point is properly restored.
349 candidate = candidate.returnPoint || candidate.parent;
352 if ( candidate != fragment )
354 // Add all elements that have been found in the above loop.
355 for ( i = 0 ; i < pendingAdd.length ; i++ )
357 var node = pendingAdd[ i ];
358 addElement( node, node.parent );
361 currentNode = candidate;
363 if ( currentNode.name == 'pre' )
366 if ( currentNode.name == 'textarea' )
369 if ( candidate._.isBlockLike )
372 addElement( candidate, candidate.parent );
374 // The parent should start receiving new nodes now, except if
375 // addElement changed the currentNode.
376 if ( candidate == currentNode )
377 currentNode = currentNode.parent;
379 pendingInline = pendingInline.concat( newPendingInline );
382 if ( tagName == 'body' )
386 parser.onText = function( text )
388 // Trim empty spaces at beginning of text contents except <pre> and <textarea>.
389 if ( ( !currentNode._.hasInlineStarted || pendingBRs.length ) && !inPre && !inTextarea )
391 text = CKEDITOR.tools.ltrim( text );
393 if ( text.length === 0 )
401 && ( !currentNode.type || currentNode.name == 'body' )
402 && CKEDITOR.tools.trim( text ) )
404 this.onTagOpen( fixForBody, {}, 0, 1 );
407 // Shrinking consequential spaces into one single for all elements
409 if ( !inPre && !inTextarea )
410 text = text.replace( /[\t\r\n ]{2,}|[\t\r\n]/g, ' ' );
412 currentNode.add( new CKEDITOR.htmlParser.text( text ) );
415 parser.onCDATA = function( cdata )
417 currentNode.add( new CKEDITOR.htmlParser.cdata( cdata ) );
420 parser.onComment = function( comment )
424 currentNode.add( new CKEDITOR.htmlParser.comment( comment ) );
428 parser.parse( fragmentHtml );
430 // Send all pending BRs except one, which we consider a unwanted bogus. (#5293)
431 sendPendingBRs( !CKEDITOR.env.ie && 1 );
433 // Close all pending nodes, make sure return point is properly restored.
434 while ( currentNode != fragment )
435 addElement( currentNode, currentNode.parent, 1 );
440 CKEDITOR.htmlParser.fragment.prototype =
443 * Adds a node to this fragment.
444 * @param {Object} node The node to be added. It can be any of of the
445 * following types: {@link CKEDITOR.htmlParser.element},
446 * {@link CKEDITOR.htmlParser.text} and
447 * {@link CKEDITOR.htmlParser.comment}.
448 * @param {Number} [index] From where the insertion happens.
451 add : function( node, index )
453 isNaN( index ) && ( index = this.children.length );
455 var previous = index > 0 ? this.children[ index - 1 ] : null;
458 // If the block to be appended is following text, trim spaces at
460 if ( node._.isBlockLike && previous.type == CKEDITOR.NODE_TEXT )
462 previous.value = CKEDITOR.tools.rtrim( previous.value );
464 // If we have completely cleared the previous node.
465 if ( previous.value.length === 0 )
467 // Remove it from the list and add the node again.
474 previous.next = node;
477 node.previous = previous;
480 this.children.splice( index, 0, node );
482 this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike );
486 * Writes the fragment HTML to a CKEDITOR.htmlWriter.
487 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
489 * var writer = new CKEDITOR.htmlWriter();
490 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<P><B>Example' );
491 * fragment.writeHtml( writer )
492 * alert( writer.getHtml() ); "<p><b>Example</b></p>"
494 writeHtml : function( writer, filter )
496 var isChildrenFiltered;
497 this.filterChildren = function()
499 var writer = new CKEDITOR.htmlParser.basicWriter();
500 this.writeChildrenHtml.call( this, writer, filter, true );
501 var html = writer.getHtml();
502 this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;
503 isChildrenFiltered = 1;
506 // Filtering the root fragment before anything else.
507 !this.name && filter && filter.onFragment( this );
509 this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );
512 writeChildrenHtml : function( writer, filter )
514 for ( var i = 0 ; i < this.children.length ; i++ )
515 this.children[i].writeHtml( writer, filter );