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