Commit 01b3033e authored by Anders Hartvoll Ruud's avatar Anders Hartvoll Ruud Committed by Commit Bot

Use a builder-like pattern for CSSParserLocalContext.

It will soon be necessary to add more flags to CSSParserLocalContext in
order to parse custom properties via the CustomProperty class. Those flags
are not interesting to non-custom call sites, so it's more convenient if
CSSParserLocalContext is a class with reasonable defaults that can be
overridden.

Change-Id: I078d8faa4cbce5de8db24b60e0817625d7deae5f
Reviewed-on: https://chromium-review.googlesource.com/c/1341914
Commit-Queue: Anders Ruud <andruud@chromium.org>
Reviewed-by: default avatarRune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609982}
parent 191b339d
......@@ -1762,6 +1762,7 @@ jumbo_source_set("unit_tests") {
"css/media_values_test.cc",
"css/parser/css_lazy_parsing_test.cc",
"css/parser/css_parser_fast_paths_test.cc",
"css/parser/css_parser_local_context_test.cc",
"css/parser/css_parser_token_stream_test.cc",
"css/parser/css_parser_token_test.cc",
"css/parser/css_property_parser_test.cc",
......
......@@ -9,10 +9,19 @@ namespace blink {
CSSParserLocalContext::CSSParserLocalContext()
: use_alias_parsing_(false), current_shorthand_(CSSPropertyInvalid) {}
CSSParserLocalContext::CSSParserLocalContext(bool use_alias_parsing,
CSSPropertyID current_shorthand)
: use_alias_parsing_(use_alias_parsing),
current_shorthand_(current_shorthand) {}
CSSParserLocalContext CSSParserLocalContext::WithAliasParsing(
bool use_alias_parsing) const {
CSSParserLocalContext context = *this;
context.use_alias_parsing_ = use_alias_parsing;
return context;
}
CSSParserLocalContext CSSParserLocalContext::WithCurrentShorthand(
CSSPropertyID current_shorthand) const {
CSSParserLocalContext context = *this;
context.current_shorthand_ = current_shorthand;
return context;
}
bool CSSParserLocalContext::UseAliasParsing() const {
return use_alias_parsing_;
......
......@@ -12,13 +12,15 @@ namespace blink {
// A wrapper class containing all local context when parsing a property.
class CSSParserLocalContext {
class CORE_EXPORT CSSParserLocalContext {
STACK_ALLOCATED();
public:
CSSParserLocalContext();
CSSParserLocalContext(bool use_alias_parsing,
CSSPropertyID current_shorthand);
CSSParserLocalContext WithAliasParsing(bool) const;
CSSParserLocalContext WithCurrentShorthand(CSSPropertyID) const;
bool UseAliasParsing() const;
CSSPropertyID CurrentShorthand() const;
......
// Copyright 2018 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 "third_party/blink/renderer/core/css/parser/css_parser_local_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(CSSParserLocalContextTest, Constructor) {
EXPECT_FALSE(CSSParserLocalContext().UseAliasParsing());
EXPECT_EQ(CSSPropertyInvalid, CSSParserLocalContext().CurrentShorthand());
}
TEST(CSSParserLocalContextTest, WithAliasParsing) {
const CSSParserLocalContext context;
EXPECT_FALSE(context.WithAliasParsing(false).UseAliasParsing());
EXPECT_TRUE(context.WithAliasParsing(true).UseAliasParsing());
}
TEST(CSSParserLocalContextTest, WithCurrentShorthand) {
const CSSParserLocalContext context;
const CSSPropertyID shorthand = CSSPropertyBackground;
EXPECT_EQ(shorthand,
context.WithCurrentShorthand(shorthand).CurrentShorthand());
}
TEST(CSSParserLocalContextTest, LocalMutation) {
CSSParserLocalContext context;
context = context.WithAliasParsing(true);
context = context.WithCurrentShorthand(CSSPropertyBackground);
// WithAliasParsing only changes that member.
EXPECT_EQ(CSSPropertyBackground,
context.WithAliasParsing(false).CurrentShorthand());
// WithCurrentShorthand only changes that member.
EXPECT_TRUE(
context.WithCurrentShorthand(CSSPropertyInvalid).UseAliasParsing());
}
} // namespace blink
......@@ -96,13 +96,14 @@ bool CSSPropertyParser::ParseValueStart(CSSPropertyID unresolved_property,
bool is_shorthand = property.IsShorthand();
DCHECK(context_);
if (is_shorthand) {
const auto local_context =
CSSParserLocalContext()
.WithAliasParsing(isPropertyAlias(unresolved_property))
.WithCurrentShorthand(property_id);
// Variable references will fail to parse here and will fall out to the
// variable ref parser below.
if (ToShorthand(property).ParseShorthand(
important, range_, *context_,
CSSParserLocalContext(isPropertyAlias(unresolved_property),
property_id),
*parsed_properties_))
important, range_, *context_, local_context, *parsed_properties_))
return true;
} else {
if (const CSSValue* parsed_value = ParseLonghand(
......
......@@ -1802,12 +1802,13 @@ const CSSValue* ParseLonghand(CSSPropertyID unresolved_property,
return nullptr;
}
const CSSValue* result =
ToLonghand(CSSProperty::Get(property_id))
.ParseSingleValue(
range, context,
CSSParserLocalContext(isPropertyAlias(unresolved_property),
current_shorthand));
const auto local_context =
CSSParserLocalContext()
.WithAliasParsing(isPropertyAlias(unresolved_property))
.WithCurrentShorthand(current_shorthand);
const CSSValue* result = ToLonghand(CSSProperty::Get(property_id))
.ParseSingleValue(range, context, local_context);
return result;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment