LibWeb: Whitespace that causes a line to wrap should be hidden

We were only pruning trailing whitespace on lines. This patch makes it
so we also don't add whitespace as the leading line box fragment on new
lines.

This logic is pretty crufty and I think we can do better, but for now
I've just made it handle this extra case so we can stop having lines
that start with a space character. :^)
This commit is contained in:
Andreas Kling 2020-12-17 20:17:29 +01:00
parent 172707a945
commit 6d7892cfc4
Notes: sideshowbarker 2024-07-19 00:46:26 +09:00
3 changed files with 9 additions and 6 deletions

View file

@ -73,10 +73,10 @@ void LineBox::trim_trailing_whitespace()
}
}
bool LineBox::ends_in_whitespace() const
bool LineBox::is_empty_or_ends_in_whitespace() const
{
if (m_fragments.is_empty())
return false;
return true;
return m_fragments.last().ends_in_whitespace();
}

View file

@ -45,7 +45,7 @@ public:
void trim_trailing_whitespace();
bool ends_in_whitespace() const;
bool is_empty_or_ends_in_whitespace() const;
private:
friend class BlockBox;

View file

@ -210,7 +210,7 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
}
it = prev;
};
if (line_boxes.last().ends_in_whitespace())
if (line_boxes.last().is_empty_or_ends_in_whitespace())
skip_over_whitespace();
for (; it != utf8_view.end(); ++it) {
if (!isspace(*it)) {
@ -246,13 +246,13 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
auto& chunk = chunks[i];
// Collapse entire fragment into non-existence if previous fragment on line ended in whitespace.
if (do_collapse && line_boxes.last().ends_in_whitespace() && chunk.is_all_whitespace)
if (do_collapse && line_boxes.last().is_empty_or_ends_in_whitespace() && chunk.is_all_whitespace)
continue;
float chunk_width;
bool need_collapse = false;
if (do_wrap_lines) {
need_collapse = do_collapse && isspace(*chunk.view.begin()) && line_boxes.last().ends_in_whitespace();
need_collapse = do_collapse && isspace(*chunk.view.begin()) && line_boxes.last().is_empty_or_ends_in_whitespace();
if (need_collapse)
chunk_width = space_width;
@ -262,6 +262,9 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
if (line_boxes.last().width() > 0 && chunk_width > available_width) {
containing_block.add_line_box();
available_width = context.available_width_at_line(line_boxes.size() - 1);
if (do_collapse && chunk.is_all_whitespace)
continue;
}
if (need_collapse & line_boxes.last().fragments().is_empty())
continue;