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 java.net.MalformedURLException;
18  
19  import org.htmlunit.html.DomElement;
20  import org.htmlunit.html.HtmlForm;
21  import org.htmlunit.html.HtmlObject;
22  import org.htmlunit.html.HtmlPage;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  import org.htmlunit.javascript.configuration.JsxFunction;
26  import org.htmlunit.javascript.configuration.JsxGetter;
27  import org.htmlunit.javascript.configuration.JsxSetter;
28  
29  /**
30   * The JavaScript object {@code HTMLObjectElement}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   * @author Frank Danek
35   *
36   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement">MDN Documentation</a>
37   */
38  @JsxClass(domClass = HtmlObject.class)
39  public class HTMLObjectElement extends HTMLElement {
40  
41      /**
42       * JavaScript constructor.
43       */
44      @Override
45      @JsxConstructor
46      public void jsConstructor() {
47          super.jsConstructor();
48      }
49  
50      /**
51       * Returns the {@code data} property; the url of the data attribute resolved against the
52       * base url of the page (an empty attribute is resolved to the base url, a missing attribute
53       * returns an empty string).
54       * @return the {@code data} property
55       */
56      @JsxGetter
57      public String getData() {
58          final DomElement element = getDomNodeOrDie();
59          final String data = element.getAttributeDirect("data");
60          if (DomElement.ATTRIBUTE_NOT_DEFINED == data) {
61              return "";
62          }
63          if (element.getPage() instanceof HtmlPage page) {
64              try {
65                  return page.getFullyQualifiedUrl(data).toExternalForm();
66              }
67              catch (final MalformedURLException e) {
68                  return data;
69              }
70          }
71          return data;
72      }
73  
74      /**
75       * Sets the {@code data} attribute.
76       * @param data the {@code data} attribute value
77       */
78      @JsxSetter
79      public void setData(final String data) {
80          getDomNodeOrDie().setAttribute("data", data);
81      }
82  
83      /**
84       * Gets the {@code border} attribute.
85       * @return the {@code border} attribute
86       */
87      @JsxGetter
88      public String getBorder() {
89          return getDomNodeOrDie().getAttributeDirect("border");
90      }
91  
92      /**
93       * Sets the {@code border} attribute.
94       * @param border the {@code border} attribute value
95       */
96      @JsxSetter
97      public void setBorder(final String border) {
98          getDomNodeOrDie().setAttribute("border", border);
99      }
100 
101     /**
102      * Gets the {@code classid} attribute.
103      * @return the {@code classid} attribute
104      */
105     public String getClassid() {
106         return getDomNodeOrDie().getAttributeDirect("classid");
107     }
108 
109     /**
110      * Sets the {@code classid} attribute.
111      * @param classid the {@code classid} attribute value
112      */
113     public void setClassid(final String classid) {
114         getDomNodeOrDie().setAttribute("classid", classid);
115     }
116 
117     /**
118      * Returns the value of the {@code width} property.
119      * @return the value of the {@code width} property
120      */
121     @JsxGetter(propertyName = "width")
122     public String getWidth_js() {
123         return getWidthOrHeight("width", Boolean.TRUE);
124     }
125 
126     /**
127      * Sets the value of the {@code width} property.
128      * @param width the value of the {@code width} property
129      */
130     @JsxSetter(propertyName = "width")
131     public void setWidth_js(final String width) {
132         setWidthOrHeight("width", width, true);
133     }
134 
135     /**
136      * Returns the value of the {@code height} property.
137      * @return the value of the {@code height} property
138      */
139     @JsxGetter(propertyName = "height")
140     public String getHeight_js() {
141         return getWidthOrHeight("height", Boolean.TRUE);
142     }
143 
144     /**
145      * Sets the value of the {@code height} property.
146      * @param height the value of the {@code height} property
147      */
148     @JsxSetter(propertyName = "height")
149     public void setHeight_js(final String height) {
150         setWidthOrHeight("height", height, true);
151     }
152 
153     /**
154      * Returns the value of the {@code align} property.
155      * @return the value of the {@code align} property
156      */
157     @JsxGetter
158     public String getAlign() {
159         return getAlign(true);
160     }
161 
162     /**
163      * Sets the value of the {@code align} property.
164      * @param align the value of the {@code align} property
165      */
166     @JsxSetter
167     public void setAlign(final String align) {
168         setAlign(align, false);
169     }
170 
171     /**
172      * Returns the {@code name} attribute.
173      * @return the {@code name} attribute
174      */
175     @JsxGetter
176     @Override
177     public String getName() {
178         return getDomNodeOrDie().getAttributeDirect(DomElement.NAME_ATTRIBUTE);
179     }
180 
181     /**
182      * Sets the {@code name} attribute.
183      * @param name the {@code name} attribute
184      */
185     @JsxSetter
186     @Override
187     public void setName(final String name) {
188         getDomNodeOrDie().setAttribute(DomElement.NAME_ATTRIBUTE, name);
189     }
190 
191     /**
192      * Returns the value of the JavaScript {@code form} attribute.
193      *
194      * @return the value of the JavaScript {@code form} attribute
195      */
196     @JsxGetter
197     @Override
198     public HTMLFormElement getForm() {
199         final HtmlForm form = getDomNodeOrDie().getEnclosingForm();
200         if (form == null) {
201             return null;
202         }
203         return (HTMLFormElement) getScriptableFor(form);
204     }
205 
206     /**
207      * Checks whether the element has any constraints and whether it satisfies them.
208      * @return {@code true} if the element is valid
209      */
210     @JsxFunction
211     public boolean checkValidity() {
212         return ValidatableHTMLElement.doCheckValidity(getDomNodeOrDie());
213     }
214 
215     /**
216      * Performs the same validity checking steps as the checkValidity() method.
217      * @return {@code true} if the element is valid
218      */
219     @JsxFunction
220     public boolean reportValidity() {
221         return ValidatableHTMLElement.doReportValidity(getDomNodeOrDie());
222     }
223 
224     /**
225      * Returns the message describing why the element's value fails constraint
226      * validation, or "" if it's valid or barred from validation.
227      * @return the validation message
228      */
229     @JsxGetter
230     public String getValidationMessage() {
231         return ValidatableHTMLElement.getValidationMessage(getDomNodeOrDie());
232     }
233 
234     /**
235      * Returns a {@link ValidityState} object representing the validity states of this element.
236      * @return a {@link ValidityState} object representing the validity states of this element
237      */
238     @JsxGetter
239     public ValidityState getValidity() {
240         final ValidityState validityState = new ValidityState();
241         validityState.setPrototype(getPrototype(validityState.getClass()));
242         validityState.setParentScope(getParentScope());
243         validityState.setDomNode(getDomNodeOrDie());
244         return validityState;
245     }
246 
247     /**
248      * Returns whether the element is a candidate for constraint validation.
249      * @return whether the element is a candidate for constraint validation
250      */
251     @JsxGetter
252     public boolean isWillValidate() {
253         return getDomNodeOrDie().willValidate();
254     }
255 
256     /**
257      * Sets the custom validity message for the element to the specified message.
258      * @param message the new message
259      */
260     @JsxFunction
261     public void setCustomValidity(final String message) {
262         getDomNodeOrDie().setCustomValidity(message);
263     }
264 
265     /**
266      * {@inheritDoc}
267      */
268     @Override
269     public HtmlObject getDomNodeOrDie() {
270         return (HtmlObject) super.getDomNodeOrDie();
271     }
272 }