Skip to content

Lightpanda: the headless browser designed for AI and automation

use Type checkers to implicitly check html namespace

Karl Seguin33fc4fa5008d
diff --git a/src/browser/tests/element/html/table.html b/src/browser/tests/element/html/table.html
index feb8d2349..10714274d 100644
--- a/src/browser/tests/element/html/table.html
+++ b/src/browser/tests/element/html/table.html
@@ -101,22 +101,44 @@
add(add(table, 'caption'), 'tr', 'nested1');
add(add(add(tbody.firstChild, 'td'), 'table'), 'tr', 'nested2');
- const ids = () => Array.from(table.rows ?? [], (row) => row.id).join(',');
+ const ids = () => Array.from(table.rows, (row) => row.id).join(',');
// thead rows first, then table-level and tbody rows, then tfoot rows.
testing.expectEqual('head1,top1,body1,foot1', ids());
- testing.expectEqual('HTMLCollection', table.rows?.constructor.name);
- testing.expectEqual(4, table.rows?.length);
- testing.expectEqual('body1', table.rows?.[2].id);
- testing.expectEqual('foot1', table.rows?.namedItem('foot1')?.id);
- testing.expectEqual(null, table.rows?.namedItem('nested1'));
+ testing.expectEqual('HTMLCollection', table.rows.constructor.name);
+ testing.expectEqual(4, table.rows.length);
+ testing.expectEqual('body1', table.rows[2].id);
+ testing.expectEqual('foot1', table.rows.namedItem('foot1').id);
+ testing.expectEqual(null, table.rows.namedItem('nested1'));
// The collection is live.
const rows = table.rows;
add(tbody, 'tr', 'body2');
- testing.expectEqual(5, rows?.length);
+ testing.expectEqual(5, rows.length);
testing.expectEqual('head1,top1,body1,body2,foot1', ids());
- testing.expectEqual(0, document.createElement('table').rows?.length);
+ testing.expectEqual(0, document.createElement('table').rows.length);
+ }
+</script>
+
+<script id="svg_rows">
+ {
+ // Only HTML tr/tbody count: an SVG element named "tr" or "tbody" isn't one.
+ const svg = 'http://www.w3.org/2000/svg';
+ const table = document.createElement('table');
+ const tbody = table.appendChild(document.createElement('tbody'));
+ tbody.appendChild(document.createElement('tr')).id = 'real';
+ tbody.appendChild(document.createElementNS(svg, 'tr')).id = 'svg1';
+ table.appendChild(document.createElementNS(svg, 'tr')).id = 'svg2';
+ const svgBody = table.appendChild(document.createElementNS(svg, 'tbody'));
+ svgBody.appendChild(document.createElement('tr')).id = 'inSvg';
+
+ testing.expectEqual(1, table.rows.length);
+ testing.expectEqual('real', table.rows[0].id);
+ testing.expectEqual(null, table.rows.namedItem('svg1'));
+ testing.expectEqual(null, table.rows.namedItem('inSvg'));
+ testing.expectEqual(1, table.tBodies.length);
+ testing.expectEqual(1, tbody.rows.length);
+ testing.expectEqual('real', tbody.rows[0].id);
}
</script>
diff --git a/src/browser/webapi/collections/node_live.zig b/src/browser/webapi/collections/node_live.zig
index c8a3d4642..76b554381 100644
--- a/src/browser/webapi/collections/node_live.zig
+++ b/src/browser/webapi/collections/node_live.zig
@@ -322,6 +322,8 @@ pub fn NodeLive(comptime mode: Mode) type {
.child_elements => return node._type == .element,
.child_tag => {
const el = node.is(Element) orelse return false;
+ // getTag() is namespace-blind: an SVG "tr" reports .tr
+ if (el._namespace != .html) return false;
return el.getTag() == self._filter;
},
.cells => {
diff --git a/src/browser/webapi/element/html/Table.zig b/src/browser/webapi/element/html/Table.zig
index f2bfdac3e..fd085238b 100644
--- a/src/browser/webapi/element/html/Table.zig
+++ b/src/browser/webapi/element/html/Table.zig
@@ -88,6 +88,9 @@ pub const RowWalker = struct {
// the next candidate row inside the current section
_row: ?*Node = null,
+ const Row = Element.Html.TableRow;
+ const Section = Element.Html.TableSection;
+
const Phase = enum { head, body, foot };
const Opts = struct {};
@@ -102,7 +105,7 @@ pub const RowWalker = struct {
while (true) {
while (self._row) |node| {
self._row = node.nextSibling();
- if (tagOf(node) == .tr) {
+ if (node.is(Row) != null) {
return node;
}
}
@@ -119,15 +122,15 @@ pub const RowWalker = struct {
self._child = child.nextSibling();
switch (self._phase) {
- .head => if (tagOf(child) == .thead) {
+ .head => if (sectionTag(child) == .thead) {
self._row = child.firstChild();
},
- .body => switch (tagOf(child) orelse continue) {
- .tr => return child,
- .tbody => self._row = child.firstChild(),
- else => {},
+ .body => if (child.is(Row) != null) {
+ return child;
+ } else if (sectionTag(child) == .tbody) {
+ self._row = child.firstChild();
},
- .foot => if (tagOf(child) == .tfoot) {
+ .foot => if (sectionTag(child) == .tfoot) {
self._row = child.firstChild();
},
}
@@ -143,22 +146,19 @@ pub const RowWalker = struct {
}
pub fn contains(self: *const RowWalker, target: *Node) bool {
- if (tagOf(target) != .tr) {
+ if (target.is(Row) == null) {
return false;
}
const parent = target._parent orelse return false;
if (parent == self._root) {
return true;
}
- return switch (tagOf(parent) orelse return false) {
- .thead, .tbody, .tfoot => parent._parent == self._root,
- else => false,
- };
+ return parent.is(Section) != null and parent._parent == self._root;
}
- fn tagOf(node: *Node) ?Element.Tag {
- const el = node.is(Element) orelse return null;
- return el.getTag();
+ fn sectionTag(node: *Node) ?Element.Tag {
+ const section = node.is(Section) orelse return null;
+ return section._tag;
}
};