1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:07:44 +00:00

LibWeb: Fix inline blocks swallowing trailing whitespace

In #10434 an issue with leading whitespace in new lines after
a <br> element was fixed by checking whether the last fragment
of LineBox is empty.

However, this introduced a regression by which whitespace following
inline elements was swallowed, so `<b>Test</b> 123` would appear
like `Test123`.

By asking specifically if we are handling a forced linebreak
instead of implicity asking for a property that may be shared by
other Node types, we can maintain the correct behavior in regards
to leading whitespace on new lines, as well as trailing whitespace
of inline elements.
This commit is contained in:
Felix Rauch 2021-10-26 16:15:53 +02:00 committed by Andreas Kling
parent 7426feacf7
commit 30c39e0e41
5 changed files with 15 additions and 3 deletions

View file

@ -19,9 +19,13 @@ public:
const HTML::HTMLBRElement& dom_node() const { return verify_cast<HTML::HTMLBRElement>(*Node::dom_node()); }
private:
virtual bool is_break_node() const final { return true; }
virtual void paint(PaintContext&, PaintPhase) override;
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
};
template<>
inline bool Node::fast_is<BreakNode>() const { return is_break_node(); }
}

View file

@ -5,8 +5,10 @@
*/
#include <AK/CharacterTypes.h>
#include <AK/TypeCasts.h>
#include <AK/Utf8View.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/LineBox.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/TextNode.h>
@ -61,9 +63,13 @@ bool LineBox::is_empty_or_ends_in_whitespace() const
{
if (m_fragments.is_empty())
return true;
if (m_fragments.last().length() == 0)
return true;
return m_fragments.last().ends_in_whitespace();
}
bool LineBox::ends_with_forced_line_break() const
{
return is<BreakNode>(m_fragments.last().layout_node());
}
}

View file

@ -26,6 +26,7 @@ public:
void trim_trailing_whitespace();
bool is_empty_or_ends_in_whitespace() const;
bool ends_with_forced_line_break() const;
private:
friend class BlockContainer;

View file

@ -99,6 +99,7 @@ public:
// These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
virtual bool is_box() const { return false; }
virtual bool is_block_container() const { return false; }
virtual bool is_break_node() const { return false; }
virtual bool is_text_node() const { return false; }
virtual bool is_initial_containing_block_box() const { return false; }
virtual bool is_svg_box() const { return false; }

View file

@ -217,7 +217,7 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
float chunk_width;
if (do_wrap_lines) {
if (do_collapse && is_ascii_space(*chunk.view.begin()) && line_boxes.last().is_empty_or_ends_in_whitespace()) {
if (do_collapse && is_ascii_space(*chunk.view.begin()) && (line_boxes.last().is_empty_or_ends_in_whitespace() || line_boxes.last().ends_with_forced_line_break())) {
// This is a non-empty chunk that starts with collapsible whitespace.
// We are at either at the start of a new line, or after something that ended in whitespace,
// so we don't need to contribute our own whitespace to the line. Skip over it instead!