LibWeb: Handle nested shorthands in all-same-CSS-wide-keyword to_string

Previously we only checked direct sub-properties, not accounting for the
nested case.
This commit is contained in:
Callum Law 2025-06-10 17:38:55 +12:00 committed by Sam Atkins
commit 335190e925
Notes: github-actions[bot] 2025-06-16 11:39:18 +00:00
3 changed files with 26 additions and 8 deletions

View file

@ -8,6 +8,7 @@
#include "ShorthandStyleValue.h" #include "ShorthandStyleValue.h"
#include <LibGfx/Font/FontWeight.h> #include <LibGfx/Font/FontWeight.h>
#include <LibWeb/CSS/PropertyID.h> #include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h> #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h> #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h> #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
@ -43,23 +44,25 @@ String ShorthandStyleValue::to_string(SerializationMode mode) const
// If all the longhands are the same CSS-wide keyword, just return that once. // If all the longhands are the same CSS-wide keyword, just return that once.
Optional<Keyword> built_in_keyword; Optional<Keyword> built_in_keyword;
bool all_same_keyword = true; bool all_same_keyword = true;
for (auto& value : m_properties.values) { StyleComputer::for_each_property_expanding_shorthands(m_properties.shorthand_property, *this, [&](PropertyID name, CSSStyleValue const& value) {
if (!value->is_css_wide_keyword()) { (void)name;
if (!value.is_css_wide_keyword()) {
all_same_keyword = false; all_same_keyword = false;
break; return;
} }
auto keyword = value->to_keyword(); auto keyword = value.to_keyword();
if (!built_in_keyword.has_value()) { if (!built_in_keyword.has_value()) {
built_in_keyword = keyword; built_in_keyword = keyword;
continue; return;
} }
if (built_in_keyword != keyword) { if (built_in_keyword != keyword) {
all_same_keyword = false; all_same_keyword = false;
break; return;
}
} }
});
if (all_same_keyword && built_in_keyword.has_value()) if (all_same_keyword && built_in_keyword.has_value())
return m_properties.values.first()->to_string(mode); return MUST(String::from_utf8(string_from_keyword(built_in_keyword.value())));
auto default_to_string = [&]() { auto default_to_string = [&]() {
auto all_properties_same_value = true; auto all_properties_same_value = true;

View file

@ -0,0 +1 @@
#a { background: initial; }

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<style>
#a {
background: initial;
}
</style>
<script src="../include.js"></script>
<script>
test(() => {
println(document.styleSheets[0].cssRules[0].cssText);
});
</script>
</html>