Changeset 51822 in webkit


Ignore:
Timestamp:
Dec 7, 2009 6:26:53 PM (14 years ago)
Author:
tkent@chromium.org
Message:

2009-12-07 Kent Tamura <tkent@chromium.org>

Reviewed by Darin Adler.

Add tests for HTMLInputElement::stepUp() and stepDown().
https://bugs.webkit.org/show_bug.cgi?id=27451

  • fast/forms/input-step-number-expected.txt: Added.
  • fast/forms/input-step-number.html: Added.
  • fast/forms/input-step-range-expected.txt: Added.
  • fast/forms/input-step-range.html: Added.
  • fast/forms/input-step-unsupported-expected.txt: Added.
  • fast/forms/input-step-unsupported.html: Added.
  • fast/forms/script-tests/input-step-number.js: Added.
  • fast/forms/script-tests/input-step-range.js: Added.
  • fast/forms/script-tests/input-step-unsupported.js: Added.

2009-12-07 Kent Tamura <tkent@chromium.org>

Reviewed by Darin Adler.

Add support for HTMLInputElement::stepUp() and stepDown() for
type=number and type=range.
https://bugs.webkit.org/show_bug.cgi?id=27451

Our implementation of stepUp() and stepDown() rounds the resultant
value to conform to the step value.
Change the number-string conversion method for RenderSlider to be
consistent with type=number.

Tests: fast/forms/input-step-number.html

fast/forms/input-step-range.html
fast/forms/input-step-unsupported.html

  • html/HTMLInputElement.cpp: (WebCore::HTMLInputElement::rangeUnderflow): Unify the code for NUMBER and RANGE. (WebCore::HTMLInputElement::rangeOverflow): Unify the code for NUMBER and RANGE. (WebCore::HTMLInputElement::minimum): Renamed from rangeMinimum(), and support for NUMBER. (WebCore::HTMLInputElement::maximum): Renamed from rangeMaximum(), and support for NUMBER. (WebCore::HTMLInputElement::stepBase): (WebCore::HTMLInputElement::stepMismatch): Use stepBase(). (WebCore::HTMLInputElement::applyStepForNumberOrRange): (WebCore::HTMLInputElement::stepUp): (WebCore::HTMLInputElement::stepDown): (WebCore::HTMLInputElement::formStringFromDouble):
  • html/HTMLInputElement.h: (WebCore::HTMLInputElement::stepUp): (WebCore::HTMLInputElement::stepDown):
  • html/HTMLInputElement.idl: Add stepUp() and stepDown().
  • rendering/RenderSlider.cpp: (WebCore::SliderRange::SliderRange): Sync with rangeMinimum()/rangeMaximum() renaming. (WebCore::RenderSlider::updateFromElement): Use formStringFromDouble(). (WebCore::RenderSlider::setValueForPosition): Use formStringFromDouble().
