1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.html;
16
17 import static org.htmlunit.BrowserVersionFeatures.JS_HTMLOPTIONSCOLLECTION_NAMED_PROPERTIES_ENUMERABLE;
18 import static org.htmlunit.BrowserVersionFeatures.JS_NAMED_PROPERTIES_OWN_PROPERTY_NAMES_INCLUDE_HIDDEN;
19 import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.htmlunit.SgmlPage;
26 import org.htmlunit.WebAssert;
27 import org.htmlunit.corejs.javascript.CompoundOperationMap;
28 import org.htmlunit.corejs.javascript.Context;
29 import org.htmlunit.corejs.javascript.Scriptable;
30 import org.htmlunit.corejs.javascript.ScriptableObject;
31 import org.htmlunit.corejs.javascript.VarScope;
32 import org.htmlunit.html.DomElement;
33 import org.htmlunit.html.ElementFactory;
34 import org.htmlunit.html.HtmlOption;
35 import org.htmlunit.html.HtmlSelect;
36 import org.htmlunit.javascript.HtmlUnitScriptable;
37 import org.htmlunit.javascript.JavaScriptEngine;
38 import org.htmlunit.javascript.configuration.JsxClass;
39 import org.htmlunit.javascript.configuration.JsxConstructor;
40 import org.htmlunit.javascript.configuration.JsxFunction;
41 import org.htmlunit.javascript.configuration.JsxGetter;
42 import org.htmlunit.javascript.configuration.JsxSetter;
43 import org.htmlunit.javascript.configuration.JsxSymbol;
44 import org.htmlunit.javascript.host.dom.DOMException;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @JsxClass
60 public class HTMLOptionsCollection extends HtmlUnitScriptable {
61
62 private HtmlSelect htmlSelect_;
63
64
65
66
67 public HTMLOptionsCollection() {
68 super();
69 }
70
71
72
73
74 @JsxConstructor
75 public void jsConstructor() {
76
77 }
78
79
80
81
82
83 public HTMLOptionsCollection(final VarScope parentScope) {
84 super();
85 setParentScope(parentScope);
86 setPrototype(getPrototype(getClass()));
87 }
88
89
90
91
92
93 public void initialize(final HtmlSelect select) {
94 WebAssert.notNull("select", select);
95 htmlSelect_ = select;
96 }
97
98
99
100
101 @Override
102 protected int getIndexedPropertyCount() {
103 if (htmlSelect_ == null) {
104 return 0;
105 }
106 return getLength();
107 }
108
109
110
111
112 @Override
113 protected boolean isIndexedPropertyWritable() {
114 return true;
115 }
116
117
118
119
120
121 @Override
122 public Object get(final String name, final Scriptable start) {
123 final Object response = super.get(name, start);
124 if (response != NOT_FOUND) {
125 return response;
126 }
127
128 final HtmlOption option = getNamedOption(name);
129 if (option != null) {
130 return getScriptableFor(option);
131 }
132 return NOT_FOUND;
133 }
134
135
136
137
138
139 @Override
140 public boolean has(final String name, final Scriptable start) {
141 return super.has(name, start) || getNamedOption(name) != null;
142 }
143
144
145
146
147
148
149 @Override
150 protected Object[] getIds(final CompoundOperationMap<Scriptable> map,
151 final boolean getNonEnumerable, final boolean getSymbols) {
152 final Object[] ids = super.getIds(map, getNonEnumerable, getSymbols);
153 if (htmlSelect_ == null
154 || !(getNonEnumerable
155 || getBrowserVersion().hasFeature(JS_HTMLOPTIONSCOLLECTION_NAMED_PROPERTIES_ENUMERABLE))) {
156 return ids;
157 }
158
159
160 final boolean includeHidden = getNonEnumerable
161 && getBrowserVersion().hasFeature(JS_NAMED_PROPERTIES_OWN_PROPERTY_NAMES_INCLUDE_HIDDEN);
162 final List<Object> result = new ArrayList<>(Arrays.asList(ids));
163 for (final String name : getSupportedPropertyNames()) {
164 if (!result.contains(name) && (includeHidden || getNamedOption(name) != null)) {
165 result.add(name);
166 }
167 }
168 return result.toArray();
169 }
170
171
172
173
174
175 @Override
176 protected DescriptorInfo getOwnPropertyDescriptor(final Context cx, final Object id) {
177 final DescriptorInfo descInfo = super.getOwnPropertyDescriptor(cx, id);
178 if (descInfo != null) {
179 return descInfo;
180 }
181
182 if (id instanceof CharSequence) {
183 final HtmlOption option = getNamedOption(id.toString());
184 if (option != null) {
185 int attributes = ScriptableObject.READONLY;
186 if (!getBrowserVersion().hasFeature(JS_HTMLOPTIONSCOLLECTION_NAMED_PROPERTIES_ENUMERABLE)) {
187 attributes |= ScriptableObject.DONTENUM;
188 }
189 return ScriptableObject.buildDataDescriptor(getScriptableFor(option), attributes);
190 }
191 }
192 return null;
193 }
194
195
196
197
198
199
200
201
202 private List<String> getSupportedPropertyNames() {
203 final List<String> names = new ArrayList<>();
204 for (final HtmlOption option : htmlSelect_.getOptions()) {
205 final String id = option.getId();
206 if (!id.isEmpty() && !names.contains(id)) {
207 names.add(id);
208 }
209 final String name = option.getAttributeDirect(DomElement.NAME_ATTRIBUTE);
210 if (ATTRIBUTE_NOT_DEFINED != name && !name.isEmpty() && !names.contains(name)) {
211 names.add(name);
212 }
213 }
214 return names;
215 }
216
217
218
219
220
221
222
223
224 private HtmlOption getNamedOption(final String name) {
225 if (htmlSelect_ == null || name == null || name.isEmpty()) {
226 return null;
227 }
228 final Scriptable prototype = getPrototype();
229 if (prototype != null && ScriptableObject.hasProperty(prototype, name)) {
230 return null;
231 }
232
233 for (final HtmlOption option : htmlSelect_.getOptions()) {
234 if (name.equals(option.getId())
235 || name.equals(option.getAttributeDirect(DomElement.NAME_ATTRIBUTE))) {
236 return option;
237 }
238 }
239 return null;
240 }
241
242
243
244
245
246
247
248
249 @Override
250 public Object get(final int index, final Scriptable start) {
251 if (htmlSelect_ == null || index < 0) {
252 return JavaScriptEngine.UNDEFINED;
253 }
254
255 if (index >= htmlSelect_.getOptionSize()) {
256 return JavaScriptEngine.UNDEFINED;
257 }
258
259 return getScriptableFor(htmlSelect_.getOption(index));
260 }
261
262
263
264
265 @Override
266 public void put(final String name, final Scriptable start, final Object value) {
267 if (htmlSelect_ == null) {
268
269
270 super.put(name, start, value);
271 return;
272 }
273
274 final HTMLSelectElement parent = htmlSelect_.getScriptableObject();
275
276 if (!super.has(name, start) && ScriptableObject.hasProperty(parent, name)) {
277 ScriptableObject.putProperty(parent, name, value);
278 }
279 else {
280 super.put(name, start, value);
281 }
282 }
283
284
285
286
287
288
289
290 @JsxFunction
291 public Object item(final int index) {
292 final Object item = get(index, this);
293 if (JavaScriptEngine.UNDEFINED == item) {
294 return null;
295 }
296 return item;
297 }
298
299
300
301
302
303
304
305 @Override
306 public void put(final int index, final Scriptable start, final Object newValue) {
307 if (newValue == null) {
308
309 htmlSelect_.removeOption(index);
310 }
311 else {
312 final HTMLOptionElement option = (HTMLOptionElement) newValue;
313 final HtmlOption htmlOption = (HtmlOption) option.getDomNodeOrNull();
314 if (index >= getLength()) {
315 setLength(index);
316
317 htmlSelect_.appendOption(htmlOption);
318 }
319 else {
320
321 htmlSelect_.replaceOption(index, htmlOption);
322 }
323 }
324 }
325
326
327
328
329
330
331 @JsxGetter
332 public int getLength() {
333 return htmlSelect_.getOptionSize();
334 }
335
336
337
338
339
340
341
342 @JsxSetter
343 public void setLength(final int newLength) {
344 if (newLength < 0) {
345 return;
346 }
347
348 final int currentLength = htmlSelect_.getOptionSize();
349 if (currentLength > newLength) {
350 htmlSelect_.setOptionSize(newLength);
351 }
352 else {
353 final SgmlPage page = htmlSelect_.getPage();
354 final ElementFactory factory = page.getWebClient().getPageCreator()
355 .getHtmlParser().getFactory(HtmlOption.TAG_NAME);
356 for (int i = currentLength; i < newLength; i++) {
357 final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
358 htmlSelect_.appendOption(option);
359 }
360 }
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400 @JsxFunction
401 public void add(final Object newOptionObject, final Object beforeOptionObject) {
402 final HtmlOption htmlOption = (HtmlOption) ((HTMLOptionElement) newOptionObject).getDomNodeOrNull();
403
404 HtmlOption beforeOption = null;
405
406 if (beforeOptionObject instanceof Number) {
407 final int index = ((Integer) Context.jsToJava(beforeOptionObject, Integer.class)).intValue();
408 if (index < 0 || index >= getLength()) {
409
410 htmlSelect_.appendOption(htmlOption);
411 return;
412 }
413
414 beforeOption = (HtmlOption) ((HTMLOptionElement) item(index)).getDomNodeOrDie();
415 }
416 else if (beforeOptionObject instanceof HTMLOptionElement element) {
417 beforeOption = (HtmlOption) element.getDomNodeOrDie();
418 if (beforeOption.getParentNode() != htmlSelect_) {
419 throw JavaScriptEngine.asJavaScriptException(
420 getWindow(),
421 "Unknown option.",
422 DOMException.NOT_FOUND_ERR);
423
424 }
425 }
426
427 if (beforeOption == null) {
428 htmlSelect_.appendOption(htmlOption);
429 return;
430 }
431
432 beforeOption.insertBefore(htmlOption);
433 }
434
435
436
437
438
439 @JsxFunction
440 public void remove(final int index) {
441 if (index < 0 || index >= getLength()) {
442 return;
443 }
444
445 htmlSelect_.removeOption(index);
446 }
447
448
449
450
451
452 @JsxGetter
453 public int getSelectedIndex() {
454 return htmlSelect_.getSelectedIndex();
455 }
456
457
458
459
460
461 @JsxSetter
462 public void setSelectedIndex(final int index) {
463 htmlSelect_.setSelectedIndex(index);
464 }
465
466
467
468
469
470 @JsxSymbol
471 public Scriptable iterator() {
472 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
473 }
474 }