- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / NumberInputType.cpp
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 "core/html/forms/NumberInputType.h"
34
35 #include <limits>
36 #include "HTMLNames.h"
37 #include "bindings/v8/ExceptionState.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/events/KeyboardEvent.h"
40 #include "core/html/HTMLInputElement.h"
41 #include "core/html/forms/InputTypeNames.h"
42 #include "core/html/parser/HTMLParserIdioms.h"
43 #include "core/rendering/RenderTextControl.h"
44 #include "platform/text/PlatformLocale.h"
45 #include "wtf/MathExtras.h"
46 #include "wtf/PassOwnPtr.h"
47
48 namespace WebCore {
49
50 using WebKit::WebLocalizedString;
51 using namespace HTMLNames;
52 using namespace std;
53
54 static const int numberDefaultStep = 1;
55 static const int numberDefaultStepBase = 0;
56 static const int numberStepScaleFactor = 1;
57
58 struct RealNumberRenderSize {
59     unsigned sizeBeforeDecimalPoint;
60     unsigned sizeAfteDecimalPoint;
61
62     RealNumberRenderSize(unsigned before, unsigned after)
63         : sizeBeforeDecimalPoint(before)
64         , sizeAfteDecimalPoint(after)
65     {
66     }
67
68     RealNumberRenderSize max(const RealNumberRenderSize& other) const
69     {
70         return RealNumberRenderSize(
71             std::max(sizeBeforeDecimalPoint, other.sizeBeforeDecimalPoint),
72             std::max(sizeAfteDecimalPoint, other.sizeAfteDecimalPoint));
73     }
74 };
75
76 static RealNumberRenderSize calculateRenderSize(const Decimal& value)
77 {
78     ASSERT(value.isFinite());
79     const unsigned sizeOfDigits = String::number(value.value().coefficient()).length();
80     const unsigned sizeOfSign = value.isNegative() ? 1 : 0;
81     const int exponent = value.exponent();
82     if (exponent >= 0)
83         return RealNumberRenderSize(sizeOfSign + sizeOfDigits, 0);
84
85     const int sizeBeforeDecimalPoint = exponent + sizeOfDigits;
86     if (sizeBeforeDecimalPoint > 0) {
87         // In case of "123.456"
88         return RealNumberRenderSize(sizeOfSign + sizeBeforeDecimalPoint, sizeOfDigits - sizeBeforeDecimalPoint);
89     }
90
91     // In case of "0.00012345"
92     const unsigned sizeOfZero = 1;
93     const unsigned numberOfZeroAfterDecimalPoint = -sizeBeforeDecimalPoint;
94     return RealNumberRenderSize(sizeOfSign + sizeOfZero , numberOfZeroAfterDecimalPoint + sizeOfDigits);
95 }
96
97 PassRefPtr<InputType> NumberInputType::create(HTMLInputElement& element)
98 {
99     return adoptRef(new NumberInputType(element));
100 }
101
102 void NumberInputType::countUsage()
103 {
104     countUsageIfVisible(UseCounter::InputTypeNumber);
105 }
106
107 const AtomicString& NumberInputType::formControlType() const
108 {
109     return InputTypeNames::number();
110 }
111
112 void NumberInputType::setValue(const String& sanitizedValue, bool valueChanged, TextFieldEventBehavior eventBehavior)
113 {
114     if (!valueChanged && sanitizedValue.isEmpty() && !element().innerTextValue().isEmpty())
115         updateInnerTextValue();
116     TextFieldInputType::setValue(sanitizedValue, valueChanged, eventBehavior);
117 }
118
119 double NumberInputType::valueAsDouble() const
120 {
121     return parseToDoubleForNumberType(element().value());
122 }
123
124 void NumberInputType::setValueAsDouble(double newValue, TextFieldEventBehavior eventBehavior, ExceptionState& es) const
125 {
126     // FIXME: We should use numeric_limits<double>::max for number input type.
127     const double floatMax = numeric_limits<float>::max();
128     if (newValue < -floatMax) {
129         es.throwUninformativeAndGenericDOMException(InvalidStateError);
130         return;
131     }
132     if (newValue > floatMax) {
133         es.throwUninformativeAndGenericDOMException(InvalidStateError);
134         return;
135     }
136     element().setValue(serializeForNumberType(newValue), eventBehavior);
137 }
138
139 void NumberInputType::setValueAsDecimal(const Decimal& newValue, TextFieldEventBehavior eventBehavior, ExceptionState& es) const
140 {
141     // FIXME: We should use numeric_limits<double>::max for number input type.
142     const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max());
143     if (newValue < -floatMax) {
144         es.throwUninformativeAndGenericDOMException(InvalidStateError);
145         return;
146     }
147     if (newValue > floatMax) {
148         es.throwUninformativeAndGenericDOMException(InvalidStateError);
149         return;
150     }
151     element().setValue(serializeForNumberType(newValue), eventBehavior);
152 }
153
154 bool NumberInputType::typeMismatchFor(const String& value) const
155 {
156     return !value.isEmpty() && !std::isfinite(parseToDoubleForNumberType(value));
157 }
158
159 bool NumberInputType::typeMismatch() const
160 {
161     ASSERT(!typeMismatchFor(element().value()));
162     return false;
163 }
164
165 StepRange NumberInputType::createStepRange(AnyStepHandling anyStepHandling) const
166 {
167     DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (numberDefaultStep, numberDefaultStepBase, numberStepScaleFactor));
168     const Decimal stepBase = parseToDecimalForNumberType(element().fastGetAttribute(minAttr), numberDefaultStepBase);
169     // FIXME: We should use numeric_limits<double>::max for number input type.
170     const Decimal floatMax = Decimal::fromDouble(numeric_limits<float>::max());
171     const Decimal minimum = parseToNumber(element().fastGetAttribute(minAttr), -floatMax);
172     const Decimal maximum = parseToNumber(element().fastGetAttribute(maxAttr), floatMax);
173     const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element().fastGetAttribute(stepAttr));
174     return StepRange(stepBase, minimum, maximum, step, stepDescription);
175 }
176
177 bool NumberInputType::sizeShouldIncludeDecoration(int defaultSize, int& preferredSize) const
178 {
179     preferredSize = defaultSize;
180
181     const String stepString = element().fastGetAttribute(stepAttr);
182     if (equalIgnoringCase(stepString, "any"))
183         return false;
184
185     const Decimal minimum = parseToDecimalForNumberType(element().fastGetAttribute(minAttr));
186     if (!minimum.isFinite())
187         return false;
188
189     const Decimal maximum = parseToDecimalForNumberType(element().fastGetAttribute(maxAttr));
190     if (!maximum.isFinite())
191         return false;
192
193     const Decimal step = parseToDecimalForNumberType(stepString, 1);
194     ASSERT(step.isFinite());
195
196     RealNumberRenderSize size = calculateRenderSize(minimum).max(calculateRenderSize(maximum).max(calculateRenderSize(step)));
197
198     preferredSize = size.sizeBeforeDecimalPoint + size.sizeAfteDecimalPoint + (size.sizeAfteDecimalPoint ? 1 : 0);
199
200     return true;
201 }
202
203 bool NumberInputType::isSteppable() const
204 {
205     return true;
206 }
207
208 void NumberInputType::handleKeydownEvent(KeyboardEvent* event)
209 {
210     handleKeydownEventForSpinButton(event);
211     if (!event->defaultHandled())
212         TextFieldInputType::handleKeydownEvent(event);
213 }
214
215 Decimal NumberInputType::parseToNumber(const String& src, const Decimal& defaultValue) const
216 {
217     return parseToDecimalForNumberType(src, defaultValue);
218 }
219
220 String NumberInputType::serialize(const Decimal& value) const
221 {
222     if (!value.isFinite())
223         return String();
224     return serializeForNumberType(value);
225 }
226
227 static bool isE(UChar ch)
228 {
229     return ch == 'e' || ch == 'E';
230 }
231
232 String NumberInputType::localizeValue(const String& proposedValue) const
233 {
234     if (proposedValue.isEmpty())
235         return proposedValue;
236     // We don't localize scientific notations.
237     if (proposedValue.find(isE) != kNotFound)
238         return proposedValue;
239     return element().locale().convertToLocalizedNumber(proposedValue);
240 }
241
242 String NumberInputType::visibleValue() const
243 {
244     return localizeValue(element().value());
245 }
246
247 String NumberInputType::convertFromVisibleValue(const String& visibleValue) const
248 {
249     if (visibleValue.isEmpty())
250         return visibleValue;
251     // We don't localize scientific notations.
252     if (visibleValue.find(isE) != kNotFound)
253         return visibleValue;
254     return element().locale().convertFromLocalizedNumber(visibleValue);
255 }
256
257 String NumberInputType::sanitizeValue(const String& proposedValue) const
258 {
259     if (proposedValue.isEmpty())
260         return proposedValue;
261     return std::isfinite(parseToDoubleForNumberType(proposedValue)) ? proposedValue : emptyString();
262 }
263
264 bool NumberInputType::hasBadInput() const
265 {
266     String standardValue = convertFromVisibleValue(element().innerTextValue());
267     return !standardValue.isEmpty() && !std::isfinite(parseToDoubleForNumberType(standardValue));
268 }
269
270 String NumberInputType::badInputText() const
271 {
272     return locale().queryString(WebLocalizedString::ValidationBadInputForNumber);
273 }
274
275 String NumberInputType::rangeOverflowText(const Decimal& maximum) const
276 {
277     return locale().queryString(WebLocalizedString::ValidationRangeOverflow, localizeValue(serialize(maximum)));
278 }
279
280 String NumberInputType::rangeUnderflowText(const Decimal& minimum) const
281 {
282     return locale().queryString(WebLocalizedString::ValidationRangeUnderflow, localizeValue(serialize(minimum)));
283 }
284
285 bool NumberInputType::shouldRespectSpeechAttribute()
286 {
287     return true;
288 }
289
290 bool NumberInputType::supportsPlaceholder() const
291 {
292     return true;
293 }
294
295 bool NumberInputType::isNumberField() const
296 {
297     return true;
298 }
299
300 void NumberInputType::minOrMaxAttributeChanged()
301 {
302     InputType::minOrMaxAttributeChanged();
303
304     if (element().renderer())
305         element().renderer()->setNeedsLayoutAndPrefWidthsRecalc();
306 }
307
308 void NumberInputType::stepAttributeChanged()
309 {
310     InputType::stepAttributeChanged();
311
312     if (element().renderer())
313         element().renderer()->setNeedsLayoutAndPrefWidthsRecalc();
314 }
315
316 } // namespace WebCore