Skip to content
Permalink
Browse files
Move number parsing code out of HTMLInputElement.
https://bugs.webkit.org/show_bug.cgi?id=38203

Reviewed by Darin Adler.

The numebr parsing code follows HTML5 parsing rules and should be available outside of HTMLInputElement.
No new tests as no new functionality was introduced.

* html/HTMLInputElement.cpp:
* html/HTMLInputElement.h:
* html/HTMLParser.cpp:
* html/HTMLParser.h:
* html/StepRange.cpp:
* html/ValidityState.cpp:
* rendering/RenderSlider.cpp:



Canonical link: https://commits.webkit.org/49890@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@58635 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Yael Aharon committed May 1, 2010
1 parent a11b375 commit 60328f2095f3b31bbe1c810af1da02c4fe07bee3
Showing 8 changed files with 78 additions and 48 deletions.
@@ -1,3 +1,27 @@
2010-05-01 Yael Aharon <yael.aharon@nokia.com>

Reviewed by Darin Adler.

Move number parsing code out of HTMLInputElement.
https://bugs.webkit.org/show_bug.cgi?id=38203

The numebr parsing code follows HTML5 parsing rules and should be available outside of HTMLInputElement.
No new tests as no new functionality was introduced.

* html/HTMLInputElement.cpp:
* html/HTMLInputElement.h:
* html/HTMLParser.cpp:
(WebCore::serializeForNumberType):
(WebCore::parseToDoubleForNumberType):
* html/HTMLParser.h:
* html/StepRange.cpp:
(WebCore::StepRange::clampValue):
(WebCore::StepRange::valueFromElement):
* html/ValidityState.cpp:
(WebCore::ValidityState::typeMismatch):
* rendering/RenderSlider.cpp:
(WebCore::RenderSlider::setValueForPosition):

2010-05-01 Xan Lopez <xlopez@igalia.com>

Reviewed by Oliver Hunt.
@@ -46,6 +46,7 @@
#include "HTMLImageLoader.h"
#include "HTMLNames.h"
#include "HTMLOptionElement.h"
#include "HTMLParser.h"
#include "KeyboardEvent.h"
#include "LocalizedStrings.h"
#include "MappedAttribute.h"
@@ -66,7 +67,6 @@
#include <wtf/HashMap.h>
#include <wtf/MathExtras.h>
#include <wtf/StdLibExtras.h>
#include <wtf/dtoa.h>

using namespace std;

@@ -2628,42 +2628,6 @@ bool HTMLInputElement::recalcWillValidate() const
&& inputType() != HIDDEN && inputType() != BUTTON && inputType() != RESET;
}

String HTMLInputElement::serializeForNumberType(double number)
{
// According to HTML5, "the best representation of the number n as a floating
// point number" is a string produced by applying ToString() to n.
DtoaBuffer buffer;
unsigned length;
doubleToStringInJavaScriptFormat(number, buffer, &length);
return String(buffer, length);
}

bool HTMLInputElement::parseToDoubleForNumberType(const String& src, double* out)
{
// See HTML5 2.4.4.3 `Real numbers.'

if (src.isEmpty())
return false;
// String::toDouble() accepts leading + \t \n \v \f \r and SPACE, which are invalid in HTML5.
// So, check the first character.
if (src[0] != '-' && (src[0] < '0' || src[0] > '9'))
return false;

bool valid = false;
double value = src.toDouble(&valid);
if (!valid)
return false;
// NaN and Infinity are not valid numbers according to the standard.
if (!isfinite(value))
return false;
// -0 -> 0
if (!value)
value = 0;
if (out)
*out = value;
return true;
}

