Changeset 67164 in webkit


Ignore:
Timestamp:
Sep 9, 2010 9:49:09 PM (14 years ago)
Author:
tkent@chromium.org
Message:

2010-09-09 Kent Tamura <tkent@chromium.org>

Reviewed by Dimitri Glazkov.

<input type=number> should not have an invalid number as its value
https://bugs.webkit.org/show_bug.cgi?id=43973

  • fast/forms/ValidityState-rangeOverflow-expected.txt:
  • fast/forms/ValidityState-rangeUnderflow-expected.txt:
  • fast/forms/ValidityState-typeMismatch-number-expected.txt:
  • fast/forms/input-number-commit-valid-only-expected.txt: Added.
  • fast/forms/input-number-commit-valid-only.html: Added.
  • fast/forms/script-tests/ValidityState-typeMismatch-number.js:
  • fast/forms/script-tests/input-number-commit-valid-only.js: Added.

2010-09-09 Kent Tamura <tkent@chromium.org>

Reviewed by Dimitri Glazkov.

<input type=number> should not have an invalid number as its value
https://bugs.webkit.org/show_bug.cgi?id=43973

This change implements the value sanitization algorithm for
type=number, and remove validity.typeMismatch support for
type=number in order to improve HTML5 conformance.

HTMLInputElement::value for type=number always has a valid number
string or an empty string. However, the input field for it, a
RenderTextControlSingleLine, can have a non-number string. For
example, '-' is a non-number string, but a user needs to type
'-'. So, the string in the input field has never been committed to
HTMLInputElement until the string becomes a valid number string.

This change is also a preparation of supporting localized
numbers. A localized string in the input field would not be
matched with HTMLInputElement::value.

Test: fast/forms/input-number-commit-valid-only.html

  • dom/InputElement.h: Add isCommittableValue()
  • html/HTMLInputElement.cpp: (WebCore::HTMLInputElement::handleBlurEvent): Set formControlValueMatchesRenderer false to clean an invalid value in the renderer. (WebCore::HTMLInputElement::isCommittableValue): (WebCore::HTMLInputElement::sanitizeValue):
  • html/HTMLInputElement.h:
  • html/ValidityState.cpp: (WebCore::ValidityState::typeMismatch): Always returns false for type=number.
  • rendering/RenderTextControlSingleLine.cpp: (WebCore::RenderTextControlSingleLine::subtreeHasChanged):
  • wml/WMLInputElement.h: (WebCore::WMLInputElement::isCommittableValue):
