fix: resolve inline style= declarations in getComputedStyle

This commit is contained in:
Matt Van Horn
2026-06-27 23:13:36 -07:00
parent 01ad869dff
commit 1c044b089b
2 changed files with 35 additions and 0 deletions

View File

@@ -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>

View File

@@ -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 });