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#if ENABLE(INPUT_TYPE_DATETIME_INCOMPLETE)
33#include "DateTimeInputType.h"
34
35#include "DateComponents.h"
36#include "HTMLInputElement.h"
37#include "HTMLNames.h"
38#include "InputTypeNames.h"
39#include <wtf/CurrentTime.h>
40#include <wtf/PassOwnPtr.h>
41
42namespace WebCore {
43
44using namespace HTMLNames;
45
46static const int dateTimeDefaultStep = 60;
47static const int dateTimeDefaultStepBase = 0;
48static const int dateTimeStepScaleFactor = 1000;
49
50PassOwnPtr<InputType> DateTimeInputType::create(HTMLInputElement* element)
51{
52    return adoptPtr(new DateTimeInputType(element));
53}
54
55void DateTimeInputType::attach()
56{
57    observeFeatureIfVisible(FeatureObserver::InputTypeDateTime);
58}
59
60const AtomicString& DateTimeInputType::formControlType() const
61{
62    return InputTypeNames::datetime();
63}
64
65DateComponents::Type DateTimeInputType::dateType() const
66{
67    return DateComponents::DateTime;
68}
69
70Decimal DateTimeInputType::defaultValueForStepUp() const
71{
72    return Decimal::fromDouble(currentTimeMS());
73}
74
75StepRange DateTimeInputType::createStepRange(AnyStepHandling anyStepHandling) const
76{
77    DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateTimeDefaultStep, dateTimeDefaultStepBase, dateTimeStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
78
79    const Decimal stepBase = parseToNumber(element()->fastGetAttribute(minAttr), 0);
80    const Decimal minimum = parseToNumber(element()->fastGetAttribute(minAttr), Decimal::fromDouble(DateComponents::minimumDateTime()));
81    const Decimal maximum = parseToNumber(element()->fastGetAttribute(maxAttr), Decimal::fromDouble(DateComponents::maximumDateTime()));
82    const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element()->fastGetAttribute(stepAttr));
83    return StepRange(stepBase, minimum, maximum, step, stepDescription);
84}
85
86bool DateTimeInputType::parseToDateComponentsInternal(const UChar* characters, unsigned length, DateComponents* out) const
87{
88    ASSERT(out);
89    unsigned end;
90    return out->parseDateTime(characters, length, 0, end) && end == length;
91}
92
93bool DateTimeInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
94{
95    ASSERT(date);
96    return date->setMillisecondsSinceEpochForDateTime(value);
97}
98
99bool DateTimeInputType::isDateTimeField() const
100{
101    return true;
102}
103
104String DateTimeInputType::sanitizeValue(const String& proposedValue) const
105{
106    DateComponents date;
107    if (!parseToDateComponents(proposedValue, &date))
108        return String();
109    return date.toString();
110}
111
112} // namespace WebCore
113
114#endif
115