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 |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 14 * ANY 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 |
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * OF THIS 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 "wtf/MathExtras.h" | 28 #include "wtf/MathExtras.h" |
29 #include "wtf/text/AtomicString.h" | 29 #include "wtf/text/AtomicString.h" |
30 #include "wtf/text/ParsingUtilities.h" | 30 #include "wtf/text/ParsingUtilities.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" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 } | 91 } |
92 | 92 |
93 String serializeForNumberType(double number) { | 93 String serializeForNumberType(double number) { |
94 // According to HTML5, "the best representation of the number n as a floating | 94 // According to HTML5, "the best representation of the number n as a floating |
95 // point number" is a string produced by applying ToString() to n. | 95 // point number" is a string produced by applying ToString() to n. |
96 return String::numberToStringECMAScript(number); | 96 return String::numberToStringECMAScript(number); |
97 } | 97 } |
98 | 98 |
99 Decimal parseToDecimalForNumberType(const String& string, | 99 Decimal parseToDecimalForNumberType(const String& string, |
100 const Decimal& fallbackValue) { | 100 const Decimal& fallbackValue) { |
101 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers a
nd parseToDoubleForNumberType | 101 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers |
102 // String::toDouble() accepts leading + and whitespace characters, which are n
ot valid here. | 102 // and parseToDoubleForNumberType String::toDouble() accepts leading + and |
| 103 // whitespace characters, which are not valid here. |
103 const UChar firstCharacter = string[0]; | 104 const UChar firstCharacter = string[0]; |
104 if (firstCharacter != '-' && firstCharacter != '.' && | 105 if (firstCharacter != '-' && firstCharacter != '.' && |
105 !isASCIIDigit(firstCharacter)) | 106 !isASCIIDigit(firstCharacter)) |
106 return fallbackValue; | 107 return fallbackValue; |
107 | 108 |
108 const Decimal value = Decimal::fromString(string); | 109 const Decimal value = Decimal::fromString(string); |
109 if (!value.isFinite()) | 110 if (!value.isFinite()) |
110 return fallbackValue; | 111 return fallbackValue; |
111 | 112 |
112 // Numbers are considered finite IEEE 754 Double-precision floating point valu
es. | 113 // Numbers are considered finite IEEE 754 Double-precision floating point |
| 114 // values. |
113 const Decimal doubleMax = | 115 const Decimal doubleMax = |
114 Decimal::fromDouble(std::numeric_limits<double>::max()); | 116 Decimal::fromDouble(std::numeric_limits<double>::max()); |
115 if (value < -doubleMax || value > doubleMax) | 117 if (value < -doubleMax || value > doubleMax) |
116 return fallbackValue; | 118 return fallbackValue; |
117 | 119 |
118 // We return +0 for -0 case. | 120 // We return +0 for -0 case. |
119 return value.isZero() ? Decimal(0) : value; | 121 return value.isZero() ? Decimal(0) : value; |
120 } | 122 } |
121 | 123 |
122 static double checkDoubleValue(double value, bool valid, double fallbackValue) { | 124 static double checkDoubleValue(double value, bool valid, double fallbackValue) { |
123 if (!valid) | 125 if (!valid) |
124 return fallbackValue; | 126 return fallbackValue; |
125 | 127 |
126 // NaN and infinity are considered valid by String::toDouble, but not valid he
re. | 128 // NaN and infinity are considered valid by String::toDouble, but not valid |
| 129 // here. |
127 if (!std::isfinite(value)) | 130 if (!std::isfinite(value)) |
128 return fallbackValue; | 131 return fallbackValue; |
129 | 132 |
130 // Numbers are considered finite IEEE 754 Double-precision floating point valu
es. | 133 // Numbers are considered finite IEEE 754 Double-precision floating point |
| 134 // values. |
131 if (-std::numeric_limits<double>::max() > value || | 135 if (-std::numeric_limits<double>::max() > value || |
132 value > std::numeric_limits<double>::max()) | 136 value > std::numeric_limits<double>::max()) |
133 return fallbackValue; | 137 return fallbackValue; |
134 | 138 |
135 // The following expression converts -0 to +0. | 139 // The following expression converts -0 to +0. |
136 return value ? value : 0; | 140 return value ? value : 0; |
137 } | 141 } |
138 | 142 |
139 double parseToDoubleForNumberType(const String& string, double fallbackValue) { | 143 double parseToDoubleForNumberType(const String& string, double fallbackValue) { |
140 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers | 144 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers |
141 // String::toDouble() accepts leading + and whitespace characters, which are n
ot valid here. | 145 // String::toDouble() accepts leading + and whitespace characters, which are |
| 146 // not valid here. |
142 UChar firstCharacter = string[0]; | 147 UChar firstCharacter = string[0]; |
143 if (firstCharacter != '-' && firstCharacter != '.' && | 148 if (firstCharacter != '-' && firstCharacter != '.' && |
144 !isASCIIDigit(firstCharacter)) | 149 !isASCIIDigit(firstCharacter)) |
145 return fallbackValue; | 150 return fallbackValue; |
146 if (string.endsWith('.')) | 151 if (string.endsWith('.')) |
147 return fallbackValue; | 152 return fallbackValue; |
148 | 153 |
149 bool valid = false; | 154 bool valid = false; |
150 double value = string.toDouble(&valid); | 155 double value = string.toDouble(&valid); |
151 return checkDoubleValue(value, valid, fallbackValue); | 156 return checkDoubleValue(value, valid, fallbackValue); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 224 |
220 template <typename CharacterType> | 225 template <typename CharacterType> |
221 static bool parseHTMLNonNegativeIntegerInternal(const CharacterType* position, | 226 static bool parseHTMLNonNegativeIntegerInternal(const CharacterType* position, |
222 const CharacterType* end, | 227 const CharacterType* end, |
223 unsigned& value) { | 228 unsigned& value) { |
224 // This function is an implementation of the following algorithm: | 229 // This function is an implementation of the following algorithm: |
225 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsin
g-non-negative-integers | 230 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsin
g-non-negative-integers |
226 // However, in order to support integers >= 2^31, we fold [1] into this. | 231 // However, in order to support integers >= 2^31, we fold [1] into this. |
227 // 'Step N' in the following comments refers to [1]. | 232 // 'Step N' in the following comments refers to [1]. |
228 // | 233 // |
229 // [1] https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-pa
rsing-integers | 234 // [1] |
| 235 // https://html.spec.whatwg.org/multipage/infrastructure.html#rules-for-parsin
g-integers |
230 | 236 |
231 // Step 3: Let sign have the value "positive". | 237 // Step 3: Let sign have the value "positive". |
232 int sign = 1; | 238 int sign = 1; |
233 | 239 |
234 // Step 4: Skip whitespace. | 240 // Step 4: Skip whitespace. |
235 while (position < end) { | 241 while (position < end) { |
236 if (!isHTMLSpace<CharacterType>(*position)) | 242 if (!isHTMLSpace<CharacterType>(*position)) |
237 break; | 243 break; |
238 ++position; | 244 ++position; |
239 } | 245 } |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 if (length > StringImpl::highestStaticStringLength()) | 457 if (length > StringImpl::highestStaticStringLength()) |
452 return nullptr; | 458 return nullptr; |
453 // computeHashAndMaskTop8Bits is the function StringImpl::hash() uses. | 459 // computeHashAndMaskTop8Bits is the function StringImpl::hash() uses. |
454 unsigned hash = StringHasher::computeHashAndMaskTop8Bits(characters, length); | 460 unsigned hash = StringHasher::computeHashAndMaskTop8Bits(characters, length); |
455 const WTF::StaticStringsTable& table = StringImpl::allStaticStrings(); | 461 const WTF::StaticStringsTable& table = StringImpl::allStaticStrings(); |
456 ASSERT(!table.isEmpty()); | 462 ASSERT(!table.isEmpty()); |
457 | 463 |
458 WTF::StaticStringsTable::const_iterator it = table.find(hash); | 464 WTF::StaticStringsTable::const_iterator it = table.find(hash); |
459 if (it == table.end()) | 465 if (it == table.end()) |
460 return nullptr; | 466 return nullptr; |
461 // It's possible to have hash collisions between arbitrary strings and | 467 // It's possible to have hash collisions between arbitrary strings and known |
462 // known identifiers (e.g. "bvvfg" collides with "script"). | 468 // identifiers (e.g. "bvvfg" collides with "script"). However ASSERTs in |
463 // However ASSERTs in StringImpl::createStatic guard against there ever being
collisions | 469 // StringImpl::createStatic guard against there ever being collisions between |
464 // between static strings. | 470 // static strings. |
465 if (!equal(it->value, characters, length)) | 471 if (!equal(it->value, characters, length)) |
466 return nullptr; | 472 return nullptr; |
467 return it->value; | 473 return it->value; |
468 } | 474 } |
469 | 475 |
470 String attemptStaticStringCreation(const LChar* characters, size_t size) { | 476 String attemptStaticStringCreation(const LChar* characters, size_t size) { |
471 String string(findStringIfStatic(characters, size)); | 477 String string(findStringIfStatic(characters, size)); |
472 if (string.impl()) | 478 if (string.impl()) |
473 return string; | 479 return string; |
474 return String(characters, size); | 480 return String(characters, size); |
475 } | 481 } |
476 | 482 |
477 String attemptStaticStringCreation(const UChar* characters, | 483 String attemptStaticStringCreation(const UChar* characters, |
478 size_t size, | 484 size_t size, |
479 CharacterWidth width) { | 485 CharacterWidth width) { |
480 String string(findStringIfStatic(characters, size)); | 486 String string(findStringIfStatic(characters, size)); |
481 if (string.impl()) | 487 if (string.impl()) |
482 return string; | 488 return string; |
483 if (width == Likely8Bit) | 489 if (width == Likely8Bit) |
484 string = StringImpl::create8BitIfPossible(characters, size); | 490 string = StringImpl::create8BitIfPossible(characters, size); |
485 else if (width == Force8Bit) | 491 else if (width == Force8Bit) |
486 string = String::make8BitFrom16BitSource(characters, size); | 492 string = String::make8BitFrom16BitSource(characters, size); |
487 else | 493 else |
488 string = String(characters, size); | 494 string = String(characters, size); |
489 | 495 |
490 return string; | 496 return string; |
491 } | 497 } |
492 | 498 |
493 } // namespace blink | 499 } // namespace blink |
OLD | NEW |