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 | #if ENABLE(INPUT_TYPE_MONTH) |
33 | #include "MonthInputType.h" |
34 | |
35 | #include "HTMLInputElement.h" |
36 | #include "HTMLNames.h" |
37 | #include "InputTypeNames.h" |
38 | #include <wtf/DateMath.h> |
39 | #include <wtf/MathExtras.h> |
40 | #include <wtf/NeverDestroyed.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | using namespace HTMLNames; |
45 | |
46 | static const int monthDefaultStep = 1; |
47 | static const int monthDefaultStepBase = 0; |
48 | static const int monthStepScaleFactor = 1; |
49 | static const StepRange::StepDescription monthStepDescription { monthDefaultStep, monthDefaultStepBase, monthStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger }; |
50 | |
51 | const AtomicString& MonthInputType::formControlType() const |
52 | { |
53 | return InputTypeNames::month(); |
54 | } |
55 | |
56 | DateComponents::Type MonthInputType::dateType() const |
57 | { |
58 | return DateComponents::Month; |
59 | } |
60 | |
61 | double MonthInputType::valueAsDate() const |
62 | { |
63 | ASSERT(element()); |
64 | DateComponents date; |
65 | if (!parseToDateComponents(element()->value(), &date)) |
66 | return DateComponents::invalidMilliseconds(); |
67 | double msec = date.millisecondsSinceEpoch(); |
68 | ASSERT(std::isfinite(msec)); |
69 | return msec; |
70 | } |
71 | |
72 | String MonthInputType::serializeWithMilliseconds(double value) const |
73 | { |
74 | DateComponents date; |
75 | if (!date.setMillisecondsSinceEpochForMonth(value)) |
76 | return String(); |
77 | return serializeWithComponents(date); |
78 | } |
79 | |
80 | Decimal MonthInputType::defaultValueForStepUp() const |
81 | { |
82 | double current = WallTime::now().secondsSinceEpoch().milliseconds(); |
83 | int offset = calculateLocalTimeOffset(current).offset / msPerMinute; |
84 | current += offset * msPerMinute; |
85 | |
86 | DateComponents date; |
87 | date.setMillisecondsSinceEpochForMonth(current); |
88 | double months = date.monthsSinceEpoch(); |
89 | ASSERT(std::isfinite(months)); |
90 | return Decimal::fromDouble(months); |
91 | } |
92 | |
93 | StepRange MonthInputType::createStepRange(AnyStepHandling anyStepHandling) const |
94 | { |
95 | ASSERT(element()); |
96 | const Decimal stepBase = parseToNumber(element()->attributeWithoutSynchronization(minAttr), Decimal::fromDouble(monthDefaultStepBase)); |
97 | const Decimal minimum = parseToNumber(element()->attributeWithoutSynchronization(minAttr), Decimal::fromDouble(DateComponents::minimumMonth())); |
98 | const Decimal maximum = parseToNumber(element()->attributeWithoutSynchronization(maxAttr), Decimal::fromDouble(DateComponents::maximumMonth())); |
99 | const Decimal step = StepRange::parseStep(anyStepHandling, monthStepDescription, element()->attributeWithoutSynchronization(stepAttr)); |
100 | return StepRange(stepBase, RangeLimitations::Valid, minimum, maximum, step, monthStepDescription); |
101 | } |
102 | |
103 | Decimal MonthInputType::parseToNumber(const String& src, const Decimal& defaultValue) const |
104 | { |
105 | DateComponents date; |
106 | if (!parseToDateComponents(src, &date)) |
107 | return defaultValue; |
108 | double months = date.monthsSinceEpoch(); |
109 | ASSERT(std::isfinite(months)); |
110 | return Decimal::fromDouble(months); |
111 | } |
112 | |
113 | bool MonthInputType::parseToDateComponentsInternal(const UChar* characters, unsigned length, DateComponents* out) const |
114 | { |
115 | ASSERT(out); |
116 | unsigned end; |
117 | return out->parseMonth(characters, length, 0, end) && end == length; |
118 | } |
119 | |
120 | bool MonthInputType::setMillisecondToDateComponents(double value, DateComponents* date) const |
121 | { |
122 | ASSERT(date); |
123 | return date->setMonthsSinceEpoch(value); |
124 | } |
125 | |
126 | bool MonthInputType::isMonthField() const |
127 | { |
128 | return true; |
129 | } |
130 | |
131 | } // namespace WebCore |
132 | |
133 | #endif |
134 | |