X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Fnode.js;h=b05b4a7aa6fb0cd880b4c82b9500c0825da34de6;hb=4e90e78dc97789709ee7404359a5517540c27553;hp=30df0a192a85dcef2249d97514c93eee1d0a33e4;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/core/dom/node.js b/_source/core/dom/node.js index 30df0a1..b05b4a7 100644 --- a/_source/core/dom/node.js +++ b/_source/core/dom/node.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -25,6 +25,10 @@ CKEDITOR.dom.node = function( domNode ) { switch ( domNode.nodeType ) { + // Safari don't consider document as element node type. (#3389) + case CKEDITOR.NODE_DOCUMENT : + return new CKEDITOR.dom.document( domNode ); + case CKEDITOR.NODE_ELEMENT : return new CKEDITOR.dom.element( domNode ); @@ -49,6 +53,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 @@ -96,24 +107,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 ); }, @@ -192,29 +204,15 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, while ( node && node != $documentElement ) { var parentNode = node.parentNode; - var currentIndex = -1; - for ( var i = 0 ; i < parentNode.childNodes.length ; i++ ) + if ( parentNode ) { - var candidate = parentNode.childNodes[i]; - - if ( normalized && - candidate.nodeType == 3 && - candidate.previousSibling && - candidate.previousSibling.nodeType == 3 ) - { - continue; - } - - currentIndex++; - - if ( candidate == node ) - break; + // Get the node index. For performance, call getIndex + // directly, instead of creating a new node object. + address.unshift( this.getIndex.call( { $ : node }, normalized ) ); } - address.unshift( currentIndex ); - - node = node.parentNode; + node = parentNode; } return address; @@ -229,34 +227,31 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, */ getDocument : function() { - var document = new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument ); - - return ( - /** @ignore */ - 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 ) @@ -492,11 +487,18 @@ 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 node name. + * @param {String} name The node name of the ancestor node to search. + * @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. + * @example + * // Suppose we have the following HTML: + * // <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> */ getAscendant : function( name, includeSelf ) { @@ -644,6 +646,45 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype, child.parentNode.removeChild( child ) ; } } + }, + + /** + * Checks is this node is read-only (should not be changed). It + * additionaly returns the element, if any, which defines the read-only + * state of this node. It may be the node itself or any of its parent + * nodes. + * @returns {CKEDITOR.dom.element|Boolean} An element containing + * read-only attributes or "false" if none is found. + * @since 3.5 + * @example + * // For the following HTML: + * // <div contenteditable="false">Some <b>text</b></div> + * + * // If "ele" is the above <div> + * ele.isReadOnly(); // the <div> element + * + * // If "ele" is the above <b> + * ele.isReadOnly(); // the <div> element + */ + isReadOnly : function() + { + var current = this; + while( current ) + { + if ( current.type == CKEDITOR.NODE_ELEMENT ) + { + if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) ) + break; + + if ( current.getAttribute( 'contentEditable' ) == 'false' ) + return current; + else if ( current.getAttribute( 'contentEditable' ) == 'true' ) + break; + } + current = current.getParent(); + } + + return false; } } );