JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / core / htmlparser / fragment.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * A lightweight representation of an HTML DOM structure.\r
8  * @constructor\r
9  * @example\r
10  */\r
11 CKEDITOR.htmlParser.fragment = function()\r
12 {\r
13         /**\r
14          * The nodes contained in the root of this fragment.\r
15          * @type Array\r
16          * @example\r
17          * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );\r
18          * alert( fragment.children.length );  "2"\r
19          */\r
20         this.children = [];\r
21 \r
22         /**\r
23          * Get the fragment parent. Should always be null.\r
24          * @type Object\r
25          * @default null\r
26          * @example\r
27          */\r
28         this.parent = null;\r
29 \r
30         /** @private */\r
31         this._ =\r
32         {\r
33                 isBlockLike : true,\r
34                 hasInlineStarted : false\r
35         };\r
36 };\r
37 \r
38 (function()\r
39 {\r
40         // Block-level elements whose internal structure should be respected during\r
41         // parser fixing.\r
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 );\r
43 \r
44         // IE < 8 don't output the close tag on definition list items. (#6975)\r
45         var optionalCloseTags = CKEDITOR.env.ie && CKEDITOR.env.version < 8 ? { dd : 1, dt :1 } : {};\r
46 \r
47         var listBlocks = { ol:1, ul:1 };\r
48 \r
49         // Dtd of the fragment element, basically it accept anything except for intermediate structure, e.g. orphan <li>.\r
50         var rootDtd = CKEDITOR.tools.extend( {}, { html: 1 }, CKEDITOR.dtd.html, CKEDITOR.dtd.body, CKEDITOR.dtd.head, { style:1,script:1 } );\r
51 \r
52         /**\r
53          * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.\r
54          * @param {String} fragmentHtml The HTML to be parsed, filling the fragment.\r
55          * @param {Number} [fixForBody=false] Wrap body with specified element if needed.\r
56          * @param {CKEDITOR.htmlParser.element} contextNode Parse the html as the content of this element.\r
57          * @returns CKEDITOR.htmlParser.fragment The fragment created.\r
58          * @example\r
59          * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );\r
60          * alert( fragment.children[0].name );  "b"\r
61          * alert( fragment.children[1].value );  " Text"\r
62          */\r
63         CKEDITOR.htmlParser.fragment.fromHtml = function( fragmentHtml, fixForBody, contextNode )\r
64         {\r
65                 var parser = new CKEDITOR.htmlParser(),\r
66                         fragment = contextNode || new CKEDITOR.htmlParser.fragment(),\r
67                         pendingInline = [],\r
68                         pendingBRs = [],\r
69                         currentNode = fragment,\r
70                     // Indicate we're inside a <pre> element, spaces should be touched differently.\r
71                         inPre = false;\r
72 \r
73                 function checkPending( newTagName )\r
74                 {\r
75                         var pendingBRsSent;\r
76 \r
77                         if ( pendingInline.length > 0 )\r
78                         {\r
79                                 for ( var i = 0 ; i < pendingInline.length ; i++ )\r
80                                 {\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
85 \r
86                                         if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) )\r
87                                         {\r
88                                                 if ( !pendingBRsSent )\r
89                                                 {\r
90                                                         sendPendingBRs();\r
91                                                         pendingBRsSent = 1;\r
92                                                 }\r
93 \r
94                                                 // Get a clone for the pending element.\r
95                                                 pendingElement = pendingElement.clone();\r
96 \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
101 \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
105                                                 i--;\r
106                                         }\r
107                                 }\r
108                         }\r
109                 }\r
110 \r
111                 function sendPendingBRs()\r
112                 {\r
113                         while ( pendingBRs.length )\r
114                                 currentNode.add( pendingBRs.shift() );\r
115                 }\r
116 \r
117                 /*\r
118                 * Beside of simply append specified element to target, this function also takes\r
119                 * care of other dirty lifts like forcing block in body, trimming spaces at\r
120                 * the block boundaries etc.\r
121                 *\r
122                 * @param {Element} element  The element to be added as the last child of {@link target}.\r
123                 * @param {Element} target The parent element to relieve the new node.\r
124                 * @param {Boolean} [moveCurrent=false] Don't change the "currentNode" global unless\r
125                 * there's a return point node specified on the element, otherwise move current onto {@link target} node.\r
126                  */\r
127                 function addElement( element, target, moveCurrent )\r
128                 {\r
129                         // Ignore any element that has already been added.\r
130                         if ( element.previous !== undefined )\r
131                                 return;\r
132 \r
133                         target = target || currentNode || fragment;\r
134 \r
135                         // Current element might be mangled by fix body below,\r
136                         // save it for restore later.\r
137                         var savedCurrent = currentNode;\r
138 \r
139                         // If the target is the fragment and this inline element can't go inside\r
140                         // body (if fixForBody).\r
141                         if ( fixForBody && ( !target.type || target.name == 'body' ) )\r
142                         {\r
143                                 var elementName, realElementName;\r
144                                 if ( element.attributes\r
145                                          && ( realElementName =\r
146                                                   element.attributes[ 'data-cke-real-element-type' ] ) )\r
147                                         elementName = realElementName;\r
148                                 else\r
149                                         elementName =  element.name;\r
150 \r
151                                 if ( elementName && !( elementName in CKEDITOR.dtd.$body || elementName == 'body' || element.isOrphan ) )\r
152                                 {\r
153                                         // Create a <p> in the fragment.\r
154                                         currentNode = target;\r
155                                         parser.onTagOpen( fixForBody, {} );\r
156 \r
157                                         // The new target now is the <p>.\r
158                                         element.returnPoint = target = currentNode;\r
159                                 }\r
160                         }\r
161 \r
162                         // Rtrim empty spaces on block end boundary. (#3585)\r
163                         if ( element._.isBlockLike\r
164                                  && element.name != 'pre' )\r
165                         {\r
166 \r
167                                 var length = element.children.length,\r
168                                         lastChild = element.children[ length - 1 ],\r
169                                         text;\r
170                                 if ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT )\r
171                                 {\r
172                                         if ( !( text = CKEDITOR.tools.rtrim( lastChild.value ) ) )\r
173                                                 element.children.length = length -1;\r
174                                         else\r
175                                                 lastChild.value = text;\r
176                                 }\r
177                         }\r
178 \r
179                         target.add( element );\r
180 \r
181                         if ( element.returnPoint )\r
182                         {\r
183                                 currentNode = element.returnPoint;\r
184                                 delete element.returnPoint;\r
185                         }\r
186                         else\r
187                                 currentNode = moveCurrent ? target : savedCurrent;\r
188                 }\r
189 \r
190                 parser.onTagOpen = function( tagName, attributes, selfClosing, optionalClose )\r
191                 {\r
192                         var element = new CKEDITOR.htmlParser.element( tagName, attributes );\r
193 \r
194                         // "isEmpty" will be always "false" for unknown elements, so we\r
195                         // must force it if the parser has identified it as a selfClosing tag.\r
196                         if ( element.isUnknown && selfClosing )\r
197                                 element.isEmpty = true;\r
198 \r
199                         // Check for optional closed elements, including browser quirks and manually opened blocks.\r
200                         element.isOptionalClose = tagName in optionalCloseTags || optionalClose;\r
201 \r
202                         // This is a tag to be removed if empty, so do not add it immediately.\r
203                         if ( CKEDITOR.dtd.$removeEmpty[ tagName ] )\r
204                         {\r
205                                 pendingInline.push( element );\r
206                                 return;\r
207                         }\r
208                         else if ( tagName == 'pre' )\r
209                                 inPre = true;\r
210                         else if ( tagName == 'br' && inPre )\r
211                         {\r
212                                 currentNode.add( new CKEDITOR.htmlParser.text( '\n' ) );\r
213                                 return;\r
214                         }\r
215 \r
216                         if ( tagName == 'br' )\r
217                         {\r
218                                 pendingBRs.push( element );\r
219                                 return;\r
220                         }\r
221 \r
222                         while( 1 )\r
223                         {\r
224                                 var currentName = currentNode.name;\r
225 \r
226                                 var currentDtd = currentName ? ( CKEDITOR.dtd[ currentName ]\r
227                                                 || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) )\r
228                                                 : rootDtd;\r
229 \r
230                                 // If the element cannot be child of the current element.\r
231                                 if ( !element.isUnknown && !currentNode.isUnknown && !currentDtd[ tagName ] )\r
232                                 {\r
233                                         // Current node doesn't have a close tag, time for a close\r
234                                         // as this element isn't fit in. (#7497)\r
235                                         if ( currentNode.isOptionalClose )\r
236                                                 parser.onTagClose( currentName );\r
237                                         // Fixing malformed nested lists by moving it into a previous list item. (#3828)\r
238                                         else if ( tagName in listBlocks\r
239                                                 && currentName in listBlocks )\r
240                                         {\r
241                                                 var children = currentNode.children,\r
242                                                         lastChild = children[ children.length - 1 ];\r
243 \r
244                                                 // Establish the list item if it's not existed.\r
245                                                 if ( !( lastChild && lastChild.name == 'li' ) )\r
246                                                         addElement( ( lastChild = new CKEDITOR.htmlParser.element( 'li' ) ), currentNode );\r
247 \r
248                                                 !element.returnPoint && ( element.returnPoint = currentNode );\r
249                                                 currentNode = lastChild;\r
250                                         }\r
251                                         // Establish new list root for orphan list items.\r
252                                         else if ( tagName in CKEDITOR.dtd.$listItem && currentName != tagName )\r
253                                                 parser.onTagOpen( tagName == 'li' ? 'ul' : 'dl', {}, 0, 1 );\r
254                                         // We're inside a structural block like table and list, AND the incoming element\r
255                                         // is not of the same type (e.g. <td>td1<td>td2</td>), we simply add this new one before it,\r
256                                         // and most importantly, return back to here once this element is added,\r
257                                         // e.g. <table><tr><td>td1</td><p>p1</p><td>td2</td></tr></table>\r
258                                         else if ( currentName in nonBreakingBlocks && currentName != tagName )\r
259                                         {\r
260                                                 !element.returnPoint && ( element.returnPoint = currentNode );\r
261                                                 currentNode = currentNode.parent;\r
262                                         }\r
263                                         else\r
264                                         {\r
265                                                 // The current element is an inline element, which\r
266                                                 // need to be continued even after the close, so put\r
267                                                 // it in the pending list.\r
268                                                 if ( currentName in CKEDITOR.dtd.$inline )\r
269                                                         pendingInline.unshift( currentNode );\r
270 \r
271                                                 // The most common case where we just need to close the\r
272                                                 // current one and append the new one to the parent.\r
273                                                 if ( currentNode.parent )\r
274                                                         addElement( currentNode, currentNode.parent, 1 );\r
275                                                 // We've tried our best to fix the embarrassment here, while\r
276                                                 // this element still doesn't find it's parent, mark it as\r
277                                                 // orphan and show our tolerance to it.\r
278                                                 else\r
279                                                 {\r
280                                                         element.isOrphan = 1;\r
281                                                         break;\r
282                                                 }\r
283                                         }\r
284                                 }\r
285                                 else\r
286                                         break;\r
287                         }\r
288 \r
289                         checkPending( tagName );\r
290                         sendPendingBRs();\r
291 \r
292                         element.parent = currentNode;\r
293 \r
294                         if ( element.isEmpty )\r
295                                 addElement( element );\r
296                         else\r
297                                 currentNode = element;\r
298                 };\r
299 \r
300                 parser.onTagClose = function( tagName )\r
301                 {\r
302                         // Check if there is any pending tag to be closed.\r
303                         for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )\r
304                         {\r
305                                 // If found, just remove it from the list.\r
306                                 if ( tagName == pendingInline[ i ].name )\r
307                                 {\r
308                                         pendingInline.splice( i, 1 );\r
309                                         return;\r
310                                 }\r
311                         }\r
312 \r
313                         var pendingAdd = [],\r
314                                 newPendingInline = [],\r
315                                 candidate = currentNode;\r
316 \r
317                         while ( candidate != fragment && candidate.name != tagName )\r
318                         {\r
319                                 // If this is an inline element, add it to the pending list, if we're\r
320                                 // really closing one of the parents element later, they will continue\r
321                                 // after it.\r
322                                 if ( !candidate._.isBlockLike )\r
323                                         newPendingInline.unshift( candidate );\r
324 \r
325                                 // This node should be added to it's parent at this point. But,\r
326                                 // it should happen only if the closing tag is really closing\r
327                                 // one of the nodes. So, for now, we just cache it.\r
328                                 pendingAdd.push( candidate );\r
329 \r
330                                 // Make sure return point is properly restored.\r
331                                 candidate = candidate.returnPoint || candidate.parent;\r
332                         }\r
333 \r
334                         if ( candidate != fragment )\r
335                         {\r
336                                 // Add all elements that have been found in the above loop.\r
337                                 for ( i = 0 ; i < pendingAdd.length ; i++ )\r
338                                 {\r
339                                         var node = pendingAdd[ i ];\r
340                                         addElement( node, node.parent );\r
341                                 }\r
342 \r
343                                 currentNode = candidate;\r
344 \r
345                                 if ( currentNode.name == 'pre' )\r
346                                         inPre = false;\r
347 \r
348                                 if ( candidate._.isBlockLike )\r
349                                         sendPendingBRs();\r
350 \r
351                                 addElement( candidate, candidate.parent );\r
352 \r
353                                 // The parent should start receiving new nodes now, except if\r
354                                 // addElement changed the currentNode.\r
355                                 if ( candidate == currentNode )\r
356                                         currentNode = currentNode.parent;\r
357 \r
358                                 pendingInline = pendingInline.concat( newPendingInline );\r
359                         }\r
360 \r
361                         if ( tagName == 'body' )\r
362                                 fixForBody = false;\r
363                 };\r
364 \r
365                 parser.onText = function( text )\r
366                 {\r
367                         // Trim empty spaces at beginning of text contents except <pre>.\r
368                         if ( ( !currentNode._.hasInlineStarted || pendingBRs.length ) && !inPre )\r
369                         {\r
370                                 text = CKEDITOR.tools.ltrim( text );\r
371 \r
372                                 if ( text.length === 0 )\r
373                                         return;\r
374                         }\r
375 \r
376                         sendPendingBRs();\r
377                         checkPending();\r
378 \r
379                         if ( fixForBody\r
380                                  && ( !currentNode.type || currentNode.name == 'body' )\r
381                                  && CKEDITOR.tools.trim( text ) )\r
382                         {\r
383                                 this.onTagOpen( fixForBody, {}, 0, 1 );\r
384                         }\r
385 \r
386                         // Shrinking consequential spaces into one single for all elements\r
387                         // text contents.\r
388                         if ( !inPre )\r
389                                 text = text.replace( /[\t\r\n ]{2,}|[\t\r\n]/g, ' ' );\r
390 \r
391                         currentNode.add( new CKEDITOR.htmlParser.text( text ) );\r
392                 };\r
393 \r
394                 parser.onCDATA = function( cdata )\r
395                 {\r
396                         currentNode.add( new CKEDITOR.htmlParser.cdata( cdata ) );\r
397                 };\r
398 \r
399                 parser.onComment = function( comment )\r
400                 {\r
401                         sendPendingBRs();\r
402                         checkPending();\r
403                         currentNode.add( new CKEDITOR.htmlParser.comment( comment ) );\r
404                 };\r
405 \r
406                 // Parse it.\r
407                 parser.parse( fragmentHtml );\r
408 \r
409                 // Send all pending BRs except one, which we consider a unwanted bogus. (#5293)\r
410                 sendPendingBRs( !CKEDITOR.env.ie && 1 );\r
411 \r
412                 // Close all pending nodes, make sure return point is properly restored.\r
413                 while ( currentNode != fragment )\r
414                         addElement( currentNode, currentNode.parent, 1 );\r
415 \r
416                 return fragment;\r
417         };\r
418 \r
419         CKEDITOR.htmlParser.fragment.prototype =\r
420         {\r
421                 /**\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
427                  *      @param {Number} [index] From where the insertion happens.\r
428                  * @example\r
429                  */\r
430                 add : function( node, index )\r
431                 {\r
432                         isNaN( index ) && ( index = this.children.length );\r
433 \r
434                         var previous = index > 0 ? this.children[ index - 1 ] : null;\r
435                         if ( previous )\r
436                         {\r
437                                 // If the block to be appended is following text, trim spaces at\r
438                                 // the right of it.\r
439                                 if ( node._.isBlockLike && previous.type == CKEDITOR.NODE_TEXT )\r
440                                 {\r
441                                         previous.value = CKEDITOR.tools.rtrim( previous.value );\r
442 \r
443                                         // If we have completely cleared the previous node.\r
444                                         if ( previous.value.length === 0 )\r
445                                         {\r
446                                                 // Remove it from the list and add the node again.\r
447                                                 this.children.pop();\r
448                                                 this.add( node );\r
449                                                 return;\r
450                                         }\r
451                                 }\r
452 \r
453                                 previous.next = node;\r
454                         }\r
455 \r
456                         node.previous = previous;\r
457                         node.parent = this;\r
458 \r
459                         this.children.splice( index, 0, node );\r
460 \r
461                         this._.hasInlineStarted = node.type == CKEDITOR.NODE_TEXT || ( node.type == CKEDITOR.NODE_ELEMENT && !node._.isBlockLike );\r
462                 },\r
463 \r
464                 /**\r
465                  * Writes the fragment HTML to a CKEDITOR.htmlWriter.\r
466                  * @param {CKEDITOR.htmlWriter} writer The writer to which write the HTML.\r
467                  * @example\r
468                  * var writer = new CKEDITOR.htmlWriter();\r
469                  * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '&lt;P&gt;&lt;B&gt;Example' );\r
470                  * fragment.writeHtml( writer )\r
471                  * alert( writer.getHtml() );  "&lt;p&gt;&lt;b&gt;Example&lt;/b&gt;&lt;/p&gt;"\r
472                  */\r
473                 writeHtml : function( writer, filter )\r
474                 {\r
475                         var isChildrenFiltered;\r
476                         this.filterChildren = function()\r
477                         {\r
478                                 var writer = new CKEDITOR.htmlParser.basicWriter();\r
479                                 this.writeChildrenHtml.call( this, writer, filter, true );\r
480                                 var html = writer.getHtml();\r
481                                 this.children = new CKEDITOR.htmlParser.fragment.fromHtml( html ).children;\r
482                                 isChildrenFiltered = 1;\r
483                         };\r
484 \r
485                         // Filtering the root fragment before anything else.\r
486                         !this.name && filter && filter.onFragment( this );\r
487 \r
488                         this.writeChildrenHtml( writer, isChildrenFiltered ? null : filter );\r
489                 },\r
490 \r
491                 writeChildrenHtml : function( writer, filter )\r
492                 {\r
493                         for ( var i = 0 ; i < this.children.length ; i++ )\r
494                                 this.children[i].writeHtml( writer, filter );\r
495                 }\r
496         };\r
497 })();\r