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.html;
16  
17  import org.htmlunit.html.DomElement;
18  import org.htmlunit.html.HtmlButton;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxFunction;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.configuration.JsxSetter;
24  import org.htmlunit.javascript.host.dom.NodeList;
25  
26  /**
27   * The JavaScript object that represents a {@link HtmlButton} (<button type=...>).
28   *
29   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
30   * @author Marc Guillemot
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   * @author Frank Danek
34   */
35  @JsxClass(domClass = HtmlButton.class)
36  public class HTMLButtonElement extends HTMLElement {
37  
38      /** "Live" labels collection; has to be a member to have equality (==) working. */
39      private NodeList labels_;
40  
41      /**
42       * JavaScript constructor.
43       */
44      @Override
45      @JsxConstructor
46      public void jsConstructor() {
47          super.jsConstructor();
48      }
49  
50      /**
51       * Sets the value of the attribute {@code type}.
52       * <p>Note that there is no GUI change in the shape of the button,
53       * so we don't treat it like {@link HTMLInputElement#setType(String)}.</p>
54       * @param newType the new type to set
55       */
56      @JsxSetter
57      public void setType(final String newType) {
58          getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, newType);
59      }
60  
61      /**
62       * Returns the {@code type} property.
63       * @return the {@code type} property
64       */
65      @JsxGetter
66      public String getType() {
67          return ((HtmlButton) getDomNodeOrDie()).getType();
68      }
69  
70      /**
71       * Returns the labels associated with the element.
72       * @return the labels associated with the element
73       */
74      @JsxGetter
75      public NodeList getLabels() {
76          if (labels_ == null) {
77              labels_ = new LabelsNodeList(getDomNodeOrDie());
78          }
79          return labels_;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @JsxGetter
86      @Override
87      public String getName() {
88          return super.getName();
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @JsxSetter
95      @Override
96      public void setName(final String newName) {
97          super.setName(newName);
98      }
99  
100     /**
101      * {@inheritDoc} Overridden to modify browser configurations.
102      */
103     @Override
104     @JsxGetter
105     public boolean isDisabled() {
106         return super.isDisabled();
107     }
108 
109     /**
110      * {@inheritDoc} Overridden to modify browser configurations.
111      */
112     @Override
113     @JsxSetter
114     public void setDisabled(final boolean disabled) {
115         super.setDisabled(disabled);
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @JsxGetter
122     @Override
123     public HTMLFormElement getForm() {
124         return super.getForm();
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @JsxGetter
131     @Override
132     public Object getValue() {
133         return super.getValue();
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @JsxSetter
140     @Override
141     public void setValue(final Object newValue) {
142         super.setValue(newValue);
143     }
144 
145     /**
146      * Checks whether the element has any constraints and whether it satisfies them.
147      * @return if the element is valid
148      */
149     @JsxFunction
150     public boolean checkValidity() {
151         return getDomNodeOrDie().isValid();
152     }
153 
154     /**
155      * @return a ValidityState with the validity states that this element is in.
156      */
157     @JsxGetter
158     public ValidityState getValidity() {
159         final ValidityState validityState = new ValidityState();
160         validityState.setPrototype(getPrototype(validityState.getClass()));
161         validityState.setParentScope(getParentScope());
162         validityState.setDomNode(getDomNodeOrDie());
163         return validityState;
164     }
165 
166     /**
167      * @return always false
168      */
169     @JsxGetter
170     public boolean getWillValidate() {
171         return ((HtmlButton) getDomNodeOrDie()).willValidate();
172     }
173 
174     /**
175      * Sets the custom validity message for the element to the specified message.
176      * @param message the new message
177      */
178     @JsxFunction
179     public void setCustomValidity(final String message) {
180         ((HtmlButton) getDomNodeOrDie()).setCustomValidity(message);
181     }
182 
183     /**
184      * Returns the value of the property {@code formnovalidate}.
185      * @return the value of this property
186      */
187     @JsxGetter
188     public boolean isFormNoValidate() {
189         return ((HtmlButton) getDomNodeOrDie()).isFormNoValidate();
190     }
191 
192     /**
193      * Sets the value of the property {@code formnovalidate}.
194      * @param value the new value
195      */
196     @JsxSetter
197     public void setFormNoValidate(final boolean value) {
198         ((HtmlButton) getDomNodeOrDie()).setFormNoValidate(value);
199     }
200 }