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.DomNode;
18  import org.htmlunit.html.ValidatableElement;
19  import org.htmlunit.javascript.HtmlUnitScriptable;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  
24  /**
25   * A JavaScript object for {@code ValidityState}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  @JsxClass
31  public class ValidityState extends HtmlUnitScriptable {
32  
33      /**
34       * JavaScript constructor.
35       */
36      @JsxConstructor
37      public void jsConstructor() {
38          // nothing to do
39      }
40  
41      private ValidatableElement getValidatableElementOrDie() {
42          return (ValidatableElement) getDomNodeOrDie();
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void setDomNode(final DomNode domNode) {
50          setDomNode(domNode, false);
51      }
52  
53      /**
54       * @return a boolean value that is true if the user has provided
55       *         input that the browser is unable to convert.
56       */
57      @JsxGetter
58      public boolean isBadInput() {
59          return getValidatableElementOrDie().hasBadInputValidityState();
60      }
61  
62      /**
63       * @return whether the customer validity message is set or not
64       */
65      @JsxGetter
66      public boolean isCustomError() {
67          return getValidatableElementOrDie().isCustomErrorValidityState();
68      }
69  
70      /**
71       * Returns whether the element value does not match its {@code pattern} attribute.
72       * @return whether the element value does not match its {@code pattern} attribute
73       */
74      @JsxGetter
75      public boolean isPatternMismatch() {
76          return getValidatableElementOrDie().hasPatternMismatchValidityState();
77      }
78  
79      /**
80       * Returns whether the element value is greater than its {@code max} attribute.
81       * @return whether the element value is greater than its {@code max} attribute
82       */
83      @JsxGetter
84      public boolean isRangeOverflow() {
85          return getValidatableElementOrDie().hasRangeOverflowValidityState();
86      }
87  
88      /**
89       * Returns whether the element value is less than its {@code min} attribute.
90       * @return whether the element value is less than its {@code min} attribute
91       */
92      @JsxGetter
93      public boolean isRangeUnderflow() {
94          return getValidatableElementOrDie().hasRangeUnderflowValidityState();
95      }
96  
97      /**
98       * Returns whether the element value is invalid per its {@code step} attribute.
99       * @return whether the element value is invalid per its {@code step} attribute
100      */
101     @JsxGetter
102     public boolean isStepMismatch() {
103         return getValidatableElementOrDie().isStepMismatchValidityState();
104     }
105 
106     /**
107      * Returns whether the element value exceeds its {@code maxLength} attribute.
108      * @return whether the element value exceeds its {@code maxLength} attribute
109      */
110     @JsxGetter
111     public boolean isTooLong() {
112         return getValidatableElementOrDie().isTooLongValidityState();
113     }
114 
115     /**
116      * Returns whether the element value exceeds its {@code minLength} attribute.
117      * @return whether the element value exceeds its {@code minLength} attribute
118      */
119     @JsxGetter
120     public boolean isTooShort() {
121         return getValidatableElementOrDie().isTooShortValidityState();
122     }
123 
124     /**
125      * Returns whether the element value is invalid per its {@code type} attribute.
126      * @return whether the element value is invalid per its {@code type} attribute
127      */
128     @JsxGetter
129     public boolean isTypeMismatch() {
130         return getValidatableElementOrDie().hasTypeMismatchValidityState();
131     }
132 
133     /**
134      * Returns whether the element (with a {@code required} attribute) has no value.
135      * @return whether the element (with a {@code required} attribute) has no value
136      */
137     @JsxGetter
138     public boolean isValueMissing() {
139         return getValidatableElementOrDie().isValueMissingValidityState();
140     }
141 
142     /**
143      * Returns whether the element value is valid.
144      * @return whether the element value is valid
145      */
146     @JsxGetter
147     public boolean isValid() {
148         return getValidatableElementOrDie().isValidValidityState();
149     }
150 }