Lightpanda: the headless browser designed for AI and automation
webapi: add various missing namespace methods
Karl Seguin6db5890f76ca
diff --git a/src/browser/tests/element/attributes.html b/src/browser/tests/element/attributes.htmlindex 77bd68d4b..e19558cf4 100644--- a/src/browser/tests/element/attributes.html+++ b/src/browser/tests/element/attributes.html@@ -429,3 +429,38 @@testing.expectEqual('id', Object.getOwnPropertyDescriptor(attrs, 'id').value.name);}</script>++<script id=namespaced>+ {+ const XLINK = 'http://www.w3.org/1999/xlink';+ const el = document.createElement('div');+ el.setAttribute('xlink:href', '#a');+ el.setAttribute('title', 't');++ testing.expectEqual('#a', el.getAttributeNodeNS(XLINK, 'href').value);+ testing.expectEqual('t', el.getAttributeNodeNS(null, 'title').value);+ testing.expectEqual(null, el.getAttributeNodeNS(null, 'nope'));++ el.removeAttributeNS(XLINK, 'href');+ testing.expectEqual(false, el.hasAttribute('xlink:href'));+ el.removeAttributeNS(null, 'title');+ testing.expectEqual(false, el.hasAttribute('title'));+ // removing a missing attribute is a no-op+ el.removeAttributeNS(null, 'nope');++ const attr = document.createAttribute('data-n');+ attr.value = '1';+ testing.expectEqual(null, el.setAttributeNodeNS(attr));+ testing.expectEqual(el, attr.ownerElement);++ const attrs = el.attributes;+ testing.expectEqual(attr, attrs.getNamedItemNS(null, 'data-n'));+ testing.expectEqual(null, attrs.getNamedItemNS(null, 'nope'));+ testing.expectEqual(attr, attrs.removeNamedItemNS(null, 'data-n'));+ testing.expectEqual(null, attr.ownerElement);+ testing.expectEqual(0, attrs.length);++ testing.expectEqual(null, attrs.setNamedItemNS(attr));+ testing.expectEqual('1', el.getAttribute('data-n'));+ }+</script>diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 50bfd8377..f61f6016b 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -708,16 +708,21 @@ pub fn getAttributeNS(local_name: String,frame: *Frame,) !?String {+ return self.getAttribute(try self.attributeNameNS(namespace_, local_name, frame), frame);+}++// The stored name a namespaced lookup resolves to. We don't really support+// namespaces, but if the namespace has a fixed prefix and the prefixed+// attribute exists, that's the one.+pub fn attributeNameNS(self: *const Element, namespace_: ?[]const u8, local_name: String, frame: *Frame) !String {if (namespace_) |namespace| {- // we don't really support namespaces, but if the namespace has a fixed- // prefix, we can try to fetch the attribute with itif (try prefixedAttributeName(namespace, local_name.str(), frame)) |prefixed| {- if (try self.getAttribute(.wrap(prefixed), frame)) |value| {- return value;+ if (try self.hasAttribute(.wrap(prefixed), frame)) {+ return .wrap(prefixed);}}}- return self.getAttribute(local_name, frame);+ return local_name;}fn prefixedAttributeName(namespace: []const u8, local_name: []const u8, frame: *Frame) !?[]const u8 {@@ -833,6 +838,10 @@ pub fn getAttributeNode(self: *Element, name: String, frame: *Frame) !?*Attributreturn self._attributes.getAttribute(name, self, frame);}+pub fn getAttributeNodeNS(self: *Element, namespace_: ?[]const u8, local_name: String, frame: *Frame) !?*Attribute {+ return self.getAttributeNode(try self.attributeNameNS(namespace_, local_name, frame), frame);+}+pub fn setAttribute(self: *Element, name: String, value: String, frame: *Frame) !void {try Attribute.validateAttributeName(name);_ = try self._attributes.put(name, value, self, frame);@@ -977,6 +986,10 @@ pub fn removeAttribute(self: *Element, name: String, frame: *Frame) !void {return self._attributes.delete(name, self, frame);}+pub fn removeAttributeNS(self: *Element, namespace_: ?[]const u8, local_name: String, frame: *Frame) !void {+ return self.removeAttribute(try self.attributeNameNS(namespace_, local_name, frame), frame);+}+pub fn removeAttributeSafe(self: *Element, name: String, frame: *Frame) void {self._attributes.deleteSafe(name, self, frame);}@@ -2694,8 +2707,12 @@ pub const JsApi = struct {pub const getAttribute = bridge.function(Element.getAttribute, .{});pub const getAttributeNS = bridge.function(Element.getAttributeNS, .{});pub const getAttributeNode = bridge.function(Element.getAttributeNode, .{});+ pub const getAttributeNodeNS = bridge.function(Element.getAttributeNodeNS, .{});pub const setAttributeNode = bridge.function(Element.setAttributeNode, .{ .ce_reactions = true });+ // Attributes don't carry a namespace, so this is setAttributeNode.+ pub const setAttributeNodeNS = bridge.function(Element.setAttributeNode, .{ .ce_reactions = true });pub const removeAttribute = bridge.function(Element.removeAttribute, .{ .ce_reactions = true });+ pub const removeAttributeNS = bridge.function(Element.removeAttributeNS, .{ .ce_reactions = true });pub const toggleAttribute = bridge.function(Element.toggleAttribute, .{ .ce_reactions = true });pub const getAttributeNames = bridge.function(Element.getAttributeNames, .{});pub const removeAttributeNode = bridge.function(Element.removeAttributeNode, .{ .ce_reactions = true });diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zigindex 52721c4fe..ed161009e 100644--- a/src/browser/webapi/element/Attribute.zig+++ b/src/browser/webapi/element/Attribute.zig@@ -614,6 +614,10 @@ pub const NamedNodeMap = struct {return self.list().getAttribute(name, self._element, frame);}+ fn getByNameNS(self: *const NamedNodeMap, namespace: ?[]const u8, local_name: String, frame: *Frame) !?*Attribute {+ return self._element.getAttributeNodeNS(namespace, local_name, frame);+ }+pub fn set(self: *const NamedNodeMap, attribute: *Attribute, frame: *Frame) !?*Attribute {return self._element.setAttributeNode(attribute, frame);}@@ -626,6 +630,10 @@ pub const NamedNodeMap = struct {return attr;}+ fn removeByNameNS(self: *const NamedNodeMap, namespace: ?[]const u8, local_name: String, frame: *Frame) !?*Attribute {+ return self.removeByName(try self._element.attributeNameNS(namespace, local_name, frame), frame);+ }+pub fn iterator(self: *const NamedNodeMap, frame: *Frame) !*Iterator {return .init(.{ .list = self }, frame);}@@ -682,8 +690,12 @@ pub const NamedNodeMap = struct {}.wrap, .{ .null_as_undefined = true });pub const getNamedItem = bridge.function(NamedNodeMap.getByName, .{});+ pub const getNamedItemNS = bridge.function(NamedNodeMap.getByNameNS, .{});pub const setNamedItem = bridge.function(NamedNodeMap.set, .{ .ce_reactions = true });+ // Attributes don't carry a namespace, so this is setNamedItem.+ pub const setNamedItemNS = bridge.function(NamedNodeMap.set, .{ .ce_reactions = true });pub const removeNamedItem = bridge.function(NamedNodeMap.removeByName, .{ .ce_reactions = true });+ pub const removeNamedItemNS = bridge.function(NamedNodeMap.removeByNameNS, .{ .ce_reactions = true });pub const item = bridge.function(_item, .{});fn _item(self: *const NamedNodeMap, index: i32, frame: *Frame) !?*Attribute {// the bridge.indexed handles this, so if we want