TextNode.cpp 6.8 KB

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