Skip to content

Truly independent web browser

LibWeb: Skip settling pseudo-elements an element cannot generate

Andreas Kling2a75f7cbeb48
diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rs
index 773af4d0bce..52ee0cb2023 100644
--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs
@@ -2984,6 +2984,12 @@ impl ComputedGroupSets {
/// The record a pseudo-element of the node holds, with its animation overlay if any.
#[must_use]
+ /// Whether the node holds a row for any of its pseudo-elements, a record or the state one was
+ /// computed from.
+ pub(super) fn has_pseudo_rows(&self, node: StyleNodeID) -> bool {
+ !self.pseudo_rows(node).is_empty()
+ }
+
pub fn pseudo_style_record(&self, node: StyleNodeID, pseudo_kind: u8) -> Option<FinalStyleRecordID> {
let assignment = self.pseudo_row(node, pseudo_kind)?.assignment.as_ref()?;
Some(self.final_style_record(assignment.style_record, assignment.animation_overlay_slot))
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
index e92a6cd3a8c..620135c6790 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
@@ -162,6 +162,11 @@ impl RetainedState {
// Only an element's settled record leads here, which an unhosted engine never computes.
debug_assert!(self.computes_records());
+ if settlement == PseudoSettlement::Generated
+ && self.settles_no_pseudo_element(node, old_element_record, old_is_list_item, new_element_record)?
+ {
+ return Ok(());
+ }
let mut inputs = self.document_style_computation_inputs;
// NB: Root pseudos use the originating record's current font, independently of
// the document context used for the root's own remaining properties.
@@ -631,6 +636,62 @@ impl RetainedState {
Ok(())
}
+ /// OPTIMIZATION: Whether a style update's settlement of the node's pseudo-elements would settle
+ /// none of them: the node holds no pseudo-element row to remove or keep, and no kind it
+ /// settles can generate a box. A kind generates one where the node's answer has rules for it,
+ /// but for a backdrop outside the top layer and for a marker, which only a list item (or an
+ /// element that stops being one) generates; highlight kinds inherit an ancestor's record
+ /// without rules of their own, so a settlement selecting one is never skipped. Most elements
+ /// match only the user agent's universal `::backdrop` and `::marker` rules, and an element
+ /// styled for the first time holds no pseudo-element row.
+ fn settles_no_pseudo_element(
+ &mut self,
+ node: StyleNodeID,
+ old_element_record: Option<computed::FinalStyleRecordID>,
+ old_is_list_item: Option<bool>,
+ new_element_record: computed::FinalStyleRecordID,
+ ) -> Drive<bool> {
+ use bridge::element_adjustment_fact as fact;
+ use pseudo_kind::{BACKDROP, MARKER};
+
+ let facts = self.computed_group_sets.adjustment_facts(node);
+ // The document element's pseudo-elements read the root font inputs its record settles.
+ if facts & fact::IS_DOCUMENT_ELEMENT != 0 || self.computed_group_sets.has_pseudo_rows(node) {
+ return Ok(false);
+ }
+ let settlement = PseudoSettlement::Generated;
+ let mut generated = settlement
+ .kinds()
+ .iter()
+ .filter(|&&kind| settlement.selects(kind, self.deferred_pseudo_elements))
+ .fold(0_u64, |kinds, &kind| kinds | (1 << kind));
+ if pseudo_kind::SETTLEMENT_ORDER
+ .iter()
+ .any(|&kind| pseudo_kind::is_highlight(kind) && generated & (1 << kind) != 0)
+ {
+ return Ok(false);
+ }
+ generated &= self.pseudo_kinds_with_rules(node, settlement)?;
+ if facts & fact::RENDERED_IN_TOP_LAYER == 0 {
+ generated &= !(1_u64 << BACKDROP);
+ }
+ if generated & (1 << MARKER) != 0 {
+ let display_is_list_item = |engine: &Self, record: computed::FinalStyleRecordID| {
+ engine
+ .computed_group_sets
+ .style_record_view(record.raw())
+ .and_then(|view| view.longhand_table)
+ .is_none_or(|table| table.display_is_list_item())
+ };
+ let old_is_list_item = old_is_list_item
+ .unwrap_or_else(|| old_element_record.is_some_and(|record| display_is_list_item(self, record)));
+ if !old_is_list_item && !display_is_list_item(self, new_element_record) {
+ generated &= !(1_u64 << MARKER);
+ }
+ }
+ Ok(generated == 0)
+ }
+
/// A later pseudo-element alike in what this record is computed from takes it in a later
/// transaction. The cache is small and bounded.
fn remember_pseudo_record(
diff --git a/Tests/LibWeb/Layout/expected/pseudo-elements-after-first-style.txt b/Tests/LibWeb/Layout/expected/pseudo-elements-after-first-style.txt
new file mode 100644
index 00000000000..1ec2ec3de8a
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/pseudo-elements-after-first-style.txt
@@ -0,0 +1,85 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 112 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 96 0+0+8] children: not-inline
+ BlockContainer <div#plain.item> at [8,8] [0+0+0 200 0+0+584] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [8,8 37.171875x16] baseline: 12.796875
+ "plain"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,24] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ BlockContainer <div#before.item> at [8,24] [0+0+0 200 0+0+584] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [31.875,24 52.53125x16] baseline: 12.796875
+ "before"
+ InlineNode <(anonymous)> at [8,24] [0+0+0 23.875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,24 23.875x16] baseline: 12.796875
+ "[x]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,40] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ ListItemBox <div#list.item.later-list-item> at [8,40] [0+0+0 200 0+0+584] [0+0+0 16 0+0+0] children: inline
+ frag 0 from ListItemMarkerBox start: 0, length: 0, rect: [8,40 13.84375x16] baseline: 12.796875
+ frag 1 from TextNode start: 0, length: 4, rect: [21.84375,40 25x16] baseline: 12.796875
+ "list"
+ ListItemMarkerBox <(anonymous)> at [8,40] [0+0+0 13.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from GeneratedTextNode start: 0, length: 2, rect: [8,40 13.84375x16] baseline: 12.796875
+ "• "
+ GeneratedTextNode <(anonymous)> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,56] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div#hidden> at [8,56] [0+0+0 784 0+0+0] [0+0+0 48 0+0+0] children: not-inline
+ BlockContainer <div.gen> at [8,56] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 10, rect: [29.34375,56 82.09375x16] baseline: 12.796875
+ "inserted 0"
+ InlineNode <(anonymous)> at [8,56] [0+0+0 21.34375 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,56 21.34375x16] baseline: 12.796875
+ "[y]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ TextNode <#text> (not painted)
+ InlineNode <(anonymous)> at [111.4375,56] [0+0+0 23.875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [111.4375,56 23.875x16] baseline: 12.796875
+ "[x]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ ListItemBox <li.gen> at [8,72] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 10, rect: [29.34375,72 78.84375x16] baseline: 12.796875
+ "inserted 1"
+ ListItemMarkerBox <(anonymous)> at [-6,72] [0+0+0 13.84375 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from GeneratedTextNode start: 0, length: 2, rect: [-6,72 13.84375x16] baseline: 12.796875
+ "• "
+ GeneratedTextNode <(anonymous)> (not painted)
+ InlineNode <(anonymous)> at [8,72] [0+0+0 21.34375 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,72 21.34375x16] baseline: 12.796875
+ "[y]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ TextNode <#text> (not painted)
+ InlineNode <(anonymous)> at [108.1875,72] [0+0+0 23.875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [108.1875,72 23.875x16] baseline: 12.796875
+ "[x]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <div.gen> at [8,88] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 10, rect: [29.34375,88 81.3125x16] baseline: 12.796875
+ "inserted 2"
+ InlineNode <(anonymous)> at [8,88] [0+0+0 21.34375 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,88 21.34375x16] baseline: 12.796875
+ "[y]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ TextNode <#text> (not painted)
+ InlineNode <(anonymous)> at [110.65625,88] [0+0+0 23.875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [110.65625,88 23.875x16] baseline: 12.796875
+ "[x]"
+ GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <(anonymous)> at [8,104] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [0,0] positioned [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <dialog#dialog> at [378.03125,292] positioned [359.03125+3+16 43.921875 16+3+359.03125] [273+3+16 16 16+3+273] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [378.03125,292 43.921875x16] baseline: 12.796875
+ "dialog"
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x112] (z-index: auto)
+ SC for BlockContainer(anonymous) [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<DIALOG>#dialog [378.03125,292 43.921875x16] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/pseudo-elements-after-first-style.html b/Tests/LibWeb/Layout/input/pseudo-elements-after-first-style.html
new file mode 100644
index 00000000000..1b7f915ec41
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/pseudo-elements-after-first-style.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<style>
+ .item { display: block; width: 200px; }
+ .later-list-item { list-style-position: inside; }
+ dialog::backdrop { background: green; }
+</style>
+<div id="plain" class="item">plain</div>
+<div id="before" class="item">before</div>
+<div id="list" class="item later-list-item">list</div>
+<dialog id="dialog">dialog</dialog>
+<div id="hidden" style="display: none"></div>
+<script>
+ // Every element above is styled once without any pseudo-element that generates a box. Each then gains one: a
+ // ::before from a stylesheet inserted later, a ::marker from becoming a list item, a ::backdrop from entering
+ // the top layer, and in a display:none subtree, elements inserted after their parent was styled whose
+ // pseudo-elements generate once the subtree is revealed.
+ document.body.offsetHeight;
+ const sheet = document.createElement("style");
+ sheet.textContent = "#before::before, #hidden .gen::after { content: '[x]'; } #hidden .gen::before { content: '[y]'; }";
+ document.head.appendChild(sheet);
+ document.body.offsetHeight;
+ document.getElementById("list").style.display = "list-item";
+ document.body.offsetHeight;
+ document.getElementById("dialog").showModal();
+ document.body.offsetHeight;
+ const hidden = document.getElementById("hidden");
+ for (let i = 0; i < 3; ++i) {
+ const element = document.createElement(i == 1 ? "li" : "div");
+ element.className = "gen";
+ element.textContent = "inserted " + i;
+ hidden.appendChild(element);
+ }
+ document.body.offsetHeight;
+ hidden.style.display = "block";
+</script>