Truly independent web browser
LibWeb: Clip abspos descendants of positioned inlines
sideshowbarker0c236b934e20
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex d9be757df3e..74b8636271f 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -500,12 +500,6 @@ pub(crate) fn place_child();return None;};- fragments.note_placed_box(- &run.pass,- node,- placement,- cells_kept_for_contained_abspos(run, node, || placed_child.geometry.cells),- );let PlacedChild {mut geometry,facts_decided_by_parent,@@ -543,6 +537,12 @@ pub(crate) fn place_child(Some(root) => fragments.take_in_child_root_at_placement(node, root, pass),None => fragments.open_fragment_of_placed_box(node),}+ fragments.note_placed_box(+ &run.pass,+ node,+ placement,+ cells_kept_for_contained_abspos(run, node, || geometry.cells),+ );let mut abspos_boxes_placed_with_it = Vec::new();loop {let batch = fragments.take_drainable_abspos(node, pass);@@ -598,7 +598,10 @@ fn cells_kept_for_contained_abspos(let data = run.pass.node_data(node);let may_contain_abspos = node_facts::has_flag(data, NodeFlag::EstablishesAbsolutePositionContainingBlock)|| node_facts::has_flag(data, NodeFlag::EstablishesFixedPositionContainingBlock)- || data.parent.get().is_invalid();+ || data.parent.get().is_invalid()+ || run+ .fragments+ .is_some_and(|fragments| fragments.any_pending_abspos_contained_by(node));may_contain_abspos.then(|| fragment_tree::CellsInFrame {cells: cells_in_run_frame(),frame: run.pass.frame,@@ -4035,18 +4038,18 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {let placement = facts_decided_by_parent.placement.expect("the root of a partial relayout keeps the placement it committed");+ let Some(run_root_fragment) = fragment_arrival else {+ panic!("the root of a partial relayout is laid out by a run of its own");+ };+ fragments.take_in_child_root_at_placement(root, run_root_fragment, pass);+ // The retained offset already includes relative positioning. Publish it directly instead+ // of applying placement adjustments a second time.fragments.note_placed_box(&run.pass,root,placement,cells_kept_for_contained_abspos(run, root, || root_geometry.cells),);- // The retained offset already includes relative positioning. Publish it directly instead- // of applying placement adjustments a second time.- let Some(run_root_fragment) = fragment_arrival else {- panic!("the root of a partial relayout is laid out by a run of its own");- };- fragments.take_in_child_root_at_placement(root, run_root_fragment, pass);fragments.build_fragment_for_placed_box(pass,root,diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 3b3a2b19743..513891fd067 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -1850,6 +1850,21 @@ impl RunFragmentBuilder {.any(|entry| !entry.inline_containing_block().is_invalid())}+ pub(crate) fn any_pending_abspos_contained_by(&self, node: crate::layout::node_data::NodeSlotId) -> bool {+ let inner = self.inner.borrow();+ inner+ .pending_abspos_at_root+ .iter()+ .chain(+ inner+ .pending_fragments+ .get(&node.slot_index())+ .into_iter()+ .flat_map(|fragment| fragment.pending_abspos.iter()),+ )+ .any(|entry| entry.containing_block() == node)+ }+pub(crate) fn any_pending_abspos_names_inline_containing_block(&self,inline_box: crate::layout::node_data::NodeSlotId,diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex c57c74f7c4e..6542b51bb31 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -3969,7 +3969,13 @@ impl LayoutNodeArena {let data = self.data(node);let kind = data.kind.get();if super::node_facts::kind_is_box(kind) {- if super::node_facts::has_flag(data, establishes_containing_block) {+ if super::node_facts::has_flag(data, establishes_containing_block)+ || (!search.inline_containing_block.is_invalid()+ && super::node_facts::node_forms_containing_block_for_children(+ data,+ super::node_facts::node_style_view(data),+ ))+ {search.containing_block = node;break;}@@ -4006,15 +4012,7 @@ impl LayoutNodeArena {fn derive_containing_block_establishment_flags(&mut self, node: NodeSlotId) {let data = self.data(node);let previous_flags = data.flags.get();- let (absolute, fixed) = if data.kind.get() == NodeKind::InlineNode {- let absolute = !super::node_facts::has_flag(data, NodeFlag::Anonymous)- && self- .node_style_if_live(node)- .is_some_and(crate::painting::style_queries::inline_establishes_absolute_position_containing_block);- (absolute, false)- } else {- crate::painting::style_queries::establishes_positioning_containing_blocks(self, node)- };+ let (absolute, fixed) = crate::painting::style_queries::establishes_positioning_containing_blocks(self, node);let establishment_flags = NodeFlag::EstablishesAbsolutePositionContainingBlock as u32| NodeFlag::EstablishesFixedPositionContainingBlock as u32;let mut flags = previous_flags & !establishment_flags;diff --git a/Libraries/LibWeb/Rust/src/painting/paint_read.rs b/Libraries/LibWeb/Rust/src/painting/paint_read.rsindex dcfea61f7b4..237ae77c715 100644--- a/Libraries/LibWeb/Rust/src/painting/paint_read.rs+++ b/Libraries/LibWeb/Rust/src/painting/paint_read.rs@@ -236,10 +236,10 @@ pub(crate) trait PaintRead: GeometryRead {}fn stacking_context_entries(&self, root: NodeSlotId) -> Option<impl Deref<Target = StackingContextEntries> + '_>;- /// The box whose content box a node is laid out against: an in-flow node's is its nearest- /// ancestor that forms a containing block for its children, and a positioned box's its nearest- /// ancestor box that establishes one for its position, or the root for a fixed-position box- /// without one. Layout and painting both walk to it with this.+ /// The box whose content box supplies a node's layout coordinate space. An in-flow node uses the nearest ancestor+ /// that forms a containing block for its children. An absolutely positioned node uses its positioning containing+ /// block, or that inline's in-flow containing block when the positioning containing block is inline. A fixed-position+ /// node uses the nearest ancestor that establishes a fixed-position containing block, or the root if there isn't one.fn node_containing_block_if_live(&self, id: NodeSlotId) -> Option<NodeSlotId> {use crate::css::css_enums::positioning;let mut row = self.node(id)?;@@ -267,9 +267,13 @@ pub(crate) trait PaintRead: GeometryRead {let Some(ancestor_row) = self.node(ancestor) else {break;};- if node_facts::kind_is_box(ancestor_row.kind()) && ancestor_row.flags() & establishes_containing_block != 0- {- return Some(current);+ if ancestor_row.flags() & establishes_containing_block != 0 {+ if node_facts::kind_is_box(ancestor_row.kind()) {+ return Some(current);+ }+ if ancestor_row.kind() == NodeKind::InlineNode {+ return self.node_containing_block_if_live(current);+ }}row = ancestor_row;}diff --git a/Libraries/LibWeb/Rust/src/painting/style_queries.rs b/Libraries/LibWeb/Rust/src/painting/style_queries.rsindex 1a847976f70..1c027a52bfe 100644--- a/Libraries/LibWeb/Rust/src/painting/style_queries.rs+++ b/Libraries/LibWeb/Rust/src/painting/style_queries.rs@@ -310,7 +310,7 @@ fn style_establishes_fixed_positioning_containing_block(false}-pub(crate) fn inline_establishes_absolute_position_containing_block(style: ComputedValuesView<'_>) -> bool {+fn inline_establishes_absolute_position_containing_block(style: ComputedValuesView<'_>) -> bool {let effects = style.effects();style.position() != positioning::STATIC|| filter_has_filters(&effects.filter)@@ -323,6 +323,13 @@ pub(crate) fn establishes_positioning_containing_blocks(arena: &impl PaintRead,return (false, false);};let kind = row.kind();+ if kind == NodeKind::InlineNode {+ let absolute = !node_facts::has_flag(&row, NodeFlag::Anonymous)+ && row+ .style()+ .is_some_and(inline_establishes_absolute_position_containing_block);+ return (absolute, false);+ }if !node_facts::kind_is_box(kind) {return (false, false);}diff --git a/Tests/LibWeb/Ref/expected/abspos-inline-containing-block-clip-ref.html b/Tests/LibWeb/Ref/expected/abspos-inline-containing-block-clip-ref.htmlnew file mode 100644index 00000000000..38d73f31ef0--- /dev/null+++ b/Tests/LibWeb/Ref/expected/abspos-inline-containing-block-clip-ref.html@@ -0,0 +1,58 @@+<!doctype html>+<style>+ body {+ margin: 0;+ }+ .outer {+ position: relative;+ width: 300px;+ height: 40px;+ }+ .clip {+ overflow: hidden;+ height: 40px;+ }+ .inline {+ position: relative;+ font: 20px/20px monospace;+ }+ .target {+ position: absolute;+ left: 0;+ top: 0;+ width: 300px;+ height: 20px;+ background: green;+ }+ .static {+ position: static;+ }+ .static .target {+ width: 600px;+ }+ .block {+ display: block;+ }+</style>+<div class="outer">+ <div class="clip">+ <span class="inline">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline block">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline static">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline"+ >text<span><span class="target"></span></span+ ></span>+ </div>+</div>diff --git a/Tests/LibWeb/Ref/input/abspos-inline-containing-block-clip.html b/Tests/LibWeb/Ref/input/abspos-inline-containing-block-clip.htmlnew file mode 100644index 00000000000..01cf0a75b6a--- /dev/null+++ b/Tests/LibWeb/Ref/input/abspos-inline-containing-block-clip.html@@ -0,0 +1,56 @@+<!doctype html>+<link rel="match" href="../expected/abspos-inline-containing-block-clip-ref.html" />+<style>+ body {+ margin: 0;+ }+ .outer {+ position: relative;+ width: 300px;+ height: 40px;+ }+ .clip {+ overflow: hidden;+ height: 40px;+ }+ .inline {+ position: relative;+ font: 20px/20px monospace;+ }+ .target {+ position: absolute;+ left: 0;+ top: 0;+ width: 600px;+ height: 20px;+ background: green;+ }+ .static {+ position: static;+ }+ .block {+ display: block;+ }+</style>+<div class="outer">+ <div class="clip">+ <span class="inline">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline block">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline static">text<span class="target"></span></span>+ </div>+</div>+<div class="outer">+ <div class="clip">+ <span class="inline"+ >text<span><span class="target"></span></span+ ></span>+ </div>+</div>diff --git a/Tests/LibWeb/Text/expected/abspos-inline-containing-block-clip.txt b/Tests/LibWeb/Text/expected/abspos-inline-containing-block-clip.txtnew file mode 100644index 00000000000..2d3fc272966--- /dev/null+++ b/Tests/LibWeb/Text/expected/abspos-inline-containing-block-clip.txt@@ -0,0 +1,11 @@+inline: scrollWidth 300, inside true, outside false+inline made static: scrollWidth 600, inside true, outside true+inline made relative: scrollWidth 300, inside true, outside false+block: scrollWidth 300, inside true, outside false+static inline: scrollWidth 600, inside true, outside true+nested inline: scrollWidth 300, inside true, outside false+block inside inline: scrollWidth 300, inside true, outside false+will-change inline: scrollWidth 300, inside true, outside false+fixed child: scrollWidth 300, inside true, outside true+pseudo-element: scrollWidth 300+pseudo-element with block: scrollWidth 300diff --git a/Tests/LibWeb/Text/input/abspos-inline-containing-block-clip.html b/Tests/LibWeb/Text/input/abspos-inline-containing-block-clip.htmlnew file mode 100644index 00000000000..d614d375ddc--- /dev/null+++ b/Tests/LibWeb/Text/input/abspos-inline-containing-block-clip.html@@ -0,0 +1,82 @@+<!doctype html>+<script src="include.js"></script>+<style>+ body {+ margin: 0;+ }+ .outer {+ position: relative;+ width: 300px;+ height: 40px;+ }+ .clip {+ overflow: hidden;+ height: 40px;+ }+ .inline {+ position: relative;+ font: 20px/20px monospace;+ }+ .target {+ position: absolute;+ left: 0;+ top: 0;+ width: 600px;+ height: 20px;+ }+ .category::after {+ content: "";+ position: absolute;+ left: 0;+ width: 600px;+ height: 4px;+ }+</style>+<script>+ test(() => {+ function check(label, inlineStyle, targetStyle = "", wrapperTag = "") {+ const outer = document.createElement("div");+ outer.className = "outer";+ outer.innerHTML = `<div class="clip"><span class="inline" style="${inlineStyle}">text<span class="target" style="${targetStyle}"></span></span></div>`;+ document.body.appendChild(outer);+ const inline = outer.querySelector(".inline");+ const target = outer.querySelector(".target");+ if (wrapperTag) {+ const wrapper = document.createElement(wrapperTag);+ inline.insertBefore(wrapper, target);+ wrapper.appendChild(target);+ }+ function measure(state) {+ const rect = target.getBoundingClientRect();+ println(+ `${label}${state}: scrollWidth ${outer.scrollWidth}, inside ${document.elementFromPoint(150, rect.top + 5) === target}, outside ${document.elementFromPoint(450, rect.top + 5) === target}`+ );+ }+ measure("");+ if (label === "inline") {+ inline.style.position = "static";+ measure(" made static");+ inline.style.position = "relative";+ measure(" made relative");+ }+ outer.remove();+ }+ check("inline", "");+ check("block", "display: block");+ check("static inline", "position: static");+ check("nested inline", "", "", "span");+ check("block inside inline", "", "", "div");+ check("will-change inline", "position: static; will-change: position");+ check("fixed child", "", "position: fixed");++ const scroller = document.createElement("div");+ scroller.style = "width: 300px; overflow: auto";+ scroller.innerHTML =+ '<div class="outer"><div class="clip"><a class="inline"><span class="category">inline</span></a></div></div>';+ document.body.appendChild(scroller);+ println(`pseudo-element: scrollWidth ${scroller.scrollWidth}`);+ scroller.querySelector("a").style.display = "block";+ println(`pseudo-element with block: scrollWidth ${scroller.scrollWidth}`);+ scroller.remove();+ });+</script>