Skip to content

Lightpanda: the headless browser designed for AI and automation

css: resolve computed inline styles via StyleManager

Pierre Tachoire0614795fc5b5
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 79936e1d3..d96eba0b0 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -871,6 +871,15 @@ fn getInlineStyleProperty(el: *Element, property_name: String, frame: *Frame) ?*
return style.asCSSStyleDeclaration().findProperty(property_name);
}
+/// Resolved value of an element's inline `style=` declaration for `property_name`,
+/// or null when the element has no such declaration. Reads the element's parsed
+/// inline style (the same source `el.style` exposes), so `getComputedStyle` and
+/// `el.style` agree on inline values instead of resolving them independently.
+pub fn inlineStyleValue(self: *StyleManager, el: *Element, property_name: String) ?[]const u8 {
+ const property = getInlineStyleProperty(el, property_name, self.frame) orelse return null;
+ return property._value.str();
+}
+
const testing = @import("../testing.zig");
test "StyleManager: computeSpecificity: element selector" {
// div -> (0, 0, 1)
diff --git a/src/browser/tests/element/styles.html b/src/browser/tests/element/styles.html
index adc9d395f..b4e81aa2b 100644
--- a/src/browser/tests/element/styles.html
+++ b/src/browser/tests/element/styles.html
@@ -216,10 +216,12 @@
document.body.appendChild(plainDiv);
testing.expectEqual('block', window.getComputedStyle(plainDiv).getPropertyValue('display'));
- // A normal declaration must not override an earlier !important one.
+ // A normal declaration must not override an earlier !important one, and the
+ // computed and inline (el.style) paths must agree on the resolved value.
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'));
+ testing.expectEqual('red', impDiv.style.getPropertyValue('color'));
}
</script>
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index f58b7fd74..5cf1c21b2 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -49,7 +49,7 @@ pub fn init(element: ?*Element, is_computed: bool, frame: *Frame) !*CSSStyleDecl
if (el.getAttributeSafe(comptime .wrap("style"))) |attr_value| {
var it = CssParser.parseDeclarationsList(attr_value);
while (it.next()) |declaration| {
- try self.setPropertyImpl(declaration.name, declaration.value, declaration.important, frame);
+ try self.applyParsedDeclaration(declaration, frame);
}
}
}
@@ -97,7 +97,9 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
// Only return default values for computed styles
if (self._is_computed) {
if (self._element) |element| {
- if (findInlineValue(element, normalized, frame)) |value| return value;
+ // Resolve inline `style=` declarations through the element's
+ // parsed inline style, so computed values match `el.style`.
+ if (frame._style_manager.inlineStyleValue(element, wrapped)) |value| return value;
}
return getDefaultPropertyValue(self, wrapped);
}
@@ -127,6 +129,19 @@ pub fn setProperty(self: *CSSStyleDeclaration, property_name: []const u8, value:
try self.syncStyleAttribute(frame);
}
+// Apply one declaration parsed from a `style=` block. Unlike the imperative
+// setProperty path, within a single declaration block a normal declaration must
+// not override an earlier !important one (CSS cascade precedence).
+fn applyParsedDeclaration(self: *CSSStyleDeclaration, declaration: CssParser.Declaration, frame: *Frame) !void {
+ if (!declaration.important) {
+ const normalized = normalizePropertyName(declaration.name, &frame.buf);
+ if (self.findProperty(.wrap(normalized))) |existing| {
+ if (existing._important) return;
+ }
+ }
+ try self.setPropertyImpl(declaration.name, declaration.value, declaration.important, frame);
+}
+
fn setPropertyImpl(self: *CSSStyleDeclaration, property_name: []const u8, value: []const u8, important: bool, frame: *Frame) !void {
if (value.len == 0) {
_ = try self.removePropertyImpl(property_name, frame);
@@ -210,7 +225,7 @@ pub fn setCssText(self: *CSSStyleDeclaration, text: []const u8, frame: *Frame) !
// Parse and set new properties
var it = CssParser.parseDeclarationsList(text);
while (it.next()) |declaration| {
- try self.setPropertyImpl(declaration.name, declaration.value, declaration.important, frame);
+ try self.applyParsedDeclaration(declaration, frame);
}
try self.syncStyleAttribute(frame);
}
@@ -239,26 +254,6 @@ pub fn findProperty(self: *const CSSStyleDeclaration, name: String) ?*Property {
return null;
}
-fn findInlineValue(element: *const Element, normalized_name: []const u8, frame: *Frame) ?[]const u8 {
- 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)) {
- 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;
-}
-
fn normalizePropertyName(name: []const u8, buf: []u8) []const u8 {
if (name.len > buf.len) {
log.info(.dom, "css.long.name", .{ .name = name });