Truly independent web browser
LibWeb: Keep hidden SVG style only for elements that can be referenced
Tim Ledbetterbfb95b5d06f2
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cppindex d0408ed0ea6..d8a18b9899b 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp@@ -841,6 +841,8 @@ enum ElementConstructionFact : u32 {IsEditingHost = 1 << 4,IsBody = 1 << 5,ConstructedAsDocumentElement = 1 << 6,+ IsSvgResourceElement = 1 << 7,+ IsSvgGeometryElement = 1 << 8,};u32 element_box_type_adjustment_facts(DOM::Element const& element){@@ -947,6 +949,7 @@ static u32 element_construction_facts(DOM::Element const& element)};auto shadow_root = element.containing_shadow_root();auto const* html_element = as_if<HTML::HTMLElement>(element);+ auto const* svg_element = as_if<SVG::SVGElement>(element);set(is<HTML::HTMLInputElement>(element), ElementConstructionFact::IsHtmlInputElement);set(element.is_html_html_element(), ElementConstructionFact::ConstructedAsHtmlHtmlElement);set(shadow_root && shadow_root->is_user_agent_internal(), ElementConstructionFact::IsInUserAgentShadowTree);@@ -954,6 +957,8 @@ static u32 element_construction_facts(DOM::Element const& element)set(element.is_editing_host(), ElementConstructionFact::IsEditingHost);set(&element == element.document().body(), ElementConstructionFact::IsBody);set(element.is_document_element(), ElementConstructionFact::ConstructedAsDocumentElement);+ set(svg_element && svg_element->is_resource_element(), ElementConstructionFact::IsSvgResourceElement);+ set(element.is_svg_geometry_element(), ElementConstructionFact::IsSvgGeometryElement);return facts;}@@ -2604,6 +2609,9 @@ void record_element_id_changed(DOM::Element& element, Optional<Utf16FlyString> c// `getElementById` is case-sensitive in every mode, so the name the inverse index is keyed by// is the one written rather than the one a quirks-mode selector folds it to.StyleEngineFFI::style_engine_set_element_id_name(style_engine->host(), element.style_node_id(), new_value.has_value() ? style_engine->intern_atom(*new_value) : StyleAtomID {});++ if (element.is_svg_geometry_element())+ style_engine->record_derived_element_style_input_change(element.style_node_id(), StyleEngine::PublishedStyle | StyleEngine::RecomputeStyle);}void record_element_class_list_changed(DOM::Element& element, ReadonlySpan<Utf16FlyString> old_classes, ReadonlySpan<Utf16FlyString> new_classes)diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 5615d21653c..df9b70e1af4 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -2474,7 +2474,7 @@ void Element::clear_computed_styles_from_display_none_descendants(Layout::BegunR// Restore their styles in this style update, so painting never needs to materialize// styles for referenced resources. The engine still holds their records and settles// the reaction from them.- if (element->is_svg_element())+ if (auto const* svg_element = as_if<SVG::SVGElement>(*element); svg_element && svg_element->is_referenceable_in_display_none_subtree())element->document().style_computer().style_engine().record_derived_element_style_input_change(element->style_node_id(), CSS::StyleEngine::PublishedStyle | CSS::StyleEngine::RecomputeStyle);element->for_each_synthetic_pseudo_element([&](CSS::PseudoElement, SyntheticPseudoElement& pseudo_element) {diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.hindex 0aef97d0a21..51d4958c2f8 100644--- a/Libraries/LibWeb/DOM/Node.h+++ b/Libraries/LibWeb/DOM/Node.h@@ -204,6 +204,8 @@ public:virtual bool is_svg_text_positioning_element() const { return false; }virtual bool is_svg_text_element() const { return false; }virtual bool is_svg_text_path_element() const { return false; }+ virtual bool is_svg_geometry_element() const { return false; }+ virtual bool is_svg_filter_element() const { return false; }bool in_a_document_tree() const;diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex 33e3e48ffd8..67a9940279c 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -726,6 +726,8 @@ pub mod element_construction_fact {pub const IS_EDITING_HOST: u32 = 1 << 4;pub const IS_BODY: u32 = 1 << 5;pub const IS_DOCUMENT_ELEMENT: u32 = 1 << 6;+ pub const IS_SVG_RESOURCE_ELEMENT: u32 = 1 << 7;+ pub const IS_SVG_GEOMETRY_ELEMENT: u32 = 1 << 8;}/// Which principal box an element asks for before its computed style has a say. The element's owndiff --git a/Libraries/LibWeb/Rust/src/css/style/flush.rs b/Libraries/LibWeb/Rust/src/css/style/flush.rsindex 418880949ec..c7759fb39d4 100644--- a/Libraries/LibWeb/Rust/src/css/style/flush.rs+++ b/Libraries/LibWeb/Rust/src/css/style/flush.rs@@ -256,6 +256,29 @@ impl RetainedState {false}+ fn is_referenceable_in_display_none_subtree(&self, node: StyleNodeID) -> bool {+ use bridge::element_adjustment_fact::IS_SVG_ELEMENT;+ use bridge::element_construction_fact::{IS_SVG_GEOMETRY_ELEMENT, IS_SVG_RESOURCE_ELEMENT};+ if self.computed_group_sets.construction_facts(node) & IS_SVG_GEOMETRY_ELEMENT != 0+ && !self.facts.id_of_node(node).is_none()+ {+ return true;+ }+ let mut current = Some(node);+ while let Some(element) = current+ && self.computed_group_sets.adjustment_facts(element) & IS_SVG_ELEMENT != 0+ {+ if self.computed_group_sets.construction_facts(element) & IS_SVG_RESOURCE_ELEMENT != 0 {+ return true;+ }+ current = self+ .tree+ .parent(element)+ .map(|parent| self.tree.host_of(parent).unwrap_or(parent));+ }+ false+ }+/// Note what the record loop reads of the answer a transaction publishes for the node before that/// answer is installed: its pseudo-element inventory, kept with the node's record columns for as/// long as that answer stands, the answer's completeness and the matches its custom-property@@ -2138,13 +2161,14 @@ impl StyleEngine {for &node in &published_nodes {self.retained.note_batch_answer_facts(node, &published_match_answers);}- // The rows that read style while hidden, an SVG element (which an SVG resource reference- // paints) and an element with animations, and every element they inherit from: none of- // them is answered as hidden.+ // The rows that read style while hidden, an SVG element that can be referenced (which+ // an SVG resource reference paints) and an element with animations, and every element+ // they inherit from: none of them is answered as hidden.let mut hidden_style_readers = HashSet::<StyleNodeID>::default();for &node in &published_nodes {use bridge::element_adjustment_fact as fact;- if self.computed_group_sets.adjustment_facts(node) & (fact::IS_SVG_ELEMENT | fact::HAS_ANIMATIONS) == 0+ if self.computed_group_sets.adjustment_facts(node) & fact::HAS_ANIMATIONS == 0+ && !self.is_referenceable_in_display_none_subtree(node){continue;}diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cppindex da21668c8f9..152ec411e0a 100644--- a/Libraries/LibWeb/SVG/SVGElement.cpp+++ b/Libraries/LibWeb/SVG/SVGElement.cpp@@ -399,6 +399,22 @@ bool SVGElement::describes_svg_paint_resource() constreturn false;}+bool SVGElement::is_resource_element() const+{+ return is_svg_gradient_element() || is_svg_pattern_element() || is_svg_clip_path_element() || is_svg_mask_element() || is_svg_filter_element();+}++bool SVGElement::is_referenceable_in_display_none_subtree() const+{+ if (is_svg_geometry_element() && id().has_value())+ return true;+ for (auto const* element = this; element; element = as_if<SVGElement>(element->parent_or_shadow_host_element())) {+ if (element->is_resource_element())+ return true;+ }+ return false;+}+void SVGElement::note_svg_paint_resource_description_may_have_changed(){if (describes_svg_paint_resource())diff --git a/Libraries/LibWeb/SVG/SVGElement.h b/Libraries/LibWeb/SVG/SVGElement.hindex 0d23e7ed1a3..893f7bcb526 100644--- a/Libraries/LibWeb/SVG/SVGElement.h+++ b/Libraries/LibWeb/SVG/SVGElement.h@@ -48,6 +48,9 @@ public:// layout pass reads them.void publish_svg_attribute_facts();+ bool is_resource_element() const;+ bool is_referenceable_in_display_none_subtree() const;+GC::Ref<SVGAnimatedLength> svg_animated_length_for_attribute(Utf16FlyString const&, SVGLength::Directionality, SVGLengthValue default_value);virtual bool is_presentational_hint(Utf16FlyString const&) const final override;diff --git a/Libraries/LibWeb/SVG/SVGFilterElement.h b/Libraries/LibWeb/SVG/SVGFilterElement.hindex ef8257b8020..291811f9ea0 100644--- a/Libraries/LibWeb/SVG/SVGFilterElement.h+++ b/Libraries/LibWeb/SVG/SVGFilterElement.h@@ -25,6 +25,8 @@ class SVGFilterElement finalpublic:virtual ~SVGFilterElement() override = default;+ virtual bool is_svg_filter_element() const final { return true; }+virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;// Hands every supported child primitive to the Rust primitive list behind `sink` as attribute@@ -55,3 +57,10 @@ private:};}++namespace Web::DOM {++template<>+inline bool Node::fast_is<SVG::SVGFilterElement>() const { return is_svg_filter_element(); }++}diff --git a/Libraries/LibWeb/SVG/SVGGeometryElement.h b/Libraries/LibWeb/SVG/SVGGeometryElement.hindex 80dbec8c4f2..e657206e023 100644--- a/Libraries/LibWeb/SVG/SVGGeometryElement.h+++ b/Libraries/LibWeb/SVG/SVGGeometryElement.h@@ -16,6 +16,8 @@ class SVGGeometryElement : public SVGGraphicsElement {WEB_WRAPPABLE(SVGGeometryElement, SVGGraphicsElement);public:+ virtual bool is_svg_geometry_element() const final { return true; }+virtual CSS::ElementBoxKind box_kind() const override;virtual Gfx::Path get_path(CSSPixelSize viewport_size, CSS::ComputedValues const&) = 0;@@ -34,3 +36,10 @@ private:};}++namespace Web::DOM {++template<>+inline bool Node::fast_is<SVG::SVGGeometryElement>() const { return is_svg_geometry_element(); }++}diff --git a/Tests/LibWeb/Ref/input/svg/mask-use-in-display-none-subtree.html b/Tests/LibWeb/Ref/input/svg/mask-use-in-display-none-subtree.htmlnew file mode 100644index 00000000000..a4a81b1c00a--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg/mask-use-in-display-none-subtree.html@@ -0,0 +1,11 @@+<!DOCTYPE html>+<link rel="match" href="../../expected/svg/green-square-100-ref.html" />+<div style="display: none">+ <svg>+ <g id="shape"><rect width="100" height="100" fill="white" /></g>+ <mask id="m"><use href="#shape" /></mask>+ </svg>+</div>+<svg width="100" height="100">+ <rect width="100" height="100" fill="green" mask="url(#m)" />+</svg>diff --git a/Tests/LibWeb/Text/expected/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.txt b/Tests/LibWeb/Text/expected/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.txtnew file mode 100644index 00000000000..3ae01f677ee--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.txt@@ -0,0 +1,9 @@+hidden icon has no style records: true+hidden linearGradient and its content have style records: true+hidden pattern and its content have style records: true+hidden clipPath and its content have style records: true+hidden mask and its content have style records: true+hidden filter and its content have style records: true+text on the hidden path is laid out: true+hidden path given an id later has a style record: true+icon hidden later has no style records: truediff --git a/Tests/LibWeb/Text/input/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.html b/Tests/LibWeb/Text/input/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.htmlnew file mode 100644index 00000000000..835e1ad4870--- /dev/null+++ b/Tests/LibWeb/Text/input/css/style-engine/hidden-svg-style-records-only-for-referenceable-elements.html@@ -0,0 +1,42 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<div id="hidden" style="display: none"></div>+<div style="display: none"><svg><path id="hiddenPath" d="M 0 20 L 200 20" /></svg></div>+<svg width="200" height="40"><text id="textOnHiddenPath"><textPath href="#hiddenPath">Text</textPath></text></svg>+<div id="hiddenLater"></div>+<script>+ test(() => {+ const hasRecord = element => internals.styleRecordIdentity(element) !== 0;+ document.body.offsetWidth;++ hidden.innerHTML = `<svg id="icon"><g id="group"><path d="M 0 0 L 24 24" /></g></svg>`;+ internals.updateStyle();+ println(`hidden icon has no style records: ${!hasRecord(icon) && !hasRecord(group) && !hasRecord(group.firstElementChild)}`);++ hidden.innerHTML = `<svg><defs>+ <linearGradient id="gradient"><stop /></linearGradient>+ <pattern id="pattern"><rect /></pattern>+ <clipPath id="clip"><rect /></clipPath>+ <mask id="mask"><rect /></mask>+ <filter id="filter"><feFlood /></filter>+ </defs></svg>`;+ internals.updateStyle();+ for (const resource of [gradient, pattern, clip, mask, filter])+ println(`hidden ${resource.localName} and its content have style records: ${hasRecord(resource) && hasRecord(resource.firstElementChild)}`);++ println(`text on the hidden path is laid out: ${textOnHiddenPath.getBBox().width > 0}`);++ hidden.innerHTML = `<svg><path d="M 0 20 L 200 20" /></svg>`;+ internals.updateStyle();+ const pathGivenIdLater = hidden.querySelector("path");+ pathGivenIdLater.id = "pathGivenIdLater";+ internals.updateStyle();+ println(`hidden path given an id later has a style record: ${hasRecord(pathGivenIdLater)}`);++ hiddenLater.innerHTML = `<svg id="iconHiddenLater"><path d="M 0 0 L 24 24" /></svg>`;+ internals.updateStyle();+ hiddenLater.style.display = "none";+ internals.updateStyle();+ println(`icon hidden later has no style records: ${!hasRecord(iconHiddenLater) && !hasRecord(iconHiddenLater.firstElementChild)}`);+ });+</script>