Changeset 117929 in webkit
- Timestamp:
- May 22, 2012 12:53:22 AM (12 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r117928 r117929 1 2012-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 1 47 2012-05-22 Kentaro Hara <haraken@chromium.org> 2 48 -
trunk/Source/WebCore/html/HTMLMeterElement.cpp
r117195 r117929 82 82 double HTMLMeterElement::min() const 83 83 { 84 double min = 0; 85 parseToDoubleForNumberType(getAttribute(minAttr), &min); 86 return min; 84 return parseToDoubleForNumberType(getAttribute(minAttr), 0); 87 85 } 88 86 … … 98 96 double HTMLMeterElement::max() const 99 97 { 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()); 103 99 } 104 100 … … 114 110 double HTMLMeterElement::value() const 115 111 { 116 double value = 0; 117 parseToDoubleForNumberType(getAttribute(valueAttr), &value); 112 double value = parseToDoubleForNumberType(getAttribute(valueAttr), 0); 118 113 return std::min(std::max(value, min()), max()); 119 114 } … … 130 125 double HTMLMeterElement::low() const 131 126 { 132 double low = min(); 133 parseToDoubleForNumberType(getAttribute(lowAttr), &low); 127 double low = parseToDoubleForNumberType(getAttribute(lowAttr), min()); 134 128 return std::min(std::max(low, min()), max()); 135 129 } … … 146 140 double HTMLMeterElement::high() const 147 141 { 148 double high = max(); 149 parseToDoubleForNumberType(getAttribute(highAttr), &high); 142 double high = parseToDoubleForNumberType(getAttribute(highAttr), max()); 150 143 return std::min(std::max(high, low()), max()); 151 144 } … … 162 155 double HTMLMeterElement::optimum() const 163 156 { 164 double optimum = (max() + min()) / 2; 165 parseToDoubleForNumberType(getAttribute(optimumAttr), &optimum); 157 double optimum = parseToDoubleForNumberType(getAttribute(optimumAttr), (max() + min()) / 2); 166 158 return std::min(std::max(optimum, min()), max()); 167 159 } -
trunk/Source/WebCore/html/HTMLProgressElement.cpp
r117195 r117929 92 92 double HTMLProgressElement::value() const 93 93 { 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()); 99 96 } 100 97 … … 110 107 double HTMLProgressElement::max() const 111 108 { 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; 117 111 } 118 112 -
trunk/Source/WebCore/html/NumberInputType.cpp
r117738 r117929 100 100 bool NumberInputType::typeMismatchFor(const String& value) const 101 101 { 102 return !value.isEmpty() && ! parseToDoubleForNumberType(value, 0);102 return !value.isEmpty() && !isfinite(parseToDoubleForNumberType(value)); 103 103 } 104 104 … … 128 128 129 129 unsigned minValueDecimalPlaces; 130 double minValueDouble;131 130 String minValue = element()->fastGetAttribute(minAttr); 132 if (!parseToDoubleForNumberTypeWithDecimalPlaces(minValue, &minValueDouble, &minValueDecimalPlaces)) 131 double minValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(minValue, &minValueDecimalPlaces); 132 if (!isfinite(minValueDouble)) 133 133 return false; 134 134 135 135 unsigned maxValueDecimalPlaces; 136 double maxValueDouble;137 136 String maxValue = element()->fastGetAttribute(maxAttr); 138 if (!parseToDoubleForNumberTypeWithDecimalPlaces(maxValue, &maxValueDouble, &maxValueDecimalPlaces)) 137 double maxValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(maxValue, &maxValueDecimalPlaces); 138 if (!isfinite(maxValueDouble)) 139 139 return false; 140 140 … … 144 144 } 145 145 146 unsigned stepValueDecimalPlaces;147 double stepValueDouble;148 146 String stepValue = element()->fastGetAttribute(stepAttr); 149 147 if (equalIgnoringCase(stepValue, "any")) 150 148 return false; 151 if (!parseToDoubleForNumberTypeWithDecimalPlaces(stepValue, &stepValueDouble, &stepValueDecimalPlaces)) { 149 unsigned stepValueDecimalPlaces; 150 double stepValueDouble = parseToDoubleForNumberTypeWithDecimalPlaces(stepValue, &stepValueDecimalPlaces); 151 if (!isfinite(stepValueDouble)) { 152 152 stepValueDouble = 1; 153 153 stepValueDecimalPlaces = 0; … … 189 189 double NumberInputType::parseToDouble(const String& src, double defaultValue) const 190 190 { 191 double numberValue; 192 if (!parseToDoubleForNumberType(src, &numberValue)) 193 return defaultValue; 194 ASSERT(isfinite(numberValue)); 195 return numberValue; 191 return parseToDoubleForNumberType(src, defaultValue); 196 192 } 197 193 198 194 double NumberInputType::parseToDoubleWithDecimalPlaces(const String& src, double defaultValue, unsigned *decimalPlaces) const 199 195 { 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); 205 197 } 206 198 … … 237 229 // FIXME: The following three lines should be removed when we 238 230 // 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. 240 233 unsigned decimalPlace; 241 parseToDoubleForNumberTypeWithDecimalPlaces(currentValue, &d oubleValue, &decimalPlace);234 parseToDoubleForNumberTypeWithDecimalPlaces(currentValue, &decimalPlace); 242 235 return convertToLocalizedNumber(currentValue, decimalPlace); 243 236 } … … 263 256 if (proposedValue.isEmpty()) 264 257 return proposedValue; 265 return parseToDoubleForNumberType(proposedValue, 0) ? proposedValue : emptyAtom.string();258 return isfinite(parseToDoubleForNumberType(proposedValue)) ? proposedValue : emptyString(); 266 259 } 267 260 -
trunk/Source/WebCore/html/RangeInputType.cpp
r117738 r117929 222 222 double RangeInputType::parseToDouble(const String& src, double defaultValue) const 223 223 { 224 double numberValue; 225 if (!parseToDoubleForNumberType(src, &numberValue)) 226 return defaultValue; 227 ASSERT(isfinite(numberValue)); 228 return numberValue; 224 return parseToDoubleForNumberType(src, defaultValue); 229 225 } 230 226 -
trunk/Source/WebCore/html/StepRange.cpp
r117743 r117929 123 123 124 124 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) 126 127 return DoubleWithDecimalPlacesOrMissing(stepDescription.defaultValue()); 127 128 -
trunk/Source/WebCore/html/StepRange.h
r117739 r117929 107 107 108 108 // Clamp the middle value according to the step 109 double defaultValue() 109 double defaultValue() const 110 110 { 111 111 return clampValue((m_minimum + m_maximum) / 2); … … 113 113 114 114 // Map value into 0-1 range 115 double proportionFromValue(double value) 115 double proportionFromValue(double value) const 116 116 { 117 117 if (m_minimum == m_maximum) … … 122 122 123 123 // Map from 0-1 range to value 124 double valueFromProportion(double proportion) 124 double valueFromProportion(double proportion) const 125 125 { 126 126 return m_minimum + proportion * (m_maximum - m_minimum); -
trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp
r113513 r117929 67 67 } 68 68 69 bool parseToDoubleForNumberType(const String& string, double* result)69 double parseToDoubleForNumberType(const String& string, double fallbackValue) 70 70 { 71 71 // See HTML5 2.5.4.3 `Real numbers.' … … 74 74 UChar firstCharacter = string[0]; 75 75 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCharacter)) 76 return fal se;76 return fallbackValue; 77 77 78 78 bool valid = false; 79 79 double value = string.toDouble(&valid); 80 80 if (!valid) 81 return fal se;81 return fallbackValue; 82 82 83 83 // NaN and infinity are considered valid by String::toDouble, but not valid here. 84 84 if (!isfinite(value)) 85 return fal se;85 return fallbackValue; 86 86 87 87 // Numbers are considered finite IEEE 754 single-precision floating point values. 88 88 // See HTML5 2.5.4.3 `Real numbers.' 89 89 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 96 double parseToDoubleForNumberType(const String& string) 97 { 98 return parseToDoubleForNumberType(string, std::numeric_limits<double>::quiet_NaN()); 99 } 100 101 double parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, unsigned *decimalPlaces, double fallbackValue) 101 102 { 102 103 if (decimalPlaces) 103 104 *decimalPlaces = 0; 104 105 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; 107 109 108 110 if (!decimalPlaces) 109 return true;111 return value; 110 112 111 113 size_t dotIndex = string.find('.'); … … 167 169 *decimalPlaces = static_cast<unsigned>(intDecimalPlaces); 168 170 169 return true; 171 return value; 172 } 173 174 double parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, unsigned *decimalPlaces) 175 { 176 return parseToDoubleForNumberTypeWithDecimalPlaces(string, decimalPlaces, std::numeric_limits<double>::quiet_NaN()); 170 177 } 171 178 -
trunk/Source/WebCore/html/parser/HTMLParserIdioms.h
r91601 r117929 45 45 // Leading or trailing illegal characters cause failure, as does passing an empty string. 46 46 // 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*); 47 double parseToDoubleForNumberType(const String&); 48 double parseToDoubleForNumberType(const String&, double fallbackValue); 49 double parseToDoubleForNumberTypeWithDecimalPlaces(const String&, unsigned*); 50 double parseToDoubleForNumberTypeWithDecimalPlaces(const String&, unsigned*, double fallbackValue); 49 51 50 52 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers -
trunk/Source/WebCore/html/shadow/SliderThumbElement.cpp
r117738 r117929 54 54 inline static double sliderPosition(HTMLInputElement* element) 55 55 { 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)); 64 59 } 65 60
Note: See TracChangeset
for help on using the changeset viewer.