View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.html;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START;
18  
19  import java.awt.Color;
20  import java.util.Map;
21  
22  import com.gargoylesoftware.htmlunit.SgmlPage;
23  
24  /**
25   * Wrapper for the HTML element "input" where type is "color".
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  public class HtmlColorInput extends HtmlInput {
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 && !hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
43              setValueAttribute("#" + Integer.toHexString(Color.black.getRGB()).substring(2));
44          }
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public void setValueAttribute(final String newValue) {
52          if (hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START) || isValid(newValue)) {
53              super.setValueAttribute(newValue);
54          }
55      }
56  
57      private static boolean isValid(final String value) {
58          boolean valid = false;
59          if (value.length() == 7 && value.charAt(0) == '#') {
60              try {
61                  new Color(
62                          Integer.valueOf(value.substring(1, 3), 16),
63                          Integer.valueOf(value.substring(3, 5), 16),
64                          Integer.valueOf(value.substring(5, 7), 16));
65                  valid = true;
66              }
67              catch (final NumberFormatException e) {
68                  // ignore
69              }
70              catch (final IllegalArgumentException e) {
71                  // ignore
72              }
73          }
74          return valid;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      protected boolean isRequiredSupported() {
82          return false;
83      }
84  }