Skip to content

Truly independent web browser

LibWeb: List the elements waiting to publish features in a vector

Andreas Kling2ffe58478326
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
index f2a991b2dc2..a2808d0e8a8 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
@@ -99,7 +99,21 @@ void StyleEngine::mint_text_style_nodes(Span<StyleNodeID> nodes)
StyleEngineFFI::style_engine_mint_style_nodes(m_render_document->host(), true, raw_nodes);
}
-HashTable<StyleNodeID> StyleEngine::take_deferred_element_initial_features()
+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());
+ if (element.needs_preparation_for_style_computation())
+ m_nodes_awaiting_first_style_computation.set(element.style_node_id());
+}
+
+void StyleEngine::cancel_deferred_element_initial_features(DOM::Element& element)
+{
+ element.set_initial_style_features_pending(false);
+ m_nodes_awaiting_first_style_computation.remove(element.style_node_id());
+}
+
+Vector<StyleNodeID> StyleEngine::take_deferred_element_initial_features()
{
return move(m_nodes_with_pending_initial_features);
}
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index d220ba491e1..5ecf08f6406 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -186,20 +186,11 @@ public:
StyleNodeID mint_style_node();
void mint_style_nodes(Span<StyleNodeID> nodes);
void mint_text_style_nodes(Span<StyleNodeID> nodes);
- void defer_element_initial_features(StyleNodeID style_node)
- {
- m_nodes_with_pending_initial_features.set(style_node);
- m_nodes_awaiting_first_style_computation.set(style_node);
- }
- void cancel_deferred_element_initial_features(StyleNodeID style_node)
- {
- m_nodes_with_pending_initial_features.remove(style_node);
- m_nodes_awaiting_first_style_computation.remove(style_node);
- }
- [[nodiscard]] bool has_deferred_element_initial_features(StyleNodeID style_node) const { return m_nodes_with_pending_initial_features.contains(style_node); }
+ void defer_element_initial_features(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(); }
- HashTable<StyleNodeID> take_deferred_element_initial_features();
+ Vector<StyleNodeID> take_deferred_element_initial_features();
HashTable<StyleNodeID> take_elements_awaiting_first_style_computation();
void set_element_parts(StyleNodeID node, ReadonlySpan<StyleAtomID> names, ReadonlySpan<StyleNodeID> hosts);
@@ -467,7 +458,9 @@ private:
StyleAtomID m_last_language_atom;
u64 m_last_language_atom_generation { 0 };
- HashTable<StyleNodeID> m_nodes_with_pending_initial_features;
+ // 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;
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.cpp
index 156020fec0a..d21cbefc2be 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -96,7 +96,7 @@ static StyleEngine* style_engine_for_identified(DOM::Element& element)
static StyleEngine* style_engine_for_published(DOM::Element& element)
{
auto* style_engine = style_engine_for_identified(element);
- return style_engine && !style_engine->has_deferred_element_initial_features(element.style_node_id()) ? style_engine : nullptr;
+ return style_engine && !element.initial_style_features_pending() ? style_engine : nullptr;
}
// A relation is only nameable if the element on its other end already has an identity. Naming a
@@ -321,26 +321,26 @@ static void record_element_relinked(StyleEngine& style_engine, StyleNodeID node,
void publish_pending_element_features(StyleEngine& style_engine, StyleComputer& style_computer, Layout::BegunRead const& read)
{
- auto nodes = style_engine.take_deferred_element_initial_features().values();
+ auto nodes = style_engine.take_deferred_element_initial_features();
// 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.
if (!nodes.is_empty())
StyleEngineFFI::document_host_learn_selector_requirements(style_engine.host(), &read);
- // Feature postings are ordered by node identity. Publish arrivals in that order so growing
- // their indexes appends members instead of repeatedly searching and splitting posting chunks.
- quick_sort(nodes);
for (auto node : nodes) {
auto element = style_computer.element_for_style_node(node);
- if (!element)
+ // NB: An element that left the tree gave up its mark. Its identity may have gone to an element since, which is
+ // then listed twice and published at the first.
+ if (!element || !element->initial_style_features_pending())
continue;
// An inserted subtree takes its identities before its insertion steps mark its nodes connected, in tree order.
// A step that submits the recorded input on the way, as a <style> writing its sheet to the engine in place
// while a style update flies, finds the elements after it unconnected: their arrivals are submitted, and
// their features wait for the next submit, which folds them onto those arrivals.
if (!element->is_connected()) {
- style_engine.defer_element_initial_features(node);
+ style_engine.defer_element_initial_features(*element);
continue;
}
+ element->set_initial_style_features_pending(false);
record_element_initial_features(*element);
}
}
@@ -715,7 +715,7 @@ static void record_subtree_arrivals(DOM::Document& document, ReadonlySpan<Pendin
.node = element->style_node_id().value(),
.relations = relations_of(*element, style_engine, arrival.tree_scope),
});
- style_engine.defer_element_initial_features(element->style_node_id());
+ style_engine.defer_element_initial_features(*element);
}
style_engine.record_element_arrivals(arriving_elements.span());
for (auto const& slottable : slottables_identified_earlier)
@@ -1876,7 +1876,7 @@ static void record_element_disconnecting(DOM::Element& element, TreeScopeID tree
}
}
- style_engine->cancel_deferred_element_initial_features(node);
+ style_engine->cancel_deferred_element_initial_features(element);
style_engine->record_tree_delta({
.node = node.value(),
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 75e6e52dcbe..9971b320bf2 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -924,6 +924,11 @@ public:
virtual void initialize_element() { }
void prepare_for_style_computation(Badge<CSS::StyleComputer>) { prepare_for_style_computation(); }
+ virtual bool needs_preparation_for_style_computation() const { return false; }
+
+ // Whether the element holds a style node whose initial features the style engine has not been told yet.
+ bool initial_style_features_pending() const { return m_initial_style_features_pending; }
+ void set_initial_style_features_pending(bool value) { m_initial_style_features_pending = value; }
// Unregisters the anchor names this element registered in the tree scope `tree_root` is the root of.
void unregister_anchor_names_before_leaving_tree_scope(Badge<Node>, Node& tree_root);
@@ -1086,6 +1091,7 @@ private:
friend class Attr;
friend class NamedNodeMap;
+ bool m_initial_style_features_pending : 1 { false };
bool m_is_being_activated : 1 { false };
bool m_in_top_layer : 1 { false };
bool m_rendered_in_top_layer : 1 { false };
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h
index 08cb1cbfce6..24d10cd9cab 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.h
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h
@@ -324,6 +324,7 @@ private:
void update_radio_button_group_registration();
virtual void computed_properties_changed() override;
virtual void prepare_for_style_computation() override { create_shadow_tree_if_needed(); }
+ virtual bool needs_preparation_for_style_computation() const override { return true; }
virtual bool is_presentational_hint(Utf16FlyString const&) const override;
virtual void apply_presentational_hints(Vector<CSS::StyleProperty>&) const override;