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 "ColorInputType.h"
36 #include "HTMLDivElement.h"
37 #include "HTMLInputElement.h"
38 #include "MouseEvent.h"
39 #include "ScriptController.h"
40 #include "ShadowRoot.h"
41 #include <wtf/PassOwnPtr.h>
42 #include <wtf/text/WTFString.h>
44 #if ENABLE(INPUT_COLOR)
48 static bool isValidColorString(const String& value)
55 // We don't accept #rgb and #aarrggbb formats.
56 if (value.length() != 7)
59 return color.isValid() && !color.hasAlpha();
62 PassOwnPtr<InputType> ColorInputType::create(HTMLInputElement* element)
64 return adoptPtr(new ColorInputType(element));
67 ColorInputType::~ColorInputType()
69 cleanupColorChooserIfCurrentClient();
72 bool ColorInputType::isColorControl() const
77 const AtomicString& ColorInputType::formControlType() const
79 return InputTypeNames::color();
82 bool ColorInputType::supportsRequired() const
87 String ColorInputType::fallbackValue()
89 return String("#000000");
92 String ColorInputType::sanitizeValue(const String& proposedValue)
94 if (proposedValue.isNull())
97 if (!isValidColorString(proposedValue))
98 return fallbackValue();
100 return proposedValue.lower();
103 Color ColorInputType::valueAsColor() const
105 return Color(element()->value());
108 void ColorInputType::createShadowSubtree()
110 Document* document = element()->document();
111 RefPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document);
112 wrapperElement->setShadowPseudoId("-webkit-color-swatch-wrapper");
113 RefPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document);
114 colorSwatch->setShadowPseudoId("-webkit-color-swatch");
115 ExceptionCode ec = 0;
116 wrapperElement->appendChild(colorSwatch.release(), ec);
118 element()->ensureShadowRoot()->appendChild(wrapperElement.release(), ec);
124 void ColorInputType::setValue(const String& value, bool valueChanged, bool sendChangeEvent);
126 InputType::setValue(value, valueChanged, sendChangeEvent);
132 if (ColorChooser::chooser()->client() == this) {
133 if (Chrome* chrome = this->chrome())
134 chrome->setSelectedColorInColorChooser(valueAsColor());
138 void ColorInputType::handleClickEvent(MouseEvent* event)
140 if (event->isSimulated())
143 if (element()->disabled() || element()->readOnly())
146 if (Chrome* chrome = this->chrome()) {
147 ColorChooser::chooser()->connectClient(this);
148 chrome->openColorChooser(ColorChooser::chooser(), valueAsColor());
150 event->setDefaultHandled();
153 void ColorInputType::handleDOMActivateEvent(Event* event)
155 if (element()->disabled() || element()->readOnly() || !element()->renderer())
158 if (!ScriptController::processingUserGesture())
161 if (Chrome* chrome = this->chrome()) {
162 ColorChooser::chooser()->connectClient(this);
163 chrome->openColorChooser(ColorChooser::chooser(), valueAsColor());
165 event->setDefaultHandled();
168 void ColorInputType::detach()
170 cleanupColorChooserIfCurrentClient();
173 void ColorInputType::didChooseColor(const Color& color)
175 if (element()->disabled() || element()->readOnly() || color == valueAsColor())
177 element()->setValueFromRenderer(color.serialized());
179 element()->dispatchFormControlChangeEvent();
182 bool ColorInputType::isColorInputType() const
187 void ColorInputType::cleanupColorChooserIfCurrentClient() const
189 if (ColorChooser::chooser()->client() != this)
191 if (Chrome* chrome = this->chrome())
192 chrome->cleanupColorChooser();
195 void ColorInputType::updateColorSwatch()
197 HTMLElement* colorSwatch = shadowColorSwatch();
202 colorSwatch->style()->setProperty("background-color", element()->value(), ec);
205 HTMLElement* ColorInputType::shadowColorSwatch() const
207 ShadowRoot* shadow = element()->shadowRoot();
208 return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0;
211 } // namespace WebCore
213 #endif // ENABLE(INPUT_COLOR)