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.apache.commons.lang3.StringUtils;
20  import org.htmlunit.SgmlPage;
21  import org.htmlunit.html.impl.Color;
22  
23  /**
24   * Wrapper for the HTML element "input" where type is "color".
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   * @author Frank Danek
29   */
30  public class HtmlColorInput extends HtmlInput implements LabelableElement {
31  
32      /**
33       * Creates an instance.
34       *
35       * @param qualifiedName the qualified name of the element type to instantiate
36       * @param page the page that contains this element
37       * @param attributes the initial attributes
38       */
39      HtmlColorInput(final String qualifiedName, final SgmlPage page,
40              final Map<String, DomAttr> attributes) {
41          super(qualifiedName, page, attributes);
42          if (getValueAttribute() == ATTRIBUTE_NOT_DEFINED) {
43              setValue("#000000");
44          }
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public void setDefaultChecked(final boolean defaultChecked) {
52          // Empty.
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void setValue(final String newValue) {
60          if (StringUtils.isEmpty(newValue)) {
61              super.setValue("#000000");
62              return;
63          }
64  
65          if (isValid(newValue)) {
66              super.setValue(newValue);
67          }
68      }
69  
70      @Override
71      protected void valueAttributeChanged(final String attributeValue, final boolean isValueDirty) {
72          if (isValueDirty) {
73              return;
74          }
75  
76          if (StringUtils.isEmpty(attributeValue)) {
77              setRawValue("#000000");
78              return;
79          }
80  
81          if (isValid(attributeValue)) {
82              setRawValue(attributeValue);
83          }
84      }
85  
86      private static boolean isValid(final String value) {
87          boolean valid = false;
88          if (value.length() == 7 && value.charAt(0) == '#') {
89              try {
90                  new Color(
91                          Integer.valueOf(value.substring(1, 3), 16),
92                          Integer.valueOf(value.substring(3, 5), 16),
93                          Integer.valueOf(value.substring(5, 7), 16));
94                  valid = true;
95              }
96              catch (final IllegalArgumentException ignored) {
97                  // ignore
98              }
99          }
100         return valid;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     protected boolean isRequiredSupported() {
108         return false;
109     }
110 }