TextNode.cpp 5.6 KB

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