Truly independent web browser
LibWeb: Keep text elided by text-overflow in the scrollable overflow
Andreas Klinge5ed007a32ef
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rsindex 9e1b0cc1dc6..a41ef391643 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>>,+ /// 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)>,}#[derive(Clone, Copy, Debug, PartialEq, Eq)]@@ -138,7 +140,11 @@ impl InlineContent {pub(crate) fn finish(data: used_values::LineData, arena: &LayoutNodeArena, content_inline_size: CssPixels) -> Self {let mut lines = Vec::with_capacity(data.line_boxes.len());let mut fragment_count = 0usize;- for line in &data.line_boxes {+ let mut elided_content_rects = Vec::new();+ for (line_index, line) in data.line_boxes.iter().enumerate() {+ if let Some(rect) = inline_formatting_context::elided_content_rect(line) {+ elided_content_rects.push((line_index as u32, rect));+ }let visible_fragment_count = line.visible_fragments().count();fragment_count = fragment_count.checked_add(visible_fragment_count)@@ -194,6 +200,7 @@ impl InlineContent {inline_box_pieces: pieces,items: Vec::new(),lines_after_clamp_point_rect: data.lines_after_clamp_point_rect,+ elided_content_rects,};// Plain text needs no interleaving and keeps its single foreground producer.if content.inline_box_pieces.is_empty() && !content.fragments.iter().any(|fragment| fragment.is_atomic_inline) {@@ -305,6 +312,12 @@ impl InlineContent {line.next_line_block_offset += offset.y;line}));+ self.elided_content_rects+ .extend(content.elided_content_rects.iter().map(|&(line_index, mut rect)| {+ rect.x += offset.x;+ rect.y += offset.y;+ (line_index + line_offset, rect)+ }));self.fragments.extend(content.fragments.iter().cloned().map(|mut fragment| {fragment.line_index += line_offset;diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex efb885c57ed..0e07414e18a 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -122,8 +122,14 @@ fn apply_text_overflow_to_line(line: &mut line_box::LineBoxData) {should_paint: true,is_rtl: false,});+ let ellipsis_fragment_inline_length = line.fragments[index].inline_length;line.fragments[index].inline_length = CssPixels::nearest_value_for_f32(last_kept_end + ellipsis_inline_size);line.fragments[index].has_text_overflow_ellipsis = true;+ line.text_overflow_elision = Some(line_box::TextOverflowElision {+ inline_length: line.inline_length,+ ellipsis_fragment_start: fragment_start,+ ellipsis_fragment_inline_length,+ });for later in &mut line.fragments[index + 1..] {later.is_fully_truncated = true;}@@ -1447,6 +1453,7 @@ impl<'context> InlineFormattingContext<'context> {.collect(),static_position_markers: Vec::new(),inline_box_baselines: Vec::new(),+ text_overflow_elision: None,};fragment_start = fragment_end;let line_item_start = item_index;@@ -2614,10 +2621,9 @@ impl<'context> InlineFormattingContext<'context> {})};for line in &mut data.line_boxes {+ // NB: Fragments that text-overflow elided keep their shift too, since the elided content still extends+ // the scrollable overflow.for fragment in &mut line.fragments {- if fragment.is_fully_truncated {- continue;- }fragment.relpos_delta = accumulated_relative_offset_from(self.callbacks.parent(fragment.layout_node));}}@@ -2749,6 +2755,61 @@ pub(crate) fn line_physical_horizontal_extent(line: &line_box::LineBoxData) -> Cright - left}+// https://drafts.csswg.org/css-overflow-3/#ellipsing-details+// Ellipsing only affects rendering and must not affect layout+// NB: So the content that text-overflow elided still extends the scrollable overflow. This is where the line's content+// would be without the ellipsis: overflowing its line box from the inline start edge.+pub(crate) fn elided_content_rect(line: &line_box::LineBoxData) -> Option<FfiCssPixelRect> {+ let elision = line.text_overflow_elision?;+ let inline_length = elision.inline_length;+ let ellipsis_index = line+ .fragments+ .iter()+ .position(|fragment| fragment.has_text_overflow_ellipsis)?;+ let fragment = &line.fragments[ellipsis_index];+ let line_box_inline_start = fragment.inline_offset - elision.ellipsis_fragment_start;+ let content_inline_start = if line.direction == direction::RTL {+ line_box_inline_start + line.original_available_inline_size.to_px_or_zero() - inline_length+ } else {+ line_box_inline_start+ };+ let content_inline_shift = content_inline_start - line_box_inline_start;+ let to_rect = |inline_start, block_start, inline_size, block_size, relpos_delta: FfiCssPixelPoint| {+ let (x, y) = geometry::to_physical(line.writing_mode, inline_start, block_start);+ let (width, height) = geometry::to_physical(line.writing_mode, inline_size, block_size);+ CssPixelRect {+ x: x + relpos_delta.x,+ y: y + relpos_delta.y,+ width,+ height,+ }+ };+ let mut rect = to_rect(+ content_inline_start,+ fragment.block_offset,+ inline_length,+ fragment.block_length,+ FfiCssPixelPoint::default(),+ );+ // NB: Like the fragments that were not elided, each elided fragment's content moves with its own relative+ // positioning, so unite the elided content of each one where it would be without the ellipsis.+ for (index, elided) in line.fragments.iter().enumerate().skip(ellipsis_index) {+ let inline_size = if index == ellipsis_index {+ elision.ellipsis_fragment_inline_length+ } else {+ elided.inline_length+ };+ rect.unite(to_rect(+ elided.inline_offset + content_inline_shift,+ elided.block_offset,+ inline_size,+ elided.block_length,+ elided.relpos_delta,+ ));+ }+ Some(rect.into())+}+pub(crate) fn line_rect(line: &line_box::LineBoxData, content_inline_size: CssPixels) -> FfiCssPixelRect {let mut rect = None::<FfiCssPixelRect>;let mut include = |x, y, width, height| {diff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rsindex c61cce67e8b..efb8c6aa934 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs@@ -39,12 +39,24 @@ pub(crate) struct InlineBoxBaseline {pub(crate) accumulated_vertical_shift: CssPixels,}+/// What text-overflow elided from the end of a line.+#[derive(Clone, Copy, PartialEq)]+pub(crate) struct TextOverflowElision {+ /// The inline length of the line before the ellipsis.+ pub(crate) inline_length: CssPixels,+ /// Where in the line the fragment that ends in the ellipsis starts.+ pub(crate) ellipsis_fragment_start: CssPixels,+ /// The inline length of the fragment that ends in the ellipsis before it was shortened.+ pub(crate) ellipsis_fragment_inline_length: CssPixels,+}+#[derive(Clone, PartialEq)]pub(crate) struct LineBoxData {pub(crate) start: super::fragmentation::LinePosition,pub(crate) fragments: Vec<line_box_fragment::LineBoxFragmentData>,pub(crate) static_position_markers: Vec<StaticPositionMarker>,pub(crate) inline_box_baselines: Vec<InlineBoxBaseline>,+ pub(crate) text_overflow_elision: Option<TextOverflowElision>,pub(crate) record: inline_content::LineRecord,}@@ -68,6 +80,7 @@ impl LineBoxData {fragments: Vec::new(),static_position_markers: Vec::new(),inline_box_baselines: Vec::new(),+ text_overflow_elision: None,record: inline_content::LineRecord {direction,writing_mode,diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs b/Libraries/LibWeb/Rust/src/painting/paintable_data.rsindex 213e8ea3949..4bb44dd9109 100644--- a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs+++ b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs@@ -268,6 +268,11 @@ impl CommittedSideData {pub(crate) fn lines(&self) -> &[crate::layout::inline_content::LineRecord] {self.inline_content.as_ref().map_or(&[], |content| &content.lines)}+ pub(crate) fn elided_content_rects(&self) -> &[(u32, used_values::FfiCssPixelRect)] {+ self.inline_content+ .as_ref()+ .map_or(&[], |content| &content.elided_content_rects)+ }pub(crate) fn lines_after_clamp_point_rect(&self) -> Option<used_values::FfiCssPixelRect> {self.inline_content.as_ref()diff --git a/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs b/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rsindex a9ebb30eb32..0322e34dc31 100644--- a/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs+++ b/Libraries/LibWeb/Rust/src/painting/scrollable_overflow.rs@@ -310,14 +310,17 @@ fn measure_scrollable_overflow_impl(if crate::painting::node_painting::has_lines(layout_arena, box_paintable) {let side_data = layout_arena.committed_side_data(box_paintable);let positions = paintable_geometry::LineRootPositions::new(layout_arena, box_paintable);- let line_rects = side_data- .lines()+ let lines = side_data.lines();+ let line_rects = lines.iter().map(|line| CssPixelRect::from(line.rect).translated_by(positions.of_fragment(line.box_fragment)));+ let elided_content_rects = side_data.elided_content_rects().iter().map(|&(line_index, rect)| {+ CssPixelRect::from(rect).translated_by(positions.of_fragment(lines[line_index as usize].box_fragment))+ });let lines_after_clamp_point = side_data.lines_after_clamp_point_rect().map(|rect| CssPixelRect::from(rect).translated_by(positions.of_fragment(0)));- for line_rect in line_rects.chain(lines_after_clamp_point) {+ for line_rect in line_rects.chain(elided_content_rects).chain(lines_after_clamp_point) {scrollable_overflow_rect.unite(line_rect);in_flow_and_floated_content_bounds.unite(line_rect);}diff --git a/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width-relative-position.txt b/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width-relative-position.txtnew file mode 100644index 00000000000..9e44c72d4e8--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width-relative-position.txt@@ -0,0 +1,4 @@+clip includes the relative shift: true+ellipsis: scrollWidth matches clip: true+shifted-first: ellipsis scrollWidth matches clip: true+shifted-last: ellipsis scrollWidth matches clip: truediff --git a/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width.txt b/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width.txtnew file mode 100644index 00000000000..c8735995716--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/text-overflow-ellipsis-scroll-width.txt@@ -0,0 +1,6 @@+clip overflows: true+ellipsis: scrollWidth matches clip: true+ellipsis-rtl: scrollWidth matches clip: true+ellipsis-after-float: scrollWidth matches clip: true+fits: scrollWidth matches clientWidth: true+ellipsis-rtl scrolls to the left: truediff --git a/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width-relative-position.html b/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width-relative-position.htmlnew file mode 100644index 00000000000..5fe8f22d723--- /dev/null+++ b/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width-relative-position.html@@ -0,0 +1,37 @@+<!DOCTYPE html>+<style>+ div {+ width: 100px;+ overflow: hidden;+ white-space: pre;+ font: 16px SerenitySans;+ }+ span {+ position: relative;+ left: 50px;+ }+ .shifted {+ left: 80px;+ }+ .unshifted {+ position: static;+ }+</style>+<div id="clip" style="text-overflow: clip">hello <span>this is a long line of text</span></div>+<div id="ellipsis" style="text-overflow: ellipsis">hello <span>this is a long line of text</span></div>+<div id="clip-unshifted" style="text-overflow: clip">hello this is a long line of text</div>+<div id="clip-shifted-first" style="text-overflow: clip"><span class="shifted">this is a long line of text</span><span class="unshifted">x</span></div>+<div id="ellipsis-shifted-first" style="text-overflow: ellipsis"><span class="shifted">this is a long line of text</span><span class="unshifted">x</span></div>+<div id="clip-shifted-last" style="text-overflow: clip"><span class="unshifted">this is a long line of text</span><span class="shifted">x</span></div>+<div id="ellipsis-shifted-last" style="text-overflow: ellipsis"><span class="unshifted">this is a long line of text</span><span class="shifted">x</span></div>+<script src="../include.js"></script>+<script>+ test(() => {+ const width = id => document.getElementById(id).scrollWidth;+ println(`clip includes the relative shift: ${width("clip") === width("clip-unshifted") + 50}`);+ println(`ellipsis: scrollWidth matches clip: ${width("ellipsis") === width("clip")}`);+ for (const variant of ["shifted-first", "shifted-last"]) {+ println(`${variant}: ellipsis scrollWidth matches clip: ${width(`ellipsis-${variant}`) === width(`clip-${variant}`)}`);+ }+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width.html b/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width.htmlnew file mode 100644index 00000000000..15c2bda0852--- /dev/null+++ b/Tests/LibWeb/Text/input/css/text-overflow-ellipsis-scroll-width.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<style>+ div {+ width: 100px;+ overflow: hidden;+ white-space: pre;+ font: 16px SerenitySans;+ }+</style>+<div id="clip" style="text-overflow: clip">hello this is a long line of text</div>+<div id="ellipsis" style="text-overflow: ellipsis">hello this is a long line of text</div>+<div id="ellipsis-rtl" style="text-overflow: ellipsis" dir="rtl">hello this is a long line of text</div>+<div id="fits" style="text-overflow: ellipsis">short</div>+<div id="clip-after-float" style="text-overflow: clip"><span style="float: left; width: 40px; height: 10px"></span>hello this is a long line of text</div>+<div id="ellipsis-after-float" style="text-overflow: ellipsis"><span style="float: left; width: 40px; height: 10px"></span>hello this is a long line of text</div>+<script src="../include.js"></script>+<script>+ test(() => {+ const clipWidth = document.getElementById("clip").scrollWidth;+ println(`clip overflows: ${clipWidth > 100}`);+ for (const id of ["ellipsis", "ellipsis-rtl"]) {+ const element = document.getElementById(id);+ println(`${id}: scrollWidth matches clip: ${element.scrollWidth === clipWidth}`);+ }+ const clipAfterFloat = document.getElementById("clip-after-float").scrollWidth;+ println(`ellipsis-after-float: scrollWidth matches clip: ${document.getElementById("ellipsis-after-float").scrollWidth === clipAfterFloat}`);+ const fits = document.getElementById("fits");+ println(`fits: scrollWidth matches clientWidth: ${fits.scrollWidth === fits.clientWidth}`);+ const rtl = document.getElementById("ellipsis-rtl");+ rtl.scrollLeft = -1000;+ println(`ellipsis-rtl scrolls to the left: ${rtl.scrollLeft < 0}`);+ });+</script>