View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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 Mike Bowler
30   * @author Marc Guillemot
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   * @author Frank Danek
34   *
35   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement">MDN Documentation</a>
36   */
37  @JsxClass(domClass = HtmlButton.class)
38  public class HTMLButtonElement extends HTMLElement {
39  
40      /** "Live" labels collection; has to be a member to have equality (==) working. */
41      private NodeList labels_;
42  
43      /**
44       * JavaScript constructor.
45       */
46      @Override
47      @JsxConstructor
48      public void jsConstructor() {
49          super.jsConstructor();
50      }
51  
52      /**
53       * Sets the value of the attribute {@code type}.
54       * <p>Note that there is no GUI change in the shape of the button,
55       * so we don't treat it like {@link HTMLInputElement#setType(String)}.</p>
56       * @param newType the new type to set
57       */
58      @JsxSetter
59      public void setType(final String newType) {
60          getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, newType);
61      }
62  
63      /**
64       * Returns the {@code type} property.
65       * @return the {@code type} property
66       */
67      @JsxGetter
68      public String getType() {
69          return getDomNodeOrDie().getType();
70      }
71  
72      /**
73       * Returns the labels associated with the element.
74       * @return the labels associated with the element
75       */
76      @JsxGetter
77      public NodeList getLabels() {
78          if (labels_ == null) {
79              labels_ = new LabelsNodeList(getDomNodeOrDie());
80          }
81          return labels_;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @JsxGetter
88      @Override
89      public String getName() {
90          return super.getName();
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @JsxSetter
97      @Override
98      public void setName(final String newName) {
99          super.setName(newName);
100     }
101 
102     /**
103      * {@inheritDoc} Overridden to modify browser configurations.
104      */
105     @Override
106     @JsxGetter
107     public boolean isDisabled() {
108         return super.isDisabled();
109     }
110 
111     /**
112      * {@inheritDoc} Overridden to modify browser configurations.
113      */
114     @Override
115     @JsxSetter
116     public void setDisabled(final boolean disabled) {
117         super.setDisabled(disabled);
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @JsxGetter
124     @Override
125     public HTMLFormElement getForm() {
126         return super.getForm();
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @JsxGetter
133     @Override
134     public Object getValue() {
135         return super.getValue();
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @JsxSetter
142     @Override
143     public void setValue(final Object newValue) {
144         super.setValue(newValue);
145     }
146 
147     /**
148      * Checks whether the element has any constraints and whether it satisfies them.
149      * @return {@code true} if the element is valid
150      */
151     @JsxFunction
152     public boolean checkValidity() {
153         return ValidatableHTMLElement.doCheckValidity(getDomNodeOrDie());
154     }
155 
156     /**
157      * Performs the same validity checking steps as the checkValidity() method.
158      * @return {@code true} if the element is valid
159      */
160     @JsxFunction
161     public boolean reportValidity() {
162         return ValidatableHTMLElement.doReportValidity(getDomNodeOrDie());
163     }
164 
165     /**
166      * Returns a {@link ValidityState} object representing the validity states of this element.
167      * @return a {@link ValidityState} object representing the validity states of this element
168      */
169     @JsxGetter
170     public ValidityState getValidity() {
171         final ValidityState validityState = new ValidityState();
172         validityState.setPrototype(getPrototype(validityState.getClass()));
173         validityState.setParentScope(getParentScope());
174         validityState.setDomNode(getDomNodeOrDie());
175         return validityState;
176     }
177 
178     /**
179      * Returns the message describing why the element's value fails constraint
180      * validation, or "" if it's valid or barred from validation.
181      * @return the validation message
182      */
183     @JsxGetter
184     public String getValidationMessage() {
185         return ValidatableHTMLElement.getValidationMessage(getDomNodeOrDie());
186     }
187 
188     @Override
189     public HtmlButton getDomNodeOrDie() {
190         return (HtmlButton) super.getDomNodeOrDie();
191     }
192 
193     /**
194      * Returns whether this element will be validated when the form is submitted.
195      * @return always {@code false}
196      */
197     @JsxGetter
198     public boolean isWillValidate() {
199         return getDomNodeOrDie().willValidate();
200     }
201 
202     /**
203      * Sets the custom validity message for the element to the specified message.
204      * @param message the new message
205      */
206     @JsxFunction
207     public void setCustomValidity(final String message) {
208         getDomNodeOrDie().setCustomValidity(message);
209     }
210 
211     /**
212      * Returns the value of the property {@code formnovalidate}.
213      * @return the value of the {@code formnovalidate} property
214      */
215     @JsxGetter
216     public boolean isFormNoValidate() {
217         return getDomNodeOrDie().isFormNoValidate();
218     }
219 
220     /**
221      * Sets the value of the property {@code formnovalidate}.
222      * @param value the new value of the {@code formnovalidate} property
223      */
224     @JsxSetter
225     public void setFormNoValidate(final boolean value) {
226         getDomNodeOrDie().setFormNoValidate(value);
227     }
228 }