Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: compare element namespace URIs in isEqualNode

Ayub34b0b5ec2f4f
diff --git a/src/browser/tests/node/is_equal_node.html b/src/browser/tests/node/is_equal_node.html
index cfbd2001e..63398c22c 100644
--- a/src/browser/tests/node/is_equal_node.html
+++ b/src/browser/tests/node/is_equal_node.html
@@ -38,6 +38,20 @@
e2.innerHTML = "<h1>from the midnight sun where the hot springs flow</h1>";
testing.expectEqual(false, e1.isEqualNode(e2));
}
+
+ {
+ const e1 = document.createElementNS("urn:first", "item");
+ const e2 = document.createElementNS("urn:second", "item");
+ testing.expectEqual(false, e1.isEqualNode(e2));
+ }
+
+ {
+ const e1 = document.createElement("div");
+ const e2 = document.createElement("div");
+ e1.appendChild(document.createElementNS("urn:first", "item"));
+ e2.appendChild(document.createElementNS("urn:second", "item"));
+ testing.expectEqual(false, e1.isEqualNode(e2));
+ }
</script>
<script id=isEqualNode_null>
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 0276102d3..ea34fbd93 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -242,13 +242,16 @@ pub fn asConstNode(self: *const Element) *const Node {
return Factory.protoOf(@constCast(self));
}
-/// TODO: localName and prefix comparison.
-pub fn isEqualNode(self: *Element, other: *Element) bool {
- const self_tag = self.getTagNameDump();
- const other_tag = other.getTagNameDump();
- // Compare namespaces and tags.
- const dirty = self._namespace != other._namespace or !std.mem.eql(u8, self_tag, other_tag);
- if (dirty) {
+pub fn isEqualNode(self: *Element, other: *Element, frame: *Frame) bool {
+ if (!optionalStringEql(self.getNamespaceUri(frame), other.getNamespaceUri(frame))) {
+ return false;
+ }
+
+ if (!optionalStringEql(self._prefix(), other._prefix())) {
+ return false;
+ }
+
+ if (!std.mem.eql(u8, self.getLocalName(), other.getLocalName())) {
return false;
}
@@ -256,7 +259,13 @@ pub fn isEqualNode(self: *Element, other: *Element) bool {
return false;
}
- return self.asNode().isEqualChildren(other.asNode());
+ return self.asNode().isEqualChildren(other.asNode(), frame);
+}
+
+fn optionalStringEql(a: ?[]const u8, b: ?[]const u8) bool {
+ const av = a orelse return b == null;
+ const bv = b orelse return false;
+ return std.mem.eql(u8, av, bv);
}
pub fn getTagNameLower(self: *const Element) []const u8 {
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 216293467..2380a5920 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -567,7 +567,7 @@ pub fn isDefaultNamespace(self: *Node, namespace_arg: ?[]const u8, frame: *Frame
return false;
}
-pub fn isEqualNode(self: *Node, other: *Node) bool {
+pub fn isEqualNode(self: *Node, other: *Node, frame: *Frame) bool {
if (self == other) {
return true;
}
@@ -577,17 +577,16 @@ pub fn isEqualNode(self: *Node, other: *Node) bool {
return false;
}
- // TODO: Compare `localName` and prefix.
return switch (self._type) {
- .element => self.as(Element).isEqualNode(other.as(Element)),
+ .element => self.as(Element).isEqualNode(other.as(Element), frame),
.attribute => self.as(Element.Attribute).isEqualNode(other.as(Element.Attribute)),
.cdata => self.as(CData).isEqualNode(other.as(CData)),
.document_type => self.as(DocumentType).isEqualNode(other.as(DocumentType)),
- .document_fragment, .document => self.isEqualChildren(other),
+ .document_fragment, .document => self.isEqualChildren(other, frame),
};
}
-pub fn isEqualChildren(a: *Node, b: *Node) bool {
+pub fn isEqualChildren(a: *Node, b: *Node, frame: *Frame) bool {
var a_count: usize = 0;
var a_iter = a.childrenIterator();
@@ -597,7 +596,7 @@ pub fn isEqualChildren(a: *Node, b: *Node) bool {
while (a_iter.next()) |a_node| : (a_count += 1) {
const b_node = b_iter.next() orelse return false;
b_count += 1;
- if (a_node.isEqualNode(b_node)) {
+ if (a_node.isEqualNode(b_node, frame)) {
continue;
}
@@ -1952,8 +1951,8 @@ pub const JsApi = struct {
return self.getRootNode(opts orelse .{});
}
pub const isEqualNode = bridge.function(_isEqualNode, .{});
- fn _isEqualNode(self: *Node, other: js.Nullable(*Node)) bool {
- return self.isEqualNode(other.value orelse return false);
+ fn _isEqualNode(self: *Node, other: js.Nullable(*Node), frame: *Frame) bool {
+ return self.isEqualNode(other.value orelse return false, frame);
}
pub const lookupNamespaceURI = bridge.function(_lookupNamespaceURI, .{});
fn _lookupNamespaceURI(self: *Node, prefix: js.Nullable([]const u8), frame: *Frame) ?[]const u8 {