Commit 37a5cf6a authored by Rune Lillesveen's avatar Rune Lillesveen Committed by Commit Bot

Use a separate CSSValue class for -internal-light-dark-color.

This is necessary for a correct serialization of colors in devtools which
would otherwise serialize as simple space separated color values.

Bug: 939811
Change-Id: If9f3cc7d42c9c603ac3e743037ed23f2bebb7226
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1789393
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Reviewed-by: default avatarMorten Stenshorne <mstensho@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695097}
parent dee33c39
......@@ -106,6 +106,8 @@ blink_core_sources("css") {
"css_keyframes_rule.h",
"css_layout_function_value.cc",
"css_layout_function_value.h",
"css_light_dark_color_pair.cc",
"css_light_dark_color_pair.h",
"css_markup.cc",
"css_markup.h",
"css_math_expression_node.cc",
......@@ -590,6 +592,7 @@ blink_core_tests("unit_tests") {
"css_font_face_source_test.cc",
"css_gradient_value_test.cc",
"css_invalid_variable_value_test.cc",
"css_light_dark_color_pair_test.cc",
"css_math_expression_node_test.cc",
"css_page_rule_test.cc",
"css_paint_value_test.cc",
......
// Copyright 2019 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/css_light_dark_color_pair.h"
namespace blink {
String CSSLightDarkColorPair::CustomCSSText() const {
String first = First().CssText();
String second = Second().CssText();
return "-internal-light-dark-color(" + first + ", " + second + ")";
}
} // namespace blink
// Copyright 2019 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_LIGHT_DARK_COLOR_PAIR_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_LIGHT_DARK_COLOR_PAIR_H_
#include "third_party/blink/renderer/core/css/css_value_pair.h"
namespace blink {
class CORE_EXPORT CSSLightDarkColorPair : public CSSValuePair {
public:
CSSLightDarkColorPair(const CSSValue* first, const CSSValue* second)
: CSSValuePair(kLightDarkColorPairClass, first, second) {}
String CustomCSSText() const;
void TraceAfterDispatch(blink::Visitor* visitor) {
CSSValuePair::TraceAfterDispatch(visitor);
}
};
template <>
struct DowncastTraits<CSSLightDarkColorPair> {
static bool AllowFrom(const CSSValue& value) {
return value.IsLightDarkColorPair();
}
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_LIGHT_DARK_COLOR_PAIR_H_
// Copyright 2019 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/css_light_dark_color_pair.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
namespace blink {
namespace {
const CSSValue* CreateLightDarkColorPair(const char* value) {
auto* ua_context = MakeGarbageCollected<CSSParserContext>(
kUASheetMode, SecureContextMode::kInsecureContext);
return CSSParser::ParseSingleValue(CSSPropertyID::kColor, value, ua_context);
}
TEST(CSSLightDarkColorPairTest, Equals) {
const auto* value1 =
CreateLightDarkColorPair("-internal-light-dark-color(red, green)");
const auto* value2 =
CreateLightDarkColorPair("-internal-light-dark-color(red, green)");
const auto* value3 =
CreateLightDarkColorPair("-internal-light-dark-color(#000, #fff)");
const auto* value4 = CreateLightDarkColorPair(
"-internal-light-dark-color(rgb(0, 0, 0), rgb(255, 255, 255))");
ASSERT_TRUE(value1);
ASSERT_TRUE(value2);
ASSERT_TRUE(value3);
ASSERT_TRUE(value4);
EXPECT_TRUE(*value1 == *value1);
EXPECT_TRUE(*value1 == *value2);
EXPECT_TRUE(*value3 == *value3);
EXPECT_TRUE(*value4 == *value4);
EXPECT_TRUE(*value3 == *value4);
}
} // namespace
} // namespace blink
......@@ -55,6 +55,7 @@
#include "third_party/blink/renderer/core/css/css_invalid_variable_value.h"
#include "third_party/blink/renderer/core/css/css_keyframe_shorthand_value.h"
#include "third_party/blink/renderer/core/css/css_layout_function_value.h"
#include "third_party/blink/renderer/core/css/css_light_dark_color_pair.h"
#include "third_party/blink/renderer/core/css/css_math_function_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_paint_value.h"
......@@ -263,6 +264,8 @@ bool CSSValue::operator==(const CSSValue& other) const {
return CompareCSSValues<CSSPendingSubstitutionValue>(*this, other);
case kInvalidVariableValueClass:
return CompareCSSValues<CSSInvalidVariableValue>(*this, other);
case kLightDarkColorPairClass:
return CompareCSSValues<CSSLightDarkColorPair>(*this, other);
}
NOTREACHED();
return false;
......@@ -378,6 +381,8 @@ String CSSValue::CssText() const {
return To<CSSPendingSubstitutionValue>(this)->CustomCSSText();
case kInvalidVariableValueClass:
return To<CSSInvalidVariableValue>(this)->CustomCSSText();
case kLightDarkColorPairClass:
return To<CSSLightDarkColorPair>(this)->CustomCSSText();
}
NOTREACHED();
return String();
......@@ -545,6 +550,9 @@ void CSSValue::FinalizeGarbageCollectedObject() {
case kInvalidVariableValueClass:
To<CSSInvalidVariableValue>(this)->~CSSInvalidVariableValue();
return;
case kLightDarkColorPairClass:
To<CSSLightDarkColorPair>(this)->~CSSLightDarkColorPair();
return;
}
NOTREACHED();
}
......@@ -710,6 +718,9 @@ void CSSValue::Trace(blink::Visitor* visitor) {
case kInvalidVariableValueClass:
To<CSSInvalidVariableValue>(this)->TraceAfterDispatch(visitor);
return;
case kLightDarkColorPairClass:
To<CSSLightDarkColorPair>(this)->TraceAfterDispatch(visitor);
return;
}
NOTREACHED();
}
......
......@@ -171,6 +171,9 @@ class CORE_EXPORT CSSValue : public GarbageCollectedFinalized<CSSValue> {
bool IsShorthandWrapperValue() const {
return class_type_ == kKeyframeShorthandClass;
}
bool IsLightDarkColorPair() const {
return class_type_ == kLightDarkColorPairClass;
}
bool HasFailedOrCanceledSubresources() const;
bool MayContainUrl() const;
......@@ -201,6 +204,7 @@ class CORE_EXPORT CSSValue : public GarbageCollectedFinalized<CSSValue> {
kStringClass,
kURIClass,
kValuePairClass,
kLightDarkColorPairClass,
// Basic shape classes.
// TODO(sashab): Represent these as a single subclass, BasicShapeClass.
......
......@@ -66,6 +66,18 @@ class CORE_EXPORT CSSValuePair : public CSSValue {
void TraceAfterDispatch(blink::Visitor*);
protected:
CSSValuePair(ClassType class_type,
const CSSValue* first,
const CSSValue* second)
: CSSValue(class_type),
first_(first),
second_(second),
identical_values_policy_(kKeepIdenticalValues) {
DCHECK(first_);
DCHECK(second_);
}
private:
Member<const CSSValue> first_;
Member<const CSSValue> second_;
......
......@@ -11,6 +11,7 @@
#include "third_party/blink/renderer/core/css/css_image_set_value.h"
#include "third_party/blink/renderer/core/css/css_image_value.h"
#include "third_party/blink/renderer/core/css/css_initial_value.h"
#include "third_party/blink/renderer/core/css/css_light_dark_color_pair.h"
#include "third_party/blink/renderer/core/css/css_math_expression_node.h"
#include "third_party/blink/renderer/core/css/css_math_function_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
......@@ -859,8 +860,8 @@ static bool ParseColorFunction(CSSParserTokenRange& range, RGBA32& result) {
return true;
}
static CSSValuePair* ParseLightDarkColor(CSSParserTokenRange& range,
CSSParserMode mode) {
static CSSLightDarkColorPair* ParseLightDarkColor(CSSParserTokenRange& range,
CSSParserMode mode) {
if (range.Peek().FunctionId() != CSSValueID::kInternalLightDarkColor)
return nullptr;
if (!isValueAllowedInMode(CSSValueID::kInternalLightDarkColor, mode))
......@@ -872,8 +873,7 @@ static CSSValuePair* ParseLightDarkColor(CSSParserTokenRange& range,
CSSValue* dark_color = ConsumeColor(args, kUASheetMode);
if (!dark_color || !args.AtEnd())
return nullptr;
return MakeGarbageCollected<CSSValuePair>(light_color, dark_color,
CSSValuePair::kKeepIdenticalValues);
return MakeGarbageCollected<CSSLightDarkColorPair>(light_color, dark_color);
}
CSSValue* ConsumeColor(CSSParserTokenRange& range,
......
......@@ -714,4 +714,15 @@ TEST(CSSPropertyParserTest, UAInternalLightDarkColor) {
}
}
TEST(CSSPropertyParserTest, UAInternalLightDarkColorSerialization) {
auto* ua_context = MakeGarbageCollected<CSSParserContext>(
kUASheetMode, SecureContextMode::kInsecureContext);
const CSSValue* value = CSSParser::ParseSingleValue(
CSSPropertyID::kColor, "-internal-light-dark-color(red,#aaa)",
ua_context);
ASSERT_TRUE(value);
EXPECT_EQ("-internal-light-dark-color(red, rgb(170, 170, 170))",
value->CssText());
}
} // namespace blink
......@@ -31,7 +31,7 @@
#include "third_party/blink/renderer/core/css/css_color_value.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_value_pair.h"
#include "third_party/blink/renderer/core/css/css_light_dark_color_pair.h"
#include "third_party/blink/renderer/core/css/style_color.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
......@@ -65,7 +65,7 @@ Color TextLinkColors::ColorFromCSSValue(const CSSValue& value,
if (auto* color_value = DynamicTo<CSSColorValue>(value))
return color_value->Value();
if (auto* pair = DynamicTo<CSSValuePair>(value)) {
if (auto* pair = DynamicTo<CSSLightDarkColorPair>(value)) {
const CSSValue& color_value =
color_scheme == WebColorScheme::kLight ? pair->First() : pair->Second();
return ColorFromCSSValue(color_value, current_color, color_scheme,
......
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