TextNode.cpp 6.6 KB

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