Skip to content

Truly independent web browser

LibWeb: Preserve important custom properties while parsing

sideshowbarkerdb70c91e8668
diff --git a/Libraries/LibWeb/Rust/src/css/declaration_block.rs b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
index d683ab35716..430c1526718 100644
--- a/Libraries/LibWeb/Rust/src/css/declaration_block.rs
+++ b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
@@ -711,7 +711,18 @@ impl DeclarationBlockBuilder {
self.append_normalized_property(declaration.to_declared());
}
for property in other.custom_properties().iter() {
- self.set_custom_property(property.name.clone(), property.declaration.to_declared());
+ self.append_custom_property(property.name.clone(), property.declaration.to_declared());
+ }
+ }
+
+ pub(crate) fn append_custom_property(&mut self, name: CssString, declaration: DeclaredProperty) {
+ if let Some(property) = self.custom_properties.iter_mut().find(|property| property.name == name) {
+ if property.declaration.important && !declaration.important {
+ return;
+ }
+ property.declaration = declaration;
+ } else {
+ self.custom_properties.push(CustomProperty { name, declaration });
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 266c906d923..b9e48d65836 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -3590,7 +3590,7 @@ impl<'a> SyntaxParseBuilder<'a> {
.entry(name.into())
.or_insert_with(|| CssString::from_utf16(name))
.clone();
- block.set_custom_property(name, property);
+ block.append_custom_property(name, property);
} else {
block.append_in_specified_order(property);
}
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-importance.txt b/Tests/LibWeb/Text/expected/css/custom-property-importance.txt
new file mode 100644
index 00000000000..f9ab9d5b794
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-importance.txt
@@ -0,0 +1,33 @@
+Harness status: OK
+
+Found 28 tests
+
+28 Pass
+Pass inline: !important followed by red
+Pass inline: red followed by !important
+Pass inline: green !important followed by red
+Pass inline: red followed by green !important
+Pass inline: green !important followed by red !important
+Pass inline: red !important followed by !important
+Pass inline: !important followed by green !important
+Pass inline: green followed by red
+Pass stylesheet: !important followed by red
+Pass stylesheet: red followed by !important
+Pass stylesheet: green !important followed by red
+Pass stylesheet: red followed by green !important
+Pass stylesheet: green !important followed by red !important
+Pass stylesheet: red !important followed by !important
+Pass stylesheet: !important followed by green !important
+Pass stylesheet: green followed by red
+Pass inline with ignored rule: !important followed by red
+Pass inline with ignored rule: red followed by !important
+Pass inline with ignored rule: green !important followed by red
+Pass inline with ignored rule: red followed by green !important
+Pass inline with ignored rule: green !important followed by red !important
+Pass inline with ignored rule: red !important followed by !important
+Pass inline with ignored rule: !important followed by green !important
+Pass inline with ignored rule: green followed by red
+Pass inline: setProperty replaces !important with a normal value
+Pass inline: setProperty replaces green !important with a normal value
+Pass stylesheet: setProperty replaces !important with a normal value
+Pass stylesheet: setProperty replaces green !important with a normal value
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/variable-definition.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/variable-definition.txt
index 1f5e6e06a47..c5e81811e93 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/variable-definition.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/variable-definition.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 73 tests
-53 Pass
-20 Fail
+59 Pass
+14 Fail
Pass no variable
Pass variable
Pass single char variable
@@ -25,8 +25,8 @@ Pass leading white space (double space) 2
Pass middle white space (double space) 2
Pass trailing white space (double space) 2
Pass !important
-Fail !important 2
-Fail !important (with space)
+Pass !important 2
+Pass !important (with space)
Pass no variable (Computed Style)
Pass variable (Computed Style)
Pass single char variable (Computed Style)
@@ -48,8 +48,8 @@ Pass leading white space (double space) 2 (Computed Style)
Pass middle white space (double space) 2 (Computed Style)
Pass trailing white space (double space) 2 (Computed Style)
Pass !important (Computed Style)
-Fail !important 2 (Computed Style)
-Fail !important (with space) (Computed Style)
+Pass !important 2 (Computed Style)
+Pass !important (with space) (Computed Style)
Pass no variable (Cascading)
Pass variable (Cascading)
Pass single char variable (Cascading)
@@ -71,8 +71,8 @@ Pass leading white space (double space) 2 (Cascading)
Pass middle white space (double space) 2 (Cascading)
Pass trailing white space (double space) 2 (Cascading)
Pass !important (Cascading)
-Fail !important 2 (Cascading)
-Fail !important (with space) (Cascading)
+Pass !important 2 (Cascading)
+Pass !important (with space) (Cascading)
Pass basic CSSOM.setProperty
Fail CSSOM.setProperty with space 1
Fail CSSOM.setProperty with space 2
diff --git a/Tests/LibWeb/Text/input/css/custom-property-importance.html b/Tests/LibWeb/Text/input/css/custom-property-importance.html
new file mode 100644
index 00000000000..c7a56c52890
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-importance.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Custom property importance within declaration blocks</title>
+<link rel="help" href="https://drafts.csswg.org/css-cascade-5/#cascade-sort">
+<link rel="help" href="https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-setproperty">
+<script src="../wpt-import/resources/testharness.js"></script>
+<script src="../wpt-import/resources/testharnessreport.js"></script>
+<body>
+<script>
+ function createDeclaration(t, context, declarations) {
+ const target = document.createElement("div");
+ target.id = "target";
+ document.body.append(target);
+ t.add_cleanup(() => target.remove());
+ if (context === "stylesheet") {
+ const style = document.createElement("style");
+ style.textContent = `#target { ${declarations.join(";")}; }`;
+ document.head.append(style);
+ t.add_cleanup(() => style.remove());
+ return [target, style.sheet.cssRules[0].style];
+ }
+ const separator = context === "inline with ignored rule" ? "; @unknown {} " : ";";
+ target.style.cssText = declarations.join(separator) + ";";
+ return [target, target.style];
+ }
+
+ for (const context of ["inline", "stylesheet", "inline with ignored rule"]) {
+ for (const [first, second, value, priority] of [
+ ["!important", "red", "", "important"],
+ ["red", "!important", "", "important"],
+ ["green !important", "red", "green", "important"],
+ ["red", "green !important", "green", "important"],
+ ["green !important", "red !important", "red", "important"],
+ ["red !important", "!important", "", "important"],
+ ["!important", "green !important", "green", "important"],
+ ["green", "red", "red", ""],
+ ]) {
+ test(t => {
+ const [target, declaration] = createDeclaration(t, context, [`--value: ${first}`, `--value: ${second}`]);
+ assert_equals(declaration.length, 1);
+ assert_equals(declaration.getPropertyValue("--value").trim(), value);
+ assert_equals(declaration.getPropertyPriority("--value"), priority);
+ assert_equals(getComputedStyle(target).getPropertyValue("--value").trim(), value);
+ }, `${context}: ${first} followed by ${second}`);
+ }
+ }
+
+ for (const context of ["inline", "stylesheet"]) {
+ for (const initial of ["!important", "green !important"]) {
+ test(t => {
+ const [target, declaration] = createDeclaration(t, context, [`--value: ${initial}`]);
+ declaration.setProperty("--value", "red");
+ assert_equals(declaration.getPropertyValue("--value"), "red");
+ assert_equals(declaration.getPropertyPriority("--value"), "");
+ assert_equals(getComputedStyle(target).getPropertyValue("--value"), "red");
+ }, `${context}: setProperty replaces ${initial} with a normal value`);
+ }
+ }
+</script>