+++ /dev/null
-/*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
-For licensing, see LICENSE.html or http://ckeditor.com/license\r
-*/\r
-\r
-/**\r
- * @fileOverview Defines the {@link CKEDITOR.dom.node} class which is the base\r
- * class for classes that represent DOM nodes.\r
- */\r
-\r
-/**\r
- * Base class for classes representing DOM nodes. This constructor may return\r
- * an instance of a class that inherits from this class, like\r
- * {@link CKEDITOR.dom.element} or {@link CKEDITOR.dom.text}.\r
- * @augments CKEDITOR.dom.domObject\r
- * @param {Object} domNode A native DOM node.\r
- * @constructor\r
- * @see CKEDITOR.dom.element\r
- * @see CKEDITOR.dom.text\r
- * @example\r
- */\r
-CKEDITOR.dom.node = function( domNode )\r
-{\r
- if ( domNode )\r
- {\r
- switch ( domNode.nodeType )\r
- {\r
- // Safari don't consider document as element node type. (#3389)\r
- case CKEDITOR.NODE_DOCUMENT :\r
- return new CKEDITOR.dom.document( domNode );\r
-\r
- case CKEDITOR.NODE_ELEMENT :\r
- return new CKEDITOR.dom.element( domNode );\r
-\r
- case CKEDITOR.NODE_TEXT :\r
- return new CKEDITOR.dom.text( domNode );\r
- }\r
-\r
- // Call the base constructor.\r
- CKEDITOR.dom.domObject.call( this, domNode );\r
- }\r
-\r
- return this;\r
-};\r
-\r
-CKEDITOR.dom.node.prototype = new CKEDITOR.dom.domObject();\r
-\r
-/**\r
- * Element node type.\r
- * @constant\r
- * @example\r
- */\r
-CKEDITOR.NODE_ELEMENT = 1;\r
-\r
-/**\r
- * Document node type.\r
- * @constant\r
- * @example\r
- */\r
-CKEDITOR.NODE_DOCUMENT = 9;\r
-\r
-/**\r
- * Text node type.\r
- * @constant\r
- * @example\r
- */\r
-CKEDITOR.NODE_TEXT = 3;\r
-\r
-/**\r
- * Comment node type.\r
- * @constant\r
- * @example\r
- */\r
-CKEDITOR.NODE_COMMENT = 8;\r
-\r
-CKEDITOR.NODE_DOCUMENT_FRAGMENT = 11;\r
-\r
-CKEDITOR.POSITION_IDENTICAL = 0;\r
-CKEDITOR.POSITION_DISCONNECTED = 1;\r
-CKEDITOR.POSITION_FOLLOWING = 2;\r
-CKEDITOR.POSITION_PRECEDING = 4;\r
-CKEDITOR.POSITION_IS_CONTAINED = 8;\r
-CKEDITOR.POSITION_CONTAINS = 16;\r
-\r
-CKEDITOR.tools.extend( CKEDITOR.dom.node.prototype,\r
- /** @lends CKEDITOR.dom.node.prototype */\r
- {\r
- /**\r
- * Makes this node a child of another element.\r
- * @param {CKEDITOR.dom.element} element The target element to which\r
- * this node will be appended.\r
- * @returns {CKEDITOR.dom.element} The target element.\r
- * @example\r
- * var p = new CKEDITOR.dom.element( 'p' );\r
- * var strong = new CKEDITOR.dom.element( 'strong' );\r
- * strong.appendTo( p );\r
- *\r
- * // result: "<p><strong></strong></p>"\r
- */\r
- appendTo : function( element, toStart )\r
- {\r
- element.append( this, toStart );\r
- return element;\r
- },\r
-\r
- clone : function( includeChildren, cloneId )\r
- {\r
- var $clone = this.$.cloneNode( includeChildren );\r
-\r
- var removeIds = function( node )\r
- {\r
- if ( node.nodeType != CKEDITOR.NODE_ELEMENT )\r
- return;\r
-\r
- if ( !cloneId )\r
- node.removeAttribute( 'id', false );\r
- node.removeAttribute( 'data-cke-expando', false );\r
-\r
- if ( includeChildren )\r
- {\r
- var childs = node.childNodes;\r
- for ( var i=0; i < childs.length; i++ )\r
- removeIds( childs[ i ] );\r
- }\r
- };\r
-\r
- // The "id" attribute should never be cloned to avoid duplication.\r
- removeIds( $clone );\r
-\r
- return new CKEDITOR.dom.node( $clone );\r
- },\r
-\r
- hasPrevious : function()\r
- {\r
- return !!this.$.previousSibling;\r
- },\r
-\r
- hasNext : function()\r
- {\r
- return !!this.$.nextSibling;\r
- },\r
-\r
- /**\r
- * Inserts this element after a node.\r
- * @param {CKEDITOR.dom.node} node The node that will precede this element.\r
- * @returns {CKEDITOR.dom.node} The node preceding this one after\r
- * insertion.\r
- * @example\r
- * var em = new CKEDITOR.dom.element( 'em' );\r
- * var strong = new CKEDITOR.dom.element( 'strong' );\r
- * strong.insertAfter( em );\r
- *\r
- * // result: "<em></em><strong></strong>"\r
- */\r
- insertAfter : function( node )\r
- {\r
- node.$.parentNode.insertBefore( this.$, node.$.nextSibling );\r
- return node;\r
- },\r
-\r
- /**\r
- * Inserts this element before a node.\r
- * @param {CKEDITOR.dom.node} node The node that will succeed this element.\r
- * @returns {CKEDITOR.dom.node} The node being inserted.\r
- * @example\r
- * var em = new CKEDITOR.dom.element( 'em' );\r
- * var strong = new CKEDITOR.dom.element( 'strong' );\r
- * strong.insertBefore( em );\r
- *\r
- * // result: "<strong></strong><em></em>"\r
- */\r
- insertBefore : function( node )\r
- {\r
- node.$.parentNode.insertBefore( this.$, node.$ );\r
- return node;\r
- },\r
-\r
- insertBeforeMe : function( node )\r
- {\r
- this.$.parentNode.insertBefore( node.$, this.$ );\r
- return node;\r
- },\r
-\r
- /**\r
- * Retrieves a uniquely identifiable tree address for this node.\r
- * The tree address returned is an array of integers, with each integer\r
- * indicating a child index of a DOM node, starting from\r
- * <code>document.documentElement</code>.\r
- *\r
- * For example, assuming <code><body></code> is the second child\r
- * of <code><html></code> (<code><head></code> being the first),\r
- * and we would like to address the third child under the\r
- * fourth child of <code><body></code>, the tree address returned would be:\r
- * [1, 3, 2]\r
- *\r
- * The tree address cannot be used for finding back the DOM tree node once\r
- * the DOM tree structure has been modified.\r
- */\r
- getAddress : function( normalized )\r
- {\r
- var address = [];\r
- var $documentElement = this.getDocument().$.documentElement;\r
- var node = this.$;\r
-\r
- while ( node && node != $documentElement )\r
- {\r
- var parentNode = node.parentNode;\r
-\r
- if ( parentNode )\r
- {\r
- // Get the node index. For performance, call getIndex\r
- // directly, instead of creating a new node object.\r
- address.unshift( this.getIndex.call( { $ : node }, normalized ) );\r
- }\r
-\r
- node = parentNode;\r
- }\r
-\r
- return address;\r
- },\r
-\r
- /**\r
- * Gets the document containing this element.\r
- * @returns {CKEDITOR.dom.document} The document.\r
- * @example\r
- * var element = CKEDITOR.document.getById( 'example' );\r
- * alert( <strong>element.getDocument().equals( CKEDITOR.document )</strong> ); // "true"\r
- */\r
- getDocument : function()\r
- {\r
- return new CKEDITOR.dom.document( this.$.ownerDocument || this.$.parentNode.ownerDocument );\r
- },\r
-\r
- getIndex : function( normalized )\r
- {\r
- // Attention: getAddress depends on this.$\r
-\r
- var current = this.$,\r
- index = 0;\r
-\r
- while ( ( current = current.previousSibling ) )\r
- {\r
- // When normalizing, do not count it if this is an\r
- // empty text node or if it's a text node following another one.\r
- if ( normalized && current.nodeType == 3 &&\r
- ( !current.nodeValue.length ||\r
- ( current.previousSibling && current.previousSibling.nodeType == 3 ) ) )\r
- {\r
- continue;\r
- }\r
-\r
- index++;\r
- }\r
-\r
- return index;\r
- },\r
-\r
- getNextSourceNode : function( startFromSibling, nodeType, guard )\r
- {\r
- // If "guard" is a node, transform it in a function.\r
- if ( guard && !guard.call )\r
- {\r
- var guardNode = guard;\r
- guard = function( node )\r
- {\r
- return !node.equals( guardNode );\r
- };\r
- }\r
-\r
- var node = ( !startFromSibling && this.getFirst && this.getFirst() ),\r
- parent;\r
-\r
- // Guarding when we're skipping the current element( no children or 'startFromSibling' ).\r
- // send the 'moving out' signal even we don't actually dive into.\r
- if ( !node )\r
- {\r
- if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )\r
- return null;\r
- node = this.getNext();\r
- }\r
-\r
- while ( !node && ( parent = ( parent || this ).getParent() ) )\r
- {\r
- // The guard check sends the "true" paramenter to indicate that\r
- // we are moving "out" of the element.\r
- if ( guard && guard( parent, true ) === false )\r
- return null;\r
-\r
- node = parent.getNext();\r
- }\r
-\r
- if ( !node )\r
- return null;\r
-\r
- if ( guard && guard( node ) === false )\r
- return null;\r
-\r
- if ( nodeType && nodeType != node.type )\r
- return node.getNextSourceNode( false, nodeType, guard );\r
-\r
- return node;\r
- },\r
-\r
- getPreviousSourceNode : function( startFromSibling, nodeType, guard )\r
- {\r
- if ( guard && !guard.call )\r
- {\r
- var guardNode = guard;\r
- guard = function( node )\r
- {\r
- return !node.equals( guardNode );\r
- };\r
- }\r
-\r
- var node = ( !startFromSibling && this.getLast && this.getLast() ),\r
- parent;\r
-\r
- // Guarding when we're skipping the current element( no children or 'startFromSibling' ).\r
- // send the 'moving out' signal even we don't actually dive into.\r
- if ( !node )\r
- {\r
- if ( this.type == CKEDITOR.NODE_ELEMENT && guard && guard( this, true ) === false )\r
- return null;\r
- node = this.getPrevious();\r
- }\r
-\r
- while ( !node && ( parent = ( parent || this ).getParent() ) )\r
- {\r
- // The guard check sends the "true" paramenter to indicate that\r
- // we are moving "out" of the element.\r
- if ( guard && guard( parent, true ) === false )\r
- return null;\r
-\r
- node = parent.getPrevious();\r
- }\r
-\r
- if ( !node )\r
- return null;\r
-\r
- if ( guard && guard( node ) === false )\r
- return null;\r
-\r
- if ( nodeType && node.type != nodeType )\r
- return node.getPreviousSourceNode( false, nodeType, guard );\r
-\r
- return node;\r
- },\r
-\r
- getPrevious : function( evaluator )\r
- {\r
- var previous = this.$, retval;\r
- do\r
- {\r
- previous = previous.previousSibling;\r
- retval = previous && new CKEDITOR.dom.node( previous );\r
- }\r
- while ( retval && evaluator && !evaluator( retval ) )\r
- return retval;\r
- },\r
-\r
- /**\r
- * Gets the node that follows this element in its parent's child list.\r
- * @param {Function} evaluator Filtering the result node.\r
- * @returns {CKEDITOR.dom.node} The next node or null if not available.\r
- * @example\r
- * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b> <i>next</i></div>' );\r
- * var first = <strong>element.getFirst().getNext()</strong>;\r
- * alert( first.getName() ); // "i"\r
- */\r
- getNext : function( evaluator )\r
- {\r
- var next = this.$, retval;\r
- do\r
- {\r
- next = next.nextSibling;\r
- retval = next && new CKEDITOR.dom.node( next );\r
- }\r
- while ( retval && evaluator && !evaluator( retval ) )\r
- return retval;\r
- },\r
-\r
- /**\r
- * Gets the parent element for this node.\r
- * @returns {CKEDITOR.dom.element} The parent element.\r
- * @example\r
- * var node = editor.document.getBody().getFirst();\r
- * var parent = node.<strong>getParent()</strong>;\r
- * alert( node.getName() ); // "body"\r
- */\r
- getParent : function()\r
- {\r
- var parent = this.$.parentNode;\r
- return ( parent && parent.nodeType == 1 ) ? new CKEDITOR.dom.node( parent ) : null;\r
- },\r
-\r
- getParents : function( closerFirst )\r
- {\r
- var node = this;\r
- var parents = [];\r
-\r
- do\r
- {\r
- parents[ closerFirst ? 'push' : 'unshift' ]( node );\r
- }\r
- while ( ( node = node.getParent() ) )\r
-\r
- return parents;\r
- },\r
-\r
- getCommonAncestor : function( node )\r
- {\r
- if ( node.equals( this ) )\r
- return this;\r
-\r
- if ( node.contains && node.contains( this ) )\r
- return node;\r
-\r
- var start = this.contains ? this : this.getParent();\r
-\r
- do\r
- {\r
- if ( start.contains( node ) )\r
- return start;\r
- }\r
- while ( ( start = start.getParent() ) );\r
-\r
- return null;\r
- },\r
-\r
- getPosition : function( otherNode )\r
- {\r
- var $ = this.$;\r
- var $other = otherNode.$;\r
-\r
- if ( $.compareDocumentPosition )\r
- return $.compareDocumentPosition( $other );\r
-\r
- // IE and Safari have no support for compareDocumentPosition.\r
-\r
- if ( $ == $other )\r
- return CKEDITOR.POSITION_IDENTICAL;\r
-\r
- // Only element nodes support contains and sourceIndex.\r
- if ( this.type == CKEDITOR.NODE_ELEMENT && otherNode.type == CKEDITOR.NODE_ELEMENT )\r
- {\r
- if ( $.contains )\r
- {\r
- if ( $.contains( $other ) )\r
- return CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING;\r
-\r
- if ( $other.contains( $ ) )\r
- return CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;\r
- }\r
-\r
- if ( 'sourceIndex' in $ )\r
- {\r
- return ( $.sourceIndex < 0 || $other.sourceIndex < 0 ) ? CKEDITOR.POSITION_DISCONNECTED :\r
- ( $.sourceIndex < $other.sourceIndex ) ? CKEDITOR.POSITION_PRECEDING :\r
- CKEDITOR.POSITION_FOLLOWING;\r
- }\r
- }\r
-\r
- // For nodes that don't support compareDocumentPosition, contains\r
- // or sourceIndex, their "address" is compared.\r
-\r
- var addressOfThis = this.getAddress(),\r
- addressOfOther = otherNode.getAddress(),\r
- minLevel = Math.min( addressOfThis.length, addressOfOther.length );\r
-\r
- // Determinate preceed/follow relationship.\r
- for ( var i = 0 ; i <= minLevel - 1 ; i++ )\r
- {\r
- if ( addressOfThis[ i ] != addressOfOther[ i ] )\r
- {\r
- if ( i < minLevel )\r
- {\r
- return addressOfThis[ i ] < addressOfOther[ i ] ?\r
- CKEDITOR.POSITION_PRECEDING : CKEDITOR.POSITION_FOLLOWING;\r
- }\r
- break;\r
- }\r
- }\r
-\r
- // Determinate contains/contained relationship.\r
- return ( addressOfThis.length < addressOfOther.length ) ?\r
- CKEDITOR.POSITION_CONTAINS + CKEDITOR.POSITION_PRECEDING :\r
- CKEDITOR.POSITION_IS_CONTAINED + CKEDITOR.POSITION_FOLLOWING;\r
- },\r
-\r
- /**\r
- * Gets the closest ancestor node of this node, specified by its name.\r
- * @param {String} reference The name of the ancestor node to search or\r
- * an object with the node names to search for.\r
- * @param {Boolean} [includeSelf] Whether to include the current\r
- * node in the search.\r
- * @returns {CKEDITOR.dom.node} The located ancestor node or null if not found.\r
- * @since 3.6.1\r
- * @example\r
- * // Suppose we have the following HTML structure:\r
- * // <div id="outer"><div id="inner"><p><b>Some text</b></p></div></div>\r
- * // If node == <b>\r
- * ascendant = node.getAscendant( 'div' ); // ascendant == <div id="inner">\r
- * ascendant = node.getAscendant( 'b' ); // ascendant == null\r
- * ascendant = node.getAscendant( 'b', true ); // ascendant == <b>\r
- * ascendant = node.getAscendant( { div: 1, p: 1} ); // Searches for the first 'div' or 'p': ascendant == <div id="inner">\r
- */\r
- getAscendant : function( reference, includeSelf )\r
- {\r
- var $ = this.$,\r
- name;\r
-\r
- if ( !includeSelf )\r
- $ = $.parentNode;\r
-\r
- while ( $ )\r
- {\r
- if ( $.nodeName && ( name = $.nodeName.toLowerCase(), ( typeof reference == 'string' ? name == reference : name in reference ) ) )\r
- return new CKEDITOR.dom.node( $ );\r
-\r
- $ = $.parentNode;\r
- }\r
- return null;\r
- },\r
-\r
- hasAscendant : function( name, includeSelf )\r
- {\r
- var $ = this.$;\r
-\r
- if ( !includeSelf )\r
- $ = $.parentNode;\r
-\r
- while ( $ )\r
- {\r
- if ( $.nodeName && $.nodeName.toLowerCase() == name )\r
- return true;\r
-\r
- $ = $.parentNode;\r
- }\r
- return false;\r
- },\r
-\r
- move : function( target, toStart )\r
- {\r
- target.append( this.remove(), toStart );\r
- },\r
-\r
- /**\r
- * Removes this node from the document DOM.\r
- * @param {Boolean} [preserveChildren] Indicates that the children\r
- * elements must remain in the document, removing only the outer\r
- * tags.\r
- * @example\r
- * var element = CKEDITOR.dom.element.getById( 'MyElement' );\r
- * <strong>element.remove()</strong>;\r
- */\r
- remove : function( preserveChildren )\r
- {\r
- var $ = this.$;\r
- var parent = $.parentNode;\r
-\r
- if ( parent )\r
- {\r
- if ( preserveChildren )\r
- {\r
- // Move all children before the node.\r
- for ( var child ; ( child = $.firstChild ) ; )\r
- {\r
- parent.insertBefore( $.removeChild( child ), $ );\r
- }\r
- }\r
-\r
- parent.removeChild( $ );\r
- }\r
-\r
- return this;\r
- },\r
-\r
- replace : function( nodeToReplace )\r
- {\r
- this.insertBefore( nodeToReplace );\r
- nodeToReplace.remove();\r
- },\r
-\r
- trim : function()\r
- {\r
- this.ltrim();\r
- this.rtrim();\r
- },\r
-\r
- ltrim : function()\r
- {\r
- var child;\r
- while ( this.getFirst && ( child = this.getFirst() ) )\r
- {\r
- if ( child.type == CKEDITOR.NODE_TEXT )\r
- {\r
- var trimmed = CKEDITOR.tools.ltrim( child.getText() ),\r
- originalLength = child.getLength();\r
-\r
- if ( !trimmed )\r
- {\r
- child.remove();\r
- continue;\r
- }\r
- else if ( trimmed.length < originalLength )\r
- {\r
- child.split( originalLength - trimmed.length );\r
-\r
- // IE BUG: child.remove() may raise JavaScript errors here. (#81)\r
- this.$.removeChild( this.$.firstChild );\r
- }\r
- }\r
- break;\r
- }\r
- },\r
-\r
- rtrim : function()\r
- {\r
- var child;\r
- while ( this.getLast && ( child = this.getLast() ) )\r
- {\r
- if ( child.type == CKEDITOR.NODE_TEXT )\r
- {\r
- var trimmed = CKEDITOR.tools.rtrim( child.getText() ),\r
- originalLength = child.getLength();\r
-\r
- if ( !trimmed )\r
- {\r
- child.remove();\r
- continue;\r
- }\r
- else if ( trimmed.length < originalLength )\r
- {\r
- child.split( trimmed.length );\r
-\r
- // IE BUG: child.getNext().remove() may raise JavaScript errors here.\r
- // (#81)\r
- this.$.lastChild.parentNode.removeChild( this.$.lastChild );\r
- }\r
- }\r
- break;\r
- }\r
-\r
- if ( !CKEDITOR.env.ie && !CKEDITOR.env.opera )\r
- {\r
- child = this.$.lastChild;\r
-\r
- if ( child && child.type == 1 && child.nodeName.toLowerCase() == 'br' )\r
- {\r
- // Use "eChildNode.parentNode" instead of "node" to avoid IE bug (#324).\r
- child.parentNode.removeChild( child ) ;\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Checks if this node is read-only (should not be changed). Additionally\r
- * it returns the element that defines the read-only state of this node\r
- * (if present). It may be the node itself or any of its parent\r
- * nodes.\r
- * @returns {CKEDITOR.dom.element|Boolean} An element containing\r
- * read-only attributes or "false" if none is found.\r
- * @since 3.5\r
- * @example\r
- * // For the following HTML:\r
- * // <div contenteditable="false">Some <b>text</b></div>\r
- *\r
- * // If "ele" is the above <div>\r
- * ele.isReadOnly(); // the <div> element\r
- *\r
- * // If "ele" is the above <b>\r
- * ele.isReadOnly(); // the <div> element\r
- */\r
- isReadOnly : function()\r
- {\r
- var current = this;\r
- while( current )\r
- {\r
- if ( current.type == CKEDITOR.NODE_ELEMENT )\r
- {\r
- if ( current.is( 'body' ) || !!current.data( 'cke-editable' ) )\r
- break;\r
-\r
- if ( current.getAttribute( 'contentEditable' ) == 'false' )\r
- return current;\r
- else if ( current.getAttribute( 'contentEditable' ) == 'true' )\r
- break;\r
- }\r
- current = current.getParent();\r
- }\r
-\r
- return false;\r
- }\r
- }\r
-);\r