1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #include "RangeInputType.h"
34
35 #include "HTMLInputElement.h"
36 #include "HTMLNames.h"
37 #include "HTMLParserIdioms.h"
38 #include "KeyboardEvent.h"
39 #include "MouseEvent.h"
40 #include "PlatformMouseEvent.h"
41 #include "RenderSlider.h"
42 #include "ShadowRoot.h"
43 #include "SliderThumbElement.h"
44 #include "StepRange.h"
45 #include <limits>
46 #include <wtf/MathExtras.h>
47 #include <wtf/PassOwnPtr.h>
48
49 namespace WebCore {
50
51 using namespace HTMLNames;
52 using namespace std;
53
54 static const double rangeDefaultMinimum = 0.0;
55 static const double rangeDefaultMaximum = 100.0;
56 static const double rangeDefaultStep = 1.0;
57 static const double rangeStepScaleFactor = 1.0;
58
create(HTMLInputElement * element)59 PassOwnPtr<InputType> RangeInputType::create(HTMLInputElement* element)
60 {
61 return adoptPtr(new RangeInputType(element));
62 }
63
isRangeControl() const64 bool RangeInputType::isRangeControl() const
65 {
66 return true;
67 }
68
formControlType() const69 const AtomicString& RangeInputType::formControlType() const
70 {
71 return InputTypeNames::range();
72 }
73
valueAsNumber() const74 double RangeInputType::valueAsNumber() const
75 {
76 return parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
77 }
78
setValueAsNumber(double newValue,ExceptionCode &) const79 void RangeInputType::setValueAsNumber(double newValue, ExceptionCode&) const
80 {
81 element()->setValue(serialize(newValue));
82 }
83
supportsRequired() const84 bool RangeInputType::supportsRequired() const
85 {
86 return false;
87 }
88
rangeUnderflow(const String & value) const89 bool RangeInputType::rangeUnderflow(const String& value) const
90 {
91 // Guaranteed by sanitization.
92 ASSERT_UNUSED(value, parseToDouble(value, numeric_limits<double>::quiet_NaN()) >= minimum());
93 return false;
94 }
95
rangeOverflow(const String & value) const96 bool RangeInputType::rangeOverflow(const String& value) const
97 {
98 // Guaranteed by sanitization.
99 ASSERT_UNUSED(value, parseToDouble(value, numeric_limits<double>::quiet_NaN()) <= maximum());
100 return false;
101 }
102
supportsRangeLimitation() const103 bool RangeInputType::supportsRangeLimitation() const
104 {
105 return true;
106 }
107
minimum() const108 double RangeInputType::minimum() const
109 {
110 return parseToDouble(element()->fastGetAttribute(minAttr), rangeDefaultMinimum);
111 }
112
maximum() const113 double RangeInputType::maximum() const
114 {
115 double max = parseToDouble(element()->fastGetAttribute(maxAttr), rangeDefaultMaximum);
116 // A remedy for the inconsistent min/max values.
117 // Sets the maximum to the default or the minimum value.
118 double min = minimum();
119 if (max < min)
120 max = std::max(min, rangeDefaultMaximum);
121 return max;
122 }
123
isSteppable() const124 bool RangeInputType::isSteppable() const
125 {
126 return true;
127 }
128
stepMismatch(const String &,double) const129 bool RangeInputType::stepMismatch(const String&, double) const
130 {
131 // stepMismatch doesn't occur for type=range. RenderSlider guarantees the
132 // value matches to step on user input, and sanitization takes care
133 // of the general case.
134 return false;
135 }
136
stepBase() const137 double RangeInputType::stepBase() const
138 {
139 return minimum();
140 }
141
defaultStep() const142 double RangeInputType::defaultStep() const
143 {
144 return rangeDefaultStep;
145 }
146
stepScaleFactor() const147 double RangeInputType::stepScaleFactor() const
148 {
149 return rangeStepScaleFactor;
150 }
151
handleMouseDownEvent(MouseEvent * event)152 void RangeInputType::handleMouseDownEvent(MouseEvent* event)
153 {
154 if (event->button() != LeftButton || event->target() != element())
155 return;
156
157 if (SliderThumbElement* thumb = shadowSliderThumb())
158 thumb->dragFrom(event->absoluteLocation());
159 }
160
handleKeydownEvent(KeyboardEvent * event)161 void RangeInputType::handleKeydownEvent(KeyboardEvent* event)
162 {
163 if (element()->disabled() || element()->readOnly())
164 return;
165 const String& key = event->keyIdentifier();
166 if (key != "Up" && key != "Right" && key != "Down" && key != "Left")
167 return;
168
169 ExceptionCode ec;
170 if (equalIgnoringCase(element()->fastGetAttribute(stepAttr), "any")) {
171 double min = minimum();
172 double max = maximum();
173 // FIXME: We can't use stepUp() for the step value "any". So, we increase
174 // or decrease the value by 1/100 of the value range. Is it reasonable?
175 double step = (max - min) / 100;
176 double current = parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
177 ASSERT(isfinite(current));
178 // Stepping-up and -down for step="any" are special cases for type="range" from renderer for convenient.
179 // No stepping normally for step="any". They cannot be handled by stepUp()/stepDown()/stepUpFromRenderer().
180 // So calculating values stepped-up or -down here.
181 double newValue;
182 if (key == "Up" || key == "Right") {
183 newValue = current + step;
184 if (newValue > max)
185 newValue = max;
186 } else {
187 newValue = current - step;
188 if (newValue < min)
189 newValue = min;
190 }
191 if (newValue != current) {
192 setValueAsNumber(newValue, ec);
193 element()->dispatchFormControlChangeEvent();
194 }
195 } else {
196 int stepMagnification = (key == "Up" || key == "Right") ? 1 : -1;
197 // Reasonable stepping-up/-down by stepUpFromRenderer() unless step="any"
198 element()->stepUpFromRenderer(stepMagnification);
199 }
200 event->setDefaultHandled();
201 }
202
createShadowSubtree()203 void RangeInputType::createShadowSubtree()
204 {
205 ExceptionCode ec = 0;
206 element()->ensureShadowRoot()->appendChild(SliderThumbElement::create(element()->document()), ec);
207 }
208
createRenderer(RenderArena * arena,RenderStyle *) const209 RenderObject* RangeInputType::createRenderer(RenderArena* arena, RenderStyle*) const
210 {
211 return new (arena) RenderSlider(element());
212 }
213
parseToDouble(const String & src,double defaultValue) const214 double RangeInputType::parseToDouble(const String& src, double defaultValue) const
215 {
216 double numberValue;
217 if (!parseToDoubleForNumberType(src, &numberValue))
218 return defaultValue;
219 ASSERT(isfinite(numberValue));
220 return numberValue;
221 }
222
serialize(double value) const223 String RangeInputType::serialize(double value) const
224 {
225 if (!isfinite(value))
226 return String();
227 return serializeForNumberType(value);
228 }
229
230 // FIXME: Could share this with BaseButtonInputType and BaseCheckableInputType if we had a common base class.
accessKeyAction(bool sendToAnyElement)231 void RangeInputType::accessKeyAction(bool sendToAnyElement)
232 {
233 InputType::accessKeyAction(sendToAnyElement);
234
235 // Send mouse button events if the caller specified sendToAnyElement.
236 // FIXME: The comment above is no good. It says what we do, but not why.
237 element()->dispatchSimulatedClick(0, sendToAnyElement);
238 }
239
minOrMaxAttributeChanged()240 void RangeInputType::minOrMaxAttributeChanged()
241 {
242 InputType::minOrMaxAttributeChanged();
243
244 // Sanitize the value.
245 element()->setValue(element()->value());
246 element()->setNeedsStyleRecalc();
247 }
248
valueChanged()249 void RangeInputType::valueChanged()
250 {
251 shadowSliderThumb()->setPositionFromValue();
252 }
253
fallbackValue()254 String RangeInputType::fallbackValue()
255 {
256 return serializeForNumberType(StepRange(element()).defaultValue());
257 }
258
sanitizeValue(const String & proposedValue)259 String RangeInputType::sanitizeValue(const String& proposedValue)
260 {
261 // If the proposedValue is null than this is a reset scenario and we
262 // want the range input's value attribute to take priority over the
263 // calculated default (middle) value.
264 if (proposedValue.isNull())
265 return proposedValue;
266
267 return serializeForNumberType(StepRange(element()).clampValue(proposedValue));
268 }
269
shouldRespectListAttribute()270 bool RangeInputType::shouldRespectListAttribute()
271 {
272 return true;
273 }
274
shadowSliderThumb() const275 SliderThumbElement* RangeInputType::shadowSliderThumb() const
276 {
277 Node* shadow = element()->shadowRoot();
278 return shadow ? toSliderThumbElement(shadow->firstChild()) : 0;
279 }
280
281 } // namespace WebCore
282