2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.dom.node} class, which is the base
8 * class for classes that represent DOM nodes.
12 * Base class for classes representing DOM nodes. This constructor may return
13 * and instance of classes that inherits this class, like
14 * {@link CKEDITOR.dom.element} or {@link CKEDITOR.dom.text}.
15 * @augments CKEDITOR.dom.domObject
16 * @param {Object} domNode A native DOM node.
18 * @see CKEDITOR.dom.element
19 * @see CKEDITOR.dom.text
22 CKEDITOR.dom.node = function( domNode )
26 switch ( domNode.nodeType )
28 // Safari don't consider document as element node type. (#3389)
29 case CKEDITOR.NODE_DOCUMENT :
30 return new CKEDITOR.dom.document( domNode );
32 case CKEDITOR.NODE_ELEMENT :
33 return new CKEDITOR.dom.element( domNode );
35 case CKEDITOR.NODE_TEXT :
36 return new CKEDITOR.dom.text( domNode );
39 // Call the base constructor.
40 CKEDITOR.dom.domObject.call( this, domNode );
46 CKEDITOR.dom.node.prototype = new CKEDITOR.dom.domObject();
53 CKEDITOR.NODE_ELEMENT = 1;
60 CKEDITOR.NODE_DOCUMENT = 9;
67 CKEDITOR.NODE_TEXT = 3;
74 CKEDITOR.NODE_COMMENT = 8;
76 CKEDITOR.NODE_DOCUMENT_FRAGMENT = 11;
78 CKEDITOR.POSITION_IDENTICAL = 0;
79 CKEDITOR.POSITION_DISCONNECTED = 1;
80 CKEDITOR.POSITION_FOLLOWING = 2;
81 CKEDITOR.POSITION_PRECEDING = 4;
82 CKEDITOR.POSITION_IS_CONTAINED = 8;
83 CKEDITOR.POSITION_CONTAINS = 16;
85 CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
86 /** @lends CKEDITOR.dom.node.prototype */
89 * Makes this node child of another element.
90 * @param {CKEDITOR.dom.element} element The target element to which append
92 * @returns {CKEDITOR.dom.element} The target element.
94 * var p = new CKEDITOR.dom.element( 'p' );
95 * var strong = new CKEDITOR.dom.element( 'strong' );
96 * strong.appendTo( p );
98 * // result: "<p><strong></strong></p>"
100 appendTo : function( element, toStart )
102 element.append( this, toStart );
106 clone : function( includeChildren, cloneId )
108 var $clone = this.$.cloneNode( includeChildren );
110 var removeIds = function( node )
112 if ( node.nodeType != CKEDITOR.NODE_ELEMENT )
116 node.removeAttribute( 'id', false );
117 node.removeAttribute( 'data-cke-expando', false );
119 if ( includeChildren )
121 var childs = node.childNodes;
122 for ( var i=0; i < childs.length; i++ )
123 removeIds( childs[ i ] );
127 // The "id" attribute should never be cloned to avoid duplication.
130 return new CKEDITOR.dom.node( $clone );
133 hasPrevious : function()
135 return !!this.$.previousSibling;
140 return !!this.$.nextSibling;
144 * Inserts this element after a node.
145 * @param {CKEDITOR.dom.node} node The that will preceed this element.
146 * @returns {CKEDITOR.dom.node} The node preceeding this one after
149 * var em = new CKEDITOR.dom.element( 'em' );
150 * var strong = new CKEDITOR.dom.element( 'strong' );
151 * strong.insertAfter( em );
153 * // result: "<em></em><strong></strong>"
155 insertAfter : function( node )
157 node.$.parentNode.insertBefore( this.$, node.$.nextSibling );
162 * Inserts this element before a node.
163 * @param {CKEDITOR.dom.node} node The that will be after this element.
164 * @returns {CKEDITOR.dom.node} The node being inserted.
166 * var em = new CKEDITOR.dom.element( 'em' );
167 * var strong = new CKEDITOR.dom.element( 'strong' );
168 * strong.insertBefore( em );
170 * // result: "<strong></strong><em></em>"
172 insertBefore : function( node )
174 node.$.parentNode.insertBefore( this.$, node.$ );
178 insertBeforeMe : function( node )
180 this.$.parentNode.insertBefore( node.$, this.$ );
185 * Retrieves a uniquely identifiable tree address for this node.
186 * The tree address returns is an array of integers, with each integer
187 * indicating a child index of a DOM node, starting from
188 * document.documentElement.
190 * For example, assuming <body> is the second child from <html> (<head>
191 * being the first), and we'd like to address the third child under the
192 * fourth child of body, the tree address returned would be:
195 * The tree address cannot be used for finding back the DOM tree node once
196 * the DOM tree structure has been modified.
198 getAddress : function( normalized )
201 var $documentElement = this.getDocument().$.documentElement;
204 while ( node && node != $documentElement )
206 var parentNode = node.parentNode;
207 var currentIndex = -1;
211 for ( var i = 0 ; i < parentNode.childNodes.length ; i++ )
213 var candidate = parentNode.childNodes[i];
216 candidate.nodeType == 3 &&
217 candidate.previousSibling &&
218 candidate.previousSibling.nodeType == 3 )
225 if ( candidate == node )
229 address.unshift( currentIndex );
239 * Gets the document containing this element.
240 * @returns {CKEDITOR.dom.document} The document.
242 * var element = CKEDITOR.document.getById( 'example' );
243 * alert( <b>element.getDocument().equals( CKEDITOR.document )</b> ); // "true"
245 getDocument : function()
247 return new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument );
250 getIndex : function()
254 var currentNode = $.parentNode && $.parentNode.firstChild;
255 var currentIndex = -1;
257 while ( currentNode )
261 if ( currentNode == $ )
264 currentNode = currentNode.nextSibling;
270 getNextSourceNode : function( startFromSibling, nodeType, guard )
272 // If "guard" is a node, transform it in a function.
273 if ( guard && !guard.call )
275 var guardNode = guard;
276 guard = function( node )
278 return !node.equals( guardNode );
282 var node = ( !startFromSibling && this.getFirst && this.getFirst() ),
285 // Guarding when we're skipping the current element( no children or 'startFromSibling' ).
286 // send the 'moving out' signal even we don't actually dive into.
289 if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )
291 node = this.getNext();
294 while ( !node && ( parent = ( parent || this ).getParent() ) )
296 // The guard check sends the "true" paramenter to indicate that
297 // we are moving "out" of the element.
298 if ( guard && guard( parent, true ) === false )
301 node = parent.getNext();
307 if ( guard && guard( node ) === false )
310 if ( nodeType && nodeType != node.type )
311 return node.getNextSourceNode( false, nodeType, guard );
316 getPreviousSourceNode : function( startFromSibling, nodeType, guard )
318 if ( guard && !guard.call )
320 var guardNode = guard;
321 guard = function( node )
323 return !node.equals( guardNode );
327 var node = ( !startFromSibling && this.getLast && this.getLast() ),
330 // Guarding when we're skipping the current element( no children or 'startFromSibling' ).
331 // send the 'moving out' signal even we don't actually dive into.
334 if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )
336 node = this.getPrevious();
339 while ( !node && ( parent = ( parent || this ).getParent() ) )
341 // The guard check sends the "true" paramenter to indicate that
342 // we are moving "out" of the element.
343 if ( guard && guard( parent, true ) === false )
346 node = parent.getPrevious();
352 if ( guard && guard( node ) === false )
355 if ( nodeType && node.type != nodeType )
356 return node.getPreviousSourceNode( false, nodeType, guard );
361 getPrevious : function( evaluator )
363 var previous = this.$, retval;
366 previous = previous.previousSibling;
367 retval = previous && new CKEDITOR.dom.node( previous );
369 while ( retval && evaluator && !evaluator( retval ) )
374 * Gets the node that follows this element in its parent's child list.
375 * @param {Function} evaluator Filtering the result node.
376 * @returns {CKEDITOR.dom.node} The next node or null if not available.
378 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b> <i>next</i></div>' );
379 * var first = <b>element.getFirst().getNext()</b>;
380 * alert( first.getName() ); // "i"
382 getNext : function( evaluator )
384 var next = this.$, retval;
387 next = next.nextSibling;
388 retval = next && new CKEDITOR.dom.node( next );
390 while ( retval && evaluator && !evaluator( retval ) )
395 * Gets the parent element for this node.
396 * @returns {CKEDITOR.dom.element} The parent element.
398 * var node = editor.document.getBody().getFirst();
399 * var parent = node.<b>getParent()</b>;
400 * alert( node.getName() ); // "body"
402 getParent : function()
404 var parent = this.$.parentNode;
405 return ( parent && parent.nodeType == 1 ) ? new CKEDITOR.dom.node( parent ) : null;
408 getParents : function( closerFirst )
415 parents[ closerFirst ? 'push' : 'unshift' ]( node );
417 while ( ( node = node.getParent() ) )
422 getCommonAncestor : function( node )
424 if ( node.equals( this ) )
427 if ( node.contains && node.contains( this ) )
430 var start = this.contains ? this : this.getParent();
434 if ( start.contains( node ) )
437 while ( ( start = start.getParent() ) );
442 getPosition : function( otherNode )
445 var $other = otherNode.$;
447 if ( $.compareDocumentPosition )
448 return $.compareDocumentPosition( $other );
450 // IE and Safari have no support for compareDocumentPosition.
453 return CKEDITOR.POSITION_IDENTICAL;
455 // Only element nodes support contains and sourceIndex.
456 if ( this.type == CKEDITOR.NODE_ELEMENT && otherNode.type == CKEDITOR.NODE_ELEMENT )
460 if ( $.contains( $other ) )
461 return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING;
463 if ( $other.contains( $ ) )
464 return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;
467 if ( 'sourceIndex' in $ )
469 return ( $.sourceIndex < 0 || $other.sourceIndex < 0 ) ? CKEDITOR.POSITION_DISCONNECTED :
470 ( $.sourceIndex < $other.sourceIndex ) ? CKEDITOR.POSITION_PRECEDING :
471 CKEDITOR.POSITION_FOLLOWING;
475 // For nodes that don't support compareDocumentPosition, contains
476 // or sourceIndex, their "address" is compared.
478 var addressOfThis = this.getAddress(),
479 addressOfOther = otherNode.getAddress(),
480 minLevel = Math.min( addressOfThis.length, addressOfOther.length );
482 // Determinate preceed/follow relationship.
483 for ( var i = 0 ; i <= minLevel - 1 ; i++ )
485 if ( addressOfThis[ i ] != addressOfOther[ i ] )
489 return addressOfThis[ i ] < addressOfOther[ i ] ?
490 CKEDITOR.POSITION_PRECEDING : CKEDITOR.POSITION_FOLLOWING;
496 // Determinate contains/contained relationship.
497 return ( addressOfThis.length < addressOfOther.length ) ?
498 CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING :
499 CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;
503 * Gets the closes ancestor node of a specified node name.
504 * @param {String} name Node name of ancestor node.
505 * @param {Boolean} includeSelf (Optional) Whether to include the current
506 * node in the calculation or not.
507 * @returns {CKEDITOR.dom.node} Ancestor node.
509 getAscendant : function( name, includeSelf )
518 if ( $.nodeName && $.nodeName.toLowerCase() == name )
519 return new CKEDITOR.dom.node( $ );
526 hasAscendant : function( name, includeSelf )
535 if ( $.nodeName && $.nodeName.toLowerCase() == name )
543 move : function( target, toStart )
545 target.append( this.remove(), toStart );
549 * Removes this node from the document DOM.
550 * @param {Boolean} [preserveChildren] Indicates that the children
551 * elements must remain in the document, removing only the outer
554 * var element = CKEDITOR.dom.element.getById( 'MyElement' );
555 * <b>element.remove()</b>;
557 remove : function( preserveChildren )
560 var parent = $.parentNode;
564 if ( preserveChildren )
566 // Move all children before the node.
567 for ( var child ; ( child = $.firstChild ) ; )
569 parent.insertBefore( $.removeChild( child ), $ );
573 parent.removeChild( $ );
579 replace : function( nodeToReplace )
581 this.insertBefore( nodeToReplace );
582 nodeToReplace.remove();
594 while ( this.getFirst && ( child = this.getFirst() ) )
596 if ( child.type == CKEDITOR.NODE_TEXT )
598 var trimmed = CKEDITOR.tools.ltrim( child.getText() ),
599 originalLength = child.getLength();
606 else if ( trimmed.length < originalLength )
608 child.split( originalLength - trimmed.length );
610 // IE BUG: child.remove() may raise JavaScript errors here. (#81)
611 this.$.removeChild( this.$.firstChild );
621 while ( this.getLast && ( child = this.getLast() ) )
623 if ( child.type == CKEDITOR.NODE_TEXT )
625 var trimmed = CKEDITOR.tools.rtrim( child.getText() ),
626 originalLength = child.getLength();
633 else if ( trimmed.length < originalLength )
635 child.split( trimmed.length );
637 // IE BUG: child.getNext().remove() may raise JavaScript errors here.
639 this.$.lastChild.parentNode.removeChild( this.$.lastChild );
645 if ( !CKEDITOR.env.ie && !CKEDITOR.env.opera )
647 child = this.$.lastChild;
649 if ( child && child.type == 1 && child.nodeName.toLowerCase() == 'br' )
651 // Use "eChildNode.parentNode" instead of "node" to avoid IE bug (#324).
652 child.parentNode.removeChild( child ) ;
658 * Checks is this node is read-only (should not be changed). It
659 * additionaly returns the element, if any, which defines the read-only
660 * state of this node. It may be the node itself or any of its parent
662 * @returns {CKEDITOR.dom.element|Boolean} An element containing
663 * read-only attributes or "false" if none is found.
666 * // For the following HTML:
667 * // <div contenteditable="false">Some <b>text</b></div>
669 * // If "ele" is the above <div>
670 * ele.isReadOnly(); // the <div> element
672 * // If "ele" is the above <b>
673 * ele.isReadOnly(); // the <div> element
675 isReadOnly : function()
680 if ( current.type == CKEDITOR.NODE_ELEMENT )
682 if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) )
685 if ( current.getAttribute( 'contentEditable' ) == 'false' )
687 else if ( current.getAttribute( 'contentEditable' ) == 'true' )
690 current = current.getParent();