Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: remove duplicate labels

Karl Seguin0fa7c0ea0a4d
diff --git a/src/browser/tests/element/html/label.html b/src/browser/tests/element/html/label.html
index b2ebc2cb6..1ce775d06 100644
--- a/src/browser/tests/element/html/label.html
+++ b/src/browser/tests/element/html/label.html
@@ -72,3 +72,82 @@
testing.expectEqual(null, lDetached.control);
}
</script>
+
+<div id="labels-fixture">
+ <label id="lb-before" for="lb-both-input">Before</label>
+ <label id="lb-both" for="lb-both-input">Both <input id="lb-both-input"></label>
+ <label id="lb-other" for="lb-outside">Other <input id="lb-wrapped-elsewhere"></label>
+ <input id="lb-outside">
+ <label id="lb-two">Two <input id="lb-first"><input id="lb-second"></label>
+ <label id="lb-after" for="lb-both-input">After</label>
+</div>
+
+<script id="labels">
+ {
+ function ids(el) {
+ return Array.from(el.labels, (l) => l.id);
+ }
+
+ // #4030: wrapping + matching for = one entry, all in tree order
+ testing.expectEqual(['lb-before', 'lb-both', 'lb-after'], ids(document.getElementById('lb-both-input')));
+
+ // a wrapping label whose for points elsewhere doesn't label the wrapped input
+ testing.expectEqual([], ids(document.getElementById('lb-wrapped-elsewhere')));
+ testing.expectEqual(['lb-other'], ids(document.getElementById('lb-outside')));
+
+ // a label without for labels only its first labelable descendant
+ testing.expectEqual(['lb-two'], ids(document.getElementById('lb-first')));
+ testing.expectEqual([], ids(document.getElementById('lb-second')));
+
+ // detached tree (as Chrome): no labels at all; control resolves only
+ // through wrapping, not for
+ const div = document.createElement('div');
+ div.innerHTML = '<label id="d-for" for="d-input">A</label><input id="d-input"><label id="d-wrap"><input id="d-wrapped"></label><label id="d-both" for="d-inner"><input id="d-inner"></label>';
+ testing.expectEqual([], ids(div.querySelector('#d-input')));
+ testing.expectEqual([], ids(div.querySelector('#d-wrapped')));
+ testing.expectEqual([], ids(div.querySelector('#d-inner')));
+ testing.expectEqual(null, div.querySelector('#d-for').control);
+ testing.expectEqual('d-wrapped', div.querySelector('#d-wrap').control.id);
+ testing.expectEqual(null, div.querySelector('#d-both').control);
+
+ // once attached, all resolve
+ document.body.appendChild(div);
+ testing.expectEqual(['d-for'], ids(div.querySelector('#d-input')));
+ testing.expectEqual(['d-wrap'], ids(div.querySelector('#d-wrapped')));
+ testing.expectEqual(['d-both'], ids(div.querySelector('#d-inner')));
+ div.remove();
+
+ // shadow trees count as attached, even under a detached host
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const sr = host.attachShadow({ mode: 'open' });
+ sr.innerHTML = '<label id="s-for" for="s-in">A</label><input id="s-in"><label id="s-wrap"><input id="s-w"></label>';
+ testing.expectEqual(['s-for'], ids(sr.querySelector('#s-in')));
+ testing.expectEqual(['s-wrap'], ids(sr.querySelector('#s-w')));
+ host.remove();
+ testing.expectEqual(['s-for'], ids(sr.querySelector('#s-in')));
+ testing.expectEqual('s-in', sr.querySelector('#s-for').control.id);
+ testing.expectEqual(['s-wrap'], ids(sr.querySelector('#s-w')));
+
+ // nested labels without for: both label the input, outermost first
+ const nested = document.createElement('div');
+ nested.innerHTML = '<label id="n-outer">A <label id="n-inner">B <input></label></label>';
+ document.body.appendChild(nested);
+ testing.expectEqual(['n-outer', 'n-inner'], ids(nested.querySelector('input')));
+ nested.remove();
+
+ // a duplicate id that isn't first in the tree gets no for labels, only its wrapper
+ const dup = document.createElement('div');
+ dup.innerHTML = '<input id="dup"><label id="dup-for" for="dup">A</label><label id="dup-wrap"><input id="dup"></label>';
+ document.body.appendChild(dup);
+ const dupInputs = dup.querySelectorAll('input');
+ testing.expectEqual(['dup-for'], ids(dupInputs[0]));
+ testing.expectEqual(['dup-wrap'], ids(dupInputs[1]));
+ dup.remove();
+
+ // a document label with a matching for doesn't label a detached control
+ const detached = document.createElement('input');
+ detached.id = 'lb-both-input';
+ testing.expectEqual([], ids(detached));
+ }
+</script>
diff --git a/src/browser/webapi/element/html/Label.zig b/src/browser/webapi/element/html/Label.zig
index 37bfadd57..f8c318a7c 100644
--- a/src/browser/webapi/element/html/Label.zig
+++ b/src/browser/webapi/element/html/Label.zig
@@ -44,7 +44,13 @@ pub fn asNode(self: *Label) *Node {
pub fn getControl(self: *Label, frame: *Frame) ?*Element {
if (self.asElement().getAttributeInterned("for")) |id| {
- const el = frame.getElementByIdFromNode(self.asElement().asNode(), id) orelse return null;
+ // Like Chrome: `for` only resolves in a document or shadow tree, so a
+ // detached label has no control by id.
+ const node = self.asNode();
+ if (!node.isConnected() and !node.isInShadowTree()) {
+ return null;
+ }
+ const el = frame.getElementByIdFromNode(node, id) orelse return null;
if (!isLabelable(el)) {
return null;
}
@@ -104,33 +110,61 @@ pub const LabelByForIndex = struct {
}
};
-/// Collects the `<label>` elements associated with a labellable form control.
-/// Matches HTMLInputElement.labels (and the equivalent on button/select/etc).
-/// Includes every `<label for="id">` reference plus the nearest ancestor
-/// `<label>` wrapping the control.
+/// The `<label>` elements whose labeled control is `control`, in tree order.
pub fn getControlLabels(control: *Element, frame: *Frame) !js.Array {
const local = frame.js.local orelse return error.NotHandled;
var arr = local.newArray(0);
var idx: u32 = 0;
- if (control.getId()) |id_value| {
- if (id_value.len > 0) {
- const doc = control.asNode().ownerDocument(frame);
- const search_root: *Node = if (doc) |d| d.asNode() else control.asNode();
- var it = TreeWalker.Full.Elements.init(search_root, .{});
- while (it.next()) |el| {
- if (el.getTag() != .label) continue;
- const for_attr = el.getAttributeInterned("for") orelse continue;
- if (!std.mem.eql(u8, for_attr, id_value)) continue;
- _ = try arr.set(idx, el, .{});
- idx += 1;
+ // Like Chrome: a control outside a document or shadow tree has no labels,
+ // not even a wrapping one.
+ const node = control.asNode();
+ if (!node.isConnected() and !node.isInShadowTree()) {
+ return arr;
+ }
+
+ const root = node.getRootNode(.{});
+
+ // Only an id can pull in a `for` label from elsewhere in the tree, and only
+ // if this control is the first element with it. Then walk the whole tree.
+ const by_id = if (control.getId()) |id|
+ id.len > 0 and frame.getElementByIdFromNode(root, id) == control
+ else
+ false;
+
+ if (by_id) {
+ var it = TreeWalker.Full.Elements.init(root, .{});
+ while (it.next()) |el| {
+ const label = el.is(Label) orelse continue;
+ if (label.getControl(frame) != control) {
+ continue;
}
+ _ = try arr.set(idx, el, .{});
+ idx += 1;
}
+ return arr;
}
- if (findWrappingLabel(control)) |wrap_label| {
- _ = try arr.set(idx, wrap_label, .{});
- idx += 1;
+ // Otherwise only ancestor labels can apply. Count first so nested labels
+ // land outermost-first, i.e. in tree order.
+ var count: u32 = 0;
+ var current: ?*Node = node._parent;
+ while (current) |n| : (current = n._parent) {
+ const label = n.is(Label) orelse continue;
+ if (label.getControl(frame) == control) {
+ count += 1;
+ }
+ }
+
+ idx = count;
+ current = node._parent;
+ while (current) |n| : (current = n._parent) {
+ const label = n.is(Label) orelse continue;
+ if (label.getControl(frame) != control) {
+ continue;
+ }
+ idx -= 1;
+ _ = try arr.set(idx, label.asElement(), .{});
}
return arr;