diff options
author | Andreas Kling <kling@serenityos.org> | 2021-04-29 08:51:31 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-29 10:34:02 +0200 |
commit | d3e7529297aaadead74592119093bc58a72afc15 (patch) | |
tree | 5bc613990c8162805f31acd30c430dc858dd0357 /Userland/Libraries/LibWeb | |
parent | 2f1ee91b3e70cdec3c8c050b0f07f5748151f6ee (diff) | |
download | serenity-d3e7529297aaadead74592119093bc58a72afc15.zip |
LibWeb: Move Layout::TextNode whitespace collapse to separate function
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/TextNode.cpp | 61 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Layout/TextNode.h | 2 |
2 files changed, 35 insertions, 28 deletions
diff --git a/Userland/Libraries/LibWeb/Layout/TextNode.cpp b/Userland/Libraries/LibWeb/Layout/TextNode.cpp index adab30210a..7cee023604 100644 --- a/Userland/Libraries/LibWeb/Layout/TextNode.cpp +++ b/Userland/Libraries/LibWeb/Layout/TextNode.cpp @@ -102,6 +102,38 @@ void TextNode::paint_cursor_if_needed(PaintContext& context, const LineBoxFragme context.painter().draw_rect(cursor_rect, computed_values().color()); } +void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace) +{ + if (!collapse) { + m_text_for_rendering = dom_node().data(); + return; + } + + // Collapse whitespace into single spaces + auto utf8_view = Utf8View(dom_node().data()); + StringBuilder builder(dom_node().data().length()); + auto it = utf8_view.begin(); + auto skip_over_whitespace = [&] { + auto prev = it; + while (it != utf8_view.end() && isspace(*it)) { + prev = it; + ++it; + } + it = prev; + }; + if (previous_is_empty_or_ends_in_whitespace) + skip_over_whitespace(); + for (; it != utf8_view.end(); ++it) { + if (!isspace(*it)) { + builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.code_point_length_in_bytes()); + } else { + builder.append(' '); + skip_over_whitespace(); + } + } + m_text_for_rendering = builder.to_string(); +} + 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(); @@ -112,34 +144,7 @@ void TextNode::split_into_lines_by_rules(InlineFormattingContext& context, Layou 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 - if (do_collapse) { - auto utf8_view = Utf8View(dom_node().data()); - StringBuilder builder(dom_node().data().length()); - auto it = utf8_view.begin(); - auto skip_over_whitespace = [&] { - auto prev = it; - while (it != utf8_view.end() && isspace(*it)) { - prev = it; - ++it; - } - it = prev; - }; - if (line_boxes.last().is_empty_or_ends_in_whitespace()) - skip_over_whitespace(); - for (; it != utf8_view.end(); ++it) { - if (!isspace(*it)) { - builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.code_point_length_in_bytes()); - } else { - builder.append(' '); - skip_over_whitespace(); - } - } - m_text_for_rendering = builder.to_string(); - } else { - m_text_for_rendering = dom_node().data(); - } - + 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); for (;;) { diff --git a/Userland/Libraries/LibWeb/Layout/TextNode.h b/Userland/Libraries/LibWeb/Layout/TextNode.h index 5597f42df0..82c9570a76 100644 --- a/Userland/Libraries/LibWeb/Layout/TextNode.h +++ b/Userland/Libraries/LibWeb/Layout/TextNode.h @@ -53,6 +53,8 @@ public: Utf8View::Iterator m_iterator; }; + void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace); + private: virtual bool is_text_node() const final { return true; } virtual bool wants_mouse_events() const override; |