TextNode.cpp 5.3 KB

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