2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/html/forms/MonthInputType.h"
34 #include "HTMLNames.h"
35 #include "InputTypeNames.h"
36 #include "core/html/HTMLInputElement.h"
37 #include "core/html/forms/DateTimeFieldsState.h"
38 #include "platform/DateComponents.h"
39 #include "platform/text/PlatformLocale.h"
40 #include "wtf/CurrentTime.h"
41 #include "wtf/DateMath.h"
42 #include "wtf/MathExtras.h"
43 #include "wtf/PassOwnPtr.h"
44 #include "wtf/text/WTFString.h"
48 using namespace HTMLNames;
50 static const int monthDefaultStep = 1;
51 static const int monthDefaultStepBase = 0;
52 static const int monthStepScaleFactor = 1;
54 PassRefPtrWillBeRawPtr<InputType> MonthInputType::create(HTMLInputElement& element)
56 return adoptRefWillBeNoop(new MonthInputType(element));
59 void MonthInputType::countUsage()
61 countUsageIfVisible(UseCounter::InputTypeMonth);
64 const AtomicString& MonthInputType::formControlType() const
66 return InputTypeNames::month;
69 double MonthInputType::valueAsDate() const
72 if (!parseToDateComponents(element().value(), &date))
73 return DateComponents::invalidMilliseconds();
74 double msec = date.millisecondsSinceEpoch();
75 ASSERT(std::isfinite(msec));
79 String MonthInputType::serializeWithMilliseconds(double value) const
82 if (!date.setMillisecondsSinceEpochForMonth(value))
84 return serializeWithComponents(date);
87 Decimal MonthInputType::defaultValueForStepUp() const
89 double current = currentTimeMS();
90 double utcOffset = calculateUTCOffset();
91 double dstOffset = calculateDSTOffset(current, utcOffset);
92 int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
93 current += offset * msPerMinute;
96 date.setMillisecondsSinceEpochForMonth(current);
97 double months = date.monthsSinceEpoch();
98 ASSERT(std::isfinite(months));
99 return Decimal::fromDouble(months);
102 StepRange MonthInputType::createStepRange(AnyStepHandling anyStepHandling) const
104 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (monthDefaultStep, monthDefaultStepBase, monthStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
106 return InputType::createStepRange(anyStepHandling, Decimal::fromDouble(monthDefaultStepBase), Decimal::fromDouble(DateComponents::minimumMonth()), Decimal::fromDouble(DateComponents::maximumMonth()), stepDescription);
109 Decimal MonthInputType::parseToNumber(const String& src, const Decimal& defaultValue) const
112 if (!parseToDateComponents(src, &date))
114 double months = date.monthsSinceEpoch();
115 ASSERT(std::isfinite(months));
116 return Decimal::fromDouble(months);
119 bool MonthInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
123 return out->parseMonth(string, 0, end) && end == string.length();
126 bool MonthInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
129 return date->setMonthsSinceEpoch(value);
132 bool MonthInputType::isMonthField() const
137 bool MonthInputType::canSetSuggestedValue()
142 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
143 String MonthInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
145 if (!dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
146 return emptyString();
147 return String::format("%04u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month());
150 void MonthInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
152 layoutParameters.dateTimeFormat = layoutParameters.locale.monthFormat();
153 layoutParameters.fallbackDateTimeFormat = "yyyy-MM";
154 if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
155 layoutParameters.minimum = DateComponents();
156 if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
157 layoutParameters.maximum = DateComponents();
158 layoutParameters.placeholderForMonth = "--";
159 layoutParameters.placeholderForYear = "----";
162 bool MonthInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
164 return hasYear && hasMonth;
167 } // namespace WebCore