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/forms/DateInputType.h"
33
34 #include "core/HTMLNames.h"
35 #include "core/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"
41
42 namespace WebCore {
43
44 using blink::WebLocalizedString;
45 using namespace HTMLNames;
46
47 static const int dateDefaultStep = 1;
48 static const int dateDefaultStepBase = 0;
49 static const int dateStepScaleFactor = 86400000;
50
DateInputType(HTMLInputElement & element)51 inline DateInputType::DateInputType(HTMLInputElement& element)
52 : BaseDateInputType(element)
53 {
54 }
55
create(HTMLInputElement & element)56 PassRefPtrWillBeRawPtr<InputType> DateInputType::create(HTMLInputElement& element)
57 {
58 return adoptRefWillBeNoop(new DateInputType(element));
59 }
60
countUsage()61 void DateInputType::countUsage()
62 {
63 countUsageIfVisible(UseCounter::InputTypeDate);
64 }
65
formControlType() const66 const AtomicString& DateInputType::formControlType() const
67 {
68 return InputTypeNames::date;
69 }
70
createStepRange(AnyStepHandling anyStepHandling) const71 StepRange DateInputType::createStepRange(AnyStepHandling anyStepHandling) const
72 {
73 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateDefaultStep, dateDefaultStepBase, dateStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
74
75 return InputType::createStepRange(anyStepHandling, dateDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDate()), Decimal::fromDouble(DateComponents::maximumDate()), stepDescription);
76 }
77
parseToDateComponentsInternal(const String & string,DateComponents * out) const78 bool DateInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
79 {
80 ASSERT(out);
81 unsigned end;
82 return out->parseDate(string, 0, end) && end == string.length();
83 }
84
setMillisecondToDateComponents(double value,DateComponents * date) const85 bool DateInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
86 {
87 ASSERT(date);
88 return date->setMillisecondsSinceEpochForDate(value);
89 }
90
isDateField() const91 bool DateInputType::isDateField() const
92 {
93 return true;
94 }
95
96 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
formatDateTimeFieldsState(const DateTimeFieldsState & dateTimeFieldsState) const97 String DateInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
98 {
99 if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
100 return emptyString();
101
102 return String::format("%04u-%02u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month(), dateTimeFieldsState.dayOfMonth());
103 }
104
setupLayoutParameters(DateTimeEditElement::LayoutParameters & layoutParameters,const DateComponents & date) const105 void DateInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
106 {
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);
116 }
117
isValidFormat(bool hasYear,bool hasMonth,bool hasWeek,bool hasDay,bool hasAMPM,bool hasHour,bool hasMinute,bool hasSecond) const118 bool DateInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
119 {
120 return hasYear && hasMonth && hasDay;
121 }
122 #endif
123
124 } // namespace WebCore
125