ladybird/Libraries/LibWeb/CSS/StyleValues/CSSLightDark.cpp
Callum Law 6a9c8d7767 LibWeb: Don't resolve colors with unresolved components
`CSSColorValue`s which have unresolved `calc` components should be able
to be resolved. Previously we would always resolve them but with
incorrect values.

This is useful as we will now be able to now whether we should serialize
colors in their normalized form or not.

Slight regression in that we now serialize (RGB, HSL and HWB) colors
with components that rely on compute-time information as an empty
string, but that will be fixed in the next commit.
2025-07-16 13:05:33 +01:00

37 lines
1.2 KiB
C++

/*
* Copyright (c) 2025, Ladybird contributors
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSLightDark.h"
#include <LibWeb/Layout/Node.h>
namespace Web::CSS {
Optional<Color> CSSLightDark::to_color(Optional<Layout::NodeWithStyle const&> node, CalculationResolutionContext const& resolution_context) const
{
if (node.has_value() && node.value().computed_values().color_scheme() == PreferredColorScheme::Dark)
return m_properties.dark->to_color(node, resolution_context);
return m_properties.light->to_color(node, resolution_context);
}
bool CSSLightDark::equals(CSSStyleValue const& other) const
{
if (type() != other.type())
return false;
auto const& other_color = other.as_color();
if (color_type() != other_color.color_type())
return false;
auto const& other_light_dark = as<CSSLightDark>(other_color);
return m_properties == other_light_dark.m_properties;
}
String CSSLightDark::to_string(SerializationMode mode) const
{
// FIXME: We don't have enough information to determine the computed value here.
return MUST(String::formatted("light-dark({}, {})", m_properties.light->to_string(mode), m_properties.dark->to_string(mode)));
}
}