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 appendText : function( text ) 58 { 59 this.$.appendChild( document.createTextNode( text ) ); 60 }, 61 62 setText : function( text ) 63 { 64 this.$.text = text; 65 }, 66 67 getId : function() 68 { 69 var id = this.$.id; 70 return ( id && id.length > 0 ) ? id : null; 71 }, 72 73 /** 74 * The value of the "name" attribute. 75 * @example 76 * var element = new CKEDITOR.dom.element( document.getElementsByName( 'test' )[0] ); 77 * alert( element.getNameAtt() == 'test' ); // true 78 */ 79 getNameAtt : function() 80 { 81 var name = this.$.name 82 return ( name && name.length > 0 ) ? name : null; 83 }, 84 85 /** 86 * The element name. The name is always full lowercased. 87 * @example 88 * var element = new CKEDITOR.dom.element( 'span' ); 89 * alert( element.getName() == 'span' ); // true 90 */ 91 getName : function() 92 { 93 // Cache the lowercased name inside a closure. 94 var nodeName = this.$.nodeName.toLowerCase(); 95 96 97 return ( 98 /** @ignore */ 99 this.getName = function() 100 { 101 return nodeName; 102 })(); 103 }, 104 105 hide : function() 106 { 107 this.setStyle( 'display', 'none' ); 108 }, 109 110 show : function() 111 { 112 this.setStyle( 'display', '' ); 113 }, 114 115 setAttribute : function( attributeName, value ) 116 { 117 this.$.setAttribute( attributeName, value ); 118 }, 119 120 setAttributes : function( attributesPairs ) 121 { 122 CKEDITOR.tools.each( attributesPairs, function( value, name ) 123 { 124 this.$.setAttribute( name, value ); 125 }, this); 126 }, 127 128 setStyle : function( styleName, value ) 129 { 130 this.$.style[ styleName ] = value; 131 } 132 };