Skip to content

Truly independent web browser

LibWeb: Refit lines around floats after vertical alignment

sideshowbarker3576a586473a
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 6e075b0d057..1105eb946b0 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -1143,7 +1143,8 @@ impl<'context> InlineFormattingContext<'context> {
line_builder: &line_builder::LineBuilder<'builder, 'context>,
state_at_clamp_point: &mut Option<LineBuildingState<'builder, 'context>>,
) -> bool {
- if state_at_clamp_point.is_some() || !self.parent.line_clamp_reached() {
+ if line_builder.only_finds_line_breaks() || state_at_clamp_point.is_some() || !self.parent.line_clamp_reached()
+ {
return false;
}
if !self.parent.clamped_content_is_scrollable_overflow() || self.run.fragments.is_none() {
@@ -2010,9 +2011,92 @@ impl<'context> InlineFormattingContext<'context> {
self.block_axis_float_clearance.set(state.block_axis_float_clearance);
}
+ /// Finds the heights that line fitting needs before placing floats or atomic content. Trials stop at block-level
+ /// content, whose layout can change the space available to the following lines.
+ fn fit_lines_around_floats<'builder>(
+ &'builder self,
+ iterator: &inline_level_iterator::InlineLevelIterator,
+ line_builder: &mut line_builder::LineBuilder<'builder, 'context>,
+ item_count: usize,
+ pending_resume_offset: Option<usize>,
+ item_state: InlineItemRunState,
+ ) {
+ let items = iterator.items();
+ let segment_has_float = items
+ .iter()
+ .take_while(|item| item.type_ != inline_level_iterator::ItemType::BlockLevelBox)
+ .any(|item| item.type_ == inline_level_iterator::ItemType::FloatingElement);
+ if self.parent.floating_box_count() == 0 && !segment_has_float {
+ return;
+ }
+ let first_changed_line = self.line_data().line_boxes.len().saturating_sub(2);
+ let saved_lines = self.line_data().line_boxes[first_changed_line..].to_vec();
+ let line_start = self.current_line_start.get();
+ let mut heights = Vec::new();
+ loop {
+ let state = self.state_before_finding_line_breaks();
+ let mut trial = line_builder.clone();
+ trial.set_float_fit_heights(heights.clone(), true);
+ let mut trial_items = iterator.duplicate(0..items.len(), items.len());
+ self.break_items_into_lines_with_float_fitting(
+ &mut trial_items,
+ &mut trial,
+ item_count,
+ pending_resume_offset,
+ None,
+ None,
+ item_state,
+ true,
+ );
+ trial.update_last_line(false);
+ let refit = trial.float_refit();
+ {
+ let mut data = self.line_data_mut();
+ data.line_boxes.truncate(first_changed_line);
+ data.line_boxes.extend(saved_lines.iter().cloned());
+ }
+ self.restore_state_after_finding_line_breaks(state);
+ self.current_line_start.set(line_start);
+ let Some((line_index, height)) = refit else {
+ break;
+ };
+ heights.resize(heights.len().max(line_index + 1), CssPixels::default());
+ assert!(height > heights[line_index]);
+ heights[line_index] = height;
+ }
+ line_builder.set_float_fit_heights(heights, false);
+ }
+
/// Breaks the items left in `iterator` into lines with `line_builder`. `item_count` is the number of items the
/// iterator started with.
fn break_items_into_lines<'builder>(
+ &'builder self,
+ iterator: &mut inline_level_iterator::InlineLevelIterator,
+ line_builder: &mut line_builder::LineBuilder<'builder, 'context>,
+ item_count: usize,
+ pending_resume_offset: Option<usize>,
+ line_limit: Option<FragmentainerLineLimit>,
+ consumed_item_nodes: Option<Vec<(usize, Node)>>,
+ ) -> BrokenLines<'builder, 'context> {
+ self.break_items_into_lines_with_float_fitting(
+ iterator,
+ line_builder,
+ item_count,
+ pending_resume_offset,
+ line_limit,
+ consumed_item_nodes,
+ InlineItemRunState {
+ collapsed_whitespace_node: NodeSlotId::INVALID,
+ ..Default::default()
+ },
+ false,
+ )
+ }
+
+ #[allow(clippy::too_many_arguments)]
+ /// Breaks the items left in `iterator` into lines with `line_builder`. `item_count` is the number of items the
+ /// iterator started with.
+ fn break_items_into_lines_with_float_fitting<'builder>(
&'builder self,
iterator: &mut inline_level_iterator::InlineLevelIterator,
line_builder: &mut line_builder::LineBuilder<'builder, 'context>,
@@ -2020,20 +2104,23 @@ impl<'context> InlineFormattingContext<'context> {
mut pending_resume_offset: Option<usize>,
mut line_limit: Option<FragmentainerLineLimit>,
mut consumed_item_nodes: Option<Vec<(usize, Node)>>,
+ mut item_state: InlineItemRunState,
+ float_fit_trial: bool,
) -> BrokenLines<'builder, 'context> {
let shares_items = iterator.shares_items();
- let mut leading_margin = CssPixels::default();
- let mut leading_border = CssPixels::default();
- let mut leading_padding = CssPixels::default();
- let mut leading_right_to_left_end_margin = CssPixels::default();
- let mut collapsed_whitespace_node = NodeSlotId::INVALID;
let mut absolute_boxes = Vec::new();
- let mut previous_text_item_allows_overflow_break_after = false;
- let mut whitespace_measured_next_run = false;
let mut state_at_clamp_point = None;
let mut items_after_clamp_point = Vec::new();
let mut stopped_at_line_limit = false;
+ let mut needs_float_fitting = !float_fit_trial;
loop {
+ if float_fit_trial && line_builder.float_refit().is_some() {
+ break;
+ }
+ if needs_float_fitting {
+ needs_float_fitting = false;
+ self.fit_lines_around_floats(iterator, line_builder, item_count, pending_resume_offset, item_state);
+ }
if self.stops_at_line_clamp_point(line_builder, &mut state_at_clamp_point) {
break;
}
@@ -2062,9 +2149,9 @@ impl<'context> InlineFormattingContext<'context> {
style.word_spacing().to_double() as f32,
);
}
- let has_leading_edges = leading_margin != CssPixels::default()
- || leading_border != CssPixels::default()
- || leading_padding != CssPixels::default();
+ let has_leading_edges = item_state.leading_margin != CssPixels::default()
+ || item_state.leading_border != CssPixels::default()
+ || item_state.leading_padding != CssPixels::default();
if !has_leading_edges {
self.current_line_start.set(fragmentation::LinePosition {
item_index,
@@ -2072,8 +2159,8 @@ impl<'context> InlineFormattingContext<'context> {
});
}
let can_break_after_previous_overflow_item =
- std::mem::take(&mut previous_text_item_allows_overflow_break_after);
- let run_fits_after_previous_whitespace = std::mem::take(&mut whitespace_measured_next_run);
+ std::mem::take(&mut item_state.previous_text_item_allows_overflow_break_after);
+ let run_fits_after_previous_whitespace = std::mem::take(&mut item_state.whitespace_measured_next_run);
let line_starts_with_whitespace = self
.line_data()
.line_boxes
@@ -2081,19 +2168,19 @@ impl<'context> InlineFormattingContext<'context> {
.is_none_or(|line| line.is_empty_or_ends_in_whitespace() || line.has_block_level_box);
if item.is_collapsible_whitespace && line_starts_with_whitespace {
// NB: The whitespace disappears, but the inline boxes that end with it still end on this line.
- collapsed_whitespace_node = item.node;
- leading_margin += item.margin_start;
- leading_border += item.border_start;
- leading_padding += item.padding_start;
- leading_right_to_left_end_margin += item.entered_right_to_left_end_margin;
+ item_state.collapsed_whitespace_node = item.node;
+ item_state.leading_margin += item.margin_start;
+ item_state.leading_border += item.border_start;
+ item_state.leading_padding += item.padding_start;
+ item_state.leading_right_to_left_end_margin += item.entered_right_to_left_end_margin;
let zero = CssPixels::default();
if item.margin_end != zero || item.border_end != zero || item.padding_end != zero {
// NB: A box that ends here started on this line, so its start edges come before its end edges.
line_builder.append_start_edges_of_collapsed_whitespace(
item.node,
- std::mem::take(&mut leading_margin),
- std::mem::take(&mut leading_border),
- std::mem::take(&mut leading_padding),
+ std::mem::take(&mut item_state.leading_margin),
+ std::mem::take(&mut item_state.leading_border),
+ std::mem::take(&mut item_state.leading_padding),
);
line_builder.append_end_edges_of_collapsed_whitespace(
item.node,
@@ -2114,14 +2201,14 @@ impl<'context> InlineFormattingContext<'context> {
// NB: A float is not on the line, so the leading edges wait for the next item that is.
if item.type_ != inline_level_iterator::ItemType::FloatingElement {
- item.margin_start += leading_margin;
- item.border_start += leading_border;
- item.padding_start += leading_padding;
- item.entered_right_to_left_end_margin += leading_right_to_left_end_margin;
- leading_margin = CssPixels::default();
- leading_border = CssPixels::default();
- leading_padding = CssPixels::default();
- leading_right_to_left_end_margin = CssPixels::default();
+ item.margin_start += item_state.leading_margin;
+ item.border_start += item_state.leading_border;
+ item.padding_start += item_state.leading_padding;
+ item.entered_right_to_left_end_margin += item_state.leading_right_to_left_end_margin;
+ item_state.leading_margin = CssPixels::default();
+ item_state.leading_border = CssPixels::default();
+ item_state.leading_padding = CssPixels::default();
+ item_state.leading_right_to_left_end_margin = CssPixels::default();
}
match item.type_ {
@@ -2133,7 +2220,7 @@ impl<'context> InlineFormattingContext<'context> {
}
inline_level_iterator::ItemType::ForcedBreak => {
line_builder.append_start_edges_of_collapsed_whitespace(
- collapsed_whitespace_node,
+ item_state.collapsed_whitespace_node,
item.margin_start,
item.border_start,
item.padding_start,
@@ -2152,11 +2239,14 @@ impl<'context> InlineFormattingContext<'context> {
}
inline_level_iterator::ItemType::Element => {
line_builder.prepare_to_append_inline_content();
- if shares_items && !self.laid_out_atomic_inline(item.node) {
- // NB: The box is laid out again in this run, and keeps the content baselines it had.
- let cells = self.hold_atomic_inline(item.node);
- let (_, laid_out_cells) = self.dimension_box_on_line(item.node, cells);
- item.set_box_cells(laid_out_cells);
+ if shares_items || float_fit_trial {
+ let cells = if self.laid_out_atomic_inline(item.node) {
+ self.placed_atomic_inline(item.node).geometry.cells
+ } else {
+ let cells = self.hold_atomic_inline(item.node);
+ self.dimension_box_on_line(item.node, cells).1
+ };
+ item.set_box_cells(cells);
}
if !line_builder.only_finds_line_breaks() {
let insets = self.compute_inset(item.node);
@@ -2181,11 +2271,15 @@ impl<'context> InlineFormattingContext<'context> {
line_builder.append_box(&item);
}
inline_level_iterator::ItemType::BlockLevelBox => {
- leading_margin += item.margin_start;
- leading_border += item.border_start;
- leading_padding += item.padding_start;
- leading_right_to_left_end_margin += item.entered_right_to_left_end_margin;
+ item_state.leading_margin += item.margin_start;
+ item_state.leading_border += item.border_start;
+ item_state.leading_padding += item.padding_start;
+ item_state.leading_right_to_left_end_margin += item.entered_right_to_left_end_margin;
line_builder.finish_current_line_before_block_level_box();
+ if float_fit_trial {
+ break;
+ }
+ needs_float_fitting = true;
if self.stops_at_line_clamp_point(line_builder, &mut state_at_clamp_point) {
break;
}
@@ -2224,7 +2318,7 @@ impl<'context> InlineFormattingContext<'context> {
|| item.border_start != CssPixels::default()
|| item.padding_start != CssPixels::default();
line_builder.append_start_edges_of_collapsed_whitespace(
- collapsed_whitespace_node,
+ item_state.collapsed_whitespace_node,
item.margin_start,
item.border_start,
item.padding_start,
@@ -2239,6 +2333,12 @@ impl<'context> InlineFormattingContext<'context> {
iterator.next_unbreakable_run_inline_size(self).unwrap_or_default(),
);
if line_builder.only_finds_line_breaks() {
+ if !self.floats_laid_out_ahead_of_lines.borrow().contains_key(&item.node) {
+ let laid_out_float = self.lay_out_float_ahead_of_its_position(item.node);
+ self.floats_laid_out_ahead_of_lines
+ .borrow_mut()
+ .insert(item.node, laid_out_float);
+ }
let cells = self
.floats_laid_out_ahead_of_lines
.borrow()
@@ -2314,7 +2414,7 @@ impl<'context> InlineFormattingContext<'context> {
}
continue;
}
- whitespace_measured_next_run = true;
+ item_state.whitespace_measured_next_run = true;
}
TextItemWrapOpportunity::BeforeText
| TextItemWrapOpportunity::AfterOverflowBreakableText => {
@@ -2351,9 +2451,13 @@ impl<'context> InlineFormattingContext<'context> {
stopped_at_line_limit = true;
break;
}
- previous_text_item_allows_overflow_break_after = true;
+ item_state.previous_text_item_allows_overflow_break_after = true;
}
- } else if item.can_break_before || can_break_after_previous_overflow_item {
+ } else if item.can_break_before
+ || can_break_after_previous_overflow_item
+ || (line_builder.current_line_is_empty()
+ && self.style(self.containing_block).text_wrap_mode() == text_wrap_mode::WRAP)
+ {
line_builder.break_if_needed(iterator.unbreakable_run_inline_size(
self,
&item,
@@ -2370,10 +2474,10 @@ impl<'context> InlineFormattingContext<'context> {
}
if iterator.items().is_empty() {
line_builder.append_start_edges_of_collapsed_whitespace(
- collapsed_whitespace_node,
- leading_margin,
- leading_border,
- leading_padding,
+ item_state.collapsed_whitespace_node,
+ item_state.leading_margin,
+ item_state.leading_border,
+ item_state.leading_padding,
);
line_builder.keep_line_of_collapsed_whitespace_edges();
}
@@ -3108,6 +3212,17 @@ struct StateBeforeFindingLineBreaks {
block_axis_float_clearance: CssPixels,
}
+#[derive(Clone, Copy, Default)]
+struct InlineItemRunState {
+ leading_margin: CssPixels,
+ leading_border: CssPixels,
+ leading_padding: CssPixels,
+ leading_right_to_left_end_margin: CssPixels,
+ collapsed_whitespace_node: Node,
+ previous_text_item_allows_overflow_break_after: bool,
+ whitespace_measured_next_run: bool,
+}
+
/// What breaking the items into lines left to finish once the lines are built.
struct BrokenLines<'builder, 'context> {
absolute_boxes: Vec<Node>,
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 676bffe8bc3..56549ad217b 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -68,8 +68,13 @@ pub(crate) struct LineBuilder<'builder, 'context> {
/// between forced breaks, for the lines to break at.
balancing_reductions: Vec<Option<CssPixels>>,
forced_break_count: usize,
- /// Whether this builder only finds where lines break, to balance them, and leaves the boxes on the lines unplaced.
+ /// Whether this builder finds line breaks without placing the boxes on the lines.
only_finds_line_breaks: bool,
+ /// Heights retained across retries, indexed by content lines rather than empty lines skipped to clear floats.
+ float_fit_heights: Option<Vec<CssPixels>>,
+ float_fit_line_index: usize,
+ float_fit_trial: bool,
+ float_refit: Option<(usize, CssPixels)>,
current_block_offset: CssPixels,
max_block_size_on_current_line: CssPixels,
nodes_with_edges_of_collapsed_whitespace: Vec<(Node, CssPixels)>,
@@ -108,6 +113,29 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
builder
}
+ pub(crate) fn set_float_fit_heights(&mut self, heights: Vec<CssPixels>, trial: bool) {
+ self.float_fit_heights = Some(heights);
+ self.float_fit_line_index = 0;
+ self.float_fit_trial = trial;
+ self.float_refit = None;
+ if trial {
+ self.only_finds_line_breaks = true;
+ }
+ self.recalculate_available_space();
+ }
+
+ fn float_fit_height(&self) -> CssPixels {
+ self.float_fit_heights
+ .as_ref()
+ .and_then(|heights| heights.get(self.float_fit_line_index))
+ .copied()
+ .unwrap_or_default()
+ }
+
+ pub(crate) fn float_refit(&self) -> Option<(usize, CssPixels)> {
+ self.float_refit
+ }
+
pub(crate) fn set_balancing_reductions(&mut self, balancing_reductions: Vec<Option<CssPixels>>) {
self.balancing_reductions = balancing_reductions;
}
@@ -139,6 +167,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
balancing_reductions: Vec::new(),
forced_break_count: 0,
only_finds_line_breaks: false,
+ float_fit_heights: None,
+ float_fit_line_index: 0,
+ float_fit_trial: false,
+ float_refit: None,
current_block_offset: CssPixels::default(),
max_block_size_on_current_line: CssPixels::default(),
nodes_with_edges_of_collapsed_whitespace: Vec::new(),
@@ -277,6 +309,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
self.current_block_offset += self.max_block_size_on_current_line.max(line_height);
}
}
+ if self.float_fit_heights.is_some() {
+ self.max_block_size_on_current_line = CssPixels::default();
+ }
self.recalculate_available_space();
let line_index = self.ensure_last_line_index();
self.line_mut(line_index).original_available_inline_size = self.available_inline_size_for_current_line;
@@ -309,6 +344,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
}
self.last_line_needs_update = true;
self.update_last_line(forced == ForcedBreak::No || next_size.is_some());
+ if forced == ForcedBreak::Yes || !self.line(line_index).fragments.is_empty() {
+ self.float_fit_line_index += 1;
+ }
if forced == ForcedBreak::Yes {
self.forced_break_count += 1;
}
@@ -319,7 +357,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
self.begin_new_line(true, break_count == 0, forced);
break_count += 1;
let line_height = self.containing_style().line_height();
- let current_line_block_size = self.max_block_size_on_current_line.max(line_height);
+ let current_line_block_size = self
+ .max_block_size_on_current_line
+ .max(line_height)
+ .max(self.float_fit_height());
let floats_intrude = self.context().any_floats_intrude_in_block_range(
self.current_block_offset,
self.current_block_offset + current_line_block_size,
@@ -329,7 +370,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
if !floats_intrude
|| (self
.context()
- .can_fit_new_line_at_block_offset(self.current_block_offset, line_height)
+ .can_fit_new_line_at_block_offset(self.current_block_offset, current_line_block_size)
&& !next_too_wide)
{
break;
@@ -346,7 +387,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
}
fn floats_intrude_at_current_line_start(&self) -> bool {
- let line_height = self.containing_style().line_height();
+ let line_height = self.containing_style().line_height().max(self.float_fit_height());
self.context()
.any_floats_intrude_in_block_range(self.current_block_offset, self.current_block_offset + line_height)
}
@@ -425,7 +466,11 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
}
let line_height = self.containing_style().line_height();
let block_start = self.current_block_offset;
- let block_end = block_start + self.max_block_size_on_current_line.max(line_height);
+ let block_end = block_start
+ + self
+ .max_block_size_on_current_line
+ .max(line_height)
+ .max(self.float_fit_height());
self.context().any_floats_intrude_in_block_range(block_start, block_end)
}
@@ -1091,9 +1136,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
if has_fragments {
self.prepare_to_append_inline_content();
}
- let containing_style = self.containing_style();
+ let containing_style = self.containing_style;
let unclamped_line_block_size = self.max_block_size_on_current_line.max(containing_style.line_height());
- if has_fragments || self.line(line_index).has_forced_break {
+ if !self.only_finds_line_breaks && (has_fragments || self.line(line_index).has_forced_break) {
self.context().prepare_line_for_line_clamp(
line_index,
has_immediate_continuation,
@@ -1117,9 +1162,30 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
} else {
unclamped_line_block_size
};
+ let fitted_height = current_line_block_size.max(self.float_fit_height());
+ if self.float_fit_trial && has_fragments {
+ let line_before_alignment = self.line(line_index).clone();
+ let alignment = self.align_fragments_of_line(line_index, CssPixels::default(), CssPixels::default());
+ *self.line_mut(line_index) = line_before_alignment;
+ let height = alignment.latest - alignment.earliest;
+ let available = self
+ .context()
+ .available_space_for_line(self.current_block_offset, height);
+ if height > self.float_fit_height()
+ && (available != self.available_inline_size_for_current_line
+ || self
+ .context()
+ .leftmost_inline_offset_at(self.current_block_offset, height)
+ != self
+ .context()
+ .leftmost_inline_offset_at(self.current_block_offset, fitted_height))
+ {
+ self.float_refit.get_or_insert((self.float_fit_line_index, height));
+ }
+ }
let start_inline_offset = self
.context()
- .leftmost_inline_offset_at(self.current_block_offset, current_line_block_size);
+ .leftmost_inline_offset_at(self.current_block_offset, fitted_height);
let aligned_inline_offset = |inline_length| {
let excess = self.available_inline_size_for_current_line.to_px_or_zero() - inline_length;
if excess < CssPixels::default() {
@@ -1148,7 +1214,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
earliest,
latest,
line_box_baseline,
- should_advance_to_line_box_block_end,
+ mut should_advance_to_line_box_block_end,
has_line_relative_aligned_subtree,
mut aligned_subtrees,
first_unshifted_text_baseline,
@@ -1156,7 +1222,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
let fragment_count = self.line(line_index).fragments.len();
let current_block_offset = self.current_block_offset;
- let normal_subtree_shift = if has_line_relative_aligned_subtree {
+ let refitted = self.float_fit_height() > CssPixels::default();
+ should_advance_to_line_box_block_end |= refitted;
+ let normal_subtree_shift = if has_line_relative_aligned_subtree || refitted {
self.current_block_offset - earliest
} else {
CssPixels::default()
@@ -1169,7 +1237,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
};
}
- if has_line_relative_aligned_subtree {
+ if has_line_relative_aligned_subtree || refitted {
for fragment_index in 0..fragment_count {
let style_source = self.line(line_index).fragments[fragment_index].style_source;
let shift = self.subtree_shift_for_box(style_source, &aligned_subtrees, normal_subtree_shift);
@@ -1553,7 +1621,8 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
pub(crate) fn recalculate_available_space(&mut self) {
let current_line_block_size = self
.max_block_size_on_current_line
- .max(self.containing_style().line_height());
+ .max(self.containing_style().line_height())
+ .max(self.float_fit_height());
self.available_inline_size_for_current_line = self
.context()
.available_space_for_line(self.current_block_offset, current_line_block_size);
diff --git a/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom-floats.txt b/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom-floats.txt
new file mode 100644
index 00000000000..99232df76bd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom-floats.txt
@@ -0,0 +1,2 @@
+top float: height=200, words=100,38; 0,138
+bottom float: height=200, words=100,38; 0,138
diff --git a/Tests/LibWeb/Text/expected/css/line-height-refits-floats.txt b/Tests/LibWeb/Text/expected/css/line-height-refits-floats.txt
new file mode 100644
index 00000000000..47e02f76263
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/line-height-refits-floats.txt
@@ -0,0 +1,34 @@
+left-plain: float=0,30,100,50 line-height=24 text=0,0,48,24;72,0,48,24 atoms=
+left-nested: float=0,30,100,50 line-height=100 text=100,13,48,24;100,63,48,24 atoms=
+left-direct: float=0,30,100,50 line-height=100 text=100,13,48,24;100,63,48,24 atoms=
+left-atomic: float=0,30,100,50 line-height=69 text=100,45,48,24 atoms=100,0,60,40
+left-shift-up: float=0,30,100,50 line-height=78 text=100,0,48,24;100,54,48,24 atoms=
+left-shift-down: float=0,30,100,50 line-height=78 text=100,30,48,24;100,54,48,24 atoms=
+left-nested-atomic: float=0,30,100,50 line-height=74 text=100,50,48,24 atoms=100,12,60,20
+left-nested-wide: float=0,30,100,50 line-height=130 text=0,93,144,24 atoms=
+left-tall-last: float=0,30,100,50 line-height=69 text=0,0,48,24;72,0,48,24 atoms=100,24,60,40
+left-shift-last: float=0,30,100,50 line-height=78 text=100,0,48,24;100,54,48,24 atoms=
+left-nested-nowrap: float=0,30,100,50 line-height=130 text=0,93,48,24;72,93,48,24 atoms=
+left-nested-break-word: float=0,30,100,50 line-height=150 text=100,13,96,24;100,63,96,24;0,113,48,24 atoms=
+left-inline-float: float=0,30,100,50 line-height=100 text=100,13,48,24;100,63,48,24 atoms=
+left-interrupted: float=0,30,100,50 line-height=160 text=100,13,48,24;100,73,48,24;0,123,48,24 atoms=
+left-root-nowrap: float=0,30,100,50 line-height=50 text=100,13,48,24;172,13,48,24 atoms=
+left-nested-wide-late-float: float=0,49,100,50 line-height=149 text=0,112,144,24 atoms=
+left-atomic-beyond: float=0,42,100,50 line-height=69 text=100,45,48,24 atoms=100,0,60,40
+right-plain: float=100,30,100,50 line-height=24 text=0,0,48,24;72,0,48,24 atoms=
+right-nested: float=100,30,100,50 line-height=100 text=0,13,48,24;0,63,48,24 atoms=
+right-direct: float=100,30,100,50 line-height=100 text=0,13,48,24;0,63,48,24 atoms=
+right-atomic: float=100,30,100,50 line-height=69 text=0,45,48,24 atoms=0,0,60,40
+right-shift-up: float=100,30,100,50 line-height=78 text=0,0,48,24;0,54,48,24 atoms=
+right-shift-down: float=100,30,100,50 line-height=78 text=0,30,48,24;0,54,48,24 atoms=
+right-nested-atomic: float=100,30,100,50 line-height=74 text=0,50,48,24 atoms=0,12,60,20
+right-nested-wide: float=100,30,100,50 line-height=130 text=0,93,144,24 atoms=
+right-tall-last: float=100,30,100,50 line-height=69 text=0,0,48,24;72,0,48,24 atoms=0,24,60,40
+right-shift-last: float=100,30,100,50 line-height=78 text=0,0,48,24;0,54,48,24 atoms=
+right-nested-nowrap: float=100,30,100,50 line-height=130 text=0,93,48,24;72,93,48,24 atoms=
+right-nested-break-word: float=100,30,100,50 line-height=150 text=0,13,96,24;0,63,96,24;0,113,48,24 atoms=
+right-inline-float: float=100,30,100,50 line-height=100 text=0,13,48,24;0,63,48,24 atoms=
+right-interrupted: float=100,30,100,50 line-height=160 text=0,13,48,24;0,73,48,24;0,123,48,24 atoms=
+right-root-nowrap: float=100,30,100,50 line-height=50 text=0,13,48,24;72,13,48,24 atoms=
+right-nested-wide-late-float: float=100,49,100,50 line-height=149 text=0,112,144,24 atoms=
+right-atomic-beyond: float=100,42,100,50 line-height=69 text=0,45,48,24 atoms=0,0,60,40
diff --git a/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom-floats.html b/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom-floats.html
new file mode 100644
index 00000000000..ee8def0b4e9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom-floats.html
@@ -0,0 +1,36 @@
+<!doctype html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ .line {
+ display: flow-root;
+ width: 200px;
+ font: 24px/24px Ahem;
+ }
+ .outer {
+ line-height: 100px;
+ }
+ .inner {
+ line-height: 24px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await document.fonts.load("24px Ahem");
+ for (const alignment of ["top", "bottom"]) {
+ const line = document.createElement("div");
+ line.className = "line";
+ line.innerHTML = `<div style="height: 0"><div style="height: 30px"></div><div style="float: left; width: 100px; height: 50px"></div></div><span class="outer" style="vertical-align: ${alignment}"><span class="inner"><span class="word">AA</span> <span class="word">AA</span></span></span>`;
+ document.body.appendChild(line);
+ const rect = line.getBoundingClientRect();
+ const words = [...line.querySelectorAll(".word")].map(word => {
+ const wordRect = word.getBoundingClientRect();
+ return `${wordRect.left - rect.left},${wordRect.top - rect.top}`;
+ });
+ println(`${alignment} float: height=${rect.height}, words=${words.join("; ")}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/line-height-refits-floats.html b/Tests/LibWeb/Text/input/css/line-height-refits-floats.html
new file mode 100644
index 00000000000..b8e8b3ce9a4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/line-height-refits-floats.html
@@ -0,0 +1,122 @@
+<!doctype html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ .case {
+ display: flow-root;
+ width: 200px;
+ margin-bottom: 20px;
+ font: 24px/24px Ahem;
+ }
+ .float {
+ width: 100px;
+ height: 50px;
+ background: red;
+ }
+ .seed {
+ height: 0;
+ }
+ .spacer {
+ height: 30px;
+ }
+ .outer {
+ line-height: 50px;
+ }
+ .inner {
+ line-height: 24px;
+ }
+ .atom {
+ display: inline-block;
+ width: 60px;
+ height: 40px;
+ background: green;
+ }
+</style>
+<div id="cases"></div>
+<script>
+ const cases = [
+ ["plain", "<span class='word'>AA</span> <span class='word'>AA</span>"],
+ [
+ "nested",
+ "<span class='outer'><span class='inner'><span class='word'>AA</span> <span class='word'>AA</span></span></span>",
+ ],
+ ["direct", "<span class='outer'>AA AA</span>"],
+ ["atomic", "<span class='atom'></span> <span class='word'>AA</span>"],
+ [
+ "shift-up",
+ "<span style='vertical-align:30px'><span class='word'>AA</span></span> <span class='word'>AA</span>",
+ ],
+ [
+ "shift-down",
+ "<span style='vertical-align:-30px'><span class='word'>AA</span></span> <span class='word'>AA</span>",
+ ],
+ [
+ "nested-atomic",
+ "<span class='outer'><span class='atom' style='height:20px'></span></span> <span class='word'>AA</span>",
+ ],
+ ["nested-wide", "<span class='outer'><span class='inner'><span class='word'>AAAAAA</span></span></span>"],
+ ["tall-last", "<span class='word'>AA</span> <span class='word'>AA</span> <span class='atom'></span>"],
+ [
+ "shift-last",
+ "<span class='word'>AA</span> <span style='vertical-align:-30px'><span class='word'>AA</span></span>",
+ ],
+ [
+ "nested-nowrap",
+ "<span class='outer' style='white-space:nowrap'><span class='inner'><span class='word'>AA AA</span></span></span>",
+ ],
+ [
+ "nested-break-word",
+ "<span class='outer' style='overflow-wrap:anywhere'><span class='inner'>AAAAAAAAAA</span></span>",
+ ],
+ [
+ "inline-float",
+ "<span class='outer'><span class='inner'>AA <span style='float:right;width:48px;height:24px;background:blue'></span>AA</span></span>",
+ ],
+ ["interrupted", "<span class='outer'><span class='inner'>AA<div style='height:10px'></div>AA AA</span></span>"],
+ ["root-nowrap", "<span class='outer'><span class='inner'>AA AA</span></span>", 30, true],
+ ["nested-wide-late-float", "<span class='outer'><span class='inner'>AAAAAA</span></span>", 49],
+ ["atomic-beyond", "<span class='atom'></span> <span class='word'>AA</span>", 42],
+ ];
+ for (const side of ["left", "right"]) {
+ for (const [name, content, top = 30, nowrap = false] of cases) {
+ const box = document.createElement("div");
+ box.className = "case";
+ box.id = side + "-" + name;
+ box.innerHTML = `<div class="seed"><div class="spacer" style="height:${top}px"></div><div class="float" style="float:${side}"></div></div><div class="line">${content}</div>`;
+ if (nowrap) box.querySelector(".line").style.whiteSpace = "nowrap";
+ document.getElementById("cases").append(box);
+ }
+ }
+ promiseTest(async () => {
+ await document.fonts.ready;
+ function rect(r, b) {
+ return [r.x - b.x, r.y - b.y, r.width, r.height].map(x => Math.round(x * 100) / 100).join(",");
+ }
+ for (const box of document.querySelectorAll(".case")) {
+ const b = box.getBoundingClientRect(),
+ f = box.querySelector(".float").getBoundingClientRect(),
+ line = box.querySelector(".line");
+ const values = [];
+ const walk = document.createTreeWalker(line, NodeFilter.SHOW_TEXT);
+ while (walk.nextNode()) {
+ const node = walk.currentNode;
+ for (const match of node.textContent.matchAll(/\S+/g)) {
+ const range = document.createRange();
+ range.setStart(node, match.index);
+ range.setEnd(node, match.index + match[0].length);
+ for (const r of range.getClientRects()) values.push(rect(r, b));
+ }
+ }
+ const atoms = [...box.querySelectorAll(".atom")].map(a => rect(a.getBoundingClientRect(), b));
+ println(
+ `${box.id}: float=${rect(f, b)} line-height=${line.getBoundingClientRect().height} text=${values.join(";")} atoms=${atoms.join(";")}`
+ );
+ }
+ });
+</script>