Skip to content

Truly independent web browser

LibWeb: Accept empty important custom-property values

sideshowbarker2ed16cb44ace
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 53bd77adaad..266c906d923 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -1831,7 +1831,7 @@ impl Parser {
}
if let Some(important_index) = important_index {
let mut bang_index = None;
- for index in (1..important_index).rev() {
+ for index in (0..important_index).rev() {
if value[index].is_delim(b'!') {
bang_index = Some(index);
break;
@@ -4969,6 +4969,22 @@ mod tests {
assert!(declarations[2].important);
}
+ #[test]
+ fn consumes_important_without_value_tokens() {
+ for source in [
+ "--x:!important",
+ "--x: !important",
+ "--x:\t! \n IMPORTANT \t",
+ "--x:/**/!/**/important/**/",
+ "--x:!\\69mportant",
+ ] {
+ let declarations = declarations(source);
+ assert_eq!(declarations.len(), 1, "{source:?}");
+ assert!(declarations[0].important, "{source:?}");
+ assert!(declarations[0].value.is_empty(), "{source:?}");
+ }
+ }
+
#[test]
fn preserves_declaration_and_rule_positions() {
let rules = parse_stylesheet(b"\n\na {\n color: red;\n}");
diff --git a/Tests/LibWeb/Text/expected/css/empty-important-custom-properties.txt b/Tests/LibWeb/Text/expected/css/empty-important-custom-properties.txt
new file mode 100644
index 00000000000..a76ed54ddea
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/empty-important-custom-properties.txt
@@ -0,0 +1,24 @@
+Harness status: OK
+
+Found 19 tests
+
+19 Pass
+Pass inline: empty custom property with "!important"
+Pass stylesheet: empty custom property with "!important"
+Pass inline: empty custom property with " !important"
+Pass stylesheet: empty custom property with " !important"
+Pass inline: empty custom property with "\t! \n IMPORTANT \t"
+Pass stylesheet: empty custom property with "\t! \n IMPORTANT \t"
+Pass inline: empty custom property with "/**/!/**/important/**/"
+Pass stylesheet: empty custom property with "/**/!/**/important/**/"
+Pass inline: empty custom property with "!\\69mportant"
+Pass stylesheet: empty custom property with "!\\69mportant"
+Pass Empty important value wins and is inherited: .cascade { --empty: red; } .cascade { --empty: !important; }
+Pass Empty important value wins and is inherited: .cascade { --empty: !important; } .cascade { --empty: red; }
+Pass Empty important value selects the light background
+Pass Empty important value selects the dark background
+Pass No priority for "!"
+Pass No priority for "important"
+Pass No priority for "!important extra"
+Pass No priority for "!!important"
+Pass Empty ordinary properties remain invalid and nonempty important values are preserved
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/css/empty-important-custom-properties.html b/Tests/LibWeb/Text/input/css/empty-important-custom-properties.html
new file mode 100644
index 00000000000..22a2211a71a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/empty-important-custom-properties.html
@@ -0,0 +1,99 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Empty important custom properties</title>
+<link rel="help" href="https://drafts.csswg.org/css-syntax-3/#consume-declaration">
+<link rel="help" href="https://drafts.csswg.org/css-variables-1/#defining-variables">
+<script src="../wpt-import/resources/testharness.js"></script>
+<script src="../wpt-import/resources/testharnessreport.js"></script>
+<body>
+<script>
+ const priorities = [
+ "!important",
+ " !important",
+ "\t! \n IMPORTANT \t",
+ "/**/!/**/important/**/",
+ "!\\69mportant",
+ ];
+
+ for (const priority of priorities) {
+ for (const context of ["inline", "stylesheet"]) {
+ test(t => {
+ let declaration;
+ if (context === "inline") {
+ declaration = document.createElement("div").style;
+ declaration.cssText = `--empty:${priority};`;
+ } else {
+ const style = document.createElement("style");
+ style.textContent = `.target { --empty:${priority}; }`;
+ document.head.append(style);
+ t.add_cleanup(() => style.remove());
+ declaration = style.sheet.cssRules[0].style;
+ }
+ assert_equals(declaration.length, 1);
+ assert_equals(declaration.getPropertyPriority("--empty"), "important");
+ assert_equals(declaration.getPropertyValue("--empty").trim(), "");
+ }, `${context}: empty custom property with ${JSON.stringify(priority)}`);
+ }
+ }
+
+ for (const rules of [
+ ".cascade { --empty: red; } .cascade { --empty: !important; }",
+ ".cascade { --empty: !important; } .cascade { --empty: red; }",
+ ]) {
+ test(t => {
+ const style = document.createElement("style");
+ style.textContent = rules;
+ document.head.append(style);
+ t.add_cleanup(() => style.remove());
+ const parent = document.createElement("div");
+ const child = document.createElement("div");
+ parent.className = "cascade";
+ child.style.cssText = "background-color: var(--empty, red) green;";
+ parent.append(child);
+ document.body.append(parent);
+ t.add_cleanup(() => parent.remove());
+ assert_equals(getComputedStyle(child).backgroundColor, "rgb(0, 128, 0)");
+ }, `Empty important value wins and is inherited: ${rules}`);
+ }
+
+ for (const [theme, expected] of [["light", "rgb(255, 255, 255)"], ["dark", "rgb(41, 41, 41)"]]) {
+ test(t => {
+ const style = document.createElement("style");
+ style.textContent = `
+ .theme { --light: initial; --dark: initial; }
+ .light { --dark: !important; }
+ .dark { --light: !important; }
+ .surface { background-color: var(--light, white) var(--dark, #292929); }
+ `;
+ document.head.append(style);
+ t.add_cleanup(() => style.remove());
+ const parent = document.createElement("div");
+ parent.className = `theme ${theme}`;
+ const child = document.createElement("div");
+ child.className = "surface";
+ parent.append(child);
+ document.body.append(parent);
+ t.add_cleanup(() => parent.remove());
+ assert_equals(getComputedStyle(child).backgroundColor, expected);
+ }, `Empty important value selects the ${theme} background`);
+ }
+
+ for (const invalid of ["!", "important", "!important extra", "!!important"]) {
+ test(() => {
+ const style = document.createElement("div").style;
+ style.cssText = `--empty: ${invalid};`;
+ assert_equals(style.getPropertyPriority("--empty"), "");
+ }, `No priority for ${JSON.stringify(invalid)}`);
+ }
+
+ test(() => {
+ const style = document.createElement("div").style;
+ style.cssText = "color: !important; width: 1px !important; --value: red !important;";
+ assert_equals(style.getPropertyValue("color"), "");
+ assert_equals(style.getPropertyPriority("color"), "");
+ assert_equals(style.getPropertyValue("width"), "1px");
+ assert_equals(style.getPropertyPriority("width"), "important");
+ assert_equals(style.getPropertyValue("--value"), "red");
+ assert_equals(style.getPropertyPriority("--value"), "important");
+ }, "Empty ordinary properties remain invalid and nonempty important values are preserved");
+</script>