Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: resolve inline style= declarations in getComputedStyle

Matt Van Horn1c044b089baf
diff --git a/src/browser/tests/element/styles.html b/src/browser/tests/element/styles.html
index 05dfa34f0..42250d168 100644
--- a/src/browser/tests/element/styles.html
+++ b/src/browser/tests/element/styles.html
@@ -198,3 +198,22 @@
testing.expectEqual('1', divStyle.opacity);
}
</script>
+
+<script id="computedStyleInline">
+{
+ const div = document.createElement('div');
+ div.style.cssText = 'text-transform: uppercase; font-weight: 700; color: rgb(1, 2, 3)';
+ document.body.appendChild(div);
+
+ const cs = window.getComputedStyle(div);
+ testing.expectEqual('uppercase', cs.getPropertyValue('text-transform'));
+ testing.expectEqual('700', cs.getPropertyValue('font-weight'));
+ testing.expectEqual('rgb(1, 2, 3)', cs.getPropertyValue('color'));
+ testing.expectEqual('rgb(1, 2, 3)', div.style.getPropertyValue('color'));
+ testing.expectEqual('', cs.getPropertyValue('letter-spacing'));
+
+ const plainDiv = document.createElement('div');
+ document.body.appendChild(plainDiv);
+ testing.expectEqual('block', window.getComputedStyle(plainDiv).getPropertyValue('display'));
+}
+</script>
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index f1c915506..a106731fd 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -96,6 +96,9 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
const prop = self.findProperty(wrapped) orelse {
// Only return default values for computed styles
if (self._is_computed) {
+ if (self._element) |element| {
+ if (findInlineValue(element, normalized, frame)) |value| return value;
+ }
return getDefaultPropertyValue(self, wrapped);
}
return "";
@@ -236,6 +239,19 @@ 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;
+ 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;
+ }
+ }
+ return result;
+}
+
fn normalizePropertyName(name: []const u8, buf: []u8) []const u8 {
if (name.len > buf.len) {
log.info(.dom, "css.long.name", .{ .name = name });