source: trunk/admin/inc/ckeditor/_source/core/htmlparser/fragment.js@ 239

Last change on this file since 239 was 239, checked in by luc, 9 years ago

Admin: correzione visulaizzazione immissione dati spoglio per Chrome e Safari - Aggiornamento dell'editor da FCKeditor a CKeditor , accessibili anche a Chrome e Safari.

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