Skip to content

Truly independent web browser

LibWeb: Implement anchor-scope

Andreas Klinge9ae93cf5fef
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index cda8f34ac85..45203b8cb0b 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -5,6 +5,7 @@
*/
use super::*;
+use crate::css::style::tree::StyleNodeID;
use crate::painting::paint_read::{GeometryRead, PaintRead};
pub(super) fn axis_modes(style: StyleValues) -> (abspos_inputs::AbsposAxisMode, abspos_inputs::AbsposAxisMode) {
@@ -406,16 +407,30 @@ impl AbsposEngine<'_> {
let arena = self.callbacks.arena();
// A pseudo-element queries from its generator. Any other anonymous box stands for no element
// and has no tree scope to search.
- let querying_element = if arena.node_is_generated_for_pseudo_element(positioned_box) {
+ let is_pseudo_element = arena.node_is_generated_for_pseudo_element(positioned_box);
+ let querying_element = if is_pseudo_element {
arena.node_style_node(positioned_box)
} else {
arena.dom_node_style_node(positioned_box)
}?;
+ // NB: A pseudo-element is a descendant of its generator, so the generator's anchor-scope applies to it.
+ let querier_scope_start = if is_pseudo_element {
+ Some(querying_element)
+ } else {
+ arena.style_node_parent(querying_element)
+ };
let containing_block = arena.node_containing_block_if_live(positioned_box)?;
let mut scope_host = arena.tree_scope_host(querying_element);
loop {
+ // https://drafts.csswg.org/css-anchor-position-1/#anchor-scope
+ // NB: An anchor-scope element both keeps the matching anchor names of its subtree from everything outside
+ // it, and limits lookups from its descendants to anchors inside it. So an anchor is visible exactly
+ // when the nearest element that scopes its name around it also scopes the name around the querier.
+ let querier_scope =
+ querier_scope_start.and_then(|start| self.nearest_anchor_scope(start, anchor_name, scope_host));
let found = arena.last_element_with_anchor_name(scope_host, anchor_name, |&element| {
self.anchor_is_acceptable(arena.bound_row(element), positioned_box, containing_block)
+ && self.nearest_anchor_scope(element, anchor_name, scope_host) == querier_scope
});
if let Some(element) = found {
return Some(arena.bound_row(element));
@@ -424,6 +439,33 @@ impl AbsposEngine<'_> {
}
}
+ /// The nearest inclusive ancestor of `element` whose anchor-scope covers `anchor_name`, among the elements of the
+ /// tree scope `scope_host` names.
+ /// https://drafts.csswg.org/css-anchor-position-1/#anchor-scope
+ fn nearest_anchor_scope(
+ &self,
+ element: StyleNodeID,
+ anchor_name: usize,
+ scope_host: Option<StyleNodeID>,
+ ) -> Option<StyleNodeID> {
+ let arena = self.callbacks.arena();
+ let mut candidate = Some(element);
+ while let Some(current) = candidate {
+ if let Some(payloads) = arena.style_node_style_payloads(current) {
+ let anchor = ComputedValuesView::new(&payloads.groups).anchor();
+ // The <anchor-name> represents a strictly matched tree-scoped name, i.e. it can only match against
+ // anchor names in the same shadow tree.
+ if (anchor.anchor_scope_all || anchor.anchor_scope_names.raws().contains(&anchor_name))
+ && arena.tree_scope_host(current) == scope_host
+ {
+ return Some(current);
+ }
+ }
+ candidate = arena.style_node_parent(current);
+ }
+ None
+ }
+
/// Whether `anchor_box` may act as the anchor of `positioned_box`: it must have used values
/// from this pass, and its containing block chain must reach the positioned box's own.
fn anchor_is_acceptable(&self, anchor_box: Node, positioned_box: Node, containing_block: Node) -> bool {
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index fdee71cdeac..023a35a47cf 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -2167,6 +2167,11 @@ impl LayoutNodeArena {
.find(is_acceptable)
}
+ /// The parent element of `element` in the style tree, if any.
+ pub(crate) fn style_node_parent(&self, element: StyleNodeID) -> Option<StyleNodeID> {
+ self.with_style_store(|engine| engine.tree().parent(element))
+ }
+
/// The shadow host of the shadow tree `element` is in, or none in the document tree. A shadow
/// root has no layout row, so the tree scope is named by its host.
pub(crate) fn tree_scope_host(&self, element: StyleNodeID) -> Option<StyleNodeID> {
diff --git a/Tests/LibWeb/Text/expected/css/anchor-scope-pseudo-element.txt b/Tests/LibWeb/Text/expected/css/anchor-scope-pseudo-element.txt
new file mode 100644
index 00000000000..3245d052ad6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/anchor-scope-pseudo-element.txt
@@ -0,0 +1 @@
+::before left: 50px
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-scope-basic.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-scope-basic.txt
index 46655eadfb5..22c0bbf3ae7 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-scope-basic.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-scope-basic.txt
@@ -2,17 +2,16 @@ Harness status: OK
Found 12 tests
-3 Pass
-9 Fail
+12 Pass
Pass Can anchor to a name both defined and scoped by the same element
-Fail Sibling can not anchor into anchor-scope, even when anchor-name present
+Pass Sibling can not anchor into anchor-scope, even when anchor-name present
Pass anchor-scope:all on common ancestor
Pass anchor-scope:--a on common ancestor
-Fail anchor-scope:all on sibling
-Fail anchor-scope:all scopes multiple names
-Fail anchor-scope:--a,--b scopes --a and --b
-Fail anchor-scope:--a scopes only --a
-Fail anchor-scope:--b scopes only --b
-Fail anchor-scope:--a scopes only --a (out-of-flow anchors)
-Fail anchor-scope:--a scopes only --a (both out-of-flow and in-flow anchors)
-Fail anchor-scope:--a scopes only --a (both out-of-flow and in-flow anchors, reverse)
\ No newline at end of file
+Pass anchor-scope:all on sibling
+Pass anchor-scope:all scopes multiple names
+Pass anchor-scope:--a,--b scopes --a and --b
+Pass anchor-scope:--a scopes only --a
+Pass anchor-scope:--b scopes only --b
+Pass anchor-scope:--a scopes only --a (out-of-flow anchors)
+Pass anchor-scope:--a scopes only --a (both out-of-flow and in-flow anchors)
+Pass anchor-scope:--a scopes only --a (both out-of-flow and in-flow anchors, reverse)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/css/anchor-scope-pseudo-element.html b/Tests/LibWeb/Text/input/css/anchor-scope-pseudo-element.html
new file mode 100644
index 00000000000..a35e47e74c6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/anchor-scope-pseudo-element.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<style>
+#cb { position: relative; width: 300px; height: 100px; }
+#scope { anchor-scope: --a; }
+#scope::before { content: ""; position: absolute; width: 10px; height: 10px; left: anchor(--a left); top: 0; }
+.anchor { anchor-name: --a; width: 20px; height: 20px; }
+#inner { margin-left: 50px; }
+#outer { margin-left: 150px; }
+</style>
+<div id="cb">
+ <div id="scope"><div id="inner" class="anchor"></div></div>
+ <div id="outer" class="anchor"></div>
+</div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ println(`::before left: ${getComputedStyle(scope, "::before").left}`);
+ });
+</script>