2 Copyright (c) 2003-2012, 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
311 * @param {Boolean} defer Whether to asynchronously defer the
\r
312 * execution by 100 ms.
\r
314 * var element = CKEDITOR.document.getById( 'myTextarea' );
\r
315 * <b>element.focus()</b>;
\r
317 focus : ( function()
\r
321 // IE throws error if the element is not visible.
\r
330 return function( defer )
\r
333 CKEDITOR.tools.setTimeout( exec, 100, this );
\r
340 * Gets the inner HTML of this element.
\r
341 * @returns {String} The inner HTML of this element.
\r
343 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
344 * alert( <b>p.getHtml()</b> ); // "<b>Example</b>"
\r
346 getHtml : function()
\r
348 var retval = this.$.innerHTML;
\r
349 // Strip <?xml:namespace> tags in IE. (#3341).
\r
350 return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;
\r
353 getOuterHtml : function()
\r
355 if ( this.$.outerHTML )
\r
357 // IE includes the <?xml:namespace> tag in the outerHTML of
\r
358 // namespaced element. So, we must strip it here. (#3341)
\r
359 return this.$.outerHTML.replace( /<\?[^>]*>/, '' );
\r
362 var tmpDiv = this.$.ownerDocument.createElement( 'div' );
\r
363 tmpDiv.appendChild( this.$.cloneNode( true ) );
\r
364 return tmpDiv.innerHTML;
\r
368 * Sets the inner HTML of this element.
\r
369 * @param {String} html The HTML to be set for this element.
\r
370 * @returns {String} The inserted HTML.
\r
372 * var p = new CKEDITOR.dom.element( 'p' );
\r
373 * <b>p.setHtml( '<b>Inner</b> HTML' );</b>
\r
375 * // result: "<p><b>Inner</b> HTML</p>"
\r
377 setHtml : function( html )
\r
379 return ( this.$.innerHTML = html );
\r
383 * Sets the element contents as plain text.
\r
384 * @param {String} text The text to be set.
\r
385 * @returns {String} The inserted text.
\r
387 * var element = new CKEDITOR.dom.element( 'div' );
\r
388 * element.setText( 'A > B & C < D' );
\r
389 * alert( element.innerHTML ); // "A &gt; B &amp; C &lt; D"
\r
391 setText : function( text )
\r
393 CKEDITOR.dom.element.prototype.setText = ( this.$.innerText != undefined ) ?
\r
396 return this.$.innerText = text;
\r
400 return this.$.textContent = text;
\r
403 return this.setText( text );
\r
407 * Gets the value of an element attribute.
\r
409 * @param {String} name The attribute name.
\r
410 * @returns {String} The attribute value or null if not defined.
\r
412 * var element = CKEDITOR.dom.element.createFromHtml( '<input type="text" />' );
\r
413 * alert( <b>element.getAttribute( 'type' )</b> ); // "text"
\r
415 getAttribute : (function()
\r
417 var standard = function( name )
\r
419 return this.$.getAttribute( name, 2 );
\r
422 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
424 return function( name )
\r
429 name = 'className';
\r
433 name = 'httpEquiv';
\r
437 return this.$.name;
\r
440 var tabIndex = standard.call( this, name );
\r
442 // IE returns tabIndex=0 by default for all
\r
443 // elements. For those elements,
\r
444 // getAtrribute( 'tabindex', 2 ) returns 32768
\r
445 // instead. So, we must make this check to give a
\r
446 // uniform result among all browsers.
\r
447 if ( tabIndex !== 0 && this.$.tabIndex === 0 )
\r
455 var attr = this.$.attributes.getNamedItem( name ),
\r
456 attrValue = attr.specified ? attr.nodeValue // For value given by parser.
\r
457 : this.$.checked; // For value created via DOM interface.
\r
459 return attrValue ? 'checked' : null;
\r
464 return this.$[ name ];
\r
467 // IE does not return inline styles via getAttribute(). See #2947.
\r
468 return this.$.style.cssText;
\r
470 case 'contenteditable':
\r
471 case 'contentEditable':
\r
472 return this.$.attributes.getNamedItem( 'contentEditable' ).specified ?
\r
473 this.$.getAttribute( 'contentEditable' ) : null;
\r
476 return standard.call( this, name );
\r
483 getChildren : function()
\r
485 return new CKEDITOR.dom.nodeList( this.$.childNodes );
\r
489 * Gets the current computed value of one of the element CSS style
\r
492 * @param {String} propertyName The style property name.
\r
493 * @returns {String} The property value.
\r
495 * var element = new CKEDITOR.dom.element( 'span' );
\r
496 * alert( <b>element.getComputedStyle( 'display' )</b> ); // "inline"
\r
500 function( propertyName )
\r
502 return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ];
\r
505 function( propertyName )
\r
507 return this.getWindow().$.getComputedStyle( this.$, '' ).getPropertyValue( propertyName );
\r
511 * Gets the DTD entries for this element.
\r
512 * @returns {Object} An object containing the list of elements accepted
\r
515 getDtd : function()
\r
517 var dtd = CKEDITOR.dtd[ this.getName() ];
\r
519 this.getDtd = function()
\r
527 getElementsByTag : CKEDITOR.dom.document.prototype.getElementsByTag,
\r
530 * Gets the computed tabindex for this element.
\r
532 * @returns {Number} The tabindex value.
\r
534 * var element = CKEDITOR.document.getById( 'myDiv' );
\r
535 * alert( <b>element.getTabIndex()</b> ); // e.g. "-1"
\r
541 var tabIndex = this.$.tabIndex;
\r
543 // IE returns tabIndex=0 by default for all elements. In
\r
544 // those cases we must check that the element really has
\r
545 // the tabindex attribute set to zero, or it is one of
\r
546 // those element that should have zero by default.
\r
547 if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 )
\r
552 : CKEDITOR.env.webkit ?
\r
555 var tabIndex = this.$.tabIndex;
\r
557 // Safari returns "undefined" for elements that should not
\r
558 // have tabindex (like a div). So, we must try to get it
\r
559 // from the attribute.
\r
560 // https://bugs.webkit.org/show_bug.cgi?id=20596
\r
561 if ( tabIndex == undefined )
\r
563 tabIndex = parseInt( this.getAttribute( 'tabindex' ), 10 );
\r
565 // If the element don't have the tabindex attribute,
\r
566 // then we should return -1.
\r
567 if ( isNaN( tabIndex ) )
\r
576 return this.$.tabIndex;
\r
580 * Gets the text value of this element.
\r
582 * Only in IE (which uses innerText), <br> will cause linebreaks,
\r
583 * and sucessive whitespaces (including line breaks) will be reduced to
\r
584 * a single space. This behavior is ok for us, for now. It may change
\r
586 * @returns {String} The text value.
\r
588 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Sample <i>text</i>.</div>' );
\r
589 * alert( <b>element.getText()</b> ); // "Sample text."
\r
591 getText : function()
\r
593 return this.$.textContent || this.$.innerText || '';
\r
597 * Gets the window object that contains this element.
\r
598 * @returns {CKEDITOR.dom.window} The window object.
\r
601 getWindow : function()
\r
603 return this.getDocument().getWindow();
\r
607 * Gets the value of the "id" attribute of this element.
\r
608 * @returns {String} The element id, or null if not available.
\r
610 * var element = CKEDITOR.dom.element.createFromHtml( '<p id="myId"></p>' );
\r
611 * alert( <b>element.getId()</b> ); // "myId"
\r
615 return this.$.id || null;
\r
619 * Gets the value of the "name" attribute of this element.
\r
620 * @returns {String} The element name, or null if not available.
\r
622 * var element = CKEDITOR.dom.element.createFromHtml( '<input name="myName"></input>' );
\r
623 * alert( <b>element.getNameAtt()</b> ); // "myName"
\r
625 getNameAtt : function()
\r
627 return this.$.name || null;
\r
631 * Gets the element name (tag name). The returned name is guaranteed to
\r
632 * be always full lowercased.
\r
633 * @returns {String} The element name.
\r
635 * var element = new CKEDITOR.dom.element( 'span' );
\r
636 * alert( <b>element.getName()</b> ); // "span"
\r
638 getName : function()
\r
640 // Cache the lowercased name inside a closure.
\r
641 var nodeName = this.$.nodeName.toLowerCase();
\r
643 if ( CKEDITOR.env.ie && ! ( document.documentMode > 8 ) )
\r
645 var scopeName = this.$.scopeName;
\r
646 if ( scopeName != 'HTML' )
\r
647 nodeName = scopeName.toLowerCase() + ':' + nodeName;
\r
651 this.getName = function()
\r
658 * Gets the value set to this element. This value is usually available
\r
659 * for form field elements.
\r
660 * @returns {String} The element value.
\r
662 getValue : function()
\r
664 return this.$.value;
\r
668 * Gets the first child node of this element.
\r
669 * @param {Function} evaluator Filtering the result node.
\r
670 * @returns {CKEDITOR.dom.node} The first child node or null if not
\r
673 * var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
\r
674 * var first = <b>element.getFirst()</b>;
\r
675 * alert( first.getName() ); // "b"
\r
677 getFirst : function( evaluator )
\r
679 var first = this.$.firstChild,
\r
680 retval = first && new CKEDITOR.dom.node( first );
\r
681 if ( retval && evaluator && !evaluator( retval ) )
\r
682 retval = retval.getNext( evaluator );
\r
688 * @param {Function} evaluator Filtering the result node.
\r
690 getLast : function( evaluator )
\r
692 var last = this.$.lastChild,
\r
693 retval = last && new CKEDITOR.dom.node( last );
\r
694 if ( retval && evaluator && !evaluator( retval ) )
\r
695 retval = retval.getPrevious( evaluator );
\r
700 getStyle : function( name )
\r
702 return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ];
\r
706 * Checks if the element name matches one or more names.
\r
707 * @param {String} name[,name[,...]] One or more names to be checked.
\r
708 * @returns {Boolean} true if the element name matches any of the names.
\r
710 * var element = new CKEDITOR.element( 'span' );
\r
711 * alert( <b>element.is( 'span' )</b> ); "true"
\r
712 * alert( <b>element.is( 'p', 'span' )</b> ); "true"
\r
713 * alert( <b>element.is( 'p' )</b> ); "false"
\r
714 * alert( <b>element.is( 'p', 'div' )</b> ); "false"
\r
718 var name = this.getName();
\r
719 for ( var i = 0 ; i < arguments.length ; i++ )
\r
721 if ( arguments[ i ] == name )
\r
728 * Decide whether one element is able to receive cursor.
\r
729 * @param {Boolean} [textCursor=true] Only consider element that could receive text child.
\r
731 isEditable : function( textCursor )
\r
733 var name = this.getName();
\r
735 if ( this.isReadOnly()
\r
736 || this.getComputedStyle( 'display' ) == 'none'
\r
737 || this.getComputedStyle( 'visibility' ) == 'hidden'
\r
738 || this.is( 'a' ) && this.data( 'cke-saved-name' ) && !this.getChildCount()
\r
739 || CKEDITOR.dtd.$nonEditable[ name ]
\r
740 || CKEDITOR.dtd.$empty[ name ] )
\r
745 if ( textCursor !== false )
\r
747 // Get the element DTD (defaults to span for unknown elements).
\r
748 var dtd = CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span;
\r
749 // In the DTD # == text node.
\r
750 return ( dtd && dtd[ '#'] );
\r
756 isIdentical : function( otherElement )
\r
758 if ( this.getName() != otherElement.getName() )
\r
761 var thisAttribs = this.$.attributes,
\r
762 otherAttribs = otherElement.$.attributes;
\r
764 var thisLength = thisAttribs.length,
\r
765 otherLength = otherAttribs.length;
\r
767 for ( var i = 0 ; i < thisLength ; i++ )
\r
769 var attribute = thisAttribs[ i ];
\r
771 if ( attribute.nodeName == '_moz_dirty' )
\r
774 if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != 'data-cke-expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )
\r
778 // For IE, we have to for both elements, because it's difficult to
\r
779 // know how the atttibutes collection is organized in its DOM.
\r
780 if ( CKEDITOR.env.ie )
\r
782 for ( i = 0 ; i < otherLength ; i++ )
\r
784 attribute = otherAttribs[ i ];
\r
785 if ( attribute.specified && attribute.nodeName != 'data-cke-expando'
\r
786 && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )
\r
795 * Checks if this element is visible. May not work if the element is
\r
796 * child of an element with visibility set to "hidden", but works well
\r
797 * on the great majority of cases.
\r
798 * @return {Boolean} True if the element is visible.
\r
800 isVisible : function()
\r
802 var isVisible = ( this.$.offsetHeight || this.$.offsetWidth ) && this.getComputedStyle( 'visibility' ) != 'hidden',
\r
804 elementWindowFrame;
\r
806 // Webkit and Opera report non-zero offsetHeight despite that
\r
807 // element is inside an invisible iframe. (#4542)
\r
808 if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) )
\r
810 elementWindow = this.getWindow();
\r
812 if ( !elementWindow.equals( CKEDITOR.document.getWindow() )
\r
813 && ( elementWindowFrame = elementWindow.$.frameElement ) )
\r
815 isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible();
\r
819 return !!isVisible;
\r
823 * Whether it's an empty inline elements which has no visual impact when removed.
\r
825 isEmptyInlineRemoveable : function()
\r
827 if ( !CKEDITOR.dtd.$removeEmpty[ this.getName() ] )
\r
830 var children = this.getChildren();
\r
831 for ( var i = 0, count = children.count(); i < count; i++ )
\r
833 var child = children.getItem( i );
\r
835 if ( child.type == CKEDITOR.NODE_ELEMENT && child.data( 'cke-bookmark' ) )
\r
838 if ( child.type == CKEDITOR.NODE_ELEMENT && !child.isEmptyInlineRemoveable()
\r
839 || child.type == CKEDITOR.NODE_TEXT && CKEDITOR.tools.trim( child.getText() ) )
\r
848 * Checks if the element has any defined attributes.
\r
850 * @returns {Boolean} True if the element has attributes.
\r
852 * var element = CKEDITOR.dom.element.createFromHtml( '<div title="Test">Example</div>' );
\r
853 * alert( <b>element.hasAttributes()</b> ); // "true"
\r
855 * var element = CKEDITOR.dom.element.createFromHtml( '<div>Example</div>' );
\r
856 * alert( <b>element.hasAttributes()</b> ); // "false"
\r
859 CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) ?
\r
862 var attributes = this.$.attributes;
\r
864 for ( var i = 0 ; i < attributes.length ; i++ )
\r
866 var attribute = attributes[i];
\r
868 switch ( attribute.nodeName )
\r
871 // IE has a strange bug. If calling removeAttribute('className'),
\r
872 // the attributes collection will still contain the "class"
\r
873 // attribute, which will be marked as "specified", even if the
\r
874 // outerHTML of the element is not displaying the class attribute.
\r
875 // Note : I was not able to reproduce it outside the editor,
\r
876 // but I've faced it while working on the TC of #1391.
\r
877 if ( this.getAttribute( 'class' ) )
\r
880 // Attributes to be ignored.
\r
881 case 'data-cke-expando' :
\r
887 if ( attribute.specified )
\r
897 var attrs = this.$.attributes,
\r
898 attrsNum = attrs.length;
\r
900 // The _moz_dirty attribute might get into the element after pasting (#5455)
\r
901 var execludeAttrs = { 'data-cke-expando' : 1, _moz_dirty : 1 };
\r
903 return attrsNum > 0 &&
\r
905 !execludeAttrs[ attrs[0].nodeName ] ||
\r
906 ( attrsNum == 2 && !execludeAttrs[ attrs[1].nodeName ] ) );
\r
910 * Checks if the specified attribute is defined for this element.
\r
911 * @returns {Boolean} True if the specified attribute is defined.
\r
912 * @param {String} name The attribute name.
\r
915 hasAttribute : (function()
\r
917 function standard( name )
\r
919 var $attr = this.$.attributes.getNamedItem( name );
\r
920 return !!( $attr && $attr.specified );
\r
923 return ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 ) ?
\r
926 // On IE < 8 the name attribute cannot be retrieved
\r
927 // right after the element creation and setting the
\r
928 // name with setAttribute.
\r
929 if ( name == 'name' )
\r
930 return !!this.$.name;
\r
932 return standard.call( this, name );
\r
939 * Hides this element (display:none).
\r
941 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
942 * <b>element.hide()</b>;
\r
946 this.setStyle( 'display', 'none' );
\r
949 moveChildren : function( target, toStart )
\r
961 while ( ( child = $.lastChild ) )
\r
962 target.insertBefore( $.removeChild( child ), target.firstChild );
\r
966 while ( ( child = $.firstChild ) )
\r
967 target.appendChild( $.removeChild( child ) );
\r
972 * Merges sibling elements that are identical to this one.<br>
\r
974 * Identical child elements are also merged. For example:<br>
\r
975 * <b><i></i></b><b><i></i></b> => <b><i></i></b>
\r
977 * @param {Boolean} [inlineOnly] Allow only inline elements to be merged. Defaults to "true".
\r
979 mergeSiblings : ( function()
\r
981 function mergeElements( element, sibling, isNext )
\r
983 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )
\r
985 // Jumping over bookmark nodes and empty inline elements, e.g. <b><i></i></b>,
\r
986 // queuing them to be moved later. (#5567)
\r
987 var pendingNodes = [];
\r
989 while ( sibling.data( 'cke-bookmark' )
\r
990 || sibling.isEmptyInlineRemoveable() )
\r
992 pendingNodes.push( sibling );
\r
993 sibling = isNext ? sibling.getNext() : sibling.getPrevious();
\r
994 if ( !sibling || sibling.type != CKEDITOR.NODE_ELEMENT )
\r
998 if ( element.isIdentical( sibling ) )
\r
1000 // Save the last child to be checked too, to merge things like
\r
1001 // <b><i></i></b><b><i></i></b> => <b><i></i></b>
\r
1002 var innerSibling = isNext ? element.getLast() : element.getFirst();
\r
1004 // Move pending nodes first into the target element.
\r
1005 while( pendingNodes.length )
\r
1006 pendingNodes.shift().move( element, !isNext );
\r
1008 sibling.moveChildren( element, !isNext );
\r
1011 // Now check the last inner child (see two comments above).
\r
1012 if ( innerSibling && innerSibling.type == CKEDITOR.NODE_ELEMENT )
\r
1013 innerSibling.mergeSiblings();
\r
1018 return function( inlineOnly )
\r
1020 if ( ! ( inlineOnly === false
\r
1021 || CKEDITOR.dtd.$removeEmpty[ this.getName() ]
\r
1022 || this.is( 'a' ) ) ) // Merge empty links and anchors also. (#5567)
\r
1027 mergeElements( this, this.getNext(), true );
\r
1028 mergeElements( this, this.getPrevious() );
\r
1033 * Shows this element (display it).
\r
1035 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1036 * <b>element.show()</b>;
\r
1048 * Sets the value of an element attribute.
\r
1049 * @param {String} name The name of the attribute.
\r
1050 * @param {String} value The value to be set to the attribute.
\r
1052 * @returns {CKEDITOR.dom.element} This element instance.
\r
1054 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1055 * <b>element.setAttribute( 'class', 'myClass' )</b>;
\r
1056 * <b>element.setAttribute( 'title', 'This is an example' )</b>;
\r
1058 setAttribute : (function()
\r
1060 var standard = function( name, value )
\r
1062 this.$.setAttribute( name, value );
\r
1066 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
1068 return function( name, value )
\r
1070 if ( name == 'class' )
\r
1071 this.$.className = value;
\r
1072 else if ( name == 'style' )
\r
1073 this.$.style.cssText = value;
\r
1074 else if ( name == 'tabindex' ) // Case sensitive.
\r
1075 this.$.tabIndex = value;
\r
1076 else if ( name == 'checked' )
\r
1077 this.$.checked = value;
\r
1078 else if ( name == 'contenteditable' )
\r
1079 standard.call( this, 'contentEditable', value );
\r
1081 standard.apply( this, arguments );
\r
1085 else if ( CKEDITOR.env.ie8Compat && CKEDITOR.env.secure )
\r
1087 return function( name, value )
\r
1089 // IE8 throws error when setting src attribute to non-ssl value. (#7847)
\r
1090 if ( name == 'src' && value.match( /^http:\/\// ) )
\r
1091 try { standard.apply( this, arguments ); } catch( e ){}
\r
1093 standard.apply( this, arguments );
\r
1102 * Sets the value of several element attributes.
\r
1103 * @param {Object} attributesPairs An object containing the names and
\r
1104 * values of the attributes.
\r
1105 * @returns {CKEDITOR.dom.element} This element instance.
\r
1107 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1108 * <b>element.setAttributes({
\r
1109 * 'class' : 'myClass',
\r
1110 * 'title' : 'This is an example' })</b>;
\r
1112 setAttributes : function( attributesPairs )
\r
1114 for ( var name in attributesPairs )
\r
1115 this.setAttribute( name, attributesPairs[ name ] );
\r
1120 * Sets the element value. This function is usually used with form
\r
1122 * @param {String} value The element value.
\r
1123 * @returns {CKEDITOR.dom.element} This element instance.
\r
1125 setValue : function( value )
\r
1127 this.$.value = value;
\r
1132 * Removes an attribute from the element.
\r
1133 * @param {String} name The attribute name.
\r
1136 * var element = CKEDITOR.dom.element.createFromHtml( '<div class="classA"></div>' );
\r
1137 * element.removeAttribute( 'class' );
\r
1139 removeAttribute : (function()
\r
1141 var standard = function( name )
\r
1143 this.$.removeAttribute( name );
\r
1146 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
1148 return function( name )
\r
1150 if ( name == 'class' )
\r
1151 name = 'className';
\r
1152 else if ( name == 'tabindex' )
\r
1153 name = 'tabIndex';
\r
1154 else if ( name == 'contenteditable' )
\r
1155 name = 'contentEditable';
\r
1156 standard.call( this, name );
\r
1163 removeAttributes : function ( attributes )
\r
1165 if ( CKEDITOR.tools.isArray( attributes ) )
\r
1167 for ( var i = 0 ; i < attributes.length ; i++ )
\r
1168 this.removeAttribute( attributes[ i ] );
\r
1172 for ( var attr in attributes )
\r
1173 attributes.hasOwnProperty( attr ) && this.removeAttribute( attr );
\r
1178 * Removes a style from the element.
\r
1179 * @param {String} name The style name.
\r
1182 * var element = CKEDITOR.dom.element.createFromHtml( '<div style="display:none"></div>' );
\r
1183 * element.removeStyle( 'display' );
\r
1185 removeStyle : function( name )
\r
1187 // Removes the specified property from the current style object.
\r
1188 var $ = this.$.style;
\r
1189 $.removeProperty ? $.removeProperty( name ) : $.removeAttribute( CKEDITOR.tools.cssStyleToDomStyle( name ) );
\r
1191 if ( !this.$.style.cssText )
\r
1192 this.removeAttribute( 'style' );
\r
1196 * Sets the value of an element style.
\r
1197 * @param {String} name The name of the style. The CSS naming notation
\r
1198 * must be used (e.g. "background-color").
\r
1199 * @param {String} value The value to be set to the style.
\r
1200 * @returns {CKEDITOR.dom.element} This element instance.
\r
1202 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1203 * <b>element.setStyle( 'background-color', '#ff0000' )</b>;
\r
1204 * <b>element.setStyle( 'margin-top', '10px' )</b>;
\r
1205 * <b>element.setStyle( 'float', 'right' )</b>;
\r
1207 setStyle : function( name, value )
\r
1209 this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ] = value;
\r
1214 * Sets the value of several element styles.
\r
1215 * @param {Object} stylesPairs An object containing the names and
\r
1216 * values of the styles.
\r
1217 * @returns {CKEDITOR.dom.element} This element instance.
\r
1219 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1220 * <b>element.setStyles({
\r
1221 * 'position' : 'absolute',
\r
1222 * 'float' : 'right' })</b>;
\r
1224 setStyles : function( stylesPairs )
\r
1226 for ( var name in stylesPairs )
\r
1227 this.setStyle( name, stylesPairs[ name ] );
\r
1232 * Sets the opacity of an element.
\r
1233 * @param {Number} opacity A number within the range [0.0, 1.0].
\r
1235 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1236 * <b>element.setOpacity( 0.75 )</b>;
\r
1238 setOpacity : function( opacity )
\r
1240 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
\r
1242 opacity = Math.round( opacity * 100 );
\r
1243 this.setStyle( 'filter', opacity >= 100 ? '' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')' );
\r
1246 this.setStyle( 'opacity', opacity );
\r
1250 * Makes the element and its children unselectable.
\r
1253 * var element = CKEDITOR.dom.element.getById( 'myElement' );
\r
1254 * element.unselectable();
\r
1257 CKEDITOR.env.gecko ?
\r
1260 this.$.style.MozUserSelect = 'none';
\r
1261 this.on( 'dragstart', function( evt ) { evt.data.preventDefault(); } );
\r
1263 : CKEDITOR.env.webkit ?
\r
1266 this.$.style.KhtmlUserSelect = 'none';
\r
1267 this.on( 'dragstart', function( evt ) { evt.data.preventDefault(); } );
\r
1272 if ( CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1274 var element = this.$,
\r
1275 elements = element.getElementsByTagName("*"),
\r
1279 element.unselectable = 'on';
\r
1281 while ( ( e = elements[ i++ ] ) )
\r
1283 switch ( e.tagName.toLowerCase() )
\r
1289 /* Ignore the above tags */
\r
1292 e.unselectable = 'on';
\r
1298 getPositionedAncestor : function()
\r
1300 var current = this;
\r
1301 while ( current.getName() != 'html' )
\r
1303 if ( current.getComputedStyle( 'position' ) != 'static' )
\r
1306 current = current.getParent();
\r
1311 getDocumentPosition : function( refDocument )
\r
1314 doc = this.getDocument(),
\r
1315 body = doc.getBody(),
\r
1316 quirks = doc.$.compatMode == 'BackCompat';
\r
1318 if ( document.documentElement[ "getBoundingClientRect" ] )
\r
1320 var box = this.$.getBoundingClientRect(),
\r
1322 $docElem = $doc.documentElement;
\r
1324 var clientTop = $docElem.clientTop || body.$.clientTop || 0,
\r
1325 clientLeft = $docElem.clientLeft || body.$.clientLeft || 0,
\r
1326 needAdjustScrollAndBorders = true;
\r
1329 * #3804: getBoundingClientRect() works differently on IE and non-IE
\r
1330 * browsers, regarding scroll positions.
\r
1332 * On IE, the top position of the <html> element is always 0, no matter
\r
1333 * how much you scrolled down.
\r
1335 * On other browsers, the top position of the <html> element is negative
\r
1338 if ( CKEDITOR.env.ie )
\r
1340 var inDocElem = doc.getDocumentElement().contains( this ),
\r
1341 inBody = doc.getBody().contains( this );
\r
1343 needAdjustScrollAndBorders = ( quirks && inBody ) || ( !quirks && inDocElem );
\r
1346 if ( needAdjustScrollAndBorders )
\r
1348 x = box.left + ( !quirks && $docElem.scrollLeft || body.$.scrollLeft );
\r
1350 y = box.top + ( !quirks && $docElem.scrollTop || body.$.scrollTop );
\r
1356 var current = this, previous = null, offsetParent;
\r
1357 while ( current && !( current.getName() == 'body' || current.getName() == 'html' ) )
\r
1359 x += current.$.offsetLeft - current.$.scrollLeft;
\r
1360 y += current.$.offsetTop - current.$.scrollTop;
\r
1362 // Opera includes clientTop|Left into offsetTop|Left.
\r
1363 if ( !current.equals( this ) )
\r
1365 x += ( current.$.clientLeft || 0 );
\r
1366 y += ( current.$.clientTop || 0 );
\r
1369 var scrollElement = previous;
\r
1370 while ( scrollElement && !scrollElement.equals( current ) )
\r
1372 x -= scrollElement.$.scrollLeft;
\r
1373 y -= scrollElement.$.scrollTop;
\r
1374 scrollElement = scrollElement.getParent();
\r
1377 previous = current;
\r
1378 current = ( offsetParent = current.$.offsetParent ) ?
\r
1379 new CKEDITOR.dom.element( offsetParent ) : null;
\r
1383 if ( refDocument )
\r
1385 var currentWindow = this.getWindow(),
\r
1386 refWindow = refDocument.getWindow();
\r
1388 if ( !currentWindow.equals( refWindow ) && currentWindow.$.frameElement )
\r
1390 var iframePosition = ( new CKEDITOR.dom.element( currentWindow.$.frameElement ) ).getDocumentPosition( refDocument );
\r
1392 x += iframePosition.x;
\r
1393 y += iframePosition.y;
\r
1397 if ( !document.documentElement[ "getBoundingClientRect" ] )
\r
1399 // In Firefox, we'll endup one pixel before the element positions,
\r
1400 // so we must add it here.
\r
1401 if ( CKEDITOR.env.gecko && !quirks )
\r
1403 x += this.$.clientLeft ? 1 : 0;
\r
1404 y += this.$.clientTop ? 1 : 0;
\r
1408 return { x : x, y : y };
\r
1412 * Make any page element visible inside the browser viewport.
\r
1413 * @param {Boolean} [alignToTop]
\r
1415 scrollIntoView : function( alignToTop )
\r
1417 var parent = this.getParent();
\r
1418 if ( !parent ) return;
\r
1420 // Scroll the element into parent container from the inner out.
\r
1423 // Check ancestors that overflows.
\r
1425 parent.$.clientWidth && parent.$.clientWidth < parent.$.scrollWidth
\r
1426 || parent.$.clientHeight && parent.$.clientHeight < parent.$.scrollHeight;
\r
1429 this.scrollIntoParent( parent, alignToTop, 1 );
\r
1431 // Walk across the frame.
\r
1432 if ( parent.is( 'html' ) )
\r
1434 var win = parent.getWindow();
\r
1436 // Avoid security error.
\r
1439 var iframe = win.$.frameElement;
\r
1440 iframe && ( parent = new CKEDITOR.dom.element( iframe ) );
\r
1445 while ( ( parent = parent.getParent() ) );
\r
1449 * Make any page element visible inside one of the ancestors by scrolling the parent.
\r
1450 * @param {CKEDITOR.dom.element|CKEDITOR.dom.window} parent The container to scroll into.
\r
1451 * @param {Boolean} [alignToTop] Align the element's top side with the container's
\r
1452 * when <code>true</code> is specified; align the bottom with viewport bottom when
\r
1453 * <code>false</code> is specified. Otherwise scroll on either side with the minimum
\r
1454 * amount to show the element.
\r
1455 * @param {Boolean} [hscroll] Whether horizontal overflow should be considered.
\r
1457 scrollIntoParent : function( parent, alignToTop, hscroll )
\r
1459 !parent && ( parent = this.getWindow() );
\r
1461 var doc = parent.getDocument();
\r
1462 var isQuirks = doc.$.compatMode == 'BackCompat';
\r
1464 // On window <html> is scrolled while quirks scrolls <body>.
\r
1465 if ( parent instanceof CKEDITOR.dom.window )
\r
1466 parent = isQuirks ? doc.getBody() : doc.getDocumentElement();
\r
1468 // Scroll the parent by the specified amount.
\r
1469 function scrollBy( x, y )
\r
1471 // Webkit doesn't support "scrollTop/scrollLeft"
\r
1472 // on documentElement/body element.
\r
1473 if ( /body|html/.test( parent.getName() ) )
\r
1474 parent.getWindow().$.scrollBy( x, y );
\r
1477 parent.$[ 'scrollLeft' ] += x;
\r
1478 parent.$[ 'scrollTop' ] += y;
\r
1482 // Figure out the element position relative to the specified window.
\r
1483 function screenPos( element, refWin )
\r
1485 var pos = { x: 0, y: 0 };
\r
1487 if ( !( element.is( isQuirks ? 'body' : 'html' ) ) )
\r
1489 var box = element.$.getBoundingClientRect();
\r
1490 pos.x = box.left, pos.y = box.top;
\r
1493 var win = element.getWindow();
\r
1494 if ( !win.equals( refWin ) )
\r
1496 var outerPos = screenPos( CKEDITOR.dom.element.get( win.$.frameElement ), refWin );
\r
1497 pos.x += outerPos.x, pos.y += outerPos.y;
\r
1503 // calculated margin size.
\r
1504 function margin( element, side )
\r
1506 return parseInt( element.getComputedStyle( 'margin-' + side ) || 0, 10 ) || 0;
\r
1509 var win = parent.getWindow();
\r
1511 var thisPos = screenPos( this, win ),
\r
1512 parentPos = screenPos( parent, win ),
\r
1513 eh = this.$.offsetHeight,
\r
1514 ew = this.$.offsetWidth,
\r
1515 ch = parent.$.clientHeight,
\r
1516 cw = parent.$.clientWidth,
\r
1520 // Left-top margins.
\r
1523 x : thisPos.x - margin( this, 'left' ) - parentPos.x || 0,
\r
1524 y : thisPos.y - margin( this, 'top' ) - parentPos.y|| 0
\r
1527 // Bottom-right margins.
\r
1530 x : thisPos.x + ew + margin( this, 'right' ) - ( ( parentPos.x ) + cw ) || 0,
\r
1531 y : thisPos.y + eh + margin( this, 'bottom' ) - ( ( parentPos.y ) + ch ) || 0
\r
1534 // 1. Do the specified alignment as much as possible;
\r
1535 // 2. Otherwise be smart to scroll only the minimum amount;
\r
1536 // 3. Never cut at the top;
\r
1537 // 4. DO NOT scroll when already visible.
\r
1538 if ( lt.y < 0 || br.y > 0 )
\r
1541 alignToTop === true ? lt.y :
\r
1542 alignToTop === false ? br.y :
\r
1543 lt.y < 0 ? lt.y : br.y );
\r
1546 if ( hscroll && ( lt.x < 0 || br.x > 0 ) )
\r
1547 scrollBy( lt.x < 0 ? lt.x : br.x, 0 );
\r
1550 setState : function( state )
\r
1554 case CKEDITOR.TRISTATE_ON :
\r
1555 this.addClass( 'cke_on' );
\r
1556 this.removeClass( 'cke_off' );
\r
1557 this.removeClass( 'cke_disabled' );
\r
1559 case CKEDITOR.TRISTATE_DISABLED :
\r
1560 this.addClass( 'cke_disabled' );
\r
1561 this.removeClass( 'cke_off' );
\r
1562 this.removeClass( 'cke_on' );
\r
1565 this.addClass( 'cke_off' );
\r
1566 this.removeClass( 'cke_on' );
\r
1567 this.removeClass( 'cke_disabled' );
\r
1573 * Returns the inner document of this IFRAME element.
\r
1574 * @returns {CKEDITOR.dom.document} The inner document.
\r
1576 getFrameDocument : function()
\r
1582 // In IE, with custom document.domain, it may happen that
\r
1583 // the iframe is not yet available, resulting in "Access
\r
1584 // Denied" for the following property access.
\r
1585 $.contentWindow.document;
\r
1589 // Trick to solve this issue, forcing the iframe to get ready
\r
1590 // by simply setting its "src" property.
\r
1593 // In IE6 though, the above is not enough, so we must pause the
\r
1594 // execution for a while, giving it time to think.
\r
1595 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )
\r
1597 window.showModalDialog(
\r
1598 'javascript:document.write("' +
\r
1600 'window.setTimeout(' +
\r
1601 'function(){window.close();}' +
\r
1607 return $ && new CKEDITOR.dom.document( $.contentWindow.document );
\r
1611 * Copy all the attributes from one node to the other, kinda like a clone
\r
1612 * skipAttributes is an object with the attributes that must NOT be copied.
\r
1613 * @param {CKEDITOR.dom.element} dest The destination element.
\r
1614 * @param {Object} skipAttributes A dictionary of attributes to skip.
\r
1617 copyAttributes : function( dest, skipAttributes )
\r
1619 var attributes = this.$.attributes;
\r
1620 skipAttributes = skipAttributes || {};
\r
1622 for ( var n = 0 ; n < attributes.length ; n++ )
\r
1624 var attribute = attributes[n];
\r
1626 // Lowercase attribute name hard rule is broken for
\r
1627 // some attribute on IE, e.g. CHECKED.
\r
1628 var attrName = attribute.nodeName.toLowerCase(),
\r
1631 // We can set the type only once, so do it with the proper value, not copying it.
\r
1632 if ( attrName in skipAttributes )
\r
1635 if ( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )
\r
1636 dest.setAttribute( attrName, attrValue );
\r
1637 // IE BUG: value attribute is never specified even if it exists.
\r
1638 else if ( attribute.specified ||
\r
1639 ( CKEDITOR.env.ie && attribute.nodeValue && attrName == 'value' ) )
\r
1641 attrValue = this.getAttribute( attrName );
\r
1642 if ( attrValue === null )
\r
1643 attrValue = attribute.nodeValue;
\r
1645 dest.setAttribute( attrName, attrValue );
\r
1650 if ( this.$.style.cssText !== '' )
\r
1651 dest.$.style.cssText = this.$.style.cssText;
\r
1655 * Changes the tag name of the current element.
\r
1656 * @param {String} newTag The new tag for the element.
\r
1658 renameNode : function( newTag )
\r
1660 // If it's already correct exit here.
\r
1661 if ( this.getName() == newTag )
\r
1664 var doc = this.getDocument();
\r
1666 // Create the new node.
\r
1667 var newNode = new CKEDITOR.dom.element( newTag, doc );
\r
1669 // Copy all attributes.
\r
1670 this.copyAttributes( newNode );
\r
1672 // Move children to the new node.
\r
1673 this.moveChildren( newNode );
\r
1675 // Replace the node.
\r
1676 this.getParent() && this.$.parentNode.replaceChild( newNode.$, this.$ );
\r
1677 newNode.$[ 'data-cke-expando' ] = this.$[ 'data-cke-expando' ];
\r
1678 this.$ = newNode.$;
\r
1682 * Gets a DOM tree descendant under the current node.
\r
1683 * @param {Array|Number} indices The child index or array of child indices under the node.
\r
1684 * @returns {CKEDITOR.dom.node} The specified DOM child under the current node. Null if child does not exist.
\r
1686 * var strong = p.getChild(0);
\r
1688 getChild : function( indices )
\r
1690 var rawNode = this.$;
\r
1692 if ( !indices.slice )
\r
1693 rawNode = rawNode.childNodes[ indices ];
\r
1696 while ( indices.length > 0 && rawNode )
\r
1697 rawNode = rawNode.childNodes[ indices.shift() ];
\r
1700 return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;
\r
1703 getChildCount : function()
\r
1705 return this.$.childNodes.length;
\r
1708 disableContextMenu : function()
\r
1710 this.on( 'contextmenu', function( event )
\r
1712 // Cancel the browser context menu.
\r
1713 if ( !event.data.getTarget().hasClass( 'cke_enable_context_menu' ) )
\r
1714 event.data.preventDefault();
\r
1719 * Gets element's direction. Supports both CSS 'direction' prop and 'dir' attr.
\r
1721 getDirection : function( useComputed )
\r
1723 return useComputed ?
\r
1724 this.getComputedStyle( 'direction' )
\r
1725 // Webkit: offline element returns empty direction (#8053).
\r
1726 || this.getDirection()
\r
1727 || this.getDocument().$.dir
\r
1728 || this.getDocument().getBody().getDirection( 1 )
\r
1729 : this.getStyle( 'direction' ) || this.getAttribute( 'dir' );
\r
1733 * Gets, sets and removes custom data to be stored as HTML5 data-* attributes.
\r
1734 * @param {String} name The name of the attribute, excluding the 'data-' part.
\r
1735 * @param {String} [value] The value to set. If set to false, the attribute will be removed.
\r
1737 * element.data( 'extra-info', 'test' ); // appended the attribute data-extra-info="test" to the element
\r
1738 * alert( element.data( 'extra-info' ) ); // "test"
\r
1739 * element.data( 'extra-info', false ); // remove the data-extra-info attribute from the element
\r
1741 data : function ( name, value )
\r
1743 name = 'data-' + name;
\r
1744 if ( value === undefined )
\r
1745 return this.getAttribute( name );
\r
1746 else if ( value === false )
\r
1747 this.removeAttribute( name );
\r
1749 this.setAttribute( name, value );
\r
1758 width : [ "border-left-width", "border-right-width","padding-left", "padding-right" ],
\r
1759 height : [ "border-top-width", "border-bottom-width", "padding-top", "padding-bottom" ]
\r
1762 function marginAndPaddingSize( type )
\r
1764 var adjustment = 0;
\r
1765 for ( var i = 0, len = sides[ type ].length; i < len; i++ )
\r
1766 adjustment += parseInt( this.getComputedStyle( sides [ type ][ i ] ) || 0, 10 ) || 0;
\r
1767 return adjustment;
\r
1771 * Sets the element size considering the box model.
\r
1772 * @name CKEDITOR.dom.element.prototype.setSize
\r
1774 * @param {String} type The dimension to set. It accepts "width" and "height".
\r
1775 * @param {Number} size The length unit in px.
\r
1776 * @param {Boolean} isBorderBox Apply the size based on the border box model.
\r
1778 CKEDITOR.dom.element.prototype.setSize = function( type, size, isBorderBox )
\r
1780 if ( typeof size == 'number' )
\r
1782 if ( isBorderBox && !( CKEDITOR.env.ie && CKEDITOR.env.quirks ) )
\r
1783 size -= marginAndPaddingSize.call( this, type );
\r
1785 this.setStyle( type, size + 'px' );
\r
1790 * Gets the element size, possibly considering the box model.
\r
1791 * @name CKEDITOR.dom.element.prototype.getSize
\r
1793 * @param {String} type The dimension to get. It accepts "width" and "height".
\r
1794 * @param {Boolean} isBorderBox Get the size based on the border box model.
\r
1796 CKEDITOR.dom.element.prototype.getSize = function( type, isBorderBox )
\r
1798 var size = Math.max( this.$[ 'offset' + CKEDITOR.tools.capitalize( type ) ],
\r
1799 this.$[ 'client' + CKEDITOR.tools.capitalize( type ) ] ) || 0;
\r
1801 if ( isBorderBox )
\r
1802 size -= marginAndPaddingSize.call( this, type );
\r