Skip to content

Truly independent web browser

LibWeb: Ignore rules from other tree scopes when invalidating style

Tim Ledbetter0f0f67328c70
diff --git a/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs b/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs
index fea66cff4dd..8935af31f39 100644
--- a/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/instrumentation.rs
@@ -296,6 +296,7 @@ define_counters! {
RoutedEntryPoints => "routedEntryPoints",
OriginTruthRoutesFired => "originTruthRoutesFired",
OriginTruthRoutesSkipped => "originTruthRoutesSkipped",
+ ScopeConfinedRoutesSkipped => "scopeConfinedRoutesSkipped",
ArrivingNodeFactsFolded => "arrivingNodeFactsFolded",
SheetChangeCandidatesRejected => "sheetChangeCandidatesRejected",
RelationalAnchorsConsidered => "relationalAnchorsConsidered",
diff --git a/Libraries/LibWeb/Rust/src/css/style/program.rs b/Libraries/LibWeb/Rust/src/css/style/program.rs
index 2e3fb455690..ec6a042a48f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/program.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/program.rs
@@ -1501,6 +1501,24 @@ impl StyleSheetProgram {
self.scopes_using_document_sheets.contains(tree_scope.0 as usize)
}
+ #[must_use]
+ pub fn sheet_rules_apply_in_scope(&self, sheet: SheetID, tree_scope: TreeScopeID) -> bool {
+ self.sheet_is_attached_in_scope(sheet, tree_scope)
+ || (tree_scope != TreeScopeID::DOCUMENT
+ && self.sheet_is_attached_in_scope(sheet, TreeScopeID::DOCUMENT)
+ && (matches!(self.sheet_origin(sheet), CascadeOrigin::UserAgent | CascadeOrigin::User)
+ || self.scope_uses_document_sheets(tree_scope)))
+ }
+
+ fn sheet_is_attached_in_scope(&self, sheet: SheetID, tree_scope: TreeScopeID) -> bool {
+ let attachments = self.sheet_attachments(sheet);
+ let sheets = self.sheets_in_scope(tree_scope);
+ match attachments.len() <= sheets.len() {
+ true => attachments.iter().any(|attachment| attachment.tree_scope == tree_scope),
+ false => sheets.contains(&sheet),
+ }
+ }
+
/// The attachment of a sheet in one tree scope, if it is attached there.
#[must_use]
pub fn attachment_in_scope(&self, sheet: SheetID, tree_scope: TreeScopeID) -> Option<Attachment> {
diff --git a/Libraries/LibWeb/Rust/src/css/style/routing.rs b/Libraries/LibWeb/Rust/src/css/style/routing.rs
index 2146c9cbe91..b0aab4b116b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/routing.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/routing.rs
@@ -234,6 +234,16 @@ impl RetainedState {
{
continue;
}
+ let sheet = self.program.rule_sheet(rule);
+ if !self.programs.get(selector_program).can_leave_its_scope()
+ && !changed_sheets.contains(&sheet)
+ && !self
+ .program
+ .sheet_rules_apply_in_scope(sheet, self.tree.tree_scope(node))
+ {
+ self.counters.bump(Counter::ScopeConfinedRoutesSkipped);
+ continue;
+ }
let path = routing.path_of(route);
if !is_arrival
&& point.anchor.is_none()
diff --git a/Tests/LibWeb/Text/expected/css/style-graph-document-rule-shadow-tree-routing.txt b/Tests/LibWeb/Text/expected/css/style-graph-document-rule-shadow-tree-routing.txt
new file mode 100644
index 00000000000..2fc44571f16
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-graph-document-rule-shadow-tree-routing.txt
@@ -0,0 +1 @@
+reactions: wholeDocument=false elements=
diff --git a/Tests/LibWeb/Text/input/css/style-graph-document-rule-shadow-tree-routing.html b/Tests/LibWeb/Text/input/css/style-graph-document-rule-shadow-tree-routing.html
new file mode 100644
index 00000000000..a92c173a67a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-graph-document-rule-shadow-tree-routing.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ :checked ~ div > * { color: green }
+</style>
+<div id="host"></div>
+<script>
+ test(() => {
+ const shadowRoot = document.getElementById("host").attachShadow({ mode: "open" });
+ shadowRoot.innerHTML = `<div><input type="checkbox"><label id="label"></label><button id="button"></button></div>`;
+ document.body.offsetWidth;
+ internals.styleEngineTransactionReactions();
+
+ // A document rule never matches inside a shadow tree, so checking a box there restyles
+ // nothing through it.
+ shadowRoot.querySelector("input").checked = true;
+ const reactions = internals.styleEngineTransactionReactions();
+ println(`reactions: wholeDocument=${reactions.wholeDocument} elements=${reactions.elements.join(",")}`);
+ });
+</script>