Skip to content

Truly independent web browser

LibWeb: Don’t require an svg root in a use element’s document

sideshowbarker91ef01e824d6
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 3cc455ca18e..b127b4f36d7 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -847,6 +847,7 @@ set(SOURCES
SVG/SVGPreserveAspectRatio.cpp
SVG/SVGRadialGradientElement.cpp
SVG/SVGRectElement.cpp
+ SVG/SVGResourceDocument.cpp
SVG/SVGScriptElement.cpp
SVG/SVGSetElement.cpp
SVG/SVGStopElement.cpp
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 5c07600dc34..d4b23d52319 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -265,6 +265,7 @@
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGMaskElement.h>
#include <LibWeb/SVG/SVGPatternElement.h>
+#include <LibWeb/SVG/SVGResourceDocument.h>
#include <LibWeb/SVG/SVGSVGElement.h>
#include <LibWeb/SVG/SVGScriptElement.h>
#include <LibWeb/SVG/SVGStyleElement.h>
@@ -1076,6 +1077,8 @@ void Document::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_list_owners_with_stale_item_counters);
visitor.visit(m_shared_resource_requests);
+ visitor.visit(m_svg_resource_documents);
+ visitor.visit(m_svg_resource_document_owner);
for (auto& resource : m_css_image_resources)
resource.value->visit_edges(visitor);
for (auto& resource : m_css_image_resources_with_request_url_modifiers)
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index edf873060e2..7c0e8e971b6 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1386,6 +1386,9 @@ public:
HashMap<HTML::SharedResourceRequest::Key, GC::Ptr<HTML::SharedResourceRequest>>& shared_resource_requests();
HashMap<HTML::SharedResourceRequest::Key, GC::Ptr<HTML::SharedResourceRequest>> const& shared_resource_requests() const;
+ HashMap<URL::URL, GC::Ref<SVG::SVGResourceDocument>>& svg_resource_documents() { return m_svg_resource_documents; }
+ GC::Ptr<Document> svg_resource_document_owner() const { return m_svg_resource_document_owner; }
+ void set_svg_resource_document_owner(GC::Ref<Document> owner) { m_svg_resource_document_owner = owner; }
CSS::ImageStyleValueResource* css_image_resource(URL::URL const&);
CSS::ImageStyleValueResource& create_css_image_resource(CSS::ImageStyleValueResourceKey, GC::Ref<HTML::SharedResourceRequest>);
void remove_css_image_resource_if_unused(CSS::ImageStyleValueResourceKey const&);
@@ -2393,6 +2396,12 @@ private:
RefPtr<HTML::SessionHistoryEntry> m_latest_entry;
HashMap<HTML::SharedResourceRequest::Key, GC::Ptr<HTML::SharedResourceRequest>> m_shared_resource_requests;
+
+ // The documents that use elements of this document refer to, by URL.
+ HashMap<URL::URL, GC::Ref<SVG::SVGResourceDocument>> m_svg_resource_documents;
+
+ // For a document that a use element's reference loaded, the document whose resource documents it shares.
+ GC::Ptr<Document> m_svg_resource_document_owner;
HashMap<URL::URL, OwnPtr<CSS::ImageStyleValueResource>> m_css_image_resources;
// NB: The resources of url() values with request modifiers. m_css_image_resources holds those without any, which
// most url() values are.
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 9bb1974ab47..7a42d9f58fe 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -1156,6 +1156,7 @@ class SVGPatternElement;
class SVGPolygonElement;
class SVGPolylineElement;
class SVGRectElement;
+class SVGResourceDocument;
class SVGScriptElement;
class SVGSetElement;
class SVGSVGElement;
diff --git a/Libraries/LibWeb/SVG/SVGResourceDocument.cpp b/Libraries/LibWeb/SVG/SVGResourceDocument.cpp
new file mode 100644
index 00000000000..930a4cd51e7
--- /dev/null
+++ b/Libraries/LibWeb/SVG/SVGResourceDocument.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGC/Heap.h>
+#include <LibGC/Weak.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/Fetch/Fetching/Fetching.h>
+#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
+#include <LibWeb/Fetch/Infrastructure/FetchController.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
+#include <LibWeb/Fetch/Infrastructure/HTTP/ResponseView.h>
+#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/SVG/SVGResourceDocument.h>
+#include <LibWeb/XML/XMLDocumentBuilder.h>
+#include <LibWebCommon/Fetch/Infrastructure/HTTP/Statuses.h>
+#include <LibXML/Parser/Parser.h>
+
+namespace Web::SVG {
+
+GC_DEFINE_ALLOCATOR(SVGResourceDocument);
+
+GC::Ref<SVGResourceDocument> SVGResourceDocument::get_or_create(DOM::Document& document, URL::URL const& url)
+{
+ // A resource document's own use elements share the cache of the document that loaded it, so every URL loads once
+ // per page, and documents that refer to each other find each other there rather than loading each other anew.
+ auto& owner = document.svg_resource_document_owner() ? *document.svg_resource_document_owner() : document;
+ auto& resource_documents = owner.svg_resource_documents();
+ if (auto it = resource_documents.find(url); it != resource_documents.end())
+ return *it->value;
+ auto resource_document = document.heap().allocate<SVGResourceDocument>(owner, url);
+ resource_documents.set(url, resource_document);
+ return resource_document;
+}
+
+SVGResourceDocument::SVGResourceDocument(DOM::Document& document, URL::URL url)
+ : m_document(document)
+ , m_url(move(url))
+{
+}
+
+void SVGResourceDocument::visit_edges(Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(m_document);
+ visitor.visit(m_resource_document);
+ visitor.visit(m_fetch_controller);
+ for (auto const& callback : m_callbacks)
+ visitor.visit(callback.on_done);
+}
+
+void SVGResourceDocument::fetch(NonnullRefPtr<Fetch::Infrastructure::Request> request)
+{
+ VERIFY(needs_fetching());
+ m_state = State::Fetching;
+
+ if (!ResourceLoader::is_initialized()) {
+ finish(nullptr);
+ return;
+ }
+
+ auto& realm = HTML::relevant_realm(*m_document);
+ GC::Weak weak_this { *this };
+ Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Fetch::Engine::BodyIntent::DeliverStreamToConsumerAgent };
+ fetch_algorithms_input.process_response = GC::create_function<void(GC::Ref<Fetch::Infrastructure::ResponseView>)>(GC::Heap::the(), GC::captures(weak_this, realm), [](auto& weak_this, auto& realm, GC::Ref<Fetch::Infrastructure::ResponseView> response) {
+ auto self = weak_this.ptr();
+ if (!self)
+ return;
+ if (!response->body()) {
+ self->finish(nullptr);
+ return;
+ }
+ if (!Fetch::Infrastructure::is_ok_status(response->status())) {
+ // NB: The body of an error response still has to be read, or its transport stays paused until the network
+ // gives up on it.
+ auto ignore_chunk = GC::create_function(GC::Heap::the(), [](ByteBuffer) { });
+ auto ignore_end_of_body = GC::create_function(GC::Heap::the(), [] { });
+ auto ignore_body_error = GC::create_function(GC::Heap::the(), [](JS::Value) { });
+ (void)response->body()->incrementally_read(realm, ignore_chunk, ignore_end_of_body, ignore_body_error, GC::Ref { realm.global_object() });
+ self->finish(nullptr);
+ return;
+ }
+ auto url = response->url().value_or(self->m_url);
+ auto process_body = GC::create_function(GC::Heap::the(), GC::captures(weak_this, url), [](auto& weak_this, auto& url, ByteBuffer data) {
+ auto self = weak_this.ptr();
+ if (!self)
+ return;
+
+ // The document is parsed with scripting disabled, like an image's, and only lends elements to the shadow
+ // trees of use elements, so it needs no browsing context of its own.
+ auto& document = *self->m_document;
+ auto resource_document = DOM::Document::create(document.page(), document.relevant_global_event_target(), url);
+ resource_document->set_content_type("image/svg+xml"_utf16_fly_string);
+ resource_document->set_document_type(DOM::Document::Type::XML);
+ resource_document->set_origin(document.origin());
+ resource_document->set_svg_resource_document_owner(document);
+
+ XML::Parser parser(StringView { data.bytes() }, { .resolve_named_html_entity = resolve_named_html_entity });
+ XMLDocumentBuilder builder { *resource_document, XMLScriptingSupport::Disabled };
+ auto result = parser.parse_with_listener(builder);
+ if (result.is_error() || builder.has_error()) {
+ dbgln("SVGResourceDocument: Failed to parse {}", url);
+ self->finish(nullptr);
+ return;
+ }
+ self->finish(resource_document);
+ });
+ auto process_body_error = GC::create_function(GC::Heap::the(), GC::captures(weak_this), [](auto& weak_this, JS::Value) {
+ if (auto self = weak_this.ptr())
+ self->finish(nullptr);
+ });
+ response->body()->fully_read(realm, process_body, process_body_error, GC::Ref { realm.global_object() });
+ });
+
+ m_fetch_controller = Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(move(fetch_algorithms_input)));
+ m_fetch_controller->set_stop_steps(GC::create_function(GC::Heap::the(), GC::captures(weak_this), [](auto& weak_this) {
+ if (auto self = weak_this.ptr())
+ self->stop();
+ }));
+}
+
+Optional<u64> SVGResourceDocument::add_callback(GC::Ref<GC::Function<void()>> on_done)
+{
+ if (m_state == State::Done) {
+ on_done->function()();
+ return {};
+ }
+ auto id = m_next_callback_id++;
+ m_callbacks.append({ id, on_done });
+ return id;
+}
+
+void SVGResourceDocument::remove_callback(u64 id)
+{
+ // NB: A callback that is being called can get here, while finish() goes on over m_callbacks, so a finished
+ // resource only marks the entry as no longer waiting.
+ if (m_state != State::Done) {
+ m_callbacks.remove_first_matching([id](auto const& callback) { return callback.id == id; });
+ return;
+ }
+ for (auto& callback : m_callbacks) {
+ if (callback.id == id)
+ callback.id = 0;
+ }
+}
+
+void SVGResourceDocument::finish(GC::Ptr<DOM::Document> resource_document)
+{
+ if (m_state == State::Done)
+ return;
+ m_state = State::Done;
+ m_resource_document = resource_document;
+ m_fetch_controller = nullptr;
+ for (auto const& callback : m_callbacks) {
+ if (callback.id != 0)
+ callback.on_done->function()();
+ }
+ m_callbacks.clear();
+}
+
+// NB: A stopped fetch never responds, so its waiters end without a document, and a later reference to the URL fetches
+// it again.
+void SVGResourceDocument::stop()
+{
+ if (m_state != State::Fetching)
+ return;
+ auto& resource_documents = m_document->svg_resource_documents();
+ if (auto it = resource_documents.find(m_url); it != resource_documents.end() && it->value.ptr() == this)
+ resource_documents.remove(it);
+ finish(nullptr);
+}
+
+}
diff --git a/Libraries/LibWeb/SVG/SVGResourceDocument.h b/Libraries/LibWeb/SVG/SVGResourceDocument.h
new file mode 100644
index 00000000000..69d9ec34ad9
--- /dev/null
+++ b/Libraries/LibWeb/SVG/SVGResourceDocument.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Vector.h>
+#include <LibGC/Function.h>
+#include <LibGC/Ptr.h>
+#include <LibJS/Heap/Cell.h>
+#include <LibURL/URL.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::SVG {
+
+// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
+// The document that a use element's reference to another document fetches. It's a resource document, not an image: Any
+// well-formed XML document will do, whatever its root element, since a use element only looks up an element in it by
+// its ID. Every use element of a document that refers to the same URL shares one.
+class SVGResourceDocument final : public JS::Cell {
+ GC_CELL(SVGResourceDocument, JS::Cell);
+ GC_DECLARE_ALLOCATOR(SVGResourceDocument);
+
+public:
+ static GC::Ref<SVGResourceDocument> get_or_create(DOM::Document&, URL::URL const&);
+
+ // The parsed document, once the fetch has finished; null until then, and after a failed fetch or parse.
+ GC::Ptr<DOM::Document> document() const { return m_resource_document; }
+
+ bool needs_fetching() const { return m_state == State::New; }
+ void fetch(NonnullRefPtr<Fetch::Infrastructure::Request>);
+
+ // Calls on_done once the fetch has ended, whether it gave a document or not. If it has already ended, calls it at
+ // once and returns no ID.
+ Optional<u64> add_callback(GC::Ref<GC::Function<void()>> on_done);
+ void remove_callback(u64 id);
+
+private:
+ SVGResourceDocument(DOM::Document&, URL::URL);
+
+ virtual void visit_edges(Visitor&) override;
+
+ void finish(GC::Ptr<DOM::Document>);
+ void stop();
+
+ enum class State {
+ New,
+ Fetching,
+ Done,
+ };
+
+ struct Callback {
+ u64 id { 0 };
+ GC::Ref<GC::Function<void()>> on_done;
+ };
+
+ GC::Ref<DOM::Document> m_document;
+ URL::URL m_url;
+ State m_state { State::New };
+ GC::Ptr<DOM::Document> m_resource_document;
+ GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
+ Vector<Callback> m_callbacks;
+ u64 m_next_callback_id { 1 };
+};
+
+}
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cpp
index d25f95950bf..7e052d18109 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp
@@ -17,11 +17,10 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/HTML/PotentialCORSRequest.h>
#include <LibWeb/HTML/Scripting/Environments.h>
-#include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/FragmentIdentifier.h>
-#include <LibWeb/SVG/SVGDecodedImageData.h>
+#include <LibWeb/SVG/SVGResourceDocument.h>
#include <LibWeb/SVG/SVGSVGElement.h>
#include <LibWeb/SVG/SVGSymbolElement.h>
#include <LibWeb/SVG/SVGUseElement.h>
@@ -64,7 +63,7 @@ void SVGUseElement::visit_edges(Cell::Visitor& visitor)
Base::visit_edges(visitor);
SVGURIReferenceMixin::visit_edges(visitor);
visitor.visit(m_document_observer);
- visitor.visit(m_resource_request);
+ visitor.visit(m_resource_document);
}
void SVGUseElement::adopted_from(DOM::Document& old_document)
@@ -121,7 +120,7 @@ void SVGUseElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancest
// NB: The request of a disconnected use element must not delay the load event, which matters for an instance in a
// shadow tree that is being discarded. inserted() processes the URL again.
if (!is_connected() && m_load_event_delayer.has_value()) {
- stop_using_resource_request();
+ stop_using_resource_document();
m_href = {};
}
}
@@ -209,7 +208,7 @@ void SVGUseElement::process_the_url(Optional<Utf16String> const& href)
{
// NB: A request for a document that is no longer referenced must neither update the shadow tree nor delay the load
// event.
- stop_using_resource_request();
+ stop_using_resource_document();
// In all other cases, the URL is for a resource to be used in this SVG document. The user agent
// must parse the URL to separate out the target fragment from the rest of the URL, and compare
@@ -313,29 +312,25 @@ GC::Ptr<DOM::Element> SVGUseElement::referenced_element() const
return document_containing_reference().get_element_by_id(id);
}
- if (!m_resource_request)
+ if (!m_resource_document || !m_resource_document->document())
return nullptr;
- auto data = m_resource_request->image_data();
- if (!data || !is<SVG::SVGDecodedImageData>(*data))
- return nullptr;
-
- return as<SVG::SVGDecodedImageData>(*data).svg_document().get_element_by_id(decode_fragment_identifier(*m_href->fragment()));
+ return m_resource_document->document()->get_element_by_id(decode_fragment_identifier(*m_href->fragment()));
}
void SVGUseElement::discard_the_url()
{
clone_element_tree_as_our_shadow_tree(nullptr);
- stop_using_resource_request();
+ stop_using_resource_document();
m_href = {};
}
-void SVGUseElement::stop_using_resource_request()
+void SVGUseElement::stop_using_resource_document()
{
- if (m_resource_request && m_resource_request_callbacks_id.has_value())
- m_resource_request->remove_callbacks(*m_resource_request_callbacks_id);
- m_resource_request_callbacks_id = {};
- m_resource_request = nullptr;
+ if (m_resource_document && m_resource_document_callback_id.has_value())
+ m_resource_document->remove_callback(*m_resource_document_callback_id);
+ m_resource_document_callback_id = {};
+ m_resource_document = nullptr;
m_load_event_delayer.clear();
}
@@ -354,7 +349,7 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)
auto document_url = url;
document_url.set_fragment({});
- stop_using_resource_request();
+ stop_using_resource_document();
// https://svgwg.org/svg2-draft/struct.html#UseElement
// User agents may restrict external resource documents for security reasons. In particular, this specification
@@ -369,23 +364,16 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)
request->set_client(document().relevant_settings_object().address());
m_load_event_delayer.emplace(document());
- m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), *request);
- m_resource_request_callbacks_id = m_resource_request->add_removable_callbacks(
- GC::create_function(GC::Heap::the(), GC::captures(*this), [](SVGUseElement& self) {
- // Only a connected use element builds its shadow tree; inserted() builds it otherwise.
- if (self.is_connected())
- self.clone_element_tree_as_our_shadow_tree(self.referenced_element());
- self.m_load_event_delayer.clear();
- }),
- GC::create_function(GC::Heap::the(), GC::captures(*this), [](SVGUseElement& self) {
- self.m_load_event_delayer.clear();
- }),
- GC::create_function(GC::Heap::the(), GC::captures(*this), [](SVGUseElement& self) {
- self.m_load_event_delayer.clear();
- }));
-
- if (m_resource_request->needs_fetching())
- m_resource_request->fetch_resource(request);
+ m_resource_document = SVGResourceDocument::get_or_create(document(), document_url);
+ m_resource_document_callback_id = m_resource_document->add_callback(GC::create_function(GC::Heap::the(), GC::captures(*this), [](SVGUseElement& self) {
+ // Only a connected use element builds its shadow tree; inserted() builds it otherwise.
+ if (self.is_connected())
+ self.clone_element_tree_as_our_shadow_tree(self.referenced_element());
+ self.m_load_event_delayer.clear();
+ }));
+
+ if (m_resource_document->needs_fetching())
+ m_resource_document->fetch(request);
}
// https://svgwg.org/svg2-draft/struct.html#UseShadowTree
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.h b/Libraries/LibWeb/SVG/SVGUseElement.h
index 2d99ce83192..8b4f0858e94 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.h
+++ b/Libraries/LibWeb/SVG/SVGUseElement.h
@@ -75,7 +75,7 @@ private:
void fetch_the_document(URL::URL const& url);
void discard_the_url();
- void stop_using_resource_request();
+ void stop_using_resource_document();
bool is_referenced_element_same_document() const;
void request_reclone();
@@ -93,8 +93,8 @@ private:
Optional<URL::URL> m_href;
GC::Ptr<DOM::DocumentObserver> m_document_observer;
- GC::Ptr<HTML::SharedResourceRequest> m_resource_request;
- Optional<u64> m_resource_request_callbacks_id;
+ GC::Ptr<SVGResourceDocument> m_resource_document;
+ Optional<u64> m_resource_document_callback_id;
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
IntrusiveListNode<SVGUseElement> m_list_node;
diff --git a/Tests/LibWeb/Crash/SVG/use-element-external-documents-referring-to-each-other.html b/Tests/LibWeb/Crash/SVG/use-element-external-documents-referring-to-each-other.html
new file mode 100644
index 00000000000..6c7e1ceff41
--- /dev/null
+++ b/Tests/LibWeb/Crash/SVG/use-element-external-documents-referring-to-each-other.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html class="test-wait">
+<svg><use href="../../Text/data/svg-use-cycle-a.svg#icon"/></svg>
+<script>
+ // Each of the two documents refers to the other, which must load each of them once rather than without end.
+ window.onload = () => {
+ requestAnimationFrame(() => requestAnimationFrame(() => document.documentElement.classList.remove("test-wait")));
+ };
+</script>
+</html>
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 68c02665a0d..6c15b461ad1 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1268,6 +1268,7 @@ Text/input/wpt-import/import-maps/data-driven/resolving.html
; A use element does not render elements of a cross-origin document, and every file: URL is a different origin.
Crash/SVG/use-element-fetch-in-collected-document.html
Crash/SVG/use-element-with-cached-external-reference-adopted.html
+Crash/SVG/use-element-external-documents-referring-to-each-other.html
Layout/input/svg/svg-use-element-crashtest.html
Text/input/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.html
Text/input/SVG/use-element-blocks-cross-origin-requests.html
@@ -1277,6 +1278,7 @@ Text/input/SVG/use-element-external-reference-after-adoption.html
Text/input/SVG/use-element-fetches-once-connected.html
Text/input/SVG/use-element-nested-external-fragment.html
Text/input/SVG/use-element-nested-external-fragment-after-redirect.html
+Text/input/SVG/use-element-external-document-without-svg-root.html
Text/input/SVG/use-element-relative-external-reference-after-adoption.html
Text/input/SVG/use-element-repeated-href-changes-on-stalled-sprite.html
Text/input/SVG/use-element-same-document-href-stops-delaying-load.html
diff --git a/Tests/LibWeb/Text/data/svg-sprite-root-without-namespace.svg b/Tests/LibWeb/Text/data/svg-sprite-root-without-namespace.svg
new file mode 100644
index 00000000000..aecf47fd127
--- /dev/null
+++ b/Tests/LibWeb/Text/data/svg-sprite-root-without-namespace.svg
@@ -0,0 +1 @@
+<svg style="position: absolute"><symbol xmlns="http://www.w3.org/2000/svg" id="icon" viewBox="0 0 10 20"><rect width="10" height="20"/></symbol></svg>
diff --git a/Tests/LibWeb/Text/data/svg-use-cycle-a.svg b/Tests/LibWeb/Text/data/svg-use-cycle-a.svg
new file mode 100644
index 00000000000..4dbc7865d0c
--- /dev/null
+++ b/Tests/LibWeb/Text/data/svg-use-cycle-a.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg"><symbol id="icon"><use href="svg-use-cycle-b.svg#icon"/></symbol></svg>
diff --git a/Tests/LibWeb/Text/data/svg-use-cycle-b.svg b/Tests/LibWeb/Text/data/svg-use-cycle-b.svg
new file mode 100644
index 00000000000..c73085c065e
--- /dev/null
+++ b/Tests/LibWeb/Text/data/svg-use-cycle-b.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg"><symbol id="icon"><use href="svg-use-cycle-a.svg#icon"/></symbol></svg>
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-external-document-without-svg-root.txt b/Tests/LibWeb/Text/expected/SVG/use-element-external-document-without-svg-root.txt
new file mode 100644
index 00000000000..131d973401c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-external-document-without-svg-root.txt
@@ -0,0 +1 @@
+10x20
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-external-document-without-svg-root.html b/Tests/LibWeb/Text/input/SVG/use-element-external-document-without-svg-root.html
new file mode 100644
index 00000000000..01c98318696
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-external-document-without-svg-root.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg width="100" height="100">
+ <use id="instance" href="../../data/svg-sprite-root-without-namespace.svg#icon" width="10" height="20"/>
+</svg>
+<script>
+ // The referenced document's root has no namespace, so it's no SVG image, but a use element only needs to find the
+ // symbol in it by its ID.
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ const { width, height } = document.getElementById("instance").getBBox();
+ println(`${width}x${height}`);
+ done();
+ });
+ });
+</script>