TextNode.cpp 6.1 KB

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