From 1402c143a9c21d561d8298cf4d79b7383b59026a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 7 Aug 2025 16:23:55 +0200 Subject: [PATCH] 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. --- Libraries/LibWeb/Internals/Internals.cpp | 5 +++++ Libraries/LibWeb/Internals/Internals.h | 2 ++ Libraries/LibWeb/Internals/Internals.idl | 4 ++++ Tests/LibWeb/Text/expected/Internals/getShadowRoot.txt | 2 ++ Tests/LibWeb/Text/input/Internals/getShadowRoot.html | 9 +++++++++ 5 files changed, 22 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/Internals/getShadowRoot.txt create mode 100644 Tests/LibWeb/Text/input/Internals/getShadowRoot.html diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp index 5c09d496646..88be63572f4 100644 --- a/Libraries/LibWeb/Internals/Internals.cpp +++ b/Libraries/LibWeb/Internals/Internals.cpp @@ -325,4 +325,9 @@ String Internals::dump_display_list() return window().associated_document().dump_display_list(); } +GC::Ptr Internals::get_shadow_root(GC::Ref element) +{ + return element->shadow_root(); +} + } diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h index 806a69f5105..274e13cd5f7 100644 --- a/Libraries/LibWeb/Internals/Internals.h +++ b/Libraries/LibWeb/Internals/Internals.h @@ -66,6 +66,8 @@ public: String dump_display_list(); + GC::Ptr get_shadow_root(GC::Ref); + private: explicit Internals(JS::Realm&); diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl index 2efb9cc9dfb..d81b4c11f65 100644 --- a/Libraries/LibWeb/Internals/Internals.idl +++ b/Libraries/LibWeb/Internals/Internals.idl @@ -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); + }; diff --git a/Tests/LibWeb/Text/expected/Internals/getShadowRoot.txt b/Tests/LibWeb/Text/expected/Internals/getShadowRoot.txt new file mode 100644 index 00000000000..75acec5f8bd --- /dev/null +++ b/Tests/LibWeb/Text/expected/Internals/getShadowRoot.txt @@ -0,0 +1,2 @@ +null +[object ShadowRoot] diff --git a/Tests/LibWeb/Text/input/Internals/getShadowRoot.html b/Tests/LibWeb/Text/input/Internals/getShadowRoot.html new file mode 100644 index 00000000000..22893afbb17 --- /dev/null +++ b/Tests/LibWeb/Text/input/Internals/getShadowRoot.html @@ -0,0 +1,9 @@ + + + +