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/DateInputType.h"
34 #include "core/HTMLNames.h"
35 #include "core/InputTypeNames.h"
36 #include "core/dom/Document.h"
37 #include "core/html/HTMLInputElement.h"
38 #include "core/html/forms/DateTimeFieldsState.h"
39 #include "platform/DateComponents.h"
40 #include "platform/text/PlatformLocale.h"
41 #include "wtf/PassOwnPtr.h"
45 using blink::WebLocalizedString;
46 using namespace HTMLNames;
48 static const int dateDefaultStep = 1;
49 static const int dateDefaultStepBase = 0;
50 static const int dateStepScaleFactor = 86400000;
52 inline DateInputType::DateInputType(HTMLInputElement& element)
53 : BaseDateInputType(element)
57 PassRefPtrWillBeRawPtr<InputType> DateInputType::create(HTMLInputElement& element)
59 return adoptRefWillBeNoop(new DateInputType(element));
62 void DateInputType::countUsage()
64 countUsageIfVisible(UseCounter::InputTypeDate);
67 const AtomicString& DateInputType::formControlType() const
69 return InputTypeNames::date;
72 StepRange DateInputType::createStepRange(AnyStepHandling anyStepHandling) const
74 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateDefaultStep, dateDefaultStepBase, dateStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
76 return InputType::createStepRange(anyStepHandling, dateDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDate()), Decimal::fromDouble(DateComponents::maximumDate()), stepDescription);
79 bool DateInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
83 return out->parseDate(string, 0, end) && end == string.length();
86 bool DateInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
89 return date->setMillisecondsSinceEpochForDate(value);
92 void DateInputType::warnIfValueIsInvalid(const String& value) const
94 if (value != element().sanitizeValue(value)) {
95 element().document().addConsoleMessage(ConsoleMessage::create(RenderingMessageSource, WarningMessageLevel,
96 String::format("The specified value '%s' does not conform to the required format, 'yyyy-MM-dd'.", value.utf8().data())));
100 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
101 String DateInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
103 if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
104 return emptyString();
106 return String::format("%04u-%02u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month(), dateTimeFieldsState.dayOfMonth());
109 void DateInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
111 layoutParameters.dateTimeFormat = layoutParameters.locale.dateFormat();
112 layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd";
113 if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
114 layoutParameters.minimum = DateComponents();
115 if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
116 layoutParameters.maximum = DateComponents();
117 layoutParameters.placeholderForDay = locale().queryString(WebLocalizedString::PlaceholderForDayOfMonthField);
118 layoutParameters.placeholderForMonth = locale().queryString(WebLocalizedString::PlaceholderForMonthField);
119 layoutParameters.placeholderForYear = locale().queryString(WebLocalizedString::PlaceholderForYearField);
122 bool DateInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
124 return hasYear && hasMonth && hasDay;