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.HtmlElement;
18  import org.htmlunit.html.HtmlForm;
19  import org.htmlunit.html.HtmlLabel;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.configuration.JsxSetter;
24  
25  /**
26   * A JavaScript object for {@code HTMLLabelElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   * @author Frank Danek
31   *
32   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement">MDN Documentation</a>
33   */
34  @JsxClass(domClass = HtmlLabel.class)
35  public class HTMLLabelElement extends HTMLElement {
36  
37      /**
38       * JavaScript constructor.
39       */
40      @Override
41      @JsxConstructor
42      public void jsConstructor() {
43          super.jsConstructor();
44      }
45  
46      /**
47       * Retrieves the object to which the given label object is assigned.
48       * @return the identifier of the element to which the label element is assigned
49       */
50      @JsxGetter
51      public String getHtmlFor() {
52          return ((HtmlLabel) getDomNodeOrDie()).getForAttribute();
53      }
54  
55      /**
56       * Sets or retrieves the object to which the given label object is assigned.
57       * @param id specifies the identifier of the element to which the label element is assigned
58       * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/htmlFor">MDN Documentation</a>
59       */
60      @JsxSetter
61      public void setHtmlFor(final String id) {
62          getDomNodeOrDie().setAttribute("for", id);
63      }
64  
65      /**
66       * Returns the {@link HTMLElement} labeled by the given label object.
67       * @return the HTMLElement labeled by the given label object
68       */
69      @JsxGetter
70      public HTMLElement getControl() {
71          final HtmlLabel label = (HtmlLabel) getDomNodeOrDie();
72          final HtmlElement labeledElement = label.getLabeledElement();
73  
74          if (labeledElement == null) {
75              return null;
76          }
77  
78          return (HTMLElement) getScriptableFor(labeledElement);
79      }
80  
81      /**
82       * Returns the value of the JavaScript {@code form} attribute.
83       *
84       * @return the value of the JavaScript {@code form} attribute
85       */
86      @JsxGetter
87      @Override
88      public HTMLFormElement getForm() {
89          final HtmlLabel label = (HtmlLabel) getDomNodeOrDie();
90          final HtmlElement labeledElement = label.getLabeledElement();
91  
92          if (labeledElement == null) {
93              return null;
94          }
95  
96          final HtmlForm form = labeledElement.getEnclosingForm();
97          if (form == null) {
98              return null;
99          }
100 
101         return (HTMLFormElement) getScriptableFor(form);
102     }
103 }