Truly independent web browser
LibWeb: Ask the style engine about the element a tree build can’t style
sideshowbarker548820aba13a
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cppindex 0d70f224e76..11d14877cb6 100644--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Array.h>#include <AK/ScopeGuard.h>#include <AK/StringBuilder.h>#include <LibGfx/FontCascadeList.h>@@ -11,6 +12,8 @@#include <LibWeb/CSS/StyleEngineBridge.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/Element.h>+#include <LibWeb/DOM/ShadowRoot.h>+#include <LibWeb/HTML/AttributeNames.h>#include <LibWeb/HTML/HTMLObjectElement.h>#include <LibWeb/HTML/LocalNavigable.h>#include <LibWeb/Layout/LayoutRustFFI.h>@@ -24,8 +27,9 @@namespace Web::DOM {// Describes the node a style node names, for the panic of a layout update that ran out of passes: the node and its-// flat tree ancestors, each with whether it is styled and the display of its box, as a NUL-terminated ASCII string in-// the buffer, which the panic's message quotes.+// flat tree ancestors, each with whether it is styled and the display of its box, what the style engine knows of it,+// and the marks and attributes that bear on whether it can be styled, as a NUL-terminated ASCII string in the buffer,+// which the panic's message quotes.static void describe_node_for_bound_failure(Document& document, Layout::BegunRead const& read, u32 style_node, Bytes buffer){StringBuilder builder;@@ -35,6 +39,7 @@ static void describe_node_for_bound_failure(Document& document, Layout::BegunReaif (!node) {builder.append("none"sv);} else {+ auto const* engine_host = document.style_computer().style_engine().host();int depth = 0;for (Node* current = node.ptr(); current && depth < 6; current = current->flat_tree_parent(), ++depth) {if (depth)@@ -60,6 +65,45 @@ static void describe_node_for_bound_failure(Document& document, Layout::BegunReabuilder.appendff("{{{}}}", layout_node->display().to_string());elsebuilder.append("{no box}"sv);+ // The DOM-side marks that bear on styling the element, and the engine's view of it.+ builder.append('[');+ if (element->style_arrival_pending())+ builder.append("arrival-pending,"sv);+ if (element->child_style_arrivals_wait())+ builder.append("children-wait,"sv);+ if (element->descendant_style_arrival_pending())+ builder.append("descendant-pending,"sv);+ if (is<ShadowRoot>(element->root()))+ builder.append("in-shadow,"sv);+ if (element->has_assigned_slot())+ builder.append("slotted,"sv);+ if (element->style_node_id() == 0) {+ builder.append("no-style-node"sv);+ } else {+ auto facts = CSS::StyleEngineFFI::style_engine_bound_failure_node_facts(engine_host, &read, element->style_node_id().value());+ static constexpr Array displays { "-"sv, "none"sv, "contents"sv, "box"sv };+ static constexpr Array answers { "record"sv, "absent"sv, "refused"sv, "suspended"sv, "awaits-parent"sv };+ builder.appendff("engine:{},{},{},demand:{}", facts.is_live ? "live"sv : "dead"sv, facts.has_published_record ? "record"sv : "no-record"sv, displays[min<size_t>(facts.published_display, 3)], answers[min<size_t>(facts.read_demand_answer, 4)]);+ if (facts.owes_style_input)+ builder.append(",owes-input"sv);+ if (facts.ancestor_owes_style_input)+ builder.append(",ancestor-owes-input"sv);+ auto* flat_tree_parent_element = element->flat_tree_parent_element();+ u32 flat_tree_parent_style_node = flat_tree_parent_element ? flat_tree_parent_element->style_node_id().value() : 0;+ if (facts.inheritance_parent != flat_tree_parent_style_node || facts.flat_tree_parent != flat_tree_parent_style_node)+ builder.appendff(",engine-parents:{}/{},dom-parent:{}", facts.inheritance_parent, facts.flat_tree_parent, flat_tree_parent_style_node);+ }+ builder.append(']');+ if (depth == 0) {+ if (auto style = element->get_attribute(HTML::AttributeNames::style); style.has_value())+ builder.appendff(" style=\"{:.60}\"", style.value());+ for (auto const& name : { HTML::AttributeNames::hidden, HTML::AttributeNames::popover, HTML::AttributeNames::slot }) {+ if (element->has_attribute(name))+ builder.appendff(" {}", name);+ }+ // What one more targeted style update of the element answers now.+ builder.appendff(" targeted-update:{}", document.update_style_for_element(AbstractElement { *element }) ? "styled"sv : "unstyled"sv);+ }}}auto text = builder.string_view();diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rsindex 036b32b4873..7f481d2ca62 100644--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs@@ -19,7 +19,7 @@ use super::bridge::{use super::font_resolution::{FontResolverHost, PublishedFontFaces};use super::inputs::RetainedCustomPropertyData;use super::instrumentation::Counter;-use super::publication::RecordDemand;+use super::publication::{RecordDemand, RecordDemandAnswer, Unanswered};use super::random_bases::ParkedBaseValues;use super::tree::{StyleNodeID, TreeScopeID};use super::{HashMap, StyleAtomID};@@ -1213,6 +1213,81 @@ pub extern "C" fn style_engine_for_each_element_custom_property_environment(}}+/// What the engine knows of a node a layout update kept reaching without a style, for the panic that reports the+/// update running out of passes.+#[derive(Clone, Copy)]+#[repr(C)]+pub struct FfiBoundFailureNodeFacts {+ pub is_live: bool,+ pub has_published_record: bool,+ /// The display the node's published record holds: 0 for no record, 1 for none, 2 for contents, 3 for any other.+ pub published_display: u8,+ /// The style nodes the engine inherits the node's style from and places it under in the flat tree, or 0.+ pub inheritance_parent: u32,+ pub flat_tree_parent: u32,+ pub owes_style_input: bool,+ pub ancestor_owes_style_input: bool,+ /// How a read-only demand of the node's record answers: 0 for a record, 1 for absent, 2 for refused, 3 for+ /// suspended, 4 for a parent that holds no record yet.+ pub read_demand_answer: u8,+}++/// Answers what the engine knows of `node` for the panic of a layout update that ran out of passes reaching it without+/// a style. The demand it makes is read-only, so the engine is left as it was.+#[unsafe(no_mangle)]+pub extern "C" fn style_engine_bound_failure_node_facts(+ host: &DocumentHost,+ read: &BegunRead,+ node: u32,+) -> FfiBoundFailureNodeFacts {+ with_engine(read, host, |engine| {+ let Some(node) = StyleNodeID::from_raw(node) else {+ return FfiBoundFailureNodeFacts {+ is_live: false,+ has_published_record: false,+ published_display: 0,+ inheritance_parent: 0,+ flat_tree_parent: 0,+ owes_style_input: false,+ ancestor_owes_style_input: false,+ read_demand_answer: 2,+ };+ };+ let published_display = engine.element_published_box_facts(node).map_or(0, |facts| {+ if facts.display.is_none() {+ 1+ } else if facts.display.is_contents() {+ 2+ } else {+ 3+ }+ });+ let inheritance_parent = engine.retained.tree.inheritance_parent(node);+ let ancestor_owes_style_input = std::iter::successors(inheritance_parent, |&ancestor| {+ engine.retained.tree.inheritance_parent(ancestor)+ })+ .any(|ancestor| engine.host.owes_element_style_input(ancestor));+ let read_demand_answer =+ match engine.answer_record_demand(node, RecordDemand::Element(FfiRecordDemand::ElementRead)) {+ Ok(RecordDemandAnswer::Record { .. }) => 0,+ Ok(RecordDemandAnswer::Absent { .. }) => 1,+ Err(Unanswered::Refused) => 2,+ Err(Unanswered::Suspended(_)) => 3,+ Err(Unanswered::AwaitsParent) => 4,+ };+ FfiBoundFailureNodeFacts {+ is_live: engine.retained.tree.is_live(node),+ has_published_record: engine.published_element_record(node).is_some(),+ published_display,+ inheritance_parent: inheritance_parent.map_or(0, StyleNodeID::raw),+ flat_tree_parent: engine.retained.tree.flat_tree_parent(node).map_or(0, StyleNodeID::raw),+ owes_style_input: engine.host.owes_element_style_input(node),+ ancestor_owes_style_input,+ read_demand_answer,+ }+ })+}+#[unsafe(no_mangle)]pub extern "C" fn style_engine_has_pending_transaction(host: &DocumentHost, read: &BegunRead) -> bool {match host.known_engine_facts() {diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rsindex 573209d2d65..ece76b6832f 100644--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs@@ -153,9 +153,9 @@ pub struct FfiStabilizationBoundFailureHostFacts {pub document_child_needs_layout_tree_update: bool,/// The element the last unstyled-element report of the update named, with its flat tree ancestors, as the host/// describes them: a NUL-terminated ASCII string.- pub last_unstyled_element: [u8; 160],+ pub last_unstyled_element: [u8; 640],/// As above, for the element the last rebuild request of the update named.- pub last_rebuild_request: [u8; 160],+ pub last_rebuild_request: [u8; 640],}/// The text of a NUL-terminated string the host wrote into `bytes`.