2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "RangeInputType.h"
35 #include "HTMLInputElement.h"
36 #include "HTMLNames.h"
37 #include "HTMLParserIdioms.h"
38 #include "KeyboardEvent.h"
39 #include "RenderSlider.h"
40 #include "StepRange.h"
42 #include <wtf/MathExtras.h>
43 #include <wtf/PassOwnPtr.h>
47 using namespace HTMLNames;
50 static const double rangeDefaultMinimum = 0.0;
51 static const double rangeDefaultMaximum = 100.0;
52 static const double rangeDefaultStep = 1.0;
53 static const double rangeStepScaleFactor = 1.0;
55 PassOwnPtr<InputType> RangeInputType::create(HTMLInputElement* element)
57 return adoptPtr(new RangeInputType(element));
60 bool RangeInputType::isRangeControl() const
65 const AtomicString& RangeInputType::formControlType() const
67 return InputTypeNames::range();
70 double RangeInputType::valueAsNumber() const
72 return parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
75 void RangeInputType::setValueAsNumber(double newValue, ExceptionCode&) const
77 element()->setValue(serialize(newValue));
80 bool RangeInputType::supportsRequired() const
85 bool RangeInputType::rangeUnderflow(const String& value) const
87 // Guaranteed by sanitization.
88 ASSERT_UNUSED(value, parseToDouble(value, numeric_limits<double>::quiet_NaN()) >= minimum());
92 bool RangeInputType::rangeOverflow(const String& value) const
94 // Guaranteed by sanitization.
95 ASSERT_UNUSED(value, parseToDouble(value, numeric_limits<double>::quiet_NaN()) <= maximum());
99 bool RangeInputType::supportsRangeLimitation() const
104 double RangeInputType::minimum() const
106 return parseToDouble(element()->fastGetAttribute(minAttr), rangeDefaultMinimum);
109 double RangeInputType::maximum() const
111 double max = parseToDouble(element()->fastGetAttribute(maxAttr), rangeDefaultMaximum);
112 // A remedy for the inconsistent min/max values.
113 // Sets the maximum to the default or the minimum value.
114 double min = minimum();
116 max = std::max(min, rangeDefaultMaximum);
120 bool RangeInputType::stepMismatch(const String&, double) const
122 // stepMismatch doesn't occur for type=range. RenderSlider guarantees the
123 // value matches to step on user input, and sanitization takes care
124 // of the general case.
128 double RangeInputType::stepBase() const
133 double RangeInputType::defaultStep() const
135 return rangeDefaultStep;
138 double RangeInputType::stepScaleFactor() const
140 return rangeStepScaleFactor;
143 void RangeInputType::handleKeydownEvent(KeyboardEvent* event)
145 const String& key = event->keyIdentifier();
146 if (key != "Up" && key != "Right" && key != "Down" && key != "Left")
150 if (equalIgnoringCase(element()->fastGetAttribute(stepAttr), "any")) {
151 double min = minimum();
152 double max = maximum();
153 // FIXME: We can't use stepUp() for the step value "any". So, we increase
154 // or decrease the value by 1/100 of the value range. Is it reasonable?
155 double step = (max - min) / 100;
156 double current = parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
157 ASSERT(isfinite(current));
158 // Stepping-up and -down for step="any" are special cases for type="range" from renderer for convenient.
159 // No stepping normally for step="any". They cannot be handled by stepUp()/stepDown()/stepUpFromRenderer().
160 // So calculating values stepped-up or -down here.
162 if (key == "Up" || key == "Right") {
163 newValue = current + step;
167 newValue = current - step;
171 if (newValue != current) {
172 setValueAsNumber(newValue, ec);
173 element()->dispatchFormControlChangeEvent();
176 int stepMagnification = (key == "Up" || key == "Right") ? 1 : -1;
177 // Reasonable stepping-up/-down by stepUpFromRenderer() unless step="any"
178 element()->stepUpFromRenderer(stepMagnification);
180 event->setDefaultHandled();
183 void RangeInputType::forwardEvent(Event* event)
185 if (element()->renderer() && (event->isMouseEvent() || event->isDragEvent() || event->isWheelEvent()))
186 toRenderSlider(element()->renderer())->forwardEvent(event);
189 RenderObject* RangeInputType::createRenderer(RenderArena* arena, RenderStyle*) const
191 return new (arena) RenderSlider(element());
194 double RangeInputType::parseToDouble(const String& src, double defaultValue) const
197 if (!parseToDoubleForNumberType(src, &numberValue))
199 ASSERT(isfinite(numberValue));
203 String RangeInputType::serialize(double value) const
205 if (!isfinite(value))
207 return serializeForNumberType(value);
210 // FIXME: Could share this with BaseButtonInputType and BaseCheckableInputType if we had a common base class.
211 void RangeInputType::accessKeyAction(bool sendToAnyElement)
213 InputType::accessKeyAction(sendToAnyElement);
215 // Send mouse button events if the caller specified sendToAnyElement.
216 // FIXME: The comment above is no good. It says what we do, but not why.
217 element()->dispatchSimulatedClick(0, sendToAnyElement);
220 void RangeInputType::minOrMaxAttributeChanged()
222 InputType::minOrMaxAttributeChanged();
224 // Sanitize the value.
225 element()->setValue(element()->value());
226 element()->setNeedsStyleRecalc();
229 String RangeInputType::fallbackValue()
231 return serializeForNumberType(StepRange(element()).defaultValue());
234 String RangeInputType::sanitizeValue(const String& proposedValue)
236 // If the proposedValue is null than this is a reset scenario and we
237 // want the range input's value attribute to take priority over the
238 // calculated default (middle) value.
239 if (proposedValue.isNull())
240 return proposedValue;
242 return serializeForNumberType(StepRange(element()).clampValue(proposedValue));
245 bool RangeInputType::shouldRespectListAttribute()
250 } // namespace WebCore