Truly independent web browser
LibWeb: Run a style update when a chain read finds input pending
Andreas Klingfb3487adfd5f
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cppindex 387f6f304c2..0795878078d 100644--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp@@ -864,11 +864,29 @@ static bool update_style_for_element(Layout::BegunRead const& read, DOM::Documenreturn true;}- StyleReactionApplication application { read, document };- document.style_computer().begin_inheritance_chain_read();- ScopeGuard end_inheritance_chain_read = [&] { document.style_computer().end_inheritance_chain_read(); };- auto answer = StyleEngineFFI::style_engine_update_style_for_inheritance_chain(document.style_computer().style_engine().host(), &read, &application, first_element_of_inheritance_chain(abstract_element), static_cast<StyleEngineFFI::FfiStyleUpdateMode>(mode), embedding_document_layout_was_stale);- apply_document_style_invalidation_after_style_change(document, application.invalidation);+ auto read_inheritance_chain = [&] {+ StyleReactionApplication application { read, document };+ document.style_computer().begin_inheritance_chain_read();+ ScopeGuard end_inheritance_chain_read = [&] { document.style_computer().end_inheritance_chain_read(); };+ auto answer = StyleEngineFFI::style_engine_update_style_for_inheritance_chain(document.style_computer().style_engine().host(), &read, &application, first_element_of_inheritance_chain(abstract_element), static_cast<StyleEngineFFI::FfiStyleUpdateMode>(mode), embedding_document_layout_was_stale);+ apply_document_style_invalidation_after_style_change(document, application.invalidation);+ return answer;+ };+ auto answer = read_inheritance_chain();+ // NB: An element of the chain would read an input the next style update has yet to take: one recorded beside the+ // last style update above, or as the style of an element above it on the chain was installed. A style update+ // takes it, and the chain is read again. A read that cannot run one, or that still finds input pending after+ // these rounds, answers what the element holds.+ constexpr size_t style_update_rounds_of_a_chain_read = 4;+ for (size_t round = 0; answer == StyleEngineFFI::FfiTargetedStyleAnswer::NeedsStyleUpdate; ++round) {+ if (round == style_update_rounds_of_a_chain_read || !entered_stabilization_epoch || document.is_running_update_layout() || document.is_at_style_stabilization_bound(read)) {+ dbgln("Style read of an element answers the style it holds, with style input still pending");+ answer = StyleEngineFFI::FfiTargetedStyleAnswer::HeldStyle;+ break;+ }+ update_style(read, document, RecordedStyleUpdate { DocumentWithoutBrowsingContext::Update });+ answer = read_inheritance_chain();+ }return answer_style_read(abstract_element, answer);}diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rsindex fe359625c27..d2949743eaa 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs@@ -527,10 +527,12 @@ impl StyleEngine {node: StyleNodeID,demand: RecordDemand,) -> RecordDemandAnswer {- assert!(- self.retained.record_demand_reads_current_inputs(node, &self.host),- "a read runs style updates until none is pending, so no input is pending as the engine answers it"- );+ if let Some(pending) = self.retained.pending_input_of_record_demand(node, &self.host) {+ panic!(+ "a read runs style updates until none is pending, so no input is pending as the engine answers it: \+ {node:?} reads {pending}"+ );+ }// Only a private read of an element leaves its inline style out: the record it answers is// no element's.let without_inline_style = matches!(@@ -689,17 +691,20 @@ impl StyleEngine {/// The installation of the current record of `node`, computed on demand where it holds none, for a read of its/// style, which installs it as a style update does. No transaction lies behind it, and the render side leaves the- /// element's boxes to the host. None for a document with no live style.+ /// element's boxes to the host. None for a document with no live style. Where the demand would read an input the+ /// next transaction has yet to take, as one the host recorded while it installed the record of an element above,+ /// it answers that input and computes nothing: the host runs a style update first.pub(in super::super) fn demand_installation(&mut self,node: StyleNodeID,- ) -> Option<super::install::StyleInstallation> {+ ) -> Result<Option<super::install::StyleInstallation>, crate::css::style::publication::PendingDemandInput> {// NB: A document that is never styled, as the one holding a template's contents, has no live style.- self.retained.live_style.as_ref()?;- assert!(- self.retained.record_demand_reads_current_inputs(node, &self.host),- "a read runs style updates until none is pending, so no input is pending as the engine answers it"- );+ if self.retained.live_style.is_none() {+ return Ok(None);+ }+ if let Some(pending) = self.retained.pending_input_of_record_demand(node, &self.host) {+ return Err(pending);+ }let demanded = self.demand_element(node, false);let projection = &self.retained.projection;let root = std::iter::successors(Some(node), |&node| projection.inheritance_parent(node))@@ -741,7 +746,7 @@ impl StyleEngine {installation.leave_boxes_to_the_host();installation.note_held_by_the_host(&mut self.retained);self.consume_element_style_input(node);- Some(installation)+ Ok(Some(installation))}/// The current record of `node`, computed on demand where it holds none.diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rsindex a7ff8ff6f1b..726243aa865 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs@@ -13,7 +13,7 @@ use crate::css::cascaded_properties::NO_PSEUDO_ELEMENT;use crate::css::computed_longhand_table::IN_DISPLAY_NONE_SUBTREE;use crate::css::computed_value_views::QuoteTransfer;use animations::DeclarationScope;-pub(crate) use demand::{RecordDemand, RecordDemandAnswer};+pub(crate) use demand::{PendingDemandInput, RecordDemand, RecordDemandAnswer};/// What a subtree does to the generated-content state that the boxes after it read.#[derive(Clone, Copy, Default)]diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rsindex fbf318babca..bedf8856d27 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs@@ -45,6 +45,55 @@ pub(crate) enum RecordDemandAnswer {Absent { custom_property_environment: u64 },}+/// What a record demand would read that the next transaction has yet to take.+#[derive(Clone, Copy, Debug)]+pub(crate) enum PendingDemandInput {+ /// The node is not in the tree the engine styles.+ NodeIsNotLive,+ /// The links of an element were written.+ WrittenLinks,+ /// A rule or a sheet was edited.+ StagedRules,+ /// The rules of a sheet are being replaced.+ SheetRuleReplacement,+ /// The document's environment moved.+ Environment,+ /// The node's own row changed.+ RowChange,+ /// An element the node is below in the flat tree owes a computation, for each reason that holds.+ Ancestor {+ ancestor: StyleNodeID,+ owes_element_input: bool,+ owes_row_change: bool,+ owes_deferred_input: bool,+ deferred_inputs_are_pending: bool,+ },+}++impl std::fmt::Display for PendingDemandInput {+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ match *self {+ Self::NodeIsNotLive => formatter.write_str("a node that is not live"),+ Self::WrittenLinks => formatter.write_str("written links"),+ Self::StagedRules => formatter.write_str("staged rules"),+ Self::SheetRuleReplacement => formatter.write_str("a sheet rule replacement"),+ Self::Environment => formatter.write_str("an environment that moved"),+ Self::RowChange => formatter.write_str("a change of its own row"),+ Self::Ancestor {+ ancestor,+ owes_element_input,+ owes_row_change,+ owes_deferred_input,+ deferred_inputs_are_pending,+ } => write!(+ formatter,+ "the ancestor {ancestor:?}, which owes an element input: {owes_element_input}, a row change: \+ {owes_row_change}, a deferred input: {owes_deferred_input} (pending: {deferred_inputs_are_pending})"+ ),+ }+ }+}+impl RetainedState {/// Keep a private answer's record alive for the host, which reads it once the demand has/// returned. The record the target's previous demand held is let go.@@ -67,14 +116,33 @@ impl RetainedState {node: StyleNodeID,host: &HostState,) -> bool {- if !self.tree.is_live(node)- || self.projection.has_written_links()- || host.program_staging.is_dirty()- || host.sheet_rule_replacement.is_some()- || host.element_inputs.moves_environment()- || self.projection.has_pending_change(node)- {- return false;+ self.pending_input_of_record_demand(node, host).is_none()+ }++ /// What a demand for `node` would read that the next transaction has yet to take, or none where+ /// everything it reads is current.+ pub(in crate::css::style) fn pending_input_of_record_demand(+ &self,+ node: StyleNodeID,+ host: &HostState,+ ) -> Option<PendingDemandInput> {+ if !self.tree.is_live(node) {+ return Some(PendingDemandInput::NodeIsNotLive);+ }+ if self.projection.has_written_links() {+ return Some(PendingDemandInput::WrittenLinks);+ }+ if host.program_staging.is_dirty() {+ return Some(PendingDemandInput::StagedRules);+ }+ if host.sheet_rule_replacement.is_some() {+ return Some(PendingDemandInput::SheetRuleReplacement);+ }+ if host.element_inputs.moves_environment() {+ return Some(PendingDemandInput::Environment);+ }+ if self.projection.has_pending_change(node) {+ return Some(PendingDemandInput::RowChange);}// The nodes owing input are gathered once, so a chain of demands reads the pending inputs// once per demand, not once per ancestor.@@ -84,11 +152,20 @@ impl RetainedState {.chain(self.projection.pending_change_nodes()).chain(host.deferred_element_style_inputs.keys().copied()).collect();- owing.is_empty()- || !std::iter::successors(self.projection.flat_tree_parent(node), |&parent| {- self.projection.flat_tree_parent(parent)- })- .any(|ancestor| owing.contains(&ancestor))+ if owing.is_empty() {+ return None;+ }+ let ancestor = std::iter::successors(self.projection.flat_tree_parent(node), |&parent| {+ self.projection.flat_tree_parent(parent)+ })+ .find(|ancestor| owing.contains(ancestor))?;+ Some(PendingDemandInput::Ancestor {+ ancestor,+ owes_element_input: host.element_inputs.nodes().any(|owing| owing == ancestor),+ owes_row_change: self.projection.has_pending_change(ancestor),+ owes_deferred_input: host.deferred_element_style_inputs.contains_key(&ancestor),+ deferred_inputs_are_pending: host.deferred_element_style_inputs_are_pending,+ })}}diff --git a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rsindex 7f06ee9be8a..e0986571a6e 100644--- a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs+++ b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs@@ -14,6 +14,7 @@ use super::StyleNodeID;use super::engine_calls::{has_deferred_element_style_input, with_engine};use super::pipeline::install::{StyleInstallation, StyleSwap};use super::pipeline::main_effects::InstalledElementEffects;+use super::publication::PendingDemandInput;use crate::render_state::{BegunRead, DocumentHost};/// A `Web::DOM::Element`, which Rust names by pointer only.@@ -141,10 +142,11 @@ impl Application<'_> {/// A targeted update of `node`: the engine computes the record it demands for the element now, and the host takes/// the installation of that record as it takes a style update's. Answers whether the engine could: where it could- /// not, the element keeps what it holds until the next style update.- fn demand(self, node: StyleNodeID) -> bool {- let Some(installation) = with_engine(self.read, self.host, |engine| engine.demand_installation(node)) else {- return false;+ /// not, the element keeps what it holds until the next style update. Where the record would read an input the+ /// next transaction has yet to take, it answers that input, and the host runs a style update first.+ fn demand(self, node: StyleNodeID) -> Result<bool, PendingDemandInput> {+ let Some(installation) = with_engine(self.read, self.host, |engine| engine.demand_installation(node))? else {+ return Ok(false);};self.install_styles(&installation);with_engine(self.read, self.host, |engine| {@@ -152,7 +154,7 @@ impl Application<'_> {engine.unpin_layout_style_record(record);}});- true+ Ok(true)}}@@ -439,8 +441,12 @@ impl Application<'_> {let Some(node) = StyleNodeID::from_raw(style_node).filter(|_| !element.element.is_null()) else {return FfiTargetedStyleAnswer::HeldStyle;};- if !self.demand(node) {- return FfiTargetedStyleAnswer::HeldStyle;+ match self.demand(node) {+ Ok(true) => {}+ Ok(false) => return FfiTargetedStyleAnswer::HeldStyle,+ // NB: The host records inputs as it installs a record, as where it composes the element's animations,+ // and beside its last style update. The elements below read them once a style update took them.+ Err(_) => return FfiTargetedStyleAnswer::NeedsStyleUpdate,}let element = self.element(style_node);@@ -476,7 +482,8 @@ pub unsafe extern "C" fn style_engine_read_settled_inheritance_chain(}/// Updates the style of the chain of elements an element inherits through, from `first`, for a read of the element's-/// style, and answers the read.+/// style, and answers the read, or that it needs a style update first: an element of the chain would read an input+/// the next transaction has yet to take.////// # Safety///