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 * @fileOverview Defines the {@link CKEDITOR.dom.node} class, which is the base
\r
8 * class for classes that represent DOM nodes.
\r
12 * Base class for classes representing DOM nodes. This constructor may return
\r
13 * and instance of classes that inherits this class, like
\r
14 * {@link CKEDITOR.dom.element} or {@link CKEDITOR.dom.text}.
\r
15 * @augments CKEDITOR.dom.domObject
\r
16 * @param {Object} domNode A native DOM node.
\r
18 * @see CKEDITOR.dom.element
\r
19 * @see CKEDITOR.dom.text
\r
22 CKEDITOR.dom.node = function( domNode )
\r
26 switch ( domNode.nodeType )
\r
28 // Safari don't consider document as element node type. (#3389)
\r
29 case CKEDITOR.NODE_DOCUMENT :
\r
30 return new CKEDITOR.dom.document( domNode );
\r
32 case CKEDITOR.NODE_ELEMENT :
\r
33 return new CKEDITOR.dom.element( domNode );
\r
35 case CKEDITOR.NODE_TEXT :
\r
36 return new CKEDITOR.dom.text( domNode );
\r
39 // Call the base constructor.
\r
40 CKEDITOR.dom.domObject.call( this, domNode );
\r
46 CKEDITOR.dom.node.prototype = new CKEDITOR.dom.domObject();
\r
49 * Element node type.
\r
53 CKEDITOR.NODE_ELEMENT = 1;
\r
56 * Document node type.
\r
60 CKEDITOR.NODE_DOCUMENT = 9;
\r
67 CKEDITOR.NODE_TEXT = 3;
\r
70 * Comment node type.
\r
74 CKEDITOR.NODE_COMMENT = 8;
\r
76 CKEDITOR.NODE_DOCUMENT_FRAGMENT = 11;
\r
78 CKEDITOR.POSITION_IDENTICAL = 0;
\r
79 CKEDITOR.POSITION_DISCONNECTED = 1;
\r
80 CKEDITOR.POSITION_FOLLOWING = 2;
\r
81 CKEDITOR.POSITION_PRECEDING = 4;
\r
82 CKEDITOR.POSITION_IS_CONTAINED = 8;
\r
83 CKEDITOR.POSITION_CONTAINS = 16;
\r
85 CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
\r
86 /** @lends CKEDITOR.dom.node.prototype */
\r
89 * Makes this node child of another element.
\r
90 * @param {CKEDITOR.dom.element} element The target element to which append
\r
92 * @returns {CKEDITOR.dom.element} The target element.
\r
94 * var p = new CKEDITOR.dom.element( 'p' );
\r
95 * var strong = new CKEDITOR.dom.element( 'strong' );
\r
96 * strong.appendTo( p );
\r
98 * // result: "<p><strong></strong></p>"
\r
100 appendTo : function( element, toStart )
\r
102 element.append( this, toStart );
\r
106 clone : function( includeChildren, cloneId )
\r
108 var $clone = this.$.cloneNode( includeChildren );
\r
112 var removeIds = function( node )
\r
114 if ( node.nodeType != CKEDITOR.NODE_ELEMENT )
\r
117 node.removeAttribute( 'id', false ) ;
\r
118 node.removeAttribute( '_cke_expando', false ) ;
\r
120 var childs = node.childNodes;
\r
121 for ( var i=0 ; i < childs.length ; i++ )
\r
122 removeIds( childs[ i ] );
\r
125 // The "id" attribute should never be cloned to avoid duplication.
\r
126 removeIds( $clone );
\r
129 return new CKEDITOR.dom.node( $clone );
\r
132 hasPrevious : function()
\r
134 return !!this.$.previousSibling;
\r
137 hasNext : function()
\r
139 return !!this.$.nextSibling;
\r
143 * Inserts this element after a node.
\r
144 * @param {CKEDITOR.dom.node} node The that will preceed this element.
\r
145 * @returns {CKEDITOR.dom.node} The node preceeding this one after
\r
148 * var em = new CKEDITOR.dom.element( 'em' );
\r
149 * var strong = new CKEDITOR.dom.element( 'strong' );
\r
150 * strong.insertAfter( em );
\r
152 * // result: "<em></em><strong></strong>"
\r
154 insertAfter : function( node )
\r
156 node.$.parentNode.insertBefore( this.$, node.$.nextSibling );
\r
161 * Inserts this element before a node.
\r
162 * @param {CKEDITOR.dom.node} node The that will be after this element.
\r
163 * @returns {CKEDITOR.dom.node} The node being inserted.
\r
165 * var em = new CKEDITOR.dom.element( 'em' );
\r
166 * var strong = new CKEDITOR.dom.element( 'strong' );
\r
167 * strong.insertBefore( em );
\r
169 * // result: "<strong></strong><em></em>"
\r
171 insertBefore : function( node )
\r
173 node.$.parentNode.insertBefore( this.$, node.$ );
\r
177 insertBeforeMe : function( node )
\r
179 this.$.parentNode.insertBefore( node.$, this.$ );
\r
184 * Retrieves a uniquely identifiable tree address for this node.
\r
185 * The tree address returns is an array of integers, with each integer
\r
186 * indicating a child index of a DOM node, starting from
\r
187 * document.documentElement.
\r
189 * For example, assuming <body> is the second child from <html> (<head>
\r
190 * being the first), and we'd like to address the third child under the
\r
191 * fourth child of body, the tree address returned would be:
\r
194 * The tree address cannot be used for finding back the DOM tree node once
\r
195 * the DOM tree structure has been modified.
\r
197 getAddress : function( normalized )
\r
200 var $documentElement = this.getDocument().$.documentElement;
\r
203 while ( node && node != $documentElement )
\r
205 var parentNode = node.parentNode;
\r
206 var currentIndex = -1;
\r
210 for ( var i = 0 ; i < parentNode.childNodes.length ; i++ )
\r
212 var candidate = parentNode.childNodes[i];
\r
215 candidate.nodeType == 3 &&
\r
216 candidate.previousSibling &&
\r
217 candidate.previousSibling.nodeType == 3 )
\r
224 if ( candidate == node )
\r
228 address.unshift( currentIndex );
\r
238 * Gets the document containing this element.
\r
239 * @returns {CKEDITOR.dom.document} The document.
\r
241 * var element = CKEDITOR.document.getById( 'example' );
\r
242 * alert( <b>element.getDocument().equals( CKEDITOR.document )</b> ); // "true"
\r
244 getDocument : function()
\r
246 var document = new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument );
\r
249 this.getDocument = function()
\r
255 getIndex : function()
\r
259 var currentNode = $.parentNode && $.parentNode.firstChild;
\r
260 var currentIndex = -1;
\r
262 while ( currentNode )
\r
266 if ( currentNode == $ )
\r
267 return currentIndex;
\r
269 currentNode = currentNode.nextSibling;
\r
275 getNextSourceNode : function( startFromSibling, nodeType, guard )
\r
277 // If "guard" is a node, transform it in a function.
\r
278 if ( guard && !guard.call )
\r
280 var guardNode = guard;
\r
281 guard = function( node )
\r
283 return !node.equals( guardNode );
\r
287 var node = ( !startFromSibling && this.getFirst && this.getFirst() ),
\r
290 // Guarding when we're skipping the current element( no children or 'startFromSibling' ).
\r
291 // send the 'moving out' signal even we don't actually dive into.
\r
294 if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )
\r
296 node = this.getNext();
\r
299 while ( !node && ( parent = ( parent || this ).getParent() ) )
\r
301 // The guard check sends the "true" paramenter to indicate that
\r
302 // we are moving "out" of the element.
\r
303 if ( guard && guard( parent, true ) === false )
\r
306 node = parent.getNext();
\r
312 if ( guard && guard( node ) === false )
\r
315 if ( nodeType && nodeType != node.type )
\r
316 return node.getNextSourceNode( false, nodeType, guard );
\r
321 getPreviousSourceNode : function( startFromSibling, nodeType, guard )
\r
323 if ( guard && !guard.call )
\r
325 var guardNode = guard;
\r
326 guard = function( node )
\r
328 return !node.equals( guardNode );
\r
332 var node = ( !startFromSibling && this.getLast && this.getLast() ),
\r
335 // Guarding when we're skipping the current element( no children or 'startFromSibling' ).
\r
336 // send the 'moving out' signal even we don't actually dive into.
\r
339 if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )
\r
341 node = this.getPrevious();
\r
344 while ( !node && ( parent = ( parent || this ).getParent() ) )
\r
346 // The guard check sends the "true" paramenter to indicate that
\r
347 // we are moving "out" of the element.
\r
348 if ( guard && guard( parent, true ) === false )
\r
351 node = parent.getPrevious();
\r
357 if ( guard && guard( node ) === false )
\r
360 if ( nodeType && node.type != nodeType )
\r
361 return node.getPreviousSourceNode( false, nodeType, guard );
\r
366 getPrevious : function( evaluator )
\r
368 var previous = this.$, retval;
\r
371 previous = previous.previousSibling;
\r
372 retval = previous && new CKEDITOR.dom.node( previous );
\r
374 while ( retval && evaluator && !evaluator( retval ) )
\r
379 * Gets the node that follows this element in its parent's child list.
\r
380 * @param {Function} evaluator Filtering the result node.
\r
381 * @returns {CKEDITOR.dom.node} The next node or null if not available.
\r
383 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b> <i>next</i></div>' );
\r
384 * var first = <b>element.getFirst().getNext()</b>;
\r
385 * alert( first.getName() ); // "i"
\r
387 getNext : function( evaluator )
\r
389 var next = this.$, retval;
\r
392 next = next.nextSibling;
\r
393 retval = next && new CKEDITOR.dom.node( next );
\r
395 while ( retval && evaluator && !evaluator( retval ) )
\r
400 * Gets the parent element for this node.
\r
401 * @returns {CKEDITOR.dom.element} The parent element.
\r
403 * var node = editor.document.getBody().getFirst();
\r
404 * var parent = node.<b>getParent()</b>;
\r
405 * alert( node.getName() ); // "body"
\r
407 getParent : function()
\r
409 var parent = this.$.parentNode;
\r
410 return ( parent && parent.nodeType == 1 ) ? new CKEDITOR.dom.node( parent ) : null;
\r
413 getParents : function( closerFirst )
\r
420 parents[ closerFirst ? 'push' : 'unshift' ]( node );
\r
422 while ( ( node = node.getParent() ) )
\r
427 getCommonAncestor : function( node )
\r
429 if ( node.equals( this ) )
\r
432 if ( node.contains && node.contains( this ) )
\r
435 var start = this.contains ? this : this.getParent();
\r
439 if ( start.contains( node ) )
\r
442 while ( ( start = start.getParent() ) );
\r
447 getPosition : function( otherNode )
\r
450 var $other = otherNode.$;
\r
452 if ( $.compareDocumentPosition )
\r
453 return $.compareDocumentPosition( $other );
\r
455 // IE and Safari have no support for compareDocumentPosition.
\r
458 return CKEDITOR.POSITION_IDENTICAL;
\r
460 // Only element nodes support contains and sourceIndex.
\r
461 if ( this.type == CKEDITOR.NODE_ELEMENT && otherNode.type == CKEDITOR.NODE_ELEMENT )
\r
465 if ( $.contains( $other ) )
\r
466 return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING;
\r
468 if ( $other.contains( $ ) )
\r
469 return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;
\r
472 if ( 'sourceIndex' in $ )
\r
474 return ( $.sourceIndex < 0 || $other.sourceIndex < 0 ) ? CKEDITOR.POSITION_DISCONNECTED :
\r
475 ( $.sourceIndex < $other.sourceIndex ) ? CKEDITOR.POSITION_PRECEDING :
\r
476 CKEDITOR.POSITION_FOLLOWING;
\r
480 // For nodes that don't support compareDocumentPosition, contains
\r
481 // or sourceIndex, their "address" is compared.
\r
483 var addressOfThis = this.getAddress(),
\r
484 addressOfOther = otherNode.getAddress(),
\r
485 minLevel = Math.min( addressOfThis.length, addressOfOther.length );
\r
487 // Determinate preceed/follow relationship.
\r
488 for ( var i = 0 ; i <= minLevel - 1 ; i++ )
\r
490 if ( addressOfThis[ i ] != addressOfOther[ i ] )
\r
492 if ( i < minLevel )
\r
494 return addressOfThis[ i ] < addressOfOther[ i ] ?
\r
495 CKEDITOR.POSITION_PRECEDING : CKEDITOR.POSITION_FOLLOWING;
\r
501 // Determinate contains/contained relationship.
\r
502 return ( addressOfThis.length < addressOfOther.length ) ?
\r
503 CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING :
\r
504 CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;
\r
508 * Gets the closes ancestor node of a specified node name.
\r
509 * @param {String} name Node name of ancestor node.
\r
510 * @param {Boolean} includeSelf (Optional) Whether to include the current
\r
511 * node in the calculation or not.
\r
512 * @returns {CKEDITOR.dom.node} Ancestor node.
\r
514 getAscendant : function( name, includeSelf )
\r
518 if ( !includeSelf )
\r
523 if ( $.nodeName && $.nodeName.toLowerCase() == name )
\r
524 return new CKEDITOR.dom.node( $ );
\r
531 hasAscendant : function( name, includeSelf )
\r
535 if ( !includeSelf )
\r
540 if ( $.nodeName && $.nodeName.toLowerCase() == name )
\r
548 move : function( target, toStart )
\r
550 target.append( this.remove(), toStart );
\r
554 * Removes this node from the document DOM.
\r
555 * @param {Boolean} [preserveChildren] Indicates that the children
\r
556 * elements must remain in the document, removing only the outer
\r
559 * var element = CKEDITOR.dom.element.getById( 'MyElement' );
\r
560 * <b>element.remove()</b>;
\r
562 remove : function( preserveChildren )
\r
565 var parent = $.parentNode;
\r
569 if ( preserveChildren )
\r
571 // Move all children before the node.
\r
572 for ( var child ; ( child = $.firstChild ) ; )
\r
574 parent.insertBefore( $.removeChild( child ), $ );
\r
578 parent.removeChild( $ );
\r
584 replace : function( nodeToReplace )
\r
586 this.insertBefore( nodeToReplace );
\r
587 nodeToReplace.remove();
\r
599 while ( this.getFirst && ( child = this.getFirst() ) )
\r
601 if ( child.type == CKEDITOR.NODE_TEXT )
\r
603 var trimmed = CKEDITOR.tools.ltrim( child.getText() ),
\r
604 originalLength = child.getLength();
\r
611 else if ( trimmed.length < originalLength )
\r
613 child.split( originalLength - trimmed.length );
\r
615 // IE BUG: child.remove() may raise JavaScript errors here. (#81)
\r
616 this.$.removeChild( this.$.firstChild );
\r
626 while ( this.getLast && ( child = this.getLast() ) )
\r
628 if ( child.type == CKEDITOR.NODE_TEXT )
\r
630 var trimmed = CKEDITOR.tools.rtrim( child.getText() ),
\r
631 originalLength = child.getLength();
\r
638 else if ( trimmed.length < originalLength )
\r
640 child.split( trimmed.length );
\r
642 // IE BUG: child.getNext().remove() may raise JavaScript errors here.
\r
644 this.$.lastChild.parentNode.removeChild( this.$.lastChild );
\r
650 if ( !CKEDITOR.env.ie && !CKEDITOR.env.opera )
\r
652 child = this.$.lastChild;
\r
654 if ( child && child.type == 1 && child.nodeName.toLowerCase() == 'br' )
\r
656 // Use "eChildNode.parentNode" instead of "node" to avoid IE bug (#324).
\r
657 child.parentNode.removeChild( child ) ;
\r