Home | History | Annotate | Download | only in forms
      1 /*
      2  * Copyright (C) 2012 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
      6  * are met:
      7  * 1.  Redistributions of source code must retain the above copyright
      8  *     notice, this list of conditions and the following disclaimer.
      9  * 2.  Redistributions in binary form must reproduce the above copyright
     10  *     notice, this list of conditions and the following disclaimer in the
     11  *     documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
     17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  * SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     28 #include "core/html/forms/DateTimeFieldsState.h"
     29 
     30 #include "core/html/forms/FormController.h"
     31 
     32 namespace blink {
     33 
     34 const unsigned DateTimeFieldsState::emptyValue = static_cast<unsigned>(-1);
     35 
     36 static unsigned getNumberFromFormControlState(const FormControlState& state, size_t index)
     37 {
     38     if (index >= state.valueSize())
     39         return DateTimeFieldsState::emptyValue;
     40     bool parsed;
     41     unsigned const value = state[index].toUInt(&parsed);
     42     return parsed ? value : DateTimeFieldsState::emptyValue;
     43 }
     44 
     45 static DateTimeFieldsState::AMPMValue getAMPMFromFormControlState(const FormControlState& state, size_t index)
     46 {
     47     if (index >= state.valueSize())
     48         return DateTimeFieldsState::AMPMValueEmpty;
     49     const String value = state[index];
     50     if (value == "A")
     51         return DateTimeFieldsState::AMPMValueAM;
     52     if (value == "P")
     53         return DateTimeFieldsState::AMPMValuePM;
     54     return DateTimeFieldsState::AMPMValueEmpty;
     55 }
     56 
     57 DateTimeFieldsState::DateTimeFieldsState()
     58     : m_year(emptyValue)
     59     , m_month(emptyValue)
     60     , m_dayOfMonth(emptyValue)
     61     , m_hour(emptyValue)
     62     , m_minute(emptyValue)
     63     , m_second(emptyValue)
     64     , m_millisecond(emptyValue)
     65     , m_weekOfYear(emptyValue)
     66     , m_ampm(AMPMValueEmpty)
     67 {
     68 }
     69 
     70 unsigned DateTimeFieldsState::hour23() const
     71 {
     72     if (!hasHour() || !hasAMPM())
     73         return emptyValue;
     74     return (m_hour % 12) + (m_ampm == AMPMValuePM ? 12 : 0);
     75 }
     76 
     77 DateTimeFieldsState DateTimeFieldsState::restoreFormControlState(const FormControlState& state)
     78 {
     79     DateTimeFieldsState dateTimeFieldsState;
     80     dateTimeFieldsState.setYear(getNumberFromFormControlState(state, 0));
     81     dateTimeFieldsState.setMonth(getNumberFromFormControlState(state, 1));
     82     dateTimeFieldsState.setDayOfMonth(getNumberFromFormControlState(state, 2));
     83     dateTimeFieldsState.setHour(getNumberFromFormControlState(state, 3));
     84     dateTimeFieldsState.setMinute(getNumberFromFormControlState(state, 4));
     85     dateTimeFieldsState.setSecond(getNumberFromFormControlState(state, 5));
     86     dateTimeFieldsState.setMillisecond(getNumberFromFormControlState(state, 6));
     87     dateTimeFieldsState.setWeekOfYear(getNumberFromFormControlState(state, 7));
     88     dateTimeFieldsState.setAMPM(getAMPMFromFormControlState(state, 8));
     89     return dateTimeFieldsState;
     90 }
     91 
     92 FormControlState DateTimeFieldsState::saveFormControlState() const
     93 {
     94     FormControlState state;
     95     state.append(hasYear() ? String::number(m_year) : emptyString());
     96     state.append(hasMonth() ? String::number(m_month) : emptyString());
     97     state.append(hasDayOfMonth() ? String::number(m_dayOfMonth) : emptyString());
     98     state.append(hasHour() ? String::number(m_hour) : emptyString());
     99     state.append(hasMinute() ? String::number(m_minute) : emptyString());
    100     state.append(hasSecond() ? String::number(m_second) : emptyString());
    101     state.append(hasMillisecond() ? String::number(m_millisecond) : emptyString());
    102     state.append(hasWeekOfYear() ? String::number(m_weekOfYear) : emptyString());
    103     if (hasAMPM())
    104         state.append(m_ampm == AMPMValueAM ? "A" : "P");
    105     else
    106         state.append(emptyString());
    107     return state;
    108 }
    109 
    110 } // namespace blink
    111 
    112 #endif
    113