Skip to content

Truly independent web browser

LibWeb: Reject sibling invalidation effects before matching them

Andreas Klingb28d36e6be30
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/allocation_tests.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/allocation_tests.rs
index 1ea8ba5939e..85443b65c95 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/allocation_tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/allocation_tests.rs
@@ -11,6 +11,7 @@
//! The engine runs on the calling thread here, where every allocation is counted. Run with
//! LIBWEB_TEST_ALLOCATION_BACKTRACES set to see where a test's allocations come from.
+use super::super::bridge::element_adjustment_fact;
use super::super::*;
use super::allocation_count::{Allocations, count_allocations};
use super::test_support::*;
@@ -141,3 +142,52 @@ fn a_class_one_selector_names_on_a_leaf_allocates_at_most_as_today() {
"toggles of a class one selector names",
);
}
+
+/// What taking an element out of a list allocates in style, under `depth` ancestors, where the only
+/// selector reading siblings tests nothing of the element or of the ancestors.
+fn allocations_of_an_element_leaving_at_depth(depth: usize) -> Vec<u64> {
+ let mut engine = hosted_engine();
+ let nodes = allocate_nodes(&mut engine, depth + 2 + WARMUP_ROUNDS + MEASURED_ROUNDS);
+ let (ancestors, others) = nodes.split_at(depth + 1);
+ let (kept, leaving) = others.split_first().expect("the document holds the element kept");
+ let (root, parent, kept) = (ancestors[0], ancestors[depth], *kept);
+ engine.record_tree_delta(root, None, Some(relations(None, None, None)));
+ for pair in ancestors.windows(2) {
+ engine.record_tree_delta(pair[1], None, Some(relations(Some(pair[0]), None, None)));
+ }
+ engine.record_tree_delta(kept, None, Some(relations(Some(parent), None, None)));
+ engine.set_element_adjustment_facts(root, element_adjustment_fact::IS_DOCUMENT_ELEMENT);
+ for &node in ancestors.iter().chain([&kept]) {
+ set_tag(&mut engine, node, "div");
+ }
+ for &node in ancestors {
+ add_class(&mut engine, node, "ancestor");
+ add_class(&mut engine, node, "other-ancestor");
+ }
+ add_class(&mut engine, kept, "y");
+ let sheet = add_sheet(&mut engine, TreeScopeID::DOCUMENT);
+ add_rules(&mut engine, sheet, ".x ~ .y { width: 10px; }");
+ start_live(&mut engine, root);
+
+ let in_the_list = relations(Some(parent), None, Some(kept));
+ let allocations: Vec<u64> = leaving
+ .iter()
+ .map(|&node| {
+ engine.record_tree_delta(node, None, Some(in_the_list));
+ set_tag(&mut engine, node, "div");
+ add_class(&mut engine, node, "leaving");
+ run_style_update(&mut engine, root);
+ engine.record_tree_delta(node, Some(in_the_list), None);
+ count_allocations(|| run_style_update(&mut engine, root)).1.in_style
+ })
+ .collect();
+ allocations[WARMUP_ROUNDS..].to_vec()
+}
+
+#[test]
+fn an_element_leaving_reads_no_ancestors_where_no_selector_asks() {
+ assert_eq!(
+ allocations_of_an_element_leaving_at_depth(64),
+ allocations_of_an_element_leaving_at_depth(2)
+ );
+}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/bloom.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/bloom.rs
index d2f70b51438..4aac26287d4 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/bloom.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/bloom.rs
@@ -76,6 +76,11 @@ impl AncestorHashes {
}
}
+ /// Whether no key is required of the ancestors.
+ pub(super) fn is_empty(&self) -> bool {
+ self.hashes().next().is_none()
+ }
+
/// The words the hashes are packed in, for a holder to keep those not zero.
pub(super) fn packed_words(&self) -> [u32; 3] {
self.packed
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
index 1a1bf1d5214..64e5bff66fb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
@@ -1088,14 +1088,18 @@ impl InvalidationMap {
name_key_of(*trigger).is_some_and(|key| self.segments.iter().any(|part| part.segment.buckets_hold(key)))
}
- /// The dependencies of `trigger` whose combinator right of them is a sibling combinator's.
- pub(super) fn sibling_effects(&self, trigger: &Trigger) -> impl Iterator<Item = Dependency> + Clone + '_ {
+ /// The dependencies of `trigger` whose combinator right of them is a sibling combinator's, with
+ /// the ancestor keys each holds.
+ pub(super) fn held_sibling_effects(&self, trigger: &Trigger) -> impl Iterator<Item = HeldDependency> + Clone + '_ {
let trigger = *trigger;
self.parts().flat_map(move |(part, map, first_entry)| {
local_trigger(trigger, part)
.into_iter()
.flat_map(move |local| map.sibling_by_trigger.values(&local))
- .map(move |held| rebased(&held.dependency, part, first_entry))
+ .map(move |held| HeldDependency {
+ dependency: rebased(&held.dependency, part, first_entry),
+ ancestor_hashes: held.ancestor_hashes,
+ })
})
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
index 5dbdf8bd787..529d9063265 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
@@ -19,7 +19,7 @@ use super::super::transaction_view::TransactionFactSide;
use super::super::*;
use super::drive::DriveRecords;
use super::invalidation::{
- AnchorReach, Dependency, InvalidationMap, PositionKey, ReachCombinator, Step, Trigger, TriggerReach,
+ AnchorReach, Dependency, HeldDependency, InvalidationMap, PositionKey, ReachCombinator, Step, Trigger, TriggerReach,
};
use super::projection::ProjectionEvaluator;
use super::projection::{ElementChange, FactListsIdentity, Projection, ProjectionRow, StructuralFlags};
@@ -1404,6 +1404,12 @@ impl Live {
let restyled_whole = |node: Option<StyleNodeID>| {
std::iter::successors(node, |&node| projection.parent(node)).any(|node| arrived.contains(&node))
};
+ // NB: What the later siblings of each element are is found once over a transaction, which may move
+ // every child of a long list.
+ let next_sibling = |node: StyleNodeID| projection.next_element_sibling(node);
+ let bloom_of = |node: StyleNodeID| projection.facts(node).map_or(0, |facts| facts.dispatch_bloom(false));
+ let mut past_arrived: HashMap<StyleNodeID, Option<StyleNodeID>> = HashMap::default();
+ let mut later_blooms: HashMap<StyleNodeID, u64> = HashMap::default();
for &(node, left, first_later_sibling) in moves.iter() {
// NB: What no child of the parent matched read of its earlier siblings cannot move with them.
let parent = relations_moved
@@ -1420,18 +1426,11 @@ impl Live {
if !left && restyled_whole(projection.parent(node)) {
continue;
}
- let Some(mut first_later_sibling) = live_from(first_later_sibling) else {
+ let Some(first_later_sibling) = live_from(first_later_sibling)
+ .and_then(|sibling| first_not_arrived(sibling, &arrived, next_sibling, &mut past_arrived))
+ else {
continue;
};
- while arrived.contains(&first_later_sibling) {
- let Some(next) = projection.next_element_sibling(first_later_sibling) else {
- break;
- };
- first_later_sibling = next;
- }
- if arrived.contains(&first_later_sibling) {
- continue;
- }
let triggers = match left {
true => match follower.row_before(node).map(triggers_of) {
Some(found) => found,
@@ -1451,11 +1450,28 @@ impl Live {
},
};
links.clear();
- // NB: A compound testing several of the element's facts is matched once.
- let mut effects: SmallVec<[Dependency; 16]> = triggers
+ // NB: A compound is matched leftwards only where the compound right of it could match a later sibling
+ // and its ancestors could match around the element, which rejects most of them for far less.
+ let next_bloom = bloom_of(first_later_sibling);
+ let mut held_effects: SmallVec<[HeldDependency; 16]> = triggers
.iter()
- .flat_map(|trigger| map.sibling_effects(trigger))
+ .flat_map(|trigger| map.held_sibling_effects(trigger))
+ .filter(|held| {
+ map.right(held.dependency).is_some_and(|link| {
+ let step = map.step(link);
+ step.required_key.is_none_or(|key| {
+ let carried = match step.combinator {
+ ReachCombinator::NextSibling => next_bloom,
+ _ => bloom_from(first_later_sibling, next_sibling, bloom_of, &mut later_blooms),
+ };
+ carried & dispatch_bloom_bit(key) != 0
+ })
+ })
+ })
.collect();
+ follower.retain_effects_around(node, &mut held_effects);
+ // NB: A compound testing several of the element's facts is matched once.
+ let mut effects: SmallVec<[Dependency; 16]> = held_effects.iter().map(|held| held.dependency).collect();
effects.sort_unstable();
effects.dedup();
for effect in effects {
@@ -1487,6 +1503,7 @@ impl Live {
continue;
}
links.clear();
+ let carried = projection.facts(node).map_or(0, |facts| facts.dispatch_bloom(false));
for previous in previous_siblings.into_iter().flatten() {
let Some(row) = projection.row_of(previous).filter(|_| tree.is_live(previous)) else {
continue;
@@ -1499,24 +1516,35 @@ impl Live {
if facts_moved {
triggers.extend(follower.row_before(previous).map(triggers_of).into_iter().flatten());
}
- let mut effects: SmallVec<[Dependency; 16]> = triggers
+ // NB: As for the moves above, only where `node` could match the compound right of it.
+ let mut held_effects: SmallVec<[HeldDependency; 16]> = triggers
.iter()
- .flat_map(|trigger| map.sibling_effects(trigger))
+ .flat_map(|trigger| map.held_sibling_effects(trigger))
+ .filter(|held| {
+ map.right(held.dependency).is_some_and(|link| {
+ let step = map.step(link);
+ step.combinator == ReachCombinator::NextSibling
+ && step
+ .required_key
+ .is_none_or(|key| carried & dispatch_bloom_bit(key) != 0)
+ })
+ })
.collect();
+ follower.retain_effects_around(node, &mut held_effects);
+ let mut effects: SmallVec<[Dependency; 16]> = held_effects.iter().map(|held| held.dependency).collect();
effects.sort_unstable();
effects.dedup();
for effect in effects {
let Some(link) = follower.map.right(effect) else {
continue;
};
- if follower.map.step(link).combinator == ReachCombinator::NextSibling
- && (follower.matches_from(effect, previous, false).unwrap_or(true)
- // NB: A compound testing names alone with nothing left of it answers by the sibling's
- // facts, which answer alike on both sides where they did not move.
- || (facts_moved
- || follower.map.left(effect).is_some()
- || follower.map.operand_names(effect).is_empty())
- && follower.matches_from(effect, previous, true).unwrap_or(true))
+ if follower.matches_from(effect, previous, false).unwrap_or(true)
+ // NB: A compound testing names alone with nothing left of it answers by the sibling's
+ // facts, which answer alike on both sides where they did not move.
+ || (facts_moved
+ || follower.map.left(effect).is_some()
+ || follower.map.operand_names(effect).is_empty())
+ && follower.matches_from(effect, previous, true).unwrap_or(true)
{
links.push(link);
}
@@ -2654,6 +2682,57 @@ impl Hints {
}
}
+/// The first of `first` and its later siblings not in `arrived`, as `next` tells them, remembering in `past_arrived`
+/// what it finds for each one it walks over: a run of arrived siblings is walked once.
+fn first_not_arrived(
+ first: StyleNodeID,
+ arrived: &HashSet<StyleNodeID>,
+ next: impl Fn(StyleNodeID) -> Option<StyleNodeID>,
+ past_arrived: &mut HashMap<StyleNodeID, Option<StyleNodeID>>,
+) -> Option<StyleNodeID> {
+ let mut walked: SmallVec<[StyleNodeID; 16]> = SmallVec::new();
+ let mut current = Some(first);
+ let found = loop {
+ let Some(sibling) = current.filter(|sibling| arrived.contains(sibling)) else {
+ break current;
+ };
+ if let Some(&known) = past_arrived.get(&sibling) {
+ break known;
+ }
+ walked.push(sibling);
+ current = next(sibling);
+ };
+ past_arrived.extend(walked.into_iter().map(|sibling| (sibling, found)));
+ found
+}
+
+/// The union of `bloom_of` over `first` and its later siblings, as `next` tells them, remembering in `known` what it
+/// finds for each one it walks over: a run of siblings is walked once, however many of them it is asked from.
+fn bloom_from(
+ first: StyleNodeID,
+ next: impl Fn(StyleNodeID) -> Option<StyleNodeID>,
+ bloom_of: impl Fn(StyleNodeID) -> u64,
+ known: &mut HashMap<StyleNodeID, u64>,
+) -> u64 {
+ let mut walked: SmallVec<[StyleNodeID; 16]> = SmallVec::new();
+ let mut current = Some(first);
+ let mut bloom = loop {
+ let Some(sibling) = current else {
+ break 0;
+ };
+ if let Some(&found) = known.get(&sibling) {
+ break found;
+ }
+ walked.push(sibling);
+ current = next(sibling);
+ };
+ for &sibling in walked.iter().rev() {
+ bloom |= bloom_of(sibling);
+ known.insert(sibling, bloom);
+ }
+ bloom
+}
+
/// The links a descendant is tested against, by the key an element matching each one's compound
/// carries, where it has one.
#[derive(Default)]
@@ -3573,6 +3652,31 @@ impl<'a> Follower<'a> {
self.evaluate(link, element, false).unwrap_or(true)
}
+ /// Keep of `effects` those whose ancestor keys the ancestors of `node` might carry on either side of the
+ /// transaction, whichever parents it had on them. The ancestors are read only where an effect asks.
+ fn retain_effects_around(&mut self, node: StyleNodeID, effects: &mut SmallVec<[HeldDependency; 16]>) {
+ if effects.iter().all(|held| held.ancestor_hashes.is_empty()) {
+ return;
+ }
+ let mut keys = self.ancestor_keys(node);
+ let mut ancestor = self.projection.parent_on(TransactionFactSide::Before, node);
+ while let Some(current) = ancestor {
+ if let Some(row) = self.row_before(current) {
+ keys.extend(super::ancestor_keys_of(row));
+ }
+ ancestor = self.projection.parent_on(TransactionFactSide::Before, current);
+ }
+ let mut ancestors = self.ancestors.take().unwrap_or_else(super::bloom::AncestorFilter::new);
+ for &key in &keys {
+ ancestors.insert(key);
+ }
+ effects.retain(|held| ancestors.might_contain_all(&held.ancestor_hashes));
+ for &key in &keys {
+ ancestors.remove(key);
+ }
+ *self.ancestors = Some(ancestors);
+ }
+
/// The keys the ancestors of `node` carry, now and before the transaction.
fn ancestor_keys(&self, node: StyleNodeID) -> SmallVec<[u32; 32]> {
let mut keys = SmallVec::new();
@@ -4251,3 +4355,59 @@ impl RetainedState {
.unwrap_or(0)
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::cell::Cell;
+
+ const SIBLINGS: u32 = 1000;
+
+ fn next_of(steps: &Cell<u32>) -> impl Fn(StyleNodeID) -> Option<StyleNodeID> + '_ {
+ |node| {
+ steps.set(steps.get() + 1);
+ let index = node.element_index().unwrap();
+ (index < SIBLINGS).then(|| StyleNodeID::element(index + 1))
+ }
+ }
+
+ #[test]
+ fn the_bloom_of_later_siblings_is_found_once_from_any_of_them() {
+ let bloom_of = |node: StyleNodeID| 1u64 << (node.element_index().unwrap() % 64);
+ let expected = |first: u32| (first..=SIBLINGS).fold(0, |bits, index| bits | 1u64 << (index % 64));
+ for firsts in [
+ vec![1; SIBLINGS as usize],
+ (1..=SIBLINGS).rev().collect(),
+ (1..=SIBLINGS).collect(),
+ ] {
+ let steps = Cell::new(0);
+ let mut known = HashMap::default();
+ for first in firsts {
+ let bloom = bloom_from(StyleNodeID::element(first), next_of(&steps), bloom_of, &mut known);
+ assert_eq!(bloom, expected(first));
+ }
+ assert!(steps.get() <= SIBLINGS);
+ }
+ }
+
+ #[test]
+ fn a_run_of_arrived_siblings_is_walked_once() {
+ let arrived: HashSet<StyleNodeID> = (1..SIBLINGS).map(StyleNodeID::element).collect();
+ let steps = Cell::new(0);
+ let mut past_arrived = HashMap::default();
+ for first in 1..SIBLINGS {
+ let found = first_not_arrived(
+ StyleNodeID::element(first),
+ &arrived,
+ next_of(&steps),
+ &mut past_arrived,
+ );
+ assert_eq!(found, Some(StyleNodeID::element(SIBLINGS)));
+ }
+ assert!(steps.get() <= SIBLINGS);
+
+ let all: HashSet<StyleNodeID> = (1..=SIBLINGS).map(StyleNodeID::element).collect();
+ let found = first_not_arrived(StyleNodeID::element(1), &all, next_of(&steps), &mut HashMap::default());
+ assert_eq!(found, None);
+ }
+}
diff --git a/Tests/LibWeb/Text/expected/css/sibling-move-in-quirks-mode.txt b/Tests/LibWeb/Text/expected/css/sibling-move-in-quirks-mode.txt
new file mode 100644
index 00000000000..b8787f3b98e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/sibling-move-in-quirks-mode.txt
@@ -0,0 +1,156 @@
+b, alone:
+ b: rgb(0, 0, 0)
+b, with a sibling before it:
+ b: rgb(1, 1, 1)
+b, without it:
+ b: rgb(0, 0, 0)
+b, with another sibling between:
+ b: rgb(0, 0, 0)
+b, without the one between:
+ b: rgb(1, 1, 1)
+b, finally alone:
+ b: rgb(0, 0, 0)
+c, alone:
+ c: rgb(0, 0, 0)
+c, with a sibling before it:
+ c: rgb(2, 2, 2)
+c, without it:
+ c: rgb(0, 0, 0)
+c, with another sibling between:
+ c: rgb(2, 2, 2)
+c, without the one between:
+ c: rgb(2, 2, 2)
+c, finally alone:
+ c: rgb(0, 0, 0)
+id, alone:
+ id: rgb(0, 0, 0)
+id, with a sibling before it:
+ id: rgb(3, 3, 3)
+id, without it:
+ id: rgb(0, 0, 0)
+id, with another sibling between:
+ id: rgb(0, 0, 0)
+id, without the one between:
+ id: rgb(3, 3, 3)
+id, finally alone:
+ id: rgb(0, 0, 0)
+LOWER, alone:
+ LOWER: rgb(0, 0, 0)
+LOWER, with a sibling before it:
+ LOWER: rgb(4, 4, 4)
+LOWER, without it:
+ LOWER: rgb(0, 0, 0)
+LOWER, with another sibling between:
+ LOWER: rgb(0, 0, 0)
+LOWER, without the one between:
+ LOWER: rgb(4, 4, 4)
+LOWER, finally alone:
+ LOWER: rgb(0, 0, 0)
+LATER, alone:
+ LATER: rgb(0, 0, 0)
+LATER, with a sibling before it:
+ LATER: rgb(5, 5, 5)
+LATER, without it:
+ LATER: rgb(0, 0, 0)
+LATER, with another sibling between:
+ LATER: rgb(5, 5, 5)
+LATER, without the one between:
+ LATER: rgb(5, 5, 5)
+LATER, finally alone:
+ LATER: rgb(0, 0, 0)
+capital, alone:
+ capital: rgb(0, 0, 0)
+capital, with a sibling before it:
+ capital: rgb(6, 6, 6)
+capital, without it:
+ capital: rgb(0, 0, 0)
+capital, with another sibling between:
+ capital: rgb(0, 0, 0)
+capital, without the one between:
+ capital: rgb(6, 6, 6)
+capital, finally alone:
+ capital: rgb(0, 0, 0)
+tag, alone:
+ tag: rgb(0, 0, 0)
+tag, with a sibling before it:
+ tag: rgb(7, 7, 7)
+tag, without it:
+ tag: rgb(0, 0, 0)
+tag, with another sibling between:
+ tag: rgb(0, 0, 0)
+tag, without the one between:
+ tag: rgb(7, 7, 7)
+tag, finally alone:
+ tag: rgb(0, 0, 0)
+attribute, alone:
+ attribute: rgb(0, 0, 0)
+attribute, with a sibling before it:
+ attribute: rgb(8, 8, 8)
+attribute, without it:
+ attribute: rgb(0, 0, 0)
+attribute, with another sibling between:
+ attribute: rgb(0, 0, 0)
+attribute, without the one between:
+ attribute: rgb(8, 8, 8)
+attribute, finally alone:
+ attribute: rgb(0, 0, 0)
+ancestor-class, alone:
+ ancestor-class: rgb(0, 0, 0)
+ancestor-class, with a sibling before it:
+ ancestor-class: rgb(9, 9, 9)
+ancestor-class, without it:
+ ancestor-class: rgb(0, 0, 0)
+ancestor-class, with another sibling between:
+ ancestor-class: rgb(0, 0, 0)
+ancestor-class, without the one between:
+ ancestor-class: rgb(9, 9, 9)
+ancestor-class, finally alone:
+ ancestor-class: rgb(0, 0, 0)
+ancestor-id, alone:
+ ancestor-id: rgb(0, 0, 0)
+ancestor-id, with a sibling before it:
+ ancestor-id: rgb(10, 10, 10)
+ancestor-id, without it:
+ ancestor-id: rgb(0, 0, 0)
+ancestor-id, with another sibling between:
+ ancestor-id: rgb(0, 0, 0)
+ancestor-id, without the one between:
+ ancestor-id: rgb(10, 10, 10)
+ancestor-id, finally alone:
+ ancestor-id: rgb(0, 0, 0)
+ancestor-tag, alone:
+ ancestor-tag: rgb(0, 0, 0)
+ancestor-tag, with a sibling before it:
+ ancestor-tag: rgb(11, 11, 11)
+ancestor-tag, without it:
+ ancestor-tag: rgb(0, 0, 0)
+ancestor-tag, with another sibling between:
+ ancestor-tag: rgb(0, 0, 0)
+ancestor-tag, without the one between:
+ ancestor-tag: rgb(11, 11, 11)
+ancestor-tag, finally alone:
+ ancestor-tag: rgb(0, 0, 0)
+ancestor-attribute, alone:
+ ancestor-attribute: rgb(0, 0, 0)
+ancestor-attribute, with a sibling before it:
+ ancestor-attribute: rgb(12, 12, 12)
+ancestor-attribute, without it:
+ ancestor-attribute: rgb(0, 0, 0)
+ancestor-attribute, with another sibling between:
+ ancestor-attribute: rgb(0, 0, 0)
+ancestor-attribute, without the one between:
+ ancestor-attribute: rgb(12, 12, 12)
+ancestor-attribute, finally alone:
+ ancestor-attribute: rgb(0, 0, 0)
+ancestor-lower, alone:
+ ancestor-lower: rgb(0, 0, 0)
+ancestor-lower, with a sibling before it:
+ ancestor-lower: rgb(13, 13, 13)
+ancestor-lower, without it:
+ ancestor-lower: rgb(0, 0, 0)
+ancestor-lower, with another sibling between:
+ ancestor-lower: rgb(0, 0, 0)
+ancestor-lower, without the one between:
+ ancestor-lower: rgb(13, 13, 13)
+ancestor-lower, finally alone:
+ ancestor-lower: rgb(0, 0, 0)
diff --git a/Tests/LibWeb/Text/input/css/sibling-move-in-quirks-mode.html b/Tests/LibWeb/Text/input/css/sibling-move-in-quirks-mode.html
new file mode 100644
index 00000000000..b52db1acad5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/sibling-move-in-quirks-mode.html
@@ -0,0 +1,70 @@
+<!DOCTYPE quirks>
+<style>
+ p { color: rgb(0, 0, 0); }
+
+ .a + .B { color: rgb(1, 1, 1); }
+ .a ~ .C { color: rgb(2, 2, 2); }
+ .a + #Id { color: rgb(3, 3, 3); }
+ .a + .lower { color: rgb(4, 4, 4); }
+ .a ~ .later { color: rgb(5, 5, 5); }
+ .A + .capital { color: rgb(6, 6, 6); }
+ .a + P.tag { color: rgb(7, 7, 7); }
+ .a + [DATA-Name] { color: rgb(8, 8, 8); }
+ .X .a + .ancestor-class { color: rgb(9, 9, 9); }
+ #Y .a + .ancestor-id { color: rgb(10, 10, 10); }
+ DIV .a + .ancestor-tag { color: rgb(11, 11, 11); }
+ [DATA-Z] .a + .ancestor-attribute { color: rgb(12, 12, 12); }
+ .x .a + .ancestor-lower { color: rgb(13, 13, 13); }
+</style>
+<div id="parent" class="x" data-z>
+ <p id="b" class="b"></p>
+ <p id="c" class="c"></p>
+ <p id="id"></p>
+ <p id="LOWER" class="LOWER"></p>
+ <p id="LATER" class="LATER"></p>
+ <p id="capital" class="capital"></p>
+ <p id="tag" class="tag"></p>
+ <p id="attribute" data-name></p>
+</div>
+<div id="y" class="x" data-z>
+ <p id="ancestor-class" class="ancestor-class"></p>
+ <p id="ancestor-id" class="ancestor-id"></p>
+ <p id="ancestor-tag" class="ancestor-tag"></p>
+ <p id="ancestor-attribute" class="ancestor-attribute"></p>
+ <p id="ancestor-lower" class="ancestor-lower"></p>
+</div>
+<script src="../include.js"></script>
+<script>
+test(() => {
+ // NB: In quirks mode, ids and classes compare case-insensitively, whichever way round the two are written.
+ const report = (title, ids) => {
+ internals.updateStyle();
+ println(`${title}:`);
+ for (const id of ids)
+ println(` ${id}: ${getComputedStyle(document.getElementById(id)).color}`);
+ };
+ const newA = () => {
+ const a = document.createElement("p");
+ a.className = "a";
+ return a;
+ };
+
+ for (const id of ["b", "c", "id", "LOWER", "LATER", "capital", "tag", "attribute", "ancestor-class", "ancestor-id", "ancestor-tag", "ancestor-attribute", "ancestor-lower"]) {
+ const a = newA();
+ report(`${id}, alone`, [id]);
+ document.getElementById(id).before(a);
+ report(`${id}, with a sibling before it`, [id]);
+ a.remove();
+ report(`${id}, without it`, [id]);
+
+ // The element comes to stand right after the sibling as the one between them leaves.
+ const between = document.createElement("p");
+ document.getElementById(id).before(a, between);
+ report(`${id}, with another sibling between`, [id]);
+ between.remove();
+ report(`${id}, without the one between`, [id]);
+ a.remove();
+ report(`${id}, finally alone`, [id]);
+ }
+});
+</script>