LibWeb/CSS: Absolutize color KeywordStyleValues

These get computed to an equivalent RGBColorStyleValue.

To support this, we now store the computed color-scheme on the
ComputationContext when computing properties that might contain a color.

This has a nice bonus of correcting the css-accent-color test's result.
This commit is contained in:
Sam Atkins 2026-02-02 11:25:56 +00:00
parent 7ba7377e19
commit 92897a1dec
Notes: github-actions[bot] 2026-02-06 10:32:27 +00:00
5 changed files with 41 additions and 5 deletions

View file

@ -799,7 +799,8 @@ void StyleComputer::collect_animation_into(DOM::AbstractElement abstract_element
.viewport_rect = viewport_rect(),
.font_metrics = font_metrics,
.root_font_metrics = m_root_element_font_metrics },
.abstract_element = abstract_element
.abstract_element = abstract_element,
.color_scheme = computed_properties.color_scheme(document().page().preferred_color_scheme(), document().supported_color_schemes()),
};
// NOTE: This doesn't necessarily return the specified value if we reach into computed_properties but that
@ -1569,7 +1570,8 @@ void StyleComputer::compute_property_values(ComputedProperties& style, Optional<
.font_metrics = font_metrics,
.root_font_metrics = m_root_element_font_metrics,
},
.abstract_element = abstract_element
.abstract_element = abstract_element,
.color_scheme = style.color_scheme(document().page().preferred_color_scheme(), document().supported_color_schemes())
};
// NOTE: This doesn't necessarily return the specified value if we have already computed this property but that

View file

@ -7,6 +7,7 @@
#pragma once
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/DOM/AbstractElement.h>
namespace Web::CSS {
@ -14,6 +15,7 @@ namespace Web::CSS {
struct ComputationContext {
Length::ResolutionContext length_resolution_context;
Optional<DOM::AbstractElement> abstract_element {};
Optional<PreferredColorScheme> color_scheme {};
};
}

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -10,6 +10,8 @@
#include "KeywordStyleValue.h"
#include <LibGfx/Palette.h>
#include <LibWeb/CSS/CSSKeywordValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/RGBColorStyleValue.h>
#include <LibWeb/CSS/SystemColor.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Layout/Node.h>
@ -358,6 +360,35 @@ Optional<Color> KeywordStyleValue::to_color(ColorResolutionContext color_resolut
}
}
ValueComparingNonnullRefPtr<StyleValue const> KeywordStyleValue::absolutized(ComputationContext const& context) const
{
if (!has_color())
return *this;
// The currentcolor keyword computes to itself.
// https://drafts.csswg.org/css-color-4/#resolving-other-colors
if (keyword() == Keyword::Currentcolor)
return *this;
ColorResolutionContext color_resolution_context;
if (context.abstract_element.has_value()) {
color_resolution_context.document = context.abstract_element->document();
color_resolution_context.calculation_resolution_context = CalculationResolutionContext::from_computation_context(context);
color_resolution_context.color_scheme = context.color_scheme;
}
auto resolved_color = to_color(color_resolution_context);
if (!resolved_color.has_value())
return *this;
return RGBColorStyleValue::create(
NumberStyleValue::create(resolved_color->red()),
NumberStyleValue::create(resolved_color->green()),
NumberStyleValue::create(resolved_color->blue()),
NumberStyleValue::create(resolved_color->alpha() / 255.0f),
ColorSyntax::Legacy);
}
Vector<Parser::ComponentValue> KeywordStyleValue::tokenize() const
{
return { Parser::Token::create_ident(FlyString::from_utf8_without_validation(string_from_keyword(m_keyword).bytes())) };

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -50,6 +50,7 @@ public:
static bool is_color(Keyword);
virtual bool has_color() const override;
virtual Optional<Color> to_color(ColorResolutionContext) const override;
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
virtual void serialize(StringBuilder&, SerializationMode) const override;
virtual Vector<Parser::ComponentValue> tokenize() const override;
virtual GC::Ref<CSSStyleValue> reify(JS::Realm&, FlyString const& associated_property) const override;

View file

@ -1,2 +1,2 @@
Before bgColor=rgb(105, 105, 105)
After bgColor=rgb(0, 0, 255)
After bgColor=rgb(61, 174, 233)