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 "bindings/core/v8/ExceptionStatePlaceholder.h"
35 #include "bindings/core/v8/ScriptController.h"
36 #include "core/CSSPropertyNames.h"
37 #include "core/InputTypeNames.h"
38 #include "core/events/MouseEvent.h"
39 #include "core/dom/shadow/ShadowRoot.h"
40 #include "core/html/HTMLDataListElement.h"
41 #include "core/html/HTMLDataListOptionsCollection.h"
42 #include "core/html/HTMLDivElement.h"
43 #include "core/html/HTMLInputElement.h"
44 #include "core/html/HTMLOptionElement.h"
45 #include "core/html/forms/ColorChooser.h"
46 #include "core/page/Chrome.h"
47 #include "core/rendering/RenderTheme.h"
48 #include "core/rendering/RenderView.h"
49 #include "platform/RuntimeEnabledFeatures.h"
50 #include "platform/UserGestureIndicator.h"
51 #include "platform/graphics/Color.h"
52 #include "wtf/PassOwnPtr.h"
53 #include "wtf/text/WTFString.h"
57 using namespace HTMLNames;
59 // Upper limit of number of datalist suggestions shown.
60 static const unsigned maxSuggestions = 1000;
61 // Upper limit for the length of the labels for datalist suggestions.
62 static const unsigned maxSuggestionLabelLength = 1000;
64 static bool isValidColorString(const String& value)
71 // We don't accept #rgb and #aarrggbb formats.
72 if (value.length() != 7)
75 return color.setFromString(value) && !color.hasAlpha();
78 PassRefPtrWillBeRawPtr<InputType> ColorInputType::create(HTMLInputElement& element)
80 return adoptRefWillBeNoop(new ColorInputType(element));
83 ColorInputType::~ColorInputType()
88 void ColorInputType::countUsage()
90 countUsageIfVisible(UseCounter::InputTypeColor);
93 const AtomicString& ColorInputType::formControlType() const
95 return InputTypeNames::color;
98 bool ColorInputType::supportsRequired() const
103 String ColorInputType::fallbackValue() const
105 return String("#000000");
108 String ColorInputType::sanitizeValue(const String& proposedValue) const
110 if (!isValidColorString(proposedValue))
111 return fallbackValue();
113 return proposedValue.lower();
116 Color ColorInputType::valueAsColor() const
119 bool success = color.setFromString(element().value());
120 ASSERT_UNUSED(success, success);
124 void ColorInputType::createShadowSubtree()
126 ASSERT(element().shadow());
128 Document& document = element().document();
129 RefPtrWillBeRawPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document);
130 wrapperElement->setShadowPseudoId(AtomicString("-webkit-color-swatch-wrapper", AtomicString::ConstructFromLiteral));
131 RefPtrWillBeRawPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document);
132 colorSwatch->setShadowPseudoId(AtomicString("-webkit-color-swatch", AtomicString::ConstructFromLiteral));
133 wrapperElement->appendChild(colorSwatch.release());
134 element().userAgentShadowRoot()->appendChild(wrapperElement.release());
136 element().updateView();
139 void ColorInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
141 InputType::setValue(value, valueChanged, eventBehavior);
146 element().updateView();
148 m_chooser->setSelectedColor(valueAsColor());
151 void ColorInputType::handleDOMActivateEvent(Event* event)
153 if (element().isDisabledFormControl() || !element().renderer())
156 if (!UserGestureIndicator::processingUserGesture())
159 Chrome* chrome = this->chrome();
160 if (chrome && !m_chooser)
161 m_chooser = chrome->createColorChooser(element().document().frame(), this, valueAsColor());
163 event->setDefaultHandled();
166 void ColorInputType::closePopupView()
171 bool ColorInputType::shouldRespectListAttribute()
176 bool ColorInputType::typeMismatchFor(const String& value) const
178 return !isValidColorString(value);
181 void ColorInputType::didChooseColor(const Color& color)
183 if (element().isDisabledFormControl() || color == valueAsColor())
185 element().setValueFromRenderer(color.serialized());
186 element().updateView();
187 if (!RenderTheme::theme().isModalColorChooser())
188 element().dispatchFormControlChangeEvent();
191 void ColorInputType::didEndChooser()
193 if (RenderTheme::theme().isModalColorChooser())
194 element().dispatchFormControlChangeEvent();
198 void ColorInputType::endColorChooser()
201 m_chooser->endChooser();
204 void ColorInputType::updateView()
206 HTMLElement* colorSwatch = shadowColorSwatch();
210 colorSwatch->setInlineStyleProperty(CSSPropertyBackgroundColor, element().value());
213 HTMLElement* ColorInputType::shadowColorSwatch() const
215 ShadowRoot* shadow = element().userAgentShadowRoot();
216 return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0;
219 Element& ColorInputType::ownerElement() const
224 IntRect ColorInputType::elementRectRelativeToRootView() const
226 return element().document().view()->contentsToRootView(element().pixelSnappedBoundingBox());
229 Color ColorInputType::currentColor()
231 return valueAsColor();
234 bool ColorInputType::shouldShowSuggestions() const
236 return element().fastHasAttribute(listAttr);
239 Vector<ColorSuggestion> ColorInputType::suggestions() const
241 Vector<ColorSuggestion> suggestions;
242 HTMLDataListElement* dataList = element().dataList();
244 RefPtrWillBeRawPtr<HTMLDataListOptionsCollection> options = dataList->options();
245 for (unsigned i = 0; HTMLOptionElement* option = options->item(i); i++) {
246 if (!element().isValidValue(option->value()))
249 if (!color.setFromString(option->value()))
251 ColorSuggestion suggestion(color, option->label().left(maxSuggestionLabelLength));
252 suggestions.append(suggestion);
253 if (suggestions.size() >= maxSuggestions)
260 AXObject* ColorInputType::popupRootAXObject()
262 return m_chooser ? m_chooser->rootAXObject() : 0;
265 ColorChooserClient* ColorInputType::colorChooserClient()