Truly independent web browser
LibWeb: Keep SVG resource styles below nested `display: none` elements
Tim Ledbettere8ea3cf66d4d
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rsindex 0c8520d928d..8bac08c791e 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs@@ -363,7 +363,7 @@ impl RetainedState {let new = new_record.raw();// The main thread swaps in the record where it holds another one, or none, as below an element that was// display:none.- let main_holds_new = self.held_style_record(node) == Some(new);+ let main_holds_new = self.held_style_record(node) == Some(new) && !self.cleared_by_ancestor(node, scratch);if !main_holds_new {installation.swaps.push(StyleSwap::new(self,@@ -643,6 +643,12 @@ impl RetainedState {};let first_style = facts.element_damage.is_none();let effects = self.main_effects(node, old_view, new_view, facts);+ if effects+ .effects+ .intersects(bridge::MainEffects::CLEAR_HIDDEN_DESCENDANTS)+ {+ scratch.note_effects_clear_hidden_descendants(node);+ }scratch.document_damage.push(effects.damage());installation.note_element_effects(effects, first_swap);if first_style {@@ -750,6 +756,32 @@ impl RetainedState {hidden}+ /// Whether an ancestor of `node` that the installation installed already clears the styles of its descendants,+ /// noting the answer for each element asked in `scratch`.+ fn cleared_by_ancestor(&self, node: StyleNodeID, scratch: &mut InstallScratch) -> bool {+ if !scratch.any_effects_clear_hidden_descendants() {+ return false;+ }+ let mut chain = std::mem::take(&mut scratch.chain);+ chain.clear();+ let mut ancestor = self.projection.inheritance_parent(node);+ let mut cleared = false;+ while let Some(current) = ancestor {+ if let Some(answer) = scratch.cleared_below(current) {+ cleared = answer;+ break;+ }+ chain.push(current);+ ancestor = self.projection.inheritance_parent(current);+ }+ for ¤t in chain.iter().rev() {+ cleared = cleared || scratch.effects_clear_hidden_descendants(current);+ scratch.note_cleared_below(current, cleared);+ }+ scratch.chain = chain;+ cleared+ }+/// Whether the host clears the styles of `node`'s descendants as `node` moves from a record whose display was none/// or not, `old_is_none`, to one whose display is none or not, `new_is_none`, the move changing the style groups of/// `inherited_groups_changed`, an `FfiStyleInvalidationField` inherited groups mask, and moving the element'sdiff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/install_scratch.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/install_scratch.rsindex 393f9b91caa..993d4ef1a53 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/install_scratch.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/install_scratch.rs@@ -26,7 +26,13 @@ const HIDDEN_BEFORE_KNOWN: u32 = 1 << 2;const HIDDEN_BEFORE: u32 = 1 << 3;/// The element takes its first style in the installation.const FIRST_STYLE: u32 = 1 << 4;-const MARK_BITS: u32 = 5;+/// The effects of the element in the installation clear the styles of its descendants.+const EFFECTS_CLEAR_HIDDEN_DESCENDANTS: u32 = 1 << 5;+/// Whether the element, or an ancestor of it, clears the styles of its descendants in the installation is known.+const CLEARED_BELOW_KNOWN: u32 = 1 << 6;+/// The element, or an ancestor of it, clears the styles of its descendants in the installation.+const CLEARED_BELOW: u32 = 1 << 7;+const MARK_BITS: u32 = 8;const LAST_GENERATION: u32 = u32::MAX >> MARK_BITS;#[derive(Default)]@@ -48,6 +54,7 @@ pub(super) struct InstallScratch {/// The elements that installed a copy of their records, which they keep in place of their own,/// by the address of their own.pub(super) adopted: Vec<(StyleNodeID, usize, std::sync::Arc<super::Record>)>,+ any_effects_clear_hidden_descendants: bool,}impl InstallScratch {@@ -76,6 +83,7 @@ impl InstallScratch {self.marks.fill(0);self.generation = 1;}+ self.any_effects_clear_hidden_descendants = false;}/// Gives `node` `mark`, answering whether it had it not.@@ -160,6 +168,38 @@ impl InstallScratch {self.marks_of(node) & FIRST_STYLE != 0}+ /// Notes that the effects of `node` in the installation clear the styles of its descendants.+ pub(super) fn note_effects_clear_hidden_descendants(&mut self, node: StyleNodeID) {+ self.mark(node, EFFECTS_CLEAR_HIDDEN_DESCENDANTS);+ self.any_effects_clear_hidden_descendants = true;+ }++ /// Whether the effects of any element in the installation clear the styles of its descendants, as noted so far.+ pub(super) fn any_effects_clear_hidden_descendants(&self) -> bool {+ self.any_effects_clear_hidden_descendants+ }++ /// Whether the effects of `node` in the installation clear the styles of its descendants, as noted so far.+ pub(super) fn effects_clear_hidden_descendants(&self, node: StyleNodeID) -> bool {+ self.marks_of(node) & EFFECTS_CLEAR_HIDDEN_DESCENDANTS != 0+ }++ /// Whether `node`, or an ancestor of it, clears the styles of its descendants in the installation, where known.+ pub(super) fn cleared_below(&self, node: StyleNodeID) -> Option<bool> {+ let marks = self.marks_of(node);+ (marks & CLEARED_BELOW_KNOWN != 0).then_some(marks & CLEARED_BELOW != 0)+ }++ pub(super) fn note_cleared_below(&mut self, node: StyleNodeID, cleared: bool) {+ self.mark(node, CLEARED_BELOW_KNOWN);+ if let Some(marks) = self.marks.get_mut(node.element_slot()) {+ *marks = match cleared {+ true => *marks | CLEARED_BELOW,+ false => *marks & !CLEARED_BELOW,+ };+ }+ }+/// Empties the scratch for the next installation, keeping its room up to/// `KEPT_SCRATCH_CAPACITY` entries in each collection.pub(super) fn recycle(&mut self) {diff --git a/Tests/LibWeb/Ref/input/svg/filter-in-display-none-subtree-hidden-again.html b/Tests/LibWeb/Ref/input/svg/filter-in-display-none-subtree-hidden-again.htmlnew file mode 100644index 00000000000..fcd72eb092a--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg/filter-in-display-none-subtree-hidden-again.html@@ -0,0 +1,35 @@+<!DOCTYPE html>+<html class="reftest-wait">+<link rel="match" href="../../expected/svg/filter-in-display-none-subtree-ref.html">+<style>+ body { margin: 0; }+ #definitions { position: absolute; color: red; }+ #target { width: 100px; height: 100px; background: red; filter: url(#flood); }+</style>+<div id="definitions">+ <div style="display: none">+ <svg>+ <filter id="flood">+ <feFlood flood-color="currentColor" />+ <feComposite in2="SourceGraphic" operator="in" />+ </filter>+ </svg>+ </div>+</div>+<div id="target"></div>+<script>+new IntersectionObserver(() => {}).observe(target);+requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ definitions.style.display = "none";+ target.style.width = "101px";+ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ definitions.style.color = "green";+ target.style.width = "102px";+ document.documentElement.classList.remove("reftest-wait");+ });+ });+ });+});+</script>