Changeset 117929 in webkit


Ignore:
Timestamp:
May 22, 2012 12:53:22 AM (12 years ago)
Author:
yosin@chromium.org
Message:

[Forms][Meter][Progress] Change function signature of parseToDoubleForNumberType
https://bugs.webkit.org/show_bug.cgi?id=87077

Reviewed by Hajime Morita.

This patch changes function signature of parseToDoubleForNumberType and
parseToDoubleForNumberTypeWithDecimalPlaces to return double value instead
of bool for reducing code in call sites for ease of maintenance. This patch
also allows to use functional style of using these functions.

No new tests. This patch doesn't change behavior.

  • html/HTMLMeterElement.cpp:

(WebCore::HTMLMeterElement::min): Changed for using double return value.
(WebCore::HTMLMeterElement::max): Changed for using double return value.
(WebCore::HTMLMeterElement::value): Changed for using double return value.
(WebCore::HTMLMeterElement::low): Changed for using double return value.
(WebCore::HTMLMeterElement::high): Changed for using double return value.
(WebCore::HTMLMeterElement::optimum): Changed for using double return value.

  • html/HTMLProgressElement.cpp:

(WebCore::HTMLProgressElement::value): Changed for using double return value.
(WebCore::HTMLProgressElement::max): Changed for using double return value.

  • html/NumberInputType.cpp:

(WebCore::NumberInputType::typeMismatchFor): Changed for using double return value.
(WebCore::NumberInputType::sizeShouldIncludeDecoration): Changed for using double return value.
(WebCore::NumberInputType::parseToDouble): Changed for using double return value.
(WebCore::NumberInputType::parseToDoubleWithDecimalPlaces): Changed for using double return value.
(WebCore::NumberInputType::visibleValue): Changed for using double return value.
(WebCore::NumberInputType::sanitizeValue): Changed for using double return value.

  • html/RangeInputType.cpp:

(WebCore::RangeInputType::parseToDouble): Changed for using double return value.

  • html/StepRange.cpp:

(WebCore::StepRange::parseStep): Changed for using double return value.

  • html/StepRange.h:

(WebCore::StepRange::defaultValue): Added "const" attribute
(WebCore::StepRange::proportionFromValue): Added "const" attribute
(WebCore::StepRange::valueFromProportion): Added "const" attribute

  • html/parser/HTMLParserIdioms.cpp:

(WebCore::parseToDoubleForNumberType): Changed for using double return value. Added one parameter function.
(WebCore::parseToDoubleForNumberTypeWithDecimalPlaces): Changed for using double return value. Added function for providing default fallback value.

  • html/parser/HTMLParserIdioms.h: Changed function prototype and added one parameter prototypes.
  • html/shadow/SliderThumbElement.cpp:

(WebCore::sliderPosition): Changed for using double return value.

