/
Element.ts
1003 lines (901 loc) · 25.1 KB
/
Element.ts
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
import Node from '../node/Node';
import ShadowRoot from '../shadow-root/ShadowRoot';
import Attr from '../../attribute/Attr';
import DOMRect from './DOMRect';
import Range from '../../range/Range';
import DOMTokenList from '../../dom-token-list/DOMTokenList';
import IDOMTokenList from '../../dom-token-list/IDOMTokenList';
import QuerySelector from '../../query-selector/QuerySelector';
import SelectorItem from '../../query-selector/SelectorItem';
import MutationRecord from '../../mutation-observer/MutationRecord';
import MutationTypeEnum from '../../mutation-observer/MutationTypeEnum';
import NamespaceURI from '../../config/NamespaceURI';
import XMLParser from '../../xml-parser/XMLParser';
import XMLSerializer from '../../xml-serializer/XMLSerializer';
import ChildNodeUtility from '../child-node/ChildNodeUtility';
import ParentNodeUtility from '../parent-node/ParentNodeUtility';
import NonDocumentChildNodeUtility from '../child-node/NonDocumentChildNodeUtility';
import IElement from './IElement';
import DOMException from '../../exception/DOMException';
import IShadowRoot from '../shadow-root/IShadowRoot';
import INode from '../node/INode';
import IDocument from '../document/IDocument';
import IHTMLCollection from './IHTMLCollection';
import INodeList from '../node/INodeList';
import HTMLCollectionFactory from './HTMLCollectionFactory';
import { TInsertAdjacentPositions } from './IElement';
import IText from '../text/IText';
/**
* Element.
*/
export default class Element extends Node implements IElement {
// ObservedAttributes should only be called once by CustomElementRegistry (see #117)
// CustomElementRegistry will therefore populate _observedAttributes when CustomElementRegistry.define() is called
public static _observedAttributes: string[];
public static observedAttributes: string[];
public tagName: string = null;
public nodeType = Node.ELEMENT_NODE;
public shadowRoot: IShadowRoot = null;
public scrollTop = 0;
public scrollLeft = 0;
public children: IHTMLCollection<IElement> = HTMLCollectionFactory.create();
public readonly namespaceURI: string = null;
// Used for being able to access closed shadow roots
public _shadowRoot: IShadowRoot = null;
public _attributes: { [k: string]: Attr } = {};
private _classList: DOMTokenList = null;
public _isValue?: string;
/**
* Returns class list.
*
* @returns Class list.
*/
public get classList(): IDOMTokenList {
if (!this._classList) {
this._classList = new DOMTokenList(this, 'class');
}
return <IDOMTokenList>this._classList;
}
/**
* Returns ID.
*
* @returns ID.
*/
public get id(): string {
return this.getAttribute('id') || '';
}
/**
* Sets ID.
*
* @param id ID.
*/
public set id(id: string) {
this.setAttribute('id', id);
}
/**
* Returns class name.
*
* @returns Class name.
*/
public get className(): string {
return this.getAttribute('class') || '';
}
/**
* Sets class name.
*
* @param className Class name.
*/
public set className(className: string) {
this.setAttribute('class', className);
}
/**
* Node name.
*
* @returns Node name.
*/
public get nodeName(): string {
return this.tagName;
}
/**
* Local name.
*
* @returns Local name.
*/
public get localName(): string {
return this.tagName ? this.tagName.toLowerCase() : 'unknown';
}
/**
* Previous element sibling.
*
* @returns Element.
*/
public get previousElementSibling(): IElement {
return NonDocumentChildNodeUtility.previousElementSibling(this);
}
/**
* Next element sibling.
*
* @returns Element.
*/
public get nextElementSibling(): IElement {
return NonDocumentChildNodeUtility.nextElementSibling(this);
}
/**
* Get text value of children.
*
* @returns Text content.
*/
public get textContent(): string {
let result = '';
for (const childNode of this.childNodes) {
if (childNode.nodeType === Node.ELEMENT_NODE || childNode.nodeType === Node.TEXT_NODE) {
result += childNode.textContent;
}
}
return result;
}
/**
* Sets text content.
*
* @param textContent Text content.
*/
public set textContent(textContent: string) {
for (const child of this.childNodes.slice()) {
this.removeChild(child);
}
if (textContent) {
this.appendChild(this.ownerDocument.createTextNode(textContent));
}
}
/**
* Returns inner HTML.
*
* @returns HTML.
*/
public get innerHTML(): string {
const xmlSerializer = new XMLSerializer();
let xml = '';
for (const node of this.childNodes) {
xml += xmlSerializer.serializeToString(node);
}
return xml;
}
/**
* Sets inner HTML.
*
* @param html HTML.
*/
public set innerHTML(html: string) {
for (const child of this.childNodes.slice()) {
this.removeChild(child);
}
for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.appendChild(node);
}
}
/**
* Returns outer HTML.
*
* @returns HTML.
*/
public get outerHTML(): string {
return new XMLSerializer().serializeToString(this);
}
/**
* Returns outer HTML.
*
* @param html HTML.
*/
public set outerHTML(html: string) {
this.replaceWith(html);
}
/**
* Returns attributes.
*
* @returns Attributes.
*/
public get attributes(): { [k: string]: Attr | number } {
const attributes = Object.values(this._attributes);
return Object.assign({}, this._attributes, attributes, {
length: attributes.length
});
}
/**
* First element child.
*
* @returns Element.
*/
public get firstElementChild(): IElement {
return this.children ? this.children[0] || null : null;
}
/**
* Last element child.
*
* @returns Element.
*/
public get lastElementChild(): IElement {
return this.children ? this.children[this.children.length - 1] || null : null;
}
/**
* Last element child.
*
* @returns Element.
*/
public get childElementCount(): number {
return this.children.length;
}
/**
* Returns slot.
*
* @returns Slot.
*/
public get slot(): string {
return this.getAttributeNS(null, 'slot') || '';
}
/**
* Returns slot.
*
* @param slot Slot.
*/
public set slot(title: string) {
this.setAttributeNS(null, 'slot', title);
}
/**
* Attribute changed callback.
*
* @param name Name.
* @param oldValue Old value.
* @param newValue New value.
*/
public attributeChangedCallback?(name: string, oldValue: string, newValue: string): void;
/**
* Returns inner HTML and optionally the content of shadow roots.
*
* This is a feature implemented in Chromium, but not supported by Mozilla yet.
*
* @see https://web.dev/declarative-shadow-dom/
* @see https://chromestatus.com/feature/5191745052606464
* @param [options] Options.
* @param [options.includeShadowRoots] Set to "true" to include shadow roots.
* @returns HTML.
*/
public getInnerHTML(options?: { includeShadowRoots?: boolean }): string {
const xmlSerializer = new XMLSerializer();
let xml = '';
for (const node of this.childNodes) {
xml += xmlSerializer.serializeToString(node, options);
}
return xml;
}
/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): IElement {
const clone = <Element | IElement>super.cloneNode(deep);
for (const key of Object.keys(this._attributes)) {
const attr = Object.assign(new Attr(), this._attributes[key]);
(<IElement>attr.ownerElement) = clone;
(<Element>clone)._attributes[key] = attr;
}
if (deep) {
for (const node of clone.childNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
clone.children.push(<IElement>node);
}
}
}
(<string>clone.tagName) = this.tagName;
clone.scrollLeft = this.scrollLeft;
clone.scrollTop = this.scrollTop;
(<string>clone.namespaceURI) = this.namespaceURI;
return <IElement>clone;
}
/**
* Append a child node to childNodes.
*
* @override
* @param node Node to append.
* @returns Appended node.
*/
public appendChild(node: INode): INode {
// If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
if (node.parentNode && node.parentNode['children']) {
const index = node.parentNode['children'].indexOf(node);
if (index !== -1) {
node.parentNode['children'].splice(index, 1);
}
}
if (node !== this && node.nodeType === Node.ELEMENT_NODE) {
this.children.push(<IElement>node);
}
}
return super.appendChild(node);
}
/**
* Remove Child element from childNodes array.
*
* @override
* @param node Node to remove.
*/
public removeChild(node: INode): INode {
if (node.nodeType === Node.ELEMENT_NODE) {
const index = this.children.indexOf(<IElement>node);
if (index !== -1) {
this.children.splice(index, 1);
}
}
return super.removeChild(node);
}
/**
* Removes the node from its parent.
*/
public remove(): void {
ChildNodeUtility.remove(this);
}
/**
* Inserts a node before another.
*
* @override
* @param newNode Node to insert.
* @param [referenceNode] Node to insert before.
* @returns Inserted node.
*/
public insertBefore(newNode: INode, referenceNode?: INode): INode {
const returnValue = super.insertBefore(newNode, referenceNode);
// If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
if (newNode.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
if (newNode.parentNode && newNode.parentNode['children']) {
const index = newNode.parentNode['children'].indexOf(newNode);
if (index !== -1) {
newNode.parentNode['children'].splice(index, 1);
}
}
this.children.length = 0;
for (const node of this.childNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
this.children.push(<IElement>node);
}
}
}
return returnValue;
}
/**
* The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or DOMString objects.
*
* @param nodes List of Node or DOMString.
*/
public replaceWith(...nodes: (INode | string)[]): void {
ChildNodeUtility.replaceWith(this, ...nodes);
}
/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public before(...nodes: (string | INode)[]): void {
ChildNodeUtility.before(this, ...nodes);
}
/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public after(...nodes: (string | INode)[]): void {
ChildNodeUtility.after(this, ...nodes);
}
/**
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public append(...nodes: (string | INode)[]): void {
ParentNodeUtility.append(this, ...nodes);
}
/**
* Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public prepend(...nodes: (string | INode)[]): void {
ParentNodeUtility.prepend(this, ...nodes);
}
/**
* Replaces the existing children of a node with a specified new set of children.
*
* @param nodes List of Node or DOMString.
*/
public replaceChildren(...nodes: (string | INode)[]): void {
ParentNodeUtility.replaceChildren(this, ...nodes);
}
/**
* Inserts a node to the given position.
*
* @param position Position to insert element.
* @param element Node to insert.
* @returns Inserted node or null if couldn't insert.
*/
public insertAdjacentElement(position: TInsertAdjacentPositions, element: INode): INode | null {
if (position === 'beforebegin') {
if (!this.parentElement) {
return null;
}
this.parentElement.insertBefore(element, this);
} else if (position === 'afterbegin') {
this.insertBefore(element, this.firstChild);
} else if (position === 'beforeend') {
this.appendChild(element);
} else if (position === 'afterend') {
if (!this.parentElement) {
return null;
}
this.parentElement.insertBefore(element, this.nextSibling);
}
return element;
}
/**
* Inserts an HTML string to the given position.
*
* @param position Position to insert text.
* @param text HTML string to insert.
*/
public insertAdjacentHTML(position: TInsertAdjacentPositions, text: string): void {
for (const node of XMLParser.parse(this.ownerDocument, text).childNodes.slice()) {
this.insertAdjacentElement(position, node);
}
}
/**
* Inserts text to the given position.
*
* @param position Position to insert text.
* @param text String to insert.
*/
public insertAdjacentText(position: TInsertAdjacentPositions, text: string): void {
if (!text) {
return;
}
const textNode = <IText>this.ownerDocument.createTextNode(text);
this.insertAdjacentElement(position, textNode);
}
/**
* Sets an attribute.
*
* @param name Name.
* @param value Value.
*/
public setAttribute(name: string, value: string): void {
const attribute = this.ownerDocument.createAttributeNS(null, name);
attribute.value = String(value);
this.setAttributeNode(attribute);
}
/**
* Sets a namespace attribute.
*
* @param namespaceURI Namespace URI.
* @param name Name.
* @param value Value.
*/
public setAttributeNS(namespaceURI: string, name: string, value: string): void {
const attribute = this.ownerDocument.createAttributeNS(namespaceURI, name);
attribute.value = String(value);
this.setAttributeNode(attribute);
}
/**
* Returns attribute names.
*
* @returns Attribute names.
*/
public getAttributeNames(): string[] {
return Object.keys(this._attributes);
}
/**
* Returns attribute value.
*
* @param name Name.
*/
public getAttribute(name: string): string {
const attribute = this.getAttributeNode(name);
if (attribute) {
return attribute.value;
}
return null;
}
/**
* Returns namespace attribute value.
*
* @param namespace Namespace URI.
* @param localName Local name.
*/
public getAttributeNS(namespace: string, localName: string): string {
const attribute = this.getAttributeNodeNS(namespace, localName);
if (attribute) {
return attribute.value;
}
return null;
}
/**
* Returns a boolean value indicating whether the specified element has the attribute or not.
*
* @param name Attribute name.
* @returns True if attribute exists, false otherwise.
*/
public hasAttribute(name: string): boolean {
return !!this.getAttributeNode(name);
}
/**
* Returns a boolean value indicating whether the specified element has the namespace attribute or not.
*
* @param namespace Namespace URI.
* @param localName Local name.
* @returns True if attribute exists, false otherwise.
*/
public hasAttributeNS(namespace: string, localName: string): boolean {
for (const name of Object.keys(this._attributes)) {
const attribute = this._attributes[name];
if (attribute.namespaceURI === namespace && attribute.localName === localName) {
return true;
}
}
return false;
}
/**
* Returns "true" if the element has attributes.
*
* @returns "true" if the element has attributes.
*/
public hasAttributes(): boolean {
return Object.keys(this._attributes).length > 0;
}
/**
* Removes an attribute.
*
* @param name Name.
*/
public removeAttribute(name: string): void {
const attribute = this._attributes[this._getAttributeName(name)];
if (attribute) {
this.removeAttributeNode(attribute);
}
}
/**
* Removes a namespace attribute.
*
* @param namespace Namespace URI.
* @param localName Local name.
*/
public removeAttributeNS(namespace: string, localName: string): void {
for (const name of Object.keys(this._attributes)) {
const attribute = this._attributes[name];
if (attribute.namespaceURI === namespace && attribute.localName === localName) {
this.removeAttribute(attribute.name);
}
}
}
/**
* Attaches a shadow root.
*
* @param _shadowRootInit Shadow root init.
* @param shadowRootInit
* @param shadowRootInit.mode
* @returns Shadow root.
*/
public attachShadow(shadowRootInit: { mode: string }): IShadowRoot {
if (this._shadowRoot) {
throw new DOMException('Shadow root has already been attached.');
}
(<IShadowRoot>this._shadowRoot) = new ShadowRoot();
(<IDocument>this._shadowRoot.ownerDocument) = this.ownerDocument;
(<Element>this._shadowRoot.host) = this;
(<string>this._shadowRoot.mode) = shadowRootInit.mode;
(<ShadowRoot>this._shadowRoot)._connectToNode(this);
if (this._shadowRoot.mode === 'open') {
(<IShadowRoot>this.shadowRoot) = this._shadowRoot;
}
return this._shadowRoot;
}
/**
* Converts to string.
*
* @returns String.
*/
public toString(): string {
return this.outerHTML;
}
/**
* Returns the size of an element and its position relative to the viewport.
*
* @returns DOM rect.
*/
public getBoundingClientRect(): DOMRect {
return new DOMRect();
}
/**
* Returns a range.
*
* @returns Range.
*/
public createTextRange(): Range {
return new Range();
}
/**
* The matches() method checks to see if the Element would be selected by the provided selectorString.
*
* @param selector Selector.
* @returns "true" if matching.
*/
public matches(selector: string): boolean {
for (const part of selector.split(',')) {
if (new SelectorItem(part.trim()).match(this)) {
return true;
}
}
return false;
}
/**
* Traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string.
*
* @param selector Selector.
* @returns Closest matching element.
*/
public closest(selector: string): IElement {
let rootElement: IElement = this.ownerDocument.documentElement;
if (!this.isConnected) {
rootElement = this;
while (rootElement.parentNode) {
rootElement = <IElement>rootElement.parentNode;
}
}
const elements = rootElement.querySelectorAll(selector);
// eslint-disable-next-line
let parent: IElement = this;
while (parent) {
if (elements.includes(parent)) {
return parent;
}
parent = parent.parentElement;
}
// QuerySelectorAll() will not match the element it is looking in when searched for
// Therefore we need to check if it matches the root
if (rootElement.matches(selector)) {
return rootElement;
}
return null;
}
/**
* Query CSS selector to find matching nodes.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll(selector: string): INodeList<IElement> {
return QuerySelector.querySelectorAll(this, selector);
}
/**
* Query CSS Selector to find matching node.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): IElement {
return QuerySelector.querySelector(this, selector);
}
/**
* Returns an elements by class name.
*
* @param className Tag name.
* @returns Matching element.
*/
public getElementsByClassName(className: string): IHTMLCollection<IElement> {
return ParentNodeUtility.getElementsByClassName(this, className);
}
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName(tagName: string): IHTMLCollection<IElement> {
return ParentNodeUtility.getElementsByTagName(this, tagName);
}
/**
* Returns an elements by tag name and namespace.
*
* @param namespaceURI Namespace URI.
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagNameNS(namespaceURI: string, tagName: string): IHTMLCollection<IElement> {
return ParentNodeUtility.getElementsByTagNameNS(this, namespaceURI, tagName);
}
/**
* The setAttributeNode() method adds a new Attr node to the specified element.
*
* @param attribute Attribute.
* @returns Replaced attribute.
*/
public setAttributeNode(attribute: Attr): Attr {
const name = this._getAttributeName(attribute.name);
const replacedAttribute = this._attributes[name];
const oldValue = replacedAttribute ? replacedAttribute.value : null;
attribute.name = name;
(<IElement>attribute.ownerElement) = this;
(<IDocument>attribute.ownerDocument) = this.ownerDocument;
this._attributes[name] = attribute;
this._updateDomListIndices();
if (
this.attributeChangedCallback &&
(<typeof Element>this.constructor)._observedAttributes &&
(<typeof Element>this.constructor)._observedAttributes.includes(name)
) {
this.attributeChangedCallback(name, oldValue, attribute.value);
}
// MutationObserver
if (this._observers.length > 0) {
for (const observer of this._observers) {
if (
observer.options.attributes &&
(!observer.options.attributeFilter || observer.options.attributeFilter.includes(name))
) {
const record = new MutationRecord();
record.target = this;
record.type = MutationTypeEnum.attributes;
record.attributeName = name;
record.oldValue = observer.options.attributeOldValue ? oldValue : null;
observer.callback([record]);
}
}
}
return replacedAttribute || null;
}
/**
* The setAttributeNodeNS() method adds a new Attr node to the specified element.
*
* @param attribute Attribute.
* @returns Replaced attribute.
*/
public setAttributeNodeNS(attribute: Attr): Attr {
return this.setAttributeNode(attribute);
}
/**
* Returns an Attr node.
*
* @param name Name.
* @returns Replaced attribute.
*/
public getAttributeNode(name: string): Attr {
return this._attributes[this._getAttributeName(name)] || null;
}
/**
* Returns a namespaced Attr node.
*
* @param namespace Namespace.
* @param name Name.
* @returns Replaced attribute.
*/
public getAttributeNodeNS(namespace: string, name: string): Attr {
const attributeName = this._getAttributeName(name);
if (
this._attributes[attributeName] &&
this._attributes[attributeName].namespaceURI === namespace &&
this._attributes[attributeName].localName === attributeName
) {
return this._attributes[attributeName];
}
for (const name of Object.keys(this._attributes)) {
const attribute = this._attributes[name];
if (attribute.namespaceURI === namespace && attribute.localName === attributeName) {
return attribute;
}
}
return null;
}
/**
* Removes an Attr node.
*
* @param attribute Attribute.
*/
public removeAttributeNode(attribute: Attr): void {
delete this._attributes[attribute.name];
this._updateDomListIndices();
if (
this.attributeChangedCallback &&
(<typeof Element>this.constructor)._observedAttributes &&
(<typeof Element>this.constructor)._observedAttributes.includes(attribute.name)
) {
this.attributeChangedCallback(attribute.name, attribute.value, null);
}
// MutationObserver
if (this._observers.length > 0) {
for (const observer of this._observers) {
if (
observer.options.attributes &&
(!observer.options.attributeFilter ||
observer.options.attributeFilter.includes(attribute.name))
) {
const record = new MutationRecord();
record.target = this;
record.type = MutationTypeEnum.attributes;
record.attributeName = attribute.name;
record.oldValue = observer.options.attributeOldValue ? attribute.value : null;
observer.callback([record]);
}
}
}
}
/**
* Removes an Attr node.
*
* @param attribute Attribute.
*/
public removeAttributeNodeNS(attribute: Attr): void {
this.removeAttributeNode(attribute);
}
/**
* Scrolls to a particular set of coordinates.
*
* @param x X position or options object.
* @param y Y position.
*/
public scroll(x: { top?: number; left?: number; behavior?: string } | number, y?: number): void {
if (typeof x === 'object') {
if (x.behavior === 'smooth') {
this.ownerDocument.defaultView.setTimeout(() => {
if (x.top !== undefined) {
(<number>this.scrollTop) = x.top;
}
if (x.left !== undefined) {
(<number>this.scrollLeft) = x.left;
}
});
} else {
if (x.top !== undefined) {
(<number>this.scrollTop) = x.top;
}
if (x.left !== undefined) {
(<number>this.scrollLeft) = x.left;
}
}
} else if (x !== undefined && y !== undefined) {
(<number>this.scrollLeft) = x;
(<number>this.scrollTop) = y;
}
}
/**
* Scrolls to a particular set of coordinates.
*
* @param x X position or options object.
* @param y Y position.
*/
public scrollTo(
x: { top?: number; left?: number; behavior?: string } | number,
y?: number
): void {
this.scroll(x, y);
}
/**
* Returns attribute name.
*
* @param name Name.
* @returns Attribute name based on namespace.
*/
protected _getAttributeName(name): string {
if (this.namespaceURI === NamespaceURI.svg) {
return name;
}
return name.toLowerCase();
}
/**
* Updates DOM list indices.
*/
protected _updateDomListIndices(): void {
if (this._classList) {
this._classList._updateIndices();