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  * Represents a DOM element.
 24  * @constructor
 25  * @param {Object|String} element A native DOM element or the element name for new elements.
 26  */
 27 CKEDITOR.dom.element = function( element )
 28 {
 29 	if ( typeof element == 'string' )
 30 		element = document.createElement( element );
 31 
 32 	/**
 33 	 * The native DOM element represented by this class instance.
 34 	 * @type Object
 35 	 */
 36 	this.$ = element;
 37 };
 38 
 39 CKEDITOR.dom.element.getHead = function()
 40 {
 41 	var head = document.getElementsByTagName( 'head' )[0];
 42 	return new CKEDITOR.dom.element( head );
 43 };
 44 
 45 CKEDITOR.dom.element.prototype =
 46 {
 47 	append : function( node )
 48 	{
 49 		this.$.appendChild( node.$ );
 50 	},
 51 
 52 	appendTo : function( element )
 53 	{
 54 		element.append( this );
 55 	},
 56 	
 57 	insertAfter : function( element )
 58 	{
 59 		element.$.parentNode.insertBefore( this.$, element.$.nextSibling );
 60 	},
 61 
 62 	insertBefore : function( element )
 63 	{
 64 		element.$.parentNode.insertBefore( this.$, element.$ );
 65 	},
 66 
 67 	appendText : function( text )
 68 	{
 69 		this.$.appendChild( document.createTextNode( text ) );
 70 	},
 71 
 72 	setText : function( text )
 73 	{
 74 		this.$.text = text;
 75 	},
 76 
 77 	setHtml : function( html )
 78 	{
 79 		this.$.innerHTML = html;
 80 	},
 81 
 82 	getId : function()
 83 	{
 84 		return this.$.id || null;
 85 	},
 86 
 87 	/**
 88 	 * The value of the "name" attribute.
 89 	 * @example
 90 	 * var element = new CKEDITOR.dom.element( document.getElementsByName( 'test' )[0] );
 91 	 * alert( element.getNameAtt() == 'test' );	// true
 92 	 */
 93 	getNameAtt : function()
 94 	{
 95 		return this.$.name || null;
 96 	},
 97 
 98 	/**
 99 	 * The element name. The name is always full lowercased.
100 	 * @example
101 	 * var element = new CKEDITOR.dom.element( 'span' );
102 	 * alert( element.getName() == 'span' );	// true
103 	 */
104 	getName : function()
105 	{
106 		// Cache the lowercased name inside a closure.
107 		var nodeName = this.$.nodeName.toLowerCase();
108 
109 		return (
110 		/** @ignore */
111 		this.getName = function()
112 			{
113 				return nodeName;
114 			})();
115 	},
116 
117 	hide : function()
118 	{
119 		this.setStyle( 'display', 'none' );
120 	},
121 
122 	show : function()
123 	{
124 		this.setStyle( 'display', '' );
125 	},
126 
127 	setAttribute : function( attributeName, value )
128 	{
129 		this.$.setAttribute( attributeName, value );
130 	},
131 
132 	setAttributes : function( attributesPairs )
133 	{
134 		CKEDITOR.tools.each( attributesPairs, function( value, name )
135 			{
136 				this.$.setAttribute( name, value );
137 			}, this);
138 	},
139 
140 	setStyle : function( styleName, value )
141 	{
142 		this.$.style[ styleName ] = value;
143 	}
144 };