Location:
trunk/Source/WebCore
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r117928 r117929  
     12012-05-22  Yoshifumi Inoue  <yosin@chromium.org>
     2
     3        [Forms][Meter][Progress] Change function signature of parseToDoubleForNumberType
     4        https://bugs.webkit.org/show_bug.cgi?id=87077
     5
     6        Reviewed by Hajime Morita.
     7
     8        This patch changes function signature of parseToDoubleForNumberType and
     9        parseToDoubleForNumberTypeWithDecimalPlaces to return double value instead
     10        of bool for reducing code in call sites for ease of maintenance. This patch
     11        also allows to use functional style of using these functions.
     12
     13        No new tests. This patch doesn't change behavior.
     14
     15        * html/HTMLMeterElement.cpp:
     16        (WebCore::HTMLMeterElement::min): Changed for using double return value.
     17        (WebCore::HTMLMeterElement::max): Changed for using double return value.
     18        (WebCore::HTMLMeterElement::value): Changed for using double return value.
     19        (WebCore::HTMLMeterElement::low): Changed for using double return value.
     20        (WebCore::HTMLMeterElement::high): Changed for using double return value.
     21        (WebCore::HTMLMeterElement::optimum): Changed for using double return value.
     22        * html/HTMLProgressElement.cpp:
     23        (WebCore::HTMLProgressElement::value): Changed for using double return value.
     24        (WebCore::HTMLProgressElement::max): Changed for using double return value.
     25        * html/NumberInputType.cpp:
     26        (WebCore::NumberInputType::typeMismatchFor): Changed for using double return value.
     27        (WebCore::NumberInputType::sizeShouldIncludeDecoration): Changed for using double return value.
     28        (WebCore::NumberInputType::parseToDouble): Changed for using double return value.
     29        (WebCore::NumberInputType::parseToDoubleWithDecimalPlaces): Changed for using double return value.
     30        (WebCore::NumberInputType::visibleValue): Changed for using double return value.
     31        (WebCore::NumberInputType::sanitizeValue): Changed for using double return value.
     32        * html/RangeInputType.cpp:
     33        (WebCore::RangeInputType::parseToDouble): Changed for using double return value.
     34        * html/StepRange.cpp:
     35        (WebCore::StepRange::parseStep): Changed for using double return value.
     36        * html/StepRange.h:
     37        (WebCore::StepRange::defaultValue): Added "const" attribute
     38        (WebCore::StepRange::proportionFromValue): Added "const" attribute
     39        (WebCore::StepRange::valueFromProportion): Added "const" attribute
     40        * html/parser/HTMLParserIdioms.cpp:
     41        (WebCore::parseToDoubleForNumberType): Changed for using double return value. Added one parameter function.
     42        (WebCore::parseToDoubleForNumberTypeWithDecimalPlaces): Changed for using double return value. Added function for providing default fallback value.
     43        * html/parser/HTMLParserIdioms.h: Changed function prototype and added one parameter prototypes.
     44        * html/shadow/SliderThumbElement.cpp:
     45        (WebCore::sliderPosition): Changed for using double return value.
     46
    1472012-05-22  Kentaro Hara  <haraken@chromium.org>
    248
  • trunk/Source/WebCore/html/HTMLMeterElement.cpp

    r117195 r117929  
    8282double HTMLMeterElement::min() const
    8383{
    84     double min = 0;
    85     parseToDoubleForNumberType(getAttribute(minAttr), &min);
    86     return min;
     84    return parseToDoubleForNumberType(getAttribute(minAttr), 0);
    8785}
    8886
     
    9896double HTMLMeterElement::max() const
    9997{
    100     double max = std::max(1.0, min());
    101     parseToDoubleForNumberType(getAttribute(maxAttr), &max);
    102     return std::max(max, min());
     98    return std::max(parseToDoubleForNumberType(getAttribute(maxAttr), std::max(1.0, min())), min());
    10399}
    104100
     
    114110double HTMLMeterElement::value() const
    115111{
    116     double value = 0;
    117     parseToDoubleForNumberType(getAttribute(valueAttr), &value);
     112    double value = parseToDoubleForNumberType(getAttribute(valueAttr), 0);
    118113    return std::min(std::max(value, min()), max());
    119114}
     
    130125double HTMLMeterElement::low() const
    131126{
    132     double low = min();
    133     parseToDoubleForNumberType(getAttribute(lowAttr), &low);
     127    double low = parseToDoubleForNumberType(getAttribute(lowAttr), min());
    134128    return std::min(std::max(low, min()), max());
    135129}
     
    146140double HTMLMeterElement::high() const
    147141{
    148     double high = max();
    149     parseToDoubleForNumberType(getAttribute(highAttr), &high);
     142    double high = parseToDoubleForNumberType(getAttribute(highAttr), max());
    150143    return std::min(std::max(high, low()), max());
    151144}
     
    162155double HTMLMeterElement::optimum() const
    163156{
    164     double optimum = (max() + min()) / 2;
    165     parseToDoubleForNumberType(getAttribute(optimumAttr), &optimum);
     157    double optimum = parseToDoubleForNumberType(getAttribute(optimumAttr), (max() + min()) / 2);
    166158    return std::min(std::max(optimum, min()), max());
    167159}
  • trunk/Source/WebCore/html/HTMLProgressElement.cpp

    r117195 r117929  
    9292double HTMLProgressElement::value() const
    9393{
    94     double value;
    95     bool ok = parseToDoubleForNumberType(fastGetAttribute(valueAttr), &value);
    96     if (!ok || value < 0)
    97         return 0;
    98     return (value > max()) ? max() : value;
     94    double value = parseToDoubleForNumberType(fastGetAttribute(valueAttr));
     95    return !isfinite(value) || value < 0 ? 0 : std::min(value, max());
    9996}
    10097
     
    110107double HTMLProgressElement::max() const
    111108{
    112     double max;
    113     bool ok = parseToDoubleForNumberType(getAttribute(maxAttr), &max);
    114     if (!ok || max <= 0)
    115         return 1;
    116     return max;
     109    double max = parseToDoubleForNumberType(getAttribute(maxAttr));
     110    return !isfinite(max) || max <= 0 ? 1 : max;
    117111}
    118112
  • trunk/Source/WebCore/html/NumberInputType.cpp

    r117738 r117929  
    100100bool NumberInputType::typeMismatchFor(const String& value) const
    101101{
    102     return !value.isEmpty() && !parseToDoubleForNumberType(value, 0);
     102    return !value.isEmpty() && !isfinite(parseToDoubleForNumberType(value));
    103103}
    104104
     
    128128
    129129    unsigned minValueDecimalPlaces;
    130     double minValueDouble;
    131130    String minValue = element()->fastGetAttribute(minAttr);
    132     if (!parseToDoubleForNumberTypeWithDecimalPlaces(minValue, &minValueDouble, &minValueDecimalPlaces))
     131    double minValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(minValue, &minValueDecimalPlaces);
     132    if (!isfinite(minValueDouble))
    133133        return false;
    134134
    135135    unsigned maxValueDecimalPlaces;
    136     double maxValueDouble;
    137136    String maxValue = element()->fastGetAttribute(maxAttr);
    138     if (!parseToDoubleForNumberTypeWithDecimalPlaces(maxValue, &maxValueDouble, &maxValueDecimalPlaces))
     137    double maxValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(maxValue, &maxValueDecimalPlaces);
     138    if (!isfinite(maxValueDouble))
    139139        return false;
    140140
     
    144144    }
    145145
    146     unsigned stepValueDecimalPlaces;
    147     double stepValueDouble;
    148146    String stepValue = element()->fastGetAttribute(stepAttr);
    149147    if (equalIgnoringCase(stepValue, "any"))
    150148        return false;
    151     if (!parseToDoubleForNumberTypeWithDecimalPlaces(stepValue, &stepValueDouble, &stepValueDecimalPlaces)) {
     149    unsigned stepValueDecimalPlaces;
     150    double stepValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(stepValue, &stepValueDecimalPlaces);
     151    if (!isfinite(stepValueDouble)) {
    152152        stepValueDouble = 1;
    153153        stepValueDecimalPlaces = 0;
     
    189189double NumberInputType::parseToDouble(const String& src, double defaultValue) const
    190190{
    191     double numberValue;
    192     if (!parseToDoubleForNumberType(src, &numberValue))
    193         return defaultValue;
    194     ASSERT(isfinite(numberValue));
    195     return numberValue;
     191    return parseToDoubleForNumberType(src, defaultValue);
    196192}
    197193
    198194double NumberInputType::parseToDoubleWithDecimalPlaces(const String& src, double defaultValue, unsigned *decimalPlaces) const
    199195{
    200     double numberValue;
    201     if (!parseToDoubleForNumberTypeWithDecimalPlaces(src, &numberValue, decimalPlaces))
    202         return defaultValue;
    203     ASSERT(isfinite(numberValue));
    204     return numberValue;
     196    return parseToDoubleForNumberTypeWithDecimalPlaces(src, decimalPlaces, defaultValue);
    205197}
    206198
     
    237229    // FIXME: The following three lines should be removed when we
    238230    // remove the second argument of convertToLocalizedNumber().
    239     double doubleValue = numeric_limits<double>::quiet_NaN();
     231    // Note: parseToDoubleForNumberTypeWithDecimalPlaces set zero to decimalPlaces
     232    // if currentValue isn't valid floating pointer number.
    240233    unsigned decimalPlace;
    241     parseToDoubleForNumberTypeWithDecimalPlaces(currentValue, &doubleValue, &decimalPlace);
     234    parseToDoubleForNumberTypeWithDecimalPlaces(currentValue, &decimalPlace);
    242235    return convertToLocalizedNumber(currentValue, decimalPlace);
    243236}
     
    263256    if (proposedValue.isEmpty())
    264257        return proposedValue;
    265     return parseToDoubleForNumberType(proposedValue, 0) ? proposedValue : emptyAtom.string();
     258    return isfinite(parseToDoubleForNumberType(proposedValue)) ? proposedValue : emptyString();
    266259}
    267260
  • trunk/Source/WebCore/html/RangeInputType.cpp

    r117738 r117929  
    222222double RangeInputType::parseToDouble(const String& src, double defaultValue) const
    223223{
    224     double numberValue;
    225     if (!parseToDoubleForNumberType(src, &numberValue))
    226         return defaultValue;
    227     ASSERT(isfinite(numberValue));
    228     return numberValue;
     224    return parseToDoubleForNumberType(src, defaultValue);
    229225}
    230226
  • trunk/Source/WebCore/html/StepRange.cpp

    r117743 r117929  
    123123
    124124    DoubleWithDecimalPlacesOrMissing step(0);
    125     if (!parseToDoubleForNumberTypeWithDecimalPlaces(stepString, &step.value.value, &step.value.decimalPlaces) || step.value.value <= 0.0)
     125    step.value.value = parseToDoubleForNumberTypeWithDecimalPlaces(stepString, &step.value.decimalPlaces);
     126    if (!isfinite(step.value.value) || step.value.value <= 0.0)
    126127        return DoubleWithDecimalPlacesOrMissing(stepDescription.defaultValue());
    127128
  • trunk/Source/WebCore/html/StepRange.h

    r117739 r117929  
    107107
    108108    // Clamp the middle value according to the step
    109     double defaultValue()
     109    double defaultValue() const
    110110    {
    111111        return clampValue((m_minimum + m_maximum) / 2);
     
    113113
    114114    // Map value into 0-1 range
    115     double proportionFromValue(double value)
     115    double proportionFromValue(double value) const
    116116    {
    117117        if (m_minimum == m_maximum)
     
    122122
    123123    // Map from 0-1 range to value
    124     double valueFromProportion(double proportion)
     124    double valueFromProportion(double proportion) const
    125125    {
    126126        return m_minimum + proportion * (m_maximum - m_minimum);
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp

    r113513 r117929  
    6767}
    6868
    69 bool parseToDoubleForNumberType(const String& string, double* result)
     69double parseToDoubleForNumberType(const String& string, double fallbackValue)
    7070{
    7171    // See HTML5 2.5.4.3 `Real numbers.'
     
    7474    UChar firstCharacter = string[0];
    7575    if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCharacter))
    76         return false;
     76        return fallbackValue;
    7777
    7878    bool valid = false;
    7979    double value = string.toDouble(&valid);
    8080    if (!valid)
    81         return false;
     81        return fallbackValue;
    8282
    8383    // NaN and infinity are considered valid by String::toDouble, but not valid here.
    8484    if (!isfinite(value))
    85         return false;
     85        return fallbackValue;
    8686
    8787    // Numbers are considered finite IEEE 754 single-precision floating point values.
    8888    // See HTML5 2.5.4.3 `Real numbers.'
    8989    if (-std::numeric_limits<float>::max() > value || value > std::numeric_limits<float>::max())
    90         return false;
    91 
    92     if (result) {
    93         // The following expression converts -0 to +0.
    94         *result = value ? value : 0;
    95     }
    96 
    97     return true;
    98 }
    99 
    100 bool parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, double *result, unsigned *decimalPlaces)
     90        return fallbackValue;
     91
     92    // The following expression converts -0 to +0.
     93    return value ? value : 0;
     94}
     95
     96double parseToDoubleForNumberType(const String& string)
     97{
     98    return parseToDoubleForNumberType(string, std::numeric_limits<double>::quiet_NaN());
     99}
     100
     101double parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, unsigned *decimalPlaces, double fallbackValue)
    101102{
    102103    if (decimalPlaces)
    103104        *decimalPlaces = 0;
    104105
    105     if (!parseToDoubleForNumberType(string, result))
    106         return false;
     106    double value = parseToDoubleForNumberType(string, std::numeric_limits<double>::quiet_NaN());
     107    if (!isfinite(value))
     108        return fallbackValue;
    107109
    108110    if (!decimalPlaces)
    109         return true;
     111        return value;
    110112
    111113    size_t dotIndex = string.find('.');
     
    167169        *decimalPlaces = static_cast<unsigned>(intDecimalPlaces);
    168170
    169     return true;
     171    return value;
     172}
     173
     174double parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, unsigned *decimalPlaces)
     175{
     176    return parseToDoubleForNumberTypeWithDecimalPlaces(string, decimalPlaces, std::numeric_limits<double>::quiet_NaN());
    170177}
    171178
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.h

    r91601 r117929  
    4545// Leading or trailing illegal characters cause failure, as does passing an empty string.
    4646// The double* parameter may be 0 to check if the string can be parsed without getting the result.
    47 bool parseToDoubleForNumberType(const String&, double*);
    48 bool parseToDoubleForNumberTypeWithDecimalPlaces(const String&, double*, unsigned*);
     47double parseToDoubleForNumberType(const String&);
     48double parseToDoubleForNumberType(const String&, double fallbackValue);
     49double parseToDoubleForNumberTypeWithDecimalPlaces(const String&, unsigned*);
     50double parseToDoubleForNumberTypeWithDecimalPlaces(const String&, unsigned*, double fallbackValue);
    4951
    5052// http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
  • trunk/Source/WebCore/html/shadow/SliderThumbElement.cpp

    r117738 r117929  
    5454inline static double sliderPosition(HTMLInputElement* element)
    5555{
    56     StepRange stepRange(element->createStepRange(RejectAny));
    57 
    58     double oldValue;
    59     bool parseSuccess = parseToDoubleForNumberType(element->value(), &oldValue);
    60     if (!parseSuccess)
    61         oldValue = stepRange.defaultValue();
    62     double newValue = stepRange.clampValue(oldValue);
    63     return stepRange.proportionFromValue(newValue);
     56    const StepRange stepRange(element->createStepRange(RejectAny));
     57    const double oldValue = parseToDoubleForNumberType(element->value(), stepRange.defaultValue());
     58    return stepRange.proportionFromValue(stepRange.clampValue(oldValue));
    6459}
    6560
Note: See TracChangeset for help on using the changeset viewer.