Truly independent web browser
LibWeb: Rebuild `use` element shadow trees once per style update
Tim Ledbetterc03524763f0d
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cppindex bc2738e4180..414782dc3d7 100644--- a/Libraries/LibWeb/CSS/StyleComputer.cpp+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp@@ -327,7 +327,7 @@ DOM::ShadowRoot* StyleComputer::shadow_root_for_tree_scope(TreeScopeID tree_scopvoid StyleComputer::prepare_elements_for_style_computation(){for (;;) {- auto elements = m_style_engine.take_elements_awaiting_first_style_computation();+ auto elements = m_style_engine.take_elements_awaiting_preparation_for_style_computation();if (elements.is_empty())break;for (auto style_node : elements) {diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cppindex 3af2470fab0..1f4c270f488 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp@@ -103,19 +103,19 @@ void StyleEngine::defer_element_initial_features(DOM::Element& element){element.set_initial_style_features_pending(true);m_nodes_with_pending_initial_features.append(element.style_node_id());- note_element_awaiting_first_style_computation(element);+ note_element_awaiting_preparation_for_style_computation(element);}-void StyleEngine::note_element_awaiting_first_style_computation(DOM::Element& element)+void StyleEngine::note_element_awaiting_preparation_for_style_computation(DOM::Element& element){if (element.needs_preparation_for_style_computation())- m_nodes_awaiting_first_style_computation.set(element.style_node_id());+ m_nodes_awaiting_preparation_for_style_computation.set(element.style_node_id());}void StyleEngine::cancel_deferred_element_initial_features(DOM::Element& element){element.set_initial_style_features_pending(false);- m_nodes_awaiting_first_style_computation.remove(element.style_node_id());+ m_nodes_awaiting_preparation_for_style_computation.remove(element.style_node_id());}Vector<StyleNodeID> StyleEngine::take_deferred_element_initial_features()@@ -123,9 +123,9 @@ Vector<StyleNodeID> StyleEngine::take_deferred_element_initial_features()return move(m_nodes_with_pending_initial_features);}-HashTable<StyleNodeID> StyleEngine::take_elements_awaiting_first_style_computation()+HashTable<StyleNodeID> StyleEngine::take_elements_awaiting_preparation_for_style_computation(){- return move(m_nodes_awaiting_first_style_computation);+ return move(m_nodes_awaiting_preparation_for_style_computation);}void StyleEngine::set_element_parts(StyleNodeID node, ReadonlySpan<StyleAtomID> names, ReadonlySpan<StyleNodeID> hosts)diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.hindex 3c7b8d9c6a5..a0b487435eb 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h@@ -191,12 +191,12 @@ public:bool is_taking_in_pending_arrivals() const { return m_taking_in_pending_arrivals; }[[nodiscard]] TemporaryChange<bool> begin_taking_in_pending_arrivals() { return TemporaryChange<bool>(m_taking_in_pending_arrivals, true); }void defer_element_initial_features(DOM::Element&);- void note_element_awaiting_first_style_computation(DOM::Element&);+ void note_element_awaiting_preparation_for_style_computation(DOM::Element&);void cancel_deferred_element_initial_features(DOM::Element&);- // Whether any element waits to have its initial features published, or to be prepared for its first style.- [[nodiscard]] bool has_elements_awaiting_initial_style_work() const { return !m_nodes_with_pending_initial_features.is_empty() || !m_nodes_awaiting_first_style_computation.is_empty(); }+ // Whether any element waits to have its initial features published, or to be prepared for style computation.+ [[nodiscard]] bool has_elements_awaiting_initial_style_work() const { return !m_nodes_with_pending_initial_features.is_empty() || !m_nodes_awaiting_preparation_for_style_computation.is_empty(); }Vector<StyleNodeID> take_deferred_element_initial_features();- HashTable<StyleNodeID> take_elements_awaiting_first_style_computation();+ HashTable<StyleNodeID> take_elements_awaiting_preparation_for_style_computation();void set_element_parts(StyleNodeID node, ReadonlySpan<StyleAtomID> names, ReadonlySpan<StyleNodeID> hosts);// Which longhand properties one of an element's own declarations covers, their canonical@@ -477,7 +477,7 @@ private:// longer holds its mark, DOM::Element::initial_style_features_pending().Vector<StyleNodeID> m_nodes_with_pending_initial_features;bool m_taking_in_pending_arrivals { false };- HashTable<StyleNodeID> m_nodes_awaiting_first_style_computation;+ HashTable<StyleNodeID> m_nodes_awaiting_preparation_for_style_computation;HashTable<StyleNodeID> m_parents_whose_children_changed_beside_flown_transaction;size_t m_pending_arrival_count { 0 };diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cppindex ea7f2ecd925..27746db11c0 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp@@ -978,7 +978,7 @@ static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<Pendin.relations = relations_of(*element, style_engine, arrival.tree_scope),};if (publication && element->is_connected()) {- style_engine.note_element_awaiting_first_style_computation(*element);+ style_engine.note_element_awaiting_preparation_for_style_computation(*element);publish_element_arrival(style_engine, *element, *publication, arriving);} else {arriving_elements.append(arriving);diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.hindex 2cafd353e63..14ff0f72c8d 100644--- a/Libraries/LibWeb/DOM/Node.h+++ b/Libraries/LibWeb/DOM/Node.h@@ -133,6 +133,7 @@ enum class SetNeedsLayoutReason {X(SVGResourceContentChange) \X(SVGResourceElementRemoved) \X(SVGResourceReferenceChange) \+ X(SVGUseElementShadowTreeRebuild) \X(TopLayerMembershipChange) \X(WhitespaceTextBoxChange) \X(WritingSuggestionChange)diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cppindex f3f34ce0e41..52d85494ed0 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp@@ -8,6 +8,7 @@#include <LibGC/Heap.h>#include <LibGC/HeapHashTable.h>#include <LibWeb/CSS/ElementBoxKind.h>+#include <LibWeb/CSS/StyleComputer.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/DocumentLoadEventDelayer.h>#include <LibWeb/DOM/ElementFactory.h>@@ -231,15 +232,32 @@ bool SVGUseElement::is_referenced_element_same_document() constvoid SVGUseElement::svg_element_changed(SVGElement& svg_element){+ if (m_reclone_pending)+ return;+auto to_clone = referenced_element();if (!to_clone) {return;}// NOTE: We need to check the ancestor because attribute_changed of a child doesn't call children_changed on the parent(s)- if (to_clone == GC::Ref { svg_element } || to_clone->is_ancestor_of(svg_element)) {- clone_element_tree_as_our_shadow_tree(to_clone);- }+ if (to_clone == GC::Ref { svg_element } || to_clone->is_ancestor_of(svg_element))+ request_reclone();+}++void SVGUseElement::request_reclone()+{+ m_reclone_pending = true;+ if (!is_connected())+ return;+ document().style_computer().style_engine().note_element_awaiting_preparation_for_style_computation(*this);+ set_needs_layout_tree_update(true, DOM::SetNeedsLayoutTreeUpdateReason::SVGUseElementShadowTreeRebuild);+}++void SVGUseElement::prepare_for_style_computation()+{+ if (m_reclone_pending)+ clone_element_tree_as_our_shadow_tree(referenced_element());}void SVGUseElement::svg_element_changed_before_parsing_complete(SVGElement& svg_element)@@ -357,6 +375,7 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)// https://svgwg.org/svg2-draft/struct.html#UseShadowTreevoid SVGUseElement::clone_element_tree_as_our_shadow_tree(GC::Ptr<Element> to_clone){+ m_reclone_pending = false;shadow_root()->remove_all_children();// https://svgwg.org/svg2-draft/struct.html#UseStyleInheritancediff --git a/Libraries/LibWeb/SVG/SVGUseElement.h b/Libraries/LibWeb/SVG/SVGUseElement.hindex 1d748e0942d..6d8367f3347 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.h+++ b/Libraries/LibWeb/SVG/SVGUseElement.h@@ -63,6 +63,9 @@ private:virtual CSS::ElementBoxKind box_kind() const override;+ virtual bool needs_preparation_for_style_computation() const override { return m_reclone_pending; }+ virtual void prepare_for_style_computation() override;+void process_the_url(Optional<Utf16String> const& href);Optional<Utf16String> href_value() const;DOM::Document const& document_containing_reference() const;@@ -74,6 +77,7 @@ private:void stop_using_resource_request();bool is_referenced_element_same_document() const;+ 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);bool is_valid_reference_element(Element const& reference_element) const;@@ -83,6 +87,7 @@ private:void unregister_for_referenced_element_changes();bool m_needs_parsing_complete_reclone { false };+ bool m_reclone_pending { false };Optional<URL::URL> m_href;diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-bbox-after-referenced-id-appears.txt b/Tests/LibWeb/Text/expected/SVG/use-element-bbox-after-referenced-id-appears.txtnew file mode 100644index 00000000000..f62778fba1e--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-element-bbox-after-referenced-id-appears.txt@@ -0,0 +1,2 @@+Use bounding box width before its referenced id exists: 0+Use bounding box width once its referenced id exists: 40diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-moveBefore-registry.txt b/Tests/LibWeb/Text/expected/SVG/use-element-moveBefore-registry.txtindex d79f5c05946..39aa957e387 100644--- a/Tests/LibWeb/Text/expected/SVG/use-element-moveBefore-registry.txt+++ b/Tests/LibWeb/Text/expected/SVG/use-element-moveBefore-registry.txt@@ -1,2 +1,2 @@-Use moved into shadow tree kept old fill: red-Use moved into document tree updated fill: green+Use moved into shadow tree, width once its target exists: 0+Use moved into document tree, width once its target exists: 10diff --git a/Tests/LibWeb/Text/input/SVG/use-element-bbox-after-referenced-id-appears.html b/Tests/LibWeb/Text/input/SVG/use-element-bbox-after-referenced-id-appears.htmlnew file mode 100644index 00000000000..fbe0720b3b3--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-element-bbox-after-referenced-id-appears.html@@ -0,0 +1,16 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg>+ <g id="group"><rect width="40" height="10"/></g>+ <use id="use" href="#target"/>+</svg>+<script>+ test(() => {+ const use = document.getElementById("use");+ const widthBefore = use.getBBox().width;+ document.getElementById("group").id = "target";+ const widthAfter = use.getBBox().width;+ println(`Use bounding box width before its referenced id exists: ${widthBefore}`);+ println(`Use bounding box width once its referenced id exists: ${widthAfter}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/use-element-inserted-together-with-referenced-element.html b/Tests/LibWeb/Text/input/SVG/use-element-inserted-together-with-referenced-element.htmlindex a2a59141f6a..dac90077c90 100644--- a/Tests/LibWeb/Text/input/SVG/use-element-inserted-together-with-referenced-element.html+++ b/Tests/LibWeb/Text/input/SVG/use-element-inserted-together-with-referenced-element.html@@ -35,6 +35,7 @@({ svg, use } = makeSubtree("useBeforeTarget", "use-first"));document.body.appendChild(svg);+ internals.updateStyle();println(`Use before referenced element: has instance root: ${!!internals.getShadowRoot(use).firstElementChild}`);done();diff --git a/Tests/LibWeb/Text/input/SVG/use-element-moveBefore-registry.html b/Tests/LibWeb/Text/input/SVG/use-element-moveBefore-registry.htmlindex 00fb4a63095..d892d71206f 100644--- a/Tests/LibWeb/Text/input/SVG/use-element-moveBefore-registry.html+++ b/Tests/LibWeb/Text/input/SVG/use-element-moveBefore-registry.html@@ -8,49 +8,44 @@return document.createElementNS(SVG_NS, localName);}- function createReferencedUse(id, fill) {- const svg = createSVGElement("svg");- svg.style.display = "none";-- const source = createSVGElement("rect");- source.id = id;- source.setAttribute("width", "10");- source.setAttribute("height", "10");- source.setAttribute("fill", fill);-- const use = createSVGElement("use");- use.setAttribute("href", `#${id}`);-- svg.append(source, use);- document.body.appendChild(svg);- return { source, svg, use };+ function createReferencedRect(id) {+ const rect = createSVGElement("rect");+ rect.id = id;+ rect.setAttribute("width", "10");+ rect.setAttribute("height", "10");+ return rect;}window.addEventListener("load", () => {setTimeout(() => {+ const svg = createSVGElement("svg");+ document.body.appendChild(svg);+ const host = document.createElement("div");+ document.body.appendChild(host);+ host.attachShadow({ mode: "open" });+ const shadowSvg = createSVGElement("svg");+ host.shadowRoot.appendChild(shadowSvg);+{- const { source, use } = createReferencedUse("moveOutSource", "red");- const host = document.createElement("div");- document.body.appendChild(host);- host.attachShadow({ mode: "open" });+ const use = createSVGElement("use");+ use.setAttribute("href", "#moveOutSource");+ svg.appendChild(use);- host.shadowRoot.moveBefore(use, null);- source.setAttribute("fill", "green");+ shadowSvg.moveBefore(use, null);+ svg.appendChild(createReferencedRect("moveOutSource"));- println(`Use moved into shadow tree kept old fill: ${internals.getShadowRoot(use).firstElementChild.getAttribute("fill")}`);+ println(`Use moved into shadow tree, width once its target exists: ${use.getBBox().width}`);}{- const { source, svg, use } = createReferencedUse("moveInSource", "red");- const host = document.createElement("div");- document.body.appendChild(host);- host.attachShadow({ mode: "open" });+ const use = createSVGElement("use");+ use.setAttribute("href", "#moveInSource");+ shadowSvg.appendChild(use);- host.shadowRoot.appendChild(use);svg.moveBefore(use, null);- source.setAttribute("fill", "green");+ svg.appendChild(createReferencedRect("moveInSource"));- println(`Use moved into document tree updated fill: ${internals.getShadowRoot(use).firstElementChild.getAttribute("fill")}`);+ println(`Use moved into document tree, width once its target exists: ${use.getBBox().width}`);}done();