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.html;
16  
17  import java.util.Map;
18  
19  import org.htmlunit.SgmlPage;
20  import org.w3c.dom.Node;
21  
22  /**
23   * Wrapper for the HTML element "optgroup".
24   *
25   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
26   * @author David K. Taylor
27   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
28   * @author David D. Kilzer
29   * @author Ahmed Ashour
30   * @author Daniel Gredler
31   * @author Ronald Brill
32   * @author Frank Danek
33   */
34  public class HtmlOptionGroup extends HtmlElement implements DisabledElement {
35  
36      /** The HTML tag represented by this element. */
37      public static final String TAG_NAME = "optgroup";
38  
39      /**
40       * Creates an instance of HtmlOptionGroup
41       *
42       * @param qualifiedName the qualified name of the element type to instantiate
43       * @param page the HtmlPage that contains this element
44       * @param attributes the initial attributes
45       */
46      HtmlOptionGroup(final String qualifiedName, final SgmlPage page,
47              final Map<String, DomAttr> attributes) {
48          super(qualifiedName, page, attributes);
49      }
50  
51      /**
52       * @return {@code true} if the disabled attribute is set for this element
53       */
54      @Override
55      public final boolean isDisabled() {
56          if (hasAttribute(ATTRIBUTE_DISABLED)) {
57              return true;
58          }
59  
60          Node node = getParentNode();
61          while (node != null) {
62              if (node instanceof DisabledElement
63                      && ((DisabledElement) node).isDisabled()) {
64                  return true;
65              }
66              node = node.getParentNode();
67          }
68  
69          return false;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public final String getDisabledAttribute() {
77          return getAttributeDirect(ATTRIBUTE_DISABLED);
78      }
79  
80      /**
81       * Returns the value of the attribute {@code label}. Refer to the
82       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
83       * documentation for details on the use of this attribute.
84       *
85       * @return the value of the attribute {@code label} or an empty string if that attribute isn't defined
86       */
87      public final String getLabelAttribute() {
88          return getAttributeDirect("label");
89      }
90  
91      /**
92       * Sets the value of the attribute {@code label}. Refer to the
93       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
94       * documentation for details on the use of this attribute.
95       *
96       * @param newLabel the value of the attribute {@code label}
97       */
98      public final void setLabelAttribute(final String newLabel) {
99          setAttribute("label", newLabel);
100     }
101 
102     /**
103      * Gets the enclosing select of this HtmlOptionGroup.
104      * @return {@code null} if no select is found (for instance malformed html)
105      */
106     public HtmlSelect getEnclosingSelect() {
107         return (HtmlSelect) getEnclosingElement(HtmlSelect.TAG_NAME);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public DisplayStyle getDefaultStyleDisplay() {
115         return DisplayStyle.BLOCK;
116     }
117 }