LibWeb: Add Internals.getShadowRoot(element)

This lets you access closed shadow roots from JavaScript, even though
they're not normally accessible to JavaScript. This can be used to poke
into UA shadow roots in tests.
This commit is contained in:
Andreas Kling 2025-08-07 16:23:55 +02:00 committed by Alexander Kalenik
commit 1402c143a9
Notes: github-actions[bot] 2025-08-07 20:17:29 +00:00
5 changed files with 22 additions and 0 deletions

View file

@ -325,4 +325,9 @@ String Internals::dump_display_list()
return window().associated_document().dump_display_list();
}
GC::Ptr<DOM::ShadowRoot> Internals::get_shadow_root(GC::Ref<DOM::Element> element)
{
return element->shadow_root();
}
}

View file

@ -66,6 +66,8 @@ public:
String dump_display_list();
GC::Ptr<DOM::ShadowRoot> get_shadow_root(GC::Ref<DOM::Element>);
private:
explicit Internals(JS::Realm&);

View file

@ -54,4 +54,8 @@ interface Internals {
readonly attribute boolean headless;
DOMString dumpDisplayList();
// Returns the shadow root of the element, if it has one, even if it's not normally accessible to JS.
ShadowRoot? getShadowRoot(Element element);
};

View file

@ -0,0 +1,2 @@
null
[object ShadowRoot]

View file

@ -0,0 +1,9 @@
<!doctype html>
<script src="../include.js"></script>
<input id="myInput">
<script>
test(() => {
println(myInput.shadowRoot);
println(internals.getShadowRoot(myInput));
});
</script>