Skip to content

Truly independent web browser

LibWeb: Splice appended subtrees that set counters

Aliaksandr Kalenik76e9975fa9c5
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index 803805347f3..e68590d54d1 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -672,16 +672,12 @@ impl<'a> ComputedValuesView<'a> {
/// Whether the style can move the generated-content state a later sibling reads: any counter
/// it touches, or a quote its content opens or closes.
pub(crate) fn affects_generated_content_state(self) -> bool {
- let content = self.content();
- let names_counters =
- |handle: &ComputedStyleValueHandle| !matches!(handle.data(), None | Some(StyleValueData::Keyword { .. }));
- if names_counters(&content.counter_increment)
- || names_counters(&content.counter_reset)
- || names_counters(&content.counter_set)
- {
- return true;
- }
- let Some(StyleValueData::Content { content, .. }) = content.content.data() else {
+ !self.counter_properties_are_none() || self.content_opens_or_closes_quotes()
+ }
+
+ /// Whether `content` opens or closes a quote, which moves the quote depth that every quote after it reads.
+ pub(crate) fn content_opens_or_closes_quotes(self) -> bool {
+ let Some(StyleValueData::Content { content, .. }) = self.content().content.data() else {
return false;
};
let Some(StyleValueData::ValueList { values, .. }) = content.optional_data() else {
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index d4e8ab4ed8d..e05f8748bbd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -116,6 +116,20 @@ impl RootFontInputs {
}
}
+/// What a subtree does to the generated-content state that the boxes after it read.
+#[derive(Clone, Copy, Default)]
+pub(crate) struct SubtreeGeneratedContentEffects {
+ /// A `counter-reset` names a counter. The counter it creates reaches only the following siblings of the element
+ /// that resets it, and their descendants.
+ pub(crate) resets_counters: bool,
+ /// A `counter-increment` or `counter-set` names a counter, or a list item below the root increments `list-item`.
+ /// The counter can be one that an ancestor or an earlier box created, and that boxes outside the subtree read. An
+ /// inserted `<li>` itself has the DOM renumber the list it joins.
+ pub(crate) increments_or_sets_counters: bool,
+ /// A `content` opens or closes a quote. The walk stops once it finds one.
+ pub(crate) opens_or_closes_quotes: bool,
+}
+
impl RetainedState {
pub(super) fn pseudo_element_style_is_deferred(&self, kind: tree::PseudoElementKind) -> bool {
u32::from(kind.0) < u64::BITS && self.deferred_pseudo_elements & (1 << kind.0) != 0
@@ -3049,36 +3063,39 @@ impl RetainedState {
self.pseudo_style_mask(node).unwrap_or(0)
}
- /// Whether the style record answers for a counter or a quote: the two things whose state runs
- /// along the whole tree rather than staying inside one box.
- fn style_record_affects_generated_content_state(&self, style_record: Option<computed::FinalStyleRecordID>) -> bool {
- self.published_style_record_view(style_record)
- .is_some_and(crate::css::computed_value_views::ComputedValuesView::affects_generated_content_state)
- }
-
- /// Whether the node or any of its DOM descendants styles a counter or a quote, itself or
- /// through a `::before`, `::after` or `::marker`. Inserting such a subtree renumbers what
- /// follows it, so the layout tree build has to rebuild rather than splice it in.
+ /// What the node and its DOM descendants do to the two pieces of generated-content state that run
+ /// along the whole tree rather than staying inside one box, themselves or through a `::before`,
+ /// `::after` or `::marker`. Inserting such a subtree can change what the boxes after it show.
#[must_use]
- pub fn subtree_affects_generated_content_state(&self, node: StyleNodeID) -> bool {
- let mut pending = vec![node];
+ pub(crate) fn subtree_generated_content_effects(&self, root: StyleNodeID) -> SubtreeGeneratedContentEffects {
+ let mut effects = SubtreeGeneratedContentEffects::default();
+ let mut pending = vec![root];
while let Some(node) = pending.pop() {
- if node.element_index().is_some()
- && (self
- .style_record_affects_generated_content_state(self.computed_group_sets.assigned_style_record(node))
- || [pseudo_kind::BEFORE, pseudo_kind::AFTER, pseudo_kind::MARKER]
- .into_iter()
- .any(|kind| {
- self.style_record_affects_generated_content_state(
- self.computed_group_sets.pseudo_style_record(node, kind),
- )
- }))
- {
- return true;
+ if node.element_index().is_some() {
+ let records = [
+ self.computed_group_sets.assigned_style_record(node),
+ self.computed_group_sets.pseudo_style_record(node, pseudo_kind::BEFORE),
+ self.computed_group_sets.pseudo_style_record(node, pseudo_kind::AFTER),
+ self.computed_group_sets.pseudo_style_record(node, pseudo_kind::MARKER),
+ ];
+ for (index, record) in records.into_iter().enumerate() {
+ let Some(view) = self.published_style_record_view(record) else {
+ continue;
+ };
+ let is_root_element = index == 0 && node == root;
+ effects.resets_counters |= !view.counter_reset().is_empty();
+ effects.increments_or_sets_counters |= !view.counter_increment().is_empty()
+ || !view.counter_set().is_empty()
+ || (view.display().is_list_item() && !is_root_element);
+ effects.opens_or_closes_quotes |= view.content_opens_or_closes_quotes();
+ }
+ if effects.opens_or_closes_quotes {
+ return effects;
+ }
}
pending.extend(self.tree.dom_children(node));
}
- false
+ effects
}
/// Whether the node's published style holds a `::first-letter` record. The tree build asks a
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index b00b5814043..fe5558cc81d 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -956,6 +956,16 @@ impl ChildListInsertionReuse<'_, '_> {
false
}
+ /// Whether a child before `inserted_child` resets a counter that counts down from its last item. The start value
+ /// of such a counter counts every increment in its scope, which includes the inserted child.
+ fn earlier_child_counts_a_counter_down(&self, inserted_child: StyleNodeID) -> bool {
+ self.dom_children()
+ .take_while(|&child| child != inserted_child)
+ .any(|child| {
+ child.element_index().is_some() && self.engine.element_counter_reset_has_reversed_counter(child)
+ })
+ }
+
fn run(&self) -> bool {
if self.pending_children_can_preserve_parent() {
// An empty set means every insertion was canceled before layout. Moves are marked dirty
@@ -1027,10 +1037,14 @@ impl ChildListInsertionReuse<'_, '_> {
// An appended out-of-flow block joins the anonymous wrapper its preceding whitespace starts, as in a full
// build, only while that wrapper stays the parent's last child.
let mut inserted_whitespace_run_must_stay_last = false;
+ // Counters are resolved from the boxes before a box, so an inserted subtree that touches one changes only what
+ // comes after it.
+ let mut first_inserted_child_touching_counters = None;
+ let mut inserted_subtree_increments_or_sets_counters = false;
for child in self.dom_children() {
let child_layout_node = self.box_of(child);
if !child_layout_node.is_invalid() {
- if inserted_whitespace_run_must_stay_last {
+ if inserted_whitespace_run_must_stay_last || first_inserted_child_touching_counters.is_some() {
return false;
}
has_pending_collapsing_whitespace = false;
@@ -1079,9 +1093,16 @@ impl ChildListInsertionReuse<'_, '_> {
if !self.needs_layout_tree_update(child) || child_display.is_none() {
continue;
}
- if self.engine.subtree_affects_generated_content_state(child) {
+ let generated_content_effects = self.engine.subtree_generated_content_effects(child);
+ if generated_content_effects.opens_or_closes_quotes {
return false;
}
+ if (generated_content_effects.resets_counters || generated_content_effects.increments_or_sets_counters)
+ && first_inserted_child_touching_counters.is_none()
+ {
+ first_inserted_child_touching_counters = Some(child);
+ }
+ inserted_subtree_increments_or_sets_counters |= generated_content_effects.increments_or_sets_counters;
if self.layout.data(self.layout_node).kind.get() == NodeKind::FieldSetBox
&& self.engine.element_box_kind(child) == ElementBoxKind::Legend
{
@@ -1163,6 +1184,13 @@ impl ChildListInsertionReuse<'_, '_> {
return false;
}
let after = self.arena().bound_pseudo_element_row(self.element, GENERATED_FOR_AFTER);
+ if let Some(first_inserted_child_touching_counters) = first_inserted_child_touching_counters
+ && (!after.is_invalid()
+ || (inserted_subtree_increments_or_sets_counters
+ && self.earlier_child_counts_a_counter_down(first_inserted_child_touching_counters)))
+ {
+ return false;
+ }
let after_is_in_inline_run = !after.is_invalid() && self.layout.parent(after) != self.layout_node;
// OPTIMIZATION: Appended inline and out-of-flow children can join the trailing inline run. Do not
// reuse a run across generated content or text that a rebuild may need to make visible.
diff --git a/Tests/LibWeb/Ref/expected/ol-render-node-insert-first-group-without-whitespace-ref.html b/Tests/LibWeb/Ref/expected/ol-render-node-insert-first-group-without-whitespace-ref.html
new file mode 100644
index 00000000000..f3c34773184
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/ol-render-node-insert-first-group-without-whitespace-ref.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<body>
+<ol><div><li>One</li><li>Two</li></div><li>Three</li><li>Four</li></ol>
+</body>
diff --git a/Tests/LibWeb/Ref/input/ol-render-node-insert-first-group-without-whitespace.html b/Tests/LibWeb/Ref/input/ol-render-node-insert-first-group-without-whitespace.html
new file mode 100644
index 00000000000..e783183830e
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/ol-render-node-insert-first-group-without-whitespace.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<head>
+ <link rel="match" href="../expected/ol-render-node-insert-first-group-without-whitespace-ref.html" />
+</head>
+<html class="reftest-wait">
+<body>
+<ol id="list"><li id="three">Three</li><li>Four</li></ol>
+<script>
+ // Two nested requestAnimationFrame() calls to force code execution _after_ initial paint
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ const group = document.createElement("div");
+ group.innerHTML = "<li>One</li><li>Two</li>";
+ document.getElementById("list").insertBefore(group, document.getElementById("three"));
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/absolutely-positioned-insertion.txt b/Tests/LibWeb/Text/expected/layout-tree-update/absolutely-positioned-insertion.txt
index a4e92bb6612..26fa931e884 100644
--- a/Tests/LibWeb/Text/expected/layout-tree-update/absolutely-positioned-insertion.txt
+++ b/Tests/LibWeb/Text/expected/layout-tree-update/absolutely-positioned-insertion.txt
@@ -108,8 +108,8 @@ float: sibling-preserved=true
float: confined-as-expected=true
float: target-attached=true
float: tree-matches=true
-generated-content: parent-preserved=false
-generated-content: sibling-preserved=false
+generated-content: parent-preserved=true
+generated-content: sibling-preserved=true
generated-content: confined-as-expected=true
generated-content: target-attached=true
generated-content: tree-matches=true
diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/direct-child-list-preservation.txt b/Tests/LibWeb/Text/expected/layout-tree-update/direct-child-list-preservation.txt
index f73004a4adc..813bac7cbb4 100644
--- a/Tests/LibWeb/Text/expected/layout-tree-update/direct-child-list-preservation.txt
+++ b/Tests/LibWeb/Text/expected/layout-tree-update/direct-child-list-preservation.txt
@@ -172,6 +172,39 @@ PASS: append absolute block child after whitespace and anonymous inline run | in
PASS: fall back for absolute block child after whitespace before existing child | rebuild parent
PASS: fall back for absolute block child after whitespace before existing child | layout tree builds=1
PASS: fall back for absolute block child after whitespace before existing child | incremental tree matches full rebuild
+PASS: append ordered list subtree | preserve parent
+PASS: append ordered list subtree | preserve before
+PASS: append ordered list subtree | layout tree builds=1
+PASS: append ordered list subtree | incremental tree matches full rebuild
+PASS: append unordered list subtree after whitespace | preserve parent
+PASS: append unordered list subtree after whitespace | preserve before
+PASS: append unordered list subtree after whitespace | layout tree builds=1
+PASS: append unordered list subtree after whitespace | incremental tree matches full rebuild
+PASS: append subtree with its own counter | preserve parent
+PASS: append subtree with its own counter | preserve before
+PASS: append subtree with its own counter | layout tree builds=1
+PASS: append subtree with its own counter | incremental tree matches full rebuild
+PASS: append block child incrementing a preceding counter | preserve parent
+PASS: append block child incrementing a preceding counter | preserve before
+PASS: append block child incrementing a preceding counter | layout tree builds=1
+PASS: append block child incrementing a preceding counter | incremental tree matches full rebuild
+PASS: append ordered list subtree before a sibling of the parent | preserve parent
+PASS: append ordered list subtree before a sibling of the parent | preserve before
+PASS: append ordered list subtree before a sibling of the parent | layout tree builds=1
+PASS: append ordered list subtree before a sibling of the parent | incremental tree matches full rebuild
+PASS: append counter increment before a sibling of the parent | preserve parent
+PASS: append counter increment before a sibling of the parent | preserve before
+PASS: append counter increment before a sibling of the parent | layout tree builds=1
+PASS: append counter increment before a sibling of the parent | incremental tree matches full rebuild
+PASS: fall back when appended counter subtree is followed by generated content | rebuild parent
+PASS: fall back when appended counter subtree is followed by generated content | layout tree builds=1
+PASS: fall back when appended counter subtree is followed by generated content | incremental tree matches full rebuild
+PASS: fall back when appending a list-item increment after a reversed list | rebuild parent
+PASS: fall back when appending a list-item increment after a reversed list | layout tree builds=1
+PASS: fall back when appending a list-item increment after a reversed list | incremental tree matches full rebuild
+PASS: fall back when appending a subtree that opens a quote | rebuild parent
+PASS: fall back when appending a subtree that opens a quote | layout tree builds=1
+PASS: fall back when appending a subtree that opens a quote | incremental tree matches full rebuild
PASS: fall back for block child before anonymous inline run | rebuild parent
PASS: fall back for block child before anonymous inline run | layout tree builds=1
PASS: fall back for block child before anonymous inline run | incremental tree matches full rebuild
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/direct-child-list-preservation.html b/Tests/LibWeb/Text/input/layout-tree-update/direct-child-list-preservation.html
index 2423f5d977c..6884af32d91 100644
--- a/Tests/LibWeb/Text/input/layout-tree-update/direct-child-list-preservation.html
+++ b/Tests/LibWeb/Text/input/layout-tree-update/direct-child-list-preservation.html
@@ -25,6 +25,9 @@
.absolute { position: absolute; }
.float { float: left; }
.contents { display: contents; }
+ .appended-counter-reset { counter-reset: appended-counter; }
+ .appended-counter-increment { counter-increment: appended-counter; }
+ .appended-counter-value::before { content: counter(appended-counter); }
</style>
<div id="fixture"></div>
<script>
@@ -306,6 +309,65 @@
tracked: ["parent"],
preserve: false,
},
+ {
+ name: "append ordered list subtree",
+ markup: '<div class="parent"><i class="before child"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<ol class="target"><li>one</li><li>two</li></ol>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "append unordered list subtree after whitespace",
+ markup: '<div class="parent"><i class="before child"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '\n<ul class="target"><li>one</li><li>two</li></ul>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "append subtree with its own counter",
+ markup: '<div class="parent"><i class="before child"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<section class="target child appended-counter-reset"><i class="child appended-counter-increment appended-counter-value"></i><i class="child appended-counter-increment appended-counter-value"></i></section>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "append block child incrementing a preceding counter",
+ markup: '<div class="parent counter"><i class="before child increment counter-value"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<section class="target child increment counter-value"></section><section class="target child increment counter-value"></section>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "append ordered list subtree before a sibling of the parent",
+ markup: '<div><div class="parent"><i class="before child"></i></div><i class="child"></i></div>',
+ parentSelector: ".parent",
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<ol class="target"><li>one</li></ol>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "append counter increment before a sibling of the parent",
+ markup: '<div><div class="parent counter"><i class="before child increment"></i></div><i class="child"></i></div>',
+ parentSelector: ".parent",
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<section class="target child increment counter-value"></section>'),
+ tracked: ["parent", "before"],
+ },
+ {
+ name: "fall back when appended counter subtree is followed by generated content",
+ markup: '<div class="parent generated"><i class="before child"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<ol class="target"><li>one</li></ol>'),
+ tracked: ["parent"],
+ preserve: false,
+ },
+ {
+ name: "fall back when appending a list-item increment after a reversed list",
+ markup: '<div class="parent"><ol reversed class="before"><li>one</li><li>two</li></ol></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<section class="target child" style="counter-increment: list-item"></section>'),
+ tracked: ["parent"],
+ preserve: false,
+ },
+ {
+ name: "fall back when appending a subtree that opens a quote",
+ markup: '<div class="parent quotes"><i class="before child"></i></div>',
+ mutate: parent => parent.insertAdjacentHTML("beforeend", '<section class="target child"><i class="quote-open"></i></section>'),
+ tracked: ["parent"],
+ preserve: false,
+ },
{
name: "fall back for block child before anonymous inline run",
markup: '<div class="parent"><i class="before child"></i><span class="inline">inline</span></div>',