Skip to content

Truly independent web browser

LibWeb: Resolve line clamp clips in layout

Aliaksandr Kalenik715cb96bf184
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 013f1985092..d37cbb06fbe 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3267,7 +3267,7 @@ impl<'pass> BlockFormattingContext<'pass> {
.track_percentage_block_size_dependency_while_laying_out(node);
let mut fragment_facts = run_state::FragmentFactsDecidedByParent::default();
if self.laying_out_invisible_line_clamp_content.get() {
- fragment_facts.is_invisible_for_line_clamp = true;
+ fragment_facts.hide_after_clamp_point();
}
if facts.is_anonymous_button_content_wrapper() {
let only_for_button_content_alignment = block_container
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
index a0cb36a276e..0083941a7a7 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use crate::css::computed_value_views::ComputedValuesView;
use crate::css::css_enums::{overflow, positioning, writing_mode};
use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixels};
use crate::css::display::FfiDisplay;
@@ -75,6 +74,15 @@ fn inflated_by(rect: CssPixelRect, sides: FfiPixelBox) -> CssPixelRect {
rect.inflated(sides.top, sides.right, sides.bottom, sides.left)
}
+/// The physical border box of `fragment` around its physical content box `content_box`, where the fragment holds its
+/// geometry in `frame`.
+pub(crate) fn physical_border_box_around(content_box: CssPixelRect, fragment: &Fragment, frame: Frame) -> CssPixelRect {
+ inflated_by(
+ inflated_by(content_box, physical_padding(fragment, frame)),
+ physical_border_box_edges(fragment, frame),
+ )
+}
+
struct BoxesRelativeToOwnPaddingBox {
content_size: FfiCssPixelSize,
padding_box: CssPixelRect,
@@ -215,18 +223,6 @@ pub(crate) fn holds_links_of_outer_containing_blocks(node: NodeSlotId, children:
.any(|link| !link.containing_block.is_invalid() && link.containing_block != node)
}
-fn line_clamp_clip_rect(style: Option<ComputedValuesView<'_>>, content_box: CssPixelRect) -> CssPixelRect {
- let extent = CssPixels::from_integer((crate::css::css_pixels::MAX_INTEGER_VALUE / 4) as i64);
- let writing_mode = style.map_or(writing_mode::HORIZONTAL_TB, |style| style.writing_mode());
- if writing_mode == writing_mode::HORIZONTAL_TB {
- CssPixelRect::new(-extent, -extent, extent * 2, content_box.bottom() + extent)
- } else if style.is_some_and(|style| style.block_axis_is_reverse()) {
- CssPixelRect::new(content_box.x, -extent, extent - content_box.x, extent * 2)
- } else {
- CssPixelRect::new(-extent, -extent, content_box.right() + extent, extent * 2)
- }
-}
-
// NB: The nearest line-clamp container among a hidden box's ancestors is the one whose formatting context hid it.
fn hidden_content_is_scrollable_overflow(
arena: &LayoutNodeArena,
@@ -334,8 +330,12 @@ pub(crate) fn scrollable_overflow_of<'link>(
}
let box_is_scroll_container = node_facts::kind_and_style_make_scroll_container(kind, style);
- let line_clamp_clip = (fragment.has_line_clamp_point && !fragment.clamped_content_is_scrollable_overflow)
- .then(|| line_clamp_clip_rect(style, own.content_box));
+ let line_clamp_clip =
+ (fragment.has_line_clamp_point && !fragment.clamped_content_is_scrollable_overflow).then(|| {
+ Frame::of(style.map_or(writing_mode::HORIZONTAL_TB, |style| style.writing_mode()))
+ .half_plane_before_block_end(own.content_size)
+ .translated_by(own.content_box.location())
+ });
let clip_in_flow = |rect: CssPixelRect| line_clamp_clip.map_or(rect, |clip| rect.intersected(clip));
let overflow_directions = style.map_or_else(Default::default, physical_overflow_directions_of_style);
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 2502f0d54dc..e5aed8a2b7e 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -61,6 +61,7 @@ pub(crate) struct FragmentLink {
pub(crate) table_column_span: u32,
pub(crate) hidden_by_collapsed_columns: bool,
pub(crate) is_invisible_for_line_clamp: bool,
+ pub(crate) clip_decided_by_parent: Option<CssPixelRect>,
pub(crate) containing_line_box_index: Option<u32>,
/// Shared, as few links place an absolutely positioned box, the inputs are larger than the rest of the link, and
/// layout run cache entries keep copies of links.
@@ -111,6 +112,14 @@ impl Fragment {
pub(crate) fn with_scrollable_overflow(
&self,
scrollable_overflow: fragment_overflow::FragmentScrollableOverflow,
+ ) -> Self {
+ self.with_scrollable_overflow_and_children(scrollable_overflow, self.children.clone())
+ }
+
+ fn with_scrollable_overflow_and_children(
+ &self,
+ scrollable_overflow: fragment_overflow::FragmentScrollableOverflow,
+ children: Vec<FragmentLink>,
) -> Self {
Self {
identity: self.identity,
@@ -143,7 +152,7 @@ impl Fragment {
continues_in_next_fragment: self.continues_in_next_fragment,
scrollable_overflow,
holds_links_of_outer_containing_blocks: self.holds_links_of_outer_containing_blocks,
- children: self.children.clone(),
+ children,
}
}
@@ -274,6 +283,7 @@ impl FragmentLink {
table_column_span: 0,
hidden_by_collapsed_columns: false,
is_invisible_for_line_clamp: false,
+ clip_decided_by_parent: None,
containing_line_box_index: None,
abspos_layout_inputs: None,
default_scroll_shift: None,
@@ -297,6 +307,7 @@ impl FragmentLink {
&& self.table_column_span == previous.table_column_span
&& self.hidden_by_collapsed_columns == previous.hidden_by_collapsed_columns
&& self.is_invisible_for_line_clamp == previous.is_invisible_for_line_clamp
+ && self.clip_decided_by_parent == previous.clip_decided_by_parent
}
pub(crate) fn has_same_placement(&self, previous: &FragmentLink) -> bool {
@@ -698,7 +709,7 @@ impl FragmentGeometry {
fn snapshot_fragment(
pass: &LayoutPass<'_>,
node: crate::layout::node_data::NodeSlotId,
- children: Vec<FragmentLink>,
+ mut children: Vec<FragmentLink>,
geometry: FragmentGeometry,
fragment_facts: run_state::FragmentFactsDecidedByParent,
payloads: Option<Box<FragmentPayloads>>,
@@ -717,6 +728,16 @@ fn snapshot_fragment(
has_line_clamp_point,
clamped_content_is_scrollable_overflow,
} = payloads.map(|payloads| *payloads).unwrap_or_default();
+ if has_line_clamp_point && !fragment_facts.continues_in_next_fragment {
+ line_clamp_edge::clip_floats_at_block_end_content_edge(
+ pass,
+ node,
+ formatting_context::frame_established_by(pass, node),
+ pass.frame
+ .size_to_physical(cells.content_inline_size, cells.content_block_size),
+ &mut children,
+ );
+ }
let mut fragment = Fragment {
identity: 0,
node,
@@ -768,6 +789,21 @@ fn snapshot_fragment(
std::sync::Arc::new(fragment)
}
+/// A fragment like `fragment` that holds `children` in place of its own: the committed fragment of the box where that
+/// holds them the same way, or else a new one. It never takes the identity of `fragment`, which caches may hold.
+pub(crate) fn fragment_with_children(
+ pass: &LayoutPass<'_>,
+ fragment: &Fragment,
+ children: Vec<FragmentLink>,
+) -> std::sync::Arc<Fragment> {
+ let mut rebuilt = fragment.with_scrollable_overflow_and_children(fragment.scrollable_overflow, children);
+ if let Some(previous) = previously_committed_fragment_matching(pass, &rebuilt) {
+ return previous;
+ }
+ rebuilt.identity = next_unique_identity(pass.worker_scratch());
+ std::sync::Arc::new(rebuilt)
+}
+
/// The fragment of a placed box: `arrived`, the fragment its own run built or the committed one, where that shows the
/// box as placed and nothing else was placed in it, or else a fragment of the box's `geometry` around the arrived
/// fragment's children and the `children` placed in it here.
@@ -831,6 +867,7 @@ pub(crate) struct PlacementData {
pub(crate) table_column_span: u32,
pub(crate) hidden_by_collapsed_columns: bool,
pub(crate) is_invisible_for_line_clamp: bool,
+ pub(crate) clip_decided_by_parent: Option<CssPixelRect>,
pub(crate) containing_line_box_index: Option<u32>,
pub(crate) abspos_layout_inputs: Option<std::sync::Arc<abspos_inputs::AbsposLayoutInputs>>,
pub(crate) default_scroll_shift: Option<abspos_inputs::DefaultScrollShift>,
@@ -862,6 +899,7 @@ impl PlacementData {
table_column_span: facts.fragment_facts.table_column_span,
hidden_by_collapsed_columns: facts.fragment_facts.hidden_by_collapsed_columns,
is_invisible_for_line_clamp: facts.fragment_facts.is_invisible_for_line_clamp,
+ clip_decided_by_parent: facts.fragment_facts.clip_decided_by_parent,
containing_line_box_index,
abspos_layout_inputs,
default_scroll_shift: facts.default_scroll_shift,
@@ -905,6 +943,7 @@ fn link_fragment(fragment: std::sync::Arc<Fragment>, placement: PlacementData) -
table_column_span: placement.table_column_span,
hidden_by_collapsed_columns: placement.hidden_by_collapsed_columns,
is_invisible_for_line_clamp: placement.is_invisible_for_line_clamp,
+ clip_decided_by_parent: placement.clip_decided_by_parent,
containing_line_box_index: placement.containing_line_box_index,
abspos_layout_inputs: placement.abspos_layout_inputs,
default_scroll_shift: placement.default_scroll_shift,
diff --git a/Libraries/LibWeb/Rust/src/layout/geometry.rs b/Libraries/LibWeb/Rust/src/layout/geometry.rs
index 7ba12a22bdc..b53eaa241eb 100644
--- a/Libraries/LibWeb/Rust/src/layout/geometry.rs
+++ b/Libraries/LibWeb/Rust/src/layout/geometry.rs
@@ -173,6 +173,21 @@ impl Frame {
other.rect_from_physical(self.rect_to_physical(rect, container_size), container_size)
}
+ /// The physical half-plane before the block-end edge of a content box whose contents use this frame and whose
+ /// physical size is `content_size`, relative to that content box.
+ pub(crate) fn half_plane_before_block_end(self, content_size: FfiCssPixelSize) -> CssPixelRect {
+ let extent = CssPixels::from_integer((crate::css::css_pixels::MAX_INTEGER_VALUE / 4) as i64);
+ let block_size = if self.is_transposed() {
+ content_size.width
+ } else {
+ content_size.height
+ };
+ self.rect_to_physical(
+ CssPixelRect::new(-extent, -extent, extent * 2, extent + block_size),
+ content_size,
+ )
+ }
+
/// Maps a size in this frame to the physical plane.
#[inline]
pub(crate) fn size_to_physical(self, width: CssPixels, height: CssPixels) -> FfiCssPixelSize {
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index e9a056a85f0..9d5c7777a8a 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -1112,7 +1112,7 @@ impl<'context> InlineFormattingContext<'context> {
placed_child
.facts_decided_by_parent
.fragment_facts
- .is_invisible_for_line_clamp = true;
+ .hide_after_clamp_point();
placed_child
});
}
@@ -2259,10 +2259,10 @@ impl<'context> InlineFormattingContext<'context> {
);
continue;
}
- let fragment_facts = run_state::FragmentFactsDecidedByParent {
- is_invisible_for_line_clamp: state_at_clamp_point.is_some(),
- ..run_state::FragmentFactsDecidedByParent::default()
- };
+ let mut fragment_facts = run_state::FragmentFactsDecidedByParent::default();
+ if state_at_clamp_point.is_some() {
+ fragment_facts.hide_after_clamp_point();
+ }
let laid_out_ahead = self.floats_laid_out_ahead_of_lines.borrow_mut().remove(&item.node);
let laid_out_float =
laid_out_ahead.unwrap_or_else(|| self.lay_out_float_ahead_of_its_position(item.node));
diff --git a/Libraries/LibWeb/Rust/src/layout/line_clamp_edge.rs b/Libraries/LibWeb/Rust/src/layout/line_clamp_edge.rs
new file mode 100644
index 00000000000..ebc001490ad
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/layout/line_clamp_edge.rs
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use crate::css::css_pixels::CssPixelRect;
+use crate::fast_hash::FastMap;
+use crate::layout::formatting_context;
+use crate::layout::fragment_overflow;
+use crate::layout::fragment_tree::{self, FragmentLink};
+use crate::layout::geometry::Frame;
+use crate::layout::layout_pass::LayoutPass;
+use crate::layout::node_data::NodeSlotId;
+use crate::layout::node_facts::{self, NodeFacts};
+use crate::layout::used_values::FfiCssPixelSize;
+use smallvec::SmallVec;
+
+/// Clips each float in the formatting context of a line-clamp container's `children` at the container's block-end
+/// content edge: the clip goes on the float's link, relative to the float's border box. The container's contents use
+/// `frame`, and its content box has the physical size `content_size`.
+pub(crate) fn clip_floats_at_block_end_content_edge(
+ pass: &LayoutPass<'_>,
+ container: NodeSlotId,
+ frame: Frame,
+ content_size: FfiCssPixelSize,
+ children: &mut [FragmentLink],
+) {
+ let mut walk = ClampEdgeWalk {
+ pass,
+ area_before_block_end_edge: frame.half_plane_before_block_end(content_size),
+ content_boxes: FastMap::default(),
+ };
+ walk.content_boxes.insert(
+ container,
+ CssPixelRect::new(
+ Default::default(),
+ Default::default(),
+ content_size.width,
+ content_size.height,
+ ),
+ );
+ for (index, link) in walk.changed_links_among(children) {
+ children[index] = link;
+ }
+}
+
+struct ClampEdgeWalk<'walk, 'pass> {
+ pass: &'walk LayoutPass<'pass>,
+ /// Relative to the container's content box, as every rect of the walk is.
+ area_before_block_end_edge: CssPixelRect,
+ /// The physical content box of each box in the container's formatting context that the walk entered.
+ content_boxes: FastMap<NodeSlotId, CssPixelRect>,
+}
+
+enum LinkOutcome {
+ Unchanged,
+ Changed(FragmentLink),
+ ContainingBlockNotEntered,
+}
+
+impl ClampEdgeWalk<'_, '_> {
+ fn changed_links_among(&mut self, links: &[FragmentLink]) -> SmallVec<[(usize, FragmentLink); 2]> {
+ let mut changed_links = SmallVec::new();
+ // NB: A link can be held beside its containing block, which another of the links places, so it waits for that.
+ let mut waiting_links: SmallVec<[usize; 4]> = (0..links.len()).collect();
+ loop {
+ let waiting_before = waiting_links.len();
+ waiting_links.retain(|index| match self.link_with_clipped_floats(&links[*index]) {
+ LinkOutcome::Unchanged => false,
+ LinkOutcome::Changed(link) => {
+ changed_links.push((*index, link));
+ false
+ }
+ LinkOutcome::ContainingBlockNotEntered => true,
+ });
+ if waiting_links.is_empty() || waiting_links.len() == waiting_before {
+ return changed_links;
+ }
+ }
+ }
+
+ fn link_with_clipped_floats(&mut self, link: &FragmentLink) -> LinkOutcome {
+ let Some(&containing_block) = self.content_boxes.get(&link.containing_block) else {
+ return LinkOutcome::ContainingBlockNotEntered;
+ };
+ if link.is_invisible_for_line_clamp {
+ return LinkOutcome::Unchanged;
+ }
+ let fragment = &link.fragment;
+ let data = self.pass.arena().data(fragment.node);
+ let style = node_facts::node_style_view(data);
+ let content_box = link
+ .frame
+ .rect_to_physical(
+ CssPixelRect::new(
+ link.committed_offset.x,
+ link.committed_offset.y,
+ fragment.content_inline_size,
+ fragment.content_block_size,
+ ),
+ FfiCssPixelSize {
+ width: containing_block.width,
+ height: containing_block.height,
+ },
+ )
+ .translated_by(containing_block.location());
+
+ if node_facts::node_is_floating(data, style) {
+ let border_box = fragment_overflow::physical_border_box_around(content_box, fragment, link.frame);
+ let clip = Some(self.area_before_block_end_edge.translated(-border_box.x, -border_box.y));
+ if link.clip_decided_by_parent == clip {
+ return LinkOutcome::Unchanged;
+ }
+ return LinkOutcome::Changed(FragmentLink {
+ clip_decided_by_parent: clip,
+ ..link.clone()
+ });
+ }
+
+ // NB: Line clamping does not reach into a box that establishes a formatting context of its own, and a nested
+ // line-clamp container clips the floats in its formatting context at its own edge.
+ if fragment.has_line_clamp_point
+ || formatting_context::formatting_context_type_created_by_box(NodeFacts::new(self.pass, fragment.node))
+ .is_some()
+ {
+ return LinkOutcome::Unchanged;
+ }
+ // NB: An inline box broken into line pieces has no content box of its own to place the links it holds in. A
+ // block can have no children of its own while the container holds floats beside it.
+ if !node_facts::node_is_fragmented_inline(data, style) {
+ self.content_boxes.insert(fragment.node, content_box);
+ }
+ if fragment.children.is_empty() {
+ return LinkOutcome::Unchanged;
+ }
+ let changed_children = self.changed_links_among(&fragment.children);
+ if changed_children.is_empty() {
+ return LinkOutcome::Unchanged;
+ }
+ let mut children = fragment.children.clone();
+ for (index, child) in changed_children {
+ children[index] = child;
+ }
+ LinkOutcome::Changed(FragmentLink {
+ fragment: fragment_tree::fragment_with_children(self.pass, fragment, children),
+ ..link.clone()
+ })
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index 18c87d505a5..eef55a193d2 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -37,6 +37,7 @@ mod layout_pass;
pub(crate) mod line_box;
pub(crate) mod line_box_fragment;
pub(crate) mod line_builder;
+pub(crate) mod line_clamp_edge;
pub(crate) mod math_formatting_context;
mod memo_invalidations;
pub mod node_data;
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index 96005b7898a..505c29075ca 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -226,7 +226,8 @@ pub(crate) struct BoxPlacement {
}
/// What the placement of a box shows of the layout of its parent, which decides it: the link that places the box's
-/// fragment shows the table and line clamp facts, and the fragment shows whether fragmentation broke the box.
+/// fragment shows the table and line clamp facts and the clip, and the fragment shows whether fragmentation broke the
+/// box.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct FragmentFactsDecidedByParent {
/// For table cells and table-column(-group) boxes: the first grid column the box occupies and the number of grid
@@ -238,12 +239,23 @@ pub(crate) struct FragmentFactsDecidedByParent {
pub(crate) hidden_by_collapsed_columns: bool,
/// Whether the box lies after the clamp point of a line-clamp container, which hides it.
pub(crate) is_invisible_for_line_clamp: bool,
+ /// The physical clip an ancestor's layout puts on the box and its contents, relative to the box's border box: empty
+ /// for a box hidden after a clamp point, and the area before the block-end content edge of a line-clamp container
+ /// for a float inside it.
+ pub(crate) clip_decided_by_parent: Option<CssPixelRect>,
/// Whether fragmentation broke the box, so that this fragment continues one in an earlier fragmentainer, or
/// continues in a later one.
pub(crate) continues_from_previous_fragment: bool,
pub(crate) continues_in_next_fragment: bool,
}
+impl FragmentFactsDecidedByParent {
+ pub(crate) fn hide_after_clamp_point(&mut self) {
+ self.is_invisible_for_line_clamp = true;
+ self.clip_decided_by_parent = Some(CssPixelRect::default());
+ }
+}
+
/// What the parent of a box decides about the box, kept with the box by the run that holds it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct FactsDecidedByParent {
diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rs
index 6e7b95fb6fc..ae7a614f8b0 100644
--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs
@@ -691,6 +691,8 @@ pub(crate) fn committed_box_from_fragment_link(pass: &LayoutPass<'_>, node: Node
table_column_index: link.table_column_index,
table_column_span: link.table_column_span,
hidden_by_collapsed_columns: link.hidden_by_collapsed_columns,
+ is_invisible_for_line_clamp: link.is_invisible_for_line_clamp,
+ clip_decided_by_parent: link.clip_decided_by_parent,
..super::run_state::FragmentFactsDecidedByParent::default()
},
},
diff --git a/Libraries/LibWeb/Rust/src/painting/style_queries.rs b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
index 0f2c697e999..b2aa8d403df 100644
--- a/Libraries/LibWeb/Rust/src/painting/style_queries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
@@ -10,7 +10,7 @@ use crate::css::css_enums::{
backface_visibility, content_visibility, isolation, line_style, mix_blend_mode, outline_style, overflow,
positioning, transform_style,
};
-use crate::css::css_pixels::{CssPixelRect, CssPixels};
+use crate::css::css_pixels::CssPixels;
use crate::css::css_string::CssString;
use crate::css::property_metadata::property_name;
use crate::css::serialize::{StringUnits, with_fly_string_units};
@@ -626,42 +626,6 @@ pub(crate) fn is_floating(arena: &impl PaintRead, node: NodeSlotId) -> bool {
arena.node(node).is_some_and(PaintRow::is_floating)
}
-pub(crate) fn is_invisible_for_line_clamp(arena: &impl PaintRead, node: NodeSlotId) -> bool {
- arena.with_committed_fragment_link(node, |link| link.is_some_and(|link| link.is_invisible_for_line_clamp))
-}
-
-pub(crate) fn has_line_clamp_point(arena: &impl PaintRead, node: NodeSlotId) -> bool {
- arena.with_committed_fragment_link(node, |link| link.is_some_and(|link| link.fragment.has_line_clamp_point))
-}
-
-fn line_clamp_container(arena: &impl PaintRead, start: Option<NodeSlotId>) -> Option<NodeSlotId> {
- std::iter::successors(start, |node| arena.node_parent_if_live(*node))
- .find(|node| has_line_clamp_point(arena, *node))
-}
-
-pub(crate) fn line_clamp_clip_rect(arena: &impl PaintRead, container: NodeSlotId) -> CssPixelRect {
- let style = arena
- .node_style_if_live(container)
- .expect("line clamp container must be live");
- let content_rect = crate::painting::paintable_geometry::absolute_rect(arena, container);
- let extent = CssPixels::from_integer((crate::css::css_pixels::MAX_INTEGER_VALUE / 4) as i64);
- if style.writing_mode() == crate::css::css_enums::writing_mode::HORIZONTAL_TB {
- CssPixelRect::new(-extent, -extent, extent * 2, content_rect.bottom() + extent)
- } else if style.block_axis_is_reverse() {
- CssPixelRect::new(content_rect.x, -extent, extent - content_rect.x, extent * 2)
- } else {
- CssPixelRect::new(-extent, -extent, content_rect.right() + extent, extent * 2)
- }
-}
-
-pub(crate) fn line_clamp_float_clip_rect(arena: &impl PaintRead, node: NodeSlotId) -> Option<CssPixelRect> {
- if is_invisible_for_line_clamp(arena, node) {
- return Some(CssPixelRect::default());
- }
- let container = line_clamp_container(arena, Some(node))?;
- Some(line_clamp_clip_rect(arena, container))
-}
-
pub(crate) fn is_inline(arena: &impl PaintRead, node: NodeSlotId) -> bool {
node_facts::node_is_inline_outside(arena.node_style_if_live(node))
}
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
index 92ab37839de..a937fa4e769 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
@@ -311,6 +311,12 @@ pub(crate) fn build_box_visual_context_nodes<Arena: PaintableRowsRead>(
}};
}
+ // NB: Layout decides the clip in the parent's space, so the box's own sticky offset, effects and transform go
+ // under it.
+ if let Some(clip_decided_by_parent) = facts.clip_decided_by_parent {
+ append_clip_to_own_and_positioned_descendant_contexts!(ClipNodeData::Rect(clip_decided_by_parent));
+ }
+
macro_rules! append_shared_effect {
($make:expr) => {{
let effect = sink.append_effect_node($make, own_state.effect, own_state.spatial, own_state.clip);
@@ -457,10 +463,6 @@ pub(crate) fn build_box_visual_context_nodes<Arena: PaintableRowsRead>(
append_clip_to_own_and_positioned_descendant_contexts!(ClipNodeData::Rect(css_clip));
}
- if let Some(line_clamp_float_clip) = facts.line_clamp_float_clip {
- append_clip_to_own_and_positioned_descendant_contexts!(ClipNodeData::Rect(line_clamp_float_clip));
- }
-
if facts.clip_path.is_some() {
append_clip_to_own_and_positioned_descendant_contexts!(ClipNodeData::Path(facts.clip_path_data().unwrap()));
}
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
index 7dbf6f52145..77970bed182 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
@@ -12,7 +12,6 @@ use crate::painting::host::FfiVisualContextTreeInputs;
use crate::painting::paint_read::PaintRead;
use crate::painting::paintable_geometry;
use crate::painting::paintable_rows::PaintableRowsRead;
-use crate::painting::style_queries;
use libgfx_rust::CornerRadii;
use libgfx_rust::{
AffineTransform, CompositingAndBlendingOperator, FloatPoint, IntRect, WindingRule, affine_to_matrix,
@@ -65,7 +64,8 @@ pub(crate) struct BoxFacts {
/// The overflow clip's shape, when shaped corners make it a path rather than a rounded rectangle.
pub overflow_clip_contour: Option<ClipPathData>,
pub css_clip: Option<ClipData>,
- pub line_clamp_float_clip: Option<ClipData>,
+ /// The clip that layout put on the box's link.
+ pub clip_decided_by_parent: Option<ClipData>,
pub clip_path: Option<(std::sync::Arc<libgfx_rust::path::Path>, IntRect, WindingRule)>,
pub mask_layers: Vec<MaskData>,
pub establishes_absolute_containing_block: bool,
@@ -155,7 +155,7 @@ impl BoxFacts {
effects: None,
overflow_clip: None,
css_clip: None,
- line_clamp_float_clip: None,
+ clip_decided_by_parent: None,
clip_path: None,
mask_layers: Vec::new(),
overflow_clip_contour: None,
@@ -196,18 +196,20 @@ impl BoxFacts {
})
.collect();
facts.css_clip = super::node_values::compute_css_clip_data(layout_arena, slot, pixel_ratio);
- if style_queries::is_floating(layout_arena, slot)
- || style_queries::is_invisible_for_line_clamp(layout_arena, slot)
- {
- facts.line_clamp_float_clip = style_queries::line_clamp_float_clip_rect(layout_arena, slot).map(|rect| {
+ facts.clip_decided_by_parent = layout_arena
+ .with_committed_fragment_link(slot, |link| link.and_then(|link| link.clip_decided_by_parent))
+ .map(|clip_relative_to_border_box| {
+ let border_box =
+ crate::painting::paintable_geometry::first_fragment_border_box_rect(layout_arena, slot);
let converter = crate::painting::display_list::device_pixels::DevicePixelConverter::new(pixel_ratio);
ClipData {
- rect: converter.rounded_device_rect(rect).to_float(),
+ rect: converter
+ .rounded_device_rect(clip_relative_to_border_box.translated_by(border_box.location()))
+ .to_float(),
corner_radii: CornerRadii::default(),
mode: ClipMode::Intersect,
}
});
- }
facts.may_have_clip = super::node_values::may_have_clip(layout_arena, slot);
facts.overflow_clip = if facts.may_have_clip {
super::node_values::compute_clip_data(layout_arena, slot, pixel_ratio)
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/line-clamp-with-floats-009-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/line-clamp-with-floats-009-ref.html
new file mode 100644
index 00000000000..8c369b26a95
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/line-clamp-with-floats-009-ref.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Reference</title>
+<style>
+.clamp {
+ font: 16px / 32px serif;
+ padding: 4px;
+ background-color: yellow;
+}
+.parent {
+ /* We need the small slice of the float that extends out of .clamp
+ * but above the content edge to not be clamped.
+ * The 12px covers the 8px of the body margin plus .clamp's 4px padding.
+ */
+ overflow: clip;
+ transform: translateX(-12px);
+ padding-left: 12px;
+}
+.float {
+ float: left;
+ margin: 5px;
+ width: 50px;
+ height: 200px;
+ background-color: skyblue;
+ transform: rotate(30deg);
+}
+</style>
+<div class="clamp">
+ <div class="parent">
+ <div class="float"></div>
+ Line 1 <br>
+ Line 2 <br>
+ Line 3 <br>
+ Line 4…
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-009.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-009.html
new file mode 100644
index 00000000000..461fe8107c9
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-009.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Overflow: floats inside line-clamp clip to the container's edge, regardless of transforms</title>
+<link rel="author" title="Andreu Botella" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-overflow-4/#line-clamp">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-overflow/line-clamp/reference/line-clamp-with-floats-009-ref.html">
+<meta name="assert" content="Floats inside a line-clamp container clip to the block-end content edge of the container, even if the float is transformed.">
+<style>
+.clamp {
+ line-clamp: 4;
+ font: 16px / 32px serif;
+ padding: 4px;
+ background-color: yellow;
+}
+.float {
+ float: left;
+ margin: 5px;
+ width: 50px;
+ height: 200px;
+ background-color: skyblue;
+ transform: rotate(30deg);
+}
+</style>
+<div class="clamp">
+ <div class="float"></div>
+ Line 1 <br>
+ Line 2 <br>
+ Line 3 <br>
+ Line 4 <br>
+ Line 5
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-014.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-014.html
new file mode 100644
index 00000000000..dbec55c286a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/line-clamp-with-floats-014.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<meta charset="utf-8">
+<title>CSS Overflow: line-clamp float clipping repaint test</title>
+<link rel="author" title="Andreu Botella" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-overflow-4/#line-clamp">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-overflow/line-clamp/reference/line-clamp-with-floats-005-ref.html">
+<meta name="assert" content="With a float inside a line-clamp container, when the container's content edge moves, the float clip will be updated to match it.">
+<script src="../../../common/reftest-wait.js"></script>
+
+<style>
+#clamp {
+ line-clamp: 4;
+ font: 16px / 32px serif;
+ padding: 4px;
+ white-space: pre;
+ background-color: yellow;
+ height: 200px; /* more than enough to fit the float */
+}
+.float {
+ float: left;
+ width: 50px;
+ height: 50px;
+ margin: 4px;
+ background-color: skyblue;
+}
+</style>
+
+<div id="clamp">Line 1
+Line 2
+Line 3
+Line 4<div class="float"></div>
+Line 5</div>
+
+<script>
+ window.onload = () => {
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.getElementById("clamp").style.height = "auto";
+ takeScreenshot();
+ });
+ });
+ };
+</script>
diff --git a/Tests/LibWeb/Text/expected/css/line-clamp-float-clip.txt b/Tests/LibWeb/Text/expected/css/line-clamp-float-clip.txt
new file mode 100644
index 00000000000..0b286c1d183
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/line-clamp-float-clip.txt
@@ -0,0 +1,12 @@
+float above the clamp edge: float
+float below the clamp edge: HTML
+float above the moved clamp edge: float
+float below the moved clamp edge: HTML
+padding of a float that clamps its own lines: clamping-float
+float in a nested block above the clamp edge: block-float
+float in a nested block below the clamp edge: HTML
+float in a nested formatting context below the clamp edge: flow-root-float
+float in a nested block above the moved clamp edge: block-float
+float in a nested block below the moved clamp edge: HTML
+vertical float before the clamp edge: vertical-float
+vertical float past the clamp edge: HTML
diff --git a/Tests/LibWeb/Text/input/css/line-clamp-float-clip.html b/Tests/LibWeb/Text/input/css/line-clamp-float-clip.html
new file mode 100644
index 00000000000..99d1597fc24
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/line-clamp-float-clip.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .line { display: inline-block; width: 150px; height: 20px; }
+ #container { width: 200px; line-clamp: 2 no-ellipsis; font-size: 0; }
+ #float { float: left; width: 50px; height: 100px; }
+ #host { position: absolute; left: 300px; top: 0; width: 200px; }
+ #clamping-float { float: left; width: 150px; padding-bottom: 20px; line-clamp: 1 no-ellipsis; font-size: 0; }
+ #nested-container { position: absolute; left: 0; top: 200px; width: 200px; line-clamp: 2 no-ellipsis; font-size: 0; }
+ .nested { height: 10px; }
+ #flow-root { display: flow-root; }
+ #block-float { float: right; width: 40px; height: 100px; }
+ #flow-root-float { float: left; width: 40px; height: 100px; }
+ #vertical { position: absolute; left: 300px; top: 100px; height: 200px; writing-mode: vertical-rl; line-clamp: 2 no-ellipsis; font-size: 0; }
+ #vertical-float { float: left; width: 100px; height: 30px; }
+ .vertical-line { display: inline-block; width: 20px; height: 150px; }
+</style>
+<div id="container"><div id="float"></div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"></div></div>
+<div id="host"><div id="clamping-float"><div class="line"></div><div class="line"></div></div></div>
+<div id="nested-container"><div class="nested"><div id="block-float"></div></div><div id="flow-root" class="nested"><div id="flow-root-float"></div></div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"></div></div>
+<div id="vertical"><div id="vertical-float"></div><div class="vertical-line"></div><div class="vertical-line"></div><div class="vertical-line"></div><div class="vertical-line"></div></div>
+<script>
+ test(() => {
+ __outputElement.style.display = "none";
+ const lines = [];
+ const println = line => lines.push(line);
+ const hit = (x, y) => {
+ const element = document.elementFromPoint(x, y);
+ return element?.id || element?.tagName;
+ };
+ println(`float above the clamp edge: ${hit(25, 30)}`);
+ println(`float below the clamp edge: ${hit(25, 50)}`);
+
+ // The edge moves with the container, while the float keeps its place and size.
+ document.getElementById("container").style.setProperty("line-clamp", "3 no-ellipsis");
+ println(`float above the moved clamp edge: ${hit(25, 50)}`);
+ println(`float below the moved clamp edge: ${hit(25, 70)}`);
+
+ println(`padding of a float that clamps its own lines: ${hit(325, 30)}`);
+
+ // A float inside a nested block is in the container's formatting context, and a float inside a nested
+ // formatting context is not.
+ println(`float in a nested block above the clamp edge: ${hit(180, 250)}`);
+ println(`float in a nested block below the clamp edge: ${hit(180, 270)}`);
+ println(`float in a nested formatting context below the clamp edge: ${hit(20, 280)}`);
+ document.getElementById("nested-container").style.setProperty("line-clamp", "3 no-ellipsis");
+ println(`float in a nested block above the moved clamp edge: ${hit(180, 270)}`);
+ println(`float in a nested block below the moved clamp edge: ${hit(180, 290)}`);
+
+ // In vertical-rl, the block-end edge is the container's left edge.
+ println(`vertical float before the clamp edge: ${hit(330, 115)}`);
+ println(`vertical float past the clamp edge: ${hit(270, 115)}`);
+
+ __outputElement.style.display = "";
+ for (const line of lines)
+ window.println(line);
+ });
+</script>