Truly independent web browser
LibWeb: Support inline elements as CSS positioning anchors
Timothy Flynnc1720e4fbc3f
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rsindex 82782b57b3e..0b288e01dbf 100644--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs@@ -473,7 +473,7 @@ impl AbsposEngine<'_> {if anchor_box == positioned_box|| !arena.node_kind_if_live(anchor_box)- .is_some_and(super::node_facts::kind_is_box)+ .is_some_and(|kind| super::node_facts::kind_is_box(kind) || kind == NodeKind::InlineNode)|| !self.fragments.as_deref()diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 5f64c9ee2f7..7a113474da9 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -972,6 +972,38 @@ impl RunFragmentBuilder {}}+ pub(crate) fn register_inline_anchor_candidate(+ &self,+ node: crate::layout::node_data::NodeSlotId,+ border_box_rect: CssPixelRect,+ coordinate_space_box: crate::layout::node_data::NodeSlotId,+ ) {+ let mut inner = self.inner.borrow_mut();+ let candidate = AnchorCandidate {+ node,+ border_box_rect,+ coordinate_space_box,+ };+ if coordinate_space_box == self.root_node {+ inner.anchor_candidates_at_root.push(candidate);+ return;+ }+ match inner.pending_fragments.get_mut(&coordinate_space_box.slot_index()) {+ Some(pending_fragment) => pending_fragment.anchor_candidates.push(candidate),+ None => {+ if self.is_entry_accumulator {+ inner.anchor_candidates_at_root.push(candidate);+ } else {+ let mut pending_fragment = PendingFragment::new(coordinate_space_box);+ pending_fragment.anchor_candidates.push(candidate);+ inner+ .pending_fragments+ .insert(coordinate_space_box.slot_index(), pending_fragment);+ }+ }+ }+ }+pub(crate) fn register_inline_containing_block_rect(&self,inline_box: crate::layout::node_data::NodeSlotId,@@ -1187,9 +1219,25 @@ impl RunFragmentBuilder {entry.translate_by(offset);inner.pending_abspos_at_root.push(entry);}+ // Lookups outside the fragmentation context use the bounding rectangle of all fragments.+ // Combine candidates only after translating them into the multicol container's coordinate space.+ let mut anchor_indices: HashMap<_, _> = inner+ .anchor_candidates_at_root+ .iter()+ .enumerate()+ .map(|(index, candidate)| ((candidate.node, candidate.coordinate_space_box), index))+ .collect();for mut candidate in root.propagated_anchor_candidates {candidate.translate_by(offset);- inner.anchor_candidates_at_root.push(candidate);+ let key = (candidate.node, candidate.coordinate_space_box);+ if let Some(&index) = anchor_indices.get(&key) {+ inner.anchor_candidates_at_root[index]+ .border_box_rect+ .unite(candidate.border_box_rect);+ } else {+ anchor_indices.insert(key, inner.anchor_candidates_at_root.len());+ inner.anchor_candidates_at_root.push(candidate);+ }}for mut payload in root.propagated_inline_containing_block_rects {payload.translate_by(offset);diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 76adc0c29ce..3baf053657b 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -2257,6 +2257,7 @@ impl<'context> InlineFormattingContext<'context> {if self.run.fragments.is_some() {self.compute_inline_box_pieces();self.fold_inline_ancestor_relative_insets_into_line_data();+ self.register_inline_anchor_candidates();}self.leave_atomic_inlines_without_a_line_unplaced();formatting_context::derive_baselines(self.run.records, &self.callbacks, self.containing_block)@@ -2299,6 +2300,31 @@ impl<'context> InlineFormattingContext<'context> {}}+ fn register_inline_anchor_candidates(&self) {+ let Some(fragments) = self.run.fragments.as_deref() else {+ return;+ };+ // An inline anchor's border box is the union of its line fragments, including relative positioning.+ // Keep discovery order so that repeated layout runs produce the same propagated candidates.+ let mut candidates = Vec::<(Node, CssPixelRect)>::new();+ let mut indices = HashMap::<Node, usize>::default();+ for piece in &self.line_data().inline_box_pieces {+ if !self.facts(piece.node).has_anchor_names() {+ continue;+ }+ let rect = CssPixelRect::from(piece.border_box_rect).translated_by(piece.relpos_delta.into());+ if let Some(&index) = indices.get(&piece.node) {+ candidates[index].1.unite(rect);+ } else {+ indices.insert(piece.node, candidates.len());+ candidates.push((piece.node, rect));+ }+ }+ for (node, rect) in candidates {+ fragments.register_inline_anchor_candidate(node, rect, self.containing_block);+ }+ }+fn keep_lines_of_fragmentainer(&self,fragmentation: fragmentation::LineRootFragmentation,diff --git a/Tests/LibWeb/Text/expected/css/anchor-positioning-inline-columns.txt b/Tests/LibWeb/Text/expected/css/anchor-positioning-inline-columns.txtnew file mode 100644index 00000000000..103cc27c147--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/anchor-positioning-inline-columns.txt@@ -0,0 +1,4 @@+Two columns: anchor=200x40, left=true, top=true, width=true, height=true+Right-to-left columns: anchor=200x40, left=true, top=true, width=true, height=true+Offset nested inline: anchor=200x40, left=true, top=true, width=true, height=true+Absolute target outside columns: anchor=200x40, left=true, top=true, width=true, height=truediff --git a/Tests/LibWeb/Text/expected/css/anchor-positioning-inline.txt b/Tests/LibWeb/Text/expected/css/anchor-positioning-inline.txtnew file mode 100644index 00000000000..c67427c41e4--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/anchor-positioning-inline.txt@@ -0,0 +1,6 @@+Wrapped inline: left=true, top=true, width=true+Relative inline: left=true, top=true, width=true+Single line: left=true, top=true, width=true+Positioned ancestor: left=true, top=true, width=true+Absolute tooltip: left=true, top=true, width=true+Restored anchor with fallbacks: left=true, top=true, width=truediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-name-inline-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-name-inline-001.txtindex e83d35d2853..182ed04d5eb 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-name-inline-001.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-anchor-position/anchor-name-inline-001.txt@@ -2,12 +2,12 @@ Harness status: OKFound 7 tests-3 Pass-4 Fail+5 Pass+2 FailFail .target 1Fail .target 2-Fail .target 3-Fail .target 4+Pass .target 3+Pass .target 4Pass .target 5Pass .target 6Pass .target 7\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/css/anchor-positioning-inline-columns.html b/Tests/LibWeb/Text/input/css/anchor-positioning-inline-columns.htmlnew file mode 100644index 00000000000..af3d560f0f0--- /dev/null+++ b/Tests/LibWeb/Text/input/css/anchor-positioning-inline-columns.html@@ -0,0 +1,51 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @font-face { font-family: Ahem; src: url(../../../Ref/data/fonts/Ahem.ttf); }+ body { margin: 0; }+ #columns {+ columns: 2;+ column-gap: 20px;+ column-fill: auto;+ width: 220px;+ height: 40px;+ margin: 40px 60px;+ font: 20px/20px Ahem;+ }+ #anchor { anchor-name: --a; }+ #target {+ position: fixed;+ position-anchor: --a;+ left: anchor(left);+ top: anchor(bottom);+ width: anchor-size(width);+ height: anchor-size(height);+ }+</style>+<div id="columns"><div id="nested"><span id="anchor">aa<br>bbb<br>cccc<br>d</span></div></div>+<div id="target"></div>+<script>+ promiseTest(async () => {+ await document.fonts.ready;+ const columns = document.getElementById("columns");+ const anchor = document.getElementById("anchor");+ const target = document.getElementById("target");+ function check(label) {+ const a = anchor.getBoundingClientRect();+ const t = target.getBoundingClientRect();+ println(`${label}: anchor=${a.width}x${a.height}, left=${t.left === a.left}, top=${t.top === a.bottom}, width=${t.width === a.width}, height=${t.height === a.height}`);+ }++ check("Two columns");+ columns.style.direction = "rtl";+ check("Right-to-left columns");+ columns.style.direction = "ltr";+ document.getElementById("nested").style.marginLeft = "10px";+ anchor.style.position = "relative";+ anchor.style.left = "5px";+ anchor.style.top = "3px";+ check("Offset nested inline");+ target.style.position = "absolute";+ check("Absolute target outside columns");+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/anchor-positioning-inline.html b/Tests/LibWeb/Text/input/css/anchor-positioning-inline.htmlnew file mode 100644index 00000000000..b3866e9e748--- /dev/null+++ b/Tests/LibWeb/Text/input/css/anchor-positioning-inline.html@@ -0,0 +1,49 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body { margin: 0; }+ #container { margin: 60px 80px; width: 140px; font: 20px/30px monospace; }+ #anchor { padding: 2px 4px; border: 1px solid; }+ #tooltip {+ position: fixed;+ position-anchor: --commit;+ top: calc(anchor(bottom) + 4px);+ left: calc(anchor(left) - 0px);+ width: anchor-size(width);+ height: 30px;+ }+</style>+<div id="container"><a id="anchor">Commit message</a></div>+<div id="tooltip"></div>+<script>+ test(() => {+ const anchor = document.getElementById("anchor");+ const tooltip = document.getElementById("tooltip");+ function check(label) {+ const a = anchor.getBoundingClientRect();+ const t = tooltip.getBoundingClientRect();+ println(`${label}: left=${t.left === a.left}, top=${t.top === a.bottom + 4}, width=${t.width === a.width}`);+ }++ // Hovercards assign the anchor name after the link has already been laid out.+ anchor.getBoundingClientRect();+ anchor.style.anchorName = "--commit";+ check("Wrapped inline");+ anchor.style.position = "relative";+ anchor.style.left = "15px";+ anchor.style.top = "10px";+ check("Relative inline");+ document.getElementById("container").style.width = "400px";+ check("Single line");+ document.getElementById("container").style.position = "relative";+ check("Positioned ancestor");+ document.getElementById("container").appendChild(tooltip);+ tooltip.style.position = "absolute";+ check("Absolute tooltip");+ anchor.style.anchorName = "none";+ anchor.getBoundingClientRect();+ anchor.style.anchorName = "--commit";+ tooltip.style.positionTryFallbacks = "flip-block, flip-inline";+ check("Restored anchor with fallbacks");+ });+</script>