forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedNodeMap.ts
145 lines (130 loc) · 2.92 KB
/
NamedNodeMap.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
import type Element from '../nodes/element/Element';
import IAttr from '../nodes/attr/IAttr';
import INamedNodeMap from './INamedNodeMap';
/**
* NamedNodeMap.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/NamedNodeMap.
*/
export default class NamedNodeMap implements INamedNodeMap {
[index: number]: IAttr;
/**
* Reference to the element.
*/
#ownerElement: Element;
/**
* Constructor.
*
* @param element Associated element.
*/
constructor(element: Element) {
this.#ownerElement = element;
}
/**
* Returns string.
*
* @returns string.
*/
public get [Symbol.toStringTag](): string {
return this.constructor.name;
}
/**
* Length.
*
* @returns Length.
*/
public get length(): number {
return Object.keys(this.#ownerElement._attributes).length;
}
/**
* Returns attribute by index.
*
* @param index Index.
*/
public item(index: number): IAttr | null {
if (index < 0) {
return null;
}
const attr = Object.values(this.#ownerElement._attributes)[index];
return attr ? attr : null;
}
/**
* Returns attribute by name.
*
* @param qualifiedName Name.
* @returns Attribute.
*/
public getNamedItem(qualifiedName: string): IAttr | null {
return this.#ownerElement.getAttributeNode(qualifiedName);
}
/**
* Returns attribute by name and namespace.
*
* @param namespace Namespace.
* @param localName Local name of the attribute.
* @returns Attribute.
*/
public getNamedItemNS(namespace: string, localName: string): IAttr | null {
return this.#ownerElement.getAttributeNodeNS(namespace, localName);
}
/**
* Adds a new attribute node.
*
* @param attr Attribute.
* @returns Replaced attribute.
*/
public setNamedItem(attr: IAttr): IAttr {
return this.#ownerElement.setAttributeNode(attr);
}
/**
* Adds a new namespaced attribute node.
*
* @param attr Attribute.
* @returns Replaced attribute.
*/
public setNamedItemNS(attr: IAttr): IAttr {
return this.#ownerElement.setAttributeNodeNS(attr);
}
/**
* Removes an attribute.
*
* @param qualifiedName Name of the attribute.
* @returns Removed attribute.
*/
public removeNamedItem(qualifiedName: string): IAttr | null {
const attr = this.getNamedItem(qualifiedName);
if (attr) {
this.#ownerElement.removeAttributeNode(attr);
}
return attr;
}
/**
* Removes a namespaced attribute.
*
* @param namespace Namespace.
* @param localName Local name of the attribute.
* @returns Removed attribute.
*/
public removeNamedItemNS(namespace: string, localName: string): IAttr | null {
const attr = this.getNamedItemNS(namespace, localName);
if (attr) {
this.#ownerElement.removeAttributeNode(attr);
}
return attr;
}
/**
* Iterator.
*
* @returns Iterator.
*/
public [Symbol.iterator](): Iterator<IAttr> {
let index = -1;
return {
next: () => {
index++;
return { value: this.item(index), done: index >= this.length };
}
};
}
}