Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: third_party/WebKit/Source/core/css/parser/CSSVariableParser.cpp

Issue 1192983003: CSS Custom Properties (Variables) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Whoops, the forward declaration just needed to be moved. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.type() == FunctionToken && token.valueEqualsIgnoringCase(" var")) {
Timothy Loh 2015/10/15 03:43:48 We have a helper for this (here and everywhere els
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 if (range.peek().blockType() == CSSParserToken::BlockEnd)
Timothy Loh 2015/10/15 03:43:48 I think this is never true and should be an assert
43 return false;
44
45 const CSSParserToken& token = range.consume();
46 switch (token.type()) {
47 case DelimiterToken: {
48 if (token.delimiter() == '!' && isTopLevelBlock)
49 return false;
50 break;
51 }
52 case RightParenthesisToken:
53 case RightBraceToken:
54 case RightBracketToken:
55 return false;
56 break;
Timothy Loh 2015/10/15 03:43:48 weird, just fall through to BadString and BadUrl
57 case BadStringToken:
58 case BadUrlToken:
59 return false;
60 case WhitespaceToken:
61 break;
62 case SemicolonToken:
63 if (isTopLevelBlock)
64 return false;
65 break;
66 default:
67 break;
68 }
69 }
70 return true;
71 }
72
73 bool isValidVariableReference(CSSParserTokenRange range)
74 {
75 range.consumeWhitespace();
76 if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace ()))
77 return false;
78 if (range.atEnd())
79 return true;
80
81 if (range.consume().type() != CommaToken)
82 return false;
83 if (range.atEnd())
84 return false;
85
86 bool hasReferences = false;
87 return classifyBlock(range, hasReferences);
88 }
89
90 static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasRefe rences)
91 {
92 hasReferences = false;
93
94 range.consumeWhitespace();
95 if (range.peek().type() == IdentToken) {
96 CSSValueID id = range.consumeIncludingWhitespace().id();
97 if (range.atEnd() && (id == CSSValueInherit || id == CSSValueInitial || id == CSSValueUnset))
98 return id;
99 }
100
101 if (classifyBlock(range, hasReferences))
102 return CSSValueInternalVariableValue;
103 return CSSValueInvalid;
104 }
105
106 bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange rang e)
107 {
108 bool hasReferences;
109 CSSValueID type = classifyVariableRange(range, hasReferences);
110 return type == CSSValueInternalVariableValue && hasReferences;
111 }
112
113 PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDec larationValue(const AtomicString& variableName, CSSParserTokenRange range)
114 {
115 if (range.atEnd())
116 return nullptr;
117
118 bool hasReferences;
119 CSSValueID type = classifyVariableRange(range, hasReferences);
120
121 if (type == CSSValueInvalid)
122 return nullptr;
123 if (type == CSSValueInternalVariableValue)
124 return CSSCustomPropertyDeclaration::create(variableName, CSSVariableDat a::create(range, hasReferences));
125 return CSSCustomPropertyDeclaration::create(variableName, type);
126 }
127
128 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698