Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: match the root element for :scope on a document

Jay Hemnani4e98fc7df344
diff --git a/src/browser/tests/element/query_selector_scope.html b/src/browser/tests/element/query_selector_scope.html
index 0f71ba153..b510af07c 100644
--- a/src/browser/tests/element/query_selector_scope.html
+++ b/src/browser/tests/element/query_selector_scope.html
@@ -126,3 +126,13 @@
testing.expectEqual($('#p1'), firstParent);
}
</script>
+
+<script id="scope_document">
+{
+ // On a document, :scope is the root element
+ testing.expectEqual(document.documentElement, document.querySelector(':scope'));
+ testing.expectEqual(1, document.querySelectorAll(':scope').length);
+ testing.expectEqual(document.body, document.querySelector(':scope > body'));
+ testing.expectEqual(0, document.createDocumentFragment().querySelectorAll(':scope').length);
+}
+</script>
diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zig
index 44f5abb37..9fe01a65c 100644
--- a/src/browser/webapi/selector/List.zig
+++ b/src/browser/webapi/selector/List.zig
@@ -691,7 +691,12 @@ fn matchesPseudoClass(el: *Node.Element, pseudo: Selector.PseudoClass, scope: *N
return parent._type == .document;
},
.scope => {
- // :scope matches the reference element (querySelector root)
+ // :scope matches the reference element (querySelector root);
+ // for a document that is its root element
+ if (scope._type == .document) {
+ const parent = node.parentNode() orelse return false;
+ return parent == scope;
+ }
return node == scope;
},
.empty => {