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 DOM structure.
\r
11 CKEDITOR.htmlParser.fragment = function()
\r
14 * The nodes contained in the root of this fragment.
\r
17 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
\r
18 * alert( fragment.children.length ); "2"
\r
23 * Get the fragment parent. Should always be null.
\r
34 hasInlineStarted : false
\r
40 // Elements which the end tag is marked as optional in the HTML 4.01 DTD
\r
41 // (expect empty elements).
\r
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};
\r
44 // Block-level elements whose internal structure should be respected during
\r
46 var nonBreakingBlocks = CKEDITOR.tools.extend(
\r
47 {table:1,ul:1,ol:1,dl:1},
\r
48 CKEDITOR.dtd.table, CKEDITOR.dtd.ul, CKEDITOR.dtd.ol, CKEDITOR.dtd.dl ),
\r
49 listBlocks = CKEDITOR.dtd.$list, listItems = CKEDITOR.dtd.$listItem;
\r
52 * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.
\r
53 * @param {String} fragmentHtml The HTML to be parsed, filling the fragment.
\r
54 * @param {Number} [fixForBody=false] Wrap body with specified element if needed.
\r
55 * @returns CKEDITOR.htmlParser.fragment The fragment created.
\r
57 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
\r
58 * alert( fragment.children[0].name ); "b"
\r
59 * alert( fragment.children[1].value ); " Text"
\r
61 CKEDITOR.htmlParser.fragment.fromHtml = function( fragmentHtml, fixForBody )
\r
63 var parser = new CKEDITOR.htmlParser(),
\r
65 fragment = new CKEDITOR.htmlParser.fragment(),
\r
68 currentNode = fragment,
\r
69 // Indicate we're inside a <pre> element, spaces should be touched differently.
\r
73 function checkPending( newTagName )
\r
77 if ( pendingInline.length > 0 )
\r
79 for ( var i = 0 ; i < pendingInline.length ; i++ )
\r
81 var pendingElement = pendingInline[ i ],
\r
82 pendingName = pendingElement.name,
\r
83 pendingDtd = CKEDITOR.dtd[ pendingName ],
\r
84 currentDtd = currentNode.name && CKEDITOR.dtd[ currentNode.name ];
\r
86 if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) )
\r
88 if ( !pendingBRsSent )
\r
94 // Get a clone for the pending element.
\r
95 pendingElement = pendingElement.clone();
\r
97 // Add it to the current node and make it the current,
\r
98 // so the new element will be added inside of it.
\r
99 pendingElement.parent = currentNode;
\r
100 currentNode = pendingElement;
\r
102 // Remove the pending element (back the index by one
\r
103 // to properly process the next entry).
\r
104 pendingInline.splice( i, 1 );
\r
111 function sendPendingBRs()
\r
113 while ( pendingBRs.length )
\r
114 currentNode.add( pendingBRs.shift() );
\r
117 function addElement( element, target, enforceCurrent )
\r
119 target = target || currentNode || fragment;
\r
121 // If the target is the fragment and this element can't go inside
\r
122 // body (if fixForBody).
\r
123 if ( fixForBody && !target.type )
\r
125 var elementName, realElementName;
\r
126 if ( element.attributes
\r
127 && ( realElementName =
\r
128 element.attributes[ '_cke_real_element_type' ] ) )
\r
129 elementName = realElementName;
\r
131 elementName = element.name;
\r
133 && !( elementName in CKEDITOR.dtd.$body )
\r
134 && !( elementName in CKEDITOR.dtd.$nonBodyContent ) )
\r
136 var savedCurrent = currentNode;
\r
138 // Create a <p> in the fragment.
\r
139 currentNode = target;
\r
140 parser.onTagOpen( fixForBody, {} );
\r
142 // The new target now is the <p>.
\r
143 target = currentNode;
\r
145 if ( enforceCurrent )
\r
146 currentNode = savedCurrent;
\r
150 // Rtrim empty spaces on block end boundary. (#3585)
\r
151 if ( element._.isBlockLike
\r
152 && element.name != 'pre' )
\r
155 var length = element.children.length,
\r
156 lastChild = element.children[ length - 1 ],
\r
158 if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )
\r
160 if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )
\r
161 element.children.length = length -1;
\r
163 lastChild.value = text;
\r
167 target.add( element );
\r
169 if ( element.returnPoint )
\r
171 currentNode = element.returnPoint;
\r
172 delete element.returnPoint;
\r
176 parser.onTagOpen = function( tagName, attributes, selfClosing )
\r
178 var element = new CKEDITOR.htmlParser.element( tagName, attributes );
\r
180 // "isEmpty" will be always "false" for unknown elements, so we
\r
181 // must force it if the parser has identified it as a selfClosing tag.
\r
182 if ( element.isUnknown && selfClosing )
\r
183 element.isEmpty = true;
\r
185 // This is a tag to be removed if empty, so do not add it immediately.
\r
186 if ( CKEDITOR.dtd.$removeEmpty[ tagName ] )
\r
188 pendingInline.push( element );
\r
191 else if ( tagName == 'pre' )
\r
193 else if ( tagName == 'br' && inPre )
\r
195 currentNode.add( new CKEDITOR.htmlParser.text( '\n' ) );
\r
199 if ( tagName == 'br' )
\r
201 pendingBRs.push( element );
\r
205 var currentName = currentNode.name;
\r
207 var currentDtd = currentName
\r
208 && ( CKEDITOR.dtd[ currentName ]
\r
209 || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) );
\r
211 // If the element cannot be child of the current element.
\r
212 if ( currentDtd // Fragment could receive any elements.
\r
213 && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )
\r
216 var reApply = false,
\r
217 addPoint; // New position to start adding nodes.
\r
219 // Fixing malformed nested lists by moving it into a previous list item. (#3828)
\r
220 if ( tagName in listBlocks
\r
221 && currentName in listBlocks )
\r
223 var children = currentNode.children,
\r
224 lastChild = children[ children.length - 1 ];
\r
226 // Establish the list item if it's not existed.
\r
227 if ( !( lastChild && lastChild.name in listItems ) )
\r
228 addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );
\r
230 returnPoint = currentNode, addPoint = lastChild;
\r
232 // If the element name is the same as the current element name,
\r
233 // then just close the current one and append the new one to the
\r
234 // parent. This situation usually happens with <p>, <li>, <dt> and
\r
235 // <dd>, specially in IE. Do not enter in this if block in this case.
\r
236 else if ( tagName == currentName )
\r
238 addElement( currentNode, currentNode.parent );
\r
242 if ( nonBreakingBlocks[ currentName ] )
\r
244 if ( !returnPoint )
\r
245 returnPoint = currentNode;
\r
249 addElement( currentNode, currentNode.parent, true );
\r
251 if ( !optionalClose[ currentName ] )
\r
253 // The current element is an inline element, which
\r
254 // cannot hold the new one. Put it in the pending list,
\r
255 // and try adding the new one after it.
\r
256 pendingInline.unshift( currentNode );
\r
264 currentNode = addPoint;
\r
265 // Try adding it to the return point, or the parent element.
\r
267 currentNode = currentNode.returnPoint || currentNode.parent;
\r
271 parser.onTagOpen.apply( this, arguments );
\r
276 checkPending( tagName );
\r
279 element.parent = currentNode;
\r
280 element.returnPoint = returnPoint;
\r
283 if ( element.isEmpty )
\r
284 addElement( element );
\r
286 currentNode = element;
\r
289 parser.onTagClose = function( tagName )
\r
291 // Check if there is any pending tag to be closed.
\r
292 for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )
\r
294 // If found, just remove it from the list.
\r
295 if ( tagName == pendingInline[ i ].name )
\r
297 pendingInline.splice( i, 1 );
\r
302 var pendingAdd = [],
\r
303 newPendingInline = [],
\r
304 candidate = currentNode;
\r
306 while ( candidate.type && candidate.name != tagName )
\r
308 // If this is an inline element, add it to the pending list, if we're
\r
309 // really closing one of the parents element later, they will continue
\r
311 if ( !candidate._.isBlockLike )
\r
312 newPendingInline.unshift( candidate );
\r
314 // This node should be added to it's parent at this point. But,
\r
315 // it should happen only if the closing tag is really closing
\r
316 // one of the nodes. So, for now, we just cache it.
\r
317 pendingAdd.push( candidate );
\r
319 candidate = candidate.parent;
\r
322 if ( candidate.type )
\r
324 // Add all elements that have been found in the above loop.
\r
325 for ( i = 0 ; i < pendingAdd.length ; i++ )
\r
327 var node = pendingAdd[ i ];
\r
328 addElement( node, node.parent );
\r
331 currentNode = candidate;
\r
333 if ( currentNode.name == 'pre' )
\r
336 if ( candidate._.isBlockLike )
\r
339 addElement( candidate, candidate.parent );
\r
341 // The parent should start receiving new nodes now, except if
\r
342 // addElement changed the currentNode.
\r
343 if ( candidate == currentNode )
\r
344 currentNode = currentNode.parent;
\r
346 pendingInline = pendingInline.concat( newPendingInline );
\r
349 if ( tagName == 'body' )
\r
350 fixForBody = false;
\r
353 parser.onText = function( text )
\r
355 // Trim empty spaces at beginning of element contents except <pre>.
\r
356 if ( !currentNode._.hasInlineStarted && !inPre )
\r
358 text = CKEDITOR.tools.ltrim( text );
\r
360 if ( text.length === 0 )
\r
368 && ( !currentNode.type || currentNode.name == 'body' )
\r
369 && CKEDITOR.tools.trim( text ) )
\r
371 this.onTagOpen( fixForBody, {} );
\r
374 // Shrinking consequential spaces into one single for all elements
\r
377 text = text.replace( /[\t\r\n ]{2,}|[\t\r\n]/g, ' ' );
\r
379 currentNode.add( new CKEDITOR.htmlParser.text( text ) );
\r
382 parser.onCDATA = function( cdata )
\r
384 currentNode.add( new CKEDITOR.htmlParser.cdata( cdata ) );
\r
387 parser.onComment = function( comment )
\r
389 currentNode.add( new CKEDITOR.htmlParser.comment( comment ) );
\r
393 parser.parse( fragmentHtml );
\r
397 // Close all pending nodes.
\r
398 while ( currentNode.type )
\r
400 var parent = currentNode.parent,
\r
401 node = currentNode;
\r
404 && ( !parent.type || parent.name == 'body' )
\r
405 && !CKEDITOR.dtd.$body[ node.name ] )
\r
407 currentNode = parent;
\r
408 parser.onTagOpen( fixForBody, {} );
\r
409 parent = currentNode;
\r
412 parent.add( node );
\r
413 currentNode = parent;
\r
419 CKEDITOR.htmlParser.fragment.prototype =
\r
422 * Adds a node to this fragment.
\r
423 * @param {Object} node The node to be added. It can be any of of the
\r
424 * following types: {@link CKEDITOR.htmlParser.element},
\r
425 * {@link CKEDITOR.htmlParser.text} and
\r
426 * {@link CKEDITOR.htmlParser.comment}.
\r
429 add : function( node )
\r
431 var len = this.children.length,
\r
432 previous = len > 0 && this.children[ len - 1 ] || null;
\r
436 // If the block to be appended is following text, trim spaces at
\r
437 // the right of it.
\r
438 if ( node._.isBlockLike && previous.type == CKEDITOR.NODE_TEXT )
\r
440 previous.value = CKEDITOR.tools.rtrim( previous.value );
\r
442 // If we have completely cleared the previous node.
\r
443 if ( previous.value.length === 0 )
\r
445 // Remove it from the list and add the node again.
\r
446 this.children.pop();
\r
452 previous.next = node;
\r
455 node.previous = previous;
\r
456 node.parent = this;
\r
458 this.children.push( node );
\r
460 this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike );
\r
464 * Writes the fragment HTML to a CKEDITOR.htmlWriter.
\r
465 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
\r
467 * var writer = new CKEDITOR.htmlWriter();
\r
468 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<P><B>Example' );
\r
469 * fragment.writeHtml( writer )
\r
470 * alert( writer.getHtml() ); "<p><b>Example</b></p>"
\r
472 writeHtml : function( writer, filter )
\r
474 var isChildrenFiltered;
\r
475 this.filterChildren = function()
\r
477 var writer = new CKEDITOR.htmlParser.basicWriter();
\r
478 this.writeChildrenHtml.call( this, writer, filter, true );
\r
479 var html = writer.getHtml();
\r
480 this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;
\r
481 isChildrenFiltered = 1;
\r
484 // Filtering the root fragment before anything else.
\r
485 !this.name && filter && filter.onFragment( this );
\r
487 this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );
\r
490 writeChildrenHtml : function( writer, filter )
\r
492 for ( var i = 0 ; i < this.children.length ; i++ )
\r
493 this.children[i].writeHtml( writer, filter );
\r