Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Implement Element.offsetParent getter

Karl Seguine667e279fb53
diff --git a/src/browser/tests/element/position.html b/src/browser/tests/element/position.html
index a09734b24..e8137bbad 100644
--- a/src/browser/tests/element/position.html
+++ b/src/browser/tests/element/position.html
@@ -161,6 +161,72 @@
}
</script>
+<script id="offsetParent">
+{
+ const test1 = $('#test1');
+
+ // Default offset ancestor is the body.
+ testing.expectEqual(document.body, test1.offsetParent);
+
+ // The root and body elements have no offsetParent.
+ testing.expectEqual(null, document.documentElement.offsetParent);
+ testing.expectEqual(null, document.body.offsetParent);
+
+ // A positioned ancestor wins over the body.
+ const wrap = document.createElement('div');
+ wrap.style.position = 'relative';
+ const inner = document.createElement('span');
+ wrap.appendChild(inner);
+ document.body.appendChild(wrap);
+ testing.expectEqual(wrap, inner.offsetParent);
+
+ // A position:fixed element has no offsetParent.
+ inner.style.position = 'fixed';
+ testing.expectEqual(null, inner.offsetParent);
+ inner.style.position = '';
+
+ // Static elements offset from table machinery: content → cell, cell → table.
+ const table = document.createElement('table');
+ const tr = document.createElement('tr');
+ const td = document.createElement('td');
+ const cellContent = document.createElement('span');
+ td.appendChild(cellContent);
+ tr.appendChild(td);
+ table.appendChild(tr);
+ document.body.appendChild(table);
+ testing.expectEqual(td, cellContent.offsetParent);
+ testing.expectEqual(table, td.offsetParent);
+
+ // ...but a positioned element ignores the td/th/table rule.
+ cellContent.style.position = 'absolute';
+ testing.expectEqual(document.body, cellContent.offsetParent);
+
+ // display:none (own or inherited) → null.
+ const hidden = document.createElement('div');
+ hidden.style.display = 'none';
+ const hiddenChild = document.createElement('span');
+ hidden.appendChild(hiddenChild);
+ document.body.appendChild(hidden);
+ testing.expectEqual(null, hidden.offsetParent);
+ testing.expectEqual(null, hiddenChild.offsetParent);
+
+ // Detached elements → null.
+ testing.expectEqual(null, document.createElement('div').offsetParent);
+
+ // Shadow-tree elements hop the boundary to the host's offset ancestor.
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const shadow = host.attachShadow({mode: 'open'});
+ const shadowChild = document.createElement('div');
+ shadow.appendChild(shadowChild);
+ testing.expectEqual(document.body, shadowChild.offsetParent);
+
+ // A positioned host is itself the offsetParent.
+ host.style.position = 'relative';
+ testing.expectEqual(host, shadowChild.offsetParent);
+}
+</script>
+
<script id="scrollIntoView">
{
// Add an element deep in the document so its position is non-zero.
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index c276650ad..f5d048d0e 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1353,6 +1353,52 @@ pub fn getOffsetLeft(self: *Element, frame: *Frame) f64 {
return calculateSiblingPosition(self.asNode());
}
+pub fn getOffsetParent(self: *Element, frame: *Frame) ?*Element {
+ if (!self.asNode().isConnected() or !self.checkVisibilityCached(null, frame)) {
+ return null;
+ }
+
+ switch (self.getTag()) {
+ .html, .body => return null,
+ else => {},
+ }
+
+ const self_position = self.positionStyle(frame);
+ if (std.mem.eql(u8, self_position, "fixed")) {
+ return null;
+ }
+ const self_static = self_position.len == 0 or std.mem.eql(u8, self_position, "static");
+
+ var node: ?*Node = self.asNode()._parent;
+ while (node) |n| {
+ if (n.is(ShadowRoot)) |sr| {
+ // this always pokes through the shadow dom
+ node = sr.getHost().asNode();
+ continue;
+ }
+ const ancestor = n.is(Element) orelse break;
+
+ const tag = ancestor.getTag();
+ if (tag == .body) {
+ return ancestor;
+ }
+ const position = ancestor.positionStyle(frame);
+ if (position.len > 0 and !std.mem.eql(u8, position, "static")) {
+ return ancestor;
+ }
+ if (self_static and (tag == .td or tag == .th or tag == .table)) {
+ return ancestor;
+ }
+ node = n._parent;
+ }
+ return null;
+}
+
+fn positionStyle(self: *Element, frame: *Frame) []const u8 {
+ const style = self.getStyle(frame) orelse return "";
+ return style.asCSSStyleDeclaration().getPropertyValue("position", frame);
+}
+
pub fn getClientTop(_: *Element) f64 {
// Border width - in our dummy layout, we don't apply borders to layout
return 0.0;
@@ -1998,6 +2044,7 @@ pub const JsApi = struct {
pub const offsetLeft = bridge.accessor(Element.getOffsetLeft, null, .{});
pub const offsetWidth = bridge.accessor(Element.getOffsetWidth, null, .{});
pub const offsetHeight = bridge.accessor(Element.getOffsetHeight, null, .{});
+ pub const offsetParent = bridge.accessor(Element.getOffsetParent, null, .{});
pub const getClientRects = bridge.function(Element.getClientRects, .{});
pub const getBoundingClientRect = bridge.function(Element.getBoundingClientRect, .{});
pub const getElementsByTagName = bridge.function(Element.getElementsByTagName, .{});