Skip to content

Lightpanda: the headless browser designed for AI and automation

css: computed padding, margin and border widths default to 0px

Scott Taylord50da5ca81d1
diff --git a/src/browser/tests/element/styles.html b/src/browser/tests/element/styles.html
index 1c30d525d..7463bb736 100644
--- a/src/browser/tests/element/styles.html
+++ b/src/browser/tests/element/styles.html
@@ -257,6 +257,36 @@
}
</script>
+<script id="computedStyleBoxModelDefaults">
+{
+ // Undeclared box-model lengths compute to their initial value, never '':
+ // measuring code that folds parseInt(...) into layout math would get NaN.
+ const div = document.createElement('div');
+ document.body.appendChild(div);
+ const cs = window.getComputedStyle(div);
+ for (const side of ['top', 'right', 'bottom', 'left']) {
+ testing.expectEqual('0px', cs.getPropertyValue(`padding-${side}`));
+ testing.expectEqual('0px', cs.getPropertyValue(`margin-${side}`));
+ testing.expectEqual('0px', cs.getPropertyValue(`border-${side}-width`));
+ }
+ testing.expectEqual('0px', cs.paddingTop);
+ testing.expectEqual('0px', cs.marginBottom);
+ testing.expectEqual('0px', cs.borderLeftWidth);
+ testing.expectEqual('0px', cs.padding);
+ testing.expectEqual('0px', cs.margin);
+ testing.expectEqual('0px', cs.borderWidth);
+ testing.expectEqual(0, parseInt(cs.paddingTop, 10) + parseInt(cs.paddingBottom, 10));
+ testing.expectEqual('', div.style.paddingTop);
+
+ const styled = document.createElement('div');
+ styled.style.cssText = 'padding-top: 7px; margin-left: 3px';
+ document.body.appendChild(styled);
+ testing.expectEqual('7px', getComputedStyle(styled).paddingTop);
+ testing.expectEqual('3px', getComputedStyle(styled).marginLeft);
+ testing.expectEqual('0px', getComputedStyle(styled).paddingBottom);
+}
+</script>
+
<script id="computedStyleInline">
{
const div = document.createElement('div');
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index 4edfbb7a5..f2c65ae77 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -949,7 +949,30 @@ fn isLengthProperty(name: []const u8) bool {
return length_properties.has(name);
}
+// Box-model lengths whose initial computed value is zero. Border widths compute
+// to zero while border-style is its initial `none`.
+const zero_length_defaults = std.StaticStringMap(void).initComptime(.{
+ .{ "margin", {} },
+ .{ "margin-top", {} },
+ .{ "margin-right", {} },
+ .{ "margin-bottom", {} },
+ .{ "margin-left", {} },
+ .{ "padding", {} },
+ .{ "padding-top", {} },
+ .{ "padding-right", {} },
+ .{ "padding-bottom", {} },
+ .{ "padding-left", {} },
+ .{ "border-width", {} },
+ .{ "border-top-width", {} },
+ .{ "border-right-width", {} },
+ .{ "border-bottom-width", {} },
+ .{ "border-left-width", {} },
+});
+
fn getDefaultPropertyValue(self: *const CSSStyleDeclaration, name: String) []const u8 {
+ if (zero_length_defaults.has(name.str())) {
+ return "0px";
+ }
switch (name.len) {
5 => {
if (name.eql(comptime .wrap("color"))) {