diff --git a/Libraries/LibWeb/DOM/ParentNode.cpp b/Libraries/LibWeb/DOM/ParentNode.cpp index 2f2b4b64761..38fcce31076 100644 --- a/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Libraries/LibWeb/DOM/ParentNode.cpp @@ -262,14 +262,16 @@ GC::Ref ParentNode::get_elements_by_class_name(StringView class_ GC::Ptr ParentNode::get_element_by_id(FlyString const& id) const { - // For document and shadow root we have a cache that allows fast lookup. - if (is_document()) { - auto const& document = static_cast(*this); - return document.element_by_id().get(id); - } - if (is_shadow_root()) { - auto const& shadow_root = static_cast(*this); - return shadow_root.element_by_id().get(id); + if (is_connected()) { + // For connected document and shadow root we have a cache that allows fast lookup. + if (is_document()) { + auto const& document = static_cast(*this); + return document.element_by_id().get(id); + } + if (is_shadow_root()) { + auto const& shadow_root = static_cast(*this); + return shadow_root.element_by_id().get(id); + } } GC::Ptr found_element; diff --git a/Tests/LibWeb/Text/expected/DOM/getElementById-on-unconnected-shadow-tree.txt b/Tests/LibWeb/Text/expected/DOM/getElementById-on-unconnected-shadow-tree.txt new file mode 100644 index 00000000000..3da1ec26e9c --- /dev/null +++ b/Tests/LibWeb/Text/expected/DOM/getElementById-on-unconnected-shadow-tree.txt @@ -0,0 +1 @@ +HelloWorld diff --git a/Tests/LibWeb/Text/input/DOM/getElementById-on-unconnected-shadow-tree.html b/Tests/LibWeb/Text/input/DOM/getElementById-on-unconnected-shadow-tree.html new file mode 100644 index 00000000000..498e8fbd508 --- /dev/null +++ b/Tests/LibWeb/Text/input/DOM/getElementById-on-unconnected-shadow-tree.html @@ -0,0 +1,13 @@ + + + + \ No newline at end of file