Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/css/parser/CSSTokenizer.h" | 5 #include "core/css/parser/CSSTokenizer.h" |
| 6 | 6 |
| 7 namespace blink { | 7 namespace blink { |
| 8 #include "core/CSSTokenizerCodepoints.cpp" | 8 #include "core/CSSTokenizerCodepoints.cpp" |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 codePointFunc = codePoints[cc]; | 357 codePointFunc = codePoints[cc]; |
| 358 } else { | 358 } else { |
| 359 codePointFunc = &CSSTokenizer::nameStart; | 359 codePointFunc = &CSSTokenizer::nameStart; |
| 360 } | 360 } |
| 361 | 361 |
| 362 if (codePointFunc) | 362 if (codePointFunc) |
| 363 return ((this)->*(codePointFunc))(cc); | 363 return ((this)->*(codePointFunc))(cc); |
| 364 return CSSParserToken(DelimiterToken, cc); | 364 return CSSParserToken(DelimiterToken, cc); |
| 365 } | 365 } |
| 366 | 366 |
| 367 static NumericSign getSign(CSSTokenizerInputStream& input, unsigned& offset) | |
| 368 { | |
| 369 UChar next = input.peekWithoutReplacement(0); | |
| 370 if (next == '+') { | |
| 371 ++offset; | |
| 372 return PlusSign; | |
| 373 } | |
| 374 if (next == '-') { | |
| 375 ++offset; | |
| 376 return MinusSign; | |
| 377 } | |
| 378 return NoSign; | |
| 379 } | |
| 380 | |
| 381 static double getInteger(CSSTokenizerInputStream& input, unsigned& offset) | |
| 382 { | |
| 383 unsigned intStartPos = offset; | |
| 384 offset = input.skipWhilePredicate<isASCIIDigit>(offset); | |
| 385 unsigned intEndPos = offset; | |
| 386 return input.getDouble(intStartPos, intEndPos); | |
| 387 } | |
| 388 | |
| 389 static double getFraction(CSSTokenizerInputStream& input, unsigned& offset) | |
| 390 { | |
| 391 if (input.peekWithoutReplacement(offset) != '.' | |
| 392 || !isASCIIDigit(input.peekWithoutReplacement(offset + 1))) | |
| 393 return 0; | |
| 394 unsigned startOffset = offset; | |
| 395 offset = input.skipWhilePredicate<isASCIIDigit>(offset + 1); | |
| 396 return input.getDouble(startOffset, offset); | |
| 397 } | |
| 398 | |
| 399 static double getExponent(CSSTokenizerInputStream& input, unsigned& offset, int& sign) | |
| 400 { | |
| 401 unsigned exponentStartPos = 0; | |
| 402 unsigned exponentEndPos = 0; | |
| 403 UChar next = input.peekWithoutReplacement(offset); | |
| 404 if (next != 'E' && next != 'e') | |
| 405 return 0; | |
| 406 int offsetBeforeExponent = offset; | |
| 407 ++offset; | |
| 408 next = input.peekWithoutReplacement(offset); | |
| 409 if (next == '+') { | |
| 410 ++offset; | |
| 411 } else if (next =='-') { | |
| 412 sign = -1; | |
| 413 ++offset; | |
| 414 } | |
| 415 exponentStartPos = offset; | |
| 416 offset = input.skipWhilePredicate<isASCIIDigit>(offset); | |
| 417 exponentEndPos = offset; | |
| 418 if (exponentEndPos == exponentStartPos) | |
| 419 offset = offsetBeforeExponent; | |
| 420 return input.getDouble(exponentStartPos, exponentEndPos); | |
| 421 } | |
| 422 | |
| 423 // This method merges the following spec sections for efficiency | 367 // This method merges the following spec sections for efficiency |
| 424 // http://www.w3.org/TR/css3-syntax/#consume-a-number | 368 // http://www.w3.org/TR/css3-syntax/#consume-a-number |
| 425 // http://www.w3.org/TR/css3-syntax/#convert-a-string-to-a-number | 369 // http://www.w3.org/TR/css3-syntax/#convert-a-string-to-a-number |
| 426 CSSParserToken CSSTokenizer::consumeNumber() | 370 CSSParserToken CSSTokenizer::consumeNumber() |
| 427 { | 371 { |
| 428 ASSERT(nextCharsAreNumber()); | 372 ASSERT(nextCharsAreNumber()); |
| 373 | |
| 429 NumericValueType type = IntegerValueType; | 374 NumericValueType type = IntegerValueType; |
| 430 double value = 0; | 375 NumericSign sign = NoSign; |
| 431 unsigned offset = 0; | 376 unsigned numberLength = 0; |
| 432 int exponentSign = 1; | |
| 433 NumericSign sign = getSign(m_input, offset); | |
| 434 double integerPart = getInteger(m_input, offset); | |
| 435 unsigned integerPartEndOffset = offset; | |
| 436 | 377 |
| 437 double fractionPart = getFraction(m_input, offset); | 378 UChar next = m_input.peekWithoutReplacement(0); |
| 438 double exponentPart = getExponent(m_input, offset, exponentSign); | 379 if (next == '+') { |
| 439 double exponent = pow(10, (float)exponentSign * (double)exponentPart); | 380 ++numberLength; |
| 440 value = ((double)integerPart + fractionPart) * exponent; | 381 sign = PlusSign; |
| 441 if (sign == MinusSign) | 382 } else if (next == '-') { |
| 442 value = -value; | 383 ++numberLength; |
| 384 sign = MinusSign; | |
| 385 } | |
| 443 | 386 |
| 444 m_input.advance(offset); | 387 numberLength = m_input.skipWhilePredicate<isASCIIDigit>(numberLength); |
| 445 if (offset != integerPartEndOffset) | 388 next = m_input.peekWithoutReplacement(numberLength); |
| 389 if (next == '.' && isASCIIDigit(m_input.peekWithoutReplacement(numberLength + 1))) { | |
| 446 type = NumberValueType; | 390 type = NumberValueType; |
| 391 numberLength = m_input.skipWhilePredicate<isASCIIDigit>(numberLength + 2 ); | |
| 392 next = m_input.peekWithoutReplacement(numberLength); | |
| 393 } | |
| 394 | |
| 395 if (next == 'E' || next == 'e') { | |
| 396 next = m_input.peekWithoutReplacement(numberLength + 1); | |
| 397 if (isASCIIDigit(next)) { | |
| 398 type = NumberValueType; | |
| 399 numberLength = m_input.skipWhilePredicate<isASCIIDigit>(numberLength + 1); | |
| 400 } else if ((next == '+' || next == '-') && isASCIIDigit(m_input.peekWith outReplacement(numberLength + 2))) { | |
| 401 type = NumberValueType; | |
| 402 numberLength = m_input.skipWhilePredicate<isASCIIDigit>(numberLength + 3); | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 double value = m_input.getDouble(0, numberLength); | |
|
Timothy Loh
2016/08/09 02:31:44
getDouble doesn't really need the start argument a
esprehn
2016/08/09 04:03:32
Yeah, I'll do that next.
| |
| 407 m_input.advance(numberLength); | |
| 447 | 408 |
| 448 return CSSParserToken(NumberToken, value, type, sign); | 409 return CSSParserToken(NumberToken, value, type, sign); |
| 449 } | 410 } |
| 450 | 411 |
| 451 // http://www.w3.org/TR/css3-syntax/#consume-a-numeric-token | 412 // http://www.w3.org/TR/css3-syntax/#consume-a-numeric-token |
| 452 CSSParserToken CSSTokenizer::consumeNumericToken() | 413 CSSParserToken CSSTokenizer::consumeNumericToken() |
| 453 { | 414 { |
| 454 CSSParserToken token = consumeNumber(); | 415 CSSParserToken token = consumeNumber(); |
| 455 if (nextCharsAreIdentifier()) | 416 if (nextCharsAreIdentifier()) |
| 456 token.convertToDimensionWithUnit(consumeName()); | 417 token.convertToDimensionWithUnit(consumeName()); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 765 return areIdentifier; | 726 return areIdentifier; |
| 766 } | 727 } |
| 767 | 728 |
| 768 StringView CSSTokenizer::registerString(const String& string) | 729 StringView CSSTokenizer::registerString(const String& string) |
| 769 { | 730 { |
| 770 m_scope.storeString(string); | 731 m_scope.storeString(string); |
| 771 return string; | 732 return string; |
| 772 } | 733 } |
| 773 | 734 |
| 774 } // namespace blink | 735 } // namespace blink |
| OLD | NEW |