OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/css/parser/CSSVariableParser.h" |
| 7 |
| 8 #include "core/css/CSSCustomPropertyDeclaration.h" |
| 9 #include "core/css/parser/CSSParserTokenRange.h" |
| 10 #include "core/css/parser/CSSParserValues.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 bool CSSVariableParser::isValidVariableName(const CSSParserToken& token) |
| 15 { |
| 16 if (token.type() != IdentToken) |
| 17 return false; |
| 18 |
| 19 CSSParserString value = token.value(); |
| 20 return value.length() >= 2 && value[0] == '-' && value[1] == '-'; |
| 21 } |
| 22 |
| 23 bool isValidVariableReference(CSSParserTokenRange); |
| 24 |
| 25 bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, bool isTopLev
elBlock = true) |
| 26 { |
| 27 while (!range.atEnd()) { |
| 28 if (range.peek().blockType() == CSSParserToken::BlockStart) { |
| 29 const CSSParserToken& token = range.peek(); |
| 30 CSSParserTokenRange block = range.consumeBlock(); |
| 31 if (token.functionId() == CSSValueVar) { |
| 32 if (!isValidVariableReference(block)) |
| 33 return false; // Bail if any references are invalid |
| 34 hasReferences = true; |
| 35 continue; |
| 36 } |
| 37 if (!classifyBlock(block, hasReferences, false)) |
| 38 return false; |
| 39 continue; |
| 40 } |
| 41 |
| 42 ASSERT(range.peek().blockType() != CSSParserToken::BlockEnd); |
| 43 |
| 44 const CSSParserToken& token = range.consume(); |
| 45 switch (token.type()) { |
| 46 case DelimiterToken: { |
| 47 if (token.delimiter() == '!' && isTopLevelBlock) |
| 48 return false; |
| 49 break; |
| 50 } |
| 51 case RightParenthesisToken: |
| 52 case RightBraceToken: |
| 53 case RightBracketToken: |
| 54 case BadStringToken: |
| 55 case BadUrlToken: |
| 56 return false; |
| 57 case SemicolonToken: |
| 58 if (isTopLevelBlock) |
| 59 return false; |
| 60 break; |
| 61 default: |
| 62 break; |
| 63 } |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool isValidVariableReference(CSSParserTokenRange range) |
| 69 { |
| 70 range.consumeWhitespace(); |
| 71 if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace
())) |
| 72 return false; |
| 73 if (range.atEnd()) |
| 74 return true; |
| 75 |
| 76 if (range.consume().type() != CommaToken) |
| 77 return false; |
| 78 if (range.atEnd()) |
| 79 return false; |
| 80 |
| 81 bool hasReferences = false; |
| 82 return classifyBlock(range, hasReferences); |
| 83 } |
| 84 |
| 85 static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasRefe
rences) |
| 86 { |
| 87 hasReferences = false; |
| 88 |
| 89 range.consumeWhitespace(); |
| 90 if (range.peek().type() == IdentToken) { |
| 91 CSSValueID id = range.consumeIncludingWhitespace().id(); |
| 92 if (range.atEnd() && (id == CSSValueInherit || id == CSSValueInitial ||
id == CSSValueUnset)) |
| 93 return id; |
| 94 } |
| 95 |
| 96 if (classifyBlock(range, hasReferences)) |
| 97 return CSSValueInternalVariableValue; |
| 98 return CSSValueInvalid; |
| 99 } |
| 100 |
| 101 bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange rang
e) |
| 102 { |
| 103 bool hasReferences; |
| 104 CSSValueID type = classifyVariableRange(range, hasReferences); |
| 105 return type == CSSValueInternalVariableValue && hasReferences; |
| 106 } |
| 107 |
| 108 PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDec
larationValue(const AtomicString& variableName, CSSParserTokenRange range) |
| 109 { |
| 110 if (range.atEnd()) |
| 111 return nullptr; |
| 112 |
| 113 bool hasReferences; |
| 114 CSSValueID type = classifyVariableRange(range, hasReferences); |
| 115 |
| 116 if (type == CSSValueInvalid) |
| 117 return nullptr; |
| 118 if (type == CSSValueInternalVariableValue) |
| 119 return CSSCustomPropertyDeclaration::create(variableName, CSSVariableDat
a::create(range, hasReferences)); |
| 120 return CSSCustomPropertyDeclaration::create(variableName, type); |
| 121 } |
| 122 |
| 123 } // namespace blink |
OLD | NEW |