TextNode.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/InlineFormattingContext.h>
  12. #include <LibWeb/Layout/TextNode.h>
  13. #include <LibWeb/Painting/TextPaintable.h>
  14. namespace Web::Layout {
  15. TextNode::TextNode(DOM::Document& document, DOM::Text& text)
  16. : Node(document, &text)
  17. {
  18. set_inline(true);
  19. }
  20. TextNode::~TextNode() = default;
  21. static bool is_all_whitespace(StringView string)
  22. {
  23. for (size_t i = 0; i < string.length(); ++i) {
  24. if (!is_ascii_space(string[i]))
  25. return false;
  26. }
  27. return true;
  28. }
  29. // NOTE: This collapes whitespace into a single ASCII space if collapse is true. If previous_is_empty_or_ends_in_whitespace, it also strips leading whitespace.
  30. void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace)
  31. {
  32. auto& data = dom_node().data();
  33. if (!collapse || data.is_empty()) {
  34. m_text_for_rendering = data;
  35. return;
  36. }
  37. // NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
  38. if (data.length() == 1) {
  39. if (is_ascii_space(data[0])) {
  40. if (previous_is_empty_or_ends_in_whitespace)
  41. m_text_for_rendering = String::empty();
  42. else {
  43. static String s_single_space_string = " ";
  44. m_text_for_rendering = s_single_space_string;
  45. }
  46. } else {
  47. m_text_for_rendering = data;
  48. }
  49. return;
  50. }
  51. bool contains_space = false;
  52. for (auto& c : data) {
  53. if (is_ascii_space(c)) {
  54. contains_space = true;
  55. break;
  56. }
  57. }
  58. if (!contains_space) {
  59. m_text_for_rendering = data;
  60. return;
  61. }
  62. StringBuilder builder(data.length());
  63. size_t index = 0;
  64. auto skip_over_whitespace = [&index, &data] {
  65. while (index < data.length() && is_ascii_space(data[index]))
  66. ++index;
  67. };
  68. if (previous_is_empty_or_ends_in_whitespace)
  69. skip_over_whitespace();
  70. while (index < data.length()) {
  71. if (is_ascii_space(data[index])) {
  72. builder.append(' ');
  73. ++index;
  74. skip_over_whitespace();
  75. } else {
  76. builder.append(data[index]);
  77. ++index;
  78. }
  79. }
  80. m_text_for_rendering = builder.to_string();
  81. }
  82. TextNode::ChunkIterator::ChunkIterator(StringView text, LayoutMode layout_mode, bool wrap_lines, bool respect_linebreaks)
  83. : m_layout_mode(layout_mode)
  84. , m_wrap_lines(wrap_lines)
  85. , m_respect_linebreaks(respect_linebreaks)
  86. , m_utf8_view(text)
  87. , m_iterator(m_utf8_view.begin())
  88. {
  89. m_last_was_space = !text.is_empty() && is_ascii_space(*m_utf8_view.begin());
  90. }
  91. Optional<TextNode::Chunk> TextNode::ChunkIterator::next()
  92. {
  93. if (m_iterator == m_utf8_view.end())
  94. return {};
  95. auto start_of_chunk = m_iterator;
  96. while (m_iterator != m_utf8_view.end()) {
  97. ++m_iterator;
  98. if (m_last_was_newline) {
  99. // NOTE: This expression looks out for the case where we have
  100. // multiple newlines in a row. Because every output next()
  101. // that's a newline newline must be prepared for in advance by
  102. // the previous next() call, we need to check whether the next
  103. // character is a newline here as well. Otherwise, the newline
  104. // becomes part of the next expression and causes rendering
  105. // issues.
  106. m_last_was_newline = m_iterator != m_utf8_view.end() && *m_iterator == '\n';
  107. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, true); result.has_value())
  108. return result.release_value();
  109. }
  110. // NOTE: The checks after this need to look at the current iterator
  111. // position, which depends on not being at the end.
  112. if (m_iterator == m_utf8_view.end())
  113. break;
  114. // NOTE: When we're supposed to stop on linebreaks, we're actually
  115. // supposed to output two chunks: "content" and "\n". Since we
  116. // can't output two chunks at once, we store this information as a
  117. // flag to output the newline immediately at the earliest
  118. // opportunity.
  119. if (m_respect_linebreaks && *m_iterator == '\n') {
  120. m_last_was_newline = true;
  121. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value()) {
  122. return result.release_value();
  123. }
  124. }
  125. if (m_wrap_lines || m_layout_mode == LayoutMode::MinContent) {
  126. bool is_space = is_ascii_space(*m_iterator);
  127. if (is_space != m_last_was_space) {
  128. m_last_was_space = is_space;
  129. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value()) {
  130. return result.release_value();
  131. }
  132. }
  133. }
  134. }
  135. if (start_of_chunk != m_utf8_view.end()) {
  136. // Try to output whatever's left at the end of the text node.
  137. if (auto result = try_commit_chunk(start_of_chunk, m_utf8_view.end(), false, true); result.has_value())
  138. return result.release_value();
  139. }
  140. return {};
  141. }
  142. Optional<TextNode::Chunk> TextNode::ChunkIterator::try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline, bool must_commit) const
  143. {
  144. if (m_layout_mode == LayoutMode::MaxContent && !must_commit)
  145. return {};
  146. auto byte_offset = m_utf8_view.byte_offset_of(start);
  147. auto byte_length = m_utf8_view.byte_offset_of(end) - byte_offset;
  148. if (byte_length > 0) {
  149. auto chunk_view = m_utf8_view.substring_view(byte_offset, byte_length);
  150. return Chunk {
  151. .view = chunk_view,
  152. .start = byte_offset,
  153. .length = byte_length,
  154. .has_breaking_newline = has_breaking_newline,
  155. .is_all_whitespace = is_all_whitespace(chunk_view.as_string()),
  156. };
  157. }
  158. return {};
  159. }
  160. RefPtr<Painting::Paintable> TextNode::create_paintable() const
  161. {
  162. return Painting::TextPaintable::create(*this);
  163. }
  164. }