diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index fe4e618caf..0eb572e979 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -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 diff --git a/Libraries/LibWeb/CSS/StyleValues/ComputationContext.h b/Libraries/LibWeb/CSS/StyleValues/ComputationContext.h index 935504a5b3..fe70494837 100644 --- a/Libraries/LibWeb/CSS/StyleValues/ComputationContext.h +++ b/Libraries/LibWeb/CSS/StyleValues/ComputationContext.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include namespace Web::CSS { @@ -14,6 +15,7 @@ namespace Web::CSS { struct ComputationContext { Length::ResolutionContext length_resolution_context; Optional abstract_element {}; + Optional color_scheme {}; }; } diff --git a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp index ed2aa4f15b..e398256477 100644 --- a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2025, Sam Atkins + * Copyright (c) 2021-2026, Sam Atkins * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause @@ -10,6 +10,8 @@ #include "KeywordStyleValue.h" #include #include +#include +#include #include #include #include @@ -358,6 +360,35 @@ Optional KeywordStyleValue::to_color(ColorResolutionContext color_resolut } } +ValueComparingNonnullRefPtr 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 KeywordStyleValue::tokenize() const { return { Parser::Token::create_ident(FlyString::from_utf8_without_validation(string_from_keyword(m_keyword).bytes())) }; diff --git a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h index 281f66584e..e953ac4464 100644 --- a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h @@ -1,7 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2025, Sam Atkins + * Copyright (c) 2021-2026, Sam Atkins * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause @@ -50,6 +50,7 @@ public: static bool is_color(Keyword); virtual bool has_color() const override; virtual Optional to_color(ColorResolutionContext) const override; + virtual ValueComparingNonnullRefPtr absolutized(ComputationContext const&) const override; virtual void serialize(StringBuilder&, SerializationMode) const override; virtual Vector tokenize() const override; virtual GC::Ref reify(JS::Realm&, FlyString const& associated_property) const override; diff --git a/Tests/LibWeb/Text/expected/css-accent-color.txt b/Tests/LibWeb/Text/expected/css-accent-color.txt index b798ef83ac..ea51c08d93 100644 --- a/Tests/LibWeb/Text/expected/css-accent-color.txt +++ b/Tests/LibWeb/Text/expected/css-accent-color.txt @@ -1,2 +1,2 @@ Before bgColor=rgb(105, 105, 105) -After bgColor=rgb(0, 0, 255) +After bgColor=rgb(61, 174, 233)