Skip to content

Truly independent web browser

LibWeb: Keep names of withheld attributes rather than their carriers

Andreas Kling18e393ff3394
diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h
index 6e83311518e..b564206fd74 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.h
+++ b/Libraries/LibWeb/CSS/StyleComputer.h
@@ -253,6 +253,15 @@ public:
void ensure_style_node_slot(StyleNodeID);
void unregister_style_node(StyleNodeID style_node_id);
[[nodiscard]] GC::Ptr<DOM::Element> element_for_style_node(StyleNodeID style_node_id) const;
+ // Every element holding a style node, in the order of the nodes.
+ template<typename Callback>
+ void for_each_element_with_style_node(Callback callback) const
+ {
+ for (auto const& node : m_element_style_nodes) {
+ if (auto* element = as_if<DOM::Element>(node.ptr()))
+ callback(*element);
+ }
+ }
[[nodiscard]] GC::Ptr<DOM::Node> node_for_style_node(StyleNodeID style_node_id) const;
// The node a style node id the render clock's lanes report names, or none where the id left its node since the
// rendering update the lanes follow took them in last: the id may name another node by now, which the lanes, on a
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
index a2808d0e8a8..f961fd42751 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
@@ -263,18 +263,19 @@ void StyleEngine::publish_html_element_namespace(StyleAtomID namespace_atom)
StyleEngine::WithheldFeaturesNowRead StyleEngine::take_withheld_features_now_read(Layout::BegunRead const& read)
{
WithheldFeaturesNowRead now_read;
- if (m_withheld_local_link_states.is_empty() && m_withheld_attribute_carriers.is_empty())
+ if (!m_local_link_state_is_withheld && m_withheld_attribute_names.is_empty())
return now_read;
StyleEngineFFI::document_host_learn_selector_requirements(host(), &read);
- if (!m_withheld_local_link_states.is_empty() && !local_link_is_known_untested())
- now_read.local_link_states = move(m_withheld_local_link_states);
- Vector<WithheldAttributeName> names_now_read;
- for (auto const& [name, carriers] : m_withheld_attribute_carriers) {
+ if (m_local_link_state_is_withheld && !local_link_is_known_untested()) {
+ m_local_link_state_is_withheld = false;
+ now_read.local_link_state = true;
+ }
+ for (auto const& name : m_withheld_attribute_names) {
if (!attribute_is_known_unread(intern_attribute_name(name.local_name, name.namespace_uri)))
- names_now_read.append(name);
+ now_read.attributes.append(name);
}
- for (auto const& name : names_now_read)
- now_read.attributes.set(name, m_withheld_attribute_carriers.take(name).release_value());
+ for (auto const& name : now_read.attributes)
+ m_withheld_attribute_names.remove(name);
return now_read;
}
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index 5ecf08f6406..9a5b7c5ea45 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -283,17 +283,16 @@ public:
};
// NB: An element's identity may have been retired, or taken by another element, by the time a fact withheld from
// it is published: what is published is read from whatever element holds the identity then.
- using WithheldAttributeCarriers = HashMap<WithheldAttributeName, HashTable<StyleNodeID>, WithheldAttributeNameTraits>;
- void note_local_link_state_withheld(StyleNodeID node) { m_withheld_local_link_states.set(node); }
- void note_attribute_withheld(Utf16FlyString const& local_name, Optional<Utf16FlyString> const& namespace_uri, StyleNodeID node)
+ void note_local_link_state_withheld() { m_local_link_state_is_withheld = true; }
+ void note_attribute_withheld(Utf16FlyString const& local_name, Optional<Utf16FlyString> const& namespace_uri)
{
- m_withheld_attribute_carriers.ensure({ local_name, namespace_uri }).set(node);
+ m_withheld_attribute_names.set({ local_name, namespace_uri });
}
// What was withheld that something may read now, which is withheld no longer, with the elements it was withheld
// from.
struct WithheldFeaturesNowRead {
- HashTable<StyleNodeID> local_link_states;
- WithheldAttributeCarriers attributes;
+ bool local_link_state { false };
+ Vector<WithheldAttributeName> attributes;
};
WithheldFeaturesNowRead take_withheld_features_now_read(Layout::BegunRead const&);
@@ -471,8 +470,8 @@ private:
RelationalArrivalKeys m_relational_arrival_keys;
Optional<u64> m_waiting_children_checked_against_keys_version;
bool m_may_have_waiting_children { false };
- HashTable<StyleNodeID> m_withheld_local_link_states;
- WithheldAttributeCarriers m_withheld_attribute_carriers;
+ bool m_local_link_state_is_withheld { false };
+ HashTable<WithheldAttributeName, WithheldAttributeNameTraits> m_withheld_attribute_names;
bool m_css_transitions_may_observe_style_changes { false };
mutable bool m_geometry_read_deferred_transaction { false };
StyleAtomID m_html_element_namespace;
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index d21cbefc2be..d4c31d0cfad 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -1035,7 +1035,7 @@ static void publish_element_selector_features(StyleEngine& style_engine, DOM::El
for (auto const& [name, value] : element.attribute_list()) {
auto name_atom = style_engine.intern_attribute_name(name.local_name(), name.namespace_());
if (style_engine.attribute_is_known_unread(name_atom)) {
- style_engine.note_attribute_withheld(name.local_name(), name.namespace_(), node);
+ style_engine.note_attribute_withheld(name.local_name(), name.namespace_());
continue;
}
attributes.append(name_atom);
@@ -1053,18 +1053,17 @@ static void publish_element_selector_features(StyleEngine& style_engine, DOM::El
auto ask_local_link = SelectorMatching::AskLocalLink::Yes;
if (element.matches_link_pseudo_class() && style_engine.local_link_is_known_untested()) {
ask_local_link = SelectorMatching::AskLocalLink::No;
- style_engine.note_local_link_state_withheld(node);
+ style_engine.note_local_link_state_withheld();
}
auto states = SelectorMatching::element_states(element, ask_local_link);
u64 state_bits = 0;
- for (size_t index = 0; index < to_underlying(PseudoClass::__Count); ++index) {
- auto pseudo_class = static_cast<PseudoClass>(index);
+ states.for_each_set([&](PseudoClass pseudo_class) {
// The engine's hover chain holds `:hover`, which the hover follows to an element that arrives hovered.
- if (pseudo_class == PseudoClass::Hover || !states.get(pseudo_class))
- continue;
+ if (pseudo_class == PseudoClass::Hover)
+ return;
if (auto fact = state_fact_for(pseudo_class); fact.has_value())
state_bits |= static_cast<u64>(1) << to_underlying(*fact);
- }
+ });
// The tag is computed afresh and not left cached. 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, and one
@@ -1585,56 +1584,47 @@ void record_element_construction_facts(DOM::Element& element)
void publish_withheld_element_features(StyleEngine& style_engine, StyleComputer& style_computer, Layout::BegunRead const& read)
{
auto now_read = style_engine.take_withheld_features_now_read(read);
- if (now_read.local_link_states.is_empty() && now_read.attributes.is_empty())
+ if (!now_read.local_link_state && now_read.attributes.is_empty())
return;
- auto element_holding = [&](StyleNodeID node) -> GC::Ptr<DOM::Element> {
- auto element = style_computer.element_for_style_node(node);
- if (!element || !element->is_connected() || element->style_node_id() != node)
- return nullptr;
- return element;
- };
auto local_link = state_fact_for(PseudoClass::LocalLink);
VERIFY(local_link.has_value());
- for (auto node : now_read.local_link_states) {
- if (auto element = element_holding(node); element && element->matches_local_link_pseudo_class())
+ struct NameNowRead {
+ StyleEngine::WithheldAttributeName name;
+ StyleAtomID atom;
+ };
+ Vector<NameNowRead, 2> names_now_read;
+ names_now_read.ensure_capacity(now_read.attributes.size());
+ for (auto const& name : now_read.attributes)
+ names_now_read.unchecked_append({ name, style_engine.intern_attribute_name(name.local_name, name.namespace_uri) });
+ // NB: A name comes to be read once in a document's life, so nothing is kept per element for it. An element whose
+ // initial features still wait reads the names as they are by then.
+ style_computer.for_each_element_with_style_node([&](DOM::Element& element) {
+ if (!element.is_connected() || element.initial_style_features_pending())
+ return;
+ auto node = element.style_node_id();
+ if (now_read.local_link_state && element.matches_local_link_pseudo_class())
style_engine.record_state_delta({ .node = node.value(), .fact = *local_link, .new_value = true });
- }
- // Each element is asked once about every name now read that was withheld from it.
- HashMap<StyleNodeID, Vector<StyleEngine::WithheldAttributeName, 2>> names_by_carrier;
- for (auto const& [name, carriers] : now_read.attributes) {
- for (auto node : carriers)
- names_by_carrier.ensure(node).append(name);
- }
- for (auto const& [node, names] : names_by_carrier) {
- auto element = element_holding(node);
- if (!element)
- continue;
- Vector<StyleEngine::WithheldAttributeName, 2> names_gone = names;
- element->for_each_attribute([&](DOM::QualifiedName const& name, Utf16String const& value) {
- StyleEngine::WithheldAttributeName withheld { name.local_name(), name.namespace_() };
- if (!names.contains_slow(withheld))
- return;
- names_gone.remove_all_matching([&](auto const& gone) { return gone == withheld; });
- auto name_atom = style_engine.intern_attribute_name(name.local_name(), name.namespace_());
- style_engine.record_local_feature_delta({
- .node = node.value(),
- .feature_kind = StyleEngineFFI::FfiFeatureKind::Attribute,
- .name_atom = name_atom.value(),
- .new_kind = StyleEngineFFI::FfiFeatureValueKind::Atom,
- .new_atom = style_engine.intern_attribute_value(name_atom, value).value(),
- });
- });
- // NB: The row may hold a value published before the name was known to be unread, of an attribute removed since.
- for (auto const& name : names_gone) {
- style_engine.record_local_feature_delta({
- .node = node.value(),
- .feature_kind = StyleEngineFFI::FfiFeatureKind::Attribute,
- .name_atom = style_engine.intern_attribute_name(name.local_name, name.namespace_uri).value(),
- .new_kind = StyleEngineFFI::FfiFeatureValueKind::Absent,
- .new_atom = 0,
- });
+ for (auto const& [name, atom] : names_now_read) {
+ if (auto value = element.get_attribute_ns(name.namespace_uri, name.local_name); value.has_value()) {
+ style_engine.record_local_feature_delta({
+ .node = node.value(),
+ .feature_kind = StyleEngineFFI::FfiFeatureKind::Attribute,
+ .name_atom = atom.value(),
+ .new_kind = StyleEngineFFI::FfiFeatureValueKind::Atom,
+ .new_atom = style_engine.intern_attribute_value(atom, *value).value(),
+ });
+ } else if (StyleEngineFFI::style_engine_row_holds_attribute(style_engine.host(), node.value(), atom.value())) {
+ // NB: The row holds a value published before the name was known to be unread, of an attribute removed since.
+ style_engine.record_local_feature_delta({
+ .node = node.value(),
+ .feature_kind = StyleEngineFFI::FfiFeatureKind::Attribute,
+ .name_atom = atom.value(),
+ .new_kind = StyleEngineFFI::FfiFeatureValueKind::Absent,
+ .new_atom = 0,
+ });
+ }
}
- }
+ });
}
// The atom an id or class name is published under. A quirks-mode document matches those selectors
@@ -3324,7 +3314,7 @@ void record_element_attribute_changed(DOM::Element& element, Utf16FlyString cons
// OPTIMIZATION: Nothing of the engine reads this name, so the element's row need not hear of the change. The element
// is noted as an arrival withholding the name is, and is published if something comes to read it.
if (style_engine->attribute_is_known_unread(atom)) {
- style_engine->note_attribute_withheld(name, namespace_uri, element.style_node_id());
+ style_engine->note_attribute_withheld(name, namespace_uri);
return;
}
auto kind_of = [](Optional<Utf16String> const& value) {
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index e992b99bfee..2267a30e8a6 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -582,6 +582,19 @@ pub unsafe extern "C" fn style_engine_stage_element_arrival(
}
}
+/// Whether the host's projection row of `node` holds the attribute `name`.
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_row_holds_attribute(host: &DocumentHost, node: u32, name: u32) -> bool {
+ let name = StyleAtomID(name);
+ StyleNodeID::from_raw(node).is_some_and(|node| {
+ host.engine_memo()
+ .projection
+ .borrow()
+ .written_facts(node)
+ .is_some_and(|facts| facts.attributes().iter().any(|attribute| attribute.name == name))
+ })
+}
+
/// Writes the local feature delta `delta` into the host's projection rows.
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_stage_local_feature_delta(host: &DocumentHost, delta: &FfiLocalFeatureDelta) {