Skip to content

Lightpanda: the headless browser designed for AI and automation

css: honor !important for duplicate inline declarations in computed style

Matt Van Horn16b16564d535
diff --git a/src/browser/tests/element/styles.html b/src/browser/tests/element/styles.html
index 42250d168..adc9d395f 100644
--- a/src/browser/tests/element/styles.html
+++ b/src/browser/tests/element/styles.html
@@ -215,5 +215,11 @@
const plainDiv = document.createElement('div');
document.body.appendChild(plainDiv);
testing.expectEqual('block', window.getComputedStyle(plainDiv).getPropertyValue('display'));
+
+ // A normal declaration must not override an earlier !important one.
+ const impDiv = document.createElement('div');
+ impDiv.setAttribute('style', 'color: red !important; color: blue');
+ document.body.appendChild(impDiv);
+ testing.expectEqual('red', window.getComputedStyle(impDiv).getPropertyValue('color'));
}
</script>
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index a106731fd..f58b7fd74 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -243,11 +243,18 @@ fn findInlineValue(element: *const Element, normalized_name: []const u8, frame:
const attr_value = element.getAttributeSafe(comptime .wrap("style")) orelse return null;
var it = CssParser.parseDeclarationsList(attr_value);
var result: ?[]const u8 = null;
+ var result_important = false;
while (it.next()) |declaration| {
- if (std.ascii.eqlIgnoreCase(declaration.name, normalized_name)) {
- // Keep the last matching declaration, matching CSS declaration order.
- result = normalizePropertyValue(frame.call_arena, normalized_name, declaration.value) catch declaration.value;
+ if (!std.ascii.eqlIgnoreCase(declaration.name, normalized_name)) {
+ continue;
+ }
+ // A later declaration wins, except a normal one never overrides an
+ // earlier !important declaration (CSS cascade precedence).
+ if (result_important and !declaration.important) {
+ continue;
}
+ result = normalizePropertyValue(frame.call_arena, normalized_name, declaration.value) catch declaration.value;
+ result_important = declaration.important;
}
return result;
}