Location:
trunk
Files:
9 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r51819 r51822  
     12009-12-07  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Add tests for HTMLInputElement::stepUp() and stepDown().
     6        https://bugs.webkit.org/show_bug.cgi?id=27451
     7
     8        * fast/forms/input-step-number-expected.txt: Added.
     9        * fast/forms/input-step-number.html: Added.
     10        * fast/forms/input-step-range-expected.txt: Added.
     11        * fast/forms/input-step-range.html: Added.
     12        * fast/forms/input-step-unsupported-expected.txt: Added.
     13        * fast/forms/input-step-unsupported.html: Added.
     14        * fast/forms/script-tests/input-step-number.js: Added.
     15        * fast/forms/script-tests/input-step-range.js: Added.
     16        * fast/forms/script-tests/input-step-unsupported.js: Added.
     17
    1182009-12-07  Enrica Casucci  <enrica@apple.com>
    219
  • trunk/WebCore/ChangeLog

    r51821 r51822  
     12009-12-07  Kent Tamura  <tkent@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Add support for HTMLInputElement::stepUp() and stepDown() for
     6        type=number and type=range.
     7        https://bugs.webkit.org/show_bug.cgi?id=27451
     8
     9        Our implementation of stepUp() and stepDown() rounds the resultant
     10        value to conform to the step value.
     11        Change the number-string conversion method for RenderSlider to be
     12        consistent with type=number.
     13
     14        Tests: fast/forms/input-step-number.html
     15               fast/forms/input-step-range.html
     16               fast/forms/input-step-unsupported.html
     17
     18        * html/HTMLInputElement.cpp:
     19        (WebCore::HTMLInputElement::rangeUnderflow): Unify the code for NUMBER and RANGE.
     20        (WebCore::HTMLInputElement::rangeOverflow): Unify the code for NUMBER and RANGE.
     21        (WebCore::HTMLInputElement::minimum): Renamed from rangeMinimum(), and support for NUMBER.
     22        (WebCore::HTMLInputElement::maximum): Renamed from rangeMaximum(), and support for NUMBER.
     23        (WebCore::HTMLInputElement::stepBase):
     24        (WebCore::HTMLInputElement::stepMismatch): Use stepBase().
     25        (WebCore::HTMLInputElement::applyStepForNumberOrRange):
     26        (WebCore::HTMLInputElement::stepUp):
     27        (WebCore::HTMLInputElement::stepDown):
     28        (WebCore::HTMLInputElement::formStringFromDouble):
     29        * html/HTMLInputElement.h:
     30        (WebCore::HTMLInputElement::stepUp):
     31        (WebCore::HTMLInputElement::stepDown):
     32        * html/HTMLInputElement.idl: Add stepUp() and stepDown().
     33        * rendering/RenderSlider.cpp:
     34        (WebCore::SliderRange::SliderRange): Sync with rangeMinimum()/rangeMaximum() renaming.
     35        (WebCore::RenderSlider::updateFromElement): Use formStringFromDouble().
     36        (WebCore::RenderSlider::setValueForPosition): Use formStringFromDouble().
     37
    1382009-12-07  Albert J. Wong  <ajwong@chromium.org>
    239
  • trunk/WebCore/html/HTMLInputElement.cpp

    r51602 r51822  
    6666#include <wtf/MathExtras.h>
    6767#include <wtf/StdLibExtras.h>
     68#include <wtf/dtoa.h>
    6869
    6970using namespace std;
     
    7879static const double numberDefaultStep = 1.0;
    7980static const double numberStepScaleFactor = 1.0;
     81// Constant values for minimum().
     82static const double numberDefaultMinimum = -DBL_MAX;
     83static const double rangeDefaultMinimum = 0.0;
     84// Constant values for maximum().
     85static const double numberDefaultMaximum = DBL_MAX;
     86static const double rangeDefaultMaximum = 100.0;
    8087
    8188HTMLInputElement::HTMLInputElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
     
    260267bool HTMLInputElement::rangeUnderflow() const
    261268{
    262     if (inputType() == NUMBER) {
    263         double min = 0.0;
    264         double doubleValue = 0.0;
    265         if (formStringToDouble(getAttribute(minAttr), &min) && formStringToDouble(value(), &doubleValue))
    266             return doubleValue < min;
    267     } else if (inputType() == RANGE) {
     269    if (inputType() == NUMBER || inputType() == RANGE) {
    268270        double doubleValue;
    269271        if (formStringToDouble(value(), &doubleValue))
    270             return doubleValue < rangeMinimum();
     272            return doubleValue < minimum();
    271273    }
    272274    return false;
     
    275277bool HTMLInputElement::rangeOverflow() const
    276278{
    277     if (inputType() == NUMBER) {
    278         double max = 0.0;
    279         double doubleValue = 0.0;
    280         if (formStringToDouble(getAttribute(maxAttr), &max) && formStringToDouble(value(), &doubleValue))
    281             return doubleValue > max;
    282     } else if (inputType() == RANGE) {
     279    if (inputType() == NUMBER || inputType() == RANGE) {
    283280        double doubleValue;
    284281        if (formStringToDouble(value(), &doubleValue))
    285             return doubleValue > rangeMaximum();
     282            return doubleValue > maximum();
    286283    }
    287284    return false;
    288285}
    289286
    290 double HTMLInputElement::rangeMinimum() const
    291 {
    292     ASSERT(inputType() == RANGE);
    293     // The range type's "default minimum" is 0.
    294     double min = 0.0;
     287double HTMLInputElement::minimum() const
     288{
     289    ASSERT(inputType() == NUMBER || inputType() == RANGE);
     290    double min = inputType() == RANGE ? rangeDefaultMinimum : numberDefaultMinimum;
    295291    formStringToDouble(getAttribute(minAttr), &min);
    296292    return min;
    297293}
    298294
    299 double HTMLInputElement::rangeMaximum() const
    300 {
    301     ASSERT(inputType() == RANGE);
    302     // The range type's "default maximum" is 100.
    303     static const double defaultMaximum = 100.0;
     295double HTMLInputElement::maximum() const
     296{
     297    ASSERT(inputType() == NUMBER || inputType() == RANGE);
     298    double defaultMaximum = inputType() == RANGE ? rangeDefaultMaximum : numberDefaultMaximum;
    304299    double max = defaultMaximum;
    305300    formStringToDouble(getAttribute(maxAttr), &max);
    306     const double min = rangeMinimum();
    307 
    308     if (max < min) {
    309         // A remedy for the inconsistent min/max values.
    310         // Sets the maxmimum to the default (100.0) or the minimum value.
    311         max = min < defaultMaximum ? defaultMaximum : min;
     301    if (inputType() == RANGE) {
     302        // A remedy for the inconsistent min/max values for RANGE.
     303        // Sets the maxmimum to the default or the minimum value.
     304        double min = minimum();
     305        if (max < min)
     306            max = std::max(min, defaultMaximum);
    312307    }
    313308    return max;
     309}
     310
     311double HTMLInputElement::stepBase() const
     312{
     313    if (inputType() == RANGE)
     314        return minimum();
     315    if (inputType() == NUMBER) {
     316        static const double defaultStepBase = 0.0;
     317        double min = defaultStepBase;
     318        formStringToDouble(getAttribute(minAttr), &min);
     319        return min;
     320    }
     321    ASSERT_NOT_REACHED();
     322    return 0.0;
    314323}
    315324
     
    323332        if (!formStringToDouble(value(), &doubleValue))
    324333            return false;
    325         double stepBase = 0.0;
    326         formStringToDouble(getAttribute(minAttr), &stepBase);
    327         doubleValue = fabs(doubleValue - stepBase);
     334        doubleValue = fabs(doubleValue - stepBase());
    328335        if (isinf(doubleValue))
    329336            return false;
     
    405412    }
    406413    *step = parsed * stepScaleFactor;
     414    ASSERT(*step > 0);
    407415    return true;
     416}
     417
     418void HTMLInputElement::applyStepForNumberOrRange(double count, ExceptionCode& ec)
     419{
     420    ASSERT(inputType() == NUMBER || inputType() == RANGE);
     421    double step;
     422    if (!getAllowedValueStep(&step)) {
     423        ec = INVALID_STATE_ERR;
     424        return;
     425    }
     426    double current;
     427    if (!formStringToDouble(value(), &current)) {
     428        ec = INVALID_STATE_ERR;
     429        return;
     430    }
     431    double newValue = current + step * count;
     432    if (isinf(newValue)) {
     433        ec = INVALID_STATE_ERR;
     434        return;
     435    }
     436    if (newValue < minimum()) {
     437        ec = INVALID_STATE_ERR;
     438        return;
     439    }
     440    double base = stepBase();
     441    newValue = base + round((newValue - base) / step) * step;
     442    if (newValue > maximum()) {
     443        ec = INVALID_STATE_ERR;
     444        return;
     445    }
     446    setValue(formStringFromDouble(newValue));
     447}
     448
     449void HTMLInputElement::stepUp(int n, ExceptionCode& ec)
     450{
     451    if (inputType() != NUMBER && inputType() != RANGE) {
     452        ec = INVALID_STATE_ERR;
     453        return;
     454    }
     455    applyStepForNumberOrRange(n, ec);
     456}
     457
     458void HTMLInputElement::stepDown(int n, ExceptionCode& ec)
     459{
     460    if (inputType() != NUMBER && inputType() != RANGE) {
     461        ec = INVALID_STATE_ERR;
     462        return;
     463    }
     464    applyStepForNumberOrRange(-n, ec);
    408465}
    409466
     
    19982055}
    19992056
     2057String HTMLInputElement::formStringFromDouble(double number)
     2058{
     2059    // According to HTML5, "the best representation of the number n as a floating
     2060    // point number" is a string produced by applying ToString() to n.
     2061    DtoaBuffer buffer;
     2062    unsigned length;
     2063    doubleToStringInJavaScriptFormat(number, buffer, &length);
     2064    return String(buffer, length);
     2065}
     2066
    20002067bool HTMLInputElement::formStringToDouble(const String& src, double* out)
    20012068{
  • trunk/WebCore/html/HTMLInputElement.h

    r51602 r51822  
    107107    bool rangeUnderflow() const;
    108108    bool rangeOverflow() const;
    109     // Returns the minimum value for type=range.  Don't call this for other types.
    110     double rangeMinimum() const;
    111     // Returns the maximum value for type=range.  Don't call this for other types.
    112     // This always returns a value which is <= rangeMinimum().
    113     double rangeMaximum() const;
     109    // Returns the minimum value for type=number or range.  Don't call this for other types.
     110    double minimum() const;
     111    // Returns the maximum value for type=number or range.  Don't call this for other types.
     112    // This always returns a value which is >= minimum().
     113    double maximum() const;
    114114    // Sets the "allowed value step" defined in the HTML spec to the specified double pointer.
    115115    // Returns false if there is no "allowed value step."
     
    117117    // For ValidityState.
    118118    bool stepMismatch() const;
     119    // Implementations of HTMLInputElement::stepUp() and stepDown().
     120    void stepUp(int, ExceptionCode&);
     121    void stepDown(int, ExceptionCode&);
     122    void stepUp(ExceptionCode& ec) { stepUp(1, ec); }
     123    void stepDown(ExceptionCode& ec) { stepDown(1, ec); }
    119124
    120125    bool isTextButton() const { return m_type == SUBMIT || m_type == RESET || m_type == BUTTON; }
     
    250255    // The double* parameter may be 0.
    251256    static bool formStringToDouble(const String&, double*);
     257    // Converts the specified number to a string. This is an implementation of
     258    // HTML5's "algorithm to convert a number to a string" for NUMBER/RANGE types.
     259    static String formStringFromDouble(double);
    252260    // Parses the specified string as the InputType, and returns true if it is successfully parsed.
    253261    // An instance pointed by the ISODateTime* parameter will have parsed values and be
     
    279287    // Helper for getAllowedValueStep();
    280288    bool getStepParameters(double* defaultStep, double* stepScaleFactor) const;
     289    // Helper for stepUp()/stepDown().  Adds step value * count to the current number/range value.
     290    void applyStepForNumberOrRange(double count, ExceptionCode&);
     291    // Helper for applyStepForNumberOrRange().
     292    double stepBase() const;
     293
    281294#if ENABLE(DATALIST)
    282295    HTMLDataListElement* dataList() const;
  • trunk/WebCore/html/HTMLInputElement.idl

    r51602 r51822  
    7272#endif
    7373
     74        void               stepUp(in [Optional] long n)
     75            raises(DOMException);
     76        void               stepDown(in [Optional] long n)
     77            raises(DOMException);
     78
    7479        readonly attribute boolean         willValidate;
    7580        readonly attribute DOMString       validationMessage;
  • trunk/WebCore/rendering/RenderSlider.cpp

    r51159 r51822  
    8383        hasStep = element->getAllowedValueStep(&step);
    8484
    85     maximum = element->rangeMaximum();
    86     minimum = element->rangeMinimum();
     85    maximum = element->maximum();
     86    minimum = element->minimum();
    8787}
    8888
     
    380380    double value = range.valueFromElement(element, &clamped);
    381381    if (clamped)
    382         element->setValueFromRenderer(String::number(value));
     382        element->setValueFromRenderer(HTMLInputElement::formStringFromDouble(value));
    383383
    384384    // Layout will take care of the thumb's size and position.
     
    436436        fraction = 1 - fraction;
    437437    double value = range.clampValue(range.valueFromProportion(fraction));
    438     element->setValueFromRenderer(String::number(value));
     438    element->setValueFromRenderer(HTMLInputElement::formStringFromDouble(value));
    439439
    440440    // Also update the position if appropriate.
Note: See TracChangeset for help on using the changeset viewer.