Truly independent web browser
LibWeb: Write an arriving element's row once, in blocks
Andreas Kling0d1f8b2dbbcc
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cppindex 524ea793217..0f3aac5aa67 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp@@ -103,6 +103,11 @@ void StyleEngine::defer_element_initial_features(DOM::Element& element){element.set_initial_style_features_pending(true);m_nodes_with_pending_initial_features.append(element.style_node_id());+ note_element_awaiting_first_style_computation(element);+}++void StyleEngine::note_element_awaiting_first_style_computation(DOM::Element& element)+{if (element.needs_preparation_for_style_computation())m_nodes_awaiting_first_style_computation.set(element.style_node_id());}@@ -368,13 +373,13 @@ void StyleEngine::record_element_arrivals(ReadonlySpan<StyleEngineFFI::FfiArriviStyleEngineFFI::style_engine_stage_element_arrivals(host(), elements);}-void StyleEngine::record_element_arrival(StyleEngineFFI::FfiElementArrival const& arrival, ReadonlySpan<StyleAtomID> custom_states, ReadonlySpan<StyleAtomID> classes, ReadonlySpan<StyleAtomID> attributes)+void StyleEngine::record_element_arrival_rows(ReadonlySpan<StyleEngineFFI::FfiElementArrivalRow> rows, ReadonlySpan<u32> atoms){- VERIFY(custom_states.size() == arrival.custom_state_count && classes.size() == arrival.class_count && attributes.size() == 2 * static_cast<size_t>(arrival.attribute_count));+ if (rows.is_empty())+ return;flush_deferred_geometry_transaction_before_non_replayable_input(*this, m_style_computer);request_frame_for_first_recorded_input(*this, m_style_computer);- static_assert(sizeof(StyleAtomID) == sizeof(u32));- StyleEngineFFI::style_engine_stage_element_arrival(host(), &arrival, reinterpret_cast<u32 const*>(custom_states.data()), reinterpret_cast<u32 const*>(classes.data()), reinterpret_cast<u32 const*>(attributes.data()));+ StyleEngineFFI::style_engine_stage_element_arrival_rows(host(), rows, atoms);}void StyleEngine::record_local_feature_delta(StyleEngineFFI::FfiLocalFeatureDelta const& delta)@@ -466,7 +471,7 @@ void StyleEngine::submit_recorded_input()m_style_computer->document().assign_slottables_if_needed();Layout::ForcedReadScope read { render_document() };if (m_style_computer) {- take_in_pending_style_arrivals(m_style_computer->document(), HiddenChildArrivals::MayWait);+ take_in_and_publish_pending_style_arrivals(m_style_computer->document(), HiddenChildArrivals::MayWait, read);publish_pending_element_features(*this, *m_style_computer, read);publish_withheld_element_features(*this, *m_style_computer, read);}diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.hindex e69606cac1c..d02d2f5371e 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h@@ -13,6 +13,7 @@#include <AK/Optional.h>#include <AK/Span.h>#include <AK/StringView.h>+#include <AK/TemporaryChange.h>#include <AK/Time.h>#include <AK/Types.h>#include <AK/Utf16FlyString.h>@@ -186,7 +187,10 @@ public:StyleNodeID mint_style_node();void mint_style_nodes(Span<StyleNodeID> nodes);void mint_text_style_nodes(Span<StyleNodeID> nodes);+ bool is_taking_in_pending_arrivals() const { return m_taking_in_pending_arrivals; }+ [[nodiscard]] TemporaryChange<bool> begin_taking_in_pending_arrivals() { return TemporaryChange<bool>(m_taking_in_pending_arrivals, true); }void defer_element_initial_features(DOM::Element&);+ void note_element_awaiting_first_style_computation(DOM::Element&);void cancel_deferred_element_initial_features(DOM::Element&);// Whether any element waits to have its initial features published, or to be prepared for its first style.[[nodiscard]] bool has_elements_awaiting_initial_style_work() const { return !m_nodes_with_pending_initial_features.is_empty() || !m_nodes_awaiting_first_style_computation.is_empty(); }@@ -306,10 +310,10 @@ public:// The document host stages deltas, which cross to the engine in one batch per style flush, never one write per// element.void record_tree_delta(StyleEngineFFI::FfiTreeDelta const&);- // Everything an element's selectors test arrives with it, as one row: its custom states, classes, and attributes- // as name and value atom pairs, beside the facts the arrival names.+ // Everything an element's selectors test arrives with it, as one row, and the rows of many elements in one write.+ // The atoms hold each row's custom states, classes, and attributes as name and value atom pairs in turn.void record_element_arrivals(ReadonlySpan<StyleEngineFFI::FfiArrivingElement>);- void record_element_arrival(StyleEngineFFI::FfiElementArrival const&, ReadonlySpan<StyleAtomID> custom_states, ReadonlySpan<StyleAtomID> classes, ReadonlySpan<StyleAtomID> attributes);+ void record_element_arrival_rows(ReadonlySpan<StyleEngineFFI::FfiElementArrivalRow>, ReadonlySpan<u32> atoms);void record_local_feature_delta(StyleEngineFFI::FfiLocalFeatureDelta const&);void record_state_delta(StyleEngineFFI::FfiStateDelta const&);// The hover moves to `target`, or off the document for none, as the events of a mouse move hover it, once the rest@@ -469,6 +473,7 @@ private:// NB: In the order the elements arrived, which is tree order. An element that left since is still listed, and no// longer holds its mark, DOM::Element::initial_style_features_pending().Vector<StyleNodeID> m_nodes_with_pending_initial_features;+ bool m_taking_in_pending_arrivals { false };HashTable<StyleNodeID> m_nodes_awaiting_first_style_computation;HashTable<StyleNodeID> m_parents_whose_children_changed_beside_flown_transaction;diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cppindex 529b5df9723..6a26f6bf6e7 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp@@ -7,7 +7,6 @@#include <AK/HashTable.h>#include <AK/QuickSort.h>#include <AK/SetUnion.h>-#include <AK/TemporaryChange.h>#include <LibGC/ConservativeVector.h>#include <LibGC/RootHashTable.h>#include <LibWeb/Animations/KeyframeEffect.h>@@ -70,7 +69,8 @@ static void record_element_heading_level(DOM::Element&);class ElementPublication;-static void record_element_initial_features(DOM::Element&, ElementPublication&);+static void publish_element_arrival(StyleEngine&, DOM::Element&, ElementPublication&, Optional<StyleEngineFFI::FfiArrivingElement const&> = {});+static void finish_element_publication(StyleEngine&, ElementPublication&);static void record_element_inline_style_properties(DOM::Element&);static void record_heading_levels_in_subtree(DOM::Element&);static void republish_assigned_slots_of(ReadonlySpan<GC::Ref<DOM::Node>>);@@ -336,6 +336,26 @@ static void prefetch_node(DOM::Node const& node)__builtin_prefetch(bytes + offset);}+// OPTIMIZATION: Most of an element's publication is first reading memory no one touched since the element was parsed.+// Asking for it ahead turns waiting for one element at a time into overlapping waits. Each stage reads+// what the one before it asked for, so the distances shrink along the chain.+template<typename NodeAt>+static void prefetch_publication_ahead(size_t index, size_t count, NodeAt const& node_at)+{+ if (index + 2 * nodes_ahead < count) {+ if (DOM::Node const* ahead = node_at(index + 2 * nodes_ahead))+ prefetch_node(*ahead);+ }+ if (index + nodes_ahead < count) {+ if (auto const* ahead = as_if<DOM::Element>(node_at(index + nodes_ahead)))+ __builtin_prefetch(ahead->inline_style_declarations());+ }+ if (index + nodes_ahead / 2 < count) {+ if (auto const* ahead = as_if<DOM::Element>(node_at(index + nodes_ahead / 2)); ahead && ahead->inline_style_declarations())+ Parser::ValueParserFFI::rust_declaration_block_prefetch_header(ahead->inline_style_declarations());+ }+}+// A node that connects takes no identity at once. Script often inserts markup and replaces it again before anything// reads style, and a node nothing observes then costs the engine nothing: its subtree is marked as waiting to arrive,// and take_in_pending_style_arrivals() gives it its identity once something observes the engine.@@ -441,13 +461,11 @@ static void release_waiting_children(DOM::Element& element)}}-static bool s_taking_in_pending_style_arrivals = false;-// Children that waited may have to arrive as the input is next submitted. A style update or a take-in only notes it.// Script records it as it records an insertion, which the transaction a geometry read deferred must not be waiting for.static void note_waiting_children_may_arrive(StyleEngine& style_engine, DOM::Document const& document){- if (s_taking_in_pending_style_arrivals || document.is_in_style_stabilization_epoch())+ if (style_engine.is_taking_in_pending_arrivals() || document.is_in_style_stabilization_epoch())style_engine.note_waiting_arrivals_may_arrive();elsestyle_engine.note_pending_arrivals(1);@@ -579,6 +597,251 @@ void record_subtree_connecting(DOM::Node& root)mark_style_arrival_pending(root, *style_engine);}+// A node that is no element hovers the element it is inside of.+static DOM::Element const* hovered_element_of(DOM::Node const* node)+{+ if (!node)+ return nullptr;+ if (auto const* element = as_if<DOM::Element>(*node))+ return element;+ return node->parent_element().ptr();+}++// The questions about an element that elements of its name answer differently.+enum class ElementOpenFacts : u16 {+ None = 0,+ Input = 1 << 0,+ Audio = 1 << 1,+ SvgRoot = 1 << 2,+ SvgSymbol = 1 << 3,+ SvgElement = 1 << 4,+ Body = 1 << 5,+ Heading = 1 << 6,+ ReplacedContent = 1 << 7,+ OwnDirectionality = 1 << 8,+ PresentationalHints = 1 << 9,+};+AK_ENUM_BITWISE_OPERATORS(ElementOpenFacts);++static ElementOpenFacts element_open_facts(DOM::Element const&);+static u32 kind_style_adjustment_facts(DOM::Element const&);+static u32 kind_construction_facts(DOM::Element const&);+static u32 open_style_adjustment_facts(DOM::Element const&, ElementOpenFacts);+static u32 open_construction_facts(DOM::Element const&, ElementOpenFacts, bool is_in_user_agent_shadow_tree);++// What an element's qualified name settles. The name picks the element's class, so this holds for every element of it,+// and only what the name leaves open is asked of each.+struct ElementKind {+ DOM::QualifiedName::Impl const* name { nullptr };+ StyleAtomID tag_atom;+ StyleAtomID folded_tag_atom;+ StyleAtomID namespace_atom;+ u32 adjustment_facts { 0 };+ u32 construction_facts { 0 };+ ElementOpenFacts open_facts { ElementOpenFacts::None };+ SelectorMatching::ElementStateQuestions state_questions { SelectorMatching::ElementStateQuestions::None };+ bool is_slot { false };+};++static ElementKind element_kind_of(StyleEngine& style_engine, DOM::Element const& element)+{+ auto tag_atom = style_engine.intern_atom(element.local_name());+ ElementKind kind {+ .name = element.qualified_name_identity(),+ .tag_atom = tag_atom,+ .folded_tag_atom = tag_atom,+ .namespace_atom = {},+ .adjustment_facts = kind_style_adjustment_facts(element),+ .construction_facts = kind_construction_facts(element),+ .open_facts = element_open_facts(element),+ .state_questions = SelectorMatching::element_state_questions(element),+ .is_slot = is<HTML::HTMLSlotElement>(element),+ };+ if (auto folded_name = element.local_name().to_ascii_lowercase(); folded_name != element.local_name())+ kind.folded_tag_atom = style_engine.intern_atom(folded_name);+ if (auto const& namespace_uri = element.namespace_uri(); namespace_uri.has_value() && !namespace_uri->is_empty())+ kind.namespace_atom = style_engine.intern_atom(*namespace_uri);+ return kind;+}++// What one attribute name settles, which is the same on every element that has it.+struct PublishedAttributeName {+ DOM::QualifiedName::Impl const* name { nullptr };+ StyleAtomID atom;+ bool is_known_unread { false };+ bool is_lang { false };+ bool is_dir { false };+};++// A table a batch keeps of what it found out by name. Names repeat, so a slot a name shares with another only costs+// asking again.+template<typename Entry, size_t slot_count>+class TableByName {+public:+ static_assert((slot_count & (slot_count - 1)) == 0);++ Entry& slot_of(DOM::QualifiedName::Impl const* name)+ {+ // Fibonacci hashing: the high bits of the product depend on all of an allocation's address.+ auto hash = (reinterpret_cast<FlatPtr>(name) * 0x9e3779b97f4a7c15ull) >> (64 - count_trailing_zeroes(slot_count));+ return m_slots[hash];+ }++private:+ Array<Entry, slot_count> m_slots {};+};++// One batch of elements publishing their initial features. Nothing but the batch changes the DOM or what the engine+// reads while it lives, so what one element answers holds for the next one asking. The elements cross to the engine in+// blocks: their rows are written in one call, and what is published of them beside their rows follows the call.+class ElementPublication {+ AK_MAKE_NONCOPYABLE(ElementPublication);+ AK_MAKE_NONMOVABLE(ElementPublication);++public:+ // The facts an element takes from its parent where it has none of its own.+ struct Inherited {+ DOM::Element::Directionality directionality { DOM::Element::Directionality::Ltr };+ StyleAtomID language_atom;+ bool is_in_user_agent_shadow_tree { false };+ };++ ElementPublication(StyleEngine& style_engine, DOM::Document const& document)+ : m_style_engine(style_engine)+ , m_focus_within_chain(document)+ , m_hovered_element(hovered_element_of(document.hovered_node()))+ , m_ltr_atom(style_engine.intern_atom("ltr"_utf16_fly_string))+ , m_rtl_atom(style_engine.intern_atom("rtl"_utf16_fly_string))+ {+ }++ ~ElementPublication()+ {+ VERIFY(m_rows.is_empty());+ // NB: A tag read now may not be final, as an XML parser sets attributes after insertion, so none is left cached.+ for (auto element : m_elements_asked_their_language)+ element->invalidate_lang_value_of_inclusive_ancestors();+ }++ ElementKind const& kind_of(DOM::Element const& element)+ {+ auto& kind = m_kinds.slot_of(element.qualified_name_identity());+ if (kind.name != element.qualified_name_identity())+ kind = element_kind_of(m_style_engine, element);+ return kind;+ }++ PublishedAttributeName const& attribute_name(DOM::QualifiedName const& name)+ {+ auto& slot = m_attribute_names.slot_of(name.identity());+ if (slot.name == name.identity())+ return slot;+ auto atom = m_style_engine.intern_attribute_name(name.local_name(), name.namespace_());+ slot = {+ .name = name.identity(),+ .atom = atom,+ .is_known_unread = m_style_engine.attribute_is_known_unread(atom),+ .is_lang = name.local_name() == HTML::AttributeNames::lang,+ .is_dir = name.local_name() == HTML::AttributeNames::dir,+ };+ if (slot.is_known_unread)+ m_style_engine.note_attribute_withheld(name.local_name(), name.namespace_());+ return slot;+ }++ // What the element's parent passes on, if it has one. Elements come mostly in tree order, so most parents were+ // just published, and most of the rest are asked once for all the children that follow.+ Optional<Inherited> inherited_by(DOM::Element& element)+ {+ auto* parent = element.parent_or_shadow_host_element();+ while (!m_elements_to_parent.is_empty() && m_elements_to_parent.last().element.ptr() != parent)+ m_elements_to_parent.take_last();+ if (!parent)+ return {};+ if (m_elements_to_parent.is_empty()) {+ auto const* shadow_root = parent->containing_shadow_root().ptr();+ published(*parent, {+ .directionality = parent->directionality(),+ .language_atom = language_atom_asked_of(*parent),+ .is_in_user_agent_shadow_tree = shadow_root && shadow_root->is_user_agent_internal(),+ });+ }+ return m_elements_to_parent.last().inherited;+ }++ void published(DOM::Element& element, Inherited inherited)+ {+ m_elements_to_parent.append({ element, inherited });+ }++ // The tag is computed afresh. One cached where the element used to be says nothing about where it arrives, as a+ // subtree that moves while detached is never reached by the walk a `lang` change runs.+ StyleAtomID language_atom_asked_of(DOM::Element& element)+ {+ element.invalidate_lang_value();+ auto const language = element.lang_view();+ m_elements_asked_their_language.append(element);+ return language.has_value() ? m_style_engine.intern_language_atom(*language) : StyleAtomID {};+ }++ // Where the next row's custom states, classes and attributes go, in that order.+ Vector<u32, 512>& atoms() { return m_atoms; }++ void append_row(DOM::Element& element, StyleEngineFFI::FfiElementArrivalRow const& row)+ {+ m_rows.append(row);+ m_elements.append(element);+ }++ bool block_is_full() const { return m_rows.size() >= rows_per_block; }++ // Writes the rows of the block, and answers its elements for what follows the rows.+ ReadonlySpan<GC::Ref<DOM::Element>> write_block()+ {+ m_style_engine.record_element_arrival_rows(m_rows.span(), m_atoms.span());+ m_rows.clear_with_capacity();+ m_atoms.clear_with_capacity();+ return m_elements.span();+ }++ void forget_block() { m_elements.clear_with_capacity(); }++ SelectorMatching::FocusWithinChain const& focus_within_chain() const { return m_focus_within_chain; }+ DOM::Element const* hovered_element() const { return m_hovered_element.ptr(); }+ StyleAtomID directionality_atom(DOM::Element::Directionality directionality) const+ {+ return directionality == DOM::Element::Directionality::Rtl ? m_rtl_atom : m_ltr_atom;+ }++private:+ static constexpr size_t rows_per_block = 256;++ struct AncestorRow {+ GC::Ptr<DOM::Element const> element;+ Inherited inherited;+ };++ StyleEngine& m_style_engine;+ SelectorMatching::FocusWithinChain m_focus_within_chain;+ GC::Ptr<DOM::Element const> m_hovered_element;+ StyleAtomID m_ltr_atom;+ StyleAtomID m_rtl_atom;+ TableByName<ElementKind, 64> m_kinds;+ TableByName<PublishedAttributeName, 32> m_attribute_names;+ GC::ConservativeVector<AncestorRow, 32> m_elements_to_parent;+ GC::RootVector<GC::Ref<DOM::Element>> m_elements_asked_their_language;+ Vector<StyleEngineFFI::FfiElementArrivalRow> m_rows;+ Vector<u32, 512> m_atoms;+ GC::RootVector<GC::Ref<DOM::Element>> m_elements;+};++// What the engine reads decides what of the elements is withheld from it, which the host asks the engine where a rule+// it queued since its last job leaves it not knowing.+static void learn_what_the_engine_reads(StyleEngine& style_engine, Layout::BegunRead const& read)+{+ StyleEngineFFI::document_host_learn_selector_requirements(style_engine.host(), &read);+}+// A subtree waiting to arrive, and whether its root arrives alone, its children waiting on in a display:none subtree.struct PendingArrivalRoot {GC::Ref<DOM::Node> node;@@ -588,7 +851,10 @@ struct PendingArrivalRoot {// Every node in the subtrees waiting to arrive, in tree order, takes its identity, and every element records its// arrival. The subtrees are taken in together, so each identity is assigned before the first arrival is recorded:// an arrival names its parent and siblings, and a sibling that waited in another subtree has arrived by then.-static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<PendingArrivalRoot> roots)+//+// With a publication, an element's features arrive with it, in one write of its row. An element not connected yet+// stands in the way of that, as its insertion steps are still running, and waits as it would without a publication.+static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<PendingArrivalRoot> roots, ElementPublication* publication){auto& style_computer = document.style_computer();auto& style_engine = style_computer.style_engine();@@ -694,11 +960,9 @@ static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<Pendin// The elements of the subtrees arrive in the engine's tree in one write, each standing in no tree before.Vector<StyleEngineFFI::FfiArrivingElement, 64> arriving_elements;- arriving_elements.ensure_capacity(element_count);for (size_t index = 0; index < arrivals.size(); ++index) {auto const& arrival = arrivals[index];- if (index + nodes_ahead < arrivals.size())- prefetch_node(*arrivals[index + nodes_ahead].node);+ prefetch_publication_ahead(index, arrivals.size(), [&](size_t index) { return arrivals[index].node.ptr(); });auto* element = as_if<DOM::Element>(*arrival.node);if (!element)continue;@@ -707,13 +971,21 @@ static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<Pendin// a `:host` or `::slotted()` rule in that tree reach the host instead of the document.if (auto shadow_root = element->shadow_root(); shadow_root && shadow_root->style_node_id() != no_style_node)StyleEngineFFI::style_engine_set_shadow_root(style_engine.host(), element->style_node_id(), shadow_root->style_node_id());- arriving_elements.unchecked_append({+ StyleEngineFFI::FfiArrivingElement arriving {.unique_node_id = static_cast<u64>(element->unique_id().value()),.node = element->style_node_id().value(),.relations = relations_of(*element, style_engine, arrival.tree_scope),- });- style_engine.defer_element_initial_features(*element);+ };+ if (publication && element->is_connected()) {+ style_engine.note_element_awaiting_first_style_computation(*element);+ publish_element_arrival(style_engine, *element, *publication, arriving);+ } else {+ arriving_elements.append(arriving);+ style_engine.defer_element_initial_features(*element);+ }}+ if (publication)+ finish_element_publication(style_engine, *publication);style_engine.record_element_arrivals(arriving_elements.span());for (auto const& slottable : slottables_identified_earlier)record_element_assigned_slot_changed(slottable, nullptr);@@ -922,11 +1194,11 @@ static bool take_listed_arrival_roots(DOM::Document& document, GC::RootVector<GCreturn true;}-void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals hidden_child_arrivals)+static void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals hidden_child_arrivals, Layout::BegunRead const* read){- if (s_taking_in_pending_style_arrivals)- return;auto& style_engine = document.style_computer().style_engine();+ if (style_engine.is_taking_in_pending_arrivals())+ return;// Nodes that waited and left the tree again are counted too, and nothing is left of them to take in.auto arrivals_were_noted = style_engine.has_pending_arrivals();style_engine.forget_pending_arrivals();@@ -943,7 +1215,9 @@ void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals// Only the arrivals of a tracked tree record which children wait.if (!document.style_engine_tracks_tree())hidden_child_arrivals = HiddenChildArrivals::TakeIn;- TemporaryChange taking_in { s_taking_in_pending_style_arrivals, true };+ // NB: Publication can bring another document up to date, as an SVG image's for its natural size, whose own take-in+ // this says nothing about.+ auto taking_in = style_engine.begin_taking_in_pending_arrivals();GC::ConservativeVector<PendingArrivalRoot, 16> roots;{HiddenChildArrivalDecision decision { document, hidden_child_arrivals };@@ -952,7 +1226,13 @@ void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivalsstyle_engine.set_may_have_waiting_children(collect_pending_style_arrival_roots(document, roots, decision));}if (!roots.is_empty() && document.style_engine_tracks_tree()) {- record_subtree_arrivals(document, roots);+ if (read) {+ learn_what_the_engine_reads(style_engine, *read);+ ElementPublication publication { style_engine, document };+ record_subtree_arrivals(document, roots, &publication);+ } else {+ record_subtree_arrivals(document, roots, nullptr);+ }// NB: An element shows a highlight only once it has an identity to be named by, which a hidden child can take// without the tree changing.document.set_needs_highlight_style_update(PseudoElement::Selection);@@ -961,6 +1241,16 @@ void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals}}+void take_in_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals hidden_child_arrivals)+{+ take_in_pending_style_arrivals(document, hidden_child_arrivals, nullptr);+}++void take_in_and_publish_pending_style_arrivals(DOM::Document& document, HiddenChildArrivals hidden_child_arrivals, Layout::BegunRead const& read)+{+ take_in_pending_style_arrivals(document, hidden_child_arrivals, &read);+}+void record_element_associated_with_animation(DOM::Element& element){auto* style_engine = style_engine_for(element);@@ -991,16 +1281,6 @@ void record_waiting_children_may_be_revealed(DOM::Element& element)note_waiting_children_may_arrive(*style_engine, element.document());}-// A node that is no element hovers the element it is inside of.-static DOM::Element const* hovered_element_of(DOM::Node const* node)-{- if (!node)- return nullptr;- if (auto const* element = as_if<DOM::Element>(*node))- return element;- return node->parent_element().ptr();-}-// The element-backed pseudo-element an element in its host's shadow tree stands for, as one plus// its kind, or zero.static u8 associated_pseudo_kind_plus_one(DOM::Element const& element)@@ -1009,211 +1289,12 @@ static u8 associated_pseudo_kind_plus_one(DOM::Element const& element)return pseudo_element.has_value() ? static_cast<u8>(to_underlying(*pseudo_element) + 1) : 0;}-// The questions about an element that elements of its name answer differently.-enum class ElementOpenFacts : u16 {- None = 0,- Input = 1 << 0,- Audio = 1 << 1,- SvgRoot = 1 << 2,- SvgSymbol = 1 << 3,- SvgElement = 1 << 4,- Body = 1 << 5,- Heading = 1 << 6,- ReplacedContent = 1 << 7,- OwnDirectionality = 1 << 8,- PresentationalHints = 1 << 9,-};-AK_ENUM_BITWISE_OPERATORS(ElementOpenFacts);--static ElementOpenFacts element_open_facts(DOM::Element const&);-static u32 kind_style_adjustment_facts(DOM::Element const&);-static u32 kind_construction_facts(DOM::Element const&);-static u32 open_style_adjustment_facts(DOM::Element const&, ElementOpenFacts);-static u32 open_construction_facts(DOM::Element const&, ElementOpenFacts, bool is_in_user_agent_shadow_tree);--// What an element's qualified name settles. The name picks the element's class, so this holds for every element of it,-// and only what the name leaves open is asked of each.-struct ElementKind {- DOM::QualifiedName::Impl const* name { nullptr };- StyleAtomID tag_atom;- StyleAtomID folded_tag_atom;- StyleAtomID namespace_atom;- u32 adjustment_facts { 0 };- u32 construction_facts { 0 };- ElementOpenFacts open_facts { ElementOpenFacts::None };- SelectorMatching::ElementStateQuestions state_questions { SelectorMatching::ElementStateQuestions::None };- bool is_slot { false };-};--static ElementKind element_kind_of(StyleEngine& style_engine, DOM::Element const& element)-{- auto tag_atom = style_engine.intern_atom(element.local_name());- ElementKind kind {- .name = element.qualified_name_identity(),- .tag_atom = tag_atom,- .folded_tag_atom = tag_atom,- .namespace_atom = {},- .adjustment_facts = kind_style_adjustment_facts(element),- .construction_facts = kind_construction_facts(element),- .open_facts = element_open_facts(element),- .state_questions = SelectorMatching::element_state_questions(element),- .is_slot = is<HTML::HTMLSlotElement>(element),- };- if (auto folded_name = element.local_name().to_ascii_lowercase(); folded_name != element.local_name())- kind.folded_tag_atom = style_engine.intern_atom(folded_name);- if (auto const& namespace_uri = element.namespace_uri(); namespace_uri.has_value() && !namespace_uri->is_empty())- kind.namespace_atom = style_engine.intern_atom(*namespace_uri);- return kind;-}--// What one attribute name settles, which is the same on every element that has it.-struct PublishedAttributeName {- DOM::QualifiedName::Impl const* name { nullptr };- StyleAtomID atom;- bool is_known_unread { false };- bool is_lang { false };- bool is_dir { false };-};--// A table a batch keeps of what it found out by name. Names repeat, so a slot a name shares with another only costs-// asking again.-template<typename Entry, size_t slot_count>-class TableByName {-public:- static_assert((slot_count & (slot_count - 1)) == 0);-- Entry& slot_of(DOM::QualifiedName::Impl const* name)- {- // Fibonacci hashing: the high bits of the product depend on all of an allocation's address.- auto hash = (reinterpret_cast<FlatPtr>(name) * 0x9e3779b97f4a7c15ull) >> (64 - count_trailing_zeroes(slot_count));- return m_slots[hash];- }--private:- Array<Entry, slot_count> m_slots {};-};--// One batch of elements publishing their initial features. Nothing but the batch changes the DOM or what the engine-// reads while it lives, so what one element answers holds for the next one asking.-class ElementPublication {- AK_MAKE_NONCOPYABLE(ElementPublication);- AK_MAKE_NONMOVABLE(ElementPublication);--public:- // The facts an element takes from its parent where it has none of its own.- struct Inherited {- DOM::Element::Directionality directionality { DOM::Element::Directionality::Ltr };- StyleAtomID language_atom;- bool is_in_user_agent_shadow_tree { false };- };-- ElementPublication(StyleEngine& style_engine, DOM::Document const& document)- : m_style_engine(style_engine)- , m_focus_within_chain(document)- , m_hovered_element(hovered_element_of(document.hovered_node()))- , m_ltr_atom(style_engine.intern_atom("ltr"_utf16_fly_string))- , m_rtl_atom(style_engine.intern_atom("rtl"_utf16_fly_string))- {- }-- ~ElementPublication()- {- // NB: A tag read now may not be final, as an XML parser sets attributes after insertion, so none is left cached.- for (auto element : m_elements_asked_their_language)- element->invalidate_lang_value_of_inclusive_ancestors();- }-- ElementKind const& kind_of(DOM::Element const& element)- {- auto& kind = m_kinds.slot_of(element.qualified_name_identity());- if (kind.name != element.qualified_name_identity())- kind = element_kind_of(m_style_engine, element);- return kind;- }-- PublishedAttributeName const& attribute_name(DOM::QualifiedName const& name)- {- auto& slot = m_attribute_names.slot_of(name.identity());- if (slot.name == name.identity())- return slot;- auto atom = m_style_engine.intern_attribute_name(name.local_name(), name.namespace_());- slot = {- .name = name.identity(),- .atom = atom,- .is_known_unread = m_style_engine.attribute_is_known_unread(atom),- .is_lang = name.local_name() == HTML::AttributeNames::lang,- .is_dir = name.local_name() == HTML::AttributeNames::dir,- };- if (slot.is_known_unread)- m_style_engine.note_attribute_withheld(name.local_name(), name.namespace_());- return slot;- }-- // What the element's parent passes on, if it has one. Elements come mostly in tree order, so most parents were- // just published, and most of the rest are asked once for all the children that follow.- Optional<Inherited> inherited_by(DOM::Element& element)- {- auto* parent = element.parent_or_shadow_host_element();- while (!m_elements_to_parent.is_empty() && m_elements_to_parent.last().element.ptr() != parent)- m_elements_to_parent.take_last();- if (!parent)- return {};- if (m_elements_to_parent.is_empty()) {- auto const* shadow_root = parent->containing_shadow_root().ptr();- published(*parent, {- .directionality = parent->directionality(),- .language_atom = language_atom_asked_of(*parent),- .is_in_user_agent_shadow_tree = shadow_root && shadow_root->is_user_agent_internal(),- });- }- return m_elements_to_parent.last().inherited;- }-- void published(DOM::Element& element, Inherited inherited)- {- m_elements_to_parent.append({ element, inherited });- }-- // The tag is computed afresh. One cached where the element used to be says nothing about where it arrives, as a- // subtree that moves while detached is never reached by the walk a `lang` change runs.- StyleAtomID language_atom_asked_of(DOM::Element& element)- {- element.invalidate_lang_value();- auto const language = element.lang_view();- m_elements_asked_their_language.append(element);- return language.has_value() ? m_style_engine.intern_language_atom(*language) : StyleAtomID {};- }-- SelectorMatching::FocusWithinChain const& focus_within_chain() const { return m_focus_within_chain; }- DOM::Element const* hovered_element() const { return m_hovered_element.ptr(); }- StyleAtomID directionality_atom(DOM::Element::Directionality directionality) const- {- return directionality == DOM::Element::Directionality::Rtl ? m_rtl_atom : m_ltr_atom;- }--private:- struct AncestorRow {- GC::Ptr<DOM::Element const> element;- Inherited inherited;- };-- StyleEngine& m_style_engine;- SelectorMatching::FocusWithinChain m_focus_within_chain;- GC::Ptr<DOM::Element const> m_hovered_element;- StyleAtomID m_ltr_atom;- StyleAtomID m_rtl_atom;- TableByName<ElementKind, 64> m_kinds;- TableByName<PublishedAttributeName, 32> m_attribute_names;- GC::ConservativeVector<AncestorRow, 32> m_elements_to_parent;- GC::RootVector<GC::Ref<DOM::Element>> m_elements_asked_their_language;-};-// Publish every selector-visible fact intrinsic to one element.//// An element arrives with facts already true of it, and the engine has heard none of them. They are// published as ordinary deltas from absent, so the projection learns them by exactly the same path// later mutations take rather than by a second one.-static ElementKind const& publish_element_selector_features(StyleEngine& style_engine, DOM::Element& element, ElementPublication& publication)+static void publish_element_selector_features(StyleEngine& style_engine, DOM::Element& element, ElementPublication& publication, Optional<StyleEngineFFI::FfiArrivingElement const&> arriving){auto node = element.style_node_id();// Everything the element's selectors test arrives as one row, which the engine writes in one go.@@ -1222,10 +1303,16 @@ static ElementKind const& publish_element_selector_features(StyleEngine& style_eStyleAtomID id_atom;if (auto const& id = element.id(); id.has_value())id_atom = intern_id_or_class_atom(style_engine, element, *id);- Vector<StyleAtomID, 8> classes;+ auto& atoms = publication.atoms();+ auto atoms_before = atoms.size();+ if (auto states = element.custom_state_set()) {+ for (auto const& state : states->states())+ atoms.append(style_engine.intern_atom(state).value());+ }+ auto custom_state_count = atoms.size() - atoms_before;for (auto const& class_name : element.class_names())- classes.append(intern_id_or_class_atom(style_engine, element, class_name));- Vector<StyleAtomID, 16> attributes;+ atoms.append(intern_id_or_class_atom(style_engine, element, class_name).value());+ auto class_count = atoms.size() - atoms_before - custom_state_count;bool has_own_language = false;bool has_own_directionality = has_flag(kind.open_facts, ElementOpenFacts::OwnDirectionality);for (auto const& [name, value] : element.attribute_list()) {@@ -1234,9 +1321,10 @@ static ElementKind const& publish_element_selector_features(StyleEngine& style_ehas_own_directionality |= attribute_name.is_dir;if (attribute_name.is_known_unread)continue;- attributes.append(attribute_name.atom);- attributes.append(style_engine.intern_attribute_value(attribute_name.atom, value));+ atoms.append(attribute_name.atom.value());+ atoms.append(style_engine.intern_attribute_value(attribute_name.atom, value).value());}+ auto attribute_count = (atoms.size() - atoms_before - custom_state_count - class_count) / 2;bool has_nonempty_text_child = false;for (GC::Ptr<DOM::Node const> child = element.first_child(); child; child = child->next_sibling()) {@@ -1286,12 +1374,8 @@ static ElementKind const& publish_element_selector_features(StyleEngine& style_eif (has_flag(kind.open_facts, ElementOpenFacts::Heading))heading_level = static_cast<u8>(min(as<HTML::HTMLHeadingElement>(element).heading_level(), 255u));- Vector<StyleAtomID> custom_states;- if (auto states = element.custom_state_set()) {- for (auto const& state : states->states())- custom_states.append(style_engine.intern_atom(state));- }- style_engine.record_element_arrival({+ publication.append_row(element, {+ .arrival = {.states = state_bits,.node = node.value(),.tag_atom = kind.tag_atom.value(),@@ -1300,9 +1384,9 @@ static ElementKind const& publish_element_selector_features(StyleEngine& style_e.namespace_atom = kind.namespace_atom.value(),.language_atom = own.language_atom.value(),.directionality_atom = publication.directionality_atom(own.directionality).value(),- .custom_state_count = static_cast<u32>(custom_states.size()),- .class_count = static_cast<u32>(classes.size()),- .attribute_count = static_cast<u32>(attributes.size() / 2),+ .custom_state_count = static_cast<u32>(custom_state_count),+ .class_count = static_cast<u32>(class_count),+ .attribute_count = static_cast<u32>(attribute_count),.heading_level = heading_level,.is_slot = kind.is_slot,.has_text_content = has_nonempty_text_child,@@ -1311,11 +1395,10 @@ static ElementKind const& publish_element_selector_features(StyleEngine& style_e.adjustment_facts = kind.adjustment_facts | open_style_adjustment_facts(element, kind.open_facts),.construction_facts = kind.construction_facts | open_construction_facts(element, kind.open_facts, own.is_in_user_agent_shadow_tree),},- custom_states, classes, attributes);- // The hover follows the element the events hover to where it arrives, the place a move took it to.- if (&element == publication.hovered_element())- style_engine.record_hover(node);- return kind;+ .unique_node_id = arriving.has_value() ? arriving->unique_node_id : 0,+ .relations = arriving.has_value() ? arriving->relations : detached_relations(),+ .has_relations = arriving.has_value(),+ });}// An element's hints move when something beside its own attributes that they are mapped from@@ -1927,25 +2010,33 @@ static StyleAtomID intern_id_or_class_atom(StyleEngine& style_engine, DOM::Elemereturn style_engine.intern_atom(name);}-static void record_element_initial_features(DOM::Element& element, ElementPublication& publication)+// Writes the block of rows the publication gathered, and what is published of their elements beside their rows.+static void finish_element_publication(StyleEngine& style_engine, ElementPublication& publication){- auto* style_engine = style_engine_for_identified(element);- if (!style_engine)- return;-- auto const& kind = publish_element_selector_features(*style_engine, element, publication);-- if (auto const& id = element.id(); id.has_value())- StyleEngineFFI::style_engine_set_element_id_name(style_engine->host(), element.style_node_id(), style_engine->intern_atom(*id));+ for (auto element : publication.write_block()) {+ // The hover follows the element the events hover to where it arrives, the place a move took it to.+ if (element.ptr() == publication.hovered_element())+ style_engine.record_hover(element->style_node_id());+ if (auto const& id = element->id(); id.has_value())+ StyleEngineFFI::style_engine_set_element_id_name(style_engine.host(), element->style_node_id(), style_engine.intern_atom(*id));+ if (!element->part_names().is_empty())+ record_element_parts_changed(element);+ if (element->has_inline_style_declarations())+ record_element_inline_style_properties(element);+ auto const& kind = publication.kind_of(element);+ if (element_has_presentational_hints_to_publish(element, kind))+ StyleComputer::collect_presentational_hint_properties({ element });+ if (has_flag(kind.open_facts, ElementOpenFacts::ReplacedContent))+ publish_element_replaced_content_input(element, style_engine);+ }+ publication.forget_block();+}- if (!element.part_names().is_empty())- record_element_parts_changed(element);- if (element.has_inline_style_declarations())- record_element_inline_style_properties(element);- if (element_has_presentational_hints_to_publish(element, kind))- StyleComputer::collect_presentational_hint_properties({ element });- if (has_flag(kind.open_facts, ElementOpenFacts::ReplacedContent))- publish_element_replaced_content_input(element, *style_engine);+static void publish_element_arrival(StyleEngine& style_engine, DOM::Element& element, ElementPublication& publication, Optional<StyleEngineFFI::FfiArrivingElement const&> arriving)+{+ publish_element_selector_features(style_engine, element, publication, arriving);+ if (publication.block_is_full())+ finish_element_publication(style_engine, publication);}void publish_pending_element_features(StyleEngine& style_engine, StyleComputer& style_computer, Layout::BegunRead const& read)@@ -1953,26 +2044,10 @@ void publish_pending_element_features(StyleEngine& style_engine, StyleComputer&auto nodes = style_engine.take_deferred_element_initial_features();if (nodes.is_empty())return;- // What the engine reads decides what of the elements is withheld from it, which the host asks the engine where a- // rule it queued since its last job leaves it not knowing.- StyleEngineFFI::document_host_learn_selector_requirements(style_engine.host(), &read);+ learn_what_the_engine_reads(style_engine, read);ElementPublication publication { style_engine, style_computer.document() };for (size_t index = 0; index < nodes.size(); ++index) {- // OPTIMIZATION: Most of an element's publication is first reading memory no one touched since the element was- // parsed. Asking for it ahead turns waiting for one element at a time into overlapping waits. Each- // stage reads what the one before it asked for, so the distances shrink along the chain.- if (index + 2 * nodes_ahead < nodes.size()) {- if (auto ahead = style_computer.node_for_style_node(nodes[index + 2 * nodes_ahead]))- prefetch_node(*ahead);- }- if (index + nodes_ahead < nodes.size()) {- if (auto ahead = style_computer.element_for_style_node(nodes[index + nodes_ahead]))- __builtin_prefetch(ahead->inline_style_declarations());- }- if (index + nodes_ahead / 2 < nodes.size()) {- if (auto ahead = style_computer.element_for_style_node(nodes[index + nodes_ahead / 2]); ahead && ahead->inline_style_declarations())- Parser::ValueParserFFI::rust_declaration_block_prefetch_header(ahead->inline_style_declarations());- }+ prefetch_publication_ahead(index, nodes.size(), [&](size_t index) { return style_computer.node_for_style_node(nodes[index]).ptr(); });auto node = nodes[index];auto element = style_computer.element_for_style_node(node);// NB: An element that left the tree gave up its mark. Its identity may have gone to an element since, which is@@ -1988,8 +2063,10 @@ void publish_pending_element_features(StyleEngine& style_engine, StyleComputer&continue;}element->set_initial_style_features_pending(false);- record_element_initial_features(*element, publication);+ if (style_engine_for_identified(*element))+ publish_element_arrival(style_engine, *element, publication);}+ finish_element_publication(style_engine, publication);}void record_node_moved_in_dom_order(DOM::Node& node, DOM::Node const& old_parent)diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.h b/Libraries/LibWeb/CSS/StyleEngineInput.hindex 9436266a4dc..bb42f8ab96f 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.h+++ b/Libraries/LibWeb/CSS/StyleEngineInput.h@@ -71,6 +71,8 @@ enum class HiddenChildArrivals {MayWait,};WEB_API void take_in_pending_style_arrivals(DOM::Document&, HiddenChildArrivals = HiddenChildArrivals::TakeIn);+// The same, with the features of the elements that arrive published as they do, in one write of each element's row.+WEB_API void take_in_and_publish_pending_style_arrivals(DOM::Document&, HiddenChildArrivals, Layout::BegunRead const&);// Called once an element whose children, or some descendant's, wait to arrive in a display:none// subtree takes a style that is not display:none, or an animation, either of which may reveal them.diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cppindex 0625561ecea..714e69edc07 100644--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp@@ -549,7 +549,7 @@ static void update_style(Layout::BegunRead const& read, DOM::Document& document,// since the last update under the identities they take here.// NB: What connected beside the transaction that flew is the next transaction's.if constexpr (!drains_flown_transaction) {- take_in_pending_style_arrivals(document, HiddenChildArrivals::MayWait);+ take_in_and_publish_pending_style_arrivals(document, HiddenChildArrivals::MayWait, read);document.update_highlight_style_observability();}StyleUpdateScope style_update_scope { document.style_computer() };@@ -795,7 +795,7 @@ static bool update_style_for_element(Layout::BegunRead const& read, DOM::Documenauto const& read_element = abstract_element.element();auto read_may_need_waiting_children = document.descendant_style_arrival_pending()&& read_element.has_inclusive_ancestor_with_display_none_ignoring_animations();- take_in_pending_style_arrivals(document, read_may_need_waiting_children ? HiddenChildArrivals::TakeIn : HiddenChildArrivals::MayWait);+ take_in_and_publish_pending_style_arrivals(document, read_may_need_waiting_children ? HiddenChildArrivals::TakeIn : HiddenChildArrivals::MayWait, read);document.update_highlight_style_observability();StyleUpdateScope style_update_scope { document.style_computer() };// Refresh computed properties for an abstract element. An ordinary read first consumes the complete exactdiff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex 0a4dfd820ea..df2c6a6aa70 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -544,7 +544,7 @@ pub struct FfiTreeRelations {}impl FfiTreeRelations {- fn decode(self) -> TreeRelations {+ pub(super) fn decode(self) -> TreeRelations {TreeRelations {parent: StyleNodeID::from_raw(self.parent),previous_element_sibling: StyleNodeID::from_raw(self.previous_element_sibling),@@ -1040,7 +1040,7 @@ impl super::projection_writes::ProjectionWrites<'_> {return;};*atoms = rest;- self.record_element_arrival(node, &element_arrival_facts(arrival, arrival_atoms));+ self.record_element_arrival(node, &element_arrival_facts(arrival, arrival_atoms), None);}/// Write the local fact the host published as `delta`.diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rsindex 2267a30e8a6..f2252dd27f7 100644--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs@@ -546,39 +546,66 @@ pub extern "C" fn style_engine_stage_tree_delta(host: &DocumentHost, delta: &Ffi.record_ffi_tree_delta(delta);}-/// Writes the arrival of an element, with every fact its selectors test, into its projection row in one go, and keeps-/// what it arrives with beside its row for the host's next barrier.-///-/// # Safety-/// `host` must be a live document host, on its document's thread, `custom_states` and `classes` must point at-/// `arrival.custom_state_count` and `arrival.class_count` readable atoms, and `attributes` at-/// `2 * arrival.attribute_count` readable atoms, a name and a value per attribute.+/// An element's facts arriving, with how it stands in the tree where it arrives there with them. Elements that took+/// their place in the tree earlier arrive with `has_relations` false.+#[repr(C)]+pub struct FfiElementArrivalRow {+ pub arrival: FfiElementArrival,+ pub unique_node_id: u64,+ pub relations: FfiTreeRelations,+ pub has_relations: bool,+}++/// Writes the arrival of each element of `rows`, with every fact its selectors test, into its projection row in one go,+/// and keeps what it arrives with beside its row for the host's next barrier. `atoms` holds each row's custom states,+/// classes and attributes in turn.#[unsafe(no_mangle)]-pub unsafe extern "C" fn style_engine_stage_element_arrival(+pub extern "C" fn style_engine_stage_element_arrival_rows(host: &DocumentHost,- arrival: &FfiElementArrival,- custom_states: *const u32,- classes: *const u32,- attributes: *const u32,+ rows: ak::Span<'_, FfiElementArrivalRow>,+ atoms: ak::Span<'_, u32>,) {- let Some(node) = StyleNodeID::from_raw(arrival.node) else {- debug_assert!(false, "an element arrival named an invalid style node");- return;- };- let mut atoms: smallvec::SmallVec<[u32; 32]> = smallvec::SmallVec::new();- // SAFETY: Guaranteed by the caller.- unsafe {- atoms.extend_from_slice(borrow(custom_states, arrival.custom_state_count as usize));- atoms.extend_from_slice(borrow(classes, arrival.class_count as usize));- atoms.extend_from_slice(borrow(attributes, 2 * arrival.attribute_count as usize));- }+ let rows = rows.as_slice();+ let mut atoms = atoms.as_slice();let memo = host.engine_memo();- memo.projection- .borrow_mut()- .writes()- .record_element_arrival(node, &super::bridge::element_arrival_facts(arrival, &atoms));- if arrival.associated_pseudo_kind_plus_one != 0 {- memo.unpublished.borrow_mut().backing_elements.push(node);+ {+ let mut projection = memo.projection.borrow_mut();+ let mut writes = projection.writes();+ let mut unpublished = memo.unpublished.borrow_mut();+ for row in rows {+ let arrival = &row.arrival;+ let count = arrival.custom_state_count as usize+ + arrival.class_count as usize+ + 2 * arrival.attribute_count as usize;+ let (row_atoms, rest) = atoms.split_at(count);+ atoms = rest;+ let Some(node) = StyleNodeID::from_raw(arrival.node) else {+ debug_assert!(false, "an element arrival named an invalid style node");+ continue;+ };+ let relations = row.has_relations.then(|| row.relations.decode());+ writes.record_element_arrival(+ node,+ &super::bridge::element_arrival_facts(arrival, row_atoms),+ relations,+ );+ if arrival.associated_pseudo_kind_plus_one != 0 {+ unpublished.backing_elements.push(node);+ }+ }+ }+ let ids: Box<[(StyleNodeID, i64)]> = rows+ .iter()+ .filter(|row| row.has_relations)+ .filter_map(|row| {+ Some((+ StyleNodeID::from_raw(row.arrival.node)?,+ row.unique_node_id.cast_signed(),+ ))+ })+ .collect();+ if !ids.is_empty() {+ host.queue_change(ArenaChange::Engine(EngineWrite::ElementUniqueNodeIds(ids)));}}diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/projection.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/projection.rsindex 6dd8d895aad..c8cac4975e8 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/projection.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/projection.rs@@ -814,6 +814,8 @@ pub(in crate::css::style) struct Projection {/// Everything an element arrives with that its selectors test, which its row takes in one write.pub(in crate::css::style) struct ArrivalFacts<'a> {+ /// How the element stands in the tree, where it arrives there with its facts.+ pub relations: Option<TreeRelations>,pub tag: StyleAtomID,/// The ASCII-lowercase folding of the tag, or the tag itself where folding leaves it as it is.pub folded_tag: StyleAtomID,diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/projection/writer.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/projection/writer.rsindex b8afc0be8d5..27cbf2cfc3b 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/projection/writer.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/projection/writer.rs@@ -659,6 +659,7 @@ impl ProjectionWriter {/// Write every fact an element arrives with in one edit of its row: the facts it names are/// set, its classes and attributes join any its row holds, and its states join those set.+ /// Relations it arrives with are written in the same edit, as [`Self::write_relations`] writes them.pub(in crate::css::style) fn write_arrival_facts(&mut self, node: StyleNodeID, arrival: &ArrivalFacts<'_>) {let empty = FactLists::default();let mut lists = self@@ -694,11 +695,28 @@ impl ProjectionWriter {true => StyleAtomID::NONE,false => arrival.folded_tag,};+ if let Some(relations) = arrival.relations {+ lists.tree_scope = relations.tree_scope;+ lists.assigned_slot = relations.assigned_slot;+ }let lists = Self::make_lists(&mut self.recent_lists, &self.catalogs.name_forms, lists);+ if arrival.relations.is_some() {+ self.relations_written.insert(node, node.element_slot());+ self.note_links_written(node);+ }if arrival.states.contains(StateFact::Disabled) {self.note_disabled(node, true);}- self.edit_facts(node, |facts| {+ self.edit(node, |row| {+ if let Some(relations) = arrival.relations {+ assert!(row.live(), "linking an identity that is not live");+ row.links.parent = relations.parent;+ row.links.previous_element_sibling = relations.previous_element_sibling;+ row.links.next_element_sibling = relations.next_element_sibling;+ row.facts.set_flag(ElementFacts::CONNECTED, true);+ }+ let facts = &mut row.facts;+ facts.set_flag(ElementFacts::RESIDENT, true);facts.tag = arrival.tag;facts.set_flag(ElementFacts::IS_SLOT, arrival.is_slot);facts.set_flag(ElementFacts::HAS_TEXT_CONTENT, arrival.has_text_content);@@ -707,6 +725,7 @@ impl ProjectionWriter {facts.heading_level = arrival.heading_level;}facts.lists = lists;+ facts.refresh_dispatch_bloom();});}diff --git a/Libraries/LibWeb/Rust/src/css/style/projection_writes.rs b/Libraries/LibWeb/Rust/src/css/style/projection_writes.rsindex 1e558b967ee..5a21d2b5333 100644--- a/Libraries/LibWeb/Rust/src/css/style/projection_writes.rs+++ b/Libraries/LibWeb/Rust/src/css/style/projection_writes.rs@@ -97,6 +97,12 @@ impl ProjectionWrites<'_> {/// Stage one structural delta and the neighbor rows it derives.fn stage_tree_delta(&mut self, node: StyleNodeID, old: Option<TreeRelations>, new: Option<TreeRelations>) {+ self.stage_tree_neighbors(node, old, new);+ self.stage_tree_row(node, old, new);+ }++ /// Stage the neighbor rows a structural delta derives.+ fn stage_tree_neighbors(&mut self, node: StyleNodeID, old: Option<TreeRelations>, new: Option<TreeRelations>) {if let Some(old) = old {if let Some(previous) = old.previous_element_sibling {self.stage_connected_tree_row(previous, |relations| {@@ -125,7 +131,6 @@ impl ProjectionWrites<'_> {});}}- self.stage_tree_row(node, old, new);}#[inline]@@ -193,12 +198,18 @@ impl ProjectionWrites<'_> {// -- Facts ---------------------------------------------------------------------------------- /// Write every fact an element arrives with in one edit of its row.+ /// Write every fact an element arrives with in one edit of its row, with how it stands in the tree where that+ /// arrives with them.pub(in crate::css::style) fn record_element_arrival(&mut self,node: StyleNodeID,arrival: &ElementArrivalFacts<'_>,+ relations: Option<TreeRelations>,) {+ if relations.is_some() {+ self.stage_tree_neighbors(node, None, relations);+ self.count(InputKey::TreeRelations(node));+ }let (custom_states, rest) = arrival.atoms.split_at(arrival.custom_state_count);let (classes, attributes) = rest.split_at(arrival.class_count);let custom_states = StyleAtomID::slice_of_raw(custom_states);@@ -219,6 +230,7 @@ impl ProjectionWrites<'_> {self.projection.write_arrival_facts(node,&ArrivalFacts {+ relations,tag: arrival.tag,folded_tag: arrival.folded_tag,namespace: arrival.namespace,