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.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 Mike Bowler
26 * @author David K. Taylor
27 * @author Christian Sell
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 * Returns whether this element is disabled.
53 *
54 * @return {@code true} if this element is disabled, either because it has
55 * the {@code disabled} attribute or because one of its ancestor
56 * elements is disabled
57 */
58 @Override
59 public final boolean isDisabled() {
60 if (hasAttribute(ATTRIBUTE_DISABLED)) {
61 return true;
62 }
63
64 Node node = getParentNode();
65 while (node != null) {
66 if (node instanceof DisabledElement element
67 && element.isDisabled()) {
68 return true;
69 }
70 node = node.getParentNode();
71 }
72
73 return false;
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public final String getDisabledAttribute() {
81 return getAttributeDirect(ATTRIBUTE_DISABLED);
82 }
83
84 /**
85 * Returns the value of the attribute {@code label}. Refer to the
86 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
87 * documentation for details on the use of this attribute.
88 *
89 * @return the value of the attribute {@code label} or an empty string if that attribute isn't defined
90 */
91 public final String getLabelAttribute() {
92 return getAttributeDirect("label");
93 }
94
95 /**
96 * Sets the value of the attribute {@code label}. Refer to the
97 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
98 * documentation for details on the use of this attribute.
99 *
100 * @param newLabel the value of the attribute {@code label}
101 */
102 public final void setLabelAttribute(final String newLabel) {
103 setAttribute("label", newLabel);
104 }
105
106 /**
107 * Gets the enclosing select of this HtmlOptionGroup.
108 * @return {@code null} if no select is found (for instance malformed html)
109 */
110 public HtmlSelect getEnclosingSelect() {
111 return (HtmlSelect) getEnclosingElement(HtmlSelect.TAG_NAME);
112 }
113
114 /**
115 * {@inheritDoc}
116 */
117 @Override
118 public DisplayStyle getDefaultStyleDisplay() {
119 return DisplayStyle.BLOCK;
120 }
121 }