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