Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / ColorInputType.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "core/html/forms/ColorInputType.h"
33
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"
51
52 namespace WebCore {
53
54 using namespace HTMLNames;
55
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;
60
61 static bool isValidColorString(const String& value)
62 {
63     if (value.isEmpty())
64         return false;
65     if (value[0] != '#')
66         return false;
67
68     // We don't accept #rgb and #aarrggbb formats.
69     if (value.length() != 7)
70         return false;
71     Color color;
72     return color.setFromString(value) && !color.hasAlpha();
73 }
74
75 PassRefPtr<InputType> ColorInputType::create(HTMLInputElement& element)
76 {
77     return adoptRef(new ColorInputType(element));
78 }
79
80 ColorInputType::~ColorInputType()
81 {
82     endColorChooser();
83 }
84
85 void ColorInputType::countUsage()
86 {
87     countUsageIfVisible(UseCounter::InputTypeColor);
88 }
89
90 bool ColorInputType::isColorControl() const
91 {
92     return true;
93 }
94
95 const AtomicString& ColorInputType::formControlType() const
96 {
97     return InputTypeNames::color;
98 }
99
100 bool ColorInputType::supportsRequired() const
101 {
102     return false;
103 }
104
105 String ColorInputType::fallbackValue() const
106 {
107     return String("#000000");
108 }
109
110 String ColorInputType::sanitizeValue(const String& proposedValue) const
111 {
112     if (!isValidColorString(proposedValue))
113         return fallbackValue();
114
115     return proposedValue.lower();
116 }
117
118 Color ColorInputType::valueAsColor() const
119 {
120     Color color;
121     bool success = color.setFromString(element().value());
122     ASSERT_UNUSED(success, success);
123     return color;
124 }
125
126 void ColorInputType::createShadowSubtree()
127 {
128     ASSERT(element().shadow());
129
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());
137
138     updateColorSwatch();
139 }
140
141 void ColorInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
142 {
143     InputType::setValue(value, valueChanged, eventBehavior);
144
145     if (!valueChanged)
146         return;
147
148     updateColorSwatch();
149     if (m_chooser)
150         m_chooser->setSelectedColor(valueAsColor());
151 }
152
153 void ColorInputType::handleDOMActivateEvent(Event* event)
154 {
155     if (element().isDisabledFormControl() || !element().renderer())
156         return;
157
158     if (!UserGestureIndicator::processingUserGesture())
159         return;
160
161     Chrome* chrome = this->chrome();
162     if (chrome && !m_chooser)
163         m_chooser = chrome->createColorChooser(this, valueAsColor());
164
165     event->setDefaultHandled();
166 }
167
168 void ColorInputType::closePopupView()
169 {
170     endColorChooser();
171 }
172
173 bool ColorInputType::shouldRespectListAttribute()
174 {
175     return InputType::themeSupportsDataListUI(this);
176 }
177
178 bool ColorInputType::typeMismatchFor(const String& value) const
179 {
180     return !isValidColorString(value);
181 }
182
183 void ColorInputType::didChooseColor(const Color& color)
184 {
185     if (element().isDisabledFormControl() || color == valueAsColor())
186         return;
187     element().setValueFromRenderer(color.serialized());
188     updateColorSwatch();
189     element().dispatchFormControlChangeEvent();
190 }
191
192 void ColorInputType::didEndChooser()
193 {
194     m_chooser.clear();
195 }
196
197 void ColorInputType::endColorChooser()
198 {
199     if (m_chooser)
200         m_chooser->endChooser();
201 }
202
203 void ColorInputType::updateColorSwatch()
204 {
205     HTMLElement* colorSwatch = shadowColorSwatch();
206     if (!colorSwatch)
207         return;
208
209     colorSwatch->setInlineStyleProperty(CSSPropertyBackgroundColor, element().value());
210 }
211
212 HTMLElement* ColorInputType::shadowColorSwatch() const
213 {
214     ShadowRoot* shadow = element().userAgentShadowRoot();
215     return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0;
216 }
217
218 IntRect ColorInputType::elementRectRelativeToRootView() const
219 {
220     return element().document().view()->contentsToRootView(element().pixelSnappedBoundingBox());
221 }
222
223 Color ColorInputType::currentColor()
224 {
225     return valueAsColor();
226 }
227
228 bool ColorInputType::shouldShowSuggestions() const
229 {
230     return element().fastHasAttribute(listAttr);
231 }
232
233 Vector<ColorSuggestion> ColorInputType::suggestions() const
234 {
235     Vector<ColorSuggestion> suggestions;
236     HTMLDataListElement* dataList = element().dataList();
237     if (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()))
241                 continue;
242             Color color;
243             if (!color.setFromString(option->value()))
244                 continue;
245             ColorSuggestion suggestion(color, option->label().left(maxSuggestionLabelLength));
246             suggestions.append(suggestion);
247             if (suggestions.size() >= maxSuggestions)
248                 break;
249         }
250     }
251     return suggestions;
252 }
253
254 } // namespace WebCore