TextNode.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <LibUnicode/CharacterTypes.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/TextNode.h>
  14. #include <LibWeb/Painting/TextPaintable.h>
  15. namespace Web::Layout {
  16. TextNode::TextNode(DOM::Document& document, DOM::Text& text)
  17. : Node(document, &text)
  18. {
  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. static ErrorOr<DeprecatedString> apply_text_transform(DeprecatedString const& string, CSS::TextTransform text_transform)
  30. {
  31. if (text_transform == CSS::TextTransform::Uppercase)
  32. return Unicode::to_unicode_uppercase_full(string);
  33. if (text_transform == CSS::TextTransform::Lowercase)
  34. return Unicode::to_unicode_lowercase_full(string);
  35. return string;
  36. }
  37. DeprecatedString const& TextNode::text_for_rendering() const
  38. {
  39. if (m_text_for_rendering.is_null())
  40. const_cast<TextNode*>(this)->compute_text_for_rendering();
  41. return m_text_for_rendering;
  42. }
  43. // NOTE: This collapses whitespace into a single ASCII space if the CSS white-space property tells us to.
  44. void TextNode::compute_text_for_rendering()
  45. {
  46. bool collapse = [](CSS::WhiteSpace white_space) {
  47. switch (white_space) {
  48. case CSS::WhiteSpace::Normal:
  49. case CSS::WhiteSpace::Nowrap:
  50. case CSS::WhiteSpace::PreLine:
  51. return true;
  52. case CSS::WhiteSpace::Pre:
  53. case CSS::WhiteSpace::PreWrap:
  54. return false;
  55. }
  56. VERIFY_NOT_REACHED();
  57. }(computed_values().white_space());
  58. if (dom_node().is_editable() && !dom_node().is_uninteresting_whitespace_node())
  59. collapse = false;
  60. auto data = apply_text_transform(dom_node().data(), computed_values().text_transform()).release_value_but_fixme_should_propagate_errors();
  61. if (dom_node().is_password_input()) {
  62. m_text_for_rendering = DeprecatedString::repeated('*', data.length());
  63. return;
  64. }
  65. if (!collapse || data.is_empty()) {
  66. m_text_for_rendering = data;
  67. return;
  68. }
  69. // NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
  70. if (data.length() == 1) {
  71. if (is_ascii_space(data[0])) {
  72. static DeprecatedString s_single_space_string = " ";
  73. m_text_for_rendering = s_single_space_string;
  74. } else {
  75. m_text_for_rendering = data;
  76. }
  77. return;
  78. }
  79. bool contains_space = false;
  80. for (auto& c : data) {
  81. if (is_ascii_space(c)) {
  82. contains_space = true;
  83. break;
  84. }
  85. }
  86. if (!contains_space) {
  87. m_text_for_rendering = data;
  88. return;
  89. }
  90. StringBuilder builder(data.length());
  91. size_t index = 0;
  92. auto skip_over_whitespace = [&index, &data] {
  93. while (index < data.length() && is_ascii_space(data[index]))
  94. ++index;
  95. };
  96. while (index < data.length()) {
  97. if (is_ascii_space(data[index])) {
  98. builder.append(' ');
  99. ++index;
  100. skip_over_whitespace();
  101. } else {
  102. builder.append(data[index]);
  103. ++index;
  104. }
  105. }
  106. m_text_for_rendering = builder.to_deprecated_string();
  107. }
  108. TextNode::ChunkIterator::ChunkIterator(StringView text, bool wrap_lines, bool respect_linebreaks, bool is_generated_empty_string)
  109. : m_wrap_lines(wrap_lines)
  110. , m_respect_linebreaks(respect_linebreaks)
  111. , m_should_emit_one_empty_chunk(is_generated_empty_string)
  112. , m_utf8_view(text)
  113. , m_iterator(m_utf8_view.begin())
  114. {
  115. }
  116. Optional<TextNode::Chunk> TextNode::ChunkIterator::next()
  117. {
  118. if (m_should_emit_one_empty_chunk) {
  119. m_should_emit_one_empty_chunk = false;
  120. return Chunk {
  121. .view = {},
  122. .start = 0,
  123. .length = 0,
  124. .has_breaking_newline = false,
  125. .is_all_whitespace = false,
  126. };
  127. }
  128. if (m_iterator == m_utf8_view.end())
  129. return {};
  130. auto start_of_chunk = m_iterator;
  131. while (m_iterator != m_utf8_view.end()) {
  132. if (m_respect_linebreaks && *m_iterator == '\n') {
  133. // Newline encountered, and we're supposed to preserve them.
  134. // If we have accumulated some code points in the current chunk, commit them now and continue with the newline next time.
  135. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  136. return result.release_value();
  137. // Otherwise, commit the newline!
  138. ++m_iterator;
  139. auto result = try_commit_chunk(start_of_chunk, m_iterator, true);
  140. VERIFY(result.has_value());
  141. return result.release_value();
  142. }
  143. if (m_wrap_lines) {
  144. if (is_ascii_space(*m_iterator)) {
  145. // Whitespace encountered, and we're allowed to break on whitespace.
  146. // If we have accumulated some code points in the current chunk, commit them now and continue with the whitespace next time.
  147. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  148. return result.release_value();
  149. // Otherwise, commit the whitespace!
  150. ++m_iterator;
  151. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  152. return result.release_value();
  153. continue;
  154. }
  155. }
  156. ++m_iterator;
  157. }
  158. if (start_of_chunk != m_utf8_view.end()) {
  159. // Try to output whatever's left at the end of the text node.
  160. if (auto result = try_commit_chunk(start_of_chunk, m_utf8_view.end(), false); result.has_value())
  161. return result.release_value();
  162. }
  163. return {};
  164. }
  165. Optional<TextNode::Chunk> TextNode::ChunkIterator::try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline) const
  166. {
  167. auto byte_offset = m_utf8_view.byte_offset_of(start);
  168. auto byte_length = m_utf8_view.byte_offset_of(end) - byte_offset;
  169. if (byte_length > 0) {
  170. auto chunk_view = m_utf8_view.substring_view(byte_offset, byte_length);
  171. return Chunk {
  172. .view = chunk_view,
  173. .start = byte_offset,
  174. .length = byte_length,
  175. .has_breaking_newline = has_breaking_newline,
  176. .is_all_whitespace = is_all_whitespace(chunk_view.as_string()),
  177. };
  178. }
  179. return {};
  180. }
  181. JS::GCPtr<Painting::Paintable> TextNode::create_paintable() const
  182. {
  183. return Painting::TextPaintable::create(*this);
  184. }
  185. }