bool HTMLInputElement::parseToDateComponents(InputType type, const String& formString, DateComponents* out)
{
if (formString.isEmpty())
@@ -264,13 +264,6 @@ class HTMLInputElement : public HTMLTextFormControlElement, public InputElement

virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;

// Converts the specified string to a floating number.
// If the conversion fails, the return value is false. Take care that leading or trailing unnecessary characters make failures. This returns false for an empty string input.
// The double* parameter may be 0.
static bool parseToDoubleForNumberType(const String&, double*);
// Converts the specified number to a string. This is an implementation of
// HTML5's "algorithm to convert a number to a string" for NUMBER/RANGE types.
static String serializeForNumberType(double);
// Parses the specified string as the InputType, and returns true if it is successfully parsed.
// An instance pointed by the DateComponents* parameter will have parsed values and be
// modified even if the parsing fails. The DateComponents* parameter may be 0.
@@ -59,6 +59,7 @@
#include "Text.h"
#include "TreeDepthLimit.h"
#include <wtf/StdLibExtras.h>
#include <wtf/dtoa.h>

namespace WebCore {

@@ -1739,4 +1740,41 @@ bool shouldCreateImplicitHead(Document* document)
}
#endif


String serializeForNumberType(double number)
{
// According to HTML5, "the best representation of the number n as a floating
// point number" is a string produced by applying ToString() to n.
DtoaBuffer buffer;
unsigned length;
doubleToStringInJavaScriptFormat(number, buffer, &length);
return String(buffer, length);
}

bool parseToDoubleForNumberType(const String& src, double* out)
{
// See HTML5 2.4.4.3 `Real numbers.'

if (src.isEmpty())
return false;
// String::toDouble() accepts leading + \t \n \v \f \r and SPACE, which are invalid in HTML5.
// So, check the first character.
if (src[0] != '-' && (src[0] < '0' || src[0] > '9'))
return false;

bool valid = false;
double value = src.toDouble(&valid);
if (!valid)
return false;
// NaN and Infinity are not valid numbers according to the standard.
if (!isfinite(value))
return false;
// -0 -> 0
if (!value)
value = 0;
if (out)
*out = value;
return true;
}

}
@@ -201,6 +201,14 @@ bool shouldCreateImplicitHead(Document*);
inline bool shouldCreateImplicitHead(Document*) { return true; }
#endif

// Converts the specified string to a floating number.
// If the conversion fails, the return value is false. Take care that leading or trailing unnecessary characters make failures. This returns false for an empty string input.
// The double* parameter may be 0.
bool parseToDoubleForNumberType(const String&, double*);
// Converts the specified number to a string. This is an implementation of
// HTML5's "algorithm to convert a number to a string" for NUMBER/RANGE types.
String serializeForNumberType(double);

}

#endif // HTMLParser_h
@@ -23,6 +23,7 @@

#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLParser.h"
#include "PlatformString.h"
#include <wtf/MathExtras.h>

@@ -61,7 +62,7 @@ double StepRange::clampValue(double value)
double StepRange::clampValue(const String& stringValue)
{
double value;
bool parseSuccess = HTMLInputElement::parseToDoubleForNumberType(stringValue, &value);
bool parseSuccess = parseToDoubleForNumberType(stringValue, &value);
if (!parseSuccess)
value = (minimum + maximum) / 2;
return clampValue(value);
@@ -70,7 +71,7 @@ double StepRange::clampValue(const String& stringValue)
double StepRange::valueFromElement(HTMLInputElement* element, bool* wasClamped)
{
double oldValue;
bool parseSuccess = HTMLInputElement::parseToDoubleForNumberType(element->value(), &oldValue);
bool parseSuccess = parseToDoubleForNumberType(element->value(), &oldValue);
if (!parseSuccess)
oldValue = (minimum + maximum) / 2;
double newValue = clampValue(oldValue);
@@ -26,6 +26,7 @@

#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLParser.h"
#include "KURL.h"
#include "LocalizedStrings.h"
#include "RegularExpression.h"
@@ -80,7 +81,7 @@ bool ValidityState::typeMismatch() const
case HTMLInputElement::COLOR:
return !isValidColorString(value);
case HTMLInputElement::NUMBER:
return !HTMLInputElement::parseToDoubleForNumberType(value, 0);
return !parseToDoubleForNumberType(value, 0);
case HTMLInputElement::URL:
return !KURL(KURL(), value).isValid();
case HTMLInputElement::EMAIL: {
@@ -30,6 +30,7 @@
#include "HTMLInputElement.h"
#include "HTMLDivElement.h"
#include "HTMLNames.h"
#include "HTMLParser.h"
#include "MediaControlElements.h"
#include "MouseEvent.h"
#include "RenderLayer.h"
@@ -356,7 +357,7 @@ void RenderSlider::setValueForPosition(int position)
if (style()->appearance() == SliderVerticalPart || style()->appearance() == MediaVolumeSliderPart)
fraction = 1 - fraction;
double value = range.clampValue(range.valueFromProportion(fraction));
element->setValueFromRenderer(HTMLInputElement::serializeForNumberType(value));
element->setValueFromRenderer(serializeForNumberType(value));

// Also update the position if appropriate.
if (position != currentPosition()) {

0 comments on commit 60328f2

Please sign in to comment.