Truly independent web browser
LibWeb: Don’t build a use element’s shadow tree during adoption
sideshowbarker8680f2844cc7
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cppindex 5f9b0d99206..1d373a0d0f4 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp@@ -75,6 +75,12 @@ void SVGUseElement::adopted_from(DOM::Document& old_document)m_document_observer->retarget_for_adoption(document());+ // Adoption removed us from our tree, so nothing of ours renders, and our shadow tree is a clone made for the old+ // document; our href may resolve to something else in this one. Drop it, and leave the new clone to inserted(),+ // once we're in a document tree again. A clone made here would join the tree being adopted — so a nested use+ // element in it would then run the adopting steps as if it had come from the old document.+ clone_element_tree_as_our_shadow_tree(nullptr);+auto href = href_value();if (!href.has_value())return;@@ -83,33 +89,33 @@ void SVGUseElement::adopted_from(DOM::Document& old_document)if (!m_href.has_value())return;- if (!is_referenced_element_same_document()) {+ if (!is_referenced_element_same_document())fetch_the_document(*m_href);- return;- }-- if (auto to_clone = referenced_element())- clone_element_tree_as_our_shadow_tree(to_clone);}void SVGUseElement::inserted(){Base::inserted();- // Only use elements in the document's node tree react to changes of the elements they reference, mirroring the- // document-wide traversal this registry replaced. A use element inside a shadow tree is itself part of a clone.- if (!root().is_document())+ // A use element inside another use element's shadow tree is itself part of a clone, so it's left alone.+ auto* containing_shadow_root = as_if<DOM::ShadowRoot>(root());+ if (containing_shadow_root && containing_shadow_root->is_user_agent_internal())return;- register_for_referenced_element_changes();+ // Only use elements in the document's node tree react to changes of the elements they reference, mirroring the+ // document-wide traversal this registry replaced.+ if (root().is_document())+ register_for_referenced_element_changes();// The insertion that connected us may have inserted our referenced element along with us, with its insertion- // steps running before ours, i.e. before we were registered to be notified about changes to it. If our shadow- // tree has not been populated yet, try resolving the reference again.- if (!instance_root()- && m_href.has_value() && m_href->fragment().has_value() && is_referenced_element_same_document()) {- clone_element_tree_as_our_shadow_tree(referenced_element());- }+ // steps running before ours, i.e. before we were registered to be notified about changes to it. Adoption also+ // drops our shadow tree (see adopted_from()), we may now be in an author's shadow tree, and an external document+ // may have finished loading while we were disconnected. If our shadow tree has not been populated yet, try+ // resolving the reference again.+ if (!is_connected() || instance_root())+ return;+ if (auto to_clone = referenced_element())+ clone_element_tree_as_our_shadow_tree(to_clone);}void SVGUseElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root)@@ -303,7 +309,10 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), document_url, HTML::CORSSettingAttribute::NoCORS);m_resource_request_callbacks_id = m_resource_request->add_removable_callbacks([this] {- clone_element_tree_as_our_shadow_tree(referenced_element());+ // A request that already finished calls back right away, which from adopted_from() is in the middle of the+ // adoption. Only a connected use element builds its shadow tree; inserted() builds it otherwise.+ if (is_connected())+ clone_element_tree_as_our_shadow_tree(referenced_element());m_load_event_delayer.clear();},[this] {diff --git a/Tests/LibWeb/Crash/SVG/use-element-adopted-from-template-content.html b/Tests/LibWeb/Crash/SVG/use-element-adopted-from-template-content.htmlnew file mode 100644index 00000000000..65e9c7f05f4--- /dev/null+++ b/Tests/LibWeb/Crash/SVG/use-element-adopted-from-template-content.html@@ -0,0 +1,7 @@+<!DOCTYPE html>+<svg style="display: none"><symbol id="icon"><use href="#leaf"/></symbol><rect id="leaf" width="10" height="10"/></svg>+<template id="template"><svg><use href="#icon"/></svg></template>+<div id="target"></div>+<script>+ document.getElementById("target").replaceWith(document.getElementById("template").content.cloneNode(true));+</script>diff --git a/Tests/LibWeb/Crash/SVG/use-element-with-cached-external-reference-adopted.html b/Tests/LibWeb/Crash/SVG/use-element-with-cached-external-reference-adopted.htmlnew file mode 100644index 00000000000..a1588868b9f--- /dev/null+++ b/Tests/LibWeb/Crash/SVG/use-element-with-cached-external-reference-adopted.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<html class="test-wait">+<svg><use href="../../Text/data/svg-symbol-with-nested-use.svg#icon"/></svg>+<template id="template"><svg><use href="../../Text/data/svg-symbol-with-nested-use.svg#icon"/></svg></template>+<div id="target"></div>+<script>+ window.onload = () => {+ // The external document is loaded by now, so the adopted use element's request has already finished.+ document.getElementById("target").replaceWith(document.getElementById("template").content.cloneNode(true));+ document.documentElement.classList.remove("test-wait");+ };+</script>+</html>diff --git a/Tests/LibWeb/Text/data/svg-symbol-with-nested-use.svg b/Tests/LibWeb/Text/data/svg-symbol-with-nested-use.svgnew file mode 100644index 00000000000..a60d2eeada2--- /dev/null+++ b/Tests/LibWeb/Text/data/svg-symbol-with-nested-use.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg"><symbol id="icon"><use href="#leaf"/></symbol><rect id="leaf" width="10" height="20"/></svg>diff --git a/Tests/LibWeb/Text/data/use-adoption-a/icon.svg b/Tests/LibWeb/Text/data/use-adoption-a/icon.svgnew file mode 100644index 00000000000..a0982646e91--- /dev/null+++ b/Tests/LibWeb/Text/data/use-adoption-a/icon.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg"><g id="icon"><rect width="10" height="20"/></g></svg>diff --git a/Tests/LibWeb/Text/data/use-adoption-b/icon.svg b/Tests/LibWeb/Text/data/use-adoption-b/icon.svgnew file mode 100644index 00000000000..dff7640ccba--- /dev/null+++ b/Tests/LibWeb/Text/data/use-adoption-b/icon.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg"><g id="icon"><rect width="30" height="40"/></g></svg>diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-external-reference-after-adoption.txt b/Tests/LibWeb/Text/expected/SVG/use-element-external-reference-after-adoption.txtnew file mode 100644index 00000000000..907fb50d17b--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-element-external-reference-after-adoption.txt@@ -0,0 +1,2 @@+rendered: true+same box as a use inserted directly: truediff --git a/Tests/LibWeb/Text/expected/SVG/use-element-relative-external-reference-after-adoption.txt b/Tests/LibWeb/Text/expected/SVG/use-element-relative-external-reference-after-adoption.txtnew file mode 100644index 00000000000..939a9bc1d3c--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-element-relative-external-reference-after-adoption.txt@@ -0,0 +1,2 @@+before the move: 10x20+after the move: 30x40 (the other document's own use: 30x40)diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-shadow-tree-after-adoption.txt b/Tests/LibWeb/Text/expected/SVG/use-element-shadow-tree-after-adoption.txtnew file mode 100644index 00000000000..58a7fd3048a--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-element-shadow-tree-after-adoption.txt@@ -0,0 +1,3 @@+use from template content: 10x20+use from another document: 10x20+use from another document, in a shadow tree: 10x20diff --git a/Tests/LibWeb/Text/input/SVG/use-element-external-reference-after-adoption.html b/Tests/LibWeb/Text/input/SVG/use-element-external-reference-after-adoption.htmlnew file mode 100644index 00000000000..d83d86bed78--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-element-external-reference-after-adoption.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg><use id="direct" href="../../data/svg-symbol-with-nested-use.svg#icon"/></svg>+<template id="template"><svg><use id="from-template" href="../../data/svg-symbol-with-nested-use.svg#icon"/></svg></template>+<div id="target"></div>+<script>+ asyncTest(done => {+ window.onload = () => {+ document.getElementById("target").replaceWith(document.getElementById("template").content.cloneNode(true));+ const direct = document.getElementById("direct").getBBox();+ const fromTemplate = document.getElementById("from-template").getBBox();+ println(`rendered: ${fromTemplate.width > 0 && fromTemplate.height > 0}`);+ println(`same box as a use inserted directly: ${fromTemplate.width === direct.width && fromTemplate.height === direct.height}`);+ done();+ };+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/use-element-relative-external-reference-after-adoption.html b/Tests/LibWeb/Text/input/SVG/use-element-relative-external-reference-after-adoption.htmlnew file mode 100644index 00000000000..e08437c89d6--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-element-relative-external-reference-after-adoption.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<iframe id="a" srcdoc='<base href="../../data/use-adoption-a/"><svg><use id="moved" href="icon.svg#icon"/></svg>'></iframe>+<iframe id="b" srcdoc='<base href="../../data/use-adoption-b/"><svg><use id="resident" href="icon.svg#icon"/></svg>'></iframe>+<script>+ asyncTest(done => {+ window.onload = () => {+ const a = document.getElementById("a").contentDocument;+ const b = document.getElementById("b").contentDocument;+ const box = (use) => { const r = use.getBBox(); return `${r.width}x${r.height}`; };+ const moved = a.getElementById("moved");+ println(`before the move: ${box(moved)}`);+ // In the other document, the same relative href resolves to the other file, which is already loaded there.+ b.body.appendChild(moved.ownerSVGElement);+ println(`after the move: ${box(moved)} (the other document's own use: ${box(b.getElementById("resident"))})`);+ done();+ };+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/use-element-shadow-tree-after-adoption.html b/Tests/LibWeb/Text/input/SVG/use-element-shadow-tree-after-adoption.htmlnew file mode 100644index 00000000000..70505a92ea4--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-element-shadow-tree-after-adoption.html@@ -0,0 +1,27 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg width="0" height="0"><defs><rect id="leaf" width="10" height="20"/><g id="icon"><use href="#leaf"/></g></defs></svg>+<template id="template"><svg><use id="from-template" href="#icon"/></svg></template>+<div id="target"></div>+<script>+ test(() => {+ document.getElementById("target").replaceWith(document.getElementById("template").content.cloneNode(true));+ const fromTemplate = document.getElementById("from-template").getBBox();+ println(`use from template content: ${fromTemplate.width}x${fromTemplate.height}`);++ const other = document.implementation.createHTMLDocument();+ other.body.innerHTML = `<svg><use id="from-other-document" href="#icon"/></svg>`;+ const svg = other.body.firstElementChild;+ document.body.appendChild(svg);+ const fromOtherDocument = document.getElementById("from-other-document").getBBox();+ println(`use from another document: ${fromOtherDocument.width}x${fromOtherDocument.height}`);++ const host = document.createElement("div");+ document.body.appendChild(host);+ const shadowRoot = host.attachShadow({ mode: "open" });+ other.body.innerHTML = `<svg><use href="#icon"/></svg>`;+ shadowRoot.appendChild(other.body.firstElementChild);+ const inShadowTree = shadowRoot.querySelector("use").getBBox();+ println(`use from another document, in a shadow tree: ${inShadowTree.width}x${inShadowTree.height}`);+ });+</script>