2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "HTMLParserIdioms.h"
29 #include <wtf/MathExtras.h>
31 #include <wtf/text/AtomicString.h>
32 #include <wtf/text/StringBuilder.h>
36 String stripLeadingAndTrailingHTMLSpaces(const String& string)
38 const UChar* characters = string.characters();
39 unsigned length = string.length();
41 unsigned numLeadingSpaces;
42 for (numLeadingSpaces = 0; numLeadingSpaces < length; ++numLeadingSpaces) {
43 if (isNotHTMLSpace(characters[numLeadingSpaces]))
47 if (numLeadingSpaces == length)
48 return string.isNull() ? string : emptyAtom.string();
50 unsigned numTrailingSpaces;
51 for (numTrailingSpaces = 0; numTrailingSpaces < length; ++numTrailingSpaces) {
52 if (isNotHTMLSpace(characters[length - numTrailingSpaces - 1]))
56 ASSERT(numLeadingSpaces + numTrailingSpaces < length);
58 return string.substring(numLeadingSpaces, length - (numLeadingSpaces + numTrailingSpaces));
61 String serializeForNumberType(double number)
63 // According to HTML5, "the best representation of the number n as a floating
64 // point number" is a string produced by applying ToString() to n.
65 NumberToStringBuffer buffer;
66 return String(numberToString(number, buffer));
69 bool parseToDoubleForNumberType(const String& string, double* result)
71 // See HTML5 2.4.4.3 `Real numbers.'
73 // String::toDouble() accepts leading + and whitespace characters, which are not valid here.
74 UChar firstCharacter = string[0];
75 if (firstCharacter != '-' && !isASCIIDigit(firstCharacter))
79 double value = string.toDouble(&valid);
83 // NaN and infinity are considered valid by String::toDouble, but not valid here.
87 // Numbers are considered finite IEEE 754 single-precision floating point values.
88 // See HTML5 2.4.4.3 `Real numbers.'
89 if (-std::numeric_limits<float>::max() > value || value > std::numeric_limits<float>::max())
93 // The following expression converts -0 to +0.
94 *result = value ? value : 0;
100 bool parseToDoubleForNumberTypeWithDecimalPlaces(const String& string, double *result, unsigned *decimalPlaces)
105 if (!parseToDoubleForNumberType(string, result))
111 size_t dotIndex = string.find('.');
112 size_t eIndex = string.find('e');
113 if (eIndex == notFound)
114 eIndex = string.find('E');
116 unsigned baseDecimalPlaces = 0;
117 if (dotIndex != notFound) {
118 if (eIndex == notFound)
119 baseDecimalPlaces = string.length() - dotIndex - 1;
121 baseDecimalPlaces = eIndex - dotIndex - 1;
125 if (eIndex != notFound) {
126 unsigned cursor = eIndex + 1, cursorSaved;
127 int digit, exponentSign;
129 size_t length = string.length();
131 // Not using String.toInt() in order to perform the same computation as dtoa() does.
133 switch (digit = string[cursor]) {
137 digit = string[++cursor];
139 if (digit >= '0' && digit <= '9') {
140 while (cursor < length && digit == '0')
141 digit = string[++cursor];
142 if (digit > '0' && digit <= '9') {
143 exponent32 = digit - '0';
144 cursorSaved = cursor;
145 while (cursor < length && (digit = string[++cursor]) >= '0' && digit <= '9')
146 exponent32 = (10 * exponent32) + digit - '0';
147 if (cursor - cursorSaved > 8 || exponent32 > 19999)
148 /* Avoid confusion from exponents
149 * so large that e might overflow.
151 exponent = 19999; /* safe for 16 bit ints */
153 exponent = static_cast<int>(exponent32);
155 exponent = -exponent;
161 int intDecimalPlaces = baseDecimalPlaces - exponent;
162 if (intDecimalPlaces < 0)
164 else if (intDecimalPlaces > 19999)
165 *decimalPlaces = 19999;
167 *decimalPlaces = static_cast<unsigned>(intDecimalPlaces);
172 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
173 bool parseHTMLInteger(const String& input, int& value)
177 const UChar* position = input.characters();
178 const UChar* end = position + input.length();
184 while (position < end) {
185 if (!isHTMLSpace(*position))
193 ASSERT(position < end);
196 if (*position == '-') {
199 } else if (*position == '+')
203 ASSERT(position < end);
206 if (!isASCIIDigit(*position))
210 StringBuilder digits;
211 while (position < end) {
212 if (!isASCIIDigit(*position))
214 digits.append(*position++);
218 value = sign * charactersToIntStrict(digits.characters(), digits.length());
222 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
223 bool parseHTMLNonNegativeInteger(const String& input, unsigned int& value)
227 const UChar* position = input.characters();
228 const UChar* end = position + input.length();
231 while (position < end) {
232 if (!isHTMLSpace(*position))
240 ASSERT(position < end);
243 if (*position == '+')
249 ASSERT(position < end);
252 if (!isASCIIDigit(*position))
256 StringBuilder digits;
257 while (position < end) {
258 if (!isASCIIDigit(*position))
260 digits.append(*position++);
264 value = charactersToUIntStrict(digits.characters(), digits.length());