Skip to content

Lightpanda: the headless browser designed for AI and automation

css: treat null as an empty string in style property setters

Jay Hemnanibe2b84531db8
diff --git a/src/browser/tests/element/css_style_properties.html b/src/browser/tests/element/css_style_properties.html
index 1db799358..e2ce4e3b8 100644
--- a/src/browser/tests/element/css_style_properties.html
+++ b/src/browser/tests/element/css_style_properties.html
@@ -231,3 +231,19 @@
testing.expectEqual('', div.style.getPropertyValue(name.toUpperCase()));
}
</script>
+
+<script id="nullRemovesDeclaration">
+{
+ // [LegacyNullToEmptyString]: null clears the declaration instead of setting "null"
+ const div = $('#test-div');
+ div.style.cssText = 'display: none; color: red';
+
+ div.style.display = null;
+ testing.expectEqual('', div.style.display);
+ testing.expectEqual('color: red;', div.style.cssText);
+
+ div.style.setProperty('color', null);
+ testing.expectEqual('', div.style.color);
+ testing.expectEqual('', div.style.cssText);
+}
+</script>
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index a3154c513..3eec441c8 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -1224,7 +1224,11 @@ pub const JsApi = struct {
pub const getPropertyValue = bridge.function(CSSStyleDeclaration.getPropertyValue, .{});
pub const getPropertyPriority = bridge.function(CSSStyleDeclaration.getPropertyPriority, .{});
- pub const setProperty = bridge.function(CSSStyleDeclaration.setProperty, .{ .ce_reactions = true });
+ pub const setProperty = bridge.function(_setProperty, .{ .ce_reactions = true });
+ fn _setProperty(self: *CSSStyleDeclaration, property_name: []const u8, value: js.Value, priority_: ?[]const u8, frame: *Frame) !void {
+ // `[LegacyNullToEmptyString] CSSOMString value`: a JS null becomes "", not "null".
+ return self.setProperty(property_name, if (value.isNull()) "" else try value.toZig([]const u8), priority_, frame);
+ }
pub const removeProperty = bridge.function(CSSStyleDeclaration.removeProperty, .{ .ce_reactions = true });
pub const cssFloat = bridge.accessor(CSSStyleDeclaration.getFloat, CSSStyleDeclaration.setFloat, .{ .ce_reactions = true });
};
diff --git a/src/browser/webapi/css/CSSStyleProperties.zig b/src/browser/webapi/css/CSSStyleProperties.zig
index e76cfb974..453170eaf 100644
--- a/src/browser/webapi/css/CSSStyleProperties.zig
+++ b/src/browser/webapi/css/CSSStyleProperties.zig
@@ -47,12 +47,14 @@ pub fn asCSSStyleDeclaration(self: *CSSStyleProperties) *CSSStyleDeclaration {
return self._proto;
}
-fn setNamed(self: *CSSStyleProperties, name: []const u8, value: []const u8, frame: *Frame) !void {
+fn setNamed(self: *CSSStyleProperties, name: []const u8, value: js.Value, frame: *Frame) !void {
if (method_names.has(name)) {
return error.NotHandled;
}
const dash_case = try camelCaseToDashCase(frame.local_arena, name);
- try self._proto.setProperty(dash_case, value, null, frame);
+ // `[LegacyNullToEmptyString] CSSOMString`: a JS null becomes "", which
+ // removes the declaration, instead of the string "null".
+ try self._proto.setProperty(dash_case, if (value.isNull()) "" else try value.toZig([]const u8), null, frame);
}
fn getNamed(self: *CSSStyleProperties, name: []const u8, frame: *Frame) ![]const u8 {