Skip to content

Truly independent web browser

LibWeb: Key invalidation through a one-compound :is() or :where()

sideshowbarker4b127496becd
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
index 15919cee0b3..ea80816080a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/invalidation.rs
@@ -611,6 +611,14 @@ pub(super) fn required_key(compound: &CompoundRef<'_>) -> Option<DispatchKey> {
))
}
SimpleSelector::TagName(name) if name.name_is_ascii_lowercase => Some(DispatchKey::TagName(name.name)),
+ // NB: Only an argument list of one compound requires what that compound does: Of a list of several, an
+ // element matches whichever alternative it carries, and a compound left of a combinator is an
+ // ancestor's or a sibling's.
+ SimpleSelector::PseudoClass(pseudo_class)
+ if matches!(pseudo_class.pseudo_class, PseudoClassType::Is | PseudoClassType::Where) =>
+ {
+ sole_compound(pseudo_class.argument_selector_list).and_then(|compound| required_key(&compound))
+ }
SimpleSelector::PseudoClass(pseudo_class) => {
state_fact_for(pseudo_class.pseudo_class).map(DispatchKey::State)
}
@@ -619,6 +627,13 @@ pub(super) fn required_key(compound: &CompoundRef<'_>) -> Option<DispatchKey> {
.min_by_key(|&key| dispatch_selectivity(key))
}
+/// The one compound `list` consists of, where it holds a single selector with no combinator.
+fn sole_compound(list: SelectorListView<'_>) -> Option<CompoundRef<'_>> {
+ let mut selectors = list.iter();
+ let selector = selectors.next()?;
+ (selectors.next().is_none() && selector.left().is_none()).then(|| selector.first_compound())
+}
+
/// Whether every element matches `compound`, as far as its own facts go.
fn matches_every_element(compound: &CompoundRef<'_>) -> bool {
compound
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/where-is-subject-class-invalidation.txt b/Tests/LibWeb/Text/expected/css/style-engine/where-is-subject-class-invalidation.txt
new file mode 100644
index 00000000000..fc50ec2dcc3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/where-is-subject-class-invalidation.txt
@@ -0,0 +1,4 @@
+colors with .prose: rgb(0, 128, 0) rgb(0, 0, 255)
+invalidation tests don't grow with unrelated elements: true
+color without .prose: rgb(0, 0, 0)
+alternatives with .other: rgb(128, 0, 128) rgb(128, 0, 128)
diff --git a/Tests/LibWeb/Text/input/css/style-engine/where-is-subject-class-invalidation.html b/Tests/LibWeb/Text/input/css/style-engine/where-is-subject-class-invalidation.html
new file mode 100644
index 00000000000..fcc0debaabe
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/where-is-subject-class-invalidation.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<style>
+ .prose :where(.one) { color: rgb(0, 128, 0) }
+ .prose :is(.two):not(:where(.skip)) { color: rgb(0, 0, 255) }
+ .other :is(.three, .four) { color: rgb(128, 0, 128) }
+</style>
+<div id="root">
+ <div id="one" class="one"></div>
+ <div id="two" class="two"></div>
+</div>
+<div id="alternatives">
+ <div id="three" class="three"></div>
+ <div id="four" class="four"></div>
+</div>
+<script>
+ test(() => {
+ const root = document.getElementById("root");
+
+ function toggleProse(on) {
+ root.classList.toggle("prose", on);
+ internals.updateStyle();
+ }
+
+ function measure() {
+ toggleProse(true);
+ toggleProse(false);
+ internals.resetStyleCounters();
+ toggleProse(true);
+ const counters = internals.styleCounters();
+ const colors = ["one", "two"].map(id => getComputedStyle(document.getElementById(id)).color);
+ toggleProse(false);
+ return { tests: counters.invalidationTests, colors: colors.join(" ") };
+ }
+
+ function growTo(size) {
+ const fragment = document.createDocumentFragment();
+ for (let index = root.childElementCount; index < size; ++index)
+ fragment.appendChild(document.createElement("div"));
+ root.appendChild(fragment);
+ internals.updateStyle();
+ }
+
+ const measurements = [];
+ // An element without .one or .two can't start matching the first two rules, however many there are.
+ for (const size of [64, 1024]) {
+ growTo(size);
+ measurements.push(measure());
+ }
+
+ println(`colors with .prose: ${measurements[0].colors}`);
+ println(`invalidation tests don't grow with unrelated elements: ${measurements[1].tests === measurements[0].tests}`);
+ toggleProse(false);
+ println(`color without .prose: ${getComputedStyle(document.getElementById("one")).color}`);
+
+ // Of a list of alternatives, an element matches whichever one it carries.
+ const alternatives = document.getElementById("alternatives");
+ alternatives.classList.add("other");
+ internals.updateStyle();
+ println(`alternatives with .other: ${["three", "four"].map(id => getComputedStyle(document.getElementById(id)).color).join(" ")}`);
+ });
+</script>