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.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  @JsxClass(domClass = HtmlOutput.class)
34  public class HTMLOutputElement extends HTMLElement {
35  
36      /** "Live" labels collection; has to be a member to have equality (==) working. */
37      private NodeList labels_;
38  
39      /**
40       * JavaScript constructor.
41       */
42      @Override
43      @JsxConstructor
44      public void jsConstructor() {
45          super.jsConstructor();
46      }
47  
48      /**
49       * Returns the {@code name} attribute.
50       * @return the {@code name} attribute
51       */
52      @JsxGetter
53      @Override
54      public String getName() {
55          return getDomNodeOrDie().getAttributeDirect(DomElement.NAME_ATTRIBUTE);
56      }
57  
58      /**
59       * Sets the {@code name} attribute.
60       * @param name the {@code name} attribute
61       */
62      @JsxSetter
63      @Override
64      public void setName(final String name) {
65          getDomNodeOrDie().setAttribute(DomElement.NAME_ATTRIBUTE, name);
66      }
67  
68      /**
69       * Returns the value of the JavaScript {@code form} attribute.
70       *
71       * @return the value of the JavaScript {@code form} attribute
72       */
73      @JsxGetter
74      @Override
75      public HTMLFormElement getForm() {
76          final HtmlForm form = getDomNodeOrDie().getEnclosingForm();
77          if (form == null) {
78              return null;
79          }
80          return (HTMLFormElement) getScriptableFor(form);
81      }
82  
83      /**
84       * Returns the labels associated with the element.
85       * @return the labels associated with the element
86       */
87      @JsxGetter
88      public NodeList getLabels() {
89          if (labels_ == null) {
90              labels_ = new LabelsNodeList(getDomNodeOrDie());
91          }
92          return labels_;
93      }
94  
95      /**
96       * Checks whether the element has any constraints and whether it satisfies them.
97       * @return if the element is valid
98       */
99      @JsxFunction
100     public boolean checkValidity() {
101         return getDomNodeOrDie().isValid();
102     }
103 
104     /**
105      * @return a ValidityState with the validity states that this element is in.
106      */
107     @JsxGetter
108     public ValidityState getValidity() {
109         final ValidityState validityState = new ValidityState();
110         validityState.setPrototype(getPrototype(validityState.getClass()));
111         validityState.setParentScope(getParentScope());
112         validityState.setDomNode(getDomNodeOrDie());
113         return validityState;
114     }
115 
116     /**
117      * @return whether the element is a candidate for constraint validation
118      */
119     @JsxGetter
120     public boolean getWillValidate() {
121         return ((HtmlOutput) getDomNodeOrDie()).willValidate();
122     }
123 
124     /**
125      * Sets the custom validity message for the element to the specified message.
126      * @param message the new message
127      */
128     @JsxFunction
129     public void setCustomValidity(final String message) {
130         ((HtmlOutput) getDomNodeOrDie()).setCustomValidity(message);
131     }
132 }