JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
77031f07f3ebeac03bdf5d4df72f00025ca01d89
[ckeditor.git] / _source / core / dom / element.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.dom.element} class, which\r
8  *              represents a DOM element.\r
9  */\r
10 \r
11 /**\r
12  * Represents a DOM element.\r
13  * @constructor\r
14  * @augments CKEDITOR.dom.node\r
15  * @param {Object|String} element A native DOM element or the element name for\r
16  *              new elements.\r
17  * @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain\r
18  *              the element in case of element creation.\r
19  * @example\r
20  * // Create a new <span> element.\r
21  * var element = new CKEDITOR.dom.element( 'span' );\r
22  * @example\r
23  * // Create an element based on a native DOM element.\r
24  * var element = new CKEDITOR.dom.element( document.getElementById( 'myId' ) );\r
25  */\r
26 CKEDITOR.dom.element = function( element, ownerDocument )\r
27 {\r
28         if ( typeof element == 'string' )\r
29                 element = ( ownerDocument ? ownerDocument.$ : document ).createElement( element );\r
30 \r
31         // Call the base constructor (we must not call CKEDITOR.dom.node).\r
32         CKEDITOR.dom.domObject.call( this, element );\r
33 };\r
34 \r
35 // PACKAGER_RENAME( CKEDITOR.dom.element )\r
36 \r
37 /**\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
42  * @example\r
43  * var element = new CKEDITOR.dom.element( 'span' );\r
44  * alert( element == <b>CKEDITOR.dom.element.get( element )</b> );  "true"\r
45  * @example\r
46  * var element = document.getElementById( 'myElement' );\r
47  * alert( <b>CKEDITOR.dom.element.get( element )</b>.getName() );  e.g. "p"\r
48  */\r
49 CKEDITOR.dom.element.get = function( element )\r
50 {\r
51         return element && ( element.$ ? element : new CKEDITOR.dom.element( element ) );\r
52 };\r
53 \r
54 CKEDITOR.dom.element.prototype = new CKEDITOR.dom.node();\r
55 \r
56 /**\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
61  *              siblings.\r
62  * @returns {CKEDITOR.dom.element} The element instance.\r
63  * @example\r
64  * var element = <b>CKEDITOR.dom.element.createFromHtml( '&lt;strong class="anyclass"&gt;My element&lt;/strong&gt;' )</b>;\r
65  * alert( element.getName() );  // "strong"\r
66  */\r
67 CKEDITOR.dom.element.createFromHtml = function( html, ownerDocument )\r
68 {\r
69         var temp = new CKEDITOR.dom.element( 'div', ownerDocument );\r
70         temp.setHtml( html );\r
71 \r
72         // When returning the node, remove it from its parent to detach it.\r
73         return temp.getFirst().remove();\r
74 };\r
75 \r
76 CKEDITOR.dom.element.setMarker = function( database, element, name, value )\r
77 {\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
84 \r
85         return element.setCustomData( name, value );\r
86 };\r
87 \r
88 CKEDITOR.dom.element.clearAllMarkers = function( database )\r
89 {\r
90         for ( var i in database )\r
91                 CKEDITOR.dom.element.clearMarkers( database, database[i], true );\r
92 };\r
93 \r
94 CKEDITOR.dom.element.clearMarkers = function( database, element, removeFromDatabase )\r
95 {\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
102         {\r
103                 element.removeCustomData( 'list_marker_id' );\r
104                 delete database[id];\r
105         }\r
106 };\r
107 \r
108 CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype,\r
109         /** @lends CKEDITOR.dom.element.prototype */\r
110         {\r
111                 /**\r
112                  * The node type. This is a constant value set to\r
113                  * {@link CKEDITOR.NODE_ELEMENT}.\r
114                  * @type Number\r
115                  * @example\r
116                  */\r
117                 type : CKEDITOR.NODE_ELEMENT,\r
118 \r
119                 /**\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
123                  * @example\r
124                  * var element = new CKEDITOR.dom.element( 'div' );\r
125                  * element.addClass( 'classA' );  // &lt;div class="classA"&gt;\r
126                  * element.addClass( 'classB' );  // &lt;div class="classA classB"&gt;\r
127                  * element.addClass( 'classA' );  // &lt;div class="classA classB"&gt;\r
128                  */\r
129                 addClass : function( className )\r
130                 {\r
131                         var c = this.$.className;\r
132                         if ( c )\r
133                         {\r
134                                 var regex = new RegExp( '(?:^|\\s)' + className + '(?:\\s|$)', '' );\r
135                                 if ( !regex.test( c ) )\r
136                                         c += ' ' + className;\r
137                         }\r
138                         this.$.className = c || className;\r
139                 },\r
140 \r
141                 /**\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
145                  * @example\r
146                  * var element = new CKEDITOR.dom.element( 'div' );\r
147                  * element.addClass( 'classA' );  // &lt;div class="classA"&gt;\r
148                  * element.addClass( 'classB' );  // &lt;div class="classA classB"&gt;\r
149                  * element.removeClass( 'classA' );  // &lt;div class="classB"&gt;\r
150                  * element.removeClass( 'classB' );  // &lt;div&gt;\r
151                  */\r
152                 removeClass : function( className )\r
153                 {\r
154                         var c = this.getAttribute( 'class' );\r
155                         if ( c )\r
156                         {\r
157                                 var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', 'i' );\r
158                                 if ( regex.test( c ) )\r
159                                 {\r
160                                         c = c.replace( regex, '' ).replace( /^\s+/, '' );\r
161 \r
162                                         if ( c )\r
163                                                 this.setAttribute( 'class', c );\r
164                                         else\r
165                                                 this.removeAttribute( 'class' );\r
166                                 }\r
167                         }\r
168                 },\r
169 \r
170                 hasClass : function( className )\r
171                 {\r
172                         var regex = new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)', '' );\r
173                         return regex.test( this.getAttribute('class') );\r
174                 },\r
175 \r
176                 /**\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
179                  *              appended.\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
183                  * @example\r
184                  * var p = new CKEDITOR.dom.element( 'p' );\r
185                  *\r
186                  * var strong = new CKEDITOR.dom.element( 'strong' );\r
187                  * <b>p.append( strong );</b>\r
188                  *\r
189                  * var em = <b>p.append( 'em' );</b>\r
190                  *\r
191                  * // result: "&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;"\r
192                  */\r
193                 append : function( node, toStart )\r
194                 {\r
195                         if ( typeof node == 'string' )\r
196                                 node = this.getDocument().createElement( node );\r
197 \r
198                         if ( toStart )\r
199                                 this.$.insertBefore( node.$, this.$.firstChild );\r
200                         else\r
201                                 this.$.appendChild( node.$ );\r
202 \r
203                         return node;\r
204                 },\r
205 \r
206                 appendHtml : function( html )\r
207                 {\r
208                         if ( !this.$.childNodes.length )\r
209                                 this.setHtml( html );\r
210                         else\r
211                         {\r
212                                 var temp = new CKEDITOR.dom.element( 'div', this.getDocument() );\r
213                                 temp.setHtml( html );\r
214                                 temp.moveChildren( this );\r
215                         }\r
216                 },\r
217 \r
218                 /**\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
222                  * @example\r
223                  * var p = new CKEDITOR.dom.element( 'p' );\r
224                  * p.appendText( 'This is' );\r
225                  * p.appendText( ' some text' );\r
226                  *\r
227                  * // result: "&lt;p&gt;This is some text&lt;/p&gt;"\r
228                  */\r
229                 appendText : function( text )\r
230                 {\r
231                         if ( this.$.text != undefined )\r
232                                 this.$.text += text;\r
233                         else\r
234                                 this.append( new CKEDITOR.dom.text( text ) );\r
235                 },\r
236 \r
237                 appendBogus : function()\r
238                 {\r
239                         var lastChild = this.getLast() ;\r
240 \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
245                         {\r
246                                 var bogus = CKEDITOR.env.opera ?\r
247                                                 this.getDocument().createText('') :\r
248                                                 this.getDocument().createElement( 'br' );\r
249 \r
250                                 CKEDITOR.env.gecko && bogus.setAttribute( 'type', '_moz' );\r
251 \r
252                                 this.append( bogus );\r
253                         }\r
254                 },\r
255 \r
256                 /**\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
260                  * @example\r
261                  * // Before breaking:\r
262                  * //     &lt;b&gt;This &lt;i&gt;is some&lt;span /&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
263                  * // If "element" is &lt;span /&gt; and "parent" is &lt;i&gt;:\r
264                  * //     &lt;b&gt;This &lt;i&gt;is some&lt;/i&gt;&lt;span /&gt;&lt;i&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
265                  * element.breakParent( parent );\r
266                  * @example\r
267                  * // Before breaking:\r
268                  * //     &lt;b&gt;This &lt;i&gt;is some&lt;span /&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
269                  * // If "element" is &lt;span /&gt; and "parent" is &lt;b&gt;:\r
270                  * //     &lt;b&gt;This &lt;i&gt;is some&lt;/i&gt;&lt;/b&gt;&lt;span /&gt;&lt;b&gt;&lt;i&gt; sample&lt;/i&gt; test text&lt;/b&gt;\r
271                  * element.breakParent( parent );\r
272                  */\r
273                 breakParent : function( parent )\r
274                 {\r
275                         var range = new CKEDITOR.dom.range( this.getDocument() );\r
276 \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
281 \r
282                         // Extract it.\r
283                         var docFrag = range.extractContents();\r
284 \r
285                         // Move the element outside the broken element.\r
286                         range.insertNode( this.remove() );\r
287 \r
288                         // Re-insert the extracted piece after the element.\r
289                         docFrag.insertAfterNode( this );\r
290                 },\r
291 \r
292                 contains :\r
293                         CKEDITOR.env.ie || CKEDITOR.env.webkit ?\r
294                                 function( node )\r
295                                 {\r
296                                         var $ = this.$;\r
297 \r
298                                         return node.type != CKEDITOR.NODE_ELEMENT ?\r
299                                                 $.contains( node.getParent().$ ) :\r
300                                                 $ != node.$ && $.contains( node.$ );\r
301                                 }\r
302                         :\r
303                                 function( node )\r
304                                 {\r
305                                         return !!( this.$.compareDocumentPosition( node.$ ) & 16 );\r
306                                 },\r
307 \r
308                 /**\r
309                  * Moves the selection focus to this element.\r
310                  * @example\r
311                  * var element = CKEDITOR.document.getById( 'myTextarea' );\r
312                  * <b>element.focus()</b>;\r
313                  */\r
314                 focus : function()\r
315                 {\r
316                         // IE throws error if the element is not visible.\r
317                         try\r
318                         {\r
319                                 this.$.focus();\r
320                         }\r
321                         catch (e)\r
322                         {}\r
323                 },\r
324 \r
325                 /**\r
326                  * Gets the inner HTML of this element.\r
327                  * @returns {String} The inner HTML of this element.\r
328                  * @example\r
329                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;div&gt;&lt;b&gt;Example&lt;/b&gt;&lt;/div&gt;' );\r
330                  * alert( <b>p.getHtml()</b> );  // "&lt;b&gt;Example&lt;/b&gt;"\r
331                  */\r
332                 getHtml : function()\r
333                 {\r
334                         var retval = this.$.innerHTML;\r
335                         // Strip <?xml:namespace> tags in IE. (#3341).\r
336                         return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;\r
337                 },\r
338 \r
339                 getOuterHtml : function()\r
340                 {\r
341                         if ( this.$.outerHTML )\r
342                         {\r
343                                 // IE includes the <?xml:namespace> tag in the outerHTML of\r
344                                 // namespaced element. So, we must strip it here. (#3341)\r
345                                 return this.$.outerHTML.replace( /<\?[^>]*>/, '' );\r
346                         }\r
347 \r
348                         var tmpDiv = this.$.ownerDocument.createElement( 'div' );\r
349                         tmpDiv.appendChild( this.$.cloneNode( true ) );\r
350                         return tmpDiv.innerHTML;\r
351                 },\r
352 \r
353                 /**\r
354                  * Sets the inner HTML of this element.\r
355                  * @param {String} html The HTML to be set for this element.\r
356                  * @returns {String} The inserted HTML.\r
357                  * @example\r
358                  * var p = new CKEDITOR.dom.element( 'p' );\r
359                  * <b>p.setHtml( '&lt;b&gt;Inner&lt;/b&gt; HTML' );</b>\r
360                  *\r
361                  * // result: "&lt;p&gt;&lt;b&gt;Inner&lt;/b&gt; HTML&lt;/p&gt;"\r
362                  */\r
363                 setHtml : function( html )\r
364                 {\r
365                         return ( this.$.innerHTML = html );\r
366                 },\r
367 \r
368                 /**\r
369                  * Sets the element contents as plain text.\r
370                  * @param {String} text The text to be set.\r
371                  * @returns {String} The inserted text.\r
372                  * @example\r
373                  * var element = new CKEDITOR.dom.element( 'div' );\r
374                  * element.setText( 'A > B & C < D' );\r
375                  * alert( element.innerHTML );  // "A &amp;gt; B &amp;amp; C &amp;lt; D"\r
376                  */\r
377                 setText : function( text )\r
378                 {\r
379                         CKEDITOR.dom.element.prototype.setText = ( this.$.innerText != undefined ) ?\r
380                                 function ( text )\r
381                                 {\r
382                                         return this.$.innerText = text;\r
383                                 } :\r
384                                 function ( text )\r
385                                 {\r
386                                         return this.$.textContent = text;\r
387                                 };\r
388 \r
389                         return this.setText( text );\r
390                 },\r
391 \r
392                 /**\r
393                  * Gets the value of an element attribute.\r
394                  * @function\r
395                  * @param {String} name The attribute name.\r
396                  * @returns {String} The attribute value or null if not defined.\r
397                  * @example\r
398                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;input type="text" /&gt;' );\r
399                  * alert( <b>element.getAttribute( 'type' )</b> );  // "text"\r
400                  */\r
401                 getAttribute : (function()\r
402                 {\r
403                         var standard = function( name )\r
404                         {\r
405                                 return this.$.getAttribute( name, 2 );\r
406                         };\r
407 \r
408                         if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )\r
409                         {\r
410                                 return function( name )\r
411                                 {\r
412                                         switch ( name )\r
413                                         {\r
414                                                 case 'class':\r
415                                                         name = 'className';\r
416                                                         break;\r
417 \r
418                                                 case 'tabindex':\r
419                                                         var tabIndex = standard.call( this, name );\r
420 \r
421                                                         // IE returns tabIndex=0 by default for all\r
422                                                         // elements. For those elements,\r
423                                                         // getAtrribute( 'tabindex', 2 ) returns 32768\r
424                                                         // instead. So, we must make this check to give a\r
425                                                         // uniform result among all browsers.\r
426                                                         if ( tabIndex !== 0 && this.$.tabIndex === 0 )\r
427                                                                 tabIndex = null;\r
428 \r
429                                                         return tabIndex;\r
430                                                         break;\r
431 \r
432                                                 case 'checked':\r
433                                                 {\r
434                                                         var attr = this.$.attributes.getNamedItem( name ),\r
435                                                                 attrValue = attr.specified ? attr.nodeValue     // For value given by parser.\r
436                                                                                                                          : this.$.checked;  // For value created via DOM interface.\r
437 \r
438                                                         return attrValue ? 'checked' : null;\r
439                                                 }\r
440 \r
441                                                 case 'hspace':\r
442                                                         return this.$.hspace;\r
443 \r
444                                                 case 'style':\r
445                                                         // IE does not return inline styles via getAttribute(). See #2947.\r
446                                                         return this.$.style.cssText;\r
447                                         }\r
448 \r
449                                         return standard.call( this, name );\r
450                                 };\r
451                         }\r
452                         else\r
453                                 return standard;\r
454                 })(),\r
455 \r
456                 getChildren : function()\r
457                 {\r
458                         return new CKEDITOR.dom.nodeList( this.$.childNodes );\r
459                 },\r
460 \r
461                 /**\r
462                  * Gets the current computed value of one of the element CSS style\r
463                  * properties.\r
464                  * @function\r
465                  * @param {String} propertyName The style property name.\r
466                  * @returns {String} The property value.\r
467                  * @example\r
468                  * var element = new CKEDITOR.dom.element( 'span' );\r
469                  * alert( <b>element.getComputedStyle( 'display' )</b> );  // "inline"\r
470                  */\r
471                 getComputedStyle :\r
472                         CKEDITOR.env.ie ?\r
473                                 function( propertyName )\r
474                                 {\r
475                                         return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ];\r
476                                 }\r
477                         :\r
478                                 function( propertyName )\r
479                                 {\r
480                                         return this.getWindow().$.getComputedStyle( this.$, '' ).getPropertyValue( propertyName );\r
481                                 },\r
482 \r
483                 /**\r
484                  * Gets the DTD entries for this element.\r
485                  * @returns {Object} An object containing the list of elements accepted\r
486                  *              by this element.\r
487                  */\r
488                 getDtd : function()\r
489                 {\r
490                         var dtd = CKEDITOR.dtd[ this.getName() ];\r
491 \r
492                         this.getDtd = function()\r
493                         {\r
494                                 return dtd;\r
495                         };\r
496 \r
497                         return dtd;\r
498                 },\r
499 \r
500                 getElementsByTag : CKEDITOR.dom.document.prototype.getElementsByTag,\r
501 \r
502                 /**\r
503                  * Gets the computed tabindex for this element.\r
504                  * @function\r
505                  * @returns {Number} The tabindex value.\r
506                  * @example\r
507                  * var element = CKEDITOR.document.getById( 'myDiv' );\r
508                  * alert( <b>element.getTabIndex()</b> );  // e.g. "-1"\r
509                  */\r
510                 getTabIndex :\r
511                         CKEDITOR.env.ie ?\r
512                                 function()\r
513                                 {\r
514                                         var tabIndex = this.$.tabIndex;\r
515 \r
516                                         // IE returns tabIndex=0 by default for all elements. In\r
517                                         // those cases we must check that the element really has\r
518                                         // the tabindex attribute set to zero, or it is one of\r
519                                         // those element that should have zero by default.\r
520                                         if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 )\r
521                                                 tabIndex = -1;\r
522 \r
523                                                 return tabIndex;\r
524                                 }\r
525                         : CKEDITOR.env.webkit ?\r
526                                 function()\r
527                                 {\r
528                                         var tabIndex = this.$.tabIndex;\r
529 \r
530                                         // Safari returns "undefined" for elements that should not\r
531                                         // have tabindex (like a div). So, we must try to get it\r
532                                         // from the attribute.\r
533                                         // https://bugs.webkit.org/show_bug.cgi?id=20596\r
534                                         if ( tabIndex == undefined )\r
535                                         {\r
536                                                 tabIndex = parseInt( this.getAttribute( 'tabindex' ), 10 );\r
537 \r
538                                                 // If the element don't have the tabindex attribute,\r
539                                                 // then we should return -1.\r
540                                                 if ( isNaN( tabIndex ) )\r
541                                                         tabIndex = -1;\r
542                                         }\r
543 \r
544                                         return tabIndex;\r
545                                 }\r
546                         :\r
547                                 function()\r
548                                 {\r
549                                         return this.$.tabIndex;\r
550                                 },\r
551 \r
552                 /**\r
553                  * Gets the text value of this element.\r
554                  *\r
555                  * Only in IE (which uses innerText), &lt;br&gt; will cause linebreaks,\r
556                  * and sucessive whitespaces (including line breaks) will be reduced to\r
557                  * a single space. This behavior is ok for us, for now. It may change\r
558                  * in the future.\r
559                  * @returns {String} The text value.\r
560                  * @example\r
561                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;div&gt;Same &lt;i&gt;text&lt;/i&gt;.&lt;/div&gt;' );\r
562                  * alert( <b>element.getText()</b> );  // "Sample text."\r
563                  */\r
564                 getText : function()\r
565                 {\r
566                         return this.$.textContent || this.$.innerText || '';\r
567                 },\r
568 \r
569                 /**\r
570                  * Gets the window object that contains this element.\r
571                  * @returns {CKEDITOR.dom.window} The window object.\r
572                  * @example\r
573                  */\r
574                 getWindow : function()\r
575                 {\r
576                         return this.getDocument().getWindow();\r
577                 },\r
578 \r
579                 /**\r
580                  * Gets the value of the "id" attribute of this element.\r
581                  * @returns {String} The element id, or null if not available.\r
582                  * @example\r
583                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;p id="myId"&gt;&lt;/p&gt;' );\r
584                  * alert( <b>element.getId()</b> );  // "myId"\r
585                  */\r
586                 getId : function()\r
587                 {\r
588                         return this.$.id || null;\r
589                 },\r
590 \r
591                 /**\r
592                  * Gets the value of the "name" attribute of this element.\r
593                  * @returns {String} The element name, or null if not available.\r
594                  * @example\r
595                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;input name="myName"&gt;&lt;/input&gt;' );\r
596                  * alert( <b>element.getNameAtt()</b> );  // "myName"\r
597                  */\r
598                 getNameAtt : function()\r
599                 {\r
600                         return this.$.name || null;\r
601                 },\r
602 \r
603                 /**\r
604                  * Gets the element name (tag name). The returned name is guaranteed to\r
605                  * be always full lowercased.\r
606                  * @returns {String} The element name.\r
607                  * @example\r
608                  * var element = new CKEDITOR.dom.element( 'span' );\r
609                  * alert( <b>element.getName()</b> );  // "span"\r
610                  */\r
611                 getName : function()\r
612                 {\r
613                         // Cache the lowercased name inside a closure.\r
614                         var nodeName = this.$.nodeName.toLowerCase();\r
615 \r
616                         if ( CKEDITOR.env.ie )\r
617                         {\r
618                                 var scopeName = this.$.scopeName;\r
619                                 if ( scopeName != 'HTML' )\r
620                                         nodeName = scopeName.toLowerCase() + ':' + nodeName;\r
621                         }\r
622 \r
623                         return (\r
624                         this.getName = function()\r
625                                 {\r
626                                         return nodeName;\r
627                                 })();\r
628                 },\r
629 \r
630                 /**\r
631                  * Gets the value set to this element. This value is usually available\r
632                  * for form field elements.\r
633                  * @returns {String} The element value.\r
634                  */\r
635                 getValue : function()\r
636                 {\r
637                         return this.$.value;\r
638                 },\r
639 \r
640                 /**\r
641                  * Gets the first child node of this element.\r
642                  * @param {Function} evaluator Filtering the result node.\r
643                  * @returns {CKEDITOR.dom.node} The first child node or null if not\r
644                  *              available.\r
645                  * @example\r
646                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;div&gt;&lt;b&gt;Example&lt;/b&gt;&lt;/div&gt;' );\r
647                  * var first = <b>element.getFirst()</b>;\r
648                  * alert( first.getName() );  // "b"\r
649                  */\r
650                 getFirst : function( evaluator )\r
651                 {\r
652                         var first = this.$.firstChild,\r
653                                 retval = first && new CKEDITOR.dom.node( first );\r
654                         if ( retval && evaluator && !evaluator( retval ) )\r
655                                 retval = retval.getNext( evaluator );\r
656 \r
657                         return retval;\r
658                 },\r
659 \r
660                 /**\r
661                  * @param {Function} evaluator Filtering the result node.\r
662                  */\r
663                 getLast : function( evaluator )\r
664                 {\r
665                         var last = this.$.lastChild,\r
666                                 retval = last && new CKEDITOR.dom.node( last );\r
667                         if ( retval && evaluator && !evaluator( retval ) )\r
668                                 retval = retval.getPrevious( evaluator );\r
669 \r
670                         return retval;\r
671                 },\r
672 \r
673                 getStyle : function( name )\r
674                 {\r
675                         return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ];\r
676                 },\r
677 \r
678                 /**\r
679                  * Checks if the element name matches one or more names.\r
680                  * @param {String} name[,name[,...]] One or more names to be checked.\r
681                  * @returns {Boolean} true if the element name matches any of the names.\r
682                  * @example\r
683                  * var element = new CKEDITOR.element( 'span' );\r
684                  * alert( <b>element.is( 'span' )</b> );  "true"\r
685                  * alert( <b>element.is( 'p', 'span' )</b> );  "true"\r
686                  * alert( <b>element.is( 'p' )</b> );  "false"\r
687                  * alert( <b>element.is( 'p', 'div' )</b> );  "false"\r
688                  */\r
689                 is : function()\r
690                 {\r
691                         var name = this.getName();\r
692                         for ( var i = 0 ; i < arguments.length ; i++ )\r
693                         {\r
694                                 if ( arguments[ i ] == name )\r
695                                         return true;\r
696                         }\r
697                         return false;\r
698                 },\r
699 \r
700                 isEditable : function()\r
701                 {\r
702                         // Get the element name.\r
703                         var name = this.getName();\r
704 \r
705                         // Get the element DTD (defaults to span for unknown elements).\r
706                         var dtd = !CKEDITOR.dtd.$nonEditable[ name ]\r
707                                                 && ( CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span );\r
708 \r
709                         // In the DTD # == text node.\r
710                         return ( dtd && dtd['#'] );\r
711                 },\r
712 \r
713                 isIdentical : function( otherElement )\r
714                 {\r
715                         if ( this.getName() != otherElement.getName() )\r
716                                 return false;\r
717 \r
718                         var thisAttribs = this.$.attributes,\r
719                                 otherAttribs = otherElement.$.attributes;\r
720 \r
721                         var thisLength = thisAttribs.length,\r
722                                 otherLength = otherAttribs.length;\r
723 \r
724                         if ( !CKEDITOR.env.ie && thisLength != otherLength )\r
725                                 return false;\r
726 \r
727                         for ( var i = 0 ; i < thisLength ; i++ )\r
728                         {\r
729                                 var attribute = thisAttribs[ i ];\r
730 \r
731                                 if ( ( !CKEDITOR.env.ie || ( attribute.specified && attribute.nodeName != '_cke_expando' ) ) && attribute.nodeValue != otherElement.getAttribute( attribute.nodeName ) )\r
732                                         return false;\r
733                         }\r
734 \r
735                         // For IE, we have to for both elements, because it's difficult to\r
736                         // know how the atttibutes collection is organized in its DOM.\r
737                         if ( CKEDITOR.env.ie )\r
738                         {\r
739                                 for ( i = 0 ; i < otherLength ; i++ )\r
740                                 {\r
741                                         attribute = otherAttribs[ i ];\r
742                                         if ( attribute.specified && attribute.nodeName != '_cke_expando'\r
743                                                         && attribute.nodeValue != this.getAttribute( attribute.nodeName ) )\r
744                                                 return false;\r
745                                 }\r
746                         }\r
747 \r
748                         return true;\r
749                 },\r
750 \r
751                 /**\r
752                  * Checks if this element is visible. May not work if the element is\r
753                  * child of an element with visibility set to "hidden", but works well\r
754                  * on the great majority of cases.\r
755                  * @return {Boolean} True if the element is visible.\r
756                  */\r
757                 isVisible : function()\r
758                 {\r
759                         var isVisible = !!this.$.offsetHeight && this.getComputedStyle( 'visibility' ) != 'hidden',\r
760                                 elementWindow,\r
761                                 elementWindowFrame;\r
762 \r
763                         // Webkit and Opera report non-zero offsetHeight despite that\r
764                         // element is inside an invisible iframe. (#4542)\r
765                         if ( isVisible && ( CKEDITOR.env.webkit || CKEDITOR.env.opera ) )\r
766                         {\r
767                                 elementWindow = this.getWindow();\r
768 \r
769                                 if ( !elementWindow.equals( CKEDITOR.document.getWindow() )\r
770                                                 && ( elementWindowFrame = elementWindow.$.frameElement ) )\r
771                                 {\r
772                                         isVisible = new CKEDITOR.dom.element( elementWindowFrame ).isVisible();\r
773                                 }\r
774                         }\r
775 \r
776                         return isVisible;\r
777                 },\r
778 \r
779                 /**\r
780                  * Whether it's an empty inline elements which has no visual impact when removed.\r
781                  */\r
782                 isEmptyInlineRemoveable : function()\r
783                 {\r
784                         if ( !CKEDITOR.dtd.$removeEmpty[ this.getName() ] )\r
785                                 return false;\r
786 \r
787                         var children = this.getChildren();\r
788                         for ( var i = 0, count = children.count(); i < count; i++ )\r
789                         {\r
790                                 var child = children.getItem( i );\r
791 \r
792                                 if ( child.type == CKEDITOR.NODE_ELEMENT && child.getAttribute( '_cke_bookmark' ) )\r
793                                         continue;\r
794 \r
795                                 if ( child.type == CKEDITOR.NODE_ELEMENT && !child.isEmptyInlineRemoveable()\r
796                                         || child.type == CKEDITOR.NODE_TEXT && CKEDITOR.tools.trim( child.getText() ) )\r
797                                 {\r
798                                         return false;\r
799                                 }\r
800                         }\r
801                         return true;\r
802                 },\r
803 \r
804                 /**\r
805                  * Indicates that the element has defined attributes.\r
806                  * @returns {Boolean} True if the element has attributes.\r
807                  * @example\r
808                  * var element = CKEDITOR.dom.element.createFromHtml( '<div title="Test">Example</div>' );\r
809                  * alert( <b>element.hasAttributes()</b> );  "true"\r
810                  * @example\r
811                  * var element = CKEDITOR.dom.element.createFromHtml( '<div>Example</div>' );\r
812                  * alert( <b>element.hasAttributes()</b> );  "false"\r
813                  */\r
814                 hasAttributes :\r
815                         CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) ?\r
816                                 function()\r
817                                 {\r
818                                         var attributes = this.$.attributes;\r
819 \r
820                                         for ( var i = 0 ; i < attributes.length ; i++ )\r
821                                         {\r
822                                                 var attribute = attributes[i];\r
823 \r
824                                                 switch ( attribute.nodeName )\r
825                                                 {\r
826                                                         case 'class' :\r
827                                                                 // IE has a strange bug. If calling removeAttribute('className'),\r
828                                                                 // the attributes collection will still contain the "class"\r
829                                                                 // attribute, which will be marked as "specified", even if the\r
830                                                                 // outerHTML of the element is not displaying the class attribute.\r
831                                                                 // Note : I was not able to reproduce it outside the editor,\r
832                                                                 // but I've faced it while working on the TC of #1391.\r
833                                                                 if ( this.getAttribute( 'class' ) )\r
834                                                                         return true;\r
835 \r
836                                                         // Attributes to be ignored.\r
837                                                         case '_cke_expando' :\r
838                                                                 continue;\r
839 \r
840                                                         /*jsl:fallthru*/\r
841 \r
842                                                         default :\r
843                                                                 if ( attribute.specified )\r
844                                                                         return true;\r
845                                                 }\r
846                                         }\r
847 \r
848                                         return false;\r
849                                 }\r
850                         :\r
851                                 function()\r
852                                 {\r
853                                         var attrs = this.$.attributes,\r
854                                                 attrsNum = attrs.length;\r
855 \r
856                                         // The _moz_dirty attribute might get into the element after pasting (#5455)\r
857                                         var execludeAttrs = { _cke_expando : 1, _moz_dirty : 1 };\r
858 \r
859                                         return attrsNum > 0 &&\r
860                                                 ( attrsNum > 2 ||\r
861                                                         !execludeAttrs[ attrs[0].nodeName ] ||\r
862                                                         ( attrsNum == 2 && !execludeAttrs[ attrs[1].nodeName ] ) );\r
863                                 },\r
864 \r
865                 /**\r
866                  * Indicates whether a specified attribute is defined for this element.\r
867                  * @returns {Boolean} True if the specified attribute is defined.\r
868                  * @param (String) name The attribute name.\r
869                  * @example\r
870                  */\r
871                 hasAttribute : function( name )\r
872                 {\r
873                         var $attr = this.$.attributes.getNamedItem( name );\r
874                         return !!( $attr && $attr.specified );\r
875                 },\r
876 \r
877                 /**\r
878                  * Hides this element (display:none).\r
879                  * @example\r
880                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
881                  * <b>element.hide()</b>;\r
882                  */\r
883                 hide : function()\r
884                 {\r
885                         this.setStyle( 'display', 'none' );\r
886                 },\r
887 \r
888                 moveChildren : function( target, toStart )\r
889                 {\r
890                         var $ = this.$;\r
891                         target = target.$;\r
892 \r
893                         if ( $ == target )\r
894                                 return;\r
895 \r
896                         var child;\r
897 \r
898                         if ( toStart )\r
899                         {\r
900                                 while ( ( child = $.lastChild ) )\r
901                                         target.insertBefore( $.removeChild( child ), target.firstChild );\r
902                         }\r
903                         else\r
904                         {\r
905                                 while ( ( child = $.firstChild ) )\r
906                                         target.appendChild( $.removeChild( child ) );\r
907                         }\r
908                 },\r
909 \r
910                 mergeSiblings : ( function()\r
911                 {\r
912                         function mergeElements( element, sibling, isNext )\r
913                         {\r
914                                 if ( sibling && sibling.type == CKEDITOR.NODE_ELEMENT )\r
915                                 {\r
916                                         // Jumping over bookmark nodes and empty inline elements, e.g. <b><i></i></b>,\r
917                                         // queuing them to be moved later. (#5567)\r
918                                         var pendingNodes = [];\r
919 \r
920                                         while ( sibling.getAttribute( '_cke_bookmark' )\r
921                                                 || sibling.isEmptyInlineRemoveable() )\r
922                                         {\r
923                                                 pendingNodes.push( sibling );\r
924                                                 sibling = isNext ? sibling.getNext() : sibling.getPrevious();\r
925                                                 if ( !sibling || sibling.type != CKEDITOR.NODE_ELEMENT )\r
926                                                         return;\r
927                                         }\r
928 \r
929                                         if ( element.isIdentical( sibling ) )\r
930                                         {\r
931                                                 // Save the last child to be checked too, to merge things like\r
932                                                 // <b><i></i></b><b><i></i></b> => <b><i></i></b>\r
933                                                 var innerSibling = isNext ? element.getLast() : element.getFirst();\r
934 \r
935                                                 // Move pending nodes first into the target element.\r
936                                                 while( pendingNodes.length )\r
937                                                         pendingNodes.shift().move( element, !isNext );\r
938 \r
939                                                 sibling.moveChildren( element, !isNext );\r
940                                                 sibling.remove();\r
941 \r
942                                                 // Now check the last inner child (see two comments above).\r
943                                                 if ( innerSibling && innerSibling.type == CKEDITOR.NODE_ELEMENT )\r
944                                                         innerSibling.mergeSiblings();\r
945                                         }\r
946                                 }\r
947                         }\r
948 \r
949                         return function()\r
950                                 {\r
951                                         // Merge empty links and anchors also. (#5567)\r
952                                         if ( !( CKEDITOR.dtd.$removeEmpty[ this.getName() ] || this.is( 'a' ) ) )\r
953                                                 return;\r
954 \r
955                                         mergeElements( this, this.getNext(), true );\r
956                                         mergeElements( this, this.getPrevious() );\r
957                                 };\r
958                 } )(),\r
959 \r
960                 /**\r
961                  * Shows this element (display it).\r
962                  * @example\r
963                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
964                  * <b>element.show()</b>;\r
965                  */\r
966                 show : function()\r
967                 {\r
968                         this.setStyles(\r
969                                 {\r
970                                         display : '',\r
971                                         visibility : ''\r
972                                 });\r
973                 },\r
974 \r
975                 /**\r
976                  * Sets the value of an element attribute.\r
977                  * @param {String} name The name of the attribute.\r
978                  * @param {String} value The value to be set to the attribute.\r
979                  * @function\r
980                  * @returns {CKEDITOR.dom.element} This element instance.\r
981                  * @example\r
982                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
983                  * <b>element.setAttribute( 'class', 'myClass' )</b>;\r
984                  * <b>element.setAttribute( 'title', 'This is an example' )</b>;\r
985                  */\r
986                 setAttribute : (function()\r
987                 {\r
988                         var standard = function( name, value )\r
989                         {\r
990                                 this.$.setAttribute( name, value );\r
991                                 return this;\r
992                         };\r
993 \r
994                         if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )\r
995                         {\r
996                                 return function( name, value )\r
997                                 {\r
998                                         if ( name == 'class' )\r
999                                                 this.$.className = value;\r
1000                                         else if ( name == 'style' )\r
1001                                                 this.$.style.cssText = value;\r
1002                                         else if ( name == 'tabindex' )  // Case sensitive.\r
1003                                                 this.$.tabIndex = value;\r
1004                                         else if ( name == 'checked' )\r
1005                                                 this.$.checked = value;\r
1006                                         else\r
1007                                                 standard.apply( this, arguments );\r
1008                                         return this;\r
1009                                 };\r
1010                         }\r
1011                         else\r
1012                                 return standard;\r
1013                 })(),\r
1014 \r
1015                 /**\r
1016                  * Sets the value of several element attributes.\r
1017                  * @param {Object} attributesPairs An object containing the names and\r
1018                  *              values of the attributes.\r
1019                  * @returns {CKEDITOR.dom.element} This element instance.\r
1020                  * @example\r
1021                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
1022                  * <b>element.setAttributes({\r
1023                  *     'class' : 'myClass',\r
1024                  *     'title' : 'This is an example' })</b>;\r
1025                  */\r
1026                 setAttributes : function( attributesPairs )\r
1027                 {\r
1028                         for ( var name in attributesPairs )\r
1029                                 this.setAttribute( name, attributesPairs[ name ] );\r
1030                         return this;\r
1031                 },\r
1032 \r
1033                 /**\r
1034                  * Sets the element value. This function is usually used with form\r
1035                  * field element.\r
1036                  * @param {String} value The element value.\r
1037                  * @returns {CKEDITOR.dom.element} This element instance.\r
1038                  */\r
1039                 setValue : function( value )\r
1040                 {\r
1041                         this.$.value = value;\r
1042                         return this;\r
1043                 },\r
1044 \r
1045                 /**\r
1046                  * Removes an attribute from the element.\r
1047                  * @param {String} name The attribute name.\r
1048                  * @function\r
1049                  * @example\r
1050                  * var element = CKEDITOR.dom.element.createFromHtml( '<div class="classA"></div>' );\r
1051                  * element.removeAttribute( 'class' );\r
1052                  */\r
1053                 removeAttribute : (function()\r
1054                 {\r
1055                         var standard = function( name )\r
1056                         {\r
1057                                 this.$.removeAttribute( name );\r
1058                         };\r
1059 \r
1060                         if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )\r
1061                         {\r
1062                                 return function( name )\r
1063                                 {\r
1064                                         if ( name == 'class' )\r
1065                                                 name = 'className';\r
1066                                         else if ( name == 'tabindex' )\r
1067                                                 name = 'tabIndex';\r
1068                                         standard.call( this, name );\r
1069                                 };\r
1070                         }\r
1071                         else\r
1072                                 return standard;\r
1073                 })(),\r
1074 \r
1075                 removeAttributes : function ( attributes )\r
1076                 {\r
1077                         if ( CKEDITOR.tools.isArray( attributes ) )\r
1078                         {\r
1079                                 for ( var i = 0 ; i < attributes.length ; i++ )\r
1080                                         this.removeAttribute( attributes[ i ] );\r
1081                         }\r
1082                         else\r
1083                         {\r
1084                                 for ( var attr in attributes )\r
1085                                         attributes.hasOwnProperty( attr ) && this.removeAttribute( attr );\r
1086                         }\r
1087                 },\r
1088 \r
1089                 /**\r
1090                  * Removes a style from the element.\r
1091                  * @param {String} name The style name.\r
1092                  * @function\r
1093                  * @example\r
1094                  * var element = CKEDITOR.dom.element.createFromHtml( '<div style="display:none"></div>' );\r
1095                  * element.removeStyle( 'display' );\r
1096                  */\r
1097                 removeStyle : function( name )\r
1098                 {\r
1099                         this.setStyle( name, '' );\r
1100                         if ( this.$.style.removeAttribute )\r
1101                                 this.$.style.removeAttribute( CKEDITOR.tools.cssStyleToDomStyle( name ) );\r
1102 \r
1103                         if ( !this.$.style.cssText )\r
1104                                 this.removeAttribute( 'style' );\r
1105                 },\r
1106 \r
1107                 /**\r
1108                  * Sets the value of an element style.\r
1109                  * @param {String} name The name of the style. The CSS naming notation\r
1110                  *              must be used (e.g. "background-color").\r
1111                  * @param {String} value The value to be set to the style.\r
1112                  * @returns {CKEDITOR.dom.element} This element instance.\r
1113                  * @example\r
1114                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
1115                  * <b>element.setStyle( 'background-color', '#ff0000' )</b>;\r
1116                  * <b>element.setStyle( 'margin-top', '10px' )</b>;\r
1117                  * <b>element.setStyle( 'float', 'right' )</b>;\r
1118                  */\r
1119                 setStyle : function( name, value )\r
1120                 {\r
1121                         this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ] = value;\r
1122                         return this;\r
1123                 },\r
1124 \r
1125                 /**\r
1126                  * Sets the value of several element styles.\r
1127                  * @param {Object} stylesPairs An object containing the names and\r
1128                  *              values of the styles.\r
1129                  * @returns {CKEDITOR.dom.element} This element instance.\r
1130                  * @example\r
1131                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
1132                  * <b>element.setStyles({\r
1133                  *     'position' : 'absolute',\r
1134                  *     'float' : 'right' })</b>;\r
1135                  */\r
1136                 setStyles : function( stylesPairs )\r
1137                 {\r
1138                         for ( var name in stylesPairs )\r
1139                                 this.setStyle( name, stylesPairs[ name ] );\r
1140                         return this;\r
1141                 },\r
1142 \r
1143                 /**\r
1144                  * Sets the opacity of an element.\r
1145                  * @param {Number} opacity A number within the range [0.0, 1.0].\r
1146                  * @example\r
1147                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
1148                  * <b>element.setOpacity( 0.75 )</b>;\r
1149                  */\r
1150                 setOpacity : function( opacity )\r
1151                 {\r
1152                         if ( CKEDITOR.env.ie )\r
1153                         {\r
1154                                 opacity = Math.round( opacity * 100 );\r
1155                                 this.setStyle( 'filter', opacity >= 100 ? '' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')' );\r
1156                         }\r
1157                         else\r
1158                                 this.setStyle( 'opacity', opacity );\r
1159                 },\r
1160 \r
1161                 /**\r
1162                  * Makes the element and its children unselectable.\r
1163                  * @function\r
1164                  * @example\r
1165                  * var element = CKEDITOR.dom.element.getById( 'myElement' );\r
1166                  * element.unselectable();\r
1167                  */\r
1168                 unselectable :\r
1169                         CKEDITOR.env.gecko ?\r
1170                                 function()\r
1171                                 {\r
1172                                         this.$.style.MozUserSelect = 'none';\r
1173                                         this.on( 'dragstart', function (evt) { evt.data.preventDefault(); } );\r
1174                                 }\r
1175                         : CKEDITOR.env.webkit ?\r
1176                                 function()\r
1177                                 {\r
1178                                         this.$.style.KhtmlUserSelect = 'none';\r
1179                                         this.on( 'dragstart', function (evt) { evt.data.preventDefault(); } );\r
1180                                 }\r
1181                         :\r
1182                                 function()\r
1183                                 {\r
1184                                         if ( CKEDITOR.env.ie || CKEDITOR.env.opera )\r
1185                                         {\r
1186                                                 var element = this.$,\r
1187                                                         e,\r
1188                                                         i = 0;\r
1189 \r
1190                                                 element.unselectable = 'on';\r
1191 \r
1192                                                 while ( ( e = element.all[ i++ ] ) )\r
1193                                                 {\r
1194                                                         switch ( e.tagName.toLowerCase() )\r
1195                                                         {\r
1196                                                                 case 'iframe' :\r
1197                                                                 case 'textarea' :\r
1198                                                                 case 'input' :\r
1199                                                                 case 'select' :\r
1200                                                                         /* Ignore the above tags */\r
1201                                                                         break;\r
1202                                                                 default :\r
1203                                                                         e.unselectable = 'on';\r
1204                                                         }\r
1205                                                 }\r
1206                                         }\r
1207                                 },\r
1208 \r
1209                 getPositionedAncestor : function()\r
1210                 {\r
1211                         var current = this;\r
1212                         while ( current.getName() != 'html' )\r
1213                         {\r
1214                                 if ( current.getComputedStyle( 'position' ) != 'static' )\r
1215                                         return current;\r
1216 \r
1217                                 current = current.getParent();\r
1218                         }\r
1219                         return null;\r
1220                 },\r
1221 \r
1222                 getDocumentPosition : function( refDocument )\r
1223                 {\r
1224                         var x = 0, y = 0,\r
1225                                 body = this.getDocument().getBody(),\r
1226                                 quirks = this.getDocument().$.compatMode == 'BackCompat';\r
1227 \r
1228                         var doc = this.getDocument();\r
1229 \r
1230                         if ( document.documentElement[ "getBoundingClientRect" ] )\r
1231                         {\r
1232                                 var box  = this.$.getBoundingClientRect(),\r
1233                                         $doc = doc.$,\r
1234                                         $docElem = $doc.documentElement;\r
1235 \r
1236                                 var clientTop = $docElem.clientTop || body.$.clientTop || 0,\r
1237                                         clientLeft = $docElem.clientLeft || body.$.clientLeft || 0,\r
1238                                         needAdjustScrollAndBorders = true;\r
1239 \r
1240                                 /*\r
1241                                  * #3804: getBoundingClientRect() works differently on IE and non-IE\r
1242                                  * browsers, regarding scroll positions.\r
1243                                  *\r
1244                                  * On IE, the top position of the <html> element is always 0, no matter\r
1245                                  * how much you scrolled down.\r
1246                                  *\r
1247                                  * On other browsers, the top position of the <html> element is negative\r
1248                                  * scrollTop.\r
1249                                  */\r
1250                                 if ( CKEDITOR.env.ie )\r
1251                                 {\r
1252                                         var inDocElem = doc.getDocumentElement().contains( this ),\r
1253                                                 inBody = doc.getBody().contains( this );\r
1254 \r
1255                                         needAdjustScrollAndBorders = ( quirks && inBody ) || ( !quirks && inDocElem );\r
1256                                 }\r
1257 \r
1258                                 if ( needAdjustScrollAndBorders )\r
1259                                 {\r
1260                                         x = box.left + ( !quirks && $docElem.scrollLeft || body.$.scrollLeft );\r
1261                                         x -= clientLeft;\r
1262                                         y = box.top  + ( !quirks && $docElem.scrollTop || body.$.scrollTop );\r
1263                                         y -= clientTop;\r
1264                                 }\r
1265                         }\r
1266                         else\r
1267                         {\r
1268                                 var current = this, previous = null, offsetParent;\r
1269                                 while ( current && !( current.getName() == 'body' || current.getName() == 'html' ) )\r
1270                                 {\r
1271                                         x += current.$.offsetLeft - current.$.scrollLeft;\r
1272                                         y += current.$.offsetTop - current.$.scrollTop;\r
1273 \r
1274                                         // Opera includes clientTop|Left into offsetTop|Left.\r
1275                                         if ( !current.equals( this ) )\r
1276                                         {\r
1277                                                 x += ( current.$.clientLeft || 0 );\r
1278                                                 y += ( current.$.clientTop || 0 );\r
1279                                         }\r
1280 \r
1281                                         var scrollElement = previous;\r
1282                                         while ( scrollElement && !scrollElement.equals( current ) )\r
1283                                         {\r
1284                                                 x -= scrollElement.$.scrollLeft;\r
1285                                                 y -= scrollElement.$.scrollTop;\r
1286                                                 scrollElement = scrollElement.getParent();\r
1287                                         }\r
1288 \r
1289                                         previous = current;\r
1290                                         current = ( offsetParent = current.$.offsetParent ) ?\r
1291                                                   new CKEDITOR.dom.element( offsetParent ) : null;\r
1292                                 }\r
1293                         }\r
1294 \r
1295                         if ( refDocument )\r
1296                         {\r
1297                                 var currentWindow = this.getWindow(),\r
1298                                         refWindow = refDocument.getWindow();\r
1299 \r
1300                                 if ( !currentWindow.equals( refWindow ) && currentWindow.$.frameElement )\r
1301                                 {\r
1302                                         var iframePosition = ( new CKEDITOR.dom.element( currentWindow.$.frameElement ) ).getDocumentPosition( refDocument );\r
1303 \r
1304                                         x += iframePosition.x;\r
1305                                         y += iframePosition.y;\r
1306                                 }\r
1307                         }\r
1308 \r
1309                         if ( !document.documentElement[ "getBoundingClientRect" ] )\r
1310                         {\r
1311                                 // In Firefox, we'll endup one pixel before the element positions,\r
1312                                 // so we must add it here.\r
1313                                 if ( CKEDITOR.env.gecko && !quirks )\r
1314                                 {\r
1315                                         x += this.$.clientLeft ? 1 : 0;\r
1316                                         y += this.$.clientTop ? 1 : 0;\r
1317                                 }\r
1318                         }\r
1319 \r
1320                         return { x : x, y : y };\r
1321                 },\r
1322 \r
1323                 scrollIntoView : function( alignTop )\r
1324                 {\r
1325                         // Get the element window.\r
1326                         var win = this.getWindow(),\r
1327                                 winHeight = win.getViewPaneSize().height;\r
1328 \r
1329                         // Starts from the offset that will be scrolled with the negative value of\r
1330                         // the visible window height.\r
1331                         var offset = winHeight * -1;\r
1332 \r
1333                         // Append the view pane's height if align to top.\r
1334                         // Append element height if we are aligning to the bottom.\r
1335                         if ( alignTop )\r
1336                                 offset += winHeight;\r
1337                         else\r
1338                         {\r
1339                                 offset += this.$.offsetHeight || 0;\r
1340 \r
1341                                 // Consider the margin in the scroll, which is ok for our current needs, but\r
1342                                 // needs investigation if we will be using this function in other places.\r
1343                                 offset += parseInt( this.getComputedStyle( 'marginBottom' ) || 0, 10 ) || 0;\r
1344                         }\r
1345 \r
1346                         // Append the offsets for the entire element hierarchy.\r
1347                         var elementPosition = this.getDocumentPosition();\r
1348                         offset += elementPosition.y;\r
1349 \r
1350                         // offset value might be out of range(nagative), fix it(#3692).\r
1351                         offset = offset < 0 ? 0 : offset;\r
1352 \r
1353                         // Scroll the window to the desired position, if not already visible(#3795).\r
1354                         var currentScroll = win.getScrollPosition().y;\r
1355                         if ( offset > currentScroll || offset < currentScroll - winHeight )\r
1356                                 win.$.scrollTo( 0, offset );\r
1357                 },\r
1358 \r
1359                 setState : function( state )\r
1360                 {\r
1361                         switch ( state )\r
1362                         {\r
1363                                 case CKEDITOR.TRISTATE_ON :\r
1364                                         this.addClass( 'cke_on' );\r
1365                                         this.removeClass( 'cke_off' );\r
1366                                         this.removeClass( 'cke_disabled' );\r
1367                                         break;\r
1368                                 case CKEDITOR.TRISTATE_DISABLED :\r
1369                                         this.addClass( 'cke_disabled' );\r
1370                                         this.removeClass( 'cke_off' );\r
1371                                         this.removeClass( 'cke_on' );\r
1372                                         break;\r
1373                                 default :\r
1374                                         this.addClass( 'cke_off' );\r
1375                                         this.removeClass( 'cke_on' );\r
1376                                         this.removeClass( 'cke_disabled' );\r
1377                                         break;\r
1378                         }\r
1379                 },\r
1380 \r
1381                 /**\r
1382                  * Returns the inner document of this IFRAME element.\r
1383                  * @returns {CKEDITOR.dom.document} The inner document.\r
1384                  */\r
1385                 getFrameDocument : function()\r
1386                 {\r
1387                         var $ = this.$;\r
1388 \r
1389                         try\r
1390                         {\r
1391                                 // In IE, with custom document.domain, it may happen that\r
1392                                 // the iframe is not yet available, resulting in "Access\r
1393                                 // Denied" for the following property access.\r
1394                                 $.contentWindow.document;\r
1395                         }\r
1396                         catch ( e )\r
1397                         {\r
1398                                 // Trick to solve this issue, forcing the iframe to get ready\r
1399                                 // by simply setting its "src" property.\r
1400                                 $.src = $.src;\r
1401 \r
1402                                 // In IE6 though, the above is not enough, so we must pause the\r
1403                                 // execution for a while, giving it time to think.\r
1404                                 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )\r
1405                                 {\r
1406                                         window.showModalDialog(\r
1407                                                 'javascript:document.write("' +\r
1408                                                         '<script>' +\r
1409                                                                 'window.setTimeout(' +\r
1410                                                                         'function(){window.close();}' +\r
1411                                                                         ',50);' +\r
1412                                                         '</script>")' );\r
1413                                 }\r
1414                         }\r
1415 \r
1416                         return $ && new CKEDITOR.dom.document( $.contentWindow.document );\r
1417                 },\r
1418 \r
1419                 /**\r
1420                  * Copy all the attributes from one node to the other, kinda like a clone\r
1421                  * skipAttributes is an object with the attributes that must NOT be copied.\r
1422                  * @param {CKEDITOR.dom.element} dest The destination element.\r
1423                  * @param {Object} skipAttributes A dictionary of attributes to skip.\r
1424                  * @example\r
1425                  */\r
1426                 copyAttributes : function( dest, skipAttributes )\r
1427                 {\r
1428                         var attributes = this.$.attributes;\r
1429                         skipAttributes = skipAttributes || {};\r
1430 \r
1431                         for ( var n = 0 ; n < attributes.length ; n++ )\r
1432                         {\r
1433                                 var attribute = attributes[n];\r
1434 \r
1435                                 // Lowercase attribute name hard rule is broken for\r
1436                                 // some attribute on IE, e.g. CHECKED.\r
1437                                 var attrName = attribute.nodeName.toLowerCase(),\r
1438                                         attrValue;\r
1439 \r
1440                                 // We can set the type only once, so do it with the proper value, not copying it.\r
1441                                 if ( attrName in skipAttributes )\r
1442                                         continue;\r
1443 \r
1444                                 if ( attrName == 'checked' && ( attrValue = this.getAttribute( attrName ) ) )\r
1445                                         dest.setAttribute( attrName, attrValue );\r
1446                                 // IE BUG: value attribute is never specified even if it exists.\r
1447                                 else if ( attribute.specified ||\r
1448                                   ( CKEDITOR.env.ie && attribute.nodeValue && attrName == 'value' ) )\r
1449                                 {\r
1450                                         attrValue = this.getAttribute( attrName );\r
1451                                         if ( attrValue === null )\r
1452                                                 attrValue = attribute.nodeValue;\r
1453 \r
1454                                         dest.setAttribute( attrName, attrValue );\r
1455                                 }\r
1456                         }\r
1457 \r
1458                         // The style:\r
1459                         if ( this.$.style.cssText !== '' )\r
1460                                 dest.$.style.cssText = this.$.style.cssText;\r
1461                 },\r
1462 \r
1463                 /**\r
1464                  * Changes the tag name of the current element.\r
1465                  * @param {String} newTag The new tag for the element.\r
1466                  */\r
1467                 renameNode : function( newTag )\r
1468                 {\r
1469                         // If it's already correct exit here.\r
1470                         if ( this.getName() == newTag )\r
1471                                 return;\r
1472 \r
1473                         var doc = this.getDocument();\r
1474 \r
1475                         // Create the new node.\r
1476                         var newNode = new CKEDITOR.dom.element( newTag, doc );\r
1477 \r
1478                         // Copy all attributes.\r
1479                         this.copyAttributes( newNode );\r
1480 \r
1481                         // Move children to the new node.\r
1482                         this.moveChildren( newNode );\r
1483 \r
1484                         // Replace the node.\r
1485                         this.getParent() && this.$.parentNode.replaceChild( newNode.$, this.$ );\r
1486                         newNode.$._cke_expando = this.$._cke_expando;\r
1487                         this.$ = newNode.$;\r
1488                 },\r
1489 \r
1490                 /**\r
1491                  * Gets a DOM tree descendant under the current node.\r
1492                  * @param {Array|Number} indices The child index or array of child indices under the node.\r
1493                  * @returns {CKEDITOR.dom.node} The specified DOM child under the current node. Null if child does not exist.\r
1494                  * @example\r
1495                  * var strong = p.getChild(0);\r
1496                  */\r
1497                 getChild : function( indices )\r
1498                 {\r
1499                         var rawNode = this.$;\r
1500 \r
1501                         if ( !indices.slice )\r
1502                                 rawNode = rawNode.childNodes[ indices ];\r
1503                         else\r
1504                         {\r
1505                                 while ( indices.length > 0 && rawNode )\r
1506                                         rawNode = rawNode.childNodes[ indices.shift() ];\r
1507                         }\r
1508 \r
1509                         return rawNode ? new CKEDITOR.dom.node( rawNode ) : null;\r
1510                 },\r
1511 \r
1512                 getChildCount : function()\r
1513                 {\r
1514                         return this.$.childNodes.length;\r
1515                 },\r
1516 \r
1517                 disableContextMenu : function()\r
1518                 {\r
1519                         this.on( 'contextmenu', function( event )\r
1520                                 {\r
1521                                         // Cancel the browser context menu.\r
1522                                         if ( !event.data.getTarget().hasClass( 'cke_enable_context_menu' ) )\r
1523                                                 event.data.preventDefault();\r
1524                                 } );\r
1525                 },\r
1526 \r
1527                 /**\r
1528                  *  Update the element's size with box model awareness.\r
1529                  * @name CKEDITOR.dom.element.setSize\r
1530                  * @param {String} type [width|height]\r
1531                  * @param {Number} size The length unit in px.\r
1532                  * @param isBorderBox Apply the {@param width} and {@param height} based on border box model.\r
1533                  */\r
1534                 setSize : ( function()\r
1535                 {\r
1536                         var sides = {\r
1537                                 width : [ "border-left-width", "border-right-width","padding-left", "padding-right" ],\r
1538                                 height : [ "border-top-width", "border-bottom-width", "padding-top",  "padding-bottom" ]\r
1539                         };\r
1540 \r
1541                         return function( type, size, isBorderBox )\r
1542                                 {\r
1543                                         if ( typeof size == 'number' )\r
1544                                         {\r
1545                                                 if ( isBorderBox && !( CKEDITOR.env.ie && CKEDITOR.env.quirks ) )\r
1546                                                 {\r
1547                                                         var     adjustment = 0;\r
1548                                                         for ( var i = 0, len = sides [ type ].length; i < len; i++ )\r
1549                                                                 adjustment += parseInt( this.getComputedStyle( sides [ type ][ i ] ) || 0, 10 );\r
1550                                                         size -= adjustment;\r
1551                                                 }\r
1552                                                 this.setStyle( type, size + 'px' );\r
1553                                         }\r
1554                                 };\r
1555                 })()\r
1556         });\r