OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 if (value[0] != '#') | 60 if (value[0] != '#') |
61 return false; | 61 return false; |
62 | 62 |
63 // We don't accept #rgb and #aarrggbb formats. | 63 // We don't accept #rgb and #aarrggbb formats. |
64 if (value.length() != 7) | 64 if (value.length() != 7) |
65 return false; | 65 return false; |
66 Color color(value); | 66 Color color(value); |
67 return color.isValid() && !color.hasAlpha(); | 67 return color.isValid() && !color.hasAlpha(); |
68 } | 68 } |
69 | 69 |
70 PassRefPtr<InputType> ColorInputType::create(HTMLInputElement* element) | 70 PassRefPtr<InputType> ColorInputType::create(HTMLInputElement& element) |
71 { | 71 { |
72 return adoptRef(new ColorInputType(element)); | 72 return adoptRef(new ColorInputType(element)); |
73 } | 73 } |
74 | 74 |
75 ColorInputType::~ColorInputType() | 75 ColorInputType::~ColorInputType() |
76 { | 76 { |
77 endColorChooser(); | 77 endColorChooser(); |
78 } | 78 } |
79 | 79 |
80 void ColorInputType::countUsage() | 80 void ColorInputType::countUsage() |
(...skipping 24 matching lines...) Expand all Loading... |
105 String ColorInputType::sanitizeValue(const String& proposedValue) const | 105 String ColorInputType::sanitizeValue(const String& proposedValue) const |
106 { | 106 { |
107 if (!isValidColorString(proposedValue)) | 107 if (!isValidColorString(proposedValue)) |
108 return fallbackValue(); | 108 return fallbackValue(); |
109 | 109 |
110 return proposedValue.lower(); | 110 return proposedValue.lower(); |
111 } | 111 } |
112 | 112 |
113 Color ColorInputType::valueAsColor() const | 113 Color ColorInputType::valueAsColor() const |
114 { | 114 { |
115 return Color(element()->value()); | 115 return Color(element().value()); |
116 } | 116 } |
117 | 117 |
118 void ColorInputType::createShadowSubtree() | 118 void ColorInputType::createShadowSubtree() |
119 { | 119 { |
120 ASSERT(element()->shadow()); | 120 ASSERT(element().shadow()); |
121 | 121 |
122 Document& document = element()->document(); | 122 Document& document = element().document(); |
123 RefPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document); | 123 RefPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document); |
124 wrapperElement->setPart(AtomicString("-webkit-color-swatch-wrapper", AtomicS
tring::ConstructFromLiteral)); | 124 wrapperElement->setPart(AtomicString("-webkit-color-swatch-wrapper", AtomicS
tring::ConstructFromLiteral)); |
125 RefPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document); | 125 RefPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document); |
126 colorSwatch->setPart(AtomicString("-webkit-color-swatch", AtomicString::Cons
tructFromLiteral)); | 126 colorSwatch->setPart(AtomicString("-webkit-color-swatch", AtomicString::Cons
tructFromLiteral)); |
127 wrapperElement->appendChild(colorSwatch.release()); | 127 wrapperElement->appendChild(colorSwatch.release()); |
128 element()->userAgentShadowRoot()->appendChild(wrapperElement.release()); | 128 element().userAgentShadowRoot()->appendChild(wrapperElement.release()); |
129 | 129 |
130 updateColorSwatch(); | 130 updateColorSwatch(); |
131 } | 131 } |
132 | 132 |
133 void ColorInputType::setValue(const String& value, bool valueChanged, TextFieldE
ventBehavior eventBehavior) | 133 void ColorInputType::setValue(const String& value, bool valueChanged, TextFieldE
ventBehavior eventBehavior) |
134 { | 134 { |
135 InputType::setValue(value, valueChanged, eventBehavior); | 135 InputType::setValue(value, valueChanged, eventBehavior); |
136 | 136 |
137 if (!valueChanged) | 137 if (!valueChanged) |
138 return; | 138 return; |
139 | 139 |
140 updateColorSwatch(); | 140 updateColorSwatch(); |
141 if (m_chooser) | 141 if (m_chooser) |
142 m_chooser->setSelectedColor(valueAsColor()); | 142 m_chooser->setSelectedColor(valueAsColor()); |
143 } | 143 } |
144 | 144 |
145 void ColorInputType::handleDOMActivateEvent(Event* event) | 145 void ColorInputType::handleDOMActivateEvent(Event* event) |
146 { | 146 { |
147 if (element()->isDisabledFormControl() || !element()->renderer()) | 147 if (element().isDisabledFormControl() || !element().renderer()) |
148 return; | 148 return; |
149 | 149 |
150 if (!UserGestureIndicator::processingUserGesture()) | 150 if (!UserGestureIndicator::processingUserGesture()) |
151 return; | 151 return; |
152 | 152 |
153 Chrome* chrome = this->chrome(); | 153 Chrome* chrome = this->chrome(); |
154 if (chrome && !m_chooser) | 154 if (chrome && !m_chooser) |
155 m_chooser = chrome->createColorChooser(this, valueAsColor()); | 155 m_chooser = chrome->createColorChooser(this, valueAsColor()); |
156 | 156 |
157 event->setDefaultHandled(); | 157 event->setDefaultHandled(); |
158 } | 158 } |
159 | 159 |
160 void ColorInputType::detach() | 160 void ColorInputType::detach() |
161 { | 161 { |
162 endColorChooser(); | 162 endColorChooser(); |
163 } | 163 } |
164 | 164 |
165 bool ColorInputType::shouldRespectListAttribute() | 165 bool ColorInputType::shouldRespectListAttribute() |
166 { | 166 { |
167 return InputType::themeSupportsDataListUI(this); | 167 return InputType::themeSupportsDataListUI(this); |
168 } | 168 } |
169 | 169 |
170 bool ColorInputType::typeMismatchFor(const String& value) const | 170 bool ColorInputType::typeMismatchFor(const String& value) const |
171 { | 171 { |
172 return !isValidColorString(value); | 172 return !isValidColorString(value); |
173 } | 173 } |
174 | 174 |
175 void ColorInputType::didChooseColor(const Color& color) | 175 void ColorInputType::didChooseColor(const Color& color) |
176 { | 176 { |
177 if (element()->isDisabledFormControl() || color == valueAsColor()) | 177 if (element().isDisabledFormControl() || color == valueAsColor()) |
178 return; | 178 return; |
179 element()->setValueFromRenderer(color.serialized()); | 179 element().setValueFromRenderer(color.serialized()); |
180 updateColorSwatch(); | 180 updateColorSwatch(); |
181 element()->dispatchFormControlChangeEvent(); | 181 element().dispatchFormControlChangeEvent(); |
182 } | 182 } |
183 | 183 |
184 void ColorInputType::didEndChooser() | 184 void ColorInputType::didEndChooser() |
185 { | 185 { |
186 m_chooser.clear(); | 186 m_chooser.clear(); |
187 } | 187 } |
188 | 188 |
189 void ColorInputType::endColorChooser() | 189 void ColorInputType::endColorChooser() |
190 { | 190 { |
191 if (m_chooser) | 191 if (m_chooser) |
192 m_chooser->endChooser(); | 192 m_chooser->endChooser(); |
193 } | 193 } |
194 | 194 |
195 void ColorInputType::updateColorSwatch() | 195 void ColorInputType::updateColorSwatch() |
196 { | 196 { |
197 HTMLElement* colorSwatch = shadowColorSwatch(); | 197 HTMLElement* colorSwatch = shadowColorSwatch(); |
198 if (!colorSwatch) | 198 if (!colorSwatch) |
199 return; | 199 return; |
200 | 200 |
201 colorSwatch->setInlineStyleProperty(CSSPropertyBackgroundColor, element()->v
alue()); | 201 colorSwatch->setInlineStyleProperty(CSSPropertyBackgroundColor, element().va
lue()); |
202 } | 202 } |
203 | 203 |
204 HTMLElement* ColorInputType::shadowColorSwatch() const | 204 HTMLElement* ColorInputType::shadowColorSwatch() const |
205 { | 205 { |
206 ShadowRoot* shadow = element()->userAgentShadowRoot(); | 206 ShadowRoot* shadow = element().userAgentShadowRoot(); |
207 return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0; | 207 return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0; |
208 } | 208 } |
209 | 209 |
210 IntRect ColorInputType::elementRectRelativeToRootView() const | 210 IntRect ColorInputType::elementRectRelativeToRootView() const |
211 { | 211 { |
212 return element()->document().view()->contentsToRootView(element()->pixelSnap
pedBoundingBox()); | 212 return element().document().view()->contentsToRootView(element().pixelSnappe
dBoundingBox()); |
213 } | 213 } |
214 | 214 |
215 Color ColorInputType::currentColor() | 215 Color ColorInputType::currentColor() |
216 { | 216 { |
217 return valueAsColor(); | 217 return valueAsColor(); |
218 } | 218 } |
219 | 219 |
220 bool ColorInputType::shouldShowSuggestions() const | 220 bool ColorInputType::shouldShowSuggestions() const |
221 { | 221 { |
222 if (RuntimeEnabledFeatures::dataListElementEnabled()) | 222 if (RuntimeEnabledFeatures::dataListElementEnabled()) |
223 return element()->fastHasAttribute(listAttr); | 223 return element().fastHasAttribute(listAttr); |
224 | 224 |
225 return false; | 225 return false; |
226 } | 226 } |
227 | 227 |
228 Vector<Color> ColorInputType::suggestions() const | 228 Vector<Color> ColorInputType::suggestions() const |
229 { | 229 { |
230 Vector<Color> suggestions; | 230 Vector<Color> suggestions; |
231 if (RuntimeEnabledFeatures::dataListElementEnabled()) { | 231 if (RuntimeEnabledFeatures::dataListElementEnabled()) { |
232 HTMLDataListElement* dataList = element()->dataList(); | 232 HTMLDataListElement* dataList = element().dataList(); |
233 if (dataList) { | 233 if (dataList) { |
234 RefPtr<HTMLCollection> options = dataList->options(); | 234 RefPtr<HTMLCollection> options = dataList->options(); |
235 for (unsigned i = 0; HTMLOptionElement* option = toHTMLOptionElement
(options->item(i)); i++) { | 235 for (unsigned i = 0; HTMLOptionElement* option = toHTMLOptionElement
(options->item(i)); i++) { |
236 if (!element()->isValidValue(option->value())) | 236 if (!element().isValidValue(option->value())) |
237 continue; | 237 continue; |
238 Color color(option->value()); | 238 Color color(option->value()); |
239 if (!color.isValid()) | 239 if (!color.isValid()) |
240 continue; | 240 continue; |
241 suggestions.append(color); | 241 suggestions.append(color); |
242 } | 242 } |
243 } | 243 } |
244 } | 244 } |
245 return suggestions; | 245 return suggestions; |
246 } | 246 } |
247 | 247 |
248 } // namespace WebCore | 248 } // namespace WebCore |
OLD | NEW |