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.getById = function( id )
 40 {
 41 	var $ = document.getElementById( id );
 42 	return $ ? new CKEDITOR.dom.element( $ ) : null;
 43 };
 44
 45 CKEDITOR.dom.element.getHead = function()
 46 {
 47 	var head = document.getElementsByTagName( 'head' )[0];
 48 	return new CKEDITOR.dom.element( head );
 49 };
 50
 51 CKEDITOR.dom.element.createFromHtml = function( html )
 52 {
 53 	var temp = document.createElement( 'div' );
 54 	temp.innerHTML = html;
 55 	return new CKEDITOR.dom.element( temp.firstChild );
 56 };
 57
 58 CKEDITOR.dom.element.prototype =
 59 {
 60 	append : function( node )
 61 	{
 62 		this.$.appendChild( node.$ );
 63 	},
 64
 65 	appendTo : function( element )
 66 	{
 67 		element.append( this );
 68 	},
 69
 70 	insertAfter : function( element )
 71 	{
 72 		element.$.parentNode.insertBefore( this.$, element.$.nextSibling );
 73 	},
 74
 75 	insertBefore : function( element )
 76 	{
 77 		element.$.parentNode.insertBefore( this.$, element.$ );
 78 	},
 79
 80 	appendText : function( text )
 81 	{
 82 		this.$.appendChild( document.createTextNode( text ) );
 83 	},
 84
 85 	setText : function( text )
 86 	{
 87 		this.$.text = text;
 88 	},
 89
 90 	setHtml : function( html )
 91 	{
 92 		this.$.innerHTML = html;
 93 	},
 94
 95 	getId : function()
 96 	{
 97 		return this.$.id || null;
 98 	},
 99
100 	/**
101 	 * The value of the "name" attribute.
102 	 * @example
103 	 * var element = new CKEDITOR.dom.element( document.getElementsByName( 'test' )[0] );
104 	 * alert( element.getNameAtt() == 'test' );	// true
105 	 */
106 	getNameAtt : function()
107 	{
108 		return this.$.name || null;
109 	},
110
111 	/**
112 	 * The element name. The name is always full lowercased.
113 	 * @example
114 	 * var element = new CKEDITOR.dom.element( 'span' );
115 	 * alert( element.getName() == 'span' );	// true
116 	 */
117 	getName : function()
118 	{
119 		// Cache the lowercased name inside a closure.
120 		var nodeName = this.$.nodeName.toLowerCase();
121
122 		return (
123 		/** @ignore */
124 		this.getName = function()
125 			{
126 				return nodeName;
127 			})();
128 	},
129
130 	hide : function()
131 	{
132 		this.setStyle( 'display', 'none' );
133 	},
134
135 	show : function()
136 	{
137 		this.setStyle( 'display', '' );
138 	},
139
140 	setAttribute : function( attributeName, value )
141 	{
142 		this.$.setAttribute( attributeName, value );
143 	},
144
145 	setAttributes : function( attributesPairs )
146 	{
147 		CKEDITOR.tools.each( attributesPairs, function( value, name )
148 			{
149 				this.$.setAttribute( name, value );
150 			}, this);
151 	},
152
153 	setStyle : function( styleName, value )
154 	{
155 		this.$.style[ styleName ] = value;
156 	}
157 };
158