From 6d7892cfc4354008dba3f09f0edbf04e0b746830 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 17 Dec 2020 20:17:29 +0100 Subject: [PATCH] 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. :^) --- Libraries/LibWeb/Layout/LineBox.cpp | 4 ++-- Libraries/LibWeb/Layout/LineBox.h | 2 +- Libraries/LibWeb/Layout/TextNode.cpp | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Libraries/LibWeb/Layout/LineBox.cpp b/Libraries/LibWeb/Layout/LineBox.cpp index b4415ae11b..cf2a127755 100644 --- a/Libraries/LibWeb/Layout/LineBox.cpp +++ b/Libraries/LibWeb/Layout/LineBox.cpp @@ -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(); } diff --git a/Libraries/LibWeb/Layout/LineBox.h b/Libraries/LibWeb/Layout/LineBox.h index cc821d9d6e..dcf1efd081 100644 --- a/Libraries/LibWeb/Layout/LineBox.h +++ b/Libraries/LibWeb/Layout/LineBox.h @@ -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; diff --git a/Libraries/LibWeb/Layout/TextNode.cpp b/Libraries/LibWeb/Layout/TextNode.cpp index 06b9093269..f3b1d7cbe8 100644 --- a/Libraries/LibWeb/Layout/TextNode.cpp +++ b/Libraries/LibWeb/Layout/TextNode.cpp @@ -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;