1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 /**
 23  * @fileOverview Defines the {@link CKEDITOR.dom.node} class, which is the base
 24  *		class for classes that represent DOM nodes.
 25  */
 26
 27 /**
 28  * Base class for classes representing DOM nodes. This constructor may return
 29  * and instance of classes that inherits this class, like
 30  * {@link CKEDITOR.dom.element} or {@link CKEDITOR.dom.text}.
 31  * @param {Object} domNode A native DOM node.
 32  * @constructor
 33  * @see CKEDITOR.dom.element
 34  * @see CKEDITOR.dom.text
 35  * @example
 36  */
 37 CKEDITOR.dom.node = function( domNode )
 38 {
 39 	if ( domNode )
 40 	{
 41 		switch ( domNode.nodeType )
 42 		{
 43 			case 1 :	// ELEMENT_NODE
 44 				return new CKEDITOR.dom.element( domNode );
 45
 46 			case 3 :	// TEXT_NODE
 47 				return new CKEDITOR.dom.text( domNode );
 48 		}
 49
 50 		/**
 51 		 * The native DOM node represented by this class instance.
 52 		 * @type Object
 53 		 * @example
 54 		 * var element = new CKEDITOR.dom.node( document.body );
 55 		 * alert( element.$.nodeType );  // "1"
 56 		 */
 57 		this.$ = domNode;
 58 	}
 59
 60 	return this;
 61 };
 62
 63 CKEDITOR.dom.node.prototype =
 64 {
 65 	/**
 66 	 * Makes this node child of another element.
 67 	 * @param {CKEDITOR.dom.element} element The target element to which append
 68 	 *		this node.
 69 	 * @returns {CKEDITOR.dom.element} The target element.
 70 	 * @example
 71 	 * var p = new CKEDITOR.dom.element( 'p' );
 72 	 * var strong = new CKEDITOR.dom.element( 'strong' );
 73 	 * strong.appendTo( p );
 74 	 *
 75 	 * // result: "<p><strong></strong></p>"
 76 	 */
 77 	appendTo : function( element )
 78 	{
 79 		element.append( this );
 80 		return element;
 81 	},
 82
 83 	/**
 84 	 * Inserts this element after a node.
 85 	 * @param {CKEDITOR.dom.node} node The that will preceed this element.
 86 	 * @returns {CKEDITOR.dom.node} The node preceeding this one after
 87 	 *		insertion.
 88 	 * @example
 89 	 * var em = new CKEDITOR.dom.element( 'em' );
 90 	 * var strong = new CKEDITOR.dom.element( 'strong' );
 91 	 * strong.insertAfter( em );
 92 	 *
 93 	 * // result: "<em></em><strong></strong>"
 94 	 */
 95 	insertAfter : function( node )
 96 	{
 97 		node.$.parentNode.insertBefore( this.$, node.$.nextSibling );
 98 		return node;
 99 	},
100
101 	/**
102 	 * Inserts this element before a node.
103 	 * @param {CKEDITOR.dom.node} node The that will be after this element.
104 	 * @returns {CKEDITOR.dom.node} The node after this one after insertion.
105 	 * @example
106 	 * var em = new CKEDITOR.dom.element( 'em' );
107 	 * var strong = new CKEDITOR.dom.element( 'strong' );
108 	 * strong.insertBefore( em );
109 	 *
110 	 * // result: "<strong></strong><em></em>"
111 	 */
112 	insertBefore : function( node )
113 	{
114 		node.$.parentNode.insertBefore( this.$, node.$ );
115 	}
116 };
117