X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Fnode.js;h=90412e4b48ee06608419f853109d0c10fdfcbcbf;hb=2f22c0c38f17e75be5541089076885442aaa2377;hp=a4188dcd17b81c685faa035588c8797fef13145e;hpb=c6e377a02b54abc07129d72b632763c727476a15;p=ckeditor.git diff --git a/_source/core/dom/node.js b/_source/core/dom/node.js index a4188dc..90412e4 100644 --- a/_source/core/dom/node.js +++ b/_source/core/dom/node.js @@ -1,16 +1,16 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ /** - * @fileOverview Defines the {@link CKEDITOR.dom.node} class, which is the base + * @fileOverview Defines the {@link CKEDITOR.dom.node} class which is the base * class for classes that represent DOM nodes. */ /** * Base class for classes representing DOM nodes. This constructor may return - * and instance of classes that inherits this class, like + * an instance of a class that inherits from this class, like * {@link CKEDITOR.dom.element} or {@link CKEDITOR.dom.text}. * @augments CKEDITOR.dom.domObject * @param {Object} domNode A native DOM node. @@ -23,17 +23,13 @@ CKEDITOR.dom.node = function( domNode ) { if ( domNode ) { - switch ( domNode.nodeType ) - { - case CKEDITOR.NODE_ELEMENT : - return new CKEDITOR.dom.element( domNode ); - - case CKEDITOR.NODE_TEXT : - return new CKEDITOR.dom.text( domNode ); - } + var type = domNode.nodeType == CKEDITOR.NODE_DOCUMENT ? 'document' + : domNode.nodeType == CKEDITOR.NODE_ELEMENT ? 'element' + : domNode.nodeType == CKEDITOR.NODE_TEXT ? 'text' + : domNode.nodeType == CKEDITOR.NODE_COMMENT ? 'comment' + : 'domObject'; // Call the base constructor otherwise. - // Call the base constructor. - CKEDITOR.dom.domObject.call( this, domNode ); + return new CKEDITOR.dom[ type ]( domNode ); } return this; @@ -49,6 +45,13 @@ CKEDITOR.dom.node.prototype = new CKEDITOR.dom.domObject(); CKEDITOR.NODE_ELEMENT = 1; /** + * Document node type. + * @constant + * @example + */ +CKEDITOR.NODE_DOCUMENT = 9; + +/** * Text node type. * @constant * @example @@ -75,9 +78,9 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, /** @lends CKEDITOR.dom.node.prototype */ { /** - * Makes this node child of another element. - * @param {CKEDITOR.dom.element} element The target element to which append - * this node. + * Makes this node a child of another element. + * @param {CKEDITOR.dom.element} element The target element to which + * this node will be appended. * @returns {CKEDITOR.dom.element} The target element. * @example * var p = new CKEDITOR.dom.element( 'p' ); @@ -96,24 +99,25 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, { var $clone = this.$.cloneNode( includeChildren ); - if ( !cloneId ) + var removeIds = function( node ) { - var removeIds = function( node ) - { - if ( node.nodeType != CKEDITOR.NODE_ELEMENT ) - return; + if ( node.nodeType != CKEDITOR.NODE_ELEMENT ) + return; - node.removeAttribute( 'id', false ) ; - node.removeAttribute( '_cke_expando', false ) ; + if ( !cloneId ) + node.removeAttribute( 'id', false ); + node.removeAttribute( 'data-cke-expando', false ); + if ( includeChildren ) + { var childs = node.childNodes; - for ( var i=0 ; i < childs.length ; i++ ) + for ( var i=0; i < childs.length; i++ ) removeIds( childs[ i ] ); - }; + } + }; - // The "id" attribute should never be cloned to avoid duplication. - removeIds( $clone ); - } + // The "id" attribute should never be cloned to avoid duplication. + removeIds( $clone ); return new CKEDITOR.dom.node( $clone ); }, @@ -130,8 +134,8 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, /** * Inserts this element after a node. - * @param {CKEDITOR.dom.node} node The that will preceed this element. - * @returns {CKEDITOR.dom.node} The node preceeding this one after + * @param {CKEDITOR.dom.node} node The node that will precede this element. + * @returns {CKEDITOR.dom.node} The node preceding this one after * insertion. * @example * var em = new CKEDITOR.dom.element( 'em' ); @@ -148,7 +152,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, /** * Inserts this element before a node. - * @param {CKEDITOR.dom.node} node The that will be after this element. + * @param {CKEDITOR.dom.node} node The node that will succeed this element. * @returns {CKEDITOR.dom.node} The node being inserted. * @example * var em = new CKEDITOR.dom.element( 'em' ); @@ -171,13 +175,14 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, /** * Retrieves a uniquely identifiable tree address for this node. - * The tree address returns is an array of integers, with each integer + * The tree address returned is an array of integers, with each integer * indicating a child index of a DOM node, starting from - * document.documentElement. + * document.documentElement. * - * For example, assuming is the second child from ( - * being the first), and we'd like to address the third child under the - * fourth child of body, the tree address returned would be: + * For example, assuming <body> is the second child + * of <html> (<head> being the first), + * and we would like to address the third child under the + * fourth child of <body>, the tree address returned would be: * [1, 3, 2] * * The tree address cannot be used for finding back the DOM tree node once @@ -192,29 +197,12 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, while ( node && node != $documentElement ) { var parentNode = node.parentNode; - var currentIndex = -1; if ( parentNode ) { - for ( var i = 0 ; i < parentNode.childNodes.length ; i++ ) - { - var candidate = parentNode.childNodes[i]; - - if ( normalized && - candidate.nodeType == 3 && - candidate.previousSibling && - candidate.previousSibling.nodeType == 3 ) - { - continue; - } - - currentIndex++; - - if ( candidate == node ) - break; - } - - address.unshift( currentIndex ); + // Get the node index. For performance, call getIndex + // directly, instead of creating a new node object. + address.unshift( this.getIndex.call( { $ : node }, normalized ) ); } node = parentNode; @@ -228,37 +216,35 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, * @returns {CKEDITOR.dom.document} The document. * @example * var element = CKEDITOR.document.getById( 'example' ); - * alert( element.getDocument().equals( CKEDITOR.document ) ); // "true" + * alert( element.getDocument().equals( CKEDITOR.document ) ); // "true" */ getDocument : function() { - var document = new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument ); - - return ( - this.getDocument = function() - { - return document; - })(); + return new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument ); }, - getIndex : function() + getIndex : function( normalized ) { - var $ = this.$; + // Attention: getAddress depends on this.$ - var currentNode = $.parentNode && $.parentNode.firstChild; - var currentIndex = -1; + var current = this.$, + index = 0; - while ( currentNode ) + while ( ( current = current.previousSibling ) ) { - currentIndex++; - - if ( currentNode == $ ) - return currentIndex; + // When normalizing, do not count it if this is an + // empty text node or if it's a text node following another one. + if ( normalized && current.nodeType == 3 && + ( !current.nodeValue.length || + ( current.previousSibling && current.previousSibling.nodeType == 3 ) ) ) + { + continue; + } - currentNode = currentNode.nextSibling; + index++; } - return -1; + return index; }, getNextSourceNode : function( startFromSibling, nodeType, guard ) @@ -358,7 +344,10 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, do { previous = previous.previousSibling; - retval = previous && new CKEDITOR.dom.node( previous ); + + // Avoid returning the doc type node. + // http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-412266927 + retval = previous && previous.nodeType != 10 && new CKEDITOR.dom.node( previous ); } while ( retval && evaluator && !evaluator( retval ) ) return retval; @@ -370,7 +359,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, * @returns {CKEDITOR.dom.node} The next node or null if not available. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b> <i>next</i></div>' ); - * var first = element.getFirst().getNext(); + * var first = element.getFirst().getNext(); * alert( first.getName() ); // "i" */ getNext : function( evaluator ) @@ -390,7 +379,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, * @returns {CKEDITOR.dom.element} The parent element. * @example * var node = editor.document.getBody().getFirst(); - * var parent = node.getParent(); + * var parent = node.getParent(); * alert( node.getName() ); // "body" */ getParent : function() @@ -494,22 +483,33 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, }, /** - * Gets the closes ancestor node of a specified node name. - * @param {String} name Node name of ancestor node. - * @param {Boolean} includeSelf (Optional) Whether to include the current - * node in the calculation or not. - * @returns {CKEDITOR.dom.node} Ancestor node. + * Gets the closest ancestor node of this node, specified by its name. + * @param {String} reference The name of the ancestor node to search or + * an object with the node names to search for. + * @param {Boolean} [includeSelf] Whether to include the current + * node in the search. + * @returns {CKEDITOR.dom.node} The located ancestor node or null if not found. + * @since 3.6.1 + * @example + * // Suppose we have the following HTML structure: + * // <div id="outer"><div id="inner"><p><b>Some text</b></p></div></div> + * // If node == <b> + * ascendant = node.getAscendant( 'div' ); // ascendant == <div id="inner"> + * ascendant = node.getAscendant( 'b' ); // ascendant == null + * ascendant = node.getAscendant( 'b', true ); // ascendant == <b> + * ascendant = node.getAscendant( { div: 1, p: 1} ); // Searches for the first 'div' or 'p': ascendant == <div id="inner"> */ - getAscendant : function( name, includeSelf ) + getAscendant : function( reference, includeSelf ) { - var $ = this.$; + var $ = this.$, + name; if ( !includeSelf ) $ = $.parentNode; while ( $ ) { - if ( $.nodeName && $.nodeName.toLowerCase() == name ) + if ( $.nodeName && ( name = $.nodeName.toLowerCase(), ( typeof reference == 'string' ? name == reference : name in reference ) ) ) return new CKEDITOR.dom.node( $ ); $ = $.parentNode; @@ -546,7 +546,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, * tags. * @example * var element = CKEDITOR.dom.element.getById( 'MyElement' ); - * element.remove(); + * element.remove(); */ remove : function( preserveChildren ) { @@ -646,6 +646,46 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, child.parentNode.removeChild( child ) ; } } + }, + + /** + * Checks if this node is read-only (should not be changed). + * @returns {Boolean} + * @since 3.5 + * @example + * // For the following HTML: + * // <div contenteditable="false">Some <b>text</b></div> + * + * // If "ele" is the above <div> + * ele.isReadOnly(); // true + */ + isReadOnly : function() + { + var element = this; + if ( this.type != CKEDITOR.NODE_ELEMENT ) + element = this.getParent(); + + if ( element && typeof element.$.isContentEditable != 'undefined' ) + return ! ( element.$.isContentEditable || element.data( 'cke-editable' ) ); + else + { + // Degrade for old browsers which don't support "isContentEditable", e.g. FF3 + var current = element; + while( current ) + { + if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) ) + break; + + if ( current.getAttribute( 'contentEditable' ) == 'false' ) + return true; + else if ( current.getAttribute( 'contentEditable' ) == 'true' ) + break; + + current = current.getParent(); + } + + return false; + } } } );