Location:
trunk
Files:
3 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r67163 r67164  
     12010-09-09  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Dimitri Glazkov.
     4
     5        <input type=number> should not have an invalid number as its value
     6        https://bugs.webkit.org/show_bug.cgi?id=43973
     7
     8        * fast/forms/ValidityState-rangeOverflow-expected.txt:
     9        * fast/forms/ValidityState-rangeUnderflow-expected.txt:
     10        * fast/forms/ValidityState-typeMismatch-number-expected.txt:
     11        * fast/forms/input-number-commit-valid-only-expected.txt: Added.
     12        * fast/forms/input-number-commit-valid-only.html: Added.
     13        * fast/forms/script-tests/ValidityState-typeMismatch-number.js:
     14        * fast/forms/script-tests/input-number-commit-valid-only.js: Added.
     15
    1162010-09-09  Tony Gentilcore  <tonyg@chromium.org>
    217
  • trunk/LayoutTests/fast/forms/ValidityState-rangeOverflow-expected.txt

    r66355 r67164  
    6464PASS The value "99" doesn't overflow the maximum value "1E+2".
    6565PASS The value "0.99" doesn't overflow the maximum value "1.00".
    66 PASS The value "abc" doesn't overflow the maximum value "100".
     66PASS The value "" doesn't overflow the maximum value "100".
    6767PASS The value "" doesn't overflow the maximum value "-1".
    6868PASS The value "101" doesn't overflow the maximum value "".
  • trunk/LayoutTests/fast/forms/ValidityState-rangeUnderflow-expected.txt

    r56242 r67164  
    6464PASS The value "101" doesn't underflow the minimum value "1E+2".
    6565PASS The value "1.01" doesn't underflow the minimum value "1.00".
    66 PASS The value "abc" doesn't underflow the minimum value "100".
     66PASS The value "" doesn't underflow the minimum value "100".
    6767PASS The value "" doesn't underflow the minimum value "1".
    6868PASS The value "-1" doesn't underflow the minimum value "".
  • trunk/LayoutTests/fast/forms/ValidityState-typeMismatch-number-expected.txt

    r48163 r67164  
    44
    55
    6 PASS 0 is a correct valid number.
    7 PASS 10 is a correct valid number.
    8 PASS 01 is a correct valid number.
    9 PASS -0 is a correct valid number.
    10 PASS -1.2 is a correct valid number.
    11 PASS 1.2E10 is a correct valid number.
    12 PASS 1.2E-10 is a correct valid number.
    13 PASS 1.2E+10 is a correct valid number.
    14 PASS 12345678901234567890123456789012345678901234567890 is a correct valid number.
    15 PASS 0.12345678901234567890123456789012345678901234567890 is a correct valid number.
    16 PASS abc is a correct invalid number.
    17 PASS 0xff is a correct invalid number.
    18 PASS +1 is a correct invalid number.
    19 PASS  10 is a correct invalid number.
    20 PASS 10  is a correct invalid number.
    21 PASS 1,2 is a correct invalid number.
    22 PASS .2 is a correct invalid number.
    23 PASS 1E is a correct invalid number.
    24 PASS NaN is a correct invalid number.
    25 PASS nan is a correct invalid number.
    26 PASS Inf is a correct invalid number.
    27 PASS inf is a correct invalid number.
    28 PASS Infinity is a correct invalid number.
    29 PASS infinity is a correct invalid number.
    30 PASS  is a correct valid number.
    31 PASS 1.2E65535 is a correct invalid number.
    32 PASS 1. is a correct valid number.
    33 PASS 1.2e10 is a correct valid number.
     6PASS "0" is a valid number.
     7PASS "10" is a valid number.
     8PASS "01" is a valid number.
     9PASS "-0" is a valid number.
     10PASS "-1.2" is a valid number.
     11PASS "1.2E10" is a valid number.
     12PASS "1.2E-10" is a valid number.
     13PASS "1.2E+10" is a valid number.
     14PASS "12345678901234567890123456789012345678901234567890" is a valid number.
     15PASS "0.12345678901234567890123456789012345678901234567890" is a valid number.
     16PASS "abc" was sanitized to "".
     17PASS "0xff" was sanitized to "".
     18PASS "+1" was sanitized to "".
     19PASS " 10" was sanitized to "".
     20PASS "10 " was sanitized to "".
     21PASS "1,2" was sanitized to "".
     22PASS ".2" was sanitized to "".
     23PASS "1E" was sanitized to "".
     24PASS "NaN" was sanitized to "".
     25PASS "nan" was sanitized to "".
     26PASS "Inf" was sanitized to "".
     27PASS "inf" was sanitized to "".
     28PASS "Infinity" was sanitized to "".
     29PASS "infinity" was sanitized to "".
     30PASS "" is a valid number.
     31PASS "1.2E65535" was sanitized to "".
     32PASS "1." is a valid number.
     33PASS "1.2e10" is a valid number.
    3434PASS successfullyParsed is true
    3535
  • trunk/LayoutTests/fast/forms/script-tests/ValidityState-typeMismatch-number.js

    r48552 r67164  
    44i.type = 'number';
    55
    6 function check(value, mismatchExpected)
     6function check(value)
    77{
    88    i.value = value;
    9     var actual = i.validity.typeMismatch;
    10     var didPass = actual == mismatchExpected;
    11     var resultText = value + ' is ' + (didPass ? 'a correct ' : 'an incorrect ') + (actual ? 'invalid' : 'valid') + ' number.';
    12     if (didPass)
     9    var mismatch = i.validity.typeMismatch;
     10    var resultText = '"' + value + '" is ' + (mismatch ? 'an invalid' : 'a valid') + ' number.';
     11    if (!mismatch)
    1312        testPassed(resultText);
    1413    else
     
    1615}
    1716
     17function checkSanitization(value, expectedValue)
     18{
     19    i.value = value;
     20    if (i.validity.typeMismatch) {
     21        testFailed('"' + value + '" made typeMismatch true.');
     22    } else if (i.value != expectedValue) {
     23        testFailed('"' + value + '" was sanitized to "' + i.value + '". It should be ' + expectedValue);
     24    } else {
     25        testPassed('"' + value + '" was sanitized to "' + i.value + '".');
     26    }
     27}
     28
    1829// Valid values
    19 check('0', false);
    20 check('10', false);
    21 check('01', false);
    22 check('-0', false);
    23 check('-1.2', false);
    24 check('1.2E10', false);
    25 check('1.2E-10', false);
    26 check('1.2E+10', false);
    27 check('12345678901234567890123456789012345678901234567890', false);
    28 check('0.12345678901234567890123456789012345678901234567890', false);
     30check('0');
     31check('10');
     32check('01');
     33check('-0');
     34check('-1.2');
     35check('1.2E10');
     36check('1.2E-10');
     37check('1.2E+10');
     38check('12345678901234567890123456789012345678901234567890');
     39check('0.12345678901234567890123456789012345678901234567890');
    2940
    3041// Invalid values
    31 check('abc', true);
    32 check('0xff', true);
     42checkSanitization('abc', '');
     43checkSanitization('0xff', '');
    3344
    34 check('+1', true);
    35 check(' 10', true);
    36 check('10 ', true);
    37 check('1,2', true);
    38 check('.2', true);
    39 check('1E', true);
    40 check('NaN', true);
    41 check('nan', true);
    42 check('Inf', true);
    43 check('inf', true);
    44 check('Infinity', true);
    45 check('infinity', true);
     45checkSanitization('+1', '');
     46checkSanitization(' 10', '');
     47checkSanitization('10 ', '');
     48checkSanitization('1,2', '');
     49checkSanitization('.2', '');
     50checkSanitization('1E', '');
     51checkSanitization('NaN', '');
     52checkSanitization('nan', '');
     53checkSanitization('Inf', '');
     54checkSanitization('inf', '');
     55checkSanitization('Infinity', '');
     56checkSanitization('infinity', '');
    4657
    4758// Assume empty string as valid.
    48 check('', false);
     59check('');
    4960
    50 // The spec allows, but our implementation doesn't
    5161// Huge exponent.
    52 check('1.2E65535', true);
     62checkSanitization('1.2E65535', '');
    5363
    5464// The spec doesn't allow, but our implementation does.
    55 check('1.', false);
    56 check('1.2e10', false);
     65check('1.');
     66check('1.2e10');
    5767
    5868var successfullyParsed = true;
  • trunk/WebCore/ChangeLog

    r67163 r67164  
     12010-09-09  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Dimitri Glazkov.
     4
     5        <input type=number> should not have an invalid number as its value
     6        https://bugs.webkit.org/show_bug.cgi?id=43973
     7
     8        This change implements the value sanitization algorithm for
     9        type=number, and remove validity.typeMismatch support for
     10        type=number in order to improve HTML5 conformance.
     11
     12        HTMLInputElement::value for type=number always has a valid number
     13        string or an empty string. However, the input field for it, a
     14        RenderTextControlSingleLine, can have a non-number string. For
     15        example, '-' is a non-number string, but a user needs to type
     16        '-'. So, the string in the input field has never been committed to
     17        HTMLInputElement until the string becomes a valid number string.
     18
     19        This change is also a preparation of supporting localized
     20        numbers. A localized string in the input field would not be
     21        matched with HTMLInputElement::value.
     22
     23        Test: fast/forms/input-number-commit-valid-only.html
     24
     25        * dom/InputElement.h: Add isCommittableValue()
     26        * html/HTMLInputElement.cpp:
     27        (WebCore::HTMLInputElement::handleBlurEvent):
     28         Set formControlValueMatchesRenderer false to clean an invalid value in the renderer.
     29        (WebCore::HTMLInputElement::isCommittableValue):
     30        (WebCore::HTMLInputElement::sanitizeValue):
     31        * html/HTMLInputElement.h:
     32        * html/ValidityState.cpp:
     33        (WebCore::ValidityState::typeMismatch): Always returns false for type=number.
     34        * rendering/RenderTextControlSingleLine.cpp:
     35        (WebCore::RenderTextControlSingleLine::subtreeHasChanged):
     36        * wml/WMLInputElement.h:
     37        (WebCore::WMLInputElement::isCommittableValue):
     38
    1392010-09-09  Tony Gentilcore  <tonyg@chromium.org>
    240
  • trunk/WebCore/dom/InputElement.h

    r65335 r67164  
    6161    virtual void setValueForUser(const String&) = 0;
    6262
     63    // Returns true if the specified string can be set as the value of InputElement.
     64    virtual bool isAcceptableValue(const String&) const = 0;
    6365    virtual String sanitizeValue(const String&) const = 0;
    6466    virtual void setValueFromRenderer(const String&) = 0;
  • trunk/WebCore/html/HTMLInputElement.cpp

    r67122 r67164  
    261261        return !isValidColorString(value);
    262262    case NUMBER:
    263         return !parseToDoubleForNumberType(value, 0);
     263        ASSERT(parseToDoubleForNumberType(value, 0));
     264        return false;
    264265    case URL:
    265266        return !KURL(KURL(), value).isValid();
     
    828829void HTMLInputElement::handleBlurEvent()
    829830{
     831    // Reset the renderer value, which might be unmatched with the element value.
     832    setFormControlValueMatchesRenderer(false);
    830833    InputElement::dispatchBlurEvent(this, this);
    831834}
     
    26652668}
    26662669
     2670bool HTMLInputElement::isAcceptableValue(const String& proposedValue) const
     2671{
     2672    if (inputType() != NUMBER)
     2673        return true;
     2674    return proposedValue.isEmpty() || parseToDoubleForNumberType(proposedValue, 0);
     2675}
     2676
    26672677String HTMLInputElement::sanitizeValue(const String& proposedValue) const
    26682678{
     2679    if (inputType() == NUMBER)
     2680        return parseToDoubleForNumberType(proposedValue, 0) ? proposedValue : String();
     2681
    26692682    if (isTextField())
    26702683        return InputElement::sanitizeValueForTextField(this, proposedValue);
  • trunk/WebCore/html/HTMLInputElement.h

    r66357 r67164  
    282282    virtual void cacheSelection(int start, int end);
    283283
     284    virtual bool isAcceptableValue(const String&) const;
    284285    virtual String sanitizeValue(const String&) const;
    285286
  • trunk/WebCore/rendering/RenderTextControlSingleLine.cpp

    r67122 r67164  
    171171    // sanitizeUserInputValue().
    172172    // sanitizeValue() is needed because IME input doesn't dispatch BeforeTextInsertedEvent.
    173     input->setValueFromRenderer(input->sanitizeValue(text()));
     173    String value = text();
     174    if (input->isAcceptableValue(value))
     175        input->setValueFromRenderer(input->sanitizeValue(value));
    174176
    175177    if (m_cancelButton)
  • trunk/WebCore/wml/WMLInputElement.h

    r66362 r67164  
    8383    virtual void cacheSelection(int start, int end);
    8484
     85    virtual bool isAcceptableValue(const String&) const { return true; }
    8586    virtual String sanitizeValue(const String& proposedValue) const { return constrainValue(proposedValue); }
    8687
Note: See TracChangeset for help on using the changeset viewer.