2 Copyright (c) 2003-2010, 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()
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
;
131 elementName
= element
.name
;
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 ],
158 if ( lastChild
&& lastChild
.type
== CKEDITOR
.NODE_TEXT
)
160 if ( !( text
= CKEDITOR
.tools
.rtrim( lastChild
.value
) ) )
161 element
.children
.length
= length
-1;
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
);
191 else if ( tagName
== 'pre' )
193 else if ( tagName
== 'br' && inPre
)
195 currentNode
.add( new CKEDITOR
.htmlParser
.text( '\n' ) );
199 if ( tagName
== 'br' )
201 pendingBRs
.push( element
);
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
] )
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
);
242 if ( nonBreakingBlocks
[ currentName
] )
245 returnPoint
= currentNode
;
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
);
264 currentNode
= addPoint
;
265 // Try adding it to the return point, or the parent element.
267 currentNode
= currentNode
.returnPoint
|| currentNode
.parent
;
271 parser
.onTagOpen
.apply( this, arguments
);
276 checkPending( tagName
);
279 element
.parent
= currentNode
;
280 element
.returnPoint
= returnPoint
;
283 if ( element
.isEmpty
)
284 addElement( element
);
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 );
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
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' )
336 if ( candidate
._
.isBlockLike
)
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' )
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 )
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
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
) );
393 parser
.parse( fragmentHtml
);
397 // Close all pending nodes.
398 while ( currentNode
.type
)
400 var parent
= currentNode
.parent
,
404 && ( !parent
.type
|| parent
.name
== 'body' )
405 && !CKEDITOR
.dtd
.$body
[ node
.name
] )
407 currentNode
= parent
;
408 parser
.onTagOpen( fixForBody
, {} );
409 parent
= currentNode
;
413 currentNode
= parent
;
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}.
429 add : function( node
)
431 var len
= this.children
.length
,
432 previous
= len
> 0 && this.children
[ len
- 1 ] || null;
436 // If the block to be appended is following text, trim spaces at
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.
452 previous
.next
= node
;
455 node
.previous
= previous
;
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.
467 * var writer = new CKEDITOR.htmlWriter();
468 * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<P><B>Example' );
469 * fragment.writeHtml( writer )
470 * alert( writer.getHtml() ); "<p><b>Example</b></p>"
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
);