2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.dom.element} class, which
\r
8 * represents a DOM element.
\r
12 * Represents a DOM element.
\r
14 * @augments CKEDITOR.dom.node
\r
15 * @param {Object|String} element A native DOM element or the element name for
\r
17 * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain
\r
18 * the element in case of element creation.
\r
20 * // Create a new <span> element.
\r
21 * var element = new CKEDITOR.dom.element( 'span' );
\r
23 * // Create an element based on a native DOM element.
\r
24 * var element = new CKEDITOR.dom.element( document.getElementById( 'myId' ) );
\r
26 CKEDITOR.dom.element = function( element, ownerDocument )
\r
28 if ( typeof element == 'string' )
\r
29 element = ( ownerDocument ? ownerDocument.$ : document ).createElement( element );
\r
31 // Call the base constructor (we must not call CKEDITOR.dom.node).
\r
32 CKEDITOR.dom.domObject.call( this, element );
\r
35 // PACKAGER_RENAME( CKEDITOR.dom.element )
\r
38 * The the {@link CKEDITOR.dom.element} representing and element. If the
\r
39 * element is a native DOM element, it will be transformed into a valid
\r
40 * CKEDITOR.dom.element object.
\r
41 * @returns {CKEDITOR.dom.element} The transformed element.
\r
43 * var element = new CKEDITOR.dom.element( 'span' );
\r
44 * alert( element == <b>CKEDITOR.dom.element.get( element )</b> ); "true"
\r
46 * var element = document.getElementById( 'myElement' );
\r
47 * alert( <b>CKEDITOR.dom.element.get( element )</b>.getName() ); e.g. "p"
\r
49 CKEDITOR.dom.element.get = function( element )
\r
51 return element && ( element.$ ? element : new CKEDITOR.dom.element( element ) );
\r
54 CKEDITOR.dom.element.prototype = new CKEDITOR.dom.node();
\r
57 * Creates an instance of the {@link CKEDITOR.dom.element} class based on the
\r
58 * HTML representation of an element.
\r
59 * @param {String} html The element HTML. It should define only one element in
\r
60 * the "root" level. The "root" element can have child nodes, but not
\r
62 * @returns {CKEDITOR.dom.element} The element instance.
\r
64 * var element = <b>CKEDITOR.dom.element.createFromHtml( '<strong class="anyclass">My element</strong>' )</b>;
\r
65 * alert( element.getName() ); // "strong"
\r
67 CKEDITOR.dom.element.createFromHtml = function( html, ownerDocument )
\r
69 var temp = new CKEDITOR.dom.element( 'div', ownerDocument );
\r
70 temp.setHtml( html );
\r
72 // When returning the node, remove it from its parent to detach it.
\r
73 return temp.getFirst().remove();
\r
76 CKEDITOR.dom.element.setMarker = function( database, element, name, value )
\r
78 var id = element.getCustomData( 'list_marker_id' ) ||
\r
79 ( element.setCustomData( 'list_marker_id', CKEDITOR.tools.getNextNumber() ).getCustomData( 'list_marker_id' ) ),
\r
80 markerNames = element.getCustomData( 'list_marker_names' ) ||
\r
81 ( element.setCustomData( 'list_marker_names', {} ).getCustomData( 'list_marker_names' ) );
\r
82 database[id] = element;
\r
83 markerNames[name] = 1;
\r
85 return element.setCustomData( name, value );
\r
88 CKEDITOR.dom.element.clearAllMarkers = function( database )
\r
90 for ( var i in database )
\r
91 CKEDITOR.dom.element.clearMarkers( database, database[i], true );
\r
94 CKEDITOR.dom.element.clearMarkers = function( database, element, removeFromDatabase )
\r
96 var names = element.getCustomData( 'list_marker_names' ),
\r
97 id = element.getCustomData( 'list_marker_id' );
\r
98 for ( var i in names )
\r
99 element.removeCustomData( i );
\r
100 element.removeCustomData( 'list_marker_names' );
\r
101 if ( removeFromDatabase )
\r
103 element.removeCustomData( 'list_marker_id' );
\r
104 delete database[id];
\r
108 CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,
\r
109 /** @lends CKEDITOR.dom.element.prototype */
\r
112 * The node type. This is a constant value set to
\r
113 * {@link CKEDITOR.NODE_ELEMENT}.
\r
117 type : CKEDITOR.NODE_ELEMENT,
\r
120 * Adds a CSS class to the element. It appends the class to the
\r
121 * already existing names.
\r
122 * @param {String} className The name of the class to be added.
\r
124 * var element = new CKEDITOR.dom.element( 'div' );
\r
125 * element.addClass( 'classA' ); // <div class="classA">
\r
126 * element.addClass( 'classB' ); // <div class="classA classB">
\r
127 * element.addClass( 'classA' ); // <div class="classA classB">
\r
129 addClass : function( className )
\r
131 var c = this.$.className;
\r
134 var regex = new RegExp( '(?:^|\\s)' + className + '(?:\\s|$)', '' );
\r
135 if ( !regex.test( c ) )
\r
136 c += ' ' + className;
\r
138 this.$.className = c || className;
\r
142 * Removes a CSS class name from the elements classes. Other classes
\r
143 * remain untouched.
\r
144 * @param {String} className The name of the class to remove.
\r
146 * var element = new CKEDITOR.dom.element( 'div' );
\r
147 * element.addClass( 'classA' ); // <div class="classA">
\r
148 * element.addClass( 'classB' ); // <div class="classA classB">
\r
149 * element.removeClass( 'classA' ); // <div class="classB">
\r
150 * element.removeClass( 'classB' ); // <div>
\r
152 removeClass : function( className )
\r
154 var c = this.getAttribute( 'class' );
\r
157 var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', 'i' );
\r
158 if ( regex.test( c ) )
\r
160 c = c.replace( regex, '' ).replace( /^\s+/, '' );
\r
163 this.setAttribute( 'class', c );
\r
165 this.removeAttribute( 'class' );
\r
170 hasClass : function( className )
\r
172 var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', '' );
\r
173 return regex.test( this.getAttribute('class') );
\r
177 * Append a node as a child of this element.
\r
178 * @param {CKEDITOR.dom.node|String} node The node or element name to be
\r
180 * @param {Boolean} [toStart] Indicates that the element is to be
\r
181 * appended at the start.
\r
182 * @returns {CKEDITOR.dom.node} The appended node.
\r
184 * var p = new CKEDITOR.dom.element( 'p' );
\r
186 * var strong = new CKEDITOR.dom.element( 'strong' );
\r
187 * <b>p.append( strong );</b>
\r
189 * var em = <b>p.append( 'em' );</b>
\r
191 * // result: "<p><strong></strong><em></em></p>"
\r
193 append : function( node, toStart )
\r
195 if ( typeof node == 'string' )
\r
196 node = this.getDocument().createElement( node );
\r
199 this.$.insertBefore( node.$, this.$.firstChild );
\r
201 this.$.appendChild( node.$ );
\r
206 appendHtml : function( html )
\r
208 if ( !this.$.childNodes.length )
\r
209 this.setHtml( html );
\r
212 var temp = new CKEDITOR.dom.element( 'div', this.getDocument() );
\r
213 temp.setHtml( html );
\r
214 temp.moveChildren( this );
\r
219 * Append text to this element.
\r
220 * @param {String} text The text to be appended.
\r
221 * @returns {CKEDITOR.dom.node} The appended node.
\r
223 * var p = new CKEDITOR.dom.element( 'p' );
\r
224 * p.appendText( 'This is' );
\r
225 * p.appendText( ' some text' );
\r
227 * // result: "<p>This is some text</p>"
\r
229 appendText : function( text )
\r
231 if ( this.$.text != undefined )
\r
232 this.$.text += text;
\r
234 this.append( new CKEDITOR.dom.text( text ) );
\r
237 appendBogus : function()
\r
239 var lastChild = this.getLast() ;
\r
241 // Ignore empty/spaces text.
\r
242 while ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT && !CKEDITOR.tools.rtrim( lastChild.getText() ) )
\r
243 lastChild = lastChild.getPrevious();
\r
244 if ( !lastChild || !lastChild.is || !lastChild.is( 'br' ) )
\r
247 CKEDITOR.env.opera ?
\r
248 this.getDocument().createText('') :
\r
249 this.getDocument().createElement( 'br' ) );
\r
254 * Breaks one of the ancestor element in the element position, moving
\r
255 * this element between the broken parts.
\r
256 * @param {CKEDITOR.dom.element} parent The anscestor element to get broken.
\r
258 * // Before breaking:
\r
259 * // <b>This <i>is some<span /> sample</i> test text</b>
\r
260 * // If "element" is <span /> and "parent" is <i>:
\r
261 * // <b>This <i>is some</i><span /><i> sample</i> test text</b>
\r
262 * element.breakParent( parent );
\r
264 * // Before breaking:
\r
265 * // <b>This <i>is some<span /> sample</i> test text</b>
\r
266 * // If "element" is <span /> and "parent" is <b>:
\r
267 * // <b>This <i>is some</i></b><span /><b><i> sample</i> test text</b>
\r
268 * element.breakParent( parent );
\r
270 breakParent : function( parent )
\r
272 var range = new CKEDITOR.dom.range( this.getDocument() );
\r
274 // We'll be extracting part of this element, so let's use our
\r
275 // range to get the correct piece.
\r
276 range.setStartAfter( this );
\r
277 range.setEndAfter( parent );
\r
280 var docFrag = range.extractContents();
\r
282 // Move the element outside the broken element.
\r
283 range.insertNode( this.remove() );
\r
285 // Re-insert the extracted piece after the element.
\r
286 docFrag.insertAfterNode( this );
\r
290 CKEDITOR.env.ie || CKEDITOR.env.webkit ?
\r
295 return node.type != CKEDITOR.NODE_ELEMENT ?
\r
296 $.contains( node.getParent().$ ) :
\r
297 $ != node.$ && $.contains( node.$ );
\r
302 return !!( this.$.compareDocumentPosition( node.$ ) & 16 );
\r
306 * Moves the selection focus to this element.
\r
308 * var element = CKEDITOR.document.getById( 'myTextarea' );
\r
309 * <b>element.focus()</b>;
\r
313 // IE throws error if the element is not visible.
\r
323 * Gets the inner HTML of this element.
\r
324 * @returns {String} The inner HTML of this element.
\r
326 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
327 * alert( <b>p.getHtml()</b> ); // "<b>Example</b>"
\r
329 getHtml : function()
\r
331 var retval = this.$.innerHTML;
\r
332 // Strip <?xml:namespace> tags in IE. (#3341).
\r
333 return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;
\r
336 getOuterHtml : function()
\r
338 if ( this.$.outerHTML )
\r
340 // IE includes the <?xml:namespace> tag in the outerHTML of
\r
341 // namespaced element. So, we must strip it here. (#3341)
\r
342 return this.$.outerHTML.replace( /<\?[^>]*>/, '' );
\r
345 var tmpDiv = this.$.ownerDocument.createElement( 'div' );
\r
346 tmpDiv.appendChild( this.$.cloneNode( true ) );
\r
347 return tmpDiv.innerHTML;
\r
351 * Sets the inner HTML of this element.
\r
352 * @param {String} html The HTML to be set for this element.
\r
353 * @returns {String} The inserted HTML.
\r
355 * var p = new CKEDITOR.dom.element( 'p' );
\r
356 * <b>p.setHtml( '<b>Inner</b> HTML' );</b>
\r
358 * // result: "<p><b>Inner</b> HTML</p>"
\r
360 setHtml : function( html )
\r
362 return ( this.$.innerHTML = html );
\r
366 * Sets the element contents as plain text.
\r
367 * @param {String} text The text to be set.
\r
368 * @returns {String} The inserted text.
\r
370 * var element = new CKEDITOR.dom.element( 'div' );
\r
371 * element.setText( 'A > B & C < D' );
\r
372 * alert( element.innerHTML ); // "A &gt; B &amp; C &lt; D"
\r
374 setText : function( text )
\r
376 CKEDITOR.dom.element.prototype.setText = ( this.$.innerText != undefined ) ?
\r
379 return this.$.innerText = text;
\r
383 return this.$.textContent = text;
\r
386 return this.setText( text );
\r
390 * Gets the value of an element attribute.
\r
392 * @param {String} name The attribute name.
\r
393 * @returns {String} The attribute value or null if not defined.
\r
395 * var element = CKEDITOR.dom.element.createFromHtml( '<input type="text" />' );
\r
396 * alert( <b>element.getAttribute( 'type' )</b> ); // "text"
\r
398 getAttribute : (function()
\r
400 var standard = function( name )
\r
402 return this.$.getAttribute( name, 2 );
\r
405 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
407 return function( name )
\r
412 name = 'className';
\r
416 var tabIndex = standard.call( this, name );
\r
418 // IE returns tabIndex=0 by default for all
\r
419 // elements. For those elements,
\r
420 // getAtrribute( 'tabindex', 2 ) returns 32768
\r
421 // instead. So, we must make this check to give a
\r
422 // uniform result among all browsers.
\r
423 if ( tabIndex !== 0 && this.$.tabIndex === 0 )
\r
431 var attr = this.$.attributes.getNamedItem( name ),
\r
432 attrValue = attr.specified ? attr.nodeValue // For value given by parser.
\r
433 : this.$.checked; // For value created via DOM interface.
\r
435 return attrValue ? 'checked' : null;
\r
439 return this.$.hspace;
\r
442 // IE does not return inline styles via getAttribute(). See #2947.
\r
443 return this.$.style.cssText;
\r
446 return standard.call( this, name );
\r
453 getChildren : function()
\r
455 return new CKEDITOR.dom.nodeList( this.$.childNodes );
\r
459 * Gets the current computed value of one of the element CSS style
\r
462 * @param {String} propertyName The style property name.
\r
463 * @returns {String} The property value.
\r
465 * var element = new CKEDITOR.dom.element( 'span' );
\r
466 * alert( <b>element.getComputedStyle( 'display' )</b> ); // "inline"
\r
470 function( propertyName )
\r
472 return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ];
\r
475 function( propertyName )
\r
477 return this.getWindow().$.getComputedStyle( this.$, '' ).getPropertyValue( propertyName );
\r
481 * Gets the DTD entries for this element.
\r
482 * @returns {Object} An object containing the list of elements accepted
\r
485 getDtd : function()
\r
487 var dtd = CKEDITOR.dtd[ this.getName() ];
\r
489 this.getDtd = function()
\r
497 getElementsByTag : CKEDITOR.dom.document.prototype.getElementsByTag,
\r
500 * Gets the computed tabindex for this element.
\r
502 * @returns {Number} The tabindex value.
\r
504 * var element = CKEDITOR.document.getById( 'myDiv' );
\r
505 * alert( <b>element.getTabIndex()</b> ); // e.g. "-1"
\r
511 var tabIndex = this.$.tabIndex;
\r
513 // IE returns tabIndex=0 by default for all elements. In
\r
514 // those cases we must check that the element really has
\r
515 // the tabindex attribute set to zero, or it is one of
\r
516 // those element that should have zero by default.
\r
517 if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 )
\r
522 : CKEDITOR.env.webkit ?
\r
525 var tabIndex = this.$.tabIndex;
\r
527 // Safari returns "undefined" for elements that should not
\r
528 // have tabindex (like a div). So, we must try to get it
\r
529 // from the attribute.
\r
530 // https://bugs.webkit.org/show_bug.cgi?id=20596
\r
531 if ( tabIndex == undefined )
\r
533 tabIndex = parseInt( this.getAttribute( 'tabindex' ), 10 );
\r
535 // If the element don't have the tabindex attribute,
\r
536 // then we should return -1.
\r
537 if ( isNaN( tabIndex ) )
\r
546 return this.$.tabIndex;
\r
550 * Gets the text value of this element.
\r
552 * Only in IE (which uses innerText), <br> will cause linebreaks,
\r
553 * and sucessive whitespaces (including line breaks) will be reduced to
\r
554 * a single space. This behavior is ok for us, for now. It may change
\r
556 * @returns {String} The text value.
\r
558 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Same <i>text</i>.</div>' );
\r
559 * alert( <b>element.getText()</b> ); // "Sample text."
\r
561 getText : function()
\r
563 return this.$.textContent || this.$.innerText || '';
\r
567 * Gets the window object that contains this element.
\r
568 * @returns {CKEDITOR.dom.window} The window object.
\r
571 getWindow : function()
\r
573 return this.getDocument().getWindow();
\r
577 * Gets the value of the "id" attribute of this element.
\r
578 * @returns {String} The element id, or null if not available.
\r
580 * var element = CKEDITOR.dom.element.createFromHtml( '<p id="myId"></p>' );
\r
581 * alert( <b>element.getId()</b> ); // "myId"
\r
585 return this.$.id || null;
\r
589 * Gets the value of the "name" attribute of this element.
\r
590 * @returns {String} The element name, or null if not available.
\r
592 * var element = CKEDITOR.dom.element.createFromHtml( '<input name="myName"></input>' );
\r
593 * alert( <b>element.getNameAtt()</b> ); // "myName"
\r
595 getNameAtt : function()
\r
597 return this.$.name || null;
\r
601 * Gets the element name (tag name). The returned name is guaranteed to
\r
602 * be always full lowercased.
\r
603 * @returns {String} The element name.
\r
605 * var element = new CKEDITOR.dom.element( 'span' );
\r
606 * alert( <b>element.getName()</b> ); // "span"
\r
608 getName : function()
\r
610 // Cache the lowercased name inside a closure.
\r
611 var nodeName = this.$.nodeName.toLowerCase();
\r
613 if ( CKEDITOR.env.ie )
\r
615 var scopeName = this.$.scopeName;
\r
616 if ( scopeName != 'HTML' )
\r
617 nodeName = scopeName.toLowerCase() + ':' + nodeName;
\r
622 this.getName = function()
\r
629 * Gets the value set to this element. This value is usually available
\r
630 * for form field elements.
\r
631 * @returns {String} The element value.
\r
633 getValue : function()
\r
635 return this.$.value;
\r
639 * Gets the first child node of this element.
\r
640 * @param {Function} evaluator Filtering the result node.
\r
641 * @returns {CKEDITOR.dom.node} The first child node or null if not
\r
644 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
645 * var first = <b>element.getFirst()</b>;
\r
646 * alert( first.getName() ); // "b"
\r
648 getFirst : function( evaluator )
\r
650 var first = this.$.firstChild,
\r
651 retval = first && new CKEDITOR.dom.node( first );
\r
652 if ( retval && evaluator && !evaluator( retval ) )
\r
653 retval = retval.getNext( evaluator );
\r
659 * @param {Function} evaluator Filtering the result node.
\r
661 getLast : function( evaluator )
\r
663 var last = this.$.lastChild,
\r
664 retval = last && new CKEDITOR.dom.node( last );
\r
665 if ( retval && evaluator && !evaluator( retval ) )
\r
666 retval = retval.getPrevious( evaluator );
\r
671 getStyle : function( name )
\r
673 return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ];
\r
677 * Checks if the element name matches one or more names.
\r
678 * @param {String} name[,name[,...]] One or more names to be checked.
\r
679 * @returns {Boolean} true if the element name matches any of the names.
\r
681 * var element = new CKEDITOR.element( 'span' );
\r
682 * alert( <b>element.is( 'span' )</b> ); "true"
\r
683 * alert( <b>element.is( 'p', 'span' )</b> ); "true"
\r
684 * alert( <b>element.is( 'p' )</b> ); "false"
\r
685 * alert( <b>element.is( 'p', 'div' )</b> ); "false"
\r
689 var name = this.getName();
\r
690 for ( var i = 0 ; i < arguments.length ; i++ )
\r
692 if ( arguments[ i ] == name )
\r
698 isEditable : function()
\r
700 // Get the element name.
\r
701 var name = this.getName();
\r
703 // Get the element DTD (defaults to span for unknown elements).
\r
704 var dtd = !CKEDITOR.dtd.$nonEditable[ name ]
\r
705 && ( CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span );
\r
707 // In the DTD # == text node.
\r
708 return ( dtd && dtd['#'] );
\r
711 isIdentical : function( otherElement )
\r
713 if ( this.getName() != otherElement.getName() )
\r
716 var thisAttribs = this.$.attributes,
\r
717 otherAttribs = otherElement.$.attributes;
\r
719 var thisLength = thisAttribs.length,
\r
720 otherLength = otherAttribs.length;
\r
722 if ( !CKEDITOR.env.ie && thisLength != otherLength )
\r
725 for ( var i = 0 ; i < thisLength ; i++ )
\r
727 var attribute = thisAttribs[ i ];
\r
729 if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != '_cke_expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )
\r
733 // For IE, we have to for both elements, because it's difficult to
\r
734 // know how the atttibutes collection is organized in its DOM.
\r
735 if ( CKEDITOR.env.ie )
\r
737 for ( i = 0 ; i < otherLength ; i++ )
\r
739 attribute = otherAttribs[ i ];
\r
740 if ( attribute.specified && attribute.nodeName != '_cke_expando'
\r
741 && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )
\r
750 * Checks if this element is visible. May not work if the element is
\r
751 * child of an element with visibility set to "hidden", but works well
\r
752 * on the great majority of cases.
\r
753 * @return {Boolean} True if the element is visible.
\r
755 isVisible : function()
\r
757 var isVisible = !!this.$.offsetHeight && this.getComputedStyle( 'visibility' ) != 'hidden',
\r
759 elementWindowFrame;
\r
761 // Webkit and Opera report non-zero offsetHeight despite that
\r
762 // element is inside an invisible iframe. (#4542)
\r
763 if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) )
\r
765 elementWindow = this.getWindow();
\r
767 if ( !elementWindow.equals( CKEDITOR.document.getWindow() )
\r
768 && ( elementWindowFrame = elementWindow.$.frameElement ) )
\r
770 isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible();
\r
778 * Indicates that the element has defined attributes.
\r
779 * @returns {Boolean} True if the element has attributes.
\r
781 * var element = CKEDITOR.dom.element.createFromHtml( '<div title="Test">Example</div>' );
\r
782 * alert( <b>element.hasAttributes()</b> ); "true"
\r
784 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Example</div>' );
\r
785 * alert( <b>element.hasAttributes()</b> ); "false"
\r
788 CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) ?
\r
791 var attributes = this.$.attributes;
\r
793 for ( var i = 0 ; i < attributes.length ; i++ )
\r
795 var attribute = attributes[i];
\r
797 switch ( attribute.nodeName )
\r
800 // IE has a strange bug. If calling removeAttribute('className'),
\r
801 // the attributes collection will still contain the "class"
\r
802 // attribute, which will be marked as "specified", even if the
\r
803 // outerHTML of the element is not displaying the class attribute.
\r
804 // Note : I was not able to reproduce it outside the editor,
\r
805 // but I've faced it while working on the TC of #1391.
\r
806 if ( this.getAttribute( 'class' ) )
\r
809 // Attributes to be ignored.
\r
810 case '_cke_expando' :
\r
816 if ( attribute.specified )
\r
826 var attributes = this.$.attributes;
\r
827 return ( attributes.length > 1 || ( attributes.length == 1 && attributes[0].nodeName != '_cke_expando' ) );
\r
831 * Indicates whether a specified attribute is defined for this element.
\r
832 * @returns {Boolean} True if the specified attribute is defined.
\r
833 * @param (String) name The attribute name.
\r
836 hasAttribute : function( name )
\r
838 var $attr = this.$.attributes.getNamedItem( name );
\r
839 return !!( $attr && $attr.specified );
\r
843 * Hides this element (display:none).
\r
845 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
846 * <b>element.hide()</b>;
\r
850 this.setStyle( 'display', 'none' );
\r
853 moveChildren : function( target, toStart )
\r
865 while ( ( child = $.lastChild ) )
\r
866 target.insertBefore( $.removeChild( child ), target.firstChild );
\r
870 while ( ( child = $.firstChild ) )
\r
871 target.appendChild( $.removeChild( child ) );
\r
876 * Shows this element (display it).
\r
878 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
879 * <b>element.show()</b>;
\r
891 * Sets the value of an element attribute.
\r
892 * @param {String} name The name of the attribute.
\r
893 * @param {String} value The value to be set to the attribute.
\r
895 * @returns {CKEDITOR.dom.element} This element instance.
\r
897 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
898 * <b>element.setAttribute( 'class', 'myClass' )</b>;
\r
899 * <b>element.setAttribute( 'title', 'This is an example' )</b>;
\r
901 setAttribute : (function()
\r
903 var standard = function( name, value )
\r
905 this.$.setAttribute( name, value );
\r
909 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
911 return function( name, value )
\r
913 if ( name == 'class' )
\r
914 this.$.className = value;
\r
915 else if ( name == 'style' )
\r
916 this.$.style.cssText = value;
\r
917 else if ( name == 'tabindex' ) // Case sensitive.
\r
918 this.$.tabIndex = value;
\r
919 else if ( name == 'checked' )
\r
920 this.$.checked = value;
\r
922 standard.apply( this, arguments );
\r
931 * Sets the value of several element attributes.
\r
932 * @param {Object} attributesPairs An object containing the names and
\r
933 * values of the attributes.
\r
934 * @returns {CKEDITOR.dom.element} This element instance.
\r
936 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
937 * <b>element.setAttributes({
\r
938 * 'class' : 'myClass',
\r
939 * 'title' : 'This is an example' })</b>;
\r
941 setAttributes : function( attributesPairs )
\r
943 for ( var name in attributesPairs )
\r
944 this.setAttribute( name, attributesPairs[ name ] );
\r
949 * Sets the element value. This function is usually used with form
\r
951 * @param {String} value The element value.
\r
952 * @returns {CKEDITOR.dom.element} This element instance.
\r
954 setValue : function( value )
\r
956 this.$.value = value;
\r
961 * Removes an attribute from the element.
\r
962 * @param {String} name The attribute name.
\r
965 * var element = CKEDITOR.dom.element.createFromHtml( '<div class="classA"></div>' );
\r
966 * element.removeAttribute( 'class' );
\r
968 removeAttribute : (function()
\r
970 var standard = function( name )
\r
972 this.$.removeAttribute( name );
\r
975 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
977 return function( name )
\r
979 if ( name == 'class' )
\r
980 name = 'className';
\r
981 else if ( name == 'tabindex' )
\r
983 standard.call( this, name );
\r
990 removeAttributes : function ( attributes )
\r
992 for ( var i = 0 ; i < attributes.length ; i++ )
\r
993 this.removeAttribute( attributes[ i ] );
\r
997 * Removes a style from the element.
\r
998 * @param {String} name The style name.
\r
1001 * var element = CKEDITOR.dom.element.createFromHtml( '<div style="display:none"></div>' );
\r
1002 * element.removeStyle( 'display' );
\r
1004 removeStyle : function( name )
\r
1006 this.setStyle( name, '' );
\r
1007 if ( this.$.style.removeAttribute )
\r
1008 this.$.style.removeAttribute( CKEDITOR.tools.cssStyleToDomStyle( name ) );
\r
1010 if ( !this.$.style.cssText )
\r
1011 this.removeAttribute( 'style' );
\r
1015 * Sets the value of an element style.
\r
1016 * @param {String} name The name of the style. The CSS naming notation
\r
1017 * must be used (e.g. "background-color").
\r
1018 * @param {String} value The value to be set to the style.
\r
1019 * @returns {CKEDITOR.dom.element} This element instance.
\r
1021 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1022 * <b>element.setStyle( 'background-color', '#ff0000' )</b>;
\r
1023 * <b>element.setStyle( 'margin-top', '10px' )</b>;
\r
1024 * <b>element.setStyle( 'float', 'right' )</b>;
\r
1026 setStyle : function( name, value )
\r
1028 this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ] = value;
\r
1033 * Sets the value of several element styles.
\r
1034 * @param {Object} stylesPairs An object containing the names and
\r
1035 * values of the styles.
\r
1036 * @returns {CKEDITOR.dom.element} This element instance.
\r
1038 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1039 * <b>element.setStyles({
\r
1040 * 'position' : 'absolute',
\r
1041 * 'float' : 'right' })</b>;
\r
1043 setStyles : function( stylesPairs )
\r
1045 for ( var name in stylesPairs )
\r
1046 this.setStyle( name, stylesPairs[ name ] );
\r
1051 * Sets the opacity of an element.
\r
1052 * @param {Number} opacity A number within the range [0.0, 1.0].
\r
1054 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1055 * <b>element.setOpacity( 0.75 )</b>;
\r
1057 setOpacity : function( opacity )
\r
1059 if ( CKEDITOR.env.ie )
\r
1061 opacity = Math.round( opacity * 100 );
\r
1062 this.setStyle( 'filter', opacity >= 100 ? '' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')' );
\r
1065 this.setStyle( 'opacity', opacity );
\r
1069 * Makes the element and its children unselectable.
\r
1072 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1073 * element.unselectable();
\r
1076 CKEDITOR.env.gecko ?
\r
1079 this.$.style.MozUserSelect = 'none';
\r
1081 : CKEDITOR.env.webkit ?
\r
1084 this.$.style.KhtmlUserSelect = 'none';
\r
1089 if ( CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1091 var element = this.$,
\r
1095 element.unselectable = 'on';
\r
1097 while ( ( e = element.all[ i++ ] ) )
\r
1099 switch ( e.tagName.toLowerCase() )
\r
1105 /* Ignore the above tags */
\r
1108 e.unselectable = 'on';
\r
1114 getPositionedAncestor : function()
\r
1116 var current = this;
\r
1117 while ( current.getName() != 'html' )
\r
1119 if ( current.getComputedStyle( 'position' ) != 'static' )
\r
1122 current = current.getParent();
\r
1127 getDocumentPosition : function( refDocument )
\r
1130 body = this.getDocument().getBody(),
\r
1131 quirks = this.getDocument().$.compatMode == 'BackCompat';
\r
1133 var doc = this.getDocument();
\r
1135 if ( document.documentElement[ "getBoundingClientRect" ] )
\r
1137 var box = this.$.getBoundingClientRect(),
\r
1139 $docElem = $doc.documentElement;
\r
1141 var clientTop = $docElem.clientTop || body.$.clientTop || 0,
\r
1142 clientLeft = $docElem.clientLeft || body.$.clientLeft || 0,
\r
1143 needAdjustScrollAndBorders = true;
\r
1146 * #3804: getBoundingClientRect() works differently on IE and non-IE
\r
1147 * browsers, regarding scroll positions.
\r
1149 * On IE, the top position of the <html> element is always 0, no matter
\r
1150 * how much you scrolled down.
\r
1152 * On other browsers, the top position of the <html> element is negative
\r
1155 if ( CKEDITOR.env.ie )
\r
1157 var inDocElem = doc.getDocumentElement().contains( this ),
\r
1158 inBody = doc.getBody().contains( this );
\r
1160 needAdjustScrollAndBorders = ( quirks && inBody ) || ( !quirks && inDocElem );
\r
1163 if ( needAdjustScrollAndBorders )
\r
1165 x = box.left + ( !quirks && $docElem.scrollLeft || body.$.scrollLeft );
\r
1167 y = box.top + ( !quirks && $docElem.scrollTop || body.$.scrollTop );
\r
1173 var current = this, previous = null, offsetParent;
\r
1174 while ( current && !( current.getName() == 'body' || current.getName() == 'html' ) )
\r
1176 x += current.$.offsetLeft - current.$.scrollLeft;
\r
1177 y += current.$.offsetTop - current.$.scrollTop;
\r
1179 // Opera includes clientTop|Left into offsetTop|Left.
\r
1180 if ( !current.equals( this ) )
\r
1182 x += ( current.$.clientLeft || 0 );
\r
1183 y += ( current.$.clientTop || 0 );
\r
1186 var scrollElement = previous;
\r
1187 while ( scrollElement && !scrollElement.equals( current ) )
\r
1189 x -= scrollElement.$.scrollLeft;
\r
1190 y -= scrollElement.$.scrollTop;
\r
1191 scrollElement = scrollElement.getParent();
\r
1194 previous = current;
\r
1195 current = ( offsetParent = current.$.offsetParent ) ?
\r
1196 new CKEDITOR.dom.element( offsetParent ) : null;
\r
1200 if ( refDocument )
\r
1202 var currentWindow = this.getWindow(),
\r
1203 refWindow = refDocument.getWindow();
\r
1205 if ( !currentWindow.equals( refWindow ) && currentWindow.$.frameElement )
\r
1207 var iframePosition = ( new CKEDITOR.dom.element( currentWindow.$.frameElement ) ).getDocumentPosition( refDocument );
\r
1209 x += iframePosition.x;
\r
1210 y += iframePosition.y;
\r
1214 if ( !document.documentElement[ "getBoundingClientRect" ] )
\r
1216 // In Firefox, we'll endup one pixel before the element positions,
\r
1217 // so we must add it here.
\r
1218 if ( CKEDITOR.env.gecko && !quirks )
\r
1220 x += this.$.clientLeft ? 1 : 0;
\r
1221 y += this.$.clientTop ? 1 : 0;
\r
1225 return { x : x, y : y };
\r
1228 scrollIntoView : function( alignTop )
\r
1230 // Get the element window.
\r
1231 var win = this.getWindow(),
\r
1232 winHeight = win.getViewPaneSize().height;
\r
1234 // Starts from the offset that will be scrolled with the negative value of
\r
1235 // the visible window height.
\r
1236 var offset = winHeight * -1;
\r
1238 // Append the view pane's height if align to top.
\r
1239 // Append element height if we are aligning to the bottom.
\r
1241 offset += winHeight;
\r
1244 offset += this.$.offsetHeight || 0;
\r
1246 // Consider the margin in the scroll, which is ok for our current needs, but
\r
1247 // needs investigation if we will be using this function in other places.
\r
1248 offset += parseInt( this.getComputedStyle( 'marginBottom' ) || 0, 10 ) || 0;
\r
1251 // Append the offsets for the entire element hierarchy.
\r
1252 var elementPosition = this.getDocumentPosition();
\r
1253 offset += elementPosition.y;
\r
1255 // offset value might be out of range(nagative), fix it(#3692).
\r
1256 offset = offset < 0 ? 0 : offset;
\r
1258 // Scroll the window to the desired position, if not already visible(#3795).
\r
1259 var currentScroll = win.getScrollPosition().y;
\r
1260 if ( offset > currentScroll || offset < currentScroll - winHeight )
\r
1261 win.$.scrollTo( 0, offset );
\r
1264 setState : function( state )
\r
1268 case CKEDITOR.TRISTATE_ON :
\r
1269 this.addClass( 'cke_on' );
\r
1270 this.removeClass( 'cke_off' );
\r
1271 this.removeClass( 'cke_disabled' );
\r
1273 case CKEDITOR.TRISTATE_DISABLED :
\r
1274 this.addClass( 'cke_disabled' );
\r
1275 this.removeClass( 'cke_off' );
\r
1276 this.removeClass( 'cke_on' );
\r
1279 this.addClass( 'cke_off' );
\r
1280 this.removeClass( 'cke_on' );
\r
1281 this.removeClass( 'cke_disabled' );
\r
1287 * Returns the inner document of this IFRAME element.
\r
1288 * @returns {CKEDITOR.dom.document} The inner document.
\r
1290 getFrameDocument : function()
\r
1296 // In IE, with custom document.domain, it may happen that
\r
1297 // the iframe is not yet available, resulting in "Access
\r
1298 // Denied" for the following property access.
\r
1299 $.contentWindow.document;
\r
1303 // Trick to solve this issue, forcing the iframe to get ready
\r
1304 // by simply setting its "src" property.
\r
1307 // In IE6 though, the above is not enough, so we must pause the
\r
1308 // execution for a while, giving it time to think.
\r
1309 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )
\r
1311 window.showModalDialog(
\r
1312 'javascript:document.write("' +
\r
1314 'window.setTimeout(' +
\r
1315 'function(){window.close();}' +
\r
1321 return $ && new CKEDITOR.dom.document( $.contentWindow.document );
\r
1325 * Copy all the attributes from one node to the other, kinda like a clone
\r
1326 * skipAttributes is an object with the attributes that must NOT be copied.
\r
1327 * @param {CKEDITOR.dom.element} dest The destination element.
\r
1328 * @param {Object} skipAttributes A dictionary of attributes to skip.
\r
1331 copyAttributes : function( dest, skipAttributes )
\r
1333 var attributes = this.$.attributes;
\r
1334 skipAttributes = skipAttributes || {};
\r
1336 for ( var n = 0 ; n < attributes.length ; n++ )
\r
1338 var attribute = attributes[n];
\r
1340 // Lowercase attribute name hard rule is broken for
\r
1341 // some attribute on IE, e.g. CHECKED.
\r
1342 var attrName = attribute.nodeName.toLowerCase(),
\r
1345 // We can set the type only once, so do it with the proper value, not copying it.
\r
1346 if ( attrName in skipAttributes )
\r
1349 if( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )
\r
1350 dest.setAttribute( attrName, attrValue );
\r
1351 // IE BUG: value attribute is never specified even if it exists.
\r
1352 else if ( attribute.specified ||
\r
1353 ( CKEDITOR.env.ie && attribute.nodeValue && attrName == 'value' ) )
\r
1355 attrValue = this.getAttribute( attrName );
\r
1356 if ( attrValue === null )
\r
1357 attrValue = attribute.nodeValue;
\r
1359 dest.setAttribute( attrName, attrValue );
\r
1364 if ( this.$.style.cssText !== '' )
\r
1365 dest.$.style.cssText = this.$.style.cssText;
\r
1369 * Changes the tag name of the current element.
\r
1370 * @param {String} newTag The new tag for the element.
\r
1372 renameNode : function( newTag )
\r
1374 // If it's already correct exit here.
\r
1375 if ( this.getName() == newTag )
\r
1378 var doc = this.getDocument();
\r
1380 // Create the new node.
\r
1381 var newNode = new CKEDITOR.dom.element( newTag, doc );
\r
1383 // Copy all attributes.
\r
1384 this.copyAttributes( newNode );
\r
1386 // Move children to the new node.
\r
1387 this.moveChildren( newNode );
\r
1389 // Replace the node.
\r
1390 this.$.parentNode.replaceChild( newNode.$, this.$ );
\r
1391 newNode.$._cke_expando = this.$._cke_expando;
\r
1392 this.$ = newNode.$;
\r
1396 * Gets a DOM tree descendant under the current node.
\r
1397 * @param {Array|Number} indices The child index or array of child indices under the node.
\r
1398 * @returns {CKEDITOR.dom.node} The specified DOM child under the current node. Null if child does not exist.
\r
1400 * var strong = p.getChild(0);
\r
1402 getChild : function( indices )
\r
1404 var rawNode = this.$;
\r
1406 if ( !indices.slice )
\r
1407 rawNode = rawNode.childNodes[ indices ];
\r
1410 while ( indices.length > 0 && rawNode )
\r
1411 rawNode = rawNode.childNodes[ indices.shift() ];
\r
1414 return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;
\r
1417 getChildCount : function()
\r
1419 return this.$.childNodes.length;
\r
1422 disableContextMenu : function()
\r
1424 this.on( 'contextmenu', function( event )
\r
1426 // Cancel the browser context menu.
\r
1427 if ( !event.data.getTarget().hasClass( 'cke_enable_context_menu' ) )
\r
1428 event.data.preventDefault();
\r