initial import
[vuplus_webkit] / Source / WebCore / html / BaseDateAndTimeInputType.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "BaseDateAndTimeInputType.h"
33
34 #include "HTMLInputElement.h"
35 #include "HTMLNames.h"
36 #include "KeyboardEvent.h"
37 #include "LocalizedDate.h"
38 #include <limits>
39 #include <wtf/CurrentTime.h>
40 #include <wtf/DateMath.h>
41 #include <wtf/MathExtras.h>
42 #include <wtf/PassOwnPtr.h>
43 #include <wtf/text/WTFString.h>
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48 using namespace std;
49
50 static const double msecPerMinute = 60 * 1000;
51 static const double msecPerSecond = 1000;
52
53 double BaseDateAndTimeInputType::valueAsDate() const
54 {
55     return parseToDouble(element()->value(), DateComponents::invalidMilliseconds());
56 }
57
58 void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionCode&) const
59 {
60     element()->setValue(serializeWithMilliseconds(value));
61 }
62
63 double BaseDateAndTimeInputType::valueAsNumber() const
64 {
65     return parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
66 }
67
68 void BaseDateAndTimeInputType::setValueAsNumber(double newValue, bool sendChangeEvent, ExceptionCode&) const
69 {
70     element()->setValue(serialize(newValue), sendChangeEvent);
71 }
72
73 bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
74 {
75     return !value.isEmpty() && !parseToDateComponents(value, 0);
76 }
77
78 bool BaseDateAndTimeInputType::typeMismatch() const
79 {
80     return typeMismatchFor(element()->value());
81 }
82
83 bool BaseDateAndTimeInputType::rangeUnderflow(const String& value) const
84 {
85     const double nan = numeric_limits<double>::quiet_NaN();
86     double doubleValue = parseToDouble(value, nan);
87     return isfinite(doubleValue) && doubleValue < minimum();
88 }
89
90 bool BaseDateAndTimeInputType::rangeOverflow(const String& value) const
91 {
92     const double nan = numeric_limits<double>::quiet_NaN();
93     double doubleValue = parseToDouble(value, nan);
94     return isfinite(doubleValue) && doubleValue > maximum();
95 }
96
97 bool BaseDateAndTimeInputType::supportsRangeLimitation() const
98 {
99     return true;
100 }
101
102 double BaseDateAndTimeInputType::defaultValueForStepUp() const
103 {
104     double ms = currentTimeMS();
105     double utcOffset = calculateUTCOffset();
106     double dstOffset = calculateDSTOffset(ms, utcOffset);
107     int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
108     return ms + (offset * msPerMinute);
109 }
110
111 bool BaseDateAndTimeInputType::isSteppable() const
112 {
113     return true;
114 }
115
116 bool BaseDateAndTimeInputType::stepMismatch(const String& value, double step) const
117 {
118     const double nan = numeric_limits<double>::quiet_NaN();
119     double doubleValue = parseToDouble(value, nan);
120     doubleValue = fabs(doubleValue - stepBase());
121     if (!isfinite(doubleValue))
122         return false;
123     ASSERT(round(doubleValue) == doubleValue);
124     ASSERT(round(step) == step);
125     return fmod(doubleValue, step);
126 }
127
128 double BaseDateAndTimeInputType::stepBase() const
129 {
130     return parseToDouble(element()->fastGetAttribute(minAttr), defaultStepBase());
131 }
132
133 void BaseDateAndTimeInputType::handleKeydownEvent(KeyboardEvent* event)
134 {
135     handleKeydownEventForSpinButton(event);
136     if (!event->defaultHandled())
137         TextFieldInputType::handleKeydownEvent(event);
138 }
139
140 void BaseDateAndTimeInputType::handleWheelEvent(WheelEvent* event)
141 {
142     handleWheelEventForSpinButton(event);
143 }
144
145 double BaseDateAndTimeInputType::parseToDouble(const String& src, double defaultValue) const
146 {
147     DateComponents date;
148     if (!parseToDateComponents(src, &date))
149         return defaultValue;
150     double msec = date.millisecondsSinceEpoch();
151     ASSERT(isfinite(msec));
152     return msec;
153 }
154
155 bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
156 {
157     if (source.isEmpty())
158         return false;
159     DateComponents ignoredResult;
160     if (!out)
161         out = &ignoredResult;
162     return parseToDateComponentsInternal(source.characters(), source.length(), out);
163 }
164
165 String BaseDateAndTimeInputType::serialize(double value) const
166 {
167     if (!isfinite(value))
168         return String();
169     DateComponents date;
170     if (!setMillisecondToDateComponents(value, &date))
171         return String();
172     return serializeWithComponents(date);
173 }
174
175 String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
176 {
177     double step;
178     if (!element()->getAllowedValueStep(&step))
179         return date.toString();
180     if (!fmod(step, msecPerMinute))
181         return date.toString(DateComponents::None);
182     if (!fmod(step, msecPerSecond))
183         return date.toString(DateComponents::Second);
184     return date.toString(DateComponents::Millisecond);
185 }
186
187 String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
188 {
189     return serialize(value);
190 }
191
192 String BaseDateAndTimeInputType::visibleValue() const
193 {
194     String currentValue = element()->value();
195     DateComponents date;
196     if (!parseToDateComponents(currentValue, &date))
197         return currentValue;
198
199     String localized = formatLocalizedDate(date);
200     return localized.isEmpty() ? currentValue : localized;
201 }
202
203 String BaseDateAndTimeInputType::convertFromVisibleValue(const String& visibleValue) const
204 {
205     if (visibleValue.isEmpty())
206         return visibleValue;
207
208     double parsedValue = parseLocalizedDate(visibleValue, dateType());
209     if (!isfinite(parsedValue))
210         return visibleValue;
211
212     return serializeWithMilliseconds(parsedValue);
213 }
214
215 } // namespace WebCore