-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathelement.js
2272 lines (1986 loc) · 70.4 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* CKEditor 4 LTS ("Long Term Support") is available under the terms of the Extended Support Model.
*/
/**
* @fileOverview Defines the {@link CKEDITOR.dom.element} class, which
* represents a DOM element.
*/
/**
* Represents a DOM element.
*
* // Create a new <span> element.
* var element = new CKEDITOR.dom.element( 'span' );
*
* // Create an element based on a native DOM element.
* var element = new CKEDITOR.dom.element( document.getElementById( 'myId' ) );
*
* @class
* @extends CKEDITOR.dom.node
* @constructor Creates an element class instance.
* @param {Object/String} element A native DOM element or the element name for
* new elements.
* @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain
* the element in case of element creation.
*/
CKEDITOR.dom.element = function( element, ownerDocument ) {
if ( typeof element == 'string' )
element = ( ownerDocument ? ownerDocument.$ : document ).createElement( element );
// Call the base constructor (we must not call CKEDITOR.dom.node).
CKEDITOR.dom.domObject.call( this, element );
};
// PACKAGER_RENAME( CKEDITOR.dom.element )
/**
* The the {@link CKEDITOR.dom.element} representing and element. If the
* element is a native DOM element, it will be transformed into a valid
* CKEDITOR.dom.element object.
*
* var element = new CKEDITOR.dom.element( 'span' );
* alert( element == CKEDITOR.dom.element.get( element ) ); // true
*
* var element = document.getElementById( 'myElement' );
* alert( CKEDITOR.dom.element.get( element ).getName() ); // (e.g.) 'p'
*
* @static
* @param {String/Object} element Element's id or name or native DOM element.
* @returns {CKEDITOR.dom.element} The transformed element.
*/
CKEDITOR.dom.element.get = function( element ) {
var el = typeof element == 'string' ? document.getElementById( element ) || document.getElementsByName( element )[ 0 ] : element;
return el && ( el.$ ? el : new CKEDITOR.dom.element( el ) );
};
CKEDITOR.dom.element.prototype = new CKEDITOR.dom.node();
/**
* Creates an instance of the {@link CKEDITOR.dom.element} class based on the
* HTML representation of an element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<strong class="anyclass">My element</strong>' );
* alert( element.getName() ); // 'strong'
*
* @static
* @param {String} html The element HTML. It should define only one element in
* the "root" level. The "root" element can have child nodes, but not siblings.
* @returns {CKEDITOR.dom.element} The element instance.
*/
CKEDITOR.dom.element.createFromHtml = function( html, ownerDocument ) {
var temp = new CKEDITOR.dom.element( 'div', ownerDocument );
temp.setHtml( html );
// When returning the node, remove it from its parent to detach it.
return temp.getFirst().remove();
};
/**
* Sets {@link CKEDITOR.dom.element#setCustomData custom data} on an element in a way that it is later
* possible to {@link #clearAllMarkers clear all data} set on all elements sharing the same database.
*
* This mechanism is very useful when processing some portion of DOM. All markers can later be removed
* by calling the {@link #clearAllMarkers} method, hence markers will not leak to second pass of this algorithm.
*
* var database = {};
* CKEDITOR.dom.element.setMarker( database, element1, 'foo', 'bar' );
* CKEDITOR.dom.element.setMarker( database, element2, 'oof', [ 1, 2, 3 ] );
*
* element1.getCustomData( 'foo' ); // 'bar'
* element2.getCustomData( 'oof' ); // [ 1, 2, 3 ]
*
* CKEDITOR.dom.element.clearAllMarkers( database );
*
* element1.getCustomData( 'foo' ); // null
*
* @static
* @param {Object} database
* @param {CKEDITOR.dom.element} element
* @param {String} name
* @param {Object} value
* @returns {CKEDITOR.dom.element} The element.
*/
CKEDITOR.dom.element.setMarker = function( database, element, name, value ) {
var id = element.getCustomData( 'list_marker_id' ) || ( element.setCustomData( 'list_marker_id', CKEDITOR.tools.getNextNumber() ).getCustomData( 'list_marker_id' ) ),
markerNames = element.getCustomData( 'list_marker_names' ) || ( element.setCustomData( 'list_marker_names', {} ).getCustomData( 'list_marker_names' ) );
database[ id ] = element;
markerNames[ name ] = 1;
return element.setCustomData( name, value );
};
/**
* Removes all markers added using this database. See the {@link #setMarker} method for more information.
*
* @param {Object} database
* @static
*/
CKEDITOR.dom.element.clearAllMarkers = function( database ) {
for ( var i in database )
CKEDITOR.dom.element.clearMarkers( database, database[ i ], 1 );
};
/**
* Removes all markers added to this element and removes it from the database if
* `removeFromDatabase` was passed. See the {@link #setMarker} method for more information.
*
* var database = {};
* CKEDITOR.dom.element.setMarker( database, element1, 'foo', 'bar' );
* CKEDITOR.dom.element.setMarker( database, element2, 'oof', [ 1, 2, 3 ] );
*
* element1.getCustomData( 'foo' ); // 'bar'
* element2.getCustomData( 'oof' ); // [ 1, 2, 3 ]
*
* CKEDITOR.dom.element.clearMarkers( database, element1, true );
*
* element1.getCustomData( 'foo' ); // null
* element2.getCustomData( 'oof' ); // [ 1, 2, 3 ]
*
* @param {Object} database
* @static
*/
CKEDITOR.dom.element.clearMarkers = function( database, element, removeFromDatabase ) {
var names = element.getCustomData( 'list_marker_names' ),
id = element.getCustomData( 'list_marker_id' );
for ( var i in names )
element.removeCustomData( i );
element.removeCustomData( 'list_marker_names' );
if ( removeFromDatabase ) {
element.removeCustomData( 'list_marker_id' );
delete database[ id ];
}
};
( function() {
var elementsClassList = document.createElement( '_' ).classList,
supportsClassLists = typeof elementsClassList !== 'undefined' && String( elementsClassList.add ).match( /\[Native code\]/gi ) !== null,
rclass = /[\n\t\r]/g;
function hasClass( classNames, className ) {
// Source: jQuery.
return ( ' ' + classNames + ' ' ).replace( rclass, ' ' ).indexOf( ' ' + className + ' ' ) > -1;
}
CKEDITOR.tools.extend( CKEDITOR.dom.element.prototype, {
/**
* The node type. This is a constant value set to {@link CKEDITOR#NODE_ELEMENT}.
*
* @readonly
* @property {Number} [=CKEDITOR.NODE_ELEMENT]
*/
type: CKEDITOR.NODE_ELEMENT,
/**
* Adds a CSS class to the element. It appends the class to the
* already existing names.
*
* var element = new CKEDITOR.dom.element( 'div' );
* element.addClass( 'classA' ); // <div class="classA">
* element.addClass( 'classB' ); // <div class="classA classB">
* element.addClass( 'classA' ); // <div class="classA classB">
*
* **Note:** Since CKEditor 4.5.0 this method cannot be used with multiple classes (`'classA classB'`).
*
* @chainable
* @method addClass
* @param {String} className The name of the class to be added.
*/
addClass: supportsClassLists ?
function( className ) {
this.$.classList.add( className );
return this;
} : function( className ) {
var c = this.$.className;
if ( c ) {
if ( !hasClass( c, className ) )
c += ' ' + className;
}
this.$.className = c || className;
return this;
},
/**
* Removes a CSS class name from the elements classes. Other classes
* remain untouched.
*
* var element = new CKEDITOR.dom.element( 'div' );
* element.addClass( 'classA' ); // <div class="classA">
* element.addClass( 'classB' ); // <div class="classA classB">
* element.removeClass( 'classA' ); // <div class="classB">
* element.removeClass( 'classB' ); // <div>
*
* @chainable
* @method removeClass
* @param {String} className The name of the class to remove.
*/
removeClass: supportsClassLists ?
function( className ) {
var $ = this.$;
$.classList.remove( className );
if ( !$.className )
$.removeAttribute( 'class' );
return this;
} : function( className ) {
var c = this.getAttribute( 'class' );
if ( c && hasClass( c, className ) ) {
c = c
.replace( new RegExp( '(?:^|\\s+)' + className + '(?=\\s|$)' ), '' )
.replace( /^\s+/, '' );
if ( c )
this.setAttribute( 'class', c );
else
this.removeAttribute( 'class' );
}
return this;
},
/**
* Checks if element has class name.
*
* @param {String} className
* @returns {Boolean}
*/
hasClass: function( className ) {
return hasClass( this.$.className, className );
},
/**
* Append a node as a child of this element.
*
* var p = new CKEDITOR.dom.element( 'p' );
*
* var strong = new CKEDITOR.dom.element( 'strong' );
* p.append( strong );
*
* var em = p.append( 'em' );
*
* // Result: '<p><strong></strong><em></em></p>'
*
* @param {CKEDITOR.dom.node/String} node The node or element name to be appended.
* @param {Boolean} [toStart=false] Indicates that the element is to be appended at the start.
* @returns {CKEDITOR.dom.node} The appended node.
*/
append: function( node, toStart ) {
if ( typeof node == 'string' )
node = this.getDocument().createElement( node );
if ( toStart )
this.$.insertBefore( node.$, this.$.firstChild );
else
this.$.appendChild( node.$ );
return node;
},
/**
* Append HTML as a child(ren) of this element.
*
* @param {String} html
*/
appendHtml: function( html ) {
if ( !this.$.childNodes.length )
this.setHtml( html );
else {
var temp = new CKEDITOR.dom.element( 'div', this.getDocument() );
temp.setHtml( html );
temp.moveChildren( this );
}
},
/**
* Append text to this element.
*
* var p = new CKEDITOR.dom.element( 'p' );
* p.appendText( 'This is' );
* p.appendText( ' some text' );
*
* // Result: '<p>This is some text</p>'
*
* @param {String} text The text to be appended.
*/
appendText: function( text ) {
// On IE8 it is impossible to append node to script tag, so we use its text.
// On the contrary, on Safari the text property is unpredictable in links. (https://dev.ckeditor.com/ticket/13232)
if ( this.$.text != null && CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
this.$.text += text;
else
this.append( new CKEDITOR.dom.text( text ) );
},
/**
* Appends a `<br>` filler element to this element if the filler is not present already.
* By default filler is appended only if {@link CKEDITOR.env#needsBrFiller} is `true`,
* however when `force` is set to `true` filler will be appended regardless of the environment.
*
* @param {Boolean} [force] Append filler regardless of the environment.
*/
appendBogus: function( force ) {
if ( !force && !CKEDITOR.env.needsBrFiller )
return;
var lastChild = this.getLast();
// Ignore empty/spaces text.
while ( lastChild && lastChild.type == CKEDITOR.NODE_TEXT && !CKEDITOR.tools.rtrim( lastChild.getText() ) )
lastChild = lastChild.getPrevious();
if ( !lastChild || !lastChild.is || !lastChild.is( 'br' ) ) {
var bogus = this.getDocument().createElement( 'br' );
CKEDITOR.env.gecko && bogus.setAttribute( 'type', '_moz' );
this.append( bogus );
}
},
/**
* Breaks one of the ancestor element in the element position, moving
* this element between the broken parts.
*
* // Before breaking:
* // <b>This <i>is some<span /> sample</i> test text</b>
* // If "element" is <span /> and "parent" is <i>:
* // <b>This <i>is some</i><span /><i> sample</i> test text</b>
* element.breakParent( parent );
*
* // Before breaking:
* // <b>This <i>is some<span /> sample</i> test text</b>
* // If "element" is <span /> and "parent" is <b>:
* // <b>This <i>is some</i></b><span /><b><i> sample</i> test text</b>
* element.breakParent( parent );
*
* @param {CKEDITOR.dom.element} parent The anscestor element to get broken.
* @param {Boolean} [cloneId=false] Whether to preserve ancestor ID attributes while breaking.
*/
breakParent: function( parent, cloneId ) {
var range = new CKEDITOR.dom.range( this.getDocument() );
// We'll be extracting part of this element, so let's use our
// range to get the correct piece.
range.setStartAfter( this );
range.setEndAfter( parent );
// Extract it.
var docFrag = range.extractContents( false, cloneId || false ),
tmpElement,
current;
// Move the element outside the broken element.
range.insertNode( this.remove() );
// In case of Internet Explorer, we must check if there is no background-color
// added to the element. In such case, we have to overwrite it to prevent "switching it off"
// by a browser (https://dev.ckeditor.com/ticket/14667).
if ( CKEDITOR.env.ie && !CKEDITOR.env.edge ) {
tmpElement = new CKEDITOR.dom.element( 'div' );
while ( current = docFrag.getFirst() ) {
if ( current.$.style.backgroundColor ) {
// This is a necessary hack to make sure that IE will track backgroundColor CSS property, see
// https://dev.ckeditor.com/ticket/14667#comment:8 for more details.
current.$.style.backgroundColor = current.$.style.backgroundColor;
}
tmpElement.append( current );
}
// Re-insert the extracted piece after the element.
tmpElement.insertAfter( this );
tmpElement.remove( true );
} else {
// Re-insert the extracted piece after the element.
docFrag.insertAfterNode( this );
}
},
/**
* Checks if this element contains given node.
*
* @method
* @param {CKEDITOR.dom.node} node
* @returns {Boolean}
*/
contains: !document.compareDocumentPosition ?
function( node ) {
var $ = this.$;
return node.type != CKEDITOR.NODE_ELEMENT ? $.contains( node.getParent().$ ) : $ != node.$ && $.contains( node.$ );
} : function( node ) {
return !!( this.$.compareDocumentPosition( node.$ ) & 16 );
},
/**
* Moves the selection focus to this element.
*
* var element = CKEDITOR.document.getById( 'myTextarea' );
* element.focus();
*
* @method
* @param {Boolean} defer Whether to asynchronously defer the
* execution by 100 ms.
*/
focus: ( function() {
function exec() {
// IE throws error if the element is not visible.
try {
this.$.focus();
} catch ( e ) {}
}
return function( defer ) {
if ( defer )
CKEDITOR.tools.setTimeout( exec, 100, this );
else
exec.call( this );
};
} )(),
/**
* Gets the inner HTML of this element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
* alert( element.getHtml() ); // '<b>Example</b>'
*
* @returns {String} The inner HTML of this element.
*/
getHtml: function() {
var retval = this.$.innerHTML;
// Strip <?xml:namespace> tags in IE. (https://dev.ckeditor.com/ticket/3341).
return CKEDITOR.env.ie ? retval.replace( /<\?[^>]*>/g, '' ) : retval;
},
/**
* Gets the outer (inner plus tags) HTML of this element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<div class="bold"><b>Example</b></div>' );
* alert( element.getOuterHtml() ); // '<div class="bold"><b>Example</b></div>'
*
* @returns {String} The outer HTML of this element.
*/
getOuterHtml: function() {
if ( this.$.outerHTML ) {
// IE includes the <?xml:namespace> tag in the outerHTML of
// namespaced element. So, we must strip it here. (https://dev.ckeditor.com/ticket/3341)
return this.$.outerHTML.replace( /<\?[^>]*>/, '' );
}
var tmpDiv = this.$.ownerDocument.createElement( 'div' );
tmpDiv.appendChild( this.$.cloneNode( true ) );
return tmpDiv.innerHTML;
},
/**
* Retrieve the bounding rectangle of the current element, in pixels,
* relative to the upper-left corner of the browser's client area.
*
* Since 4.10.0 you can pass an additional parameter if the function should return an absolute element position that can be used for
* positioning elements inside scrollable areas.
*
* For example, you can use this function with the {@link CKEDITOR.dom.window#getFrame editor's window frame} to
* calculate the absolute rectangle of the visible area of the editor viewport.
* The retrieved absolute rectangle can be used to position elements like toolbars or notifications (elements outside the editor)
* to always keep them inside the editor viewport independently from the scroll position.
*
* ```javascript
* var frame = editor.window.getFrame();
* frame.getClientRect( true );
* ```
*
* @param {Boolean} [isAbsolute=false] The function will retrieve an absolute rectangle of the element, i.e. the position relative
* to the upper-left corner of the topmost viewport. This option is available since 4.10.0.
* @returns {CKEDITOR.dom.rect} The dimensions of the DOM element.
*/
getClientRect: function( isAbsolute ) {
// http://help.dottoro.com/ljvmcrrn.php
var elementRect = CKEDITOR.tools.extend( {}, this.$.getBoundingClientRect() );
!elementRect.width && ( elementRect.width = elementRect.right - elementRect.left );
!elementRect.height && ( elementRect.height = elementRect.bottom - elementRect.top );
if ( !isAbsolute ) {
return elementRect;
}
return CKEDITOR.tools.getAbsoluteRectPosition( this.getWindow(), elementRect );
},
/**
* Sets the inner HTML of this element.
*
* var p = new CKEDITOR.dom.element( 'p' );
* p.setHtml( '<b>Inner</b> HTML' );
*
* // Result: '<p><b>Inner</b> HTML</p>'
*
* @method
* @param {String} html The HTML to be set for this element.
* @returns {String} The inserted HTML.
*/
setHtml: ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 ) ?
// old IEs throws error on HTML manipulation (through the "innerHTML" property)
// on the element which resides in an DTD invalid position, e.g. <span><div></div></span>
// fortunately it can be worked around with DOM manipulation.
function( html ) {
try {
var $ = this.$;
// Fix the case when setHtml is called on detached element.
// HTML5 shiv used for document in which this element was created
// won't affect that detached element. So get document fragment with
// all HTML5 elements enabled and set innerHTML while this element is appended to it.
if ( this.getParent() )
return ( $.innerHTML = html );
else {
var $frag = this.getDocument()._getHtml5ShivFrag();
$frag.appendChild( $ );
$.innerHTML = html;
$frag.removeChild( $ );
return html;
}
}
catch ( e ) {
this.$.innerHTML = '';
var temp = new CKEDITOR.dom.element( 'body', this.getDocument() );
temp.$.innerHTML = html;
var children = temp.getChildren();
while ( children.count() )
this.append( children.getItem( 0 ) );
return html;
}
} : function( html ) {
return ( this.$.innerHTML = html );
},
/**
* Sets the element contents as plain text.
*
* var element = new CKEDITOR.dom.element( 'div' );
* element.setText( 'A > B & C < D' );
* alert( element.innerHTML ); // 'A > B & C < D'
*
* @param {String} text The text to be set.
* @returns {String} The inserted text.
*/
setText: ( function() {
var supportsTextContent = document.createElement( 'p' );
supportsTextContent.innerHTML = 'x';
supportsTextContent = supportsTextContent.textContent;
return function( text ) {
this.$[ supportsTextContent ? 'textContent' : 'innerText' ] = text;
};
} )(),
/**
* Gets the value of an element attribute.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<input type="text" />' );
* alert( element.getAttribute( 'type' ) ); // 'text'
*
* @method
* @param {String} name The attribute name.
* @returns {String} The attribute value or null if not defined.
*/
getAttribute: ( function() {
var standard = function( name ) {
return this.$.getAttribute( name, 2 );
};
if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.quirks ) ) {
return function( name ) {
switch ( name ) {
case 'class':
name = 'className';
break;
case 'http-equiv':
name = 'httpEquiv';
break;
case 'name':
return this.$.name;
case 'tabindex':
var tabIndex = standard.call( this, name );
// IE returns tabIndex=0 by default for all
// elements. For those elements,
// getAtrribute( 'tabindex', 2 ) returns 32768
// instead. So, we must make this check to give a
// uniform result among all browsers.
if ( tabIndex !== 0 && this.$.tabIndex === 0 )
tabIndex = null;
return tabIndex;
case 'checked':
var attr = this.$.attributes.getNamedItem( name ),
attrValue = attr.specified ? attr.nodeValue // For value given by parser.
: this.$.checked; // For value created via DOM interface.
return attrValue ? 'checked' : null;
case 'hspace':
case 'value':
return this.$[ name ];
case 'style':
// IE does not return inline styles via getAttribute(). See https://dev.ckeditor.com/ticket/2947.
return this.$.style.cssText;
case 'contenteditable':
case 'contentEditable':
return this.$.attributes.getNamedItem( 'contentEditable' ).specified ? this.$.getAttribute( 'contentEditable' ) : null;
}
return standard.call( this, name );
};
} else {
return standard;
}
} )(),
/**
* Gets the values of all element attributes.
*
* @param {Array} exclude The names of attributes to be excluded from the returned object.
* @return {Object} An object containing all element attributes with their values.
*/
getAttributes: function( exclude ) {
var attributes = {},
attrDefs = this.$.attributes,
i;
exclude = CKEDITOR.tools.isArray( exclude ) ? exclude : [];
for ( i = 0; i < attrDefs.length; i++ ) {
if ( CKEDITOR.tools.indexOf( exclude, attrDefs[ i ].name ) === -1 ) {
attributes[ attrDefs[ i ].name ] = attrDefs[ i ].value;
}
}
return attributes;
},
/**
* Gets the nodes list containing all children of this element.
*
* @returns {CKEDITOR.dom.nodeList}
*/
getChildren: function() {
return new CKEDITOR.dom.nodeList( this.$.childNodes );
},
/**
* Gets the element `clientWidth` and `clientHeight`.
*
* @since 4.13.0
* @returns {Object} An object containing the width and height values.
*/
getClientSize: function() {
return {
width: this.$.clientWidth,
height: this.$.clientHeight
};
},
/**
* Gets the current computed value of one of the element CSS style
* properties.
*
* var element = new CKEDITOR.dom.element( 'span' );
* alert( element.getComputedStyle( 'display' ) ); // 'inline'
*
* @method
* @param {String} propertyName The style property name.
* @returns {String} The property value.
*/
getComputedStyle: ( document.defaultView && document.defaultView.getComputedStyle ) ?
function( propertyName ) {
var style = this.getWindow().$.getComputedStyle( this.$, null );
// Firefox may return null if we call the above on a hidden iframe. (https://dev.ckeditor.com/ticket/9117)
return style ? style.getPropertyValue( propertyName ) : '';
} : function( propertyName ) {
return this.$.currentStyle[ CKEDITOR.tools.cssStyleToDomStyle( propertyName ) ];
},
/**
* Gets the DTD entries for this element.
*
* @returns {Object} An object containing the list of elements accepted
* by this element.
*/
getDtd: function() {
var dtd = CKEDITOR.dtd[ this.getName() ];
this.getDtd = function() {
return dtd;
};
return dtd;
},
/**
* Gets all this element's descendants having given tag name.
*
* @method
* @param {String} tagName
*/
getElementsByTag: CKEDITOR.dom.document.prototype.getElementsByTag,
/**
* Gets the computed tabindex for this element.
*
* var element = CKEDITOR.document.getById( 'myDiv' );
* alert( element.getTabIndex() ); // (e.g.) '-1'
*
* @method
* @returns {Number} The tabindex value.
*/
getTabIndex: function() {
var tabIndex = this.$.tabIndex;
// IE returns tabIndex=0 by default for all elements. In
// those cases we must check that the element really has
// the tabindex attribute set to zero, or it is one of
// those element that should have zero by default.
if ( tabIndex === 0 && !CKEDITOR.dtd.$tabIndex[ this.getName() ] && parseInt( this.getAttribute( 'tabindex' ), 10 ) !== 0 )
return -1;
return tabIndex;
},
/**
* Gets the text value of this element.
*
* Only in IE (which uses innerText), `<br>` will cause linebreaks,
* and sucessive whitespaces (including line breaks) will be reduced to
* a single space. This behavior is ok for us, for now. It may change
* in the future.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<div>Sample <i>text</i>.</div>' );
* alert( <b>element.getText()</b> ); // 'Sample text.'
*
* @returns {String} The text value.
*/
getText: function() {
return this.$.textContent || this.$.innerText || '';
},
/**
* Gets the window object that contains this element.
*
* @returns {CKEDITOR.dom.window} The window object.
*/
getWindow: function() {
return this.getDocument().getWindow();
},
/**
* Gets the value of the `id` attribute of this element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<p id="myId"></p>' );
* alert( element.getId() ); // 'myId'
*
* @returns {String} The element id, or null if not available.
*/
getId: function() {
return this.$.id || null;
},
/**
* Gets the value of the `name` attribute of this element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<input name="myName"></input>' );
* alert( <b>element.getNameAtt()</b> ); // 'myName'
*
* @returns {String} The element name, or null if not available.
*/
getNameAtt: function() {
return this.$.name || null;
},
/**
* Gets the element name (tag name). The returned name is guaranteed to
* be always full lowercased.
*
* var element = new CKEDITOR.dom.element( 'span' );
* alert( element.getName() ); // 'span'
*
* @returns {String} The element name.
*/
getName: function() {
// Cache the lowercased name inside a closure.
var nodeName = this.$.nodeName.toLowerCase();
if ( CKEDITOR.env.ie && ( document.documentMode <= 8 ) ) {
var scopeName = this.$.scopeName;
if ( scopeName != 'HTML' )
nodeName = scopeName.toLowerCase() + ':' + nodeName;
}
this.getName = function() {
return nodeName;
};
return this.getName();
},
/**
* Gets the value set to this element. This value is usually available
* for form field elements.
*
* @returns {String} The element value.
*/
getValue: function() {
return this.$.value;
},
/**
* Gets the first child node of this element.
*
* var element = CKEDITOR.dom.element.createFromHtml( '<div><b>Example</b></div>' );
* var first = element.getFirst();
* alert( first.getName() ); // 'b'
*
* @param {Function} evaluator Filtering the result node.
* @returns {CKEDITOR.dom.node} The first child node or null if not available.
*/
getFirst: function( evaluator ) {
var first = this.$.firstChild,
retval = first && new CKEDITOR.dom.node( first );
if ( retval && evaluator && !evaluator( retval ) )
retval = retval.getNext( evaluator );
return retval;
},
/**
* See {@link #getFirst}.
*
* @param {Function} evaluator Filtering the result node.
* @returns {CKEDITOR.dom.node}
*/
getLast: function( evaluator ) {
var last = this.$.lastChild,
retval = last && new CKEDITOR.dom.node( last );
if ( retval && evaluator && !evaluator( retval ) )
retval = retval.getPrevious( evaluator );
return retval;
},
/**
* Gets CSS style value.
*
* @param {String} name The CSS property name.
* @returns {String} Style value.
*/
getStyle: function( name ) {
return this.$.style[ CKEDITOR.tools.cssStyleToDomStyle( name ) ];
},
/**
* Checks if the element name matches the specified criteria.
*
* var element = new CKEDITOR.element( 'span' );
* alert( element.is( 'span' ) ); // true
* alert( element.is( 'p', 'span' ) ); // true
* alert( element.is( 'p' ) ); // false
* alert( element.is( 'p', 'div' ) ); // false
* alert( element.is( { p:1,span:1 } ) ); // true
*
* @param {String.../Object} name One or more names to be checked, or a {@link CKEDITOR.dtd} object.
* @returns {Boolean} `true` if the element name matches any of the names.
*/
is: function() {
var name = this.getName();
// Check against the specified DTD liternal.
if ( typeof arguments[ 0 ] == 'object' )
return !!arguments[ 0 ][ name ];
// Check for tag names
for ( var i = 0; i < arguments.length; i++ ) {
if ( arguments[ i ] == name )
return true;
}
return false;
},
/**
* Decide whether one element is able to receive cursor.
*
* @param {Boolean} [textCursor=true] Only consider element that could receive text child.
*/
isEditable: function( textCursor ) {
var name = this.getName();
if ( this.isReadOnly() || this.getComputedStyle( 'display' ) == 'none' ||
this.getComputedStyle( 'visibility' ) == 'hidden' ||
CKEDITOR.dtd.$nonEditable[ name ] ||
CKEDITOR.dtd.$empty[ name ] ||
( this.is( 'a' ) &&
( this.data( 'cke-saved-name' ) || this.hasAttribute( 'name' ) ) &&
!this.getChildCount()
) ) {
return false;
}
if ( textCursor !== false ) {
// Get the element DTD (defaults to span for unknown elements).
var dtd = CKEDITOR.dtd[ name ] || CKEDITOR.dtd.span;
// In the DTD # == text node.
return !!( dtd && dtd[ '#' ] );
}
return true;
},
/**
* Compare this element's inner html, tag name, attributes, etc. with other one.
*
* See [W3C's DOM Level 3 spec - node#isEqualNode](http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isEqualNode)
* for more details.
*
* @param {CKEDITOR.dom.element} otherElement Element to compare.
* @returns {Boolean}
*/
isIdentical: function( otherElement ) {
// do shallow clones, but with IDs
var thisEl = this.clone( 0, 1 ),
otherEl = otherElement.clone( 0, 1 );
// Remove distractions.
thisEl.removeAttributes( [ '_moz_dirty', 'data-cke-expando', 'data-cke-saved-href', 'data-cke-saved-name' ] );
otherEl.removeAttributes( [ '_moz_dirty', 'data-cke-expando', 'data-cke-saved-href', 'data-cke-saved-name' ] );
// Native comparison available.
if ( thisEl.$.isEqualNode ) {
// Styles order matters.
thisEl.$.style.cssText = CKEDITOR.tools.normalizeCssText( thisEl.$.style.cssText );
otherEl.$.style.cssText = CKEDITOR.tools.normalizeCssText( otherEl.$.style.cssText );
return thisEl.$.isEqualNode( otherEl.$ );
} else {
thisEl = thisEl.getOuterHtml();
otherEl = otherEl.getOuterHtml();
// Fix tiny difference between link href in older IEs.
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 && this.is( 'a' ) ) {
var parent = this.getParent();
if ( parent.type == CKEDITOR.NODE_ELEMENT ) {
var el = parent.clone();
el.setHtml( thisEl ), thisEl = el.getHtml();
el.setHtml( otherEl ), otherEl = el.getHtml();
}
}
return thisEl == otherEl;
}
},
/**
* Checks if this element is visible. May not work if the element is
* child of an element with visibility set to `hidden`, but works well
* on the great majority of cases.
*
* @returns {Boolean} True if the element is visible.
*/