LibWeb: Don't resolve color components when serializing color functions

Previously, we were incorrectly replacing color components with `None`
values with 0.
This commit is contained in:
Tim Ledbetter 2025-04-05 23:29:36 +01:00 committed by Sam Atkins
commit 55769be9c6
Notes: github-actions[bot] 2025-04-09 11:12:32 +00:00
2 changed files with 164 additions and 182 deletions

View file

@ -93,55 +93,37 @@ ColorFunctionStyleValue::Resolved ColorFunctionStyleValue::resolve_properties()
// https://www.w3.org/TR/css-color-4/#serializing-color-function-values
String ColorFunctionStyleValue::to_string(SerializationMode mode) const
{
if (mode == SerializationMode::Normal) {
auto convert_percentage = [](ValueComparingNonnullRefPtr<CSSStyleValue> const& value) -> RemoveReference<decltype(value)> {
if (value->is_percentage())
return NumberStyleValue::create(value->as_percentage().value() / 100);
return value;
};
auto convert_percentage = [](ValueComparingNonnullRefPtr<CSSStyleValue> const& value) -> RemoveReference<decltype(value)> {
if (value->is_percentage())
return NumberStyleValue::create(value->as_percentage().value() / 100);
return value;
};
auto alpha = convert_percentage(m_properties.alpha);
auto alpha = convert_percentage(m_properties.alpha);
bool const is_alpha_required = [&]() {
if (alpha->is_number())
return alpha->as_number().value() < 1;
return true;
}();
bool const is_alpha_required = [&]() {
if (alpha->is_number())
return alpha->as_number().value() < 1;
return true;
}();
if (alpha->is_number() && alpha->as_number().value() < 0)
alpha = NumberStyleValue::create(0);
if (alpha->is_number() && alpha->as_number().value() < 0)
alpha = NumberStyleValue::create(0);
if (is_alpha_required) {
return MUST(String::formatted("color({} {} {} {} / {})",
string_view_from_color_type(m_color_type),
convert_percentage(m_properties.channels[0])->to_string(mode),
convert_percentage(m_properties.channels[1])->to_string(mode),
convert_percentage(m_properties.channels[2])->to_string(mode),
alpha->to_string(mode)));
}
return MUST(String::formatted("color({} {} {} {})",
if (is_alpha_required) {
return MUST(String::formatted("color({} {} {} {} / {})",
string_view_from_color_type(m_color_type),
convert_percentage(m_properties.channels[0])->to_string(mode),
convert_percentage(m_properties.channels[1])->to_string(mode),
convert_percentage(m_properties.channels[2])->to_string(mode)));
convert_percentage(m_properties.channels[2])->to_string(mode),
alpha->to_string(mode)));
}
auto resolved = resolve_properties();
if (resolved.alpha == 1) {
return MUST(String::formatted("color({} {} {} {})",
string_view_from_color_type(m_color_type),
resolved.channels[0],
resolved.channels[1],
resolved.channels[2]));
}
return MUST(String::formatted("color({} {} {} {} / {})",
return MUST(String::formatted("color({} {} {} {})",
string_view_from_color_type(m_color_type),
resolved.channels[0],
resolved.channels[1],
resolved.channels[2],
resolved.alpha));
convert_percentage(m_properties.channels[0])->to_string(mode),
convert_percentage(m_properties.channels[1])->to_string(mode),
convert_percentage(m_properties.channels[2])->to_string(mode)));
}
Color ColorFunctionStyleValue::to_color(Optional<Layout::NodeWithStyle const&>) const