Skip to content

Truly independent web browser

LibWeb: Route a positional of-type test to the counts that crossed it

Andreas Kling2ba0f09420cf
diff --git a/Libraries/LibWeb/Rust/src/css/style/routing.rs b/Libraries/LibWeb/Rust/src/css/style/routing.rs
index b5fdad8fc74..2146c9cbe91 100644
--- a/Libraries/LibWeb/Rust/src/css/style/routing.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/routing.rs
@@ -1718,6 +1718,64 @@ impl RetainedState {
name(self, at);
}
}
+ false
+ if nth.of_type
+ && nth.step == 0
+ && nth.offset >= 1
+ && !change.has_unidentified_type
+ && change.last_relocation.is_none() =>
+ {
+ // The same bound holds within each subsequence of one type, whose count moved by
+ // at most as much as the whole sequence did. Only the types that joined or left
+ // moved at all, so their children are counted from the end the test counts from,
+ // which stops once every such type is past the highest count that can have
+ // crossed. A child whose type is unknown could be of any of them, which leaves
+ // the moved side as the bound.
+ let offset = nth.offset as usize;
+ let lowest = offset.saturating_sub(change.departures as usize).max(1);
+ let highest = offset.saturating_add(change.arrivals as usize);
+ let mut counts: SmallVec<[usize; 4]> = smallvec::smallvec![0; change.changed_types.len()];
+ let mut types_past_highest = 0;
+ let mut crossing: SmallVec<[usize; 8]> = SmallVec::new();
+ let mut bounded = true;
+ for step in 0..children.len() {
+ if types_past_highest == counts.len() {
+ break;
+ }
+ let at = match nth.from_end {
+ true => children.len() - 1 - step,
+ false => step,
+ };
+ let child = children[at];
+ let tag = self.facts.tag_of_node(child);
+ if tag.is_none() {
+ bounded = false;
+ break;
+ }
+ let child_type = (tag, self.facts.namespace_of(child));
+ let Some(type_index) = change.changed_types.iter().position(|&changed| changed == child_type)
+ else {
+ continue;
+ };
+ counts[type_index] += 1;
+ let count = counts[type_index];
+ if count == highest + 1 {
+ types_past_highest += 1;
+ }
+ if (lowest..=highest).contains(&count) {
+ crossing.push(at);
+ }
+ }
+ if bounded {
+ for at in crossing {
+ name(self, at);
+ }
+ } else {
+ for at in moved.clone() {
+ name(self, at);
+ }
+ }
+ }
false => {
for at in moved.clone() {
if counts_this_sequence && change.last_relocation.is_none() {
diff --git a/Tests/LibWeb/Text/expected/css/nth-of-type-style-after-sibling-changes.txt b/Tests/LibWeb/Text/expected/css/nth-of-type-style-after-sibling-changes.txt
new file mode 100644
index 00000000000..856877d77b6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/nth-of-type-style-after-sibling-changes.txt
@@ -0,0 +1,7 @@
+initial: PASS
+span inserted at the start: PASS
+p appended: PASS
+span and p inserted in one update: PASS
+first span removed: PASS
+last two elements removed: PASS
+other type inserted at the start: PASS
diff --git a/Tests/LibWeb/Text/input/css/nth-of-type-style-after-sibling-changes.html b/Tests/LibWeb/Text/input/css/nth-of-type-style-after-sibling-changes.html
new file mode 100644
index 00000000000..1741e621862
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/nth-of-type-style-after-sibling-changes.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #list > * { color: black; }
+ #list > span:first-of-type { color: red; }
+ #list > p:last-of-type { color: green; }
+ #list > span:nth-of-type(2) { color: blue; }
+ #list > p:nth-last-of-type(2) { color: orange; }
+</style>
+<div id="list"></div>
+<script>
+ test(() => {
+ const list = document.getElementById("list");
+ for (let i = 0; i < 30; ++i)
+ list.append(document.createElement(i % 3 === 0 ? "p" : "span"));
+
+ function expectedColor(element) {
+ const all = Array.from(list.children);
+ const ofType = all.filter(other => other.localName === element.localName);
+ const index = ofType.indexOf(element);
+ if (element.localName === "span") {
+ if (index === 1)
+ return "rgb(0, 0, 255)";
+ if (index === 0)
+ return "rgb(255, 0, 0)";
+ } else if (element.localName === "p") {
+ if (index === ofType.length - 2)
+ return "rgb(255, 165, 0)";
+ if (index === ofType.length - 1)
+ return "rgb(0, 128, 0)";
+ }
+ return "rgb(0, 0, 0)";
+ }
+
+ function run(description) {
+ const wrong = Array.from(list.children)
+ .map((element, index) => [element, index])
+ .filter(([element]) => getComputedStyle(element).color !== expectedColor(element))
+ .map(([, index]) => index);
+ println(`${description}: ${wrong.length === 0 ? "PASS" : "FAIL at " + wrong.join(",")}`);
+ }
+
+ run("initial");
+ list.prepend(document.createElement("span"));
+ run("span inserted at the start");
+ list.append(document.createElement("p"));
+ run("p appended");
+ list.insertBefore(document.createElement("span"), list.children[1]);
+ list.insertBefore(document.createElement("p"), list.children[25]);
+ run("span and p inserted in one update");
+ list.firstElementChild.remove();
+ run("first span removed");
+ list.lastElementChild.remove();
+ list.lastElementChild.remove();
+ run("last two elements removed");
+ list.prepend(document.createElement("div"));
+ run("other type inserted at the start");
+ });
+</script>