1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import static org.htmlunit.BrowserVersionFeatures.JS_NAMED_PROPERTIES_OWN_PROPERTY_NAMES_INCLUDE_HIDDEN;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Locale;
23
24 import org.htmlunit.corejs.javascript.CompoundOperationMap;
25 import org.htmlunit.corejs.javascript.Context;
26 import org.htmlunit.corejs.javascript.Scriptable;
27 import org.htmlunit.corejs.javascript.ScriptableObject;
28 import org.htmlunit.html.DomElement;
29 import org.htmlunit.html.DomNode;
30 import org.htmlunit.html.HtmlElement;
31 import org.htmlunit.javascript.HtmlUnitScriptable;
32 import org.htmlunit.javascript.JavaScriptEngine;
33 import org.htmlunit.javascript.configuration.JsxClass;
34 import org.htmlunit.javascript.configuration.JsxConstructor;
35 import org.htmlunit.javascript.configuration.JsxFunction;
36 import org.htmlunit.javascript.configuration.JsxGetter;
37 import org.htmlunit.javascript.configuration.JsxSymbol;
38 import org.htmlunit.javascript.host.dom.Attr;
39 import org.htmlunit.javascript.host.dom.Node;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @JsxClass
55 public class NamedNodeMap extends HtmlUnitScriptable {
56
57 private final org.w3c.dom.NamedNodeMap attributes_;
58
59
60
61
62 public NamedNodeMap() {
63 super();
64 attributes_ = null;
65 }
66
67
68
69
70 @JsxConstructor
71 public void jsConstructor() {
72
73 }
74
75
76
77
78
79
80 public NamedNodeMap(final DomElement element) {
81 super();
82 setParentScope(element.getScriptableObject().getParentScope());
83 setPrototype(getPrototype(getClass()));
84
85 attributes_ = element.getAttributes();
86 setDomNode(element, false);
87 }
88
89
90
91
92 @Override
93 protected int getIndexedPropertyCount() {
94 if (attributes_ == null) {
95 return 0;
96 }
97 return getLength();
98 }
99
100
101
102
103 @Override
104 public final Object get(final int index, final Scriptable start) {
105 final NamedNodeMap startMap = (NamedNodeMap) start;
106 final Object response = startMap.item(index);
107 if (response != null) {
108 return response;
109 }
110 return NOT_FOUND;
111 }
112
113
114
115
116 @Override
117 public Object get(final String name, final Scriptable start) {
118 Object response = super.get(name, start);
119 if (response != NOT_FOUND) {
120 return response;
121 }
122
123 response = getNamedProperty(name);
124 if (response != null) {
125 return response;
126 }
127
128 return NOT_FOUND;
129 }
130
131
132
133
134
135 @Override
136 public boolean has(final String name, final Scriptable start) {
137 if (super.has(name, start)) {
138 return true;
139 }
140 return getNamedProperty(name) != null;
141 }
142
143
144
145
146
147
148 @Override
149 protected Object[] getIds(final CompoundOperationMap<Scriptable> map,
150 final boolean getNonEnumerable, final boolean getSymbols) {
151 final Object[] ids = super.getIds(map, getNonEnumerable, getSymbols);
152 if (attributes_ == null || !getNonEnumerable) {
153 return ids;
154 }
155
156 final boolean includeHidden =
157 getBrowserVersion().hasFeature(JS_NAMED_PROPERTIES_OWN_PROPERTY_NAMES_INCLUDE_HIDDEN);
158 final List<Object> result = new ArrayList<>(Arrays.asList(ids));
159 for (final String name : getSupportedPropertyNames()) {
160 if (!result.contains(name) && (includeHidden || !isPrototypeProperty(name))) {
161 result.add(name);
162 }
163 }
164 return result.toArray();
165 }
166
167
168
169
170
171 @Override
172 protected DescriptorInfo getOwnPropertyDescriptor(final Context cx, final Object id) {
173 final DescriptorInfo descInfo = super.getOwnPropertyDescriptor(cx, id);
174 if (descInfo != null) {
175 return descInfo;
176 }
177
178 if (id instanceof CharSequence) {
179 final HtmlUnitScriptable attr = getNamedProperty(id.toString());
180 if (attr != null) {
181 return ScriptableObject.buildDataDescriptor(attr,
182 ScriptableObject.READONLY | ScriptableObject.DONTENUM);
183 }
184 }
185 return null;
186 }
187
188
189
190
191
192
193
194
195
196 private List<String> getSupportedPropertyNames() {
197 final boolean skipUpperCase = isHtmlElementInHtmlDocument();
198
199 final List<String> names = new ArrayList<>();
200 for (int i = 0; i < attributes_.getLength(); i++) {
201 final String name = attributes_.item(i).getNodeName();
202 if (names.contains(name)) {
203 continue;
204 }
205 if (skipUpperCase && !name.equals(name.toLowerCase(Locale.ROOT))) {
206 continue;
207 }
208 names.add(name);
209 }
210 return names;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224 private HtmlUnitScriptable getNamedProperty(final String name) {
225 if (attributes_ == null || isPrototypeProperty(name)) {
226 return null;
227 }
228
229 final String qualifiedName = isHtmlElementInHtmlDocument() ? name.toLowerCase(Locale.ROOT) : name;
230 for (int i = 0; i < attributes_.getLength(); i++) {
231 final DomNode attr = (DomNode) attributes_.item(i);
232 if (qualifiedName.equals(attr.getNodeName())) {
233 return attr.getScriptableObject();
234 }
235 }
236 return null;
237 }
238
239 private boolean isHtmlElementInHtmlDocument() {
240 final DomNode domNode = getDomNodeOrNull();
241 return domNode instanceof HtmlElement && !domNode.getPage().hasCaseSensitiveTagNames();
242 }
243
244 private boolean isPrototypeProperty(final String name) {
245 final Scriptable prototype = getPrototype();
246 return prototype != null && ScriptableObject.hasProperty(prototype, name);
247 }
248
249
250
251
252
253
254
255
256
257
258 public HtmlUnitScriptable getNamedItemWithoutSytheticClassAttr(final String name) {
259 if (attributes_ != null) {
260 final DomNode attr = (DomNode) attributes_.getNamedItem(name);
261 if (attr != null) {
262 return attr.getScriptableObject();
263 }
264 }
265
266 return null;
267 }
268
269
270
271
272
273
274
275 @JsxFunction
276 public HtmlUnitScriptable getNamedItem(final String name) {
277 return getNamedItemWithoutSytheticClassAttr(name);
278 }
279
280
281
282
283
284
285
286
287 @JsxFunction
288 public Node getNamedItemNS(final String namespaceURI, final String localName) {
289 if (attributes_ != null) {
290 final DomNode attr = (DomNode) attributes_.getNamedItemNS(namespaceURI, localName);
291 if (attr != null) {
292 return attr.getScriptableObject();
293 }
294 }
295
296 return null;
297 }
298
299
300
301
302
303
304 @JsxFunction
305 public void setNamedItem(final Node node) {
306 attributes_.setNamedItem(node.getDomNodeOrDie());
307 }
308
309
310
311
312
313
314 @JsxFunction
315 public void setNamedItemNS(final Node node) {
316 attributes_.setNamedItemNS(node.getDomNodeOrDie());
317 }
318
319
320
321
322
323
324 @JsxFunction
325 public void removeNamedItem(final String name) {
326 attributes_.removeNamedItem(name);
327 }
328
329
330
331
332
333
334
335
336 @JsxFunction
337 public Attr removeNamedItemNS(final String namespaceURI, final String localName) {
338 return (Attr) attributes_.removeNamedItemNS(namespaceURI, localName);
339 }
340
341
342
343
344
345
346
347 @JsxFunction
348 public HtmlUnitScriptable item(final int index) {
349 if (attributes_ == null) {
350 return null;
351 }
352 final DomNode attr = (DomNode) attributes_.item(index);
353 if (attr != null) {
354 return attr.getScriptableObject();
355 }
356 return null;
357 }
358
359
360
361
362
363
364 @JsxGetter
365 public int getLength() {
366 return attributes_.getLength();
367 }
368
369
370
371
372 @Override
373 public boolean has(final int index, final Scriptable start) {
374
375 return index >= 0 && index < getIndexedPropertyCount();
376 }
377
378
379
380
381
382
383 @JsxSymbol
384 public Scriptable iterator() {
385 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
386 }
387 }