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 "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/unicode/CharacterNames.h" | 16 #include "wtf/unicode/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 , m_hasVariableReference(false) | |
| 22 { | 23 { |
| 23 // According to the spec, we should perform preprocessing here. | 24 // According to the spec, we should perform preprocessing here. |
| 24 // See: http://dev.w3.org/csswg/css-syntax/#input-preprocessing | 25 // See: http://dev.w3.org/csswg/css-syntax/#input-preprocessing |
| 25 // | 26 // |
| 26 // However, we can skip this step since: | 27 // However, we can skip this step since: |
| 27 // * We're using HTML spaces (which accept \r and \f as a valid white space) | 28 // * We're using HTML spaces (which accept \r and \f as a valid white space) |
| 28 // * Do not count white spaces | 29 // * Do not count white spaces |
| 29 // * CSSTokenizerInputStream::peek replaces NULLs for replacement characters | 30 // * CSSTokenizerInputStream::peek replaces NULLs for replacement characters |
| 30 | 31 |
| 31 if (string.isEmpty()) | 32 if (string.isEmpty()) |
| 32 return; | 33 return; |
| 33 | 34 |
| 34 // To avoid resizing we err on the side of reserving too much space. | 35 // 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. | 36 // Most strings we tokenize have about 3.5 to 5 characters per token. |
| 36 m_tokens.reserveInitialCapacity(string.length() / 3); | 37 m_tokens.reserveInitialCapacity(string.length() / 3); |
| 37 | 38 |
| 38 CSSTokenizerInputStream input(string); | 39 CSSTokenizerInputStream input(string); |
| 39 CSSTokenizer tokenizer(input, *this); | 40 CSSTokenizer tokenizer(input, *this); |
| 40 while (true) { | 41 while (true) { |
| 41 CSSParserToken token = tokenizer.nextToken(); | 42 CSSParserToken token = tokenizer.nextToken(); |
| 42 if (token.type() == CommentToken) | 43 if (token.type() == CommentToken) |
| 43 continue; | 44 continue; |
| 44 if (token.type() == EOFToken) | 45 if (token.type() == EOFToken) |
| 45 return; | 46 return; |
| 46 m_tokens.append(token); | 47 m_tokens.append(token); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 CSSTokenizer::Scope::Scope(const String& string, CSSParserObserverWrapper& wrapp er) | 51 CSSTokenizer::Scope::Scope(const String& string, CSSParserObserverWrapper& wrapp er) |
| 51 : m_string(string) | 52 : m_string(string) |
| 53 , m_hasVariableReference(false) | |
| 52 { | 54 { |
| 53 if (string.isEmpty()) | 55 if (string.isEmpty()) |
| 54 return; | 56 return; |
| 55 | 57 |
| 56 CSSTokenizerInputStream input(string); | 58 CSSTokenizerInputStream input(string); |
| 57 CSSTokenizer tokenizer(input, *this); | 59 CSSTokenizer tokenizer(input, *this); |
| 58 | 60 |
| 59 unsigned offset = 0; | 61 unsigned offset = 0; |
| 60 while (true) { | 62 while (true) { |
| 61 CSSParserToken token = tokenizer.nextToken(); | 63 CSSParserToken token = tokenizer.nextToken(); |
| 62 if (token.type() == EOFToken) | 64 if (token.type() == EOFToken) |
| 63 break; | 65 break; |
| 64 if (token.type() == CommentToken) { | 66 if (token.type() == CommentToken) { |
| 65 wrapper.addComment(offset, input.offset(), m_tokens.size()); | 67 wrapper.addComment(offset, input.offset(), m_tokens.size()); |
| 66 } else { | 68 } else { |
| 67 m_tokens.append(token); | 69 m_tokens.append(token); |
| 68 wrapper.addToken(offset); | 70 wrapper.addToken(offset); |
| 69 } | 71 } |
| 70 offset = input.offset(); | 72 offset = input.offset(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 wrapper.addToken(offset); | 75 wrapper.addToken(offset); |
| 74 wrapper.finalizeConstruction(m_tokens.begin()); | 76 wrapper.finalizeConstruction(m_tokens.begin()); |
| 75 } | 77 } |
| 76 | 78 |
| 77 CSSParserTokenRange CSSTokenizer::Scope::tokenRange() | 79 CSSParserTokenRange CSSTokenizer::Scope::tokenRange() |
| 78 { | 80 { |
| 79 return m_tokens; | 81 return CSSParserTokenRange(m_tokens, this); |
|
alancutter (OOO until 2018)
2015/07/14 06:12:57
This looks very dangerous, see calls to tokenRange
| |
| 80 } | 82 } |
| 81 | 83 |
| 82 unsigned CSSTokenizer::Scope::tokenCount() | 84 unsigned CSSTokenizer::Scope::tokenCount() |
| 83 { | 85 { |
| 84 return m_tokens.size(); | 86 return m_tokens.size(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 // http://dev.w3.org/csswg/css-syntax/#name-start-code-point | 89 // http://dev.w3.org/csswg/css-syntax/#name-start-code-point |
| 88 static bool isNameStart(UChar c) | 90 static bool isNameStart(UChar c) |
| 89 { | 91 { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 return current; | 132 return current; |
| 131 } | 133 } |
| 132 | 134 |
| 133 void CSSTokenizer::consume(unsigned offset) | 135 void CSSTokenizer::consume(unsigned offset) |
| 134 { | 136 { |
| 135 m_input.advance(offset); | 137 m_input.advance(offset); |
| 136 } | 138 } |
| 137 | 139 |
| 138 CSSParserToken CSSTokenizer::whiteSpace(UChar cc) | 140 CSSParserToken CSSTokenizer::whiteSpace(UChar cc) |
| 139 { | 141 { |
| 140 consumeUntilNonWhitespace(); | 142 unsigned start = m_input.offset(); |
| 141 return CSSParserToken(WhitespaceToken); | 143 unsigned length = consumeUntilNonWhitespace(); |
| 144 return CSSParserToken(WhitespaceToken, m_input.rangeAsCSSParserString(start, length)); | |
| 142 } | 145 } |
| 143 | 146 |
| 144 static bool popIfBlockMatches(Vector<CSSParserTokenType>& blockStack, CSSParserT okenType type) | 147 static bool popIfBlockMatches(Vector<CSSParserTokenType>& blockStack, CSSParserT okenType type) |
| 145 { | 148 { |
| 146 if (!blockStack.isEmpty() && blockStack.last() == type) { | 149 if (!blockStack.isEmpty() && blockStack.last() == type) { |
| 147 blockStack.removeLast(); | 150 blockStack.removeLast(); |
| 148 return true; | 151 return true; |
| 149 } | 152 } |
| 150 return false; | 153 return false; |
| 151 } | 154 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 // These get ignored, but we need a value to return. | 257 // These get ignored, but we need a value to return. |
| 255 consumeUntilCommentEndFound(); | 258 consumeUntilCommentEndFound(); |
| 256 return CSSParserToken(CommentToken); | 259 return CSSParserToken(CommentToken); |
| 257 } | 260 } |
| 258 | 261 |
| 259 return CSSParserToken(DelimiterToken, cc); | 262 return CSSParserToken(DelimiterToken, cc); |
| 260 } | 263 } |
| 261 | 264 |
| 262 CSSParserToken CSSTokenizer::colon(UChar cc) | 265 CSSParserToken CSSTokenizer::colon(UChar cc) |
| 263 { | 266 { |
| 264 return CSSParserToken(ColonToken); | 267 return CSSParserToken(ColonToken, lastConsumedCharacter()); |
| 265 } | 268 } |
| 266 | 269 |
| 267 CSSParserToken CSSTokenizer::semiColon(UChar cc) | 270 CSSParserToken CSSTokenizer::semiColon(UChar cc) |
| 268 { | 271 { |
| 269 return CSSParserToken(SemicolonToken); | 272 return CSSParserToken(SemicolonToken, lastConsumedCharacter()); |
| 270 } | 273 } |
| 271 | 274 |
| 272 CSSParserToken CSSTokenizer::hash(UChar cc) | 275 CSSParserToken CSSTokenizer::hash(UChar cc) |
| 273 { | 276 { |
| 274 UChar nextChar = m_input.nextInputChar(); | 277 UChar nextChar = m_input.nextInputChar(); |
| 275 if (isNameChar(nextChar) || twoCharsAreValidEscape(nextChar, m_input.peek(1) )) { | 278 if (isNameChar(nextChar) || twoCharsAreValidEscape(nextChar, m_input.peek(1) )) { |
| 276 HashTokenType type = nextCharsAreIdentifier() ? HashTokenId : HashTokenU nrestricted; | 279 HashTokenType type = nextCharsAreIdentifier() ? HashTokenId : HashTokenU nrestricted; |
| 277 return CSSParserToken(type, consumeName()); | 280 return CSSParserToken(type, consumeName()); |
| 278 } | 281 } |
| 279 | 282 |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 { | 487 { |
| 485 CSSParserString name = consumeName(); | 488 CSSParserString name = consumeName(); |
| 486 if (consumeIfNext('(')) { | 489 if (consumeIfNext('(')) { |
| 487 if (name.equalIgnoringCase("url")) { | 490 if (name.equalIgnoringCase("url")) { |
| 488 // The spec is slightly different so as to avoid dropping whitespace | 491 // The spec is slightly different so as to avoid dropping whitespace |
| 489 // tokens, but they wouldn't be used and this is easier. | 492 // tokens, but they wouldn't be used and this is easier. |
| 490 consumeUntilNonWhitespace(); | 493 consumeUntilNonWhitespace(); |
| 491 UChar next = m_input.nextInputChar(); | 494 UChar next = m_input.nextInputChar(); |
| 492 if (next != '"' && next != '\'') | 495 if (next != '"' && next != '\'') |
| 493 return consumeUrlToken(); | 496 return consumeUrlToken(); |
| 497 } else if (equalIgnoringCase(name, "var")) { | |
| 498 m_scope.m_hasVariableReference = true; | |
| 494 } | 499 } |
| 495 return blockStart(LeftParenthesisToken, FunctionToken, name); | 500 return blockStart(LeftParenthesisToken, FunctionToken, name); |
| 496 } | 501 } |
| 497 return CSSParserToken(IdentToken, name); | 502 return CSSParserToken(IdentToken, name); |
| 498 } | 503 } |
| 499 | 504 |
| 500 // http://dev.w3.org/csswg/css-syntax/#consume-a-string-token | 505 // http://dev.w3.org/csswg/css-syntax/#consume-a-string-token |
| 501 CSSParserToken CSSTokenizer::consumeStringTokenUntil(UChar endingCodePoint) | 506 CSSParserToken CSSTokenizer::consumeStringTokenUntil(UChar endingCodePoint) |
| 502 { | 507 { |
| 503 // Strings without escapes get handled without allocations | 508 // Strings without escapes get handled without allocations |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 { | 633 { |
| 629 while (true) { | 634 while (true) { |
| 630 UChar cc = consume(); | 635 UChar cc = consume(); |
| 631 if (cc == ')' || cc == kEndOfFileMarker) | 636 if (cc == ')' || cc == kEndOfFileMarker) |
| 632 return; | 637 return; |
| 633 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) | 638 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) |
| 634 consumeEscape(); | 639 consumeEscape(); |
| 635 } | 640 } |
| 636 } | 641 } |
| 637 | 642 |
| 638 void CSSTokenizer::consumeUntilNonWhitespace() | 643 unsigned CSSTokenizer::consumeUntilNonWhitespace() |
| 639 { | 644 { |
| 645 unsigned count = 0; | |
| 640 // Using HTML space here rather than CSS space since we don't do preprocessi ng | 646 // Using HTML space here rather than CSS space since we don't do preprocessi ng |
| 641 while (isHTMLSpace<UChar>(m_input.nextInputChar())) | 647 while (isHTMLSpace<UChar>(m_input.nextInputChar())) { |
| 648 ++count; | |
| 642 consume(); | 649 consume(); |
| 650 } | |
| 651 return count; | |
| 643 } | 652 } |
| 644 | 653 |
| 645 void CSSTokenizer::consumeSingleWhitespaceIfNext() | 654 void CSSTokenizer::consumeSingleWhitespaceIfNext() |
| 646 { | 655 { |
| 647 // We check for \r\n and HTML spaces since we don't do preprocessing | 656 // We check for \r\n and HTML spaces since we don't do preprocessing |
| 648 UChar c = m_input.nextInputChar(); | 657 UChar c = m_input.nextInputChar(); |
| 649 if (c == '\r' && m_input.peek(1) == '\n') | 658 if (c == '\r' && m_input.peek(1) == '\n') |
| 650 consume(2); | 659 consume(2); |
| 651 else if (isHTMLSpace(c)) | 660 else if (isHTMLSpace(c)) |
| 652 consume(); | 661 consume(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 701 } | 710 } |
| 702 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) { | 711 if (twoCharsAreValidEscape(cc, m_input.nextInputChar())) { |
| 703 result.append(consumeEscape()); | 712 result.append(consumeEscape()); |
| 704 continue; | 713 continue; |
| 705 } | 714 } |
| 706 reconsume(cc); | 715 reconsume(cc); |
| 707 return registerString(result.toString()); | 716 return registerString(result.toString()); |
| 708 } | 717 } |
| 709 } | 718 } |
| 710 | 719 |
| 720 CSSParserString CSSTokenizer::lastConsumedCharacter() | |
| 721 { | |
| 722 return m_input.rangeAsCSSParserString(m_input.offset() - 1, 1); | |
| 723 } | |
| 724 | |
| 711 // http://dev.w3.org/csswg/css-syntax/#consume-an-escaped-code-point | 725 // http://dev.w3.org/csswg/css-syntax/#consume-an-escaped-code-point |
| 712 UChar32 CSSTokenizer::consumeEscape() | 726 UChar32 CSSTokenizer::consumeEscape() |
| 713 { | 727 { |
| 714 UChar cc = consume(); | 728 UChar cc = consume(); |
| 715 ASSERT(!isNewLine(cc)); | 729 ASSERT(!isNewLine(cc)); |
| 716 if (isASCIIHexDigit(cc)) { | 730 if (isASCIIHexDigit(cc)) { |
| 717 unsigned consumedHexDigits = 1; | 731 unsigned consumedHexDigits = 1; |
| 718 StringBuilder hexChars; | 732 StringBuilder hexChars; |
| 719 hexChars.append(cc); | 733 hexChars.append(cc); |
| 720 while (consumedHexDigits < 6 && isASCIIHexDigit(m_input.nextInputChar()) ) { | 734 while (consumedHexDigits < 6 && isASCIIHexDigit(m_input.nextInputChar()) ) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 785 | 799 |
| 786 CSSParserString CSSTokenizer::registerString(const String& string) | 800 CSSParserString CSSTokenizer::registerString(const String& string) |
| 787 { | 801 { |
| 788 m_scope.storeString(string); | 802 m_scope.storeString(string); |
| 789 CSSParserString result; | 803 CSSParserString result; |
| 790 result.init(string); | 804 result.init(string); |
| 791 return result; | 805 return result; |
| 792 } | 806 } |
| 793 | 807 |
| 794 } // namespace blink | 808 } // namespace blink |
| OLD | NEW |