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 ((HtmlButton) 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 getDomNodeOrDie().isValid();
154     }
155 
156     /**
157      * Returns a {@link ValidityState} object representing the validity states of this element.
158      * @return a {@link ValidityState} object representing the validity states of this element
159      */
160     @JsxGetter
161     public ValidityState getValidity() {
162         final ValidityState validityState = new ValidityState();
163         validityState.setPrototype(getPrototype(validityState.getClass()));
164         validityState.setParentScope(getParentScope());
165         validityState.setDomNode(getDomNodeOrDie());
166         return validityState;
167     }
168 
169     /**
170      * Returns whether this element will be validated when the form is submitted.
171      * @return always {@code false}
172      */
173     @JsxGetter
174     public boolean isWillValidate() {
175         return ((HtmlButton) getDomNodeOrDie()).willValidate();
176     }
177 
178     /**
179      * Sets the custom validity message for the element to the specified message.
180      * @param message the new message
181      */
182     @JsxFunction
183     public void setCustomValidity(final String message) {
184         ((HtmlButton) getDomNodeOrDie()).setCustomValidity(message);
185     }
186 
187     /**
188      * Returns the value of the property {@code formnovalidate}.
189      * @return the value of the {@code formnovalidate} property
190      */
191     @JsxGetter
192     public boolean isFormNoValidate() {
193         return ((HtmlButton) getDomNodeOrDie()).isFormNoValidate();
194     }
195 
196     /**
197      * Sets the value of the property {@code formnovalidate}.
198      * @param value the new value of the {@code formnovalidate} property
199      */
200     @JsxSetter
201     public void setFormNoValidate(final boolean value) {
202         ((HtmlButton) getDomNodeOrDie()).setFormNoValidate(value);
203     }
204 }