Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / 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 "core/html/forms/BaseDateAndTimeInputType.h"
33
34 #include <limits>
35 #include "core/html/HTMLInputElement.h"
36 #include "platform/text/PlatformLocale.h"
37 #include "wtf/CurrentTime.h"
38 #include "wtf/DateMath.h"
39 #include "wtf/MathExtras.h"
40 #include "wtf/text/WTFString.h"
41
42 namespace WebCore {
43
44 using blink::WebLocalizedString;
45 using namespace HTMLNames;
46 using namespace std;
47
48 static const int msecPerMinute = 60 * 1000;
49 static const int msecPerSecond = 1000;
50
51 double BaseDateAndTimeInputType::valueAsDate() const
52 {
53     return valueAsDouble();
54 }
55
56 void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionState&) const
57 {
58     element().setValue(serializeWithMilliseconds(value));
59 }
60
61 double BaseDateAndTimeInputType::valueAsDouble() const
62 {
63     const Decimal value = parseToNumber(element().value(), Decimal::nan());
64     return value.isFinite() ? value.toDouble() : DateComponents::invalidMilliseconds();
65 }
66
67 void BaseDateAndTimeInputType::setValueAsDouble(double newValue, TextFieldEventBehavior eventBehavior, ExceptionState& exceptionState) const
68 {
69     setValueAsDecimal(Decimal::fromDouble(newValue), eventBehavior, exceptionState);
70 }
71
72 bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
73 {
74     return !value.isEmpty() && !parseToDateComponents(value, 0);
75 }
76
77 bool BaseDateAndTimeInputType::typeMismatch() const
78 {
79     return typeMismatchFor(element().value());
80 }
81
82 String BaseDateAndTimeInputType::rangeOverflowText(const Decimal& maximum) const
83 {
84     return locale().queryString(WebLocalizedString::ValidationRangeOverflowDateTime, localizeValue(serialize(maximum)));
85 }
86
87 String BaseDateAndTimeInputType::rangeUnderflowText(const Decimal& minimum) const
88 {
89     return locale().queryString(WebLocalizedString::ValidationRangeUnderflowDateTime, localizeValue(serialize(minimum)));
90 }
91
92 Decimal BaseDateAndTimeInputType::defaultValueForStepUp() const
93 {
94     double ms = currentTimeMS();
95     double utcOffset = calculateUTCOffset();
96     double dstOffset = calculateDSTOffset(ms, utcOffset);
97     int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
98     return Decimal::fromDouble(ms + (offset * msPerMinute));
99 }
100
101 bool BaseDateAndTimeInputType::isSteppable() const
102 {
103     return true;
104 }
105
106 Decimal BaseDateAndTimeInputType::parseToNumber(const String& source, const Decimal& defaultValue) const
107 {
108     DateComponents date;
109     if (!parseToDateComponents(source, &date))
110         return defaultValue;
111     double msec = date.millisecondsSinceEpoch();
112     ASSERT(std::isfinite(msec));
113     return Decimal::fromDouble(msec);
114 }
115
116 bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
117 {
118     if (source.isEmpty())
119         return false;
120     DateComponents ignoredResult;
121     if (!out)
122         out = &ignoredResult;
123     return parseToDateComponentsInternal(source, out);
124 }
125
126 String BaseDateAndTimeInputType::serialize(const Decimal& value) const
127 {
128     if (!value.isFinite())
129         return String();
130     DateComponents date;
131     if (!setMillisecondToDateComponents(value.toDouble(), &date))
132         return String();
133     return serializeWithComponents(date);
134 }
135
136 String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
137 {
138     Decimal step;
139     if (!element().getAllowedValueStep(&step))
140         return date.toString();
141     if (step.remainder(msecPerMinute).isZero())
142         return date.toString(DateComponents::None);
143     if (step.remainder(msecPerSecond).isZero())
144         return date.toString(DateComponents::Second);
145     return date.toString(DateComponents::Millisecond);
146 }
147
148 String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
149 {
150     return serialize(Decimal::fromDouble(value));
151 }
152
153 String BaseDateAndTimeInputType::localizeValue(const String& proposedValue) const
154 {
155     DateComponents date;
156     if (!parseToDateComponents(proposedValue, &date))
157         return proposedValue;
158
159     String localized = element().locale().formatDateTime(date);
160     return localized.isEmpty() ? proposedValue : localized;
161 }
162
163 String BaseDateAndTimeInputType::visibleValue() const
164 {
165     return localizeValue(element().value());
166 }
167
168 String BaseDateAndTimeInputType::sanitizeValue(const String& proposedValue) const
169 {
170     return typeMismatchFor(proposedValue) ? emptyString() : proposedValue;
171 }
172
173 bool BaseDateAndTimeInputType::supportsReadOnly() const
174 {
175     return true;
176 }
177
178 bool BaseDateAndTimeInputType::shouldRespectListAttribute()
179 {
180     return InputType::themeSupportsDataListUI(this);
181 }
182
183 bool BaseDateAndTimeInputType::valueMissing(const String& value) const
184 {
185     return element().isRequired() && value.isEmpty();
186 }
187
188 bool BaseDateAndTimeInputType::shouldShowFocusRingOnMouseFocus() const
189 {
190     return true;
191 }
192
193 } // namespace WebCore