@@ -14,11 +14,14 @@ import StringUtility from '../../utilities/StringUtility.js';
14
14
export default class NamedNodeMap {
15
15
[ index : number ] : Attr ;
16
16
17
- // All items with the namespaceURI as prefix
18
- public [ PropertySymbol . namespaceItems ] : Map < string , Attr > = new Map ( ) ;
17
+ // Items by attribute namespaceURI
18
+ public [ PropertySymbol . itemsByNamespaceURI ] : Map < string , Attr > = new Map ( ) ;
19
19
20
- // Items without namespaceURI as prefix, where the HTML namespace is the default namespace
21
- public [ PropertySymbol . namedItems ] : Map < string , Attr [ ] > = new Map ( ) ;
20
+ // Items by attribute name
21
+ public [ PropertySymbol . itemsByName ] : Map < string , Attr [ ] > = new Map ( ) ;
22
+
23
+ // All items
24
+ public [ PropertySymbol . items ] : Map < string , Attr > = new Map ( ) ;
22
25
23
26
public declare [ PropertySymbol . ownerElement ] : Element ;
24
27
@@ -37,7 +40,7 @@ export default class NamedNodeMap {
37
40
* @returns Length.
38
41
*/
39
42
public get length ( ) : number {
40
- return this [ PropertySymbol . namespaceItems ] . size ;
43
+ return this [ PropertySymbol . items ] . size ;
41
44
}
42
45
43
46
/**
@@ -64,7 +67,7 @@ export default class NamedNodeMap {
64
67
* @returns Iterator.
65
68
*/
66
69
public [ Symbol . iterator ] ( ) : IterableIterator < Attr > {
67
- return this [ PropertySymbol . namespaceItems ] . values ( ) ;
70
+ return this [ PropertySymbol . items ] . values ( ) ;
68
71
}
69
72
70
73
/**
@@ -73,7 +76,7 @@ export default class NamedNodeMap {
73
76
* @param index Index.
74
77
*/
75
78
public item ( index : number ) : Attr | null {
76
- const items = Array . from ( this [ PropertySymbol . namespaceItems ] . values ( ) ) ;
79
+ const items = Array . from ( this [ PropertySymbol . items ] . values ( ) ) ;
77
80
return index >= 0 && items [ index ] ? items [ index ] : null ;
78
81
}
79
82
@@ -91,9 +94,9 @@ export default class NamedNodeMap {
91
94
PropertySymbol . contentType
92
95
] === 'text/html'
93
96
) {
94
- return this [ PropertySymbol . namedItems ] . get ( StringUtility . asciiLowerCase ( name ) ) ?. [ 0 ] || null ;
97
+ return this [ PropertySymbol . itemsByName ] . get ( StringUtility . asciiLowerCase ( name ) ) ?. [ 0 ] || null ;
95
98
}
96
- return this [ PropertySymbol . namedItems ] . get ( name ) ?. [ 0 ] || null ;
99
+ return this [ PropertySymbol . itemsByName ] . get ( name ) ?. [ 0 ] || null ;
97
100
}
98
101
99
102
/**
@@ -104,11 +107,17 @@ export default class NamedNodeMap {
104
107
* @returns Item.
105
108
*/
106
109
public getNamedItemNS ( namespace : string , localName : string ) : Attr | null {
107
- if ( namespace === '' ) {
108
- namespace = null ;
110
+ const item = this [ PropertySymbol . itemsByNamespaceURI ] . get ( `${ namespace || '' } :${ localName } ` ) ;
111
+
112
+ // It seems like an item cant have a prefix without a namespaceURI
113
+ // E.g. element.setAttribute('ns1:key', 'value1');
114
+ // expect(element.attributes.getNamedItemNS(null, 'key')).toBeNull();
115
+
116
+ if ( item && ( ! item [ PropertySymbol . prefix ] || item [ PropertySymbol . namespaceURI ] ) ) {
117
+ return item ;
109
118
}
110
119
111
- return this [ PropertySymbol . namespaceItems ] . get ( ` ${ namespace || '' } : ${ localName } ` ) || null ;
120
+ return null ;
112
121
}
113
122
114
123
/**
@@ -199,26 +208,29 @@ export default class NamedNodeMap {
199
208
const replacedItem =
200
209
this . getNamedItemNS ( item [ PropertySymbol . namespaceURI ] , item [ PropertySymbol . localName ] ) ||
201
210
null ;
202
-
203
- const namedItems = this [ PropertySymbol . namedItems ] . get ( item [ PropertySymbol . name ] ) ;
211
+ const itemsByName = this [ PropertySymbol . itemsByName ] . get ( item [ PropertySymbol . name ] ) ;
204
212
205
213
if ( replacedItem === item ) {
206
214
return item ;
207
215
}
208
216
209
- this [ PropertySymbol . namespaceItems ] . set (
217
+ this [ PropertySymbol . itemsByNamespaceURI ] . set (
210
218
`${ item [ PropertySymbol . namespaceURI ] || '' } :${ item [ PropertySymbol . localName ] } ` ,
211
219
item
212
220
) ;
221
+ this [ PropertySymbol . items ] . set (
222
+ `${ item [ PropertySymbol . namespaceURI ] || '' } :${ item [ PropertySymbol . name ] } ` ,
223
+ item
224
+ ) ;
213
225
214
- if ( ! namedItems ?. length ) {
215
- this [ PropertySymbol . namedItems ] . set ( item [ PropertySymbol . name ] , [ item ] ) ;
226
+ if ( ! itemsByName ?. length ) {
227
+ this [ PropertySymbol . itemsByName ] . set ( item [ PropertySymbol . name ] , [ item ] ) ;
216
228
} else {
217
- const index = namedItems . indexOf ( replacedItem ) ;
229
+ const index = itemsByName . indexOf ( replacedItem ) ;
218
230
if ( index !== - 1 ) {
219
- namedItems . splice ( index , 1 ) ;
231
+ itemsByName . splice ( index , 1 ) ;
220
232
}
221
- namedItems . push ( item ) ;
233
+ itemsByName . push ( item ) ;
222
234
}
223
235
224
236
if ( ! ignoreListeners ) {
@@ -237,19 +249,22 @@ export default class NamedNodeMap {
237
249
public [ PropertySymbol . removeNamedItem ] ( item : Attr , ignoreListeners = false ) : void {
238
250
item [ PropertySymbol . ownerElement ] = null ;
239
251
240
- this [ PropertySymbol . namespaceItems ] . delete (
252
+ this [ PropertySymbol . itemsByNamespaceURI ] . delete (
241
253
`${ item [ PropertySymbol . namespaceURI ] || '' } :${ item [ PropertySymbol . localName ] } `
242
254
) ;
255
+ this [ PropertySymbol . items ] . delete (
256
+ `${ item [ PropertySymbol . namespaceURI ] || '' } :${ item [ PropertySymbol . name ] } `
257
+ ) ;
243
258
244
- const namedItems = this [ PropertySymbol . namedItems ] . get ( item [ PropertySymbol . name ] ) ;
259
+ const itemsByName = this [ PropertySymbol . itemsByName ] . get ( item [ PropertySymbol . name ] ) ;
245
260
246
- if ( namedItems ?. length ) {
247
- const index = namedItems . indexOf ( item ) ;
261
+ if ( itemsByName ?. length ) {
262
+ const index = itemsByName . indexOf ( item ) ;
248
263
if ( index !== - 1 ) {
249
- namedItems . splice ( index , 1 ) ;
264
+ itemsByName . splice ( index , 1 ) ;
250
265
}
251
- if ( ! namedItems . length ) {
252
- this [ PropertySymbol . namedItems ] . delete ( item [ PropertySymbol . name ] ) ;
266
+ if ( ! itemsByName . length ) {
267
+ this [ PropertySymbol . itemsByName ] . delete ( item [ PropertySymbol . name ] ) ;
253
268
}
254
269
}
255
270
0 commit comments