X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=_source%2Fcore%2Fdom%2Fnode.js;h=6a4cbdef8cdb627f59a73d1ce038dc3e14aa0c37;hb=refs%2Ftags%2Fv3.6.2;hp=02a726c5e08de168937ee694432c92f4758c66f8;hpb=e371ddf8abcb89013e20e6d0dd746adec344d0e5;p=ckeditor.git
diff --git a/_source/core/dom/node.js b/_source/core/dom/node.js
index 02a726c..6a4cbde 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-2011, 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.
@@ -86,9 +86,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' );
@@ -107,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 );
},
@@ -141,8 +142,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' );
@@ -159,7 +160,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' );
@@ -182,13 +183,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
<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
@@ -203,29 +205,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;
@@ -239,37 +224,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 )
@@ -381,7 +364,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 )
@@ -401,7 +384,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()
@@ -505,22 +488,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;
@@ -557,7 +551,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
* tags.
* @example
* var element = CKEDITOR.dom.element.getById( 'MyElement' );
- * element.remove();
+ * element.remove();
*/
remove : function( preserveChildren )
{
@@ -659,25 +653,44 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
}
},
+ /**
+ * 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 current = this;
- while( current )
+ 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
{
- if ( current.type == CKEDITOR.NODE_ELEMENT )
+ // Degrade for old browsers which don't support "isContentEditable", e.g. FF3
+ var current = element;
+ while( current )
{
- if ( current.is( 'body' ) || current.getCustomData( '_cke_notReadOnly' ) )
+ if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) )
break;
if ( current.getAttribute( 'contentEditable' ) == 'false' )
- return current;
+ return true;
else if ( current.getAttribute( 'contentEditable' ) == 'true' )
break;
+
+ current = current.getParent();
}
- current = current.getParent();
- }
- return false;
+ return false;
+ }
}
}
);