From 9453c259252da1f99b358da3fdf2bd7028ce82c9 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 29 Nov 2024 13:46:37 +0000 Subject: [PATCH] LibWeb/CSS: Don't repeat CSS-wide keywords when serializing shorthands Regardless of what the shorthand property is, if all its longhands are the same CSS-wide keyword such as "initial" or "inherit", then it's the same as the shorthand being that value. This gets us 2 WPT subtest passes. --- .../CSS/StyleValues/ShorthandStyleValue.cpp | 23 ++++++++++++++++++- .../wpt-import/css/cssom/serialize-values.txt | 8 +++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp index 2ae516cdfc7..2afb72ad695 100644 --- a/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp @@ -38,7 +38,28 @@ ValueComparingRefPtr ShorthandStyleValue::longhand(Property String ShorthandStyleValue::to_string() const { - // Special-cases first + // If all the longhands are the same CSS-wide keyword, just return that once. + Optional built_in_keyword; + bool all_same_keyword = true; + for (auto& value : m_properties.values) { + if (!value->is_css_wide_keyword()) { + all_same_keyword = false; + break; + } + auto keyword = value->to_keyword(); + if (!built_in_keyword.has_value()) { + built_in_keyword = keyword; + continue; + } + if (built_in_keyword != keyword) { + all_same_keyword = false; + break; + } + } + if (all_same_keyword && built_in_keyword.has_value()) + return m_properties.values.first()->to_string(); + + // Then special cases switch (m_properties.shorthand_property) { case PropertyID::Background: { auto color = longhand(PropertyID::BackgroundColor); diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/cssom/serialize-values.txt b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/serialize-values.txt index 9ada2785f50..1e60246dcf5 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/css/cssom/serialize-values.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/serialize-values.txt @@ -6,8 +6,8 @@ Rerun Found 687 tests -471 Pass -216 Fail +473 Pass +214 Fail Details Result Test Name MessagePass background-attachment: scroll Pass background-attachment: fixed @@ -191,7 +191,7 @@ Fail background-position: right -.1em Fail background-position: right top Fail background-position: right center Fail background-position: right bottom -Fail background-position: inherit +Pass background-position: inherit Pass background-repeat: repeat Pass background-repeat: repeat-x Pass background-repeat: repeat-y @@ -630,7 +630,7 @@ Fail text-decoration: underline Fail text-decoration: overline Fail text-decoration: line-through Fail text-decoration: blink -Fail text-decoration: inherit +Pass text-decoration: inherit Pass text-indent: 0px Pass text-indent: 1px Pass text-indent: .1em