1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.htmlunit.WebClient;
21 import org.htmlunit.corejs.javascript.Callable;
22 import org.htmlunit.corejs.javascript.Context;
23 import org.htmlunit.corejs.javascript.ContextAction;
24 import org.htmlunit.corejs.javascript.Function;
25 import org.htmlunit.corejs.javascript.Scriptable;
26 import org.htmlunit.corejs.javascript.VarScope;
27 import org.htmlunit.html.DomNode;
28 import org.htmlunit.javascript.HtmlUnitContextFactory;
29 import org.htmlunit.javascript.JavaScriptEngine;
30 import org.htmlunit.javascript.configuration.JsxClass;
31 import org.htmlunit.javascript.configuration.JsxConstructor;
32 import org.htmlunit.javascript.configuration.JsxFunction;
33 import org.htmlunit.javascript.configuration.JsxGetter;
34 import org.htmlunit.javascript.configuration.JsxSymbol;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @JsxClass
53 public class NodeList extends AbstractList implements Callable {
54
55
56
57
58 public NodeList() {
59 super();
60 }
61
62
63
64
65 @JsxConstructor
66 public void jsConstructor() {
67
68 }
69
70
71
72
73
74
75
76
77 public NodeList(final DomNode domNode, final boolean attributeChangeSensitive) {
78 super(domNode, attributeChangeSensitive, null);
79 }
80
81
82
83
84
85
86 public NodeList(final DomNode domNode, final List<DomNode> initialElements) {
87 super(domNode, true, new ArrayList<>(initialElements));
88 }
89
90
91
92
93
94 NodeList(final VarScope parentScope) {
95 super();
96 setParentScope(parentScope);
97 setPrototype(getPrototype(getClass()));
98 setExternalArrayData(parentScope, this);
99 }
100
101
102
103
104
105
106
107
108 public static NodeList staticNodeList(final VarScope parentScope, final List<DomNode> elements) {
109 return new NodeList(parentScope) {
110 @Override
111 public List<DomNode> getElements() {
112 return elements;
113 }
114 };
115 }
116
117
118
119
120
121 @JsxFunction
122 public Scriptable keys() {
123 return JavaScriptEngine.newArrayIteratorTypeKeys(getParentScope(), this);
124 }
125
126
127
128
129
130 @JsxFunction
131 @JsxSymbol(symbolName = "iterator")
132 public Scriptable values() {
133 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
134 }
135
136
137
138
139
140 @JsxFunction
141 public Scriptable entries() {
142 return JavaScriptEngine.newArrayIteratorTypeEntries(getParentScope(), this);
143 }
144
145
146
147
148
149 @JsxFunction
150 public void forEach(final Object callback) {
151 if (!(callback instanceof Function function)) {
152 throw JavaScriptEngine.typeError(
153 "Foreach callback '" + JavaScriptEngine.toString(callback) + "' is not a function");
154 }
155
156 if (getElements().isEmpty()) {
157 return;
158 }
159
160 final WebClient client = getWindow().getWebWindow().getWebClient();
161 final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
162
163 final ContextAction<Object> contextAction = cx -> {
164 final VarScope scope = getParentScope();
165
166 List<DomNode> nodes = getElements();
167 final int size = nodes.size();
168 int i = 0;
169 while (i < size && i < nodes.size()) {
170 function.call(cx, scope, this, new Object[] {nodes.get(i).getScriptableObject(), i, this});
171
172
173 nodes = getElements();
174 i++;
175 }
176
177 return null;
178 };
179 cf.call(contextAction);
180 }
181
182
183
184
185
186 @JsxGetter
187 @Override
188 public final int getLength() {
189 return super.getLength();
190 }
191
192
193
194
195
196
197
198 @JsxFunction
199 public Object item(final Object index) {
200 final Object object = getIt(index);
201 if (object == NOT_FOUND) {
202 return null;
203 }
204 return object;
205 }
206
207
208
209
210 @Override
211 public Object call(final Context cx, final VarScope scope, final Scriptable thisObj, final Object[] args) {
212 if (args.length == 0) {
213 throw JavaScriptEngine.reportRuntimeError("Zero arguments; need an index or a key.");
214 }
215 final Object object = getIt(args[0]);
216 if (object == NOT_FOUND) {
217 return null;
218 }
219 return object;
220 }
221
222
223
224
225 @Override
226 protected AbstractList create(final DomNode parentScope, final List<DomNode> initialElements) {
227 return new NodeList(parentScope, new ArrayList<>(initialElements));
228 }
229 }