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 "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/PassOwnPtr.h"
44 using blink::WebLocalizedString;
45 using namespace HTMLNames;
47 static const int dateDefaultStep = 1;
48 static const int dateDefaultStepBase = 0;
49 static const int dateStepScaleFactor = 86400000;
51 inline DateInputType::DateInputType(HTMLInputElement& element)
52 : BaseDateInputType(element)
56 PassRefPtr<InputType> DateInputType::create(HTMLInputElement& element)
58 return adoptRef(new DateInputType(element));
61 void DateInputType::countUsage()
63 countUsageIfVisible(UseCounter::InputTypeDate);
66 const AtomicString& DateInputType::formControlType() const
68 return InputTypeNames::date;
71 StepRange DateInputType::createStepRange(AnyStepHandling anyStepHandling) const
73 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateDefaultStep, dateDefaultStepBase, dateStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
75 return InputType::createStepRange(anyStepHandling, dateDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDate()), Decimal::fromDouble(DateComponents::maximumDate()), stepDescription);
78 bool DateInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
82 return out->parseDate(string, 0, end) && end == string.length();
85 bool DateInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
88 return date->setMillisecondsSinceEpochForDate(value);
91 bool DateInputType::isDateField() const
96 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
97 String DateInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
99 if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
100 return emptyString();
102 return String::format("%04u-%02u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month(), dateTimeFieldsState.dayOfMonth());
105 void DateInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
107 layoutParameters.dateTimeFormat = layoutParameters.locale.dateFormat();
108 layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd";
109 if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
110 layoutParameters.minimum = DateComponents();
111 if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
112 layoutParameters.maximum = DateComponents();
113 layoutParameters.placeholderForDay = locale().queryString(WebLocalizedString::PlaceholderForDayOfMonthField);
114 layoutParameters.placeholderForMonth = locale().queryString(WebLocalizedString::PlaceholderForMonthField);
115 layoutParameters.placeholderForYear = locale().queryString(WebLocalizedString::PlaceholderForYearField);
118 bool DateInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
120 return hasYear && hasMonth && hasDay;
124 } // namespace WebCore