TextNode.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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)
  109. : m_wrap_lines(wrap_lines)
  110. , m_respect_linebreaks(respect_linebreaks)
  111. , m_utf8_view(text)
  112. , m_iterator(m_utf8_view.begin())
  113. {
  114. }
  115. Optional<TextNode::Chunk> TextNode::ChunkIterator::next()
  116. {
  117. if (m_iterator == m_utf8_view.end())
  118. return {};
  119. auto start_of_chunk = m_iterator;
  120. while (m_iterator != m_utf8_view.end()) {
  121. if (m_respect_linebreaks && *m_iterator == '\n') {
  122. // Newline encountered, and we're supposed to preserve them.
  123. // If we have accumulated some code points in the current chunk, commit them now and continue with the newline next time.
  124. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  125. return result.release_value();
  126. // Otherwise, commit the newline!
  127. ++m_iterator;
  128. auto result = try_commit_chunk(start_of_chunk, m_iterator, true);
  129. VERIFY(result.has_value());
  130. return result.release_value();
  131. }
  132. if (m_wrap_lines) {
  133. if (is_ascii_space(*m_iterator)) {
  134. // Whitespace encountered, and we're allowed to break on whitespace.
  135. // If we have accumulated some code points in the current chunk, commit them now and continue with the whitespace next time.
  136. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  137. return result.release_value();
  138. // Otherwise, commit the whitespace!
  139. ++m_iterator;
  140. if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value())
  141. return result.release_value();
  142. continue;
  143. }
  144. }
  145. ++m_iterator;
  146. }
  147. if (start_of_chunk != m_utf8_view.end()) {
  148. // Try to output whatever's left at the end of the text node.
  149. if (auto result = try_commit_chunk(start_of_chunk, m_utf8_view.end(), false); result.has_value())
  150. return result.release_value();
  151. }
  152. return {};
  153. }
  154. Optional<TextNode::Chunk> TextNode::ChunkIterator::try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline) const
  155. {
  156. auto byte_offset = m_utf8_view.byte_offset_of(start);
  157. auto byte_length = m_utf8_view.byte_offset_of(end) - byte_offset;
  158. if (byte_length > 0) {
  159. auto chunk_view = m_utf8_view.substring_view(byte_offset, byte_length);
  160. return Chunk {
  161. .view = chunk_view,
  162. .start = byte_offset,
  163. .length = byte_length,
  164. .has_breaking_newline = has_breaking_newline,
  165. .is_all_whitespace = is_all_whitespace(chunk_view.as_string()),
  166. };
  167. }
  168. return {};
  169. }
  170. JS::GCPtr<Painting::Paintable> TextNode::create_paintable() const
  171. {
  172. return Painting::TextPaintable::create(*this);
  173. }
  174. }