View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import org.htmlunit.corejs.javascript.Scriptable;
18  import org.htmlunit.html.DomNode;
19  import org.htmlunit.html.HtmlRadioButtonInput;
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.JsxGetter;
24  import org.htmlunit.javascript.configuration.JsxSetter;
25  import org.htmlunit.javascript.configuration.JsxSymbol;
26  
27  import java.util.List;
28  
29  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
30  
31  /**
32   * A JavaScript object for {@code RadioNodeList}.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   * @author Lai Quang Duong
37   */
38  @JsxClass
39  public class RadioNodeList extends NodeList {
40  
41      /**
42       * Creates an instance.
43       */
44      public RadioNodeList() {
45          super();
46      }
47  
48      /**
49       * Creates an instance.
50       *
51       * @param domNode the {@link DomNode}
52       */
53      public RadioNodeList(final DomNode domNode) {
54          super(domNode, true);
55      }
56  
57      /**
58       * Creates an instance.
59       *
60       * @param domNode the {@link DomNode}
61       * @param attributeChangeSensitive indicates if the content of the collection may change when an attribute
62       *        of a descendant node of parentScope changes (attribute added, modified or removed)
63       */
64      public RadioNodeList(final DomNode domNode, final boolean attributeChangeSensitive) {
65          super(domNode, attributeChangeSensitive);
66      }
67  
68      /**
69       * Constructs an instance with an initial cache value.
70       * @param domNode the parent scope, on which we listen for changes
71       * @param initialElements the initial content for the cache
72       */
73      public RadioNodeList(final DomNode domNode, final List<DomNode> initialElements) {
74          super(domNode, initialElements);
75      }
76  
77      /**
78       * JavaScript constructor.
79       */
80      @Override
81      @JsxConstructor
82      public void jsConstructor() {
83          super.jsConstructor();
84      }
85  
86      /**
87       * Returns the value of the first checked radio button represented by radioNodeList.
88       * @return the value of the first checked radio button represented by radioNodeList ("on" if value attribute
89       *         is not defined) or an empty string if no radio button is checked.
90       * @see <a href="https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#the-htmlformcontrolscollection-interface">HTML Standard</a>
91       */
92      @JsxGetter
93      public String getValue() {
94          for (final DomNode node : getElements()) {
95              if (node instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) node).isChecked()) {
96                  final String value = ((HtmlRadioButtonInput) node).getValueAttribute();
97                  return value == ATTRIBUTE_NOT_DEFINED ? "on" : value;
98              }
99          }
100 
101         return "";
102     }
103 
104     /**
105      * Checks the first radio button represented by radioNodeList that has value equal the specified value.
106      * @param newValue the value of the radio button to be checked.
107      * @see <a href="https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#the-htmlformcontrolscollection-interface">HTML Standard</a>
108      */
109     @JsxSetter
110     public void setValue(final String newValue) {
111         for (final DomNode node : getElements()) {
112             if (node instanceof HtmlRadioButtonInput) {
113                 String value = ((HtmlRadioButtonInput) node).getValueAttribute();
114                 if (value == ATTRIBUTE_NOT_DEFINED) {
115                     value = "on";
116                 }
117                 if (newValue.equals(value)) {
118                     ((HtmlRadioButtonInput) node).setChecked(true);
119                     break;
120                 }
121             }
122         }
123     }
124 
125     /**
126      * @return the Iterator symbol
127      */
128     @JsxSymbol
129     public Scriptable iterator() {
130         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
131     }
132 }