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 "HTMLIdentifier.h"
30 #include "QualifiedName.h"
32 #include <wtf/MathExtras.h>
33 #include <wtf/text/AtomicString.h>
34 #include <wtf/text/StringBuilder.h>
38 template <typename CharType>
39 static String stripLeadingAndTrailingHTMLSpaces(String string, CharType characters, unsigned length)
41 unsigned numLeadingSpaces = 0;
42 unsigned numTrailingSpaces = 0;
44 for (; numLeadingSpaces < length; ++numLeadingSpaces) {
45 if (isNotHTMLSpace(characters[numLeadingSpaces]))
49 if (numLeadingSpaces == length)
50 return string.isNull() ? string : emptyAtom.string();
52 for (; numTrailingSpaces < length; ++numTrailingSpaces) {
53 if (isNotHTMLSpace(characters[length - numTrailingSpaces - 1]))
57 ASSERT(numLeadingSpaces + numTrailingSpaces < length);
59 if (!(numLeadingSpaces | numTrailingSpaces))
62 return string.substring(numLeadingSpaces, length - (numLeadingSpaces + numTrailingSpaces));
65 String stripLeadingAndTrailingHTMLSpaces(const String& string)
67 unsigned length = string.length();
70 return string.isNull() ? string : emptyAtom.string();
73 return stripLeadingAndTrailingHTMLSpaces(string, string.characters8(), length);
75 return stripLeadingAndTrailingHTMLSpaces(string, string.characters(), length);
78 String serializeForNumberType(const Decimal& number)
80 if (number.isZero()) {
81 // Decimal::toString appends exponent, e.g. "0e-18"
82 return number.isNegative() ? "-0" : "0";
84 return number.toString();
87 String serializeForNumberType(double number)
89 // According to HTML5, "the best representation of the number n as a floating
90 // point number" is a string produced by applying ToString() to n.
91 return String::numberToStringECMAScript(number);
94 Decimal parseToDecimalForNumberType(const String& string, const Decimal& fallbackValue)
96 // See HTML5 2.5.4.3 `Real numbers.' and parseToDoubleForNumberType
98 // String::toDouble() accepts leading + and whitespace characters, which are not valid here.
99 const UChar firstCharacter = string[0];
100 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCharacter))
101 return fallbackValue;
103 const Decimal value = Decimal::fromString(string);
104 if (!value.isFinite())
105 return fallbackValue;
107 // Numbers are considered finite IEEE 754 single-precision floating point values.
108 // See HTML5 2.5.4.3 `Real numbers.'
109 // FIXME: We should use numeric_limits<double>::max for number input type.
110 const Decimal floatMax = Decimal::fromDouble(std::numeric_limits<float>::max());
111 if (value < -floatMax || value > floatMax)
112 return fallbackValue;
114 // We return +0 for -0 case.
115 return value.isZero() ? Decimal(0) : value;
118 Decimal parseToDecimalForNumberType(const String& string)
120 return parseToDecimalForNumberType(string, Decimal::nan());
123 double parseToDoubleForNumberType(const String& string, double fallbackValue)
125 // See HTML5 2.5.4.3 `Real numbers.'
127 // String::toDouble() accepts leading + and whitespace characters, which are not valid here.
128 UChar firstCharacter = string[0];
129 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCharacter))
130 return fallbackValue;
133 double value = string.toDouble(&valid);
135 return fallbackValue;
137 // NaN and infinity are considered valid by String::toDouble, but not valid here.
138 if (!std::isfinite(value))
139 return fallbackValue;
141 // Numbers are considered finite IEEE 754 single-precision floating point values.
142 // See HTML5 2.5.4.3 `Real numbers.'
143 if (-std::numeric_limits<float>::max() > value || value > std::numeric_limits<float>::max())
144 return fallbackValue;
146 // The following expression converts -0 to +0.
147 return value ? value : 0;
150 double parseToDoubleForNumberType(const String& string)
152 return parseToDoubleForNumberType(string, std::numeric_limits<double>::quiet_NaN());
155 template <typename CharacterType>
156 static bool parseHTMLIntegerInternal(const CharacterType* position, const CharacterType* end, int& value)
162 while (position < end) {
163 if (!isHTMLSpace(*position))
171 ASSERT(position < end);
174 if (*position == '-') {
177 } else if (*position == '+')
181 ASSERT(position < end);
184 if (!isASCIIDigit(*position))
188 StringBuilder digits;
189 while (position < end) {
190 if (!isASCIIDigit(*position))
192 digits.append(*position++);
198 value = sign * charactersToIntStrict(digits.characters8(), digits.length(), &ok);
200 value = sign * charactersToIntStrict(digits.characters16(), digits.length(), &ok);
204 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers
205 bool parseHTMLInteger(const String& input, int& value)
209 unsigned length = input.length();
210 if (length && input.is8Bit()) {
211 const LChar* start = input.characters8();
212 return parseHTMLIntegerInternal(start, start + length, value);
215 const UChar* start = input.characters();
216 return parseHTMLIntegerInternal(start, start + length, value);
219 template <typename CharacterType>
220 static bool parseHTMLNonNegativeIntegerInternal(const CharacterType* position, const CharacterType* end, unsigned& value)
223 while (position < end) {
224 if (!isHTMLSpace(*position))
232 ASSERT(position < end);
235 if (*position == '+')
241 ASSERT(position < end);
244 if (!isASCIIDigit(*position))
248 StringBuilder digits;
249 while (position < end) {
250 if (!isASCIIDigit(*position))
252 digits.append(*position++);
258 value = charactersToUIntStrict(digits.characters8(), digits.length(), &ok);
260 value = charactersToUIntStrict(digits.characters16(), digits.length(), &ok);
265 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-non-negative-integers
266 bool parseHTMLNonNegativeInteger(const String& input, unsigned& value)
270 unsigned length = input.length();
271 if (length && input.is8Bit()) {
272 const LChar* start = input.characters8();
273 return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
276 const UChar* start = input.characters();
277 return parseHTMLNonNegativeIntegerInternal(start, start + length, value);
280 static bool threadSafeEqual(const StringImpl* a, const StringImpl* b)
284 if (a->hash() != b->hash())
286 return equalNonNull(a, b);
289 bool threadSafeMatch(const QualifiedName& a, const QualifiedName& b)
291 return threadSafeEqual(a.localName().impl(), b.localName().impl());
294 #if ENABLE(THREADED_HTML_PARSER)
295 bool threadSafeMatch(const HTMLIdentifier& localName, const QualifiedName& qName)
297 return threadSafeEqual(localName.asStringImpl(), qName.localName().impl());