| 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/css/parser/CSSTokenizer.h" | 6 #include "core/css/parser/CSSTokenizer.h" |
| 7 | 7 |
| 8 namespace blink { | 8 namespace blink { |
| 9 #include "core/CSSTokenizerCodepoints.cpp" | 9 #include "core/CSSTokenizerCodepoints.cpp" |
| 10 } | 10 } |
| 11 | 11 |
| 12 #include "core/css/parser/CSSParserObserverWrapper.h" | 12 #include "core/css/parser/CSSParserObserverWrapper.h" |
| 13 #include "core/css/parser/CSSParserTokenRange.h" | 13 #include "core/css/parser/CSSParserTokenRange.h" |
| 14 #include "core/css/parser/CSSTokenizerInputStream.h" | 14 #include "core/css/parser/CSSTokenizerInputStream.h" |
| 15 #include "core/html/parser/HTMLParserIdioms.h" | 15 #include "core/html/parser/HTMLParserIdioms.h" |
| 16 #include "wtf/text/CharacterNames.h" | 16 #include "wtf/text/CharacterNames.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 CSSTokenizer::Scope::Scope(const String& string) | 20 CSSTokenizer::Scope::Scope(const String& string) |
| 21 : m_string(string) | 21 : m_string(string) |
| 22 { | 22 { |
| 23 // According to the spec, we should perform preprocessing here. | 23 // According to the spec, we should perform preprocessing here. |
| 24 // See: http://dev.w3.org/csswg/css-syntax/#input-preprocessing | 24 // See: http://dev.w3.org/csswg/css-syntax/#input-preprocessing |
| 25 // | 25 // |
| 26 // However, we can skip this step since: | 26 // However, we can skip this step since: |
| 27 // * We're using HTML spaces (which accept \r and \f as a valid white space) | 27 // * We're using HTML spaces (which accept \r and \f as a valid white space) |
| 28 // * Do not count white spaces | 28 // * Do not count white spaces |
| 29 // * CSSTokenizerInputStream::peek replaces NULLs for replacement characters | 29 // * CSSTokenizerInputStream::peek replaces NULLs for replacement characters |
| 30 | 30 |
| 31 if (string.isEmpty()) | 31 if (string.isEmpty()) |
| 32 return; | 32 return; |
| 33 | 33 |
| 34 // To avoid resizing we err on the side of reserving too much space. | 34 // To avoid resizing we err on the side of reserving too much space. |
| 35 // Most strings we tokenize have about 3.5 to 5 characters per token. | 35 // Most strings we tokenize have about 3.5 to 5 characters per token. |
| 36 m_tokens.reserveInitialCapacity(string.length() / 3); | 36 m_tokens.reserveInitialCapacity(string.length() / 3); |
| 37 | 37 |
| 38 CSSTokenizerInputStream input(string); | 38 CSSTokenizerInputStream input(string); |
| 39 CSSTokenizer tokenizer(input, *this); | 39 CSSTokenizer tokenizer(input, *this); |
| 40 while (true) { | 40 while (true) { |
| 41 CSSParserToken token = tokenizer.nextToken(); | 41 CSSParserToken token = tokenizer.nextToken(); |
| 42 if (token.type() == CommentToken) | 42 if (token.type() == CommentToken) |
| 43 continue; | 43 continue; |
| 44 if (token.type() == EOFToken) | 44 if (token.type() == EOFToken) |
| 45 return; | 45 return; |
| 46 m_tokens.append(token); | 46 m_tokens.append(token); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 CSSTokenizer::Scope::Scope(const String& string, CSSParserObserverWrapper& wrapp
er) | 50 CSSTokenizer::Scope::Scope(const String& string, CSSParserObserverWrapper& wrapp
er) |
| 51 : m_string(string) | 51 : m_string(string) |
| 52 { | 52 { |
| 53 if (string.isEmpty()) | 53 if (string.isEmpty()) |
| 54 return; | 54 return; |
| 55 | 55 |
| 56 CSSTokenizerInputStream input(string); | 56 CSSTokenizerInputStream input(string); |
| 57 CSSTokenizer tokenizer(input, *this); | 57 CSSTokenizer tokenizer(input, *this); |
| 58 | 58 |
| 59 unsigned offset = 0; | 59 unsigned offset = 0; |
| 60 while (true) { | 60 while (true) { |
| 61 CSSParserToken token = tokenizer.nextToken(); | 61 CSSParserToken token = tokenizer.nextToken(); |
| (...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 { | 628 { |
| 629 while (true) { | 629 while (true) { |
| 630 UChar cc = consume(); | 630 UChar cc = consume(); |
| 631 if (cc == ')' || cc == kEndOfFileMarker) | 631 if (cc == ')' || cc == kEndOfFileMarker) |
| 632 return; | 632 return; |
| 633 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) | 633 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) |
| 634 consumeEscape(); | 634 consumeEscape(); |
| 635 } | 635 } |
| 636 } | 636 } |
| 637 | 637 |
| 638 void CSSTokenizer::consumeUntilNonWhitespace() | 638 unsigned CSSTokenizer::consumeUntilNonWhitespace() |
| 639 { | 639 { |
| 640 unsigned count = 0; |
| 640 // Using HTML space here rather than CSS space since we don't do preprocessi
ng | 641 // Using HTML space here rather than CSS space since we don't do preprocessi
ng |
| 641 while (isHTMLSpace<UChar>(m_input.nextInputChar())) | 642 while (isHTMLSpace<UChar>(m_input.nextInputChar())) { |
| 643 ++count; |
| 642 consume(); | 644 consume(); |
| 645 } |
| 646 return count; |
| 643 } | 647 } |
| 644 | 648 |
| 645 void CSSTokenizer::consumeSingleWhitespaceIfNext() | 649 void CSSTokenizer::consumeSingleWhitespaceIfNext() |
| 646 { | 650 { |
| 647 // We check for \r\n and HTML spaces since we don't do preprocessing | 651 // We check for \r\n and HTML spaces since we don't do preprocessing |
| 648 UChar c = m_input.nextInputChar(); | 652 UChar c = m_input.nextInputChar(); |
| 649 if (c == '\r' && m_input.peek(1) == '\n') | 653 if (c == '\r' && m_input.peek(1) == '\n') |
| 650 consume(2); | 654 consume(2); |
| 651 else if (isHTMLSpace(c)) | 655 else if (isHTMLSpace(c)) |
| 652 consume(); | 656 consume(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 } | 705 } |
| 702 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) { | 706 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) { |
| 703 result.append(consumeEscape()); | 707 result.append(consumeEscape()); |
| 704 continue; | 708 continue; |
| 705 } | 709 } |
| 706 reconsume(cc); | 710 reconsume(cc); |
| 707 return registerString(result.toString()); | 711 return registerString(result.toString()); |
| 708 } | 712 } |
| 709 } | 713 } |
| 710 | 714 |
| 715 CSSParserString CSSTokenizer::lastConsumedCharacter() |
| 716 { |
| 717 return m_input.rangeAsCSSParserString(m_input.offset() - 1, 1); |
| 718 } |
| 719 |
| 711 // http://dev.w3.org/csswg/css-syntax/#consume-an-escaped-code-point | 720 // http://dev.w3.org/csswg/css-syntax/#consume-an-escaped-code-point |
| 712 UChar32 CSSTokenizer::consumeEscape() | 721 UChar32 CSSTokenizer::consumeEscape() |
| 713 { | 722 { |
| 714 UChar cc = consume(); | 723 UChar cc = consume(); |
| 715 ASSERT(!isNewLine(cc)); | 724 ASSERT(!isNewLine(cc)); |
| 716 if (isASCIIHexDigit(cc)) { | 725 if (isASCIIHexDigit(cc)) { |
| 717 unsigned consumedHexDigits = 1; | 726 unsigned consumedHexDigits = 1; |
| 718 StringBuilder hexChars; | 727 StringBuilder hexChars; |
| 719 hexChars.append(cc); | 728 hexChars.append(cc); |
| 720 while (consumedHexDigits < 6 && isASCIIHexDigit(m_input.nextInputChar())
) { | 729 while (consumedHexDigits < 6 && isASCIIHexDigit(m_input.nextInputChar())
) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 | 794 |
| 786 CSSParserString CSSTokenizer::registerString(const String& string) | 795 CSSParserString CSSTokenizer::registerString(const String& string) |
| 787 { | 796 { |
| 788 m_scope.storeString(string); | 797 m_scope.storeString(string); |
| 789 CSSParserString result; | 798 CSSParserString result; |
| 790 result.init(string); | 799 result.init(string); |
| 791 return result; | 800 return result; |
| 792 } | 801 } |
| 793 | 802 |
| 794 } // namespace blink | 803 } // namespace blink |
| OLD | NEW |