Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / forms / DateInputType.cpp
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/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"
42
43 namespace blink {
44
45 using blink::WebLocalizedString;
46 using namespace HTMLNames;
47
48 static const int dateDefaultStep = 1;
49 static const int dateDefaultStepBase = 0;
50 static const int dateStepScaleFactor = 86400000;
51
52 inline DateInputType::DateInputType(HTMLInputElement& element)
53     : BaseDateInputType(element)
54 {
55 }
56
57 PassRefPtrWillBeRawPtr<InputType> DateInputType::create(HTMLInputElement& element)
58 {
59     return adoptRefWillBeNoop(new DateInputType(element));
60 }
61
62 void DateInputType::countUsage()
63 {
64     countUsageIfVisible(UseCounter::InputTypeDate);
65 }
66
67 const AtomicString& DateInputType::formControlType() const
68 {
69     return InputTypeNames::date;
70 }
71
72 StepRange DateInputType::createStepRange(AnyStepHandling anyStepHandling) const
73 {
74     DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateDefaultStep, dateDefaultStepBase, dateStepScaleFactor, StepRange::ParsedStepValueShouldBeInteger));
75
76     return InputType::createStepRange(anyStepHandling, dateDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDate()), Decimal::fromDouble(DateComponents::maximumDate()), stepDescription);
77 }
78
79 bool DateInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
80 {
81     ASSERT(out);
82     unsigned end;
83     return out->parseDate(string, 0, end) && end == string.length();
84 }
85
86 bool DateInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
87 {
88     ASSERT(date);
89     return date->setMillisecondsSinceEpochForDate(value);
90 }
91
92 void DateInputType::warnIfValueIsInvalid(const String& value) const
93 {
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())));
97     }
98 }
99
100 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
101 String DateInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
102 {
103     if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear())
104         return emptyString();
105
106     return String::format("%04u-%02u-%02u", dateTimeFieldsState.year(), dateTimeFieldsState.month(), dateTimeFieldsState.dayOfMonth());
107 }
108
109 void DateInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
110 {
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);
120 }
121
122 bool DateInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
123 {
124     return hasYear && hasMonth && hasDay;
125 }
126 #endif
127
128 } // namespace blink