Skip to content

Truly independent web browser

LibWeb: Retain inline geometry beyond a line clamp

sideshowbarker09187f62dfd4
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index 44c35c590c0..fa04a3b8fed 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -23,6 +23,8 @@ pub struct InlineContent {
/// there are no boxes to interleave, so text can be painted together.
pub items: Vec<InlineItem>,
pub lines_after_clamp_point_rect: Option<Box<used_values::FfiCssPixelRect>>,
+ /// Inline border boxes on suppressed lines, retained for geometry queries without adding paintable content.
+ pub(crate) inline_box_rects_after_clamp: Option<Box<[ClampedInlineBoxRect]>>,
/// Where the content that text-overflow elided from the end of a line would be, by line index.
pub elided_content_rects: Vec<(u32, used_values::FfiCssPixelRect)>,
/// The writing mode whose frame the lines were laid out in.
@@ -172,6 +174,12 @@ pub struct LineRecord {
}
impl LineRecord {
+ pub(crate) fn mirrored_block_offset(&self, block_offset: CssPixels, block_length: CssPixels) -> CssPixels {
+ let line_block_start = self.block_end - self.block_length;
+ let line_content_block_end = self.block_end - self.clearance_block_length;
+ line_block_start + line_content_block_end - block_offset - block_length
+ }
+
pub(crate) fn physical_horizontal_extent(&self) -> CssPixels {
super::geometry::to_physical(self.writing_mode, self.inline_length, self.block_length).0
}
@@ -423,6 +431,13 @@ pub struct InlineBoxPieceRecord {
pub is_geometry_only_placeholder: bool,
}
+#[derive(Clone, Copy, PartialEq)]
+pub(crate) struct ClampedInlineBoxRect {
+ pub node: NodeSlotId,
+ pub rect: used_values::FfiCssPixelRect,
+ pub box_fragment: u32,
+}
+
impl InlineContent {
pub(crate) fn finish(
data: used_values::LineData,
@@ -497,6 +512,7 @@ impl InlineContent {
inline_box_pieces: pieces,
items: Vec::new(),
lines_after_clamp_point_rect: data.lines_after_clamp_point_rect,
+ inline_box_rects_after_clamp: data.inline_box_rects_after_clamp,
elided_content_rects,
writing_mode,
};
@@ -645,6 +661,17 @@ impl InlineContent {
}
self.lines_after_clamp_point_rect = Some(Box::new(united.into()));
}
+ if let Some(rects) = content.inline_box_rects_after_clamp.as_deref() {
+ let mut combined = self.inline_box_rects_after_clamp.take().unwrap_or_default().into_vec();
+ combined.extend(rects.iter().map(|rect| {
+ let mut rect = *rect;
+ rect.box_fragment = box_fragment;
+ rect.rect.x += offset.x;
+ rect.rect.y += offset.y;
+ rect
+ }));
+ self.inline_box_rects_after_clamp = Some(combined.into_boxed_slice());
+ }
self.fragments
.extend(content.fragments.iter().cloned().map(|mut fragment| {
fragment.line_index += line_offset;
@@ -709,6 +736,11 @@ impl InlineContent {
if let Some(rect) = content.lines_after_clamp_point_rect.as_deref_mut() {
*rect = to_physical(*rect);
}
+ if let Some(rects) = content.inline_box_rects_after_clamp.as_deref_mut() {
+ for rect in rects {
+ rect.rect = to_physical(rect.rect);
+ }
+ }
for fragment in &mut content.fragments {
let block_offset = mirror_in_line(fragment.line_index, fragment.block_offset, fragment.block_length);
let rect = plane.unmoved_fragment_rect(fragment, block_offset);
@@ -883,9 +915,7 @@ impl<'content> PhysicalPlane<'content> {
};
// The line's content is mirrored within its own extent: a clearing break after the line extends the line
// toward the clearance, and the content stays where it is.
- let line_block_start = line.block_end - line.block_length;
- let line_content_block_end = line.block_end - line.clearance_block_length;
- line_block_start + line_content_block_end - block_offset - block_length
+ line.mirrored_block_offset(block_offset, block_length)
}
fn unmoved_fragment_rect(
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 1105eb946b0..1efa99f5daf 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2515,6 +2515,7 @@ impl<'context> InlineFormattingContext<'context> {
data.line_boxes = reused_lines;
data.inline_box_pieces.clear();
data.lines_after_clamp_point_rect = None;
+ data.inline_box_rects_after_clamp = None;
}
let item_count = iterator.items().len();
let start_item_index = resume.map_or(reused_item_count, |resume| resume.item_index.min(item_count));
@@ -2638,9 +2639,11 @@ impl<'context> InlineFormattingContext<'context> {
}
}
{
+ let rects = self.collect_clamped_inline_box_rects(state_at_clamp_point.current_line_index);
let mut data = self.line_data_mut();
data.lines_after_clamp_point_rect =
(!lines_after_clamp_point_rect.is_empty()).then(|| Box::new(lines_after_clamp_point_rect.into()));
+ data.inline_box_rects_after_clamp = rects;
data.line_boxes.truncate(state_at_clamp_point.current_line_index);
data.line_boxes.push(state_at_clamp_point.current_line);
}
@@ -2967,6 +2970,37 @@ impl<'context> InlineFormattingContext<'context> {
}
}
+ fn collect_clamped_inline_box_rects(
+ &self,
+ first_line: usize,
+ ) -> Option<Box<[inline_content::ClampedInlineBoxRect]>> {
+ if self.fragmented_inlines_in_pre_order.is_empty() {
+ return None;
+ }
+ let (pieces, _) = compute(self);
+ let data = self.line_data();
+ let rects: Vec<_> = pieces
+ .into_iter()
+ .filter(|piece| piece.line_index as usize >= first_line && !piece.is_geometry_only_placeholder)
+ .map(|piece| {
+ let mut rect = piece.border_box_rect;
+ let relative_insets = self.relative_insets_of_inline_ancestors(piece.node);
+ rect.x += relative_insets.offset_x;
+ rect.y += relative_insets.offset_y;
+ let line = &data.line_boxes[piece.line_index as usize];
+ if self.style(self.containing_block).writing_mode() == writing_mode::VERTICAL_LR {
+ rect.y = line.mirrored_block_offset(rect.y, rect.height);
+ }
+ inline_content::ClampedInlineBoxRect {
+ node: piece.node,
+ rect,
+ box_fragment: 0,
+ }
+ })
+ .collect();
+ (!rects.is_empty()).then(|| rects.into_boxed_slice())
+ }
+
fn compute_inline_box_pieces(&mut self) {
let (pieces, inline_containing_block_rect_candidates) = compute(self);
self.line_data_mut().inline_box_pieces = pieces;
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index 7282e81cb38..a83ee5b5b34 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -522,7 +522,7 @@ pub extern "C" fn layout_script_paintable_border_box_rect_of_fragments(
if !arena.paintable_row_is_populated(slot) {
return FfiCssPixelRect::default();
}
- crate::painting::paintable_geometry::absolute_border_box_rect(arena, slot).into()
+ crate::painting::client_rects::border_box_rect_of_fragments(arena, slot).into()
});
}
diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rs
index 8ef7d14edef..3515d1538c8 100644
--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs
@@ -67,6 +67,7 @@ pub(crate) struct LineData {
pub(crate) line_boxes: Vec<line_box::LineBoxData>,
pub(crate) inline_box_pieces: Vec<inline_formatting_context::InlineBoxPieceData>,
pub(crate) lines_after_clamp_point_rect: Option<Box<FfiCssPixelRect>>,
+ pub(crate) inline_box_rects_after_clamp: Option<Box<[inline_content::ClampedInlineBoxRect]>>,
}
#[derive(Default)]
diff --git a/Libraries/LibWeb/Rust/src/painting/client_rects.rs b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
index 405ccae4e6e..6a8e9fd2132 100644
--- a/Libraries/LibWeb/Rust/src/painting/client_rects.rs
+++ b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
@@ -105,6 +105,35 @@ fn for_each_inline_fragment_border_box_rect(
if let Some(rect) = empty_piece_after_last_block {
push_rect(rect);
}
+ if let Some(rects) = root_side
+ .inline_content
+ .as_ref()
+ .and_then(|content| content.inline_box_rects_after_clamp.as_deref())
+ {
+ for rect in rects.iter().filter(|rect| rect.node == inline_paintable) {
+ push_rect(CssPixelRect::from(rect.rect).translated_by(root_positions.of_fragment(rect.box_fragment)));
+ }
+ }
+}
+
+pub(crate) fn border_box_rect_of_fragments(arena: &impl PaintRead, node: NodeSlotId) -> CssPixelRect {
+ let has_clamped_lines = arena.inline_pieces_root(node).is_some_and(|root| {
+ arena
+ .committed_side_data(root)
+ .inline_content
+ .as_ref()
+ .and_then(|content| content.inline_box_rects_after_clamp.as_deref())
+ .is_some_and(|rects| rects.iter().any(|rect| rect.node == node))
+ });
+ if !has_clamped_lines {
+ return paintable_geometry::absolute_border_box_rect(arena, node);
+ }
+ let mut bounds: Option<CssPixelRect> = None;
+ for_each_inline_fragment_border_box_rect(arena, node, |rect| match &mut bounds {
+ Some(bounds) => bounds.unite(rect),
+ None => bounds = Some(rect),
+ });
+ bounds.unwrap_or_default()
}
struct InterruptingBlock {
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-006-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-006-ref.html
new file mode 100644
index 00000000000..3a17a6d5f3b
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-006-ref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Reference</title>
+<style>
+.clamp {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ font: 16px / 32px serif;
+ white-space: pre;
+ padding: 0 4px;
+ background-color: yellow;
+}
+</style>
+<div class="clamp">Line 1
+Line 2
+Line 3
+Line 4…</div>
+<p>Following content.</p>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-040-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-040-ref.html
new file mode 100644
index 00000000000..0e94ba74612
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-040-ref.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Test Reference</title>
+<style>
+.clamp {
+ font: 16px / 32px serif;
+ white-space: pre;
+ background-color: yellow;
+ height: 5em;
+}
+</style>
+<div class="clamp">Line 1
+Line 2…
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-006.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-006.html
new file mode 100644
index 00000000000..f2bbed5bdcd
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-006.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Overflow: sizing of -webkit-line-clamp affected elements</title>
+<link rel="author" title="Cameron McCormack" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-overflow-3/#webkit-line-clamp">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-006-ref.html">
+<meta name="assert" content="-webkit-line-clamp should size the element to the clamped number of lines.">
+<style>
+.clamp {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ -webkit-line-clamp: 4;
+ font: 16px / 32px serif;
+ white-space: pre;
+ padding: 0 4px;
+ background-color: yellow;
+ overflow: hidden; /* can be removed once implementations update their old -webkit-line-clamp implementations */
+}
+</style>
+<div class="clamp">Line 1
+Line 2
+Line 3
+Line 4
+Line 5</div>
+<p>Following content.</p>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-040.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-040.html
new file mode 100644
index 00000000000..f66e12758ee
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-overflow/line-clamp/webkit-line-clamp-040.html
@@ -0,0 +1,23 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Overflow: line-clamp with explicit height</title>
+<link rel="author" title="Emilio Cobos Álvarez" href="mailto:[email protected]">
+<link rel="author" title="Mozilla" href="https://mozilla.org">
+<link rel="help" href="https://drafts.csswg.org/css-overflow-3/#webkit-line-clamp">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-overflow/line-clamp/reference/webkit-line-clamp-040-ref.html">
+<style>
+.clamp {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ -webkit-line-clamp: 2;
+ font: 16px / 32px serif;
+ white-space: pre;
+ background-color: yellow;
+ height: 5em;
+}
+</style>
+<div class="clamp">Line 1
+Line 2
+Line 3
+Line 4
+Line 5</div>
diff --git a/Tests/LibWeb/Text/expected/css/line-clamp-inline-geometry.txt b/Tests/LibWeb/Text/expected/css/line-clamp-inline-geometry.txt
new file mode 100644
index 00000000000..d086acfe2fb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/line-clamp-inline-geometry.txt
@@ -0,0 +1,36 @@
+plain: box 60, scroll 120
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+nested: box 60, scroll 120
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+late: box 60, scroll 120
+span: offset 60, bounds 60, top 60, bottom 120
+line tops: 60, 80, 100
+relative: box 60, scroll 125
+span: offset 120, bounds 120, top 5, bottom 125
+line tops: 5, 25, 45, 65, 85, 105
+unclamped: box 120, scroll 120
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+visible: box 60, scroll 120
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+vertical-rl: box 60, span 120, bounds 120
+columns: 40, 20, 0, -20, -40, -60
+vertical-lr: box 60, span 120, bounds 120
+columns: 0, 20, 40, 60, 80, 100
+relative: box 60, scroll 130
+span: offset 120, bounds 120, top 10, bottom 130
+line tops: 10, 30, 50, 70, 90, 110
+plain: box 40, scroll 120
+span: offset 120, bounds 120, top 0, bottom 120
+line tops: 0, 20, 40, 60, 80, 100
+plain: box 40, scroll 60
+span: offset 60, bounds 60, top 0, bottom 60
+line tops: 0, 20, 40
+plain: box 60, scroll 60
+span: offset 60, bounds 60, top 0, bottom 60
+line tops: 0, 20, 40
diff --git a/Tests/LibWeb/Text/input/css/line-clamp-inline-geometry.html b/Tests/LibWeb/Text/input/css/line-clamp-inline-geometry.html
new file mode 100644
index 00000000000..cba499c7cee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/line-clamp-inline-geometry.html
@@ -0,0 +1,60 @@
+<!doctype html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ .clamp {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ -webkit-line-clamp: 3;
+ overflow: hidden;
+ width: 100px;
+ font: 20px/20px Ahem;
+ }
+</style>
+<div class="clamp" id="visible" style="overflow: visible"><span>AAA BBB CCC DDD EEE FFF</span></div>
+<div class="clamp" id="plain"><span>AAA BBB CCC DDD EEE FFF</span></div>
+<div class="clamp" id="nested"><span><span>AAA BBB CCC DDD EEE FFF</span></span></div>
+<div class="clamp" id="late">AAA BBB CCC <span>DDD EEE FFF</span></div>
+<div class="clamp" id="relative"><span style="position: relative; top: 5px; left: 7px">AAA BBB CCC DDD EEE FFF</span></div>
+<div class="clamp" id="unclamped" style="-webkit-line-clamp: 9"><span>AAA BBB CCC DDD EEE FFF</span></div>
+<script>
+ promiseTest(async () => {
+ await document.fonts.ready;
+ function report(id) {
+ const box = document.getElementById(id);
+ const origin = box.getBoundingClientRect();
+ println(`${id}: box ${box.offsetHeight}, scroll ${box.scrollHeight}`);
+ for (const span of box.querySelectorAll("span")) {
+ const rect = span.getBoundingClientRect();
+ const rects = [...span.getClientRects()];
+ const bottom = Math.max(...rects.map(rect => rect.bottom)) - origin.top;
+ println(`span: offset ${span.offsetHeight}, bounds ${rect.height}, top ${rect.top - origin.top}, bottom ${bottom}`);
+ println(`line tops: ${[...new Set(rects.map(rect => rect.top - origin.top))].join(", ")}`);
+ }
+ }
+ for (const id of ["plain", "nested", "late", "relative", "unclamped", "visible"])
+ report(id);
+ for (const mode of ["vertical-rl", "vertical-lr"]) {
+ const box = document.createElement("div");
+ box.className = "clamp";
+ box.style.cssText = `writing-mode: ${mode}; width: auto; height: 100px`;
+ box.innerHTML = "<span>AAA BBB CCC DDD EEE FFF</span>";
+ document.body.append(box);
+ const span = box.firstElementChild;
+ println(`${mode}: box ${box.offsetWidth}, span ${span.offsetWidth}, bounds ${span.getBoundingClientRect().width}`);
+ println(`columns: ${[...new Set([...span.getClientRects()].map(rect => rect.x - box.getBoundingClientRect().x))].join(", ")}`);
+ }
+ document.querySelector("#relative span").style.top = "10px";
+ report("relative");
+ document.getElementById("plain").style.webkitLineClamp = "2";
+ report("plain");
+ document.getElementById("plain").style.width = "140px";
+ report("plain");
+ document.getElementById("plain").style.webkitLineClamp = "unset";
+ document.getElementById("plain").style.display = "block";
+ report("plain");
+ });
+</script>