Changeset 54120 in webkit


Ignore:
Timestamp:
Jan 31, 2010 5:56:12 PM (14 years ago)
Author:
tkent@chromium.org
Message:

2010-01-31 Kent Tamura <tkent@chromium.org>

Reviewed by Darin Adler.

Fix valueAsNumber calculation for type=month.
https://bugs.webkit.org/show_bug.cgi?id=34304

valueAsNumber calculation for type=month which was checked in as
r53893 was the number of milliseconds since UNIX epoch, and it was
wrong. The correct way is the number months since UNIX epoch.

  • fast/forms/input-valueasnumber-month-expected.txt:
  • fast/forms/script-tests/input-valueasnumber-month.js:

2010-01-31 Kent Tamura <tkent@chromium.org>

Reviewed by Darin Adler.

Fix valueAsNumber calculation for type=month.
https://bugs.webkit.org/show_bug.cgi?id=34304

valueAsNumber calculation for type=month which was checked in as
r53893 was the number of milliseconds since UNIX epoch, and it was
wrong. The correct way is the number months since UNIX epoch.

  • html/DateComponents.cpp: (WebCore::DateComponents::setMonthsSinceEpoch): (WebCore::DateComponents::monthsSinceEpoch):
  • html/DateComponents.h: Declare setMonthsSinceEpoch() and monthsSinceEpoch().
  • html/HTMLInputElement.cpp: (WebCore::HTMLInputElement::parseToDouble): Switch to monthsSinceEpoch() for type=MONTH. (WebCore::HTMLInputElement::valueAsDate): Add code with millisecondsSinceEpoch() for MONTH because parseToDouble() changed its behavior. (WebCore::HTMLInputElement::setValueAsNumber): Use setMonthsSinceEpoch() for MONTH.
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r54117 r54120  
     12010-01-31  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Fix valueAsNumber calculation for type=month.
     6        https://bugs.webkit.org/show_bug.cgi?id=34304
     7
     8        valueAsNumber calculation for type=month which was checked in as
     9        r53893 was the number of milliseconds since UNIX epoch, and it was
     10        wrong. The correct way is the number months since UNIX epoch.
     11
     12        * fast/forms/input-valueasnumber-month-expected.txt:
     13        * fast/forms/script-tests/input-valueasnumber-month.js:
     14
    1152010-01-28  Ojan Vafai  <ojan@chromium.org>
    216
  • trunk/LayoutTests/fast/forms/input-valueasnumber-month-expected.txt

    r53991 r54120  
    55
    66PASS valueAsNumberFor("") is Number.NaN
    7 PASS valueAsNumberFor("1969-12") is Date.UTC(1969, 11, 1, 0, 0, 0, 0)
    8 PASS valueAsNumberFor("1970-01") is Date.UTC(1970, 0, 1)
    9 PASS valueAsNumberFor("2009-12") is Date.UTC(2009, 11, 1)
    10 PASS setValueAsNumberAndGetValue(1969, 11, 1) is "1969-12"
    11 PASS setValueAsNumberAndGetValue(1970, 0, 1) is "1970-01"
    12 PASS setValueAsNumberAndGetValue(2009, 11, 31) is "2009-12"
    13 PASS setValueAsNumberAndGetValue(10000, 0, 1) is "10000-01"
    14 PASS setValueAsNumberAndGetValue(794, 9, 22) is ""
    15 PASS setValueAsNumberAndGetValue(1582, 8, 30) is ""
    16 PASS setValueAsNumberAndGetValue(1582, 9, 1) is "1582-10"
    17 PASS setValueAsNumberAndGetValue(1582, 9, 31) is "1582-10"
    18 PASS setValueAsNumberAndGetValue(275760, 8, 13) is "275760-09"
     7PASS valueAsNumberFor("1969-01") is -12
     8PASS valueAsNumberFor("1969-12") is -1
     9PASS valueAsNumberFor("1970-01") is 0
     10PASS valueAsNumberFor("1970-12") is 11
     11PASS valueAsNumberFor("1971-01") is 12
     12PASS valueAsNumberFor("2009-12") is (2009-1970)*12+11
     13PASS input.valueAsNumber = -1; input.value is "1969-12"
     14PASS input.valueAsNumber = 0; input.value is "1970-01"
     15PASS setValueAsNumberAndGetValue(2009, 12) is "2009-12"
     16PASS setValueAsNumberAndGetValue(10000, 1) is "10000-01"
     17PASS setValueAsNumberAndGetValue(794, 9) is ""
     18PASS setValueAsNumberAndGetValue(1582, 9) is ""
     19PASS setValueAsNumberAndGetValue(1582, 10) is "1582-10"
     20PASS setValueAsNumberAndGetValue(1582, 11) is "1582-11"
     21PASS setValueAsNumberAndGetValue(275760, 9) is "275760-09"
     22PASS setValueAsNumberAndGetValue(2147483647, 12) is "2147483647-12"
    1923Tests to set invalid values to valueAsNumber:
    2024PASS input.value = ""; input.valueAsNumber = null; input.value is "1970-01"
  • trunk/LayoutTests/fast/forms/script-tests/input-valueasnumber-month.js

    r53991 r54120  
    99}
    1010
    11 function setValueAsNumberAndGetValue(year, month, day) {
    12     input.valueAsNumber = Date.UTC(year, month, day);
     11function setValueAsNumberAndGetValue(y, m) {
     12    input.valueAsNumber = (y - 1970) * 12 + m - 1;
    1313    return input.value;
    1414}
    1515
    1616shouldBe('valueAsNumberFor("")', 'Number.NaN');
    17 shouldBe('valueAsNumberFor("1969-12")', 'Date.UTC(1969, 11, 1, 0, 0, 0, 0)');
    18 shouldBe('valueAsNumberFor("1970-01")', 'Date.UTC(1970, 0, 1)');
    19 shouldBe('valueAsNumberFor("2009-12")', 'Date.UTC(2009, 11, 1)');
     17shouldBe('valueAsNumberFor("1969-01")', '-12');
     18shouldBe('valueAsNumberFor("1969-12")', '-1');
     19shouldBe('valueAsNumberFor("1970-01")', '0');
     20shouldBe('valueAsNumberFor("1970-12")', '11');
     21shouldBe('valueAsNumberFor("1971-01")', '12');
     22shouldBe('valueAsNumberFor("2009-12")', '(2009-1970)*12+11');
    2023
    21 shouldBe('setValueAsNumberAndGetValue(1969, 11, 1)', '"1969-12"');
    22 shouldBe('setValueAsNumberAndGetValue(1970, 0, 1)', '"1970-01"');
    23 shouldBe('setValueAsNumberAndGetValue(2009, 11, 31)', '"2009-12"');
    24 shouldBe('setValueAsNumberAndGetValue(10000, 0, 1)', '"10000-01"');
     24shouldBe('input.valueAsNumber = -1; input.value', '"1969-12"');
     25shouldBe('input.valueAsNumber = 0; input.value', '"1970-01"');
     26shouldBe('setValueAsNumberAndGetValue(2009, 12)', '"2009-12"');
     27shouldBe('setValueAsNumberAndGetValue(10000, 1)', '"10000-01"');
    2528
    26 shouldBe('setValueAsNumberAndGetValue(794, 9, 22)', '""');
    27 shouldBe('setValueAsNumberAndGetValue(1582, 8, 30)', '""');
    28 shouldBe('setValueAsNumberAndGetValue(1582, 9, 1)', '"1582-10"');
    29 shouldBe('setValueAsNumberAndGetValue(1582, 9, 31)', '"1582-10"');
    30 shouldBe('setValueAsNumberAndGetValue(275760, 8, 13)', '"275760-09"');
     29shouldBe('setValueAsNumberAndGetValue(794, 9)', '""');
     30shouldBe('setValueAsNumberAndGetValue(1582, 9)', '""');
     31shouldBe('setValueAsNumberAndGetValue(1582, 10)', '"1582-10"');
     32shouldBe('setValueAsNumberAndGetValue(1582, 11)', '"1582-11"');
     33shouldBe('setValueAsNumberAndGetValue(275760, 9)', '"275760-09"');
     34shouldBe('setValueAsNumberAndGetValue(2147483647, 12)', '"2147483647-12"');
     35
    3136// Date.UTC() of V8 throws an exception for the following value though JavaScriptCore doesn't.
    3237// shouldBe('setValueAsNumberAndGetValue(275760, 8, 14)', '"275760-09"');
  • trunk/WebCore/ChangeLog

    r54118 r54120  
     12010-01-31  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Fix valueAsNumber calculation for type=month.
     6        https://bugs.webkit.org/show_bug.cgi?id=34304
     7
     8        valueAsNumber calculation for type=month which was checked in as
     9        r53893 was the number of milliseconds since UNIX epoch, and it was
     10        wrong. The correct way is the number months since UNIX epoch.
     11
     12        * html/DateComponents.cpp:
     13        (WebCore::DateComponents::setMonthsSinceEpoch):
     14        (WebCore::DateComponents::monthsSinceEpoch):
     15        * html/DateComponents.h: Declare setMonthsSinceEpoch() and monthsSinceEpoch().
     16        * html/HTMLInputElement.cpp:
     17        (WebCore::HTMLInputElement::parseToDouble):
     18          Switch to monthsSinceEpoch() for type=MONTH.
     19        (WebCore::HTMLInputElement::valueAsDate):
     20          Add code with millisecondsSinceEpoch() for MONTH because
     21          parseToDouble() changed its behavior.
     22        (WebCore::HTMLInputElement::setValueAsNumber):
     23          Use setMonthsSinceEpoch() for MONTH.
     24
    1252010-01-31  Dan Bernstein  <mitz@apple.com>
    226
  • trunk/WebCore/html/DateComponents.cpp

    r54003 r54120  
    3737#include <wtf/DateMath.h>
    3838#include <wtf/MathExtras.h>
     39
     40using namespace std;
    3941
    4042namespace WebCore {
     
    538540}
    539541
     542bool DateComponents::setMonthsSinceEpoch(double months)
     543{
     544    if (!isfinite(months))
     545        return false;
     546    months = round(months);
     547    double doubleMonth = positiveFmod(months, 12);
     548    double doubleYear = 1970 + (months - doubleMonth) / 12;
     549    if (doubleYear < gregorianStartYear || numeric_limits<int>::max() < doubleYear)
     550        return false;
     551    int year = static_cast<int>(doubleYear);
     552    int month = static_cast<int>(doubleMonth);
     553    if (beforeGregorianStartDate(year, month, gregorianStartDay))
     554        return false;
     555    m_year = year;
     556    m_month = month;
     557    m_type = Month;
     558    return true;
     559}
     560
    540561// Offset from January 1st to Monday of the ISO 8601's first week.
    541562//   ex. If January 1st is Friday, such Monday is 3 days later. Returns 3.
     
    604625    ASSERT_NOT_REACHED();
    605626    return invalidMilliseconds();
     627}
     628
     629double DateComponents::monthsSinceEpoch() const
     630{
     631    ASSERT(m_type == Month);
     632    return (m_year - 1970) * 12 + m_month;
    606633}
    607634
  • trunk/WebCore/html/DateComponents.h

    r54003 r54120  
    125125    bool setMillisecondsSinceMidnight(double ms);
    126126
     127    // Another initializer for Month type. Updates m_year and m_month.
     128    bool setMonthsSinceEpoch(double months);
     129
    127130    // Returns the number of milliseconds from 1970-01-01 00:00:00 UTC.
    128131    // For a DateComponents initialized with parseDateTimeLocal(),
    129132    // millisecondsSinceEpoch() returns a value for UTC timezone.
    130133    double millisecondsSinceEpoch() const;
     134    // Returns the number of months from 1970-01.
     135    // Do not call this for types other than Month.
     136    double monthsSinceEpoch() const;
    131137    static inline double invalidMilliseconds() { return std::numeric_limits<double>::quiet_NaN(); }
    132138
  • trunk/WebCore/html/HTMLInputElement.cpp

    r54003 r54120  
    14901490    case DATETIME:
    14911491    case DATETIMELOCAL:
    1492     case MONTH:
    14931492    case TIME:
    14941493    case WEEK: {
     
    14991498        ASSERT(isfinite(msec));
    15001499        return msec;
     1500    }
     1501    case MONTH: {
     1502        DateComponents date;
     1503        if (!formStringToDateComponents(inputType(), src, &date))
     1504            return defaultValue;
     1505        double months = date.monthsSinceEpoch();
     1506        ASSERT(isfinite(months));
     1507        return months;
    15011508    }
    15021509    case NUMBER:
     
    15361543    case DATE:
    15371544    case DATETIME:
    1538     case MONTH:
    15391545    case TIME:
    15401546    case WEEK:
    15411547        return parseToDouble(value(), DateComponents::invalidMilliseconds());
     1548    case MONTH: {
     1549        DateComponents date;
     1550        if (!formStringToDateComponents(inputType(), value(), &date))
     1551            return DateComponents::invalidMilliseconds();
     1552        double msec = date.millisecondsSinceEpoch();
     1553        ASSERT(isfinite(msec));
     1554        return msec;
     1555    }
    15421556
    15431557    case BUTTON:
     
    16661680    case DATE:
    16671681    case DATETIME:
    1668     case MONTH:
    16691682    case TIME:
    16701683    case WEEK:
    16711684        setValueAsDate(newValue, ec);
    16721685        return;
     1686    case MONTH: {
     1687        DateComponents date;
     1688        if (!date.setMonthsSinceEpoch(newValue)) {
     1689            setValue(String());
     1690            return;
     1691        }
     1692        setValue(date.toString());
     1693        return;
     1694    }
    16731695    case DATETIMELOCAL: {
    16741696        DateComponents date;
Note: See TracChangeset for help on using the changeset viewer.