Skip to content

Truly independent web browser

LibWeb: Drop stale callbacks when a use element changes its reference

Andreas Klingcdee858a1566
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
index c9d713f6258..e6c5700b913 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
@@ -257,11 +257,16 @@ Optional<u64> SharedResourceRequest::add_removable_callbacks(Function<void()> on
void SharedResourceRequest::remove_callbacks(u64 id)
{
- // NB: Only mark the entry as no longer waiting, and skip it when invoking the callbacks. A callback that is being
- // invoked can get here, and the invocation goes on over m_callbacks.
- for (auto& callbacks : m_callbacks) {
- if (callbacks.id == id)
- callbacks.id = 0;
+ // NB: The callbacks are only invoked once the request is no longer new or fetching. A callback that is being
+ // invoked can get here, and the invocation goes on over m_callbacks, so then only mark the entry as no longer
+ // waiting, and skip it when invoking the callbacks.
+ if (m_state == State::New || m_state == State::Fetching) {
+ m_callbacks.remove_first_matching([id](auto const& callbacks) { return callbacks.id == id; });
+ } else {
+ for (auto& callbacks : m_callbacks) {
+ if (callbacks.id == id)
+ callbacks.id = 0;
+ }
}
// NB: The fetch keeps going so that its result can still be cached, but once nothing waits for it anymore, it
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cpp
index e23a182d40d..5f9b0d99206 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp
@@ -188,6 +188,10 @@ Optional<Utf16String> SVGUseElement::href_value() const
// https://www.w3.org/TR/SVG2/linking.html#processingURL
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();
+
// 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
// it with the document base URL. If all parts other than the target fragment are equal, this is
@@ -269,6 +273,15 @@ GC::Ptr<DOM::Element> SVGUseElement::referenced_element() const
return as<SVG::SVGDecodedImageData>(*data).svg_document().get_element_by_id(decode_fragment_identifier(*m_href->fragment()));
}
+void SVGUseElement::stop_using_resource_request()
+{
+ 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;
+ m_load_event_delayer.clear();
+}
+
// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
void SVGUseElement::fetch_the_document(URL::URL const& url)
{
@@ -284,9 +297,11 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)
auto document_url = url;
document_url.set_fragment({});
+ stop_using_resource_request();
+
m_load_event_delayer.emplace(document());
m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), document_url, HTML::CORSSettingAttribute::NoCORS);
- m_resource_request->add_callbacks(
+ m_resource_request_callbacks_id = m_resource_request->add_removable_callbacks(
[this] {
clone_element_tree_as_our_shadow_tree(referenced_element());
m_load_event_delayer.clear();
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.h b/Libraries/LibWeb/SVG/SVGUseElement.h
index 0e4e6dbb752..b5c2a84a0dc 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.h
+++ b/Libraries/LibWeb/SVG/SVGUseElement.h
@@ -69,6 +69,7 @@ private:
GC::Ptr<DOM::Element> referenced_element() const;
void fetch_the_document(URL::URL const& url);
+ void stop_using_resource_request();
bool is_referenced_element_same_document() const;
void clone_element_tree_as_our_shadow_tree(GC::Ptr<Element> to_clone);
@@ -84,6 +85,7 @@ private:
GC::Ptr<DOM::DocumentObserver> m_document_observer;
GC::Ptr<HTML::SharedResourceRequest> m_resource_request;
+ Optional<u64> m_resource_request_callbacks_id;
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
IntrusiveListNode<SVGUseElement> m_list_node;
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-repeated-href-changes-on-stalled-sprite.txt b/Tests/LibWeb/Text/expected/SVG/use-element-repeated-href-changes-on-stalled-sprite.txt
new file mode 100644
index 00000000000..8d10db2ac80
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-repeated-href-changes-on-stalled-sprite.txt
@@ -0,0 +1,3 @@
+Requests for the sprite: 1
+useChanging: second-circle
+useStable: first-rect
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-same-document-href-stops-delaying-load.txt b/Tests/LibWeb/Text/expected/SVG/use-element-same-document-href-stops-delaying-load.txt
new file mode 100644
index 00000000000..a837bccdd70
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-same-document-href-stops-delaying-load.txt
@@ -0,0 +1 @@
+Load event fired, use: local-rect
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-repeated-href-changes-on-stalled-sprite.html b/Tests/LibWeb/Text/input/SVG/use-element-repeated-href-changes-on-stalled-sprite.html
new file mode 100644
index 00000000000..de195e087e8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-repeated-href-changes-on-stalled-sprite.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<body>
+<svg width="0" height="0"><symbol id="local"><rect id="local-rect" width="10" height="10"/></symbol></svg>
+<script>
+ const server = httpTestServer();
+ const spriteURL = server.echoURL("/use-element-repeated-href-changes-stalled-sprite.svg");
+ const unblockToken = `use-element-repeated-href-changes-stalled-sprite-${crypto.randomUUID()}`;
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", `${server.baseURL}/echo`, false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path: new URL(spriteURL).pathname,
+ status: 200,
+ headers: { "Content-Type": "image/svg+xml", "Cache-Control": "no-store" },
+ body: `<svg xmlns="http://www.w3.org/2000/svg">
+ <symbol id="first"><rect id="first-rect" width="10" height="10"/></symbol>
+ <symbol id="second"><circle id="second-circle" r="5"/></symbol>
+ </svg>`,
+ wait_for_unblock: unblockToken,
+ }));
+
+ document.write(`
+ <svg width="10" height="10"><use id="useChanging" href="${spriteURL}#first"/></svg>
+ <svg width="10" height="10"><use id="useStable" href="${spriteURL}#first"/></svg>
+ `);
+
+ const useChanging = document.getElementById("useChanging");
+ for (let i = 0; i < 100; ++i) {
+ useChanging.setAttribute("href", "#local");
+ useChanging.setAttribute("href", `${spriteURL}#first`);
+ }
+ useChanging.setAttribute("href", `${spriteURL}#second`);
+
+ asyncTest(done => {
+ window.addEventListener("load", async () => {
+ const requestCount = await (await fetch(server.recordedRequestCountURL(spriteURL))).json();
+ println(`Requests for the sprite: ${requestCount}`);
+ for (const id of ["useChanging", "useStable"]) {
+ const instance = internals.getShadowRoot(document.getElementById(id)).firstElementChild;
+ println(`${id}: ${instance?.firstElementChild?.id ?? "nothing"}`);
+ }
+ done();
+ });
+ fetch(`${server.baseURL}/unblock/${unblockToken}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-same-document-href-stops-delaying-load.html b/Tests/LibWeb/Text/input/SVG/use-element-same-document-href-stops-delaying-load.html
new file mode 100644
index 00000000000..950a1be580f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-same-document-href-stops-delaying-load.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<body>
+<svg width="0" height="0"><symbol id="local"><rect id="local-rect" width="10" height="10"/></symbol></svg>
+<script>
+ const server = httpTestServer();
+ const spriteURL = server.echoURL("/use-element-same-document-href-stalled-sprite.svg");
+ const unblockToken = `use-element-same-document-href-stalled-sprite-${crypto.randomUUID()}`;
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", `${server.baseURL}/echo`, false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path: new URL(spriteURL).pathname,
+ status: 200,
+ headers: { "Content-Type": "image/svg+xml", "Cache-Control": "no-store" },
+ body: `<svg xmlns="http://www.w3.org/2000/svg"><symbol id="remote"><circle id="remote-circle" r="5"/></symbol></svg>`,
+ wait_for_unblock: unblockToken,
+ }));
+
+ document.write(`<svg width="10" height="10"><use id="use" href="${spriteURL}#remote"/></svg>`);
+ document.getElementById("use").setAttribute("href", "#local");
+
+ asyncTest(done => {
+ window.addEventListener("load", async () => {
+ const instance = internals.getShadowRoot(document.getElementById("use")).firstElementChild;
+ println(`Load event fired, use: ${instance?.firstElementChild?.id ?? "nothing"}`);
+ await fetch(`${server.baseURL}/unblock/${unblockToken}`);
+ done();
+ });
+ });
+</script>