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], 1 );
\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
246 var bogus = CKEDITOR.env.opera ?
\r
247 this.getDocument().createText('') :
\r
248 this.getDocument().createElement( 'br' );
\r
250 CKEDITOR.env.gecko && bogus.setAttribute( 'type', '_moz' );
\r
252 this.append( bogus );
\r
257 * Breaks one of the ancestor element in the element position, moving
\r
258 * this element between the broken parts.
\r
259 * @param {CKEDITOR.dom.element} parent The anscestor element to get broken.
\r
261 * // Before breaking:
\r
262 * // <b>This <i>is some<span /> sample</i> test text</b>
\r
263 * // If "element" is <span /> and "parent" is <i>:
\r
264 * // <b>This <i>is some</i><span /><i> sample</i> test text</b>
\r
265 * element.breakParent( parent );
\r
267 * // Before breaking:
\r
268 * // <b>This <i>is some<span /> sample</i> test text</b>
\r
269 * // If "element" is <span /> and "parent" is <b>:
\r
270 * // <b>This <i>is some</i></b><span /><b><i> sample</i> test text</b>
\r
271 * element.breakParent( parent );
\r
273 breakParent : function( parent )
\r
275 var range = new CKEDITOR.dom.range( this.getDocument() );
\r
277 // We'll be extracting part of this element, so let's use our
\r
278 // range to get the correct piece.
\r
279 range.setStartAfter( this );
\r
280 range.setEndAfter( parent );
\r
283 var docFrag = range.extractContents();
\r
285 // Move the element outside the broken element.
\r
286 range.insertNode( this.remove() );
\r
288 // Re-insert the extracted piece after the element.
\r
289 docFrag.insertAfterNode( this );
\r
293 CKEDITOR.env.ie || CKEDITOR.env.webkit ?
\r
298 return node.type != CKEDITOR.NODE_ELEMENT ?
\r
299 $.contains( node.getParent().$ ) :
\r
300 $ != node.$ && $.contains( node.$ );
\r
305 return !!( this.$.compareDocumentPosition( node.$ ) & 16 );
\r
309 * Moves the selection focus to this element.
\r
310 * @param {Boolean} defer Whether to asynchronously defer the
\r
311 * execution by 100 ms.
\r
313 * var element = CKEDITOR.document.getById( 'myTextarea' );
\r
314 * <b>element.focus()</b>;
\r
316 focus : ( function()
\r
320 // IE throws error if the element is not visible.
\r
329 return function( defer )
\r
332 CKEDITOR.tools.setTimeout( exec, 100, this );
\r
339 * Gets the inner HTML of this element.
\r
340 * @returns {String} The inner HTML of this element.
\r
342 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
343 * alert( <b>p.getHtml()</b> ); // "<b>Example</b>"
\r
345 getHtml : function()
\r
347 var retval = this.$.innerHTML;
\r
348 // Strip <?xml:namespace> tags in IE. (#3341).
\r
349 return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;
\r
352 getOuterHtml : function()
\r
354 if ( this.$.outerHTML )
\r
356 // IE includes the <?xml:namespace> tag in the outerHTML of
\r
357 // namespaced element. So, we must strip it here. (#3341)
\r
358 return this.$.outerHTML.replace( /<\?[^>]*>/, '' );
\r
361 var tmpDiv = this.$.ownerDocument.createElement( 'div' );
\r
362 tmpDiv.appendChild( this.$.cloneNode( true ) );
\r
363 return tmpDiv.innerHTML;
\r
367 * Sets the inner HTML of this element.
\r
368 * @param {String} html The HTML to be set for this element.
\r
369 * @returns {String} The inserted HTML.
\r
371 * var p = new CKEDITOR.dom.element( 'p' );
\r
372 * <b>p.setHtml( '<b>Inner</b> HTML' );</b>
\r
374 * // result: "<p><b>Inner</b> HTML</p>"
\r
376 setHtml : function( html )
\r
378 return ( this.$.innerHTML = html );
\r
382 * Sets the element contents as plain text.
\r
383 * @param {String} text The text to be set.
\r
384 * @returns {String} The inserted text.
\r
386 * var element = new CKEDITOR.dom.element( 'div' );
\r
387 * element.setText( 'A > B & C < D' );
\r
388 * alert( element.innerHTML ); // "A &gt; B &amp; C &lt; D"
\r
390 setText : function( text )
\r
392 CKEDITOR.dom.element.prototype.setText = ( this.$.innerText != undefined ) ?
\r
395 return this.$.innerText = text;
\r
399 return this.$.textContent = text;
\r
402 return this.setText( text );
\r
406 * Gets the value of an element attribute.
\r
408 * @param {String} name The attribute name.
\r
409 * @returns {String} The attribute value or null if not defined.
\r
411 * var element = CKEDITOR.dom.element.createFromHtml( '<input type="text" />' );
\r
412 * alert( <b>element.getAttribute( 'type' )</b> ); // "text"
\r
414 getAttribute : (function()
\r
416 var standard = function( name )
\r
418 return this.$.getAttribute( name, 2 );
\r
421 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
423 return function( name )
\r
428 name = 'className';
\r
432 var tabIndex = standard.call( this, name );
\r
434 // IE returns tabIndex=0 by default for all
\r
435 // elements. For those elements,
\r
436 // getAtrribute( 'tabindex', 2 ) returns 32768
\r
437 // instead. So, we must make this check to give a
\r
438 // uniform result among all browsers.
\r
439 if ( tabIndex !== 0 && this.$.tabIndex === 0 )
\r
447 var attr = this.$.attributes.getNamedItem( name ),
\r
448 attrValue = attr.specified ? attr.nodeValue // For value given by parser.
\r
449 : this.$.checked; // For value created via DOM interface.
\r
451 return attrValue ? 'checked' : null;
\r
455 return this.$.hspace;
\r
458 // IE does not return inline styles via getAttribute(). See #2947.
\r
459 return this.$.style.cssText;
\r
462 return standard.call( this, name );
\r
469 getChildren : function()
\r
471 return new CKEDITOR.dom.nodeList( this.$.childNodes );
\r
475 * Gets the current computed value of one of the element CSS style
\r
478 * @param {String} propertyName The style property name.
\r
479 * @returns {String} The property value.
\r
481 * var element = new CKEDITOR.dom.element( 'span' );
\r
482 * alert( <b>element.getComputedStyle( 'display' )</b> ); // "inline"
\r
486 function( propertyName )
\r
488 return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ];
\r
491 function( propertyName )
\r
493 return this.getWindow().$.getComputedStyle( this.$, '' ).getPropertyValue( propertyName );
\r
497 * Gets the DTD entries for this element.
\r
498 * @returns {Object} An object containing the list of elements accepted
\r
501 getDtd : function()
\r
503 var dtd = CKEDITOR.dtd[ this.getName() ];
\r
505 this.getDtd = function()
\r
513 getElementsByTag : CKEDITOR.dom.document.prototype.getElementsByTag,
\r
516 * Gets the computed tabindex for this element.
\r
518 * @returns {Number} The tabindex value.
\r
520 * var element = CKEDITOR.document.getById( 'myDiv' );
\r
521 * alert( <b>element.getTabIndex()</b> ); // e.g. "-1"
\r
527 var tabIndex = this.$.tabIndex;
\r
529 // IE returns tabIndex=0 by default for all elements. In
\r
530 // those cases we must check that the element really has
\r
531 // the tabindex attribute set to zero, or it is one of
\r
532 // those element that should have zero by default.
\r
533 if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 )
\r
538 : CKEDITOR.env.webkit ?
\r
541 var tabIndex = this.$.tabIndex;
\r
543 // Safari returns "undefined" for elements that should not
\r
544 // have tabindex (like a div). So, we must try to get it
\r
545 // from the attribute.
\r
546 // https://bugs.webkit.org/show_bug.cgi?id=20596
\r
547 if ( tabIndex == undefined )
\r
549 tabIndex = parseInt( this.getAttribute( 'tabindex' ), 10 );
\r
551 // If the element don't have the tabindex attribute,
\r
552 // then we should return -1.
\r
553 if ( isNaN( tabIndex ) )
\r
562 return this.$.tabIndex;
\r
566 * Gets the text value of this element.
\r
568 * Only in IE (which uses innerText), <br> will cause linebreaks,
\r
569 * and sucessive whitespaces (including line breaks) will be reduced to
\r
570 * a single space. This behavior is ok for us, for now. It may change
\r
572 * @returns {String} The text value.
\r
574 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Same <i>text</i>.</div>' );
\r
575 * alert( <b>element.getText()</b> ); // "Sample text."
\r
577 getText : function()
\r
579 return this.$.textContent || this.$.innerText || '';
\r
583 * Gets the window object that contains this element.
\r
584 * @returns {CKEDITOR.dom.window} The window object.
\r
587 getWindow : function()
\r
589 return this.getDocument().getWindow();
\r
593 * Gets the value of the "id" attribute of this element.
\r
594 * @returns {String} The element id, or null if not available.
\r
596 * var element = CKEDITOR.dom.element.createFromHtml( '<p id="myId"></p>' );
\r
597 * alert( <b>element.getId()</b> ); // "myId"
\r
601 return this.$.id || null;
\r
605 * Gets the value of the "name" attribute of this element.
\r
606 * @returns {String} The element name, or null if not available.
\r
608 * var element = CKEDITOR.dom.element.createFromHtml( '<input name="myName"></input>' );
\r
609 * alert( <b>element.getNameAtt()</b> ); // "myName"
\r
611 getNameAtt : function()
\r
613 return this.$.name || null;
\r
617 * Gets the element name (tag name). The returned name is guaranteed to
\r
618 * be always full lowercased.
\r
619 * @returns {String} The element name.
\r
621 * var element = new CKEDITOR.dom.element( 'span' );
\r
622 * alert( <b>element.getName()</b> ); // "span"
\r
624 getName : function()
\r
626 // Cache the lowercased name inside a closure.
\r
627 var nodeName = this.$.nodeName.toLowerCase();
\r
629 if ( CKEDITOR.env.ie && ! ( document.documentMode > 8 ) )
\r
631 var scopeName = this.$.scopeName;
\r
632 if ( scopeName != 'HTML' )
\r
633 nodeName = scopeName.toLowerCase() + ':' + nodeName;
\r
637 this.getName = function()
\r
644 * Gets the value set to this element. This value is usually available
\r
645 * for form field elements.
\r
646 * @returns {String} The element value.
\r
648 getValue : function()
\r
650 return this.$.value;
\r
654 * Gets the first child node of this element.
\r
655 * @param {Function} evaluator Filtering the result node.
\r
656 * @returns {CKEDITOR.dom.node} The first child node or null if not
\r
659 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
660 * var first = <b>element.getFirst()</b>;
\r
661 * alert( first.getName() ); // "b"
\r
663 getFirst : function( evaluator )
\r
665 var first = this.$.firstChild,
\r
666 retval = first && new CKEDITOR.dom.node( first );
\r
667 if ( retval && evaluator && !evaluator( retval ) )
\r
668 retval = retval.getNext( evaluator );
\r
674 * @param {Function} evaluator Filtering the result node.
\r
676 getLast : function( evaluator )
\r
678 var last = this.$.lastChild,
\r
679 retval = last && new CKEDITOR.dom.node( last );
\r
680 if ( retval && evaluator && !evaluator( retval ) )
\r
681 retval = retval.getPrevious( evaluator );
\r
686 getStyle : function( name )
\r
688 return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ];
\r
692 * Checks if the element name matches one or more names.
\r
693 * @param {String} name[,name[,...]] One or more names to be checked.
\r
694 * @returns {Boolean} true if the element name matches any of the names.
\r
696 * var element = new CKEDITOR.element( 'span' );
\r
697 * alert( <b>element.is( 'span' )</b> ); "true"
\r
698 * alert( <b>element.is( 'p', 'span' )</b> ); "true"
\r
699 * alert( <b>element.is( 'p' )</b> ); "false"
\r
700 * alert( <b>element.is( 'p', 'div' )</b> ); "false"
\r
704 var name = this.getName();
\r
705 for ( var i = 0 ; i < arguments.length ; i++ )
\r
707 if ( arguments[ i ] == name )
\r
713 isEditable : function()
\r
715 // Get the element name.
\r
716 var name = this.getName();
\r
718 // Get the element DTD (defaults to span for unknown elements).
\r
719 var dtd = !CKEDITOR.dtd.$nonEditable[ name ]
\r
720 && ( CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span );
\r
722 // In the DTD # == text node.
\r
723 return ( dtd && dtd['#'] );
\r
726 isIdentical : function( otherElement )
\r
728 if ( this.getName() != otherElement.getName() )
\r
731 var thisAttribs = this.$.attributes,
\r
732 otherAttribs = otherElement.$.attributes;
\r
734 var thisLength = thisAttribs.length,
\r
735 otherLength = otherAttribs.length;
\r
737 for ( var i = 0 ; i < thisLength ; i++ )
\r
739 var attribute = thisAttribs[ i ];
\r
741 if ( attribute.nodeName == '_moz_dirty' )
\r
744 if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != 'data-cke-expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )
\r
748 // For IE, we have to for both elements, because it's difficult to
\r
749 // know how the atttibutes collection is organized in its DOM.
\r
750 if ( CKEDITOR.env.ie )
\r
752 for ( i = 0 ; i < otherLength ; i++ )
\r
754 attribute = otherAttribs[ i ];
\r
755 if ( attribute.specified && attribute.nodeName != 'data-cke-expando'
\r
756 && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )
\r
765 * Checks if this element is visible. May not work if the element is
\r
766 * child of an element with visibility set to "hidden", but works well
\r
767 * on the great majority of cases.
\r
768 * @return {Boolean} True if the element is visible.
\r
770 isVisible : function()
\r
772 var isVisible = !!this.$.offsetHeight && this.getComputedStyle( 'visibility' ) != 'hidden',
\r
774 elementWindowFrame;
\r
776 // Webkit and Opera report non-zero offsetHeight despite that
\r
777 // element is inside an invisible iframe. (#4542)
\r
778 if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) )
\r
780 elementWindow = this.getWindow();
\r
782 if ( !elementWindow.equals( CKEDITOR.document.getWindow() )
\r
783 && ( elementWindowFrame = elementWindow.$.frameElement ) )
\r
785 isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible();
\r
793 * Whether it's an empty inline elements which has no visual impact when removed.
\r
795 isEmptyInlineRemoveable : function()
\r
797 if ( !CKEDITOR.dtd.$removeEmpty[ this.getName() ] )
\r
800 var children = this.getChildren();
\r
801 for ( var i = 0, count = children.count(); i < count; i++ )
\r
803 var child = children.getItem( i );
\r
805 if ( child.type == CKEDITOR.NODE_ELEMENT && child.data( 'cke-bookmark' ) )
\r
808 if ( child.type == CKEDITOR.NODE_ELEMENT && !child.isEmptyInlineRemoveable()
\r
809 || child.type == CKEDITOR.NODE_TEXT && CKEDITOR.tools.trim( child.getText() ) )
\r
818 * Indicates that the element has defined attributes.
\r
819 * @returns {Boolean} True if the element has attributes.
\r
821 * var element = CKEDITOR.dom.element.createFromHtml( '<div title="Test">Example</div>' );
\r
822 * alert( <b>element.hasAttributes()</b> ); "true"
\r
824 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Example</div>' );
\r
825 * alert( <b>element.hasAttributes()</b> ); "false"
\r
828 CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) ?
\r
831 var attributes = this.$.attributes;
\r
833 for ( var i = 0 ; i < attributes.length ; i++ )
\r
835 var attribute = attributes[i];
\r
837 switch ( attribute.nodeName )
\r
840 // IE has a strange bug. If calling removeAttribute('className'),
\r
841 // the attributes collection will still contain the "class"
\r
842 // attribute, which will be marked as "specified", even if the
\r
843 // outerHTML of the element is not displaying the class attribute.
\r
844 // Note : I was not able to reproduce it outside the editor,
\r
845 // but I've faced it while working on the TC of #1391.
\r
846 if ( this.getAttribute( 'class' ) )
\r
849 // Attributes to be ignored.
\r
850 case 'data-cke-expando' :
\r
856 if ( attribute.specified )
\r
866 var attrs = this.$.attributes,
\r
867 attrsNum = attrs.length;
\r
869 // The _moz_dirty attribute might get into the element after pasting (#5455)
\r
870 var execludeAttrs = { 'data-cke-expando' : 1, _moz_dirty : 1 };
\r
872 return attrsNum > 0 &&
\r
874 !execludeAttrs[ attrs[0].nodeName ] ||
\r
875 ( attrsNum == 2 && !execludeAttrs[ attrs[1].nodeName ] ) );
\r
879 * Indicates whether a specified attribute is defined for this element.
\r
880 * @returns {Boolean} True if the specified attribute is defined.
\r
881 * @param (String) name The attribute name.
\r
884 hasAttribute : function( name )
\r
886 var $attr = this.$.attributes.getNamedItem( name );
\r
887 return !!( $attr && $attr.specified );
\r
891 * Hides this element (display:none).
\r
893 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
894 * <b>element.hide()</b>;
\r
898 this.setStyle( 'display', 'none' );
\r
901 moveChildren : function( target, toStart )
\r
913 while ( ( child = $.lastChild ) )
\r
914 target.insertBefore( $.removeChild( child ), target.firstChild );
\r
918 while ( ( child = $.firstChild ) )
\r
919 target.appendChild( $.removeChild( child ) );
\r
923 mergeSiblings : ( function()
\r
925 function mergeElements( element, sibling, isNext )
\r
927 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )
\r
929 // Jumping over bookmark nodes and empty inline elements, e.g. <b><i></i></b>,
\r
930 // queuing them to be moved later. (#5567)
\r
931 var pendingNodes = [];
\r
933 while ( sibling.data( 'cke-bookmark' )
\r
934 || sibling.isEmptyInlineRemoveable() )
\r
936 pendingNodes.push( sibling );
\r
937 sibling = isNext ? sibling.getNext() : sibling.getPrevious();
\r
938 if ( !sibling || sibling.type != CKEDITOR.NODE_ELEMENT )
\r
942 if ( element.isIdentical( sibling ) )
\r
944 // Save the last child to be checked too, to merge things like
\r
945 // <b><i></i></b><b><i></i></b> => <b><i></i></b>
\r
946 var innerSibling = isNext ? element.getLast() : element.getFirst();
\r
948 // Move pending nodes first into the target element.
\r
949 while( pendingNodes.length )
\r
950 pendingNodes.shift().move( element, !isNext );
\r
952 sibling.moveChildren( element, !isNext );
\r
955 // Now check the last inner child (see two comments above).
\r
956 if ( innerSibling && innerSibling.type == CKEDITOR.NODE_ELEMENT )
\r
957 innerSibling.mergeSiblings();
\r
964 // Merge empty links and anchors also. (#5567)
\r
965 if ( !( CKEDITOR.dtd.$removeEmpty[ this.getName() ] || this.is( 'a' ) ) )
\r
968 mergeElements( this, this.getNext(), true );
\r
969 mergeElements( this, this.getPrevious() );
\r
974 * Shows this element (display it).
\r
976 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
977 * <b>element.show()</b>;
\r
989 * Sets the value of an element attribute.
\r
990 * @param {String} name The name of the attribute.
\r
991 * @param {String} value The value to be set to the attribute.
\r
993 * @returns {CKEDITOR.dom.element} This element instance.
\r
995 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
996 * <b>element.setAttribute( 'class', 'myClass' )</b>;
\r
997 * <b>element.setAttribute( 'title', 'This is an example' )</b>;
\r
999 setAttribute : (function()
\r
1001 var standard = function( name, value )
\r
1003 this.$.setAttribute( name, value );
\r
1007 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
1009 return function( name, value )
\r
1011 if ( name == 'class' )
\r
1012 this.$.className = value;
\r
1013 else if ( name == 'style' )
\r
1014 this.$.style.cssText = value;
\r
1015 else if ( name == 'tabindex' ) // Case sensitive.
\r
1016 this.$.tabIndex = value;
\r
1017 else if ( name == 'checked' )
\r
1018 this.$.checked = value;
\r
1020 standard.apply( this, arguments );
\r
1029 * Sets the value of several element attributes.
\r
1030 * @param {Object} attributesPairs An object containing the names and
\r
1031 * values of the attributes.
\r
1032 * @returns {CKEDITOR.dom.element} This element instance.
\r
1034 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1035 * <b>element.setAttributes({
\r
1036 * 'class' : 'myClass',
\r
1037 * 'title' : 'This is an example' })</b>;
\r
1039 setAttributes : function( attributesPairs )
\r
1041 for ( var name in attributesPairs )
\r
1042 this.setAttribute( name, attributesPairs[ name ] );
\r
1047 * Sets the element value. This function is usually used with form
\r
1049 * @param {String} value The element value.
\r
1050 * @returns {CKEDITOR.dom.element} This element instance.
\r
1052 setValue : function( value )
\r
1054 this.$.value = value;
\r
1059 * Removes an attribute from the element.
\r
1060 * @param {String} name The attribute name.
\r
1063 * var element = CKEDITOR.dom.element.createFromHtml( '<div class="classA"></div>' );
\r
1064 * element.removeAttribute( 'class' );
\r
1066 removeAttribute : (function()
\r
1068 var standard = function( name )
\r
1070 this.$.removeAttribute( name );
\r
1073 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
1075 return function( name )
\r
1077 if ( name == 'class' )
\r
1078 name = 'className';
\r
1079 else if ( name == 'tabindex' )
\r
1080 name = 'tabIndex';
\r
1081 standard.call( this, name );
\r
1088 removeAttributes : function ( attributes )
\r
1090 if ( CKEDITOR.tools.isArray( attributes ) )
\r
1092 for ( var i = 0 ; i < attributes.length ; i++ )
\r
1093 this.removeAttribute( attributes[ i ] );
\r
1097 for ( var attr in attributes )
\r
1098 attributes.hasOwnProperty( attr ) && this.removeAttribute( attr );
\r
1103 * Removes a style from the element.
\r
1104 * @param {String} name The style name.
\r
1107 * var element = CKEDITOR.dom.element.createFromHtml( '<div style="display:none"></div>' );
\r
1108 * element.removeStyle( 'display' );
\r
1110 removeStyle : function( name )
\r
1112 this.setStyle( name, '' );
\r
1113 if ( this.$.style.removeAttribute )
\r
1114 this.$.style.removeAttribute( CKEDITOR.tools.cssStyleToDomStyle( name ) );
\r
1116 if ( !this.$.style.cssText )
\r
1117 this.removeAttribute( 'style' );
\r
1121 * Sets the value of an element style.
\r
1122 * @param {String} name The name of the style. The CSS naming notation
\r
1123 * must be used (e.g. "background-color").
\r
1124 * @param {String} value The value to be set to the style.
\r
1125 * @returns {CKEDITOR.dom.element} This element instance.
\r
1127 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1128 * <b>element.setStyle( 'background-color', '#ff0000' )</b>;
\r
1129 * <b>element.setStyle( 'margin-top', '10px' )</b>;
\r
1130 * <b>element.setStyle( 'float', 'right' )</b>;
\r
1132 setStyle : function( name, value )
\r
1134 this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ] = value;
\r
1139 * Sets the value of several element styles.
\r
1140 * @param {Object} stylesPairs An object containing the names and
\r
1141 * values of the styles.
\r
1142 * @returns {CKEDITOR.dom.element} This element instance.
\r
1144 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1145 * <b>element.setStyles({
\r
1146 * 'position' : 'absolute',
\r
1147 * 'float' : 'right' })</b>;
\r
1149 setStyles : function( stylesPairs )
\r
1151 for ( var name in stylesPairs )
\r
1152 this.setStyle( name, stylesPairs[ name ] );
\r
1157 * Sets the opacity of an element.
\r
1158 * @param {Number} opacity A number within the range [0.0, 1.0].
\r
1160 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1161 * <b>element.setOpacity( 0.75 )</b>;
\r
1163 setOpacity : function( opacity )
\r
1165 if ( CKEDITOR.env.ie )
\r
1167 opacity = Math.round( opacity * 100 );
\r
1168 this.setStyle( 'filter', opacity >= 100 ? '' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')' );
\r
1171 this.setStyle( 'opacity', opacity );
\r
1175 * Makes the element and its children unselectable.
\r
1178 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1179 * element.unselectable();
\r
1182 CKEDITOR.env.gecko ?
\r
1185 this.$.style.MozUserSelect = 'none';
\r
1186 this.on( 'dragstart', function( evt ) { evt.data.preventDefault(); } );
\r
1188 : CKEDITOR.env.webkit ?
\r
1191 this.$.style.KhtmlUserSelect = 'none';
\r
1192 this.on( 'dragstart', function( evt ) { evt.data.preventDefault(); } );
\r
1197 if ( CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1199 var element = this.$,
\r
1203 element.unselectable = 'on';
\r
1205 while ( ( e = element.all[ i++ ] ) )
\r
1207 switch ( e.tagName.toLowerCase() )
\r
1213 /* Ignore the above tags */
\r
1216 e.unselectable = 'on';
\r
1222 getPositionedAncestor : function()
\r
1224 var current = this;
\r
1225 while ( current.getName() != 'html' )
\r
1227 if ( current.getComputedStyle( 'position' ) != 'static' )
\r
1230 current = current.getParent();
\r
1235 getDocumentPosition : function( refDocument )
\r
1238 body = this.getDocument().getBody(),
\r
1239 quirks = this.getDocument().$.compatMode == 'BackCompat';
\r
1241 var doc = this.getDocument();
\r
1243 if ( document.documentElement[ "getBoundingClientRect" ] )
\r
1245 var box = this.$.getBoundingClientRect(),
\r
1247 $docElem = $doc.documentElement;
\r
1249 var clientTop = $docElem.clientTop || body.$.clientTop || 0,
\r
1250 clientLeft = $docElem.clientLeft || body.$.clientLeft || 0,
\r
1251 needAdjustScrollAndBorders = true;
\r
1254 * #3804: getBoundingClientRect() works differently on IE and non-IE
\r
1255 * browsers, regarding scroll positions.
\r
1257 * On IE, the top position of the <html> element is always 0, no matter
\r
1258 * how much you scrolled down.
\r
1260 * On other browsers, the top position of the <html> element is negative
\r
1263 if ( CKEDITOR.env.ie )
\r
1265 var inDocElem = doc.getDocumentElement().contains( this ),
\r
1266 inBody = doc.getBody().contains( this );
\r
1268 needAdjustScrollAndBorders = ( quirks && inBody ) || ( !quirks && inDocElem );
\r
1271 if ( needAdjustScrollAndBorders )
\r
1273 x = box.left + ( !quirks && $docElem.scrollLeft || body.$.scrollLeft );
\r
1275 y = box.top + ( !quirks && $docElem.scrollTop || body.$.scrollTop );
\r
1281 var current = this, previous = null, offsetParent;
\r
1282 while ( current && !( current.getName() == 'body' || current.getName() == 'html' ) )
\r
1284 x += current.$.offsetLeft - current.$.scrollLeft;
\r
1285 y += current.$.offsetTop - current.$.scrollTop;
\r
1287 // Opera includes clientTop|Left into offsetTop|Left.
\r
1288 if ( !current.equals( this ) )
\r
1290 x += ( current.$.clientLeft || 0 );
\r
1291 y += ( current.$.clientTop || 0 );
\r
1294 var scrollElement = previous;
\r
1295 while ( scrollElement && !scrollElement.equals( current ) )
\r
1297 x -= scrollElement.$.scrollLeft;
\r
1298 y -= scrollElement.$.scrollTop;
\r
1299 scrollElement = scrollElement.getParent();
\r
1302 previous = current;
\r
1303 current = ( offsetParent = current.$.offsetParent ) ?
\r
1304 new CKEDITOR.dom.element( offsetParent ) : null;
\r
1308 if ( refDocument )
\r
1310 var currentWindow = this.getWindow(),
\r
1311 refWindow = refDocument.getWindow();
\r
1313 if ( !currentWindow.equals( refWindow ) && currentWindow.$.frameElement )
\r
1315 var iframePosition = ( new CKEDITOR.dom.element( currentWindow.$.frameElement ) ).getDocumentPosition( refDocument );
\r
1317 x += iframePosition.x;
\r
1318 y += iframePosition.y;
\r
1322 if ( !document.documentElement[ "getBoundingClientRect" ] )
\r
1324 // In Firefox, we'll endup one pixel before the element positions,
\r
1325 // so we must add it here.
\r
1326 if ( CKEDITOR.env.gecko && !quirks )
\r
1328 x += this.$.clientLeft ? 1 : 0;
\r
1329 y += this.$.clientTop ? 1 : 0;
\r
1333 return { x : x, y : y };
\r
1336 scrollIntoView : function( alignTop )
\r
1338 // Get the element window.
\r
1339 var win = this.getWindow(),
\r
1340 winHeight = win.getViewPaneSize().height;
\r
1342 // Starts from the offset that will be scrolled with the negative value of
\r
1343 // the visible window height.
\r
1344 var offset = winHeight * -1;
\r
1346 // Append the view pane's height if align to top.
\r
1347 // Append element height if we are aligning to the bottom.
\r
1349 offset += winHeight;
\r
1352 offset += this.$.offsetHeight || 0;
\r
1354 // Consider the margin in the scroll, which is ok for our current needs, but
\r
1355 // needs investigation if we will be using this function in other places.
\r
1356 offset += parseInt( this.getComputedStyle( 'marginBottom' ) || 0, 10 ) || 0;
\r
1359 // Append the offsets for the entire element hierarchy.
\r
1360 var elementPosition = this.getDocumentPosition();
\r
1361 offset += elementPosition.y;
\r
1363 // offset value might be out of range(nagative), fix it(#3692).
\r
1364 offset = offset < 0 ? 0 : offset;
\r
1366 // Scroll the window to the desired position, if not already visible(#3795).
\r
1367 var currentScroll = win.getScrollPosition().y;
\r
1368 if ( offset > currentScroll || offset < currentScroll - winHeight )
\r
1369 win.$.scrollTo( 0, offset );
\r
1372 setState : function( state )
\r
1376 case CKEDITOR.TRISTATE_ON :
\r
1377 this.addClass( 'cke_on' );
\r
1378 this.removeClass( 'cke_off' );
\r
1379 this.removeClass( 'cke_disabled' );
\r
1381 case CKEDITOR.TRISTATE_DISABLED :
\r
1382 this.addClass( 'cke_disabled' );
\r
1383 this.removeClass( 'cke_off' );
\r
1384 this.removeClass( 'cke_on' );
\r
1387 this.addClass( 'cke_off' );
\r
1388 this.removeClass( 'cke_on' );
\r
1389 this.removeClass( 'cke_disabled' );
\r
1395 * Returns the inner document of this IFRAME element.
\r
1396 * @returns {CKEDITOR.dom.document} The inner document.
\r
1398 getFrameDocument : function()
\r
1404 // In IE, with custom document.domain, it may happen that
\r
1405 // the iframe is not yet available, resulting in "Access
\r
1406 // Denied" for the following property access.
\r
1407 $.contentWindow.document;
\r
1411 // Trick to solve this issue, forcing the iframe to get ready
\r
1412 // by simply setting its "src" property.
\r
1415 // In IE6 though, the above is not enough, so we must pause the
\r
1416 // execution for a while, giving it time to think.
\r
1417 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )
\r
1419 window.showModalDialog(
\r
1420 'javascript:document.write("' +
\r
1422 'window.setTimeout(' +
\r
1423 'function(){window.close();}' +
\r
1429 return $ && new CKEDITOR.dom.document( $.contentWindow.document );
\r
1433 * Copy all the attributes from one node to the other, kinda like a clone
\r
1434 * skipAttributes is an object with the attributes that must NOT be copied.
\r
1435 * @param {CKEDITOR.dom.element} dest The destination element.
\r
1436 * @param {Object} skipAttributes A dictionary of attributes to skip.
\r
1439 copyAttributes : function( dest, skipAttributes )
\r
1441 var attributes = this.$.attributes;
\r
1442 skipAttributes = skipAttributes || {};
\r
1444 for ( var n = 0 ; n < attributes.length ; n++ )
\r
1446 var attribute = attributes[n];
\r
1448 // Lowercase attribute name hard rule is broken for
\r
1449 // some attribute on IE, e.g. CHECKED.
\r
1450 var attrName = attribute.nodeName.toLowerCase(),
\r
1453 // We can set the type only once, so do it with the proper value, not copying it.
\r
1454 if ( attrName in skipAttributes )
\r
1457 if ( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )
\r
1458 dest.setAttribute( attrName, attrValue );
\r
1459 // IE BUG: value attribute is never specified even if it exists.
\r
1460 else if ( attribute.specified ||
\r
1461 ( CKEDITOR.env.ie && attribute.nodeValue && attrName == 'value' ) )
\r
1463 attrValue = this.getAttribute( attrName );
\r
1464 if ( attrValue === null )
\r
1465 attrValue = attribute.nodeValue;
\r
1467 dest.setAttribute( attrName, attrValue );
\r
1472 if ( this.$.style.cssText !== '' )
\r
1473 dest.$.style.cssText = this.$.style.cssText;
\r
1477 * Changes the tag name of the current element.
\r
1478 * @param {String} newTag The new tag for the element.
\r
1480 renameNode : function( newTag )
\r
1482 // If it's already correct exit here.
\r
1483 if ( this.getName() == newTag )
\r
1486 var doc = this.getDocument();
\r
1488 // Create the new node.
\r
1489 var newNode = new CKEDITOR.dom.element( newTag, doc );
\r
1491 // Copy all attributes.
\r
1492 this.copyAttributes( newNode );
\r
1494 // Move children to the new node.
\r
1495 this.moveChildren( newNode );
\r
1497 // Replace the node.
\r
1498 this.getParent() && this.$.parentNode.replaceChild( newNode.$, this.$ );
\r
1499 newNode.$[ 'data-cke-expando' ] = this.$[ 'data-cke-expando' ];
\r
1500 this.$ = newNode.$;
\r
1504 * Gets a DOM tree descendant under the current node.
\r
1505 * @param {Array|Number} indices The child index or array of child indices under the node.
\r
1506 * @returns {CKEDITOR.dom.node} The specified DOM child under the current node. Null if child does not exist.
\r
1508 * var strong = p.getChild(0);
\r
1510 getChild : function( indices )
\r
1512 var rawNode = this.$;
\r
1514 if ( !indices.slice )
\r
1515 rawNode = rawNode.childNodes[ indices ];
\r
1518 while ( indices.length > 0 && rawNode )
\r
1519 rawNode = rawNode.childNodes[ indices.shift() ];
\r
1522 return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;
\r
1525 getChildCount : function()
\r
1527 return this.$.childNodes.length;
\r
1530 disableContextMenu : function()
\r
1532 this.on( 'contextmenu', function( event )
\r
1534 // Cancel the browser context menu.
\r
1535 if ( !event.data.getTarget().hasClass( 'cke_enable_context_menu' ) )
\r
1536 event.data.preventDefault();
\r
1541 * Gets element's direction. Supports both CSS 'direction' prop and 'dir' attr.
\r
1543 getDirection : function( useComputed )
\r
1545 return useComputed ? this.getComputedStyle( 'direction' ) : this.getStyle( 'direction' ) || this.getAttribute( 'dir' );
\r
1549 * Gets, sets and removes custom data to be stored as HTML5 data-* attributes.
\r
1550 * @name CKEDITOR.dom.element.data
\r
1551 * @param {String} name The name of the attribute, execluding the 'data-' part.
\r
1552 * @param {String} [value] The value to set. If set to false, the attribute will be removed.
\r
1554 data : function ( name, value )
\r
1556 name = 'data-' + name;
\r
1557 if ( value === undefined )
\r
1558 return this.getAttribute( name );
\r
1559 else if ( value === false )
\r
1560 this.removeAttribute( name );
\r
1562 this.setAttribute( name, value );
\r
1569 width : [ "border-left-width", "border-right-width","padding-left", "padding-right" ],
\r
1570 height : [ "border-top-width", "border-bottom-width", "padding-top", "padding-bottom" ]
\r
1573 function marginAndPaddingSize( type )
\r
1575 var adjustment = 0;
\r
1576 for ( var i = 0, len = sides[ type ].length; i < len; i++ )
\r
1577 adjustment += parseInt( this.getComputedStyle( sides [ type ][ i ] ) || 0, 10 ) || 0;
\r
1578 return adjustment;
\r
1582 * Update the element's size with box model awareness.
\r
1583 * @name CKEDITOR.dom.element.setSize
\r
1584 * @param {String} type [width|height]
\r
1585 * @param {Number} size The length unit in px.
\r
1586 * @param isBorderBox Apply the {@param width} and {@param height} based on border box model.
\r
1588 CKEDITOR.dom.element.prototype.setSize = function( type, size, isBorderBox )
\r
1590 if ( typeof size == 'number' )
\r
1592 if ( isBorderBox && !( CKEDITOR.env.ie && CKEDITOR.env.quirks ) )
\r
1593 size -= marginAndPaddingSize.call( this, type );
\r
1595 this.setStyle( type, size + 'px' );
\r
1600 * Get the element's size, possibly with box model awareness.
\r
1601 * @name CKEDITOR.dom.element.getSize
\r
1602 * @param {String} type [width|height]
\r
1603 * @param {Boolean} contentSize Get the {@param width} or {@param height} based on border box model.
\r
1605 CKEDITOR.dom.element.prototype.getSize = function( type, contentSize )
\r
1607 var size = Math.max( this.$[ 'offset' + CKEDITOR.tools.capitalize( type ) ],
\r
1608 this.$[ 'client' + CKEDITOR.tools.capitalize( type ) ] ) || 0;
\r
1610 if ( contentSize )
\r
1611 size -= marginAndPaddingSize.call( this, type );
\r