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 // Elements which the end tag is marked as optional in the HTML 4.01 DTD
41 // (expect empty elements).
42 var optionalClose = {colgroup:1,dd:1,dt:1,li:1,option:1,p:1,td:1,tfoot:1,th:1,thead:1,tr:1};
44 // Block-level elements whose internal structure should be respected during
46 var nonBreakingBlocks = CKEDITOR.tools.extend(
47 {table:1,ul:1,ol:1,dl:1},
48 CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl ),
49 listBlocks = CKEDITOR.dtd.$list, listItems = CKEDITOR.dtd.$listItem;
52 * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.
53 * @param {String} fragmentHtml The HTML to be parsed, filling the fragment.
54 * @param {Number} [fixForBody=false] Wrap body with specified element if needed.
55 * @returns CKEDITOR.htmlParser.fragment The fragment created.
57 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
58 * alert( fragment.children[0].name ); "b"
59 * alert( fragment.children[1].value ); " Text"
61 CKEDITOR.htmlParser.fragment.fromHtml = function( fragmentHtml, fixForBody )
63 var parser = new CKEDITOR.htmlParser(),
65 fragment = new CKEDITOR.htmlParser.fragment(),
68 currentNode = fragment,
69 // Indicate we're inside a <pre> element, spaces should be touched differently.
73 function checkPending( newTagName )
77 if ( pendingInline.length > 0 )
79 for ( var i = 0 ; i < pendingInline.length ; i++ )
81 var pendingElement = pendingInline[ i ],
82 pendingName = pendingElement.name,
83 pendingDtd = CKEDITOR.dtd[ pendingName ],
84 currentDtd = currentNode.name && CKEDITOR.dtd[ currentNode.name ];
86 if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) )
88 if ( !pendingBRsSent )
94 // Get a clone for the pending element.
95 pendingElement = pendingElement.clone();
97 // Add it to the current node and make it the current,
98 // so the new element will be added inside of it.
99 pendingElement.parent = currentNode;
100 currentNode = pendingElement;
102 // Remove the pending element (back the index by one
103 // to properly process the next entry).
104 pendingInline.splice( i, 1 );
111 function sendPendingBRs( brsToIgnore )
113 while ( pendingBRs.length - ( brsToIgnore || 0 ) > 0 )
114 currentNode.add( pendingBRs.shift() );
117 function addElement( element, target, enforceCurrent )
119 target = target || currentNode || fragment;
121 // If the target is the fragment and this inline element can't go inside
122 // body (if fixForBody).
123 if ( fixForBody && !target.type )
125 var elementName, realElementName;
126 if ( element.attributes
127 && ( realElementName =
128 element.attributes[ 'data-cke-real-element-type' ] ) )
129 elementName = realElementName;
131 elementName = element.name;
133 if ( elementName && elementName in CKEDITOR.dtd.$inline )
135 var savedCurrent = currentNode;
137 // Create a <p> in the fragment.
138 currentNode = target;
139 parser.onTagOpen( fixForBody, {} );
141 // The new target now is the <p>.
142 target = currentNode;
144 if ( enforceCurrent )
145 currentNode = savedCurrent;
149 // Rtrim empty spaces on block end boundary. (#3585)
150 if ( element._.isBlockLike
151 && element.name != 'pre' )
154 var length = element.children.length,
155 lastChild = element.children[ length - 1 ],
157 if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )
159 if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )
160 element.children.length = length -1;
162 lastChild.value = text;
166 target.add( element );
168 if ( element.returnPoint )
170 currentNode = element.returnPoint;
171 delete element.returnPoint;
175 parser.onTagOpen = function( tagName, attributes, selfClosing )
177 var element = new CKEDITOR.htmlParser.element( tagName, attributes );
179 // "isEmpty" will be always "false" for unknown elements, so we
180 // must force it if the parser has identified it as a selfClosing tag.
181 if ( element.isUnknown && selfClosing )
182 element.isEmpty = true;
184 // This is a tag to be removed if empty, so do not add it immediately.
185 if ( CKEDITOR.dtd.$removeEmpty[ tagName ] )
187 pendingInline.push( element );
190 else if ( tagName == 'pre' )
192 else if ( tagName == 'br' && inPre )
194 currentNode.add( new CKEDITOR.htmlParser.text( '\n' ) );
198 if ( tagName == 'br' )
200 pendingBRs.push( element );
204 var currentName = currentNode.name;
206 var currentDtd = currentName
207 && ( CKEDITOR.dtd[ currentName ]
208 || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) );
210 // If the element cannot be child of the current element.
211 if ( currentDtd // Fragment could receive any elements.
212 && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )
216 addPoint; // New position to start adding nodes.
218 // Fixing malformed nested lists by moving it into a previous list item. (#3828)
219 if ( tagName in listBlocks
220 && currentName in listBlocks )
222 var children = currentNode.children,
223 lastChild = children[ children.length - 1 ];
225 // Establish the list item if it's not existed.
226 if ( !( lastChild && lastChild.name in listItems ) )
227 addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );
229 returnPoint = currentNode, addPoint = lastChild;
231 // If the element name is the same as the current element name,
232 // then just close the current one and append the new one to the
233 // parent. This situation usually happens with <p>, <li>, <dt> and
234 // <dd>, specially in IE. Do not enter in this if block in this case.
235 else if ( tagName == currentName )
237 addElement( currentNode, currentNode.parent );
239 else if ( tagName in CKEDITOR.dtd.$listItem )
241 parser.onTagOpen( 'ul', {} );
242 addPoint = currentNode;
247 if ( nonBreakingBlocks[ currentName ] )
250 returnPoint = currentNode;
254 addElement( currentNode, currentNode.parent, true );
256 if ( !optionalClose[ currentName ] )
258 // The current element is an inline element, which
259 // cannot hold the new one. Put it in the pending list,
260 // and try adding the new one after it.
261 pendingInline.unshift( currentNode );
269 currentNode = addPoint;
270 // Try adding it to the return point, or the parent element.
272 currentNode = currentNode.returnPoint || currentNode.parent;
276 parser.onTagOpen.apply( this, arguments );
281 checkPending( tagName );
284 element.parent = currentNode;
285 element.returnPoint = returnPoint;
288 if ( element.isEmpty )
289 addElement( element );
291 currentNode = element;
294 parser.onTagClose = function( tagName )
296 // Check if there is any pending tag to be closed.
297 for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )
299 // If found, just remove it from the list.
300 if ( tagName == pendingInline[ i ].name )
302 pendingInline.splice( i, 1 );
308 newPendingInline = [],
309 candidate = currentNode;
311 while ( candidate.type && candidate.name != tagName )
313 // If this is an inline element, add it to the pending list, if we're
314 // really closing one of the parents element later, they will continue
316 if ( !candidate._.isBlockLike )
317 newPendingInline.unshift( candidate );
319 // This node should be added to it's parent at this point. But,
320 // it should happen only if the closing tag is really closing
321 // one of the nodes. So, for now, we just cache it.
322 pendingAdd.push( candidate );
324 candidate = candidate.parent;
327 if ( candidate.type )
329 // Add all elements that have been found in the above loop.
330 for ( i = 0 ; i < pendingAdd.length ; i++ )
332 var node = pendingAdd[ i ];
333 addElement( node, node.parent );
336 currentNode = candidate;
338 if ( currentNode.name == 'pre' )
341 if ( candidate._.isBlockLike )
344 addElement( candidate, candidate.parent );
346 // The parent should start receiving new nodes now, except if
347 // addElement changed the currentNode.
348 if ( candidate == currentNode )
349 currentNode = currentNode.parent;
351 pendingInline = pendingInline.concat( newPendingInline );
354 if ( tagName == 'body' )
358 parser.onText = function( text )
360 // Trim empty spaces at beginning of element contents except <pre>.
361 if ( !currentNode._.hasInlineStarted && !inPre )
363 text = CKEDITOR.tools.ltrim( text );
365 if ( text.length === 0 )
373 && ( !currentNode.type || currentNode.name == 'body' )
374 && CKEDITOR.tools.trim( text ) )
376 this.onTagOpen( fixForBody, {} );
379 // Shrinking consequential spaces into one single for all elements
382 text = text.replace( /[\t\r\n ]{2,}|[\t\r\n]/g, ' ' );
384 currentNode.add( new CKEDITOR.htmlParser.text( text ) );
387 parser.onCDATA = function( cdata )
389 currentNode.add( new CKEDITOR.htmlParser.cdata( cdata ) );
392 parser.onComment = function( comment )
396 currentNode.add( new CKEDITOR.htmlParser.comment( comment ) );
400 parser.parse( fragmentHtml );
402 // Send all pending BRs except one, which we consider a unwanted bogus. (#5293)
403 sendPendingBRs( !CKEDITOR.env.ie && 1 );
405 // Close all pending nodes.
406 while ( currentNode.type )
408 var parent = currentNode.parent,
412 && ( !parent.type || parent.name == 'body' )
413 && !CKEDITOR.dtd.$body[ node.name ] )
415 currentNode = parent;
416 parser.onTagOpen( fixForBody, {} );
417 parent = currentNode;
421 currentNode = parent;
427 CKEDITOR.htmlParser.fragment.prototype =
430 * Adds a node to this fragment.
431 * @param {Object} node The node to be added. It can be any of of the
432 * following types: {@link CKEDITOR.htmlParser.element},
433 * {@link CKEDITOR.htmlParser.text} and
434 * {@link CKEDITOR.htmlParser.comment}.
437 add : function( node )
439 var len = this.children.length,
440 previous = len > 0 && this.children[ len - 1 ] || null;
444 // If the block to be appended is following text, trim spaces at
446 if ( node._.isBlockLike && previous.type == CKEDITOR.NODE_TEXT )
448 previous.value = CKEDITOR.tools.rtrim( previous.value );
450 // If we have completely cleared the previous node.
451 if ( previous.value.length === 0 )
453 // Remove it from the list and add the node again.
460 previous.next = node;
463 node.previous = previous;
466 this.children.push( node );
468 this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike );
472 * Writes the fragment HTML to a CKEDITOR.htmlWriter.
473 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
475 * var writer = new CKEDITOR.htmlWriter();
476 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<P><B>Example' );
477 * fragment.writeHtml( writer )
478 * alert( writer.getHtml() ); "<p><b>Example</b></p>"
480 writeHtml : function( writer, filter )
482 var isChildrenFiltered;
483 this.filterChildren = function()
485 var writer = new CKEDITOR.htmlParser.basicWriter();
486 this.writeChildrenHtml.call( this, writer, filter, true );
487 var html = writer.getHtml();
488 this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;
489 isChildrenFiltered = 1;
492 // Filtering the root fragment before anything else.
493 !this.name && filter && filter.onFragment( this );
495 this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );
498 writeChildrenHtml : function( writer, filter )
500 for ( var i = 0 ; i < this.children.length ; i++ )
501 this.children[i].writeHtml( writer, filter );