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

Unified 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: Post-merge Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/css/parser/CSSVariableParser.cpp
diff --git a/third_party/WebKit/Source/core/css/parser/CSSVariableParser.cpp b/third_party/WebKit/Source/core/css/parser/CSSVariableParser.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..55db1d66a4592cdfbeb3cb998e19b82c95699bd7
--- /dev/null
+++ b/third_party/WebKit/Source/core/css/parser/CSSVariableParser.cpp
@@ -0,0 +1,190 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/css/parser/CSSVariableParser.h"
+
+#include "core/css/CSSCustomPropertyDeclaration.h"
+#include "core/css/parser/CSSParserTokenRange.h"
+#include "core/css/parser/CSSParserValues.h"
+
+namespace blink {
+
+static bool isValidVariableName(const CSSParserToken& token)
+{
+ if (token.type() != IdentToken)
+ return false;
+
+ CSSParserString value = token.value();
+ return value.length() >= 2 && value[0] == '-' && value[1] == '-';
+}
+
+static bool isValidVariableReference(CSSParserTokenRange range)
+{
+ range.consumeWhitespace();
+ if (!isValidVariableName(range.consumeIncludingWhitespace()))
+ return false;
+ if (range.atEnd())
+ return true;
+
+ range.consumeWhitespace();
Timothy Loh 2015/09/30 02:09:28 This is a no-op because we used consumeIncludingWh
+ if (range.consume().type() != CommaToken)
+ return false;
+ if (range.atEnd())
+ return false;
+
+ int blockNestingLevel = 0;
Timothy Loh 2015/09/30 02:09:28 Can we just call classifyBlock recursively here?
+ while (!range.atEnd()) {
+ const CSSParserToken& token = range.consume();
+
+ if (token.blockType() == CSSParserToken::BlockStart) {
+ blockNestingLevel++;
+ } else if (token.blockType() == CSSParserToken::BlockEnd) {
+ blockNestingLevel--;
+ if (blockNestingLevel < 0)
+ return false;
+ }
+
+ switch (token.type()) {
+ case DelimiterToken:
+ if (token.delimiter() == '!' && !blockNestingLevel)
+ return false;
+ break;
+ case BadStringToken:
+ case BadUrlToken:
+ return false;
+ case CommentToken:
+ case EOFToken:
+ ASSERT_NOT_REACHED();
+ case SemicolonToken:
+ if (!blockNestingLevel)
+ return false;
+ break;
+ default:
+ break;
+ }
+ }
+ return true;
+}
+
+bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, bool isTopLevelBlock = true)
+{
+ int parenthesisCount = 0;
+ int braceCount = 0;
+ int bracketCount = 0;
+
+ // TODO(leviw): Use consumeBlock to keep track of nesting instead of doing it directly here.
+ while (!range.atEnd()) {
+ if (range.peek().blockType() == CSSParserToken::BlockStart) {
+ const CSSParserToken& token = range.peek();
+ CSSParserTokenRange block = range.consumeBlock();
+ if (token.type() == FunctionToken && token.valueEqualsIgnoringCase("var")) {
+ if (!isValidVariableReference(block))
+ return false; // Bail if any references are invalid
+ hasReferences = true;
+ continue;
+ }
+ if (!classifyBlock(block, hasReferences, false))
+ return false;
+ continue;
+ }
+
+ const CSSParserToken& token = range.consume();
+ switch (token.type()) {
+ case FunctionToken:
+ ASSERT_NOT_REACHED();
+ break;
+ case CommentToken:
Timothy Loh 2015/09/30 02:09:28 I would probably leave out the ASSERT_NOT_REACHED
+ case EOFToken:
+ ASSERT_NOT_REACHED();
+ case DelimiterToken: {
+ if (token.delimiter() == '!' && isTopLevelBlock)
+ return false;
+ break;
+ }
+ case RightParenthesisToken:
+ if (!parenthesisCount)
+ return false;
+ parenthesisCount--;
+ break;
+ case LeftParenthesisToken:
+ parenthesisCount++;
+ break;
+ case RightBraceToken:
+ if (!braceCount)
+ return false;
+ braceCount--;
+ break;
+ case LeftBraceToken:
+ braceCount++;
+ break;
+ case RightBracketToken:
+ if (!bracketCount)
+ return false;
+ bracketCount--;
+ break;
+ case LeftBracketToken:
+ bracketCount++;
+ break;
+ case BadStringToken:
+ case BadUrlToken:
+ return false;
+ case WhitespaceToken:
+ break;
+ case SemicolonToken:
+ if (isTopLevelBlock)
+ return false;
+ break;
+ default:
+ break;
+ }
+ }
+ return true;
+}
+
+static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasReferences)
+{
+ hasReferences = false;
+
+ range.consumeWhitespace();
+ if (range.peek().type() == IdentToken) {
+ const CSSParserToken& ident = range.consumeIncludingWhitespace();
Timothy Loh 2015/09/30 02:09:28 We have a helper function now so we can write CSS
+ if (range.atEnd()) {
+ if (ident.valueEqualsIgnoringCase("inherit"))
+ return CSSValueInherit;
+ if (ident.valueEqualsIgnoringCase("initial"))
+ return CSSValueInitial;
+ if (ident.valueEqualsIgnoringCase("unset"))
+ return CSSValueUnset;
+ }
+ }
+
+ if (classifyBlock(range, hasReferences))
+ return CSSValueInternalVariableValue;
+ return CSSValueInvalid;
+}
+
+bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange range)
+{
+ bool hasReferences;
+ CSSValueID type = classifyVariableRange(range, hasReferences);
+ return type == CSSValueInternalVariableValue && hasReferences;
+}
+
+PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDeclarationValue(const AtomicString& variableName, CSSParserTokenRange range)
+{
+ if (range.atEnd())
+ return nullptr;
+
+ bool hasReferences;
+ CSSValueID type = classifyVariableRange(range, hasReferences);
+
+ if (type == CSSValueInvalid)
+ return nullptr;
+ if (type == CSSValueInternalVariableValue)
+ return CSSCustomPropertyDeclaration::create(variableName, CSSVariableData::create(range, hasReferences));
+ return CSSCustomPropertyDeclaration::create(variableName, type);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698