/* Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ /** * @fileOverview Defines the {@link CKEDITOR.dom.element} class, which * represents a DOM element. */ /** * Represents a DOM element. * @constructor * @augments CKEDITOR.dom.node * @param {Object|String} element A native DOM element or the element name for * new elements. * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain * the element in case of element creation. * @example * // Create a new <span> element. * var element = new CKEDITOR.dom.element( 'span' ); * @example * // Create an element based on a native DOM element. * var element = new CKEDITOR.dom.element( document.getElementById( 'myId' ) ); */ CKEDITOR.dom.element = function( element, ownerDocument ) { if ( typeof element == 'string' ) element = ( ownerDocument ? ownerDocument.$ : document ).createElement( element ); // Call the base constructor (we must not call CKEDITOR.dom.node). CKEDITOR.dom.domObject.call( this, element ); }; // PACKAGER_RENAME( CKEDITOR.dom.element ) /** * The the {@link CKEDITOR.dom.element} representing and element. If the * element is a native DOM element, it will be transformed into a valid * CKEDITOR.dom.element object. * @returns {CKEDITOR.dom.element} The transformed element. * @example * var element = new CKEDITOR.dom.element( 'span' ); * alert( element == CKEDITOR.dom.element.get( element ) ); "true" * @example * var element = document.getElementById( 'myElement' ); * alert( CKEDITOR.dom.element.get( element ).getName() ); e.g. "p" */ CKEDITOR.dom.element.get = function( element ) { return element && ( element.$ ? element : new CKEDITOR.dom.element( element ) ); }; CKEDITOR.dom.element.prototype = new CKEDITOR.dom.node(); /** * Creates an instance of the {@link CKEDITOR.dom.element} class based on the * HTML representation of an element. * @param {String} html The element HTML. It should define only one element in * the "root" level. The "root" element can have child nodes, but not * siblings. * @returns {CKEDITOR.dom.element} The element instance. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<strong class="anyclass">My element</strong>' ); * alert( element.getName() ); // "strong" */ CKEDITOR.dom.element.createFromHtml = function( html, ownerDocument ) { var temp = new CKEDITOR.dom.element( 'div', ownerDocument ); temp.setHtml( html ); // When returning the node, remove it from its parent to detach it. return temp.getFirst().remove(); }; CKEDITOR.dom.element.setMarker = function( database, element, name, value ) { var id = element.getCustomData( 'list_marker_id' ) || ( element.setCustomData( 'list_marker_id', CKEDITOR.tools.getNextNumber() ).getCustomData( 'list_marker_id' ) ), markerNames = element.getCustomData( 'list_marker_names' ) || ( element.setCustomData( 'list_marker_names', {} ).getCustomData( 'list_marker_names' ) ); database[id] = element; markerNames[name] = 1; return element.setCustomData( name, value ); }; CKEDITOR.dom.element.clearAllMarkers = function( database ) { for ( var i in database ) if ( ! ( i in Object.prototype ) ) CKEDITOR.dom.element.clearMarkers( database, database[i], true ); }; CKEDITOR.dom.element.clearMarkers = function( database, element, removeFromDatabase ) { var names = element.getCustomData( 'list_marker_names' ), id = element.getCustomData( 'list_marker_id' ); for ( var i in names ) if ( ! ( i in Object.prototype ) ) element.removeCustomData( i ); element.removeCustomData( 'list_marker_names' ); if ( removeFromDatabase ) { element.removeCustomData( 'list_marker_id' ); delete database[id]; } }; CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype, /** @lends CKEDITOR.dom.element.prototype */ { /** * The node type. This is a constant value set to * {@link CKEDITOR.NODE_ELEMENT}. * @type Number * @example */ type : CKEDITOR.NODE_ELEMENT, /** * Adds a CSS class to the element. It appends the class to the * already existing names. * @param {String} className The name of the class to be added. * @example * var element = new CKEDITOR.dom.element( 'div' ); * element.addClass( 'classA' ); // <div class="classA"> * element.addClass( 'classB' ); // <div class="classA classB"> * element.addClass( 'classA' ); // <div class="classA classB"> */ addClass : function( className ) { var c = this.$.className; if ( c ) { var regex = new RegExp( '(?:^|\\s)' + className + '(?:\\s|$)', '' ); if ( !regex.test( c ) ) c += ' ' + className; } this.$.className = c || className; }, /** * Removes a CSS class name from the elements classes. Other classes * remain untouched. * @param {String} className The name of the class to remove. * @example * var element = new CKEDITOR.dom.element( 'div' ); * element.addClass( 'classA' ); // <div class="classA"> * element.addClass( 'classB' ); // <div class="classA classB"> * element.removeClass( 'classA' ); // <div class="classB"> * element.removeClass( 'classB' ); // <div> */ removeClass : function( className ) { var c = this.getAttribute( 'class' ); if ( c ) { var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', 'i' ); if ( regex.test( c ) ) { c = c.replace( regex, '' ).replace( /^\s+/, '' ); if ( c ) this.setAttribute( 'class', c ); else this.removeAttribute( 'class' ); } } }, hasClass : function( className ) { var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', '' ); return regex.test( this.getAttribute('class') ); }, /** * Append a node as a child of this element. * @param {CKEDITOR.dom.node|String} node The node or element name to be * appended. * @param {Boolean} [toStart] Indicates that the element is to be * appended at the start. * @returns {CKEDITOR.dom.node} The appended node. * @example * var p = new CKEDITOR.dom.element( 'p' ); * * var strong = new CKEDITOR.dom.element( 'strong' ); * p.append( strong ); * * var em = p.append( 'em' ); * * // result: "<p><strong></strong><em></em></p>" */ append : function( node, toStart ) { if ( typeof node == 'string' ) node = this.getDocument().createElement( node ); if ( toStart ) this.$.insertBefore( node.$, this.$.firstChild ); else this.$.appendChild( node.$ ); return node; }, appendHtml : function( html ) { if ( !this.$.childNodes.length ) this.setHtml( html ); else { var temp = new CKEDITOR.dom.element( 'div', this.getDocument() ); temp.setHtml( html ); temp.moveChildren( this ); } }, /** * Append text to this element. * @param {String} text The text to be appended. * @returns {CKEDITOR.dom.node} The appended node. * @example * var p = new CKEDITOR.dom.element( 'p' ); * p.appendText( 'This is' ); * p.appendText( ' some text' ); * * // result: "<p>This is some text</p>" */ appendText : function( text ) { if ( this.$.text != undefined ) this.$.text += text; else this.append( new CKEDITOR.dom.text( text ) ); }, appendBogus : function() { var lastChild = this.getLast() ; // Ignore empty/spaces text. while ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT && !CKEDITOR.tools.rtrim( lastChild.getText() ) ) lastChild = lastChild.getPrevious(); if ( !lastChild || !lastChild.is || !lastChild.is( 'br' ) ) { var bogus = CKEDITOR.env.opera ? this.getDocument().createText('') : this.getDocument().createElement( 'br' ); CKEDITOR.env.gecko && bogus.setAttribute( 'type', '_moz' ); this.append( bogus ); } }, /** * Breaks one of the ancestor element in the element position, moving * this element between the broken parts. * @param {CKEDITOR.dom.element} parent The anscestor element to get broken. * @example * // Before breaking: * // <b>This <i>is some<span /> sample</i> test text</b> * // If "element" is <span /> and "parent" is <i>: * // <b>This <i>is some</i><span /><i> sample</i> test text</b> * element.breakParent( parent ); * @example * // Before breaking: * // <b>This <i>is some<span /> sample</i> test text</b> * // If "element" is <span /> and "parent" is <b>: * // <b>This <i>is some</i></b><span /><b><i> sample</i> test text</b> * element.breakParent( parent ); */ breakParent : function( parent ) { var range = new CKEDITOR.dom.range( this.getDocument() ); // We'll be extracting part of this element, so let's use our // range to get the correct piece. range.setStartAfter( this ); range.setEndAfter( parent ); // Extract it. var docFrag = range.extractContents(); // Move the element outside the broken element. range.insertNode( this.remove() ); // Re-insert the extracted piece after the element. docFrag.insertAfterNode( this ); }, contains : CKEDITOR.env.ie || CKEDITOR.env.webkit ? function( node ) { var $ = this.$; return node.type != CKEDITOR.NODE_ELEMENT ? $.contains( node.getParent().$ ) : $ != node.$ && $.contains( node.$ ); } : function( node ) { return !!( this.$.compareDocumentPosition( node.$ ) & 16 ); }, /** * Moves the selection focus to this element. * @example * var element = CKEDITOR.document.getById( 'myTextarea' ); * element.focus(); */ focus : function() { // IE throws error if the element is not visible. try { this.$.focus(); } catch (e) {} }, /** * Gets the inner HTML of this element. * @returns {String} The inner HTML of this element. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' ); * alert( p.getHtml() ); // "<b>Example</b>" */ getHtml : function() { var retval = this.$.innerHTML; // Strip tags in IE. (#3341). return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval; }, getOuterHtml : function() { if ( this.$.outerHTML ) { // IE includes the tag in the outerHTML of // namespaced element. So, we must strip it here. (#3341) return this.$.outerHTML.replace( /<\?[^>]*>/, '' ); } var tmpDiv = this.$.ownerDocument.createElement( 'div' ); tmpDiv.appendChild( this.$.cloneNode( true ) ); return tmpDiv.innerHTML; }, /** * Sets the inner HTML of this element. * @param {String} html The HTML to be set for this element. * @returns {String} The inserted HTML. * @example * var p = new CKEDITOR.dom.element( 'p' ); * p.setHtml( '<b>Inner</b> HTML' ); * * // result: "<p><b>Inner</b> HTML</p>" */ setHtml : function( html ) { return ( this.$.innerHTML = html ); }, /** * Sets the element contents as plain text. * @param {String} text The text to be set. * @returns {String} The inserted text. * @example * var element = new CKEDITOR.dom.element( 'div' ); * element.setText( 'A > B & C < D' ); * alert( element.innerHTML ); // "A > B & C < D" */ setText : function( text ) { CKEDITOR.dom.element.prototype.setText = ( this.$.innerText != undefined ) ? function ( text ) { return this.$.innerText = text; } : function ( text ) { return this.$.textContent = text; }; return this.setText( text ); }, /** * Gets the value of an element attribute. * @function * @param {String} name The attribute name. * @returns {String} The attribute value or null if not defined. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<input type="text" />' ); * alert( element.getAttribute( 'type' ) ); // "text" */ getAttribute : (function() { var standard = function( name ) { return this.$.getAttribute( name, 2 ); }; if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) ) { return function( name ) { switch ( name ) { case 'class': name = 'className'; break; case 'tabindex': var tabIndex = standard.call( this, name ); // IE returns tabIndex=0 by default for all // elements. For those elements, // getAtrribute( 'tabindex', 2 ) returns 32768 // instead. So, we must make this check to give a // uniform result among all browsers. if ( tabIndex !== 0 && this.$.tabIndex === 0 ) tabIndex = null; return tabIndex; break; case 'checked': { var attr = this.$.attributes.getNamedItem( name ), attrValue = attr.specified ? attr.nodeValue // For value given by parser. : this.$.checked; // For value created via DOM interface. return attrValue ? 'checked' : null; } case 'hspace': return this.$.hspace; case 'style': // IE does not return inline styles via getAttribute(). See #2947. return this.$.style.cssText; } return standard.call( this, name ); }; } else return standard; })(), getChildren : function() { return new CKEDITOR.dom.nodeList( this.$.childNodes ); }, /** * Gets the current computed value of one of the element CSS style * properties. * @function * @param {String} propertyName The style property name. * @returns {String} The property value. * @example * var element = new CKEDITOR.dom.element( 'span' ); * alert( element.getComputedStyle( 'display' ) ); // "inline" */ getComputedStyle : CKEDITOR.env.ie ? function( propertyName ) { return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ]; } : function( propertyName ) { return this.getWindow().$.getComputedStyle( this.$, '' ).getPropertyValue( propertyName ); }, /** * Gets the DTD entries for this element. * @returns {Object} An object containing the list of elements accepted * by this element. */ getDtd : function() { var dtd = CKEDITOR.dtd[ this.getName() ]; this.getDtd = function() { return dtd; }; return dtd; }, getElementsByTag : CKEDITOR.dom.document.prototype.getElementsByTag, /** * Gets the computed tabindex for this element. * @function * @returns {Number} The tabindex value. * @example * var element = CKEDITOR.document.getById( 'myDiv' ); * alert( element.getTabIndex() ); // e.g. "-1" */ getTabIndex : CKEDITOR.env.ie ? function() { var tabIndex = this.$.tabIndex; // IE returns tabIndex=0 by default for all elements. In // those cases we must check that the element really has // the tabindex attribute set to zero, or it is one of // those element that should have zero by default. if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 ) tabIndex = -1; return tabIndex; } : CKEDITOR.env.webkit ? function() { var tabIndex = this.$.tabIndex; // Safari returns "undefined" for elements that should not // have tabindex (like a div). So, we must try to get it // from the attribute. // https://bugs.webkit.org/show_bug.cgi?id=20596 if ( tabIndex == undefined ) { tabIndex = parseInt( this.getAttribute( 'tabindex' ), 10 ); // If the element don't have the tabindex attribute, // then we should return -1. if ( isNaN( tabIndex ) ) tabIndex = -1; } return tabIndex; } : function() { return this.$.tabIndex; }, /** * Gets the text value of this element. * * Only in IE (which uses innerText), <br> will cause linebreaks, * and sucessive whitespaces (including line breaks) will be reduced to * a single space. This behavior is ok for us, for now. It may change * in the future. * @returns {String} The text value. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<div>Same <i>text</i>.</div>' ); * alert( element.getText() ); // "Sample text." */ getText : function() { return this.$.textContent || this.$.innerText || ''; }, /** * Gets the window object that contains this element. * @returns {CKEDITOR.dom.window} The window object. * @example */ getWindow : function() { return this.getDocument().getWindow(); }, /** * Gets the value of the "id" attribute of this element. * @returns {String} The element id, or null if not available. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<p id="myId"></p>' ); * alert( element.getId() ); // "myId" */ getId : function() { return this.$.id || null; }, /** * Gets the value of the "name" attribute of this element. * @returns {String} The element name, or null if not available. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<input name="myName"></input>' ); * alert( element.getNameAtt() ); // "myName" */ getNameAtt : function() { return this.$.name || null; }, /** * Gets the element name (tag name). The returned name is guaranteed to * be always full lowercased. * @returns {String} The element name. * @example * var element = new CKEDITOR.dom.element( 'span' ); * alert( element.getName() ); // "span" */ getName : function() { // Cache the lowercased name inside a closure. var nodeName = this.$.nodeName.toLowerCase(); if ( CKEDITOR.env.ie ) { var scopeName = this.$.scopeName; if ( scopeName != 'HTML' ) nodeName = scopeName.toLowerCase() + ':' + nodeName; } return ( this.getName = function() { return nodeName; })(); }, /** * Gets the value set to this element. This value is usually available * for form field elements. * @returns {String} The element value. */ getValue : function() { return this.$.value; }, /** * Gets the first child node of this element. * @param {Function} evaluator Filtering the result node. * @returns {CKEDITOR.dom.node} The first child node or null if not * available. * @example * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' ); * var first = element.getFirst(); * alert( first.getName() ); // "b" */ getFirst : function( evaluator ) { var first = this.$.firstChild, retval = first && new CKEDITOR.dom.node( first ); if ( retval && evaluator && !evaluator( retval ) ) retval = retval.getNext( evaluator ); return retval; }, /** * @param {Function} evaluator Filtering the result node. */ getLast : function( evaluator ) { var last = this.$.lastChild, retval = last && new CKEDITOR.dom.node( last ); if ( retval && evaluator && !evaluator( retval ) ) retval = retval.getPrevious( evaluator ); return retval; }, getStyle : function( name ) { return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ]; }, /** * Checks if the element name matches one or more names. * @param {String} name[,name[,...]] One or more names to be checked. * @returns {Boolean} true if the element name matches any of the names. * @example * var element = new CKEDITOR.element( 'span' ); * alert( element.is( 'span' ) ); "true" * alert( element.is( 'p', 'span' ) ); "true" * alert( element.is( 'p' ) ); "false" * alert( element.is( 'p', 'div' ) ); "false" */ is : function() { var name = this.getName(); for ( var i = 0 ; i < arguments.length ; i++ ) { if ( arguments[ i ] == name ) return true; } return false; }, isEditable : function() { // Get the element name. var name = this.getName(); // Get the element DTD (defaults to span for unknown elements). var dtd = !CKEDITOR.dtd.$nonEditable[ name ] && ( CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span ); // In the DTD # == text node. return ( dtd && dtd['#'] ); }, isIdentical : function( otherElement ) { if ( this.getName() != otherElement.getName() ) return false; var thisAttribs = this.$.attributes, otherAttribs = otherElement.$.attributes; var thisLength = thisAttribs.length, otherLength = otherAttribs.length; if ( !CKEDITOR.env.ie && thisLength != otherLength ) return false; for ( var i = 0 ; i < thisLength ; i++ ) { var attribute = thisAttribs[ i ]; if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != '_cke_expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) ) return false; } // For IE, we have to for both elements, because it's difficult to // know how the atttibutes collection is organized in its DOM. if ( CKEDITOR.env.ie ) { for ( i = 0 ; i < otherLength ; i++ ) { attribute = otherAttribs[ i ]; if ( attribute.specified && attribute.nodeName != '_cke_expando' && attribute.nodeValue != this.getAttribute( attribute.nodeName ) ) return false; } } return true; }, /** * Checks if this element is visible. May not work if the element is * child of an element with visibility set to "hidden", but works well * on the great majority of cases. * @return {Boolean} True if the element is visible. */ isVisible : function() { var isVisible = !!this.$.offsetHeight && this.getComputedStyle( 'visibility' ) != 'hidden', elementWindow, elementWindowFrame; // Webkit and Opera report non-zero offsetHeight despite that // element is inside an invisible iframe. (#4542) if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) ) { elementWindow = this.getWindow(); if ( !elementWindow.equals( CKEDITOR.document.getWindow() ) && ( elementWindowFrame = elementWindow.$.frameElement ) ) { isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible(); } } return isVisible; }, /** * Indicates that the element has defined attributes. * @returns {Boolean} True if the element has attributes. * @example * var element = CKEDITOR.dom.element.createFromHtml( '