Truly independent web browser
LibWeb: Update `use` elements on changes inside the referenced element
Tim Ledbetter25bd7f1c06fa
diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cppindex a88bbb88a23..92a0871248e 100644--- a/Libraries/LibWeb/SVG/SVGElement.cpp+++ b/Libraries/LibWeb/SVG/SVGElement.cpp@@ -8,6 +8,7 @@#include <AK/HashMap.h>#include <AK/NeverDestroyed.h>+#include <LibGC/WeakHashSet.h>#include <LibWeb/CSS/ElementBoxKind.h>#include <LibWeb/CSS/PropertyID.h>#include <LibWeb/CSS/StyleEngineInput.h>@@ -47,7 +48,10 @@ struct SVGElement::RareData final : DOM::Element::RareData {visitor.visit(corresponding_element);}- GC::Ptr<SVGElement const> corresponding_element;+ GC::Ptr<SVGElement> corresponding_element;++ // https://svgwg.org/svg2-draft/struct.html#TermElementInstance+ GC::WeakHashSet<SVGElement> element_instances;};OwnPtr<DOM::Node::RareData> SVGElement::create_rare_data() const@@ -64,9 +68,10 @@ GC::Ptr<SVGElement const> SVGElement::corresponding_element() constreturn data->corresponding_element;}-void SVGElement::set_corresponding_element(Badge<SVGUseElement>, SVGElement const& corresponding_element)+void SVGElement::set_corresponding_element(Badge<SVGUseElement>, SVGElement& corresponding_element){static_cast<RareData&>(ensure_rare_data()).corresponding_element = corresponding_element;+ static_cast<RareData&>(corresponding_element.ensure_rare_data()).element_instances.set(*this);}SVGElement::SVGElement(DOM::Document& document, DOM::QualifiedName qualified_name)@@ -467,9 +472,11 @@ void SVGElement::attribute_changed(Utf16FlyString const& local_name, Optional<UtBase::attribute_changed(local_name, old_value, value, namespace_);HTMLOrSVGOrMathMLElement::attribute_changed(local_name, old_value, value, namespace_);- if (old_value != value)+ if (old_value != value) {update_presentation_attribute_style(local_name, value, namespace_);- update_use_elements_that_reference_this();+ invalidate_element_instances(ElementInstancesToInvalidate::All);+ update_use_elements_that_reference_this();+ }note_svg_paint_resource_description_may_have_changed();}@@ -538,6 +545,7 @@ void SVGElement::children_changed(ChildrenChangedMetadata const& metadata){Base::children_changed(metadata);+ invalidate_element_instances(ElementInstancesToInvalidate::All);update_use_elements_that_reference_this();note_svg_paint_resource_description_may_have_changed();}@@ -564,6 +572,27 @@ void SVGElement::update_use_elements_that_reference_this()}}+void SVGElement::invalidate_element_instances(ElementInstancesToInvalidate element_instances_to_invalidate)+{+ auto* data = static_cast<RareData*>(rare_data());+ if (!data)+ return;++ GC::RootVector<GC::Ref<SVGElement>> invalidated_element_instances;+ for (auto& element_instance : data->element_instances) {+ if (element_instances_to_invalidate == ElementInstancesToInvalidate::All || element_instance.shadow_including_root().is_document())+ invalidated_element_instances.append(element_instance);+ }++ for (auto& element_instance : invalidated_element_instances) {+ data->element_instances.remove(element_instance);+ static_cast<RareData&>(*element_instance->rare_data()).corresponding_element = nullptr;+ auto* shadow_root = as_if<DOM::ShadowRoot>(element_instance->root());+ if (auto* use_element = shadow_root ? as_if<SVGUseElement>(shadow_root->host()) : nullptr)+ use_element->element_instance_changed({});+ }+}+void SVGElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root){Base::removed_from(is_subtree_root, old_ancestor, old_root);@@ -574,6 +603,11 @@ void SVGElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor,return shadow_root && shadow_root->host() && is<SVGUseElement>(*shadow_root->host());};+ if (auto* data = static_cast<RareData*>(rare_data()); data && data->corresponding_element && !is_use_element_shadow_root(root())) {+ static_cast<RareData&>(*data->corresponding_element->rare_data()).element_instances.remove(*this);+ data->corresponding_element = nullptr;+ }+// If this element is in a shadow root hosted by a use element,// it already represents a clone and is not itself referenced.// NB: We check both old_root (for when the element is removed from the shadow tree directly)@@ -582,6 +616,7 @@ void SVGElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor,return;remove_from_use_element_that_reference_this();+ invalidate_element_instances(ElementInstancesToInvalidate::InDocument);// A <mask>, <clipPath>, or <pattern> referenced via url(#id) is laid out as a resource box attached to the// referencing element's layout subtree. So it outlives this element's own DOM node. Rebuild those referencingdiff --git a/Libraries/LibWeb/SVG/SVGElement.h b/Libraries/LibWeb/SVG/SVGElement.hindex 37155ede8c4..908212ecec8 100644--- a/Libraries/LibWeb/SVG/SVGElement.h+++ b/Libraries/LibWeb/SVG/SVGElement.h@@ -43,7 +43,7 @@ public:GC::Ref<SVGAnimatedString> class_name();GC::Ptr<SVGElement const> corresponding_element() const;- void set_corresponding_element(Badge<SVGUseElement>, SVGElement const&);+ void set_corresponding_element(Badge<SVGUseElement>, SVGElement&);GC::Ptr<SVGSVGElement> owner_svg_element();GC::Ptr<SVGSVGElement> outermost_svg_element();GC::Ptr<SVGElement> viewport_element();@@ -105,6 +105,12 @@ private:void update_presentation_attribute_style(Utf16FlyString const& name, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_);void publish_presentation_attribute_style();+ enum class ElementInstancesToInvalidate {+ All,+ InDocument,+ };+ void invalidate_element_instances(ElementInstancesToInvalidate);+GC::Ptr<SVGAnimatedString> m_class_name_animated_string;mutable Optional<Vector<CSS::StyleProperty>> m_presentation_attribute_style;diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cppindex 52d85494ed0..11da6d16fee 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp@@ -92,10 +92,15 @@ void SVGUseElement::inserted()// 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. 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())+ // may have finished loading while we were disconnected. If our shadow tree has not been populated yet, or was+ // copied from an element our reference no longer resolves to, try resolving the reference again.+ if (!is_connected())+ return;+ if (auto instance_root = this->instance_root()) {+ if (instance_root->corresponding_element().ptr() != referenced_element().ptr())+ request_reclone();return;+ }if (!m_href.has_value()) {process_the_url(href_value());@@ -245,6 +250,17 @@ void SVGUseElement::svg_element_changed(SVGElement& svg_element)request_reclone();}+void SVGUseElement::element_instance_changed(Badge<SVGElement>)+{+ if (is_connected() && document().readiness() == HTML::DocumentReadyState::Loading) {+ m_needs_parsing_complete_reclone = true;+ return;+ }++ if (!m_reclone_pending)+ request_reclone();+}+void SVGUseElement::request_reclone(){m_reclone_pending = true;@@ -411,14 +427,14 @@ void SVGUseElement::clone_element_tree_as_our_shadow_tree(GC::Ptr<Element> to_cl}// https://svgwg.org/svg2-draft/struct.html#TermCorrespondingElement-void SVGUseElement::link_to_corresponding_elements(SVGElement& instance, SVGElement const& original)+void SVGUseElement::link_to_corresponding_elements(SVGElement& instance, SVGElement& original){instance.set_corresponding_element({}, original);// NB: Only SVG children are paired up. Inserting a clone's HTML content can change it, for example when an option// element updates a selectedcontent element.auto* instance_child = instance.first_child_of_type<SVGElement>();- for (auto const* original_child = original.first_child_of_type<SVGElement>(); original_child && instance_child; original_child = original_child->next_sibling_of_type<SVGElement>()) {+ for (auto* original_child = original.first_child_of_type<SVGElement>(); original_child && instance_child; original_child = original_child->next_sibling_of_type<SVGElement>()) {link_to_corresponding_elements(*instance_child, *original_child);instance_child = instance_child->next_sibling_of_type<SVGElement>();}diff --git a/Libraries/LibWeb/SVG/SVGUseElement.h b/Libraries/LibWeb/SVG/SVGUseElement.hindex 6d8367f3347..2d99ce83192 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.h+++ b/Libraries/LibWeb/SVG/SVGUseElement.h@@ -29,6 +29,7 @@ public:void svg_element_changed(SVGElement&);void svg_element_changed_before_parsing_complete(SVGElement&);void svg_element_removed(SVGElement&);+ void element_instance_changed(Badge<SVGElement>);// AD-HOC: The spec states that the x, y, width and height IDL attributes reflect the respective computed values and their// corresponding presentation attributes but other browsers reflect the attribute values instead - see@@ -79,7 +80,7 @@ private:void request_reclone();void clone_element_tree_as_our_shadow_tree(GC::Ptr<Element> to_clone);- static void link_to_corresponding_elements(SVGElement& instance, SVGElement const& original);+ static void link_to_corresponding_elements(SVGElement& instance, SVGElement& original);bool is_valid_reference_element(Element const& reference_element) const;bool would_create_circular_reference(Element const& target) const;bool would_create_circular_reference_impl(Element const& target, GC::HeapHashTable<GC::Ref<Element const>>& visited) const;diff --git a/Tests/LibWeb/Ref/expected/wpt-import/svg/struct/reftests/sync-svg-attributes-ref.svg b/Tests/LibWeb/Ref/expected/wpt-import/svg/struct/reftests/sync-svg-attributes-ref.svgnew file mode 100644index 00000000000..f68db2a3065--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/svg/struct/reftests/sync-svg-attributes-ref.svg@@ -0,0 +1,4 @@+<svg xmlns="http://www.w3.org/2000/svg">+ <text y="20">The two texts should be on top of each other</text>+ <text y="20" fill="blue">The two texts should be on top of each other</text>+</svg>diff --git a/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/sync-svg-attributes.svg b/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/sync-svg-attributes.svgnew file mode 100644index 00000000000..2fc2f771c10--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/sync-svg-attributes.svg@@ -0,0 +1,17 @@+<svg xmlns="http://www.w3.org/2000/svg" xmlns:h="http://www.w3.org/1999/xhtml"+ class="reftest-wait">+ <metadata>+ <title>SVG attributes gets synced properly on use shadow dom</title>+ <h:link rel="match" href="../../../../../expected/wpt-import/svg/struct/reftests/sync-svg-attributes-ref.svg"/>+ </metadata>+ <g id="g">+ <text y="100">The two texts should be on top of each other</text>+ </g>+ <use href="#g" fill="blue"/>+ <script>+ requestAnimationFrame(() => requestAnimationFrame(() => {+ document.querySelector('#g > text').setAttribute('y', '20')+ document.documentElement.classList.remove('reftest-wait');+ }));+ </script>+</svg>diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-changes-to-cloned-elements.txt b/Tests/LibWeb/Text/expected/SVG/use-element-changes-to-cloned-elements.txtnew file mode 100644index 00000000000..c11ce3f90f6--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-element-changes-to-cloned-elements.txt@@ -0,0 +1,8 @@+Use of a group holding a use, after the inner target's path changed: 60+Use whose referenced element lost its id: 0+Use re-inserted after its target changed while it was removed: 70+Same use after a later change: 75+Use of a group holding a use, re-inserted after the inner target changed while it was removed: 60+Same use after a later change: 70+Use of a group holding a use, after the inner target was removed: 0+Use re-inserted after its target was removed while the use was detached: 0diff --git a/Tests/LibWeb/Text/input/SVG/use-element-changes-to-cloned-elements.html b/Tests/LibWeb/Text/input/SVG/use-element-changes-to-cloned-elements.htmlnew file mode 100644index 00000000000..32885d9eb4a--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-element-changes-to-cloned-elements.html@@ -0,0 +1,62 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg>+ <defs>+ <g id="inner"><path d="M0,0 L10,10"/></g>+ <g id="outer"><use href="#inner"/></g>+ <g id="renamed"><path d="M0,0 L10,10"/></g>+ <g id="detached"><path d="M0,0 L10,10"/></g>+ <g id="nested-detached-inner"><path d="M0,0 L10,10"/></g>+ <g id="nested-detached-outer"><use href="#nested-detached-inner"/></g>+ <g id="nested-removed-inner"><path d="M0,0 L20,20"/></g>+ <g id="nested-removed-outer"><use href="#nested-removed-inner"/></g>+ <g id="removed-while-detached"><path d="M0,0 L10,10"/></g>+ </defs>+ <use id="use-outer" href="#outer"/>+ <use id="use-renamed" href="#renamed"/>+ <use id="use-detached" href="#detached"/>+ <use id="use-nested-detached" href="#nested-detached-outer"/>+ <use id="use-nested-removed" href="#nested-removed-outer"/>+ <use id="use-removed-while-detached" href="#removed-while-detached"/>+</svg>+<script>+ test(() => {+ const lines = [];+ const width = id => document.getElementById(id).getBBox().width;++ document.querySelector("#inner path").setAttribute("d", "M0,0 L60,60");+ lines.push(`Use of a group holding a use, after the inner target's path changed: ${width("use-outer")}`);++ document.getElementById("renamed").removeAttribute("id");+ lines.push(`Use whose referenced element lost its id: ${width("use-renamed")}`);++ const use = document.getElementById("use-detached");+ const svg = use.parentNode;+ use.remove();+ document.querySelector("#detached path").setAttribute("d", "M0,0 L70,70");+ svg.append(use);+ lines.push(`Use re-inserted after its target changed while it was removed: ${width("use-detached")}`);+ document.querySelector("#detached path").setAttribute("d", "M0,0 L75,75");+ lines.push(`Same use after a later change: ${width("use-detached")}`);++ const nestedUse = document.getElementById("use-nested-detached");+ nestedUse.remove();+ document.querySelector("#nested-detached-inner path").setAttribute("d", "M0,0 L60,60");+ svg.append(nestedUse);+ lines.push(`Use of a group holding a use, re-inserted after the inner target changed while it was removed: ${width("use-nested-detached")}`);+ document.querySelector("#nested-detached-inner path").setAttribute("d", "M0,0 L70,70");+ lines.push(`Same use after a later change: ${width("use-nested-detached")}`);++ document.getElementById("nested-removed-inner").remove();+ lines.push(`Use of a group holding a use, after the inner target was removed: ${width("use-nested-removed")}`);++ const useOfRemovedTarget = document.getElementById("use-removed-while-detached");+ useOfRemovedTarget.remove();+ document.getElementById("removed-while-detached").remove();+ svg.append(useOfRemovedTarget);+ lines.push(`Use re-inserted after its target was removed while the use was detached: ${width("use-removed-while-detached")}`);++ for (const line of lines)+ println(line);+ });+</script>