Skip to content

Truly independent web browser

LibWeb: Stop recomputing styles of hidden subtrees

Andreas Klingecff111467a6
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index fbf6c0a6280..08048ab9187 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -522,8 +522,17 @@ static RequiredInvalidationAfterStyleChange install_engine_computed_records(Layo
auto changed_inherited_groups = StyleEngineFFI::style_engine_changed_style_groups(&old_record, &new_record, 0, ComputedValues::inherited_style_group_count);
invalidation.set_inherited_style_groups_changed(static_cast<u8>(changed_inherited_groups & RequiredInvalidationAfterStyleChange::all_inherited_style_groups));
}
- if (old_computed_values)
+ if (old_computed_values) {
element.apply_display_none_change(read, DOM::Element::DisplayNoneState::of(*old_computed_values));
+ // NB: The descendants of an element that stays display:none have no boxes, so their styles only serve a CSSOM
+ // read or the subtree's reveal, both of which materialize them again. Where what they inherit moves, clear
+ // them as entering display:none does rather than computing them all again. The SVG elements visible
+ // content can reference keep their styles, and take what they inherit from styled ancestors.
+ if (old_computed_values->display().is_none() && element.computed_style()->display().is_none()
+ && invalidation.inherited_style_groups_changed() != 0 && !did_change_custom_properties
+ && !element.has_referenceable_svg_descendant())
+ element.clear_computed_styles_from_display_none_descendants(read);
+ }
// The pseudo-elements of a record the host composes inherit the composition, so the engine settles them only now.
if (reaction.composed_by_the_host)
invalidation |= element.refresh_pseudo_element_styles_over_composition(read, old_computed_values ? &*old_computed_values : nullptr, did_change_custom_properties);
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index df9b70e1af4..6e03ab72b05 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -2449,6 +2449,26 @@ void Element::apply_display_none_change(Layout::BegunRead const& read, DisplayNo
clear_computed_styles_from_display_none_descendants(read);
}
+bool Element::has_referenceable_svg_descendant() const
+{
+ bool found = false;
+ // NB: The traversal only reads the subtree.
+ const_cast<Element&>(*this).for_each_shadow_including_descendant([&found](auto const& node) {
+ auto const* element = as_if<Element>(node);
+ if (!element)
+ return TraversalDecision::Continue;
+ // NB: A resource keeps its style, and so do the elements it inherits from, so an unstyled subtree holds none.
+ if (!element->has_style())
+ return TraversalDecision::SkipChildrenAndContinue;
+ if (auto const* svg_element = as_if<SVG::SVGElement>(*element); svg_element && svg_element->is_referenceable_in_display_none_subtree()) {
+ found = true;
+ return TraversalDecision::Break;
+ }
+ return TraversalDecision::Continue;
+ });
+ return found;
+}
+
void Element::clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read)
{
for_each_shadow_including_descendant([&read](auto& node) {
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 9ff384c0113..d2e7bcc48db 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -398,6 +398,12 @@ public:
// Terminates or resumes the animations of a subtree whose display, ignoring animations, left or entered none since
// `before`, and clears the styles of a subtree that became display:none.
void apply_display_none_change(Layout::BegunRead const& read, DisplayNoneState before);
+ // Clears the styles of the descendants of a display:none element, which have no boxes: a CSSOM read or the subtree's
+ // reveal materializes them again. The styles of the SVG elements visible content can reference are computed again.
+ void clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read);
+ // Whether a styled descendant is an SVG element visible content can reference, whose style is kept while the element
+ // is display:none.
+ [[nodiscard]] bool has_referenceable_svg_descendant() const;
// Refreshes the pseudo-element styles of an element whose record the engine settled without them, once the host has
// composed the element's animations over the record, which the pseudo-elements inherit. `old_computed_values` is
// the style the element held before the record.
@@ -1039,7 +1045,6 @@ private:
void publish_custom_property_names();
void update_anchor_name_registry(CSS::ComputedValues const* old_computed_values, CSS::ComputedValues const& new_computed_values);
void replace_style_record(CSS::StyleRecordID);
- void clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read);
WebIDL::ExceptionOr<GC::Ptr<Node>> insert_adjacent(Utf16View where, GC::Ref<Node> node);
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/hidden-subtree-styles-are-not-recomputed.txt b/Tests/LibWeb/Text/expected/css/style-engine/hidden-subtree-styles-are-not-recomputed.txt
new file mode 100644
index 00000000000..4fef4bf6219
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/hidden-subtree-styles-are-not-recomputed.txt
@@ -0,0 +1,5 @@
+hidden grandchild color: rgb(255, 0, 0)
+hidden grandchild has style after read: true
+hidden grandchild style is cleared: true
+hidden child style is cleared: true
+hidden grandchild color after move: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/css/style-engine/hidden-subtree-styles-are-not-recomputed.html b/Tests/LibWeb/Text/input/css/style-engine/hidden-subtree-styles-are-not-recomputed.html
new file mode 100644
index 00000000000..d820fc3461b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/hidden-subtree-styles-are-not-recomputed.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<style>
+ #hidden { display: none; color: red; }
+</style>
+<div id="hidden">
+ <div id="child"><span id="grandchild">hidden text</span></div>
+</div>
+<script>
+test(() => {
+ internals.updateStyle();
+
+ // A CSSOM read materializes the styles of a hidden subtree.
+ println(`hidden grandchild color: ${getComputedStyle(grandchild).color}`);
+ println(`hidden grandchild has style after read: ${internals.styleRecordIdentity(grandchild) !== 0}`);
+
+ // A moved inherited value of the hidden root clears the subtree's styles rather than computing them again.
+ hidden.style.color = "green";
+ internals.updateStyle();
+ println(`hidden grandchild style is cleared: ${internals.styleRecordIdentity(grandchild) === 0}`);
+ println(`hidden child style is cleared: ${internals.styleRecordIdentity(child) === 0}`);
+ println(`hidden grandchild color after move: ${getComputedStyle(grandchild).color}`);
+});
+</script>