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/MonthInputType.h"
33
34#include "HTMLNames.h"
35#include "core/html/HTMLInputElement.h"
36#include "core/html/InputTypeNames.h"
37#include "core/platform/DateComponents.h"
38#include "wtf/CurrentTime.h"
39#include "wtf/DateMath.h"
40#include "wtf/MathExtras.h"
41#include "wtf/PassOwnPtr.h"
42
43#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
44#include "core/html/DateTimeFieldsState.h"
45#include "core/platform/text/PlatformLocale.h"
46#include "wtf/text/WTFString.h"
47#endif
48
49namespace WebCore {
50
51using namespace HTMLNames;
52
53static const int monthDefaultStep = 1;
54static const int monthDefaultStepBase = 0;
55static const int monthStepScaleFactor = 1;
56
57PassOwnPtr<InputType> MonthInputType::create(HTMLInputElement* element)
58{
59    return adoptPtr(new MonthInputType(element));
60}
61
62void MonthInputType::attach()
63{
64    observeFeatureIfVisible(UseCounter::InputTypeMonth);
65}
66
67const AtomicString& MonthInputType::formControlType() const
68{
69    return InputTypeNames::month();
70}
71
72DateComponents::Type MonthInputType::dateType() const
73{
74    return DateComponents::Month;
75}
76
77double MonthInputType::valueAsDate() const
78{
79    DateComponents date;
80    if (!parseToDateComponents(element()->value(), &date))
81        return DateComponents::invalidMilliseconds();
82    double msec = date.millisecondsSinceEpoch();
83    ASSERT(std::isfinite(msec));
84    return msec;
85}
86
87String MonthInputType::serializeWithMilliseconds(double value) const
88{
89    DateComponents date;
90    if (!date.setMillisecondsSinceEpochForMonth(value))
91        return String();
92    return serializeWithComponents(date);
93}
94
95Decimal MonthInputType::defaultValueForStepUp() const
96{
97    double current = currentTimeMS();
98    double utcOffset = calculateUTCOffset();
99    double dstOffset = calculateDSTOffset(current, utcOffset);
100    int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
101    current += offset * msPerMinute;
102
103    DateComponents date;
104    date.setMillisecondsSinceEpochForMonth(current);
105    double months = date.monthsSinceEpoch();
106    ASSERT(std::isfinite(months));
107    return Decimal::fromDouble(months);
108}
109
110StepRange MonthInputType::createStepRange(AnyStepHandling anyStepHandling) const
111{
112    DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (monthDefaultStep, monthDefaultStepBase, monthStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
113
114    const Decimal stepBase = parseToNumber(element()->fastGetAttribute(minAttr), Decimal::fromDouble(monthDefaultStepBase));
115    const Decimal minimum = parseToNumber(element()->fastGetAttribute(minAttr), Decimal::fromDouble(DateComponents::minimumMonth()));
116    const Decimal maximum = parseToNumber(element()->fastGetAttribute(maxAttr), Decimal::fromDouble(DateComponents::maximumMonth()));
117    const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element()->fastGetAttribute(stepAttr));
118    return StepRange(stepBase, minimum, maximum, step, stepDescription);
119}
120
121Decimal MonthInputType::parseToNumber(const String& src, const Decimal& defaultValue) const
122{
123    DateComponents date;
124    if (!parseToDateComponents(src, &date))
125        return defaultValue;
126    double months = date.monthsSinceEpoch();
127    ASSERT(std::isfinite(months));
128    return Decimal::fromDouble(months);
129}
130
131bool MonthInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
132{
133    ASSERT(out);
134    unsigned end;
135    return out->parseMonth(string, 0, end) && end == string.length();
136}
137
138bool MonthInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
139{
140    ASSERT(date);
141    return date->setMonthsSinceEpoch(value);
142}
143
144bool MonthInputType::isMonthField() const
145{
146    return true;
147}
148
149#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
150String MonthInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
151{
152    if (!dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
153        return emptyString();
154    return String::format("%04u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month());
155}
156
157void MonthInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
158{
159    layoutParameters.dateTimeFormat = layoutParameters.locale.monthFormat();
160    layoutParameters.fallbackDateTimeFormat = "yyyy-MM";
161    if (!parseToDateComponents(element()->fastGetAttribute(minAttr), &layoutParameters.minimum))
162        layoutParameters.minimum = DateComponents();
163    if (!parseToDateComponents(element()->fastGetAttribute(maxAttr), &layoutParameters.maximum))
164        layoutParameters.maximum = DateComponents();
165    layoutParameters.placeholderForMonth = "--";
166    layoutParameters.placeholderForYear = "----";
167}
168
169bool MonthInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
170{
171    return hasYear && hasMonth;
172}
173#endif
174} // namespace WebCore
175