Skip to content

Truly independent web browser

LibWeb: Avoid reconstructing shorthands from unresolved longhands

Callum Law5ef9aee6f07e
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
index e4d92f22169..10cd390e9e8 100644
--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
@@ -1781,6 +1781,13 @@ Utf16String CSSStyleProperties::serialize_a_css_value_to_utf16(Vector<StylePrope
if (!shorthand.has_value())
return {};
+ // https://drafts.csswg.org/css-values-5/#pending-substitution-value
+ // If any of the component longhand properties for a given shorthand are pending-substitution
+ // values, or contain arbitrary substitution functions of their own that have not yet been substituted, the
+ // shorthand property must serialize to the empty string.
+ if (any_of(list, [](auto const& declaration) { return declaration.value->is_pending_substitution() || declaration.value->is_unresolved(); }))
+ return {};
+
// 3. Otherwise, serialize a CSS value from a hypothetical declaration of the property shorthand with its value representing the combined values of the declarations in list.
Function<ValueComparingNonnullRefPtr<ShorthandStyleValue const>(PropertyID)> make_shorthand_value = [&](PropertyID shorthand_id) {
auto longhand_ids = longhands_for_shorthand(shorthand_id);
diff --git a/Tests/LibWeb/Text/expected/css/shorthand-unresolved-longhands.txt b/Tests/LibWeb/Text/expected/css/shorthand-unresolved-longhands.txt
new file mode 100644
index 00000000000..4c410adbc3f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/shorthand-unresolved-longhands.txt
@@ -0,0 +1,18 @@
+grid: none; grid-auto-flow: var(--flow)
+grid: ""
+cssText: grid-auto-rows: auto; grid-auto-columns: auto; grid-template: none; grid-auto-flow: var(--flow);
+grid: none; grid-auto-flow: var(--flow); grid-auto-rows: 20px
+grid: ""
+cssText: grid-auto-columns: auto; grid-template: none; grid-auto-flow: var(--flow); grid-auto-rows: 20px;
+flex: 1 1 auto; flex-grow: var(--grow)
+flex: ""
+cssText: flex-shrink: 1; flex-basis: auto; flex-grow: var(--grow);
+margin: 1px; margin-left: var(--left)
+margin: ""
+cssText: margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: var(--left);
+grid-template: var(--template); grid-auto-flow: column; grid-auto-rows: auto; grid-auto-columns: 20px
+grid: ""
+cssText: grid-template: var(--template); grid-auto-flow: column; grid-auto-rows: auto; grid-auto-columns: 20px;
+grid: var(--grid)
+grid: "var(--grid)"
+cssText: grid: var(--grid);
diff --git a/Tests/LibWeb/Text/expected/css/style-attribute-serialization-with-var-in-grid-properties.txt b/Tests/LibWeb/Text/expected/css/style-attribute-serialization-with-var-in-grid-properties.txt
index e23bf33ba20..35f158e893c 100644
--- a/Tests/LibWeb/Text/expected/css/style-attribute-serialization-with-var-in-grid-properties.txt
+++ b/Tests/LibWeb/Text/expected/css/style-attribute-serialization-with-var-in-grid-properties.txt
@@ -1 +1 @@
-grid-template: var(--foo) var(--baz) var(--bar);
+grid-template-areas: var(--foo); grid-template-columns: var(--bar); grid-template-rows: var(--baz);
diff --git a/Tests/LibWeb/Text/input/css/shorthand-unresolved-longhands.html b/Tests/LibWeb/Text/input/css/shorthand-unresolved-longhands.html
new file mode 100644
index 00000000000..8763341d84a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/shorthand-unresolved-longhands.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+test(() => {
+ const style = document.createElement("div").style;
+ for (const [property, source] of [
+ ["grid", "grid: none; grid-auto-flow: var(--flow)"],
+ ["grid", "grid: none; grid-auto-flow: var(--flow); grid-auto-rows: 20px"],
+ ["flex", "flex: 1 1 auto; flex-grow: var(--grow)"],
+ ["margin", "margin: 1px; margin-left: var(--left)"],
+ ["grid", "grid-template: var(--template); grid-auto-flow: column; grid-auto-rows: auto; grid-auto-columns: 20px"],
+ ["grid", "grid: var(--grid)"],
+ ]) {
+ style.cssText = source;
+ println(source);
+ println(`${property}: "${style.getPropertyValue(property)}"`);
+ println(`cssText: ${style.cssText}`);
+ }
+});
+</script>