Home | History | Annotate | Download | only in forms
      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/DateTimeLocalInputType.h"
     33 
     34 #include "bindings/core/v8/ExceptionState.h"
     35 #include "core/HTMLNames.h"
     36 #include "core/InputTypeNames.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 #include "wtf/text/WTFString.h"
     43 
     44 namespace blink {
     45 
     46 using blink::WebLocalizedString;
     47 using namespace HTMLNames;
     48 
     49 static const int dateTimeLocalDefaultStep = 60;
     50 static const int dateTimeLocalDefaultStepBase = 0;
     51 static const int dateTimeLocalStepScaleFactor = 1000;
     52 
     53 PassRefPtrWillBeRawPtr<InputType> DateTimeLocalInputType::create(HTMLInputElement& element)
     54 {
     55     return adoptRefWillBeNoop(new DateTimeLocalInputType(element));
     56 }
     57 
     58 void DateTimeLocalInputType::countUsage()
     59 {
     60     countUsageIfVisible(UseCounter::InputTypeDateTimeLocal);
     61 }
     62 
     63 const AtomicString& DateTimeLocalInputType::formControlType() const
     64 {
     65     return InputTypeNames::datetime_local;
     66 }
     67 
     68 double DateTimeLocalInputType::valueAsDate() const
     69 {
     70     // valueAsDate doesn't work for the datetime-local type according to the standard.
     71     return DateComponents::invalidMilliseconds();
     72 }
     73 
     74 void DateTimeLocalInputType::setValueAsDate(double value, ExceptionState& exceptionState) const
     75 {
     76     // valueAsDate doesn't work for the datetime-local type according to the standard.
     77     InputType::setValueAsDate(value, exceptionState);
     78 }
     79 
     80 StepRange DateTimeLocalInputType::createStepRange(AnyStepHandling anyStepHandling) const
     81 {
     82     DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateTimeLocalDefaultStep, dateTimeLocalDefaultStepBase, dateTimeLocalStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
     83 
     84     return InputType::createStepRange(anyStepHandling, dateTimeLocalDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDateTime()), Decimal::fromDouble(DateComponents::maximumDateTime()), stepDescription);
     85 }
     86 
     87 bool DateTimeLocalInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
     88 {
     89     ASSERT(out);
     90     unsigned end;
     91     return out->parseDateTimeLocal(string, 0, end) && end == string.length();
     92 }
     93 
     94 bool DateTimeLocalInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
     95 {
     96     ASSERT(date);
     97     return date->setMillisecondsSinceEpochForDateTimeLocal(value);
     98 }
     99 
    100 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
    101 // FIXME: It is better to share code for DateTimeInputType::formatDateTimeFieldsState()
    102 // and DateTimeInputLocalType::formatDateTimeFieldsState().
    103 String DateTimeLocalInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
    104 {
    105     if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear()
    106         || !dateTimeFieldsState.hasHour() || !dateTimeFieldsState.hasMinute() || !dateTimeFieldsState.hasAMPM())
    107         return emptyString();
    108 
    109     if (dateTimeFieldsState.hasMillisecond() && dateTimeFieldsState.millisecond()) {
    110         return String::format("%04u-%02u-%02uT%02u:%02u:%02u.%03u",
    111             dateTimeFieldsState.year(),
    112             dateTimeFieldsState.month(),
    113             dateTimeFieldsState.dayOfMonth(),
    114             dateTimeFieldsState.hour23(),
    115             dateTimeFieldsState.minute(),
    116             dateTimeFieldsState.hasSecond() ? dateTimeFieldsState.second() : 0,
    117             dateTimeFieldsState.millisecond());
    118     }
    119 
    120     if (dateTimeFieldsState.hasSecond() && dateTimeFieldsState.second()) {
    121         return String::format("%04u-%02u-%02uT%02u:%02u:%02u",
    122             dateTimeFieldsState.year(),
    123             dateTimeFieldsState.month(),
    124             dateTimeFieldsState.dayOfMonth(),
    125             dateTimeFieldsState.hour23(),
    126             dateTimeFieldsState.minute(),
    127             dateTimeFieldsState.second());
    128     }
    129 
    130     return String::format("%04u-%02u-%02uT%02u:%02u",
    131         dateTimeFieldsState.year(),
    132         dateTimeFieldsState.month(),
    133         dateTimeFieldsState.dayOfMonth(),
    134         dateTimeFieldsState.hour23(),
    135         dateTimeFieldsState.minute());
    136 }
    137 
    138 void DateTimeLocalInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
    139 {
    140     if (shouldHaveSecondField(date)) {
    141         layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithSeconds();
    142         layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
    143     } else {
    144         layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithoutSeconds();
    145         layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm";
    146     }
    147     if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
    148         layoutParameters.minimum = DateComponents();
    149     if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
    150         layoutParameters.maximum = DateComponents();
    151     layoutParameters.placeholderForDay = locale().queryString(WebLocalizedString::PlaceholderForDayOfMonthField);
    152     layoutParameters.placeholderForMonth = locale().queryString(WebLocalizedString::PlaceholderForMonthField);
    153     layoutParameters.placeholderForYear = locale().queryString(WebLocalizedString::PlaceholderForYearField);
    154 }
    155 
    156 bool DateTimeLocalInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
    157 {
    158     return hasYear && hasMonth && hasDay && hasAMPM && hasHour && hasMinute;
    159 }
    160 #endif
    161 
    162 } // namespace blink
    163