1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.html;
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.JavaScriptEngine;
21 import org.htmlunit.javascript.configuration.JsxClass;
22 import org.htmlunit.javascript.configuration.JsxConstructor;
23 import org.htmlunit.javascript.configuration.JsxFunction;
24 import org.htmlunit.javascript.configuration.JsxSymbol;
25 import org.htmlunit.javascript.host.dom.RadioNodeList;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30
31
32
33
34
35
36
37 @JsxClass
38 public class HTMLFormControlsCollection extends HTMLCollection {
39
40
41
42
43 public HTMLFormControlsCollection() {
44 super();
45 }
46
47
48
49
50
51
52
53 public HTMLFormControlsCollection(final DomNode domNode, final boolean attributeChangeSensitive) {
54 super(domNode, attributeChangeSensitive);
55 }
56
57
58
59
60
61
62 HTMLFormControlsCollection(final DomNode domNode, final List<DomNode> initialElements) {
63 super(domNode, initialElements);
64 }
65
66
67
68
69 @Override
70 @JsxConstructor
71 public void jsConstructor() {
72 super.jsConstructor();
73 }
74
75
76
77
78
79
80
81
82 @Override
83 @JsxFunction
84 public Scriptable namedItem(final String name) {
85 if (name.isEmpty()) {
86 return null;
87 }
88
89 final List<DomNode> elements = new ArrayList<>();
90 for (final Object next : getElements()) {
91 if (next instanceof DomElement) {
92 final DomElement elem = (DomElement) next;
93 final String nodeName = elem.getAttributeDirect(DomElement.NAME_ATTRIBUTE);
94 if (name.equals(nodeName)) {
95 elements.add(elem);
96 continue;
97 }
98
99 final String id = elem.getId();
100 if (name.equals(id)) {
101 elements.add(elem);
102 }
103 }
104 }
105
106 if (elements.isEmpty()) {
107 return null;
108 }
109 if (elements.size() == 1) {
110 return getScriptableForElement(elements.get(0));
111 }
112
113 final RadioNodeList nodeList = new RadioNodeList(getDomNodeOrDie(), elements);
114 nodeList.setElementsSupplier(getElementSupplier());
115 return nodeList;
116 }
117
118 @JsxSymbol
119 @Override
120 public Scriptable iterator() {
121 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
122 }
123 }