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.HtmlForm;
19  import org.htmlunit.html.HtmlOutput;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxFunction;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.javascript.configuration.JsxSetter;
25  import org.htmlunit.javascript.host.dom.NodeList;
26  
27  /**
28   * The JavaScript object {@code HTMLOutputElement}.
29   *
30   * @author Ronald Brill
31   * @author Ahmed Ashour
32   *
33   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLOutputElement">MDN Documentation</a>
34   */
35  @JsxClass(domClass = HtmlOutput.class)
36  public class HTMLOutputElement 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       * Returns the {@code name} attribute.
52       * @return the {@code name} attribute
53       */
54      @JsxGetter
55      @Override
56      public String getName() {
57          return getDomNodeOrDie().getAttributeDirect(DomElement.NAME_ATTRIBUTE);
58      }
59  
60      /**
61       * Sets the {@code name} attribute.
62       * @param name the {@code name} attribute
63       */
64      @JsxSetter
65      @Override
66      public void setName(final String name) {
67          getDomNodeOrDie().setAttribute(DomElement.NAME_ATTRIBUTE, name);
68      }
69  
70      /**
71       * Returns the value of the JavaScript {@code form} attribute.
72       *
73       * @return the value of the JavaScript {@code form} attribute
74       */
75      @JsxGetter
76      @Override
77      public HTMLFormElement getForm() {
78          final HtmlForm form = getDomNodeOrDie().getEnclosingForm();
79          if (form == null) {
80              return null;
81          }
82          return (HTMLFormElement) getScriptableFor(form);
83      }
84  
85      /**
86       * Returns the labels associated with the element.
87       * @return the labels associated with the element
88       */
89      @JsxGetter
90      public NodeList getLabels() {
91          if (labels_ == null) {
92              labels_ = new LabelsNodeList(getDomNodeOrDie());
93          }
94          return labels_;
95      }
96  
97      /**
98       * Checks whether the element has any constraints and whether it satisfies them.
99       * @return {@code true} if the element is valid
100      */
101     @JsxFunction
102     public boolean checkValidity() {
103         return getDomNodeOrDie().isValid();
104     }
105 
106     /**
107      * Returns a {@link ValidityState} object representing the validity states of this element.
108      * @return a {@link ValidityState} object representing the validity states of this element
109      */
110     @JsxGetter
111     public ValidityState getValidity() {
112         final ValidityState validityState = new ValidityState();
113         validityState.setPrototype(getPrototype(validityState.getClass()));
114         validityState.setParentScope(getParentScope());
115         validityState.setDomNode(getDomNodeOrDie());
116         return validityState;
117     }
118 
119     /**
120      * Returns whether the element is a candidate for constraint validation.
121      * @return whether the element is a candidate for constraint validation
122      */
123     @JsxGetter
124     public boolean isWillValidate() {
125         return ((HtmlOutput) getDomNodeOrDie()).willValidate();
126     }
127 
128     /**
129      * Sets the custom validity message for the element to the specified message.
130      * @param message the new message
131      */
132     @JsxFunction
133     public void setCustomValidity(final String message) {
134         ((HtmlOutput) getDomNodeOrDie()).setCustomValidity(message);
135     }
136 }