1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import org.htmlunit.corejs.javascript.Scriptable;
18 import org.htmlunit.html.DomElement;
19 import org.htmlunit.html.DomNode;
20 import org.htmlunit.javascript.HtmlUnitScriptable;
21 import org.htmlunit.javascript.JavaScriptEngine;
22 import org.htmlunit.javascript.configuration.JsxClass;
23 import org.htmlunit.javascript.configuration.JsxConstructor;
24 import org.htmlunit.javascript.configuration.JsxFunction;
25 import org.htmlunit.javascript.configuration.JsxGetter;
26 import org.htmlunit.javascript.configuration.JsxSymbol;
27 import org.htmlunit.javascript.host.dom.Attr;
28 import org.htmlunit.javascript.host.dom.Node;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @JsxClass
44 public class NamedNodeMap extends HtmlUnitScriptable {
45
46 private final org.w3c.dom.NamedNodeMap attributes_;
47
48
49
50
51 public NamedNodeMap() {
52 super();
53 attributes_ = null;
54 }
55
56
57
58
59 @JsxConstructor
60 public void jsConstructor() {
61
62 }
63
64
65
66
67
68
69 public NamedNodeMap(final DomElement element) {
70 super();
71 setParentScope(element.getScriptableObject());
72 setPrototype(getPrototype(getClass()));
73
74 attributes_ = element.getAttributes();
75 setDomNode(element, false);
76 }
77
78
79
80
81
82
83 @Override
84 public final Object get(final int index, final Scriptable start) {
85 final NamedNodeMap startMap = (NamedNodeMap) start;
86 final Object response = startMap.item(index);
87 if (response != null) {
88 return response;
89 }
90 return NOT_FOUND;
91 }
92
93
94
95
96 @Override
97 public Object get(final String name, final Scriptable start) {
98 Object response = super.get(name, start);
99 if (response != NOT_FOUND) {
100 return response;
101 }
102
103 response = getNamedItem(name);
104 if (response != null) {
105 return response;
106 }
107
108 return NOT_FOUND;
109 }
110
111
112
113
114
115
116
117
118
119
120 public HtmlUnitScriptable getNamedItemWithoutSytheticClassAttr(final String name) {
121 if (attributes_ != null) {
122 final DomNode attr = (DomNode) attributes_.getNamedItem(name);
123 if (attr != null) {
124 return attr.getScriptableObject();
125 }
126 }
127
128 return null;
129 }
130
131
132
133
134
135
136 @JsxFunction
137 public HtmlUnitScriptable getNamedItem(final String name) {
138 return getNamedItemWithoutSytheticClassAttr(name);
139 }
140
141
142
143
144
145
146
147 @JsxFunction
148 public Node getNamedItemNS(final String namespaceURI, final String localName) {
149 if (attributes_ != null) {
150 final DomNode attr = (DomNode) attributes_.getNamedItemNS(namespaceURI, localName);
151 if (attr != null) {
152 return attr.getScriptableObject();
153 }
154 }
155
156 return null;
157 }
158
159
160
161
162
163 @JsxFunction
164 public void setNamedItem(final Node node) {
165 attributes_.setNamedItem(node.getDomNodeOrDie());
166 }
167
168
169
170
171
172 @JsxFunction
173 public void setNamedItemNS(final Node node) {
174 attributes_.setNamedItemNS(node.getDomNodeOrDie());
175 }
176
177
178
179
180
181 @JsxFunction
182 public void removeNamedItem(final String name) {
183 attributes_.removeNamedItem(name);
184 }
185
186
187
188
189
190
191
192 @JsxFunction
193 public Attr removeNamedItemNS(final String namespaceURI, final String localName) {
194 return (Attr) attributes_.removeNamedItemNS(namespaceURI, localName);
195 }
196
197
198
199
200
201
202 @JsxFunction
203 public HtmlUnitScriptable item(final int index) {
204 final DomNode attr = (DomNode) attributes_.item(index);
205 if (attr != null) {
206 return attr.getScriptableObject();
207 }
208 return null;
209 }
210
211
212
213
214
215 @JsxGetter
216 public int getLength() {
217 return attributes_.getLength();
218 }
219
220
221
222
223 @Override
224 public boolean has(final int index, final Scriptable start) {
225 return index >= 0 && index < getLength();
226 }
227
228
229
230
231 @JsxSymbol
232 public Scriptable iterator() {
233 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
234 }
235 }