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/DateInputType.h"
33
34#include "HTMLNames.h"
35#include "core/html/DateTimeFieldsState.h"
36#include "core/html/HTMLInputElement.h"
37#include "core/html/InputTypeNames.h"
38#include "core/platform/DateComponents.h"
39#include "core/platform/LocalizedStrings.h"
40#include "core/platform/text/PlatformLocale.h"
41#include "wtf/PassOwnPtr.h"
42
43namespace WebCore {
44
45using namespace HTMLNames;
46
47static const int dateDefaultStep = 1;
48static const int dateDefaultStepBase = 0;
49static const int dateStepScaleFactor = 86400000;
50
51inline DateInputType::DateInputType(HTMLInputElement* element)
52    : BaseDateInputType(element)
53{
54}
55
56PassOwnPtr<InputType> DateInputType::create(HTMLInputElement* element)
57{
58    return adoptPtr(new DateInputType(element));
59}
60
61void DateInputType::attach()
62{
63    observeFeatureIfVisible(UseCounter::InputTypeDate);
64}
65
66const AtomicString& DateInputType::formControlType() const
67{
68    return InputTypeNames::date();
69}
70
71DateComponents::Type DateInputType::dateType() const
72{
73    return DateComponents::Date;
74}
75
76StepRange DateInputType::createStepRange(AnyStepHandling anyStepHandling) const
77{
78    DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateDefaultStep, dateDefaultStepBase, dateStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
79
80    const Decimal stepBase = parseToNumber(element()->fastGetAttribute(minAttr), 0);
81    const Decimal minimum = parseToNumber(element()->fastGetAttribute(minAttr), Decimal::fromDouble(DateComponents::minimumDate()));
82    const Decimal maximum = parseToNumber(element()->fastGetAttribute(maxAttr), Decimal::fromDouble(DateComponents::maximumDate()));
83    const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element()->fastGetAttribute(stepAttr));
84    return StepRange(stepBase, minimum, maximum, step, stepDescription);
85}
86
87bool DateInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
88{
89    ASSERT(out);
90    unsigned end;
91    return out->parseDate(string, 0, end) && end == string.length();
92}
93
94bool DateInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
95{
96    ASSERT(date);
97    return date->setMillisecondsSinceEpochForDate(value);
98}
99
100bool DateInputType::isDateField() const
101{
102    return true;
103}
104
105#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
106String DateInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
107{
108    if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
109        return emptyString();
110
111    return String::format("%04u-%02u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month(), dateTimeFieldsState.dayOfMonth());
112}
113
114void DateInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
115{
116    layoutParameters.dateTimeFormat = layoutParameters.locale.dateFormat();
117    layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd";
118    if (!parseToDateComponents(element()->fastGetAttribute(minAttr), &layoutParameters.minimum))
119        layoutParameters.minimum = DateComponents();
120    if (!parseToDateComponents(element()->fastGetAttribute(maxAttr), &layoutParameters.maximum))
121        layoutParameters.maximum = DateComponents();
122    layoutParameters.placeholderForDay = placeholderForDayOfMonthField();
123    layoutParameters.placeholderForMonth = placeholderForMonthField();
124    layoutParameters.placeholderForYear = placeholderForYearField();
125}
126
127bool DateInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
128{
129    return hasYear && hasMonth && hasDay;
130}
131#endif
132
133} // namespace WebCore
134