LibWeb: Exclude trailing whitespace from line width when placing floats

When generating line boxes, we place floats simultaneously with the
other items on the same line. The CSS text spec requires us to trim the
whitespace at the end of each line, but we only did so after laying out
all the line boxes.

This changes the way we calculate the current line box width for floats
by subtracting the amount of pixels that the current trailing whitespace
is using.

Fixes #4050.
This commit is contained in:
Jelle Raaijmakers 2025-03-26 16:11:50 +00:00
commit 002e79a658
Notes: github-actions[bot] 2025-03-26 19:36:31 +00:00
5 changed files with 99 additions and 19 deletions

View file

@ -117,10 +117,14 @@ CSSPixels LineBuilder::y_for_float_to_be_inserted_here(Box const& box)
CSSPixels candidate_block_offset = m_current_block_offset;
// Determine the current line width and subtract trailing whitespace, since those have not yet been removed while
// placing floating boxes.
auto const& current_line = ensure_last_line_box();
auto current_line_width = current_line.width() - current_line.get_trailing_whitespace_width();
// If there's already inline content on the current line, check if the new float can fit
// alongside the content. If not, place it on the next line.
if (current_line.width() > 0 && (current_line.width() + width) > m_available_width_for_current_line)
if (current_line_width > 0 && (current_line_width + width) > m_available_width_for_current_line)
candidate_block_offset += current_line.height();
// Then, look for the next Y position where we can fit the new float.