OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 AN
Y | 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 O
N | 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 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 | 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. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 */ | 23 */ |
24 | 24 |
25 #include "core/html/parser/HTMLParserIdioms.h" | 25 #include "core/html/parser/HTMLParserIdioms.h" |
26 | 26 |
27 #include "core/HTMLNames.h" | 27 #include "core/HTMLNames.h" |
28 #include <limits> | 28 #include "platform/ParsingUtilities.h" |
29 #include "wtf/MathExtras.h" | 29 #include "wtf/MathExtras.h" |
30 #include "wtf/text/AtomicString.h" | 30 #include "wtf/text/AtomicString.h" |
31 #include "wtf/text/StringBuilder.h" | 31 #include "wtf/text/StringBuilder.h" |
32 #include "wtf/text/StringHash.h" | 32 #include "wtf/text/StringHash.h" |
33 #include "wtf/text/TextEncoding.h" | 33 #include "wtf/text/TextEncoding.h" |
34 | 34 |
| 35 #include <limits> |
| 36 |
35 namespace blink { | 37 namespace blink { |
36 | 38 |
37 using namespace HTMLNames; | 39 using namespace HTMLNames; |
38 | 40 |
39 template <typename CharType> | 41 template <typename CharType> |
40 static String stripLeadingAndTrailingHTMLSpaces(String string, const CharType* c
haracters, unsigned length) | 42 static String stripLeadingAndTrailingHTMLSpaces(String string, const CharType* c
haracters, unsigned length) |
41 { | 43 { |
42 unsigned numLeadingSpaces = 0; | 44 unsigned numLeadingSpaces = 0; |
43 unsigned numTrailingSpaces = 0; | 45 unsigned numTrailingSpaces = 0; |
44 | 46 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 108 |
107 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. | 109 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. |
108 const Decimal doubleMax = Decimal::fromDouble(std::numeric_limits<double>::m
ax()); | 110 const Decimal doubleMax = Decimal::fromDouble(std::numeric_limits<double>::m
ax()); |
109 if (value < -doubleMax || value > doubleMax) | 111 if (value < -doubleMax || value > doubleMax) |
110 return fallbackValue; | 112 return fallbackValue; |
111 | 113 |
112 // We return +0 for -0 case. | 114 // We return +0 for -0 case. |
113 return value.isZero() ? Decimal(0) : value; | 115 return value.isZero() ? Decimal(0) : value; |
114 } | 116 } |
115 | 117 |
116 double parseToDoubleForNumberType(const String& string, double fallbackValue) | 118 static double checkDoubleValue(double value, bool valid, double fallbackValue) |
117 { | 119 { |
118 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers | |
119 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. | |
120 UChar firstCharacter = string[0]; | |
121 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) | |
122 return fallbackValue; | |
123 if (string.endsWith('.')) | |
124 return fallbackValue; | |
125 | |
126 bool valid = false; | |
127 double value = string.toDouble(&valid); | |
128 if (!valid) | 120 if (!valid) |
129 return fallbackValue; | 121 return fallbackValue; |
130 | 122 |
131 // NaN and infinity are considered valid by String::toDouble, but not valid
here. | 123 // NaN and infinity are considered valid by String::toDouble, but not valid
here. |
132 if (!std::isfinite(value)) | 124 if (!std::isfinite(value)) |
133 return fallbackValue; | 125 return fallbackValue; |
134 | 126 |
135 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. | 127 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. |
136 if (-std::numeric_limits<double>::max() > value || value > std::numeric_limi
ts<double>::max()) | 128 if (-std::numeric_limits<double>::max() > value || value > std::numeric_limi
ts<double>::max()) |
137 return fallbackValue; | 129 return fallbackValue; |
138 | 130 |
139 // The following expression converts -0 to +0. | 131 // The following expression converts -0 to +0. |
140 return value ? value : 0; | 132 return value ? value : 0; |
141 } | 133 } |
142 | 134 |
| 135 double parseToDoubleForNumberType(const String& string, double fallbackValue) |
| 136 { |
| 137 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers |
| 138 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. |
| 139 UChar firstCharacter = string[0]; |
| 140 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) |
| 141 return fallbackValue; |
| 142 if (string.endsWith('.')) |
| 143 return fallbackValue; |
| 144 |
| 145 bool valid = false; |
| 146 double value = string.toDouble(&valid); |
| 147 return checkDoubleValue(value, valid, fallbackValue); |
| 148 } |
| 149 |
143 template <typename CharacterType> | 150 template <typename CharacterType> |
144 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac
terType* end, int& value) | 151 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac
terType* end, int& value) |
145 { | 152 { |
146 // Step 3 | 153 // Step 3 |
147 int sign = 1; | 154 int sign = 1; |
148 | 155 |
149 // Step 4 | 156 // Step 4 |
150 while (position < end) { | 157 while (position < end) { |
151 if (!isHTMLSpace<CharacterType>(*position)) | 158 if (!isHTMLSpace<CharacterType>(*position)) |
152 break; | 159 break; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 unsigned length = input.length(); | 265 unsigned length = input.length(); |
259 if (length && input.is8Bit()) { | 266 if (length && input.is8Bit()) { |
260 const LChar* start = input.characters8(); | 267 const LChar* start = input.characters8(); |
261 return parseHTMLNonNegativeIntegerInternal(start, start + length, value)
; | 268 return parseHTMLNonNegativeIntegerInternal(start, start + length, value)
; |
262 } | 269 } |
263 | 270 |
264 const UChar* start = input.characters16(); | 271 const UChar* start = input.characters16(); |
265 return parseHTMLNonNegativeIntegerInternal(start, start + length, value); | 272 return parseHTMLNonNegativeIntegerInternal(start, start + length, value); |
266 } | 273 } |
267 | 274 |
| 275 template<typename CharacterType> |
| 276 static bool isSpaceOrDelimiter(CharacterType c) |
| 277 { |
| 278 return isHTMLSpace(c) || c == ',' || c == ';'; |
| 279 } |
| 280 |
| 281 template<typename CharacterType> |
| 282 static bool isNotSpaceDelimiterOrNumberStart(CharacterType c) |
| 283 { |
| 284 return !(isSpaceOrDelimiter(c) || isASCIIDigit(c) || c == '.' || c == '-'); |
| 285 } |
| 286 |
| 287 template<typename CharacterType> |
| 288 static Vector<double> parseHTMLListOfFloatingPointNumbersInternal( |
| 289 const CharacterType* position, const CharacterType* end) |
| 290 { |
| 291 Vector<double> numbers; |
| 292 skipWhile<CharacterType, isSpaceOrDelimiter>(position, end); |
| 293 |
| 294 while (position < end) { |
| 295 skipWhile<CharacterType, isNotSpaceDelimiterOrNumberStart>(position, end
); |
| 296 |
| 297 const CharacterType* unparsedNumberStart = position; |
| 298 skipUntil<CharacterType, isSpaceOrDelimiter>(position, end); |
| 299 |
| 300 size_t parsedLength = 0; |
| 301 double number = charactersToDouble(unparsedNumberStart, position - unpar
sedNumberStart, parsedLength); |
| 302 numbers.append(checkDoubleValue(number, parsedLength != 0, 0)); |
| 303 |
| 304 skipWhile<CharacterType, isSpaceOrDelimiter>(position, end); |
| 305 } |
| 306 return numbers; |
| 307 } |
| 308 |
| 309 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsing-
a-list-of-floating-point-numbers |
| 310 Vector<double> parseHTMLListOfFloatingPointNumbers(const String& input) |
| 311 { |
| 312 if (input.is8Bit()) |
| 313 return parseHTMLListOfFloatingPointNumbersInternal(input.characters8(),
input.characters8() + input.length()); |
| 314 return parseHTMLListOfFloatingPointNumbersInternal(input.characters16(), inp
ut.characters16() + input.length()); |
| 315 } |
| 316 |
268 static const char charsetString[] = "charset"; | 317 static const char charsetString[] = "charset"; |
269 static const size_t charsetLength = sizeof("charset") - 1; | 318 static const size_t charsetLength = sizeof("charset") - 1; |
270 | 319 |
271 String extractCharset(const String& value) | 320 String extractCharset(const String& value) |
272 { | 321 { |
273 size_t pos = 0; | 322 size_t pos = 0; |
274 unsigned length = value.length(); | 323 unsigned length = value.length(); |
275 | 324 |
276 while (pos < length) { | 325 while (pos < length) { |
277 pos = value.find(charsetString, pos, TextCaseInsensitive); | 326 pos = value.find(charsetString, pos, TextCaseInsensitive); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 string = StringImpl::create8BitIfPossible(characters, size); | 459 string = StringImpl::create8BitIfPossible(characters, size); |
411 else if (width == Force8Bit) | 460 else if (width == Force8Bit) |
412 string = String::make8BitFrom16BitSource(characters, size); | 461 string = String::make8BitFrom16BitSource(characters, size); |
413 else | 462 else |
414 string = String(characters, size); | 463 string = String(characters, size); |
415 | 464 |
416 return string; | 465 return string; |
417 } | 466 } |
418 | 467 |
419 } // namespace blink | 468 } // namespace blink |
OLD | NEW |