2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/html/forms/ColorInputType.h"
34 #include "CSSPropertyNames.h"
35 #include "InputTypeNames.h"
36 #include "RuntimeEnabledFeatures.h"
37 #include "bindings/v8/ExceptionStatePlaceholder.h"
38 #include "bindings/v8/ScriptController.h"
39 #include "core/events/MouseEvent.h"
40 #include "core/dom/shadow/ShadowRoot.h"
41 #include "core/html/HTMLDataListElement.h"
42 #include "core/html/HTMLDivElement.h"
43 #include "core/html/HTMLInputElement.h"
44 #include "core/html/HTMLOptionElement.h"
45 #include "core/page/Chrome.h"
46 #include "core/rendering/RenderView.h"
47 #include "platform/UserGestureIndicator.h"
48 #include "platform/graphics/Color.h"
49 #include "wtf/PassOwnPtr.h"
50 #include "wtf/text/WTFString.h"
54 using namespace HTMLNames;
56 // Upper limit of number of datalist suggestions shown.
57 static const unsigned maxSuggestions = 1000;
58 // Upper limit for the length of the labels for datalist suggestions.
59 static const unsigned maxSuggestionLabelLength = 1000;
61 static bool isValidColorString(const String& value)
68 // We don't accept #rgb and #aarrggbb formats.
69 if (value.length() != 7)
72 return color.setFromString(value) && !color.hasAlpha();
75 PassRefPtr<InputType> ColorInputType::create(HTMLInputElement& element)
77 return adoptRef(new ColorInputType(element));
80 ColorInputType::~ColorInputType()
85 void ColorInputType::countUsage()
87 countUsageIfVisible(UseCounter::InputTypeColor);
90 bool ColorInputType::isColorControl() const
95 const AtomicString& ColorInputType::formControlType() const
97 return InputTypeNames::color;
100 bool ColorInputType::supportsRequired() const
105 String ColorInputType::fallbackValue() const
107 return String("#000000");
110 String ColorInputType::sanitizeValue(const String& proposedValue) const
112 if (!isValidColorString(proposedValue))
113 return fallbackValue();
115 return proposedValue.lower();
118 Color ColorInputType::valueAsColor() const
121 bool success = color.setFromString(element().value());
122 ASSERT_UNUSED(success, success);
126 void ColorInputType::createShadowSubtree()
128 ASSERT(element().shadow());
130 Document& document = element().document();
131 RefPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document);
132 wrapperElement->setShadowPseudoId(AtomicString("-webkit-color-swatch-wrapper", AtomicString::ConstructFromLiteral));
133 RefPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document);
134 colorSwatch->setShadowPseudoId(AtomicString("-webkit-color-swatch", AtomicString::ConstructFromLiteral));
135 wrapperElement->appendChild(colorSwatch.release());
136 element().userAgentShadowRoot()->appendChild(wrapperElement.release());
141 void ColorInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
143 InputType::setValue(value, valueChanged, eventBehavior);
150 m_chooser->setSelectedColor(valueAsColor());
153 void ColorInputType::handleDOMActivateEvent(Event* event)
155 if (element().isDisabledFormControl() || !element().renderer())
158 if (!UserGestureIndicator::processingUserGesture())
161 Chrome* chrome = this->chrome();
162 if (chrome && !m_chooser)
163 m_chooser = chrome->createColorChooser(this, valueAsColor());
165 event->setDefaultHandled();
168 void ColorInputType::closePopupView()
173 bool ColorInputType::shouldRespectListAttribute()
175 return InputType::themeSupportsDataListUI(this);
178 bool ColorInputType::typeMismatchFor(const String& value) const
180 return !isValidColorString(value);
183 void ColorInputType::didChooseColor(const Color& color)
185 if (element().isDisabledFormControl() || color == valueAsColor())
187 element().setValueFromRenderer(color.serialized());
189 element().dispatchFormControlChangeEvent();
192 void ColorInputType::didEndChooser()
197 void ColorInputType::endColorChooser()
200 m_chooser->endChooser();
203 void ColorInputType::updateColorSwatch()
205 HTMLElement* colorSwatch = shadowColorSwatch();
209 colorSwatch->setInlineStyleProperty(CSSPropertyBackgroundColor, element().value());
212 HTMLElement* ColorInputType::shadowColorSwatch() const
214 ShadowRoot* shadow = element().userAgentShadowRoot();
215 return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0;
218 IntRect ColorInputType::elementRectRelativeToRootView() const
220 return element().document().view()->contentsToRootView(element().pixelSnappedBoundingBox());
223 Color ColorInputType::currentColor()
225 return valueAsColor();
228 bool ColorInputType::shouldShowSuggestions() const
230 return element().fastHasAttribute(listAttr);
233 Vector<ColorSuggestion> ColorInputType::suggestions() const
235 Vector<ColorSuggestion> suggestions;
236 HTMLDataListElement* dataList = element().dataList();
238 RefPtr<HTMLCollection> options = dataList->options();
239 for (unsigned i = 0; HTMLOptionElement* option = toHTMLOptionElement(options->item(i)); i++) {
240 if (!element().isValidValue(option->value()))
243 if (!color.setFromString(option->value()))
245 ColorSuggestion suggestion(color, option->label().left(maxSuggestionLabelLength));
246 suggestions.append(suggestion);
247 if (suggestions.size() >= maxSuggestions)
254 } // namespace WebCore