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.Collection;
18  import java.util.Collections;
19  import java.util.HashSet;
20  import java.util.Map;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.htmlunit.SgmlPage;
24  
25  /**
26   * Wrapper for the HTML element "fieldset".
27   *
28   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
29   * @author David K. Taylor
30   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
31   * @author Ahmed Ashour
32   * @author Frank Danek
33   * @author Ronald Brill
34   */
35  public class HtmlFieldSet extends HtmlElement implements DisabledElement, ValidatableElement, FormFieldWithNameHistory {
36  
37      /** The HTML tag represented by this element. */
38      public static final String TAG_NAME = "fieldset";
39  
40      private final String originalName_;
41      private Collection<String> newNames_ = Collections.emptySet();
42      private String customValidity_;
43  
44      /**
45       * Creates an instance of HtmlFieldSet
46       *
47       * @param qualifiedName the qualified name of the element type to instantiate
48       * @param page the HtmlPage that contains this element
49       * @param attributes the initial attributes
50       */
51      HtmlFieldSet(final String qualifiedName, final SgmlPage page,
52              final Map<String, DomAttr> attributes) {
53          super(qualifiedName, page, attributes);
54          originalName_ = getAttributeDirect(NAME_ATTRIBUTE);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public boolean willValidate() {
62          return false;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void setCustomValidity(final String message) {
70          customValidity_ = message;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public boolean isCustomErrorValidityState() {
78          return !StringUtils.isEmpty(customValidity_);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public boolean isValid() {
86          return true;
87      }
88  
89      @Override
90      public boolean isValidValidityState() {
91          return !isCustomErrorValidityState();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
99              final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
100         final String qualifiedNameLC = StringUtils.toRootLowerCase(qualifiedName);
101         if (NAME_ATTRIBUTE.equals(qualifiedNameLC)) {
102             if (newNames_.isEmpty()) {
103                 newNames_ = new HashSet<>();
104             }
105             newNames_.add(attributeValue);
106         }
107         super.setAttributeNS(namespaceURI, qualifiedNameLC, attributeValue, notifyAttributeChangeListeners,
108                 notifyMutationObservers);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String getOriginalName() {
116         return originalName_;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public Collection<String> getNewNames() {
124         return newNames_;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public final String getDisabledAttribute() {
132         return getAttributeDirect(ATTRIBUTE_DISABLED);
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public final boolean isDisabled() {
140         if (hasAttribute(ATTRIBUTE_DISABLED)) {
141             return true;
142         }
143 
144         DomNode node = getParentNode();
145         while (node != null) {
146             if (node instanceof DisabledElement
147                     && ((DisabledElement) node).isDisabled()) {
148                 return true;
149             }
150             node = node.getParentNode();
151         }
152 
153         return false;
154     }
155 }