1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

LibWeb: Move Layout::TextNode whitespace collapse to separate function

This commit is contained in:
Andreas Kling 2021-04-29 08:51:31 +02:00
parent 2f1ee91b3e
commit d3e7529297
2 changed files with 35 additions and 28 deletions

View file

@ -102,18 +102,14 @@ void TextNode::paint_cursor_if_needed(PaintContext& context, const LineBoxFragme
context.painter().draw_rect(cursor_rect, computed_values().color()); context.painter().draw_rect(cursor_rect, computed_values().color());
} }
void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, LayoutMode layout_mode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks) void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace)
{ {
auto& containing_block = context.containing_block(); if (!collapse) {
m_text_for_rendering = dom_node().data();
auto& font = this->font(); return;
}
auto& line_boxes = containing_block.line_boxes();
containing_block.ensure_last_line_box();
float available_width = context.available_width_at_line(line_boxes.size() - 1) - line_boxes.last().width();
// Collapse whitespace into single spaces // Collapse whitespace into single spaces
if (do_collapse) {
auto utf8_view = Utf8View(dom_node().data()); auto utf8_view = Utf8View(dom_node().data());
StringBuilder builder(dom_node().data().length()); StringBuilder builder(dom_node().data().length());
auto it = utf8_view.begin(); auto it = utf8_view.begin();
@ -125,7 +121,7 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
} }
it = prev; it = prev;
}; };
if (line_boxes.last().is_empty_or_ends_in_whitespace()) if (previous_is_empty_or_ends_in_whitespace)
skip_over_whitespace(); skip_over_whitespace();
for (; it != utf8_view.end(); ++it) { for (; it != utf8_view.end(); ++it) {
if (!isspace(*it)) { if (!isspace(*it)) {
@ -136,10 +132,19 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou
} }
} }
m_text_for_rendering = builder.to_string(); m_text_for_rendering = builder.to_string();
} else {
m_text_for_rendering = dom_node().data();
} }
void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, LayoutMode layout_mode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks)
{
auto& containing_block = context.containing_block();
auto& font = this->font();
auto& line_boxes = containing_block.line_boxes();
containing_block.ensure_last_line_box();
float available_width = context.available_width_at_line(line_boxes.size() - 1) - line_boxes.last().width();
compute_text_for_rendering(do_collapse, line_boxes.last().is_empty_or_ends_in_whitespace());
ChunkIterator iterator(m_text_for_rendering, layout_mode, do_wrap_lines, do_wrap_breaks); ChunkIterator iterator(m_text_for_rendering, layout_mode, do_wrap_lines, do_wrap_breaks);
for (;;) { for (;;) {

View file

@ -53,6 +53,8 @@ public:
Utf8View::Iterator m_iterator; Utf8View::Iterator m_iterator;
}; };
void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace);
private: private:
virtual bool is_text_node() const final { return true; } virtual bool is_text_node() const final { return true; }
virtual bool wants_mouse_events() const override; virtual bool wants_mouse_events() const override;