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.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  @JsxClass(domClass = HtmlLabel.class)
33  public class HTMLLabelElement extends HTMLElement {
34  
35      /**
36       * JavaScript constructor.
37       */
38      @Override
39      @JsxConstructor
40      public void jsConstructor() {
41          super.jsConstructor();
42      }
43  
44      /**
45       * Retrieves the object to which the given label object is assigned.
46       * @return the identifier of the element to which the label element is assigned
47       */
48      @JsxGetter
49      public String getHtmlFor() {
50          return ((HtmlLabel) getDomNodeOrDie()).getForAttribute();
51      }
52  
53      /**
54       * Sets or retrieves the object to which the given label object is assigned.
55       * @param id Specifies the identifier of the element to which the label element is assigned
56       * @see <a href="http://msdn2.microsoft.com/en-us/library/ms533872.aspx">MSDN Documentation</a>
57       */
58      @JsxSetter
59      public void setHtmlFor(final String id) {
60          getDomNodeOrDie().setAttribute("for", id);
61      }
62  
63      /**
64       * @return the HTMLElement labeled by the given label object
65       */
66      @JsxGetter
67      public HTMLElement getControl() {
68          final HtmlLabel label = (HtmlLabel) getDomNodeOrDie();
69          final HtmlElement labeledElement = label.getLabeledElement();
70  
71          if (labeledElement == null) {
72              return null;
73          }
74  
75          return (HTMLElement) getScriptableFor(labeledElement);
76      }
77  
78      /**
79       * Returns the value of the JavaScript {@code form} attribute.
80       *
81       * @return the value of the JavaScript {@code form} attribute
82       */
83      @JsxGetter
84      @Override
85      public HTMLFormElement getForm() {
86          final HtmlLabel label = (HtmlLabel) getDomNodeOrDie();
87          final HtmlElement labeledElement = label.getLabeledElement();
88  
89          if (labeledElement == null) {
90              return null;
91          }
92  
93          final HtmlForm form = labeledElement.getEnclosingForm();
94          if (form == null) {
95              return null;
96          }
97  
98          return (HTMLFormElement) getScriptableFor(form);
99      }
100 }