TextNode.cpp 5.1 KB

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