X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Fnode.js;h=07bdbfc6af20c00622bac0105905fed3823122fa;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=2ae3a134c112f8183a49457dbf9c1b9f65d4baee;hpb=059b4c2fef02528bf1af189f7996e80652faddfb;p=ckeditor.git
diff --git a/_source/core/dom/node.js b/_source/core/dom/node.js
index 2ae3a13..07bdbfc 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-2013, 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,21 +23,13 @@ CKEDITOR.dom.node = function( domNode )
{
if ( 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 );
-
- 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;
@@ -86,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' );
@@ -107,24 +99,26 @@ 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;
+
+ if ( !cloneId )
+ node.removeAttribute( 'id', false );
- node.removeAttribute( 'id', false ) ;
- node.removeAttribute( '_cke_expando', false ) ;
+ node[ 'data-cke-expando' ] = undefined;
+ 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 +135,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 +153,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 +176,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 +198,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 +217,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 )
@@ -369,7 +345,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;
@@ -381,7 +360,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 +380,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 +484,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 +547,7 @@ CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,
* tags.
* @example
* var element = CKEDITOR.dom.element.getById( 'MyElement' );
- * element.remove();
+ * element.remove();
*/
remove : function( preserveChildren )
{
@@ -657,6 +647,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;
+ }
}
}
);