Skip to content

Truly independent web browser

LibWeb: Discard stale SVG use clones when href changes

Shannon Booth33309bc0692c
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cpp
index e896a24a8e3..16a1379094e 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp
@@ -204,14 +204,17 @@ void SVGUseElement::process_the_url(Optional<Utf16String> const& href)
// a same-document URL reference, and processing the URL must continue as indicated in Identifying
// the target element with the current document as the referenced document.
m_href = document().encoding_parse_url(href.value_or({}));
- if (!m_href.has_value())
+ if (m_href.has_value() && is_referenced_element_same_document()) {
+ clone_element_tree_as_our_shadow_tree(referenced_element());
return;
+ }
- if (is_referenced_element_same_document()) {
- clone_element_tree_as_our_shadow_tree(referenced_element());
- } else {
+ // If either the ‘use’ element or the referenced element is altered in a way that causes the ‘use’ element’s URL
+ // reference to become unresolved again, then the entire shadow tree for that use element is discarded.
+ clone_element_tree_as_our_shadow_tree(nullptr);
+
+ if (m_href.has_value())
fetch_the_document(*m_href);
- }
}
bool SVGUseElement::is_referenced_element_same_document() const
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-clears-clone-for-data-url.txt b/Tests/LibWeb/Text/expected/SVG/use-element-clears-clone-for-data-url.txt
new file mode 100644
index 00000000000..82b3a5bdb9b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-clears-clone-for-data-url.txt
@@ -0,0 +1,2 @@
+before: 13
+after: 0
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-clears-clone-for-data-url.html b/Tests/LibWeb/Text/input/SVG/use-element-clears-clone-for-data-url.html
new file mode 100644
index 00000000000..00036d49e97
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-clears-clone-for-data-url.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg>
+ <defs><rect id="local" width="13" height="13"/></defs>
+ <use id="instance" href="#local"/>
+</svg>
+<script>
+ test(() => {
+ const instance = document.getElementById("instance");
+ println(`before: ${instance.getBBox().width}`);
+ const svg = '<svg xmlns="http://www.w3.org/2000/svg"><rect id="target" width="23" height="23"/></svg>';
+ instance.setAttribute("href", "data:image/svg+xml," + encodeURIComponent(svg) + "#target");
+ println(`after: ${instance.getBBox().width}`);
+ });
+</script>