Update ckeditor to version 3.2.1
[gopost.git] / ckeditor / _source / core / htmlparser / fragment.js
blob244e298320d63278cf09f939e704dd5a376b74ce
1 /*
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7 * A lightweight representation of an HTML DOM structure.
8 * @constructor
9 * @example
11 CKEDITOR.htmlParser.fragment = function()
13 /**
14 * The nodes contained in the root of this fragment.
15 * @type Array
16 * @example
17 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );
18 * alert( fragment.children.length ); "2"
20 this.children = [];
22 /**
23 * Get the fragment parent. Should always be null.
24 * @type Object
25 * @default null
26 * @example
28 this.parent = null;
30 /** @private */
31 this._ =
33 isBlockLike : true,
34 hasInlineStarted : false
38 (function()
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
45 // parser fixing.
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;
51 /**
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.
56 * @example
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(),
64 html = [],
65 fragment = new CKEDITOR.htmlParser.fragment(),
66 pendingInline = [],
67 pendingBRs = [],
68 currentNode = fragment,
69 // Indicate we're inside a <pre> element, spaces should be touched differently.
70 inPre = false,
71 returnPoint;
73 function checkPending( newTagName )
75 var pendingBRsSent;
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 )
90 sendPendingBRs();
91 pendingBRsSent = 1;
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 );
105 i--;
111 function sendPendingBRs()
113 while ( pendingBRs.length )
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 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[ '_cke_real_element_type' ] ) )
129 elementName = realElementName;
130 else
131 elementName = element.name;
132 if ( elementName
133 && !( elementName in CKEDITOR.dtd.$body )
134 && !( elementName in CKEDITOR.dtd.$nonBodyContent ) )
136 var savedCurrent = currentNode;
138 // Create a <p> in the fragment.
139 currentNode = target;
140 parser.onTagOpen( fixForBody, {} );
142 // The new target now is the <p>.
143 target = currentNode;
145 if ( enforceCurrent )
146 currentNode = savedCurrent;
150 // Rtrim empty spaces on block end boundary. (#3585)
151 if ( element._.isBlockLike
152 && element.name != 'pre' )
155 var length = element.children.length,
156 lastChild = element.children[ length - 1 ],
157 text;
158 if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )
160 if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )
161 element.children.length = length -1;
162 else
163 lastChild.value = text;
167 target.add( element );
169 if ( element.returnPoint )
171 currentNode = element.returnPoint;
172 delete element.returnPoint;
176 parser.onTagOpen = function( tagName, attributes, selfClosing )
178 var element = new CKEDITOR.htmlParser.element( tagName, attributes );
180 // "isEmpty" will be always "false" for unknown elements, so we
181 // must force it if the parser has identified it as a selfClosing tag.
182 if ( element.isUnknown && selfClosing )
183 element.isEmpty = true;
185 // This is a tag to be removed if empty, so do not add it immediately.
186 if ( CKEDITOR.dtd.$removeEmpty[ tagName ] )
188 pendingInline.push( element );
189 return;
191 else if ( tagName == 'pre' )
192 inPre = true;
193 else if ( tagName == 'br' && inPre )
195 currentNode.add( new CKEDITOR.htmlParser.text( '\n' ) );
196 return;
199 if ( tagName == 'br' )
201 pendingBRs.push( element );
202 return;
205 var currentName = currentNode.name;
207 var currentDtd = currentName
208 && ( CKEDITOR.dtd[ currentName ]
209 || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) );
211 // If the element cannot be child of the current element.
212 if ( currentDtd // Fragment could receive any elements.
213 && !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )
216 var reApply = false,
217 addPoint; // New position to start adding nodes.
219 // Fixing malformed nested lists by moving it into a previous list item. (#3828)
220 if ( tagName in listBlocks
221 && currentName in listBlocks )
223 var children = currentNode.children,
224 lastChild = children[ children.length - 1 ];
226 // Establish the list item if it's not existed.
227 if ( !( lastChild && lastChild.name in listItems ) )
228 addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );
230 returnPoint = currentNode, addPoint = lastChild;
232 // If the element name is the same as the current element name,
233 // then just close the current one and append the new one to the
234 // parent. This situation usually happens with <p>, <li>, <dt> and
235 // <dd>, specially in IE. Do not enter in this if block in this case.
236 else if ( tagName == currentName )
238 addElement( currentNode, currentNode.parent );
240 else
242 if ( nonBreakingBlocks[ currentName ] )
244 if ( !returnPoint )
245 returnPoint = currentNode;
247 else
249 addElement( currentNode, currentNode.parent, true );
251 if ( !optionalClose[ currentName ] )
253 // The current element is an inline element, which
254 // cannot hold the new one. Put it in the pending list,
255 // and try adding the new one after it.
256 pendingInline.unshift( currentNode );
260 reApply = true;
263 if ( addPoint )
264 currentNode = addPoint;
265 // Try adding it to the return point, or the parent element.
266 else
267 currentNode = currentNode.returnPoint || currentNode.parent;
269 if ( reApply )
271 parser.onTagOpen.apply( this, arguments );
272 return;
276 checkPending( tagName );
277 sendPendingBRs();
279 element.parent = currentNode;
280 element.returnPoint = returnPoint;
281 returnPoint = 0;
283 if ( element.isEmpty )
284 addElement( element );
285 else
286 currentNode = element;
289 parser.onTagClose = function( tagName )
291 // Check if there is any pending tag to be closed.
292 for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )
294 // If found, just remove it from the list.
295 if ( tagName == pendingInline[ i ].name )
297 pendingInline.splice( i, 1 );
298 return;
302 var pendingAdd = [],
303 newPendingInline = [],
304 candidate = currentNode;
306 while ( candidate.type && candidate.name != tagName )
308 // If this is an inline element, add it to the pending list, if we're
309 // really closing one of the parents element later, they will continue
310 // after it.
311 if ( !candidate._.isBlockLike )
312 newPendingInline.unshift( candidate );
314 // This node should be added to it's parent at this point. But,
315 // it should happen only if the closing tag is really closing
316 // one of the nodes. So, for now, we just cache it.
317 pendingAdd.push( candidate );
319 candidate = candidate.parent;
322 if ( candidate.type )
324 // Add all elements that have been found in the above loop.
325 for ( i = 0 ; i < pendingAdd.length ; i++ )
327 var node = pendingAdd[ i ];
328 addElement( node, node.parent );
331 currentNode = candidate;
333 if ( currentNode.name == 'pre' )
334 inPre = false;
336 if ( candidate._.isBlockLike )
337 sendPendingBRs();
339 addElement( candidate, candidate.parent );
341 // The parent should start receiving new nodes now, except if
342 // addElement changed the currentNode.
343 if ( candidate == currentNode )
344 currentNode = currentNode.parent;
346 pendingInline = pendingInline.concat( newPendingInline );
349 if ( tagName == 'body' )
350 fixForBody = false;
353 parser.onText = function( text )
355 // Trim empty spaces at beginning of element contents except <pre>.
356 if ( !currentNode._.hasInlineStarted && !inPre )
358 text = CKEDITOR.tools.ltrim( text );
360 if ( text.length === 0 )
361 return;
364 sendPendingBRs();
365 checkPending();
367 if ( fixForBody
368 && ( !currentNode.type || currentNode.name == 'body' )
369 && CKEDITOR.tools.trim( text ) )
371 this.onTagOpen( fixForBody, {} );
374 // Shrinking consequential spaces into one single for all elements
375 // text contents.
376 if ( !inPre )
377 text = text.replace( /[\t\r\n ]{2,}|[\t\r\n]/g, ' ' );
379 currentNode.add( new CKEDITOR.htmlParser.text( text ) );
382 parser.onCDATA = function( cdata )
384 currentNode.add( new CKEDITOR.htmlParser.cdata( cdata ) );
387 parser.onComment = function( comment )
389 currentNode.add( new CKEDITOR.htmlParser.comment( comment ) );
392 // Parse it.
393 parser.parse( fragmentHtml );
395 sendPendingBRs();
397 // Close all pending nodes.
398 while ( currentNode.type )
400 var parent = currentNode.parent,
401 node = currentNode;
403 if ( fixForBody
404 && ( !parent.type || parent.name == 'body' )
405 && !CKEDITOR.dtd.$body[ node.name ] )
407 currentNode = parent;
408 parser.onTagOpen( fixForBody, {} );
409 parent = currentNode;
412 parent.add( node );
413 currentNode = parent;
416 return fragment;
419 CKEDITOR.htmlParser.fragment.prototype =
422 * Adds a node to this fragment.
423 * @param {Object} node The node to be added. It can be any of of the
424 * following types: {@link CKEDITOR.htmlParser.element},
425 * {@link CKEDITOR.htmlParser.text} and
426 * {@link CKEDITOR.htmlParser.comment}.
427 * @example
429 add : function( node )
431 var len = this.children.length,
432 previous = len > 0 && this.children[ len - 1 ] || null;
434 if ( previous )
436 // If the block to be appended is following text, trim spaces at
437 // the right of it.
438 if ( node._.isBlockLike && previous.type == CKEDITOR.NODE_TEXT )
440 previous.value = CKEDITOR.tools.rtrim( previous.value );
442 // If we have completely cleared the previous node.
443 if ( previous.value.length === 0 )
445 // Remove it from the list and add the node again.
446 this.children.pop();
447 this.add( node );
448 return;
452 previous.next = node;
455 node.previous = previous;
456 node.parent = this;
458 this.children.push( node );
460 this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike );
464 * Writes the fragment HTML to a CKEDITOR.htmlWriter.
465 * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.
466 * @example
467 * var writer = new CKEDITOR.htmlWriter();
468 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '&lt;P&gt;&lt;B&gt;Example' );
469 * fragment.writeHtml( writer )
470 * alert( writer.getHtml() ); "&lt;p&gt;&lt;b&gt;Example&lt;/b&gt;&lt;/p&gt;"
472 writeHtml : function( writer, filter )
474 var isChildrenFiltered;
475 this.filterChildren = function()
477 var writer = new CKEDITOR.htmlParser.basicWriter();
478 this.writeChildrenHtml.call( this, writer, filter, true );
479 var html = writer.getHtml();
480 this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;
481 isChildrenFiltered = 1;
484 // Filtering the root fragment before anything else.
485 !this.name && filter && filter.onFragment( this );
487 this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );
490 writeChildrenHtml : function( writer, filter )
492 for ( var i = 0 ; i < this.children.length ; i++ )
493 this.children[i].writeHtml( writer, filter );
496 })();