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 "output".
27   *
28   * @author Ronald Brill
29   * @author Frank Danek
30   */
31  public class HtmlOutput extends HtmlElement implements LabelableElement, ValidatableElement, FormFieldWithNameHistory {
32  
33      /** The HTML tag represented by this element. */
34      public static final String TAG_NAME = "output";
35  
36      private final String originalName_;
37      private Collection<String> newNames_ = Collections.emptySet();
38      private String customValidity_;
39  
40      /**
41       * Creates a new instance.
42       *
43       * @param qualifiedName the qualified name of the element type to instantiate
44       * @param page the page that contains this element
45       * @param attributes the initial attributes
46       */
47      HtmlOutput(final String qualifiedName, final SgmlPage page,
48              final Map<String, DomAttr> attributes) {
49          super(qualifiedName, page, attributes);
50          originalName_ = getAttributeDirect(NAME_ATTRIBUTE);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public DisplayStyle getDefaultStyleDisplay() {
58          return DisplayStyle.INLINE;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public boolean willValidate() {
66          return false;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public void setCustomValidity(final String message) {
74          customValidity_ = message;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public boolean isValid() {
82          return true;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public boolean isCustomErrorValidityState() {
90          return !StringUtils.isEmpty(customValidity_);
91      }
92  
93      @Override
94      public boolean isValidValidityState() {
95          return !isCustomErrorValidityState();
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
103             final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
104         final String qualifiedNameLC = StringUtils.toRootLowerCase(qualifiedName);
105         if (NAME_ATTRIBUTE.equals(qualifiedNameLC)) {
106             if (newNames_.isEmpty()) {
107                 newNames_ = new HashSet<>();
108             }
109             newNames_.add(attributeValue);
110         }
111         super.setAttributeNS(namespaceURI, qualifiedNameLC, attributeValue, notifyAttributeChangeListeners,
112                 notifyMutationObservers);
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public String getOriginalName() {
120         return originalName_;
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public Collection<String> getNewNames() {
128         return newNames_;
129     }
130 }