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 ValidatableHTMLElement.doCheckValidity(getDomNodeOrDie());
104 }
105
106 /**
107 * Performs the same validity checking steps as the checkValidity() method.
108 * @return {@code true} if the element is valid
109 */
110 @JsxFunction
111 public boolean reportValidity() {
112 return ValidatableHTMLElement.doReportValidity(getDomNodeOrDie());
113 }
114
115 /**
116 * Returns the message describing why the element's value fails constraint
117 * validation, or "" if it's valid or barred from validation.
118 * @return the validation message
119 */
120 @JsxGetter
121 public String getValidationMessage() {
122 return ValidatableHTMLElement.getValidationMessage(getDomNodeOrDie());
123 }
124
125 /**
126 * Returns a {@link ValidityState} object representing the validity states of this element.
127 * @return a {@link ValidityState} object representing the validity states of this element
128 */
129 @JsxGetter
130 public ValidityState getValidity() {
131 final ValidityState validityState = new ValidityState();
132 validityState.setPrototype(getPrototype(validityState.getClass()));
133 validityState.setParentScope(getParentScope());
134 validityState.setDomNode(getDomNodeOrDie());
135 return validityState;
136 }
137
138 /**
139 * Returns whether the element is a candidate for constraint validation.
140 * @return whether the element is a candidate for constraint validation
141 */
142 @JsxGetter
143 public boolean isWillValidate() {
144 return getDomNodeOrDie().willValidate();
145 }
146
147 /**
148 * Sets the custom validity message for the element to the specified message.
149 * @param message the new message
150 */
151 @JsxFunction
152 public void setCustomValidity(final String message) {
153 getDomNodeOrDie().setCustomValidity(message);
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 @Override
160 public HtmlOutput getDomNodeOrDie() {
161 return (HtmlOutput) super.getDomNodeOrDie();
162 }
163 }