InlineLevelIterator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BreakNode.h>
  7. #include <LibWeb/Layout/InlineFormattingContext.h>
  8. #include <LibWeb/Layout/InlineLevelIterator.h>
  9. #include <LibWeb/Layout/InlineNode.h>
  10. #include <LibWeb/Layout/ListItemMarkerBox.h>
  11. #include <LibWeb/Layout/ReplacedBox.h>
  12. namespace Web::Layout {
  13. InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline_formatting_context, Layout::LayoutState& layout_state, Layout::BlockContainer const& container, LayoutMode layout_mode)
  14. : m_inline_formatting_context(inline_formatting_context)
  15. , m_layout_state(layout_state)
  16. , m_container(container)
  17. , m_container_state(layout_state.get(container))
  18. , m_next_node(container.first_child())
  19. , m_layout_mode(layout_mode)
  20. {
  21. skip_to_next();
  22. }
  23. void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const& node)
  24. {
  25. if (!m_extra_leading_metrics.has_value())
  26. m_extra_leading_metrics = ExtraBoxMetrics {};
  27. // FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
  28. auto& used_values = m_layout_state.get_mutable(node);
  29. auto const& computed_values = node.computed_values();
  30. used_values.margin_left = computed_values.margin().left().resolved(node, CSS::Length::make_px(m_container_state.content_width())).to_px(node);
  31. used_values.border_left = computed_values.border_left().width;
  32. used_values.padding_left = computed_values.padding().left().resolved(node, CSS::Length::make_px(m_container_state.content_width())).to_px(node);
  33. m_extra_leading_metrics->margin += used_values.margin_left;
  34. m_extra_leading_metrics->border += used_values.border_left;
  35. m_extra_leading_metrics->padding += used_values.padding_left;
  36. m_box_model_node_stack.append(node);
  37. }
  38. void InlineLevelIterator::exit_node_with_box_model_metrics()
  39. {
  40. if (!m_extra_trailing_metrics.has_value())
  41. m_extra_trailing_metrics = ExtraBoxMetrics {};
  42. auto& node = m_box_model_node_stack.last();
  43. auto& used_values = m_layout_state.get_mutable(node);
  44. auto const& computed_values = node->computed_values();
  45. used_values.margin_right = computed_values.margin().right().resolved(node, CSS::Length::make_px(m_container_state.content_width())).to_px(node);
  46. used_values.border_right = computed_values.border_right().width;
  47. used_values.padding_right = computed_values.padding().right().resolved(node, CSS::Length::make_px(m_container_state.content_width())).to_px(node);
  48. m_extra_trailing_metrics->margin += used_values.margin_right;
  49. m_extra_trailing_metrics->border += used_values.border_right;
  50. m_extra_trailing_metrics->padding += used_values.padding_right;
  51. m_box_model_node_stack.take_last();
  52. }
  53. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  54. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  55. {
  56. if (current.first_child()
  57. && current.first_child()->display().is_inline_outside()
  58. && current.display().is_flow_inside()
  59. && !current.is_replaced_box()) {
  60. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  61. return current.first_child();
  62. }
  63. Layout::Node const* node = &current;
  64. Layout::Node const* next = nullptr;
  65. while (!(next = node->next_sibling())) {
  66. node = node->parent();
  67. // If node is the last node on the "box model node stack", pop it off.
  68. if (!m_box_model_node_stack.is_empty()
  69. && m_box_model_node_stack.last() == node) {
  70. exit_node_with_box_model_metrics();
  71. }
  72. if (!node || node == stay_within)
  73. return nullptr;
  74. }
  75. // If node is the last node on the "box model node stack", pop it off.
  76. if (!m_box_model_node_stack.is_empty()
  77. && m_box_model_node_stack.last() == node) {
  78. exit_node_with_box_model_metrics();
  79. }
  80. return next;
  81. }
  82. void InlineLevelIterator::compute_next()
  83. {
  84. if (m_next_node == nullptr)
  85. return;
  86. do {
  87. m_next_node = next_inline_node_in_pre_order(*m_next_node, m_container);
  88. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  89. }
  90. void InlineLevelIterator::skip_to_next()
  91. {
  92. if (m_next_node
  93. && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node)
  94. && m_next_node->display().is_flow_inside()
  95. && !m_next_node->is_out_of_flow(m_inline_formatting_context)
  96. && !m_next_node->is_replaced_box())
  97. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  98. m_current_node = m_next_node;
  99. compute_next();
  100. }
  101. Optional<InlineLevelIterator::Item> InlineLevelIterator::next(CSSPixels available_width)
  102. {
  103. if (!m_current_node)
  104. return {};
  105. if (is<Layout::TextNode>(*m_current_node)) {
  106. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  107. if (!m_text_node_context.has_value())
  108. enter_text_node(text_node);
  109. auto chunk_opt = m_text_node_context->next_chunk;
  110. if (!chunk_opt.has_value()) {
  111. m_text_node_context = {};
  112. skip_to_next();
  113. return next(available_width);
  114. }
  115. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  116. if (!m_text_node_context->next_chunk.has_value())
  117. m_text_node_context->is_last_chunk = true;
  118. auto& chunk = chunk_opt.value();
  119. CSSPixels chunk_width = text_node.font().width(chunk.view) + text_node.font().glyph_spacing();
  120. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  121. return Item {
  122. .type = Item::Type::ForcedBreak,
  123. };
  124. }
  125. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  126. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  127. Item item {
  128. .type = Item::Type::Text,
  129. .node = &text_node,
  130. .offset_in_node = chunk.start,
  131. .length_in_node = chunk.length,
  132. .width = chunk_width,
  133. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  134. };
  135. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  136. return item;
  137. }
  138. if (m_current_node->is_absolutely_positioned()) {
  139. auto& node = *m_current_node;
  140. skip_to_next();
  141. return Item {
  142. .type = Item::Type::AbsolutelyPositionedElement,
  143. .node = &node,
  144. };
  145. }
  146. if (m_current_node->is_floating()) {
  147. auto& node = *m_current_node;
  148. skip_to_next();
  149. return Item {
  150. .type = Item::Type::FloatingElement,
  151. .node = &node,
  152. };
  153. }
  154. if (is<Layout::BreakNode>(*m_current_node)) {
  155. skip_to_next();
  156. return Item {
  157. .type = Item::Type::ForcedBreak,
  158. };
  159. }
  160. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  161. skip_to_next();
  162. return next(available_width);
  163. }
  164. if (!is<Layout::Box>(*m_current_node)) {
  165. skip_to_next();
  166. return next(available_width);
  167. }
  168. if (is<Layout::ReplacedBox>(*m_current_node)) {
  169. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  170. // FIXME: This const_cast is gross.
  171. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  172. }
  173. auto& box = verify_cast<Layout::Box>(*m_current_node);
  174. auto& box_state = m_layout_state.get(box);
  175. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  176. skip_to_next();
  177. auto item = Item {
  178. .type = Item::Type::Element,
  179. .node = &box,
  180. .offset_in_node = 0,
  181. .length_in_node = 0,
  182. .width = box_state.content_width(),
  183. .padding_start = box_state.padding_left,
  184. .padding_end = box_state.padding_right,
  185. .border_start = box_state.border_left,
  186. .border_end = box_state.border_right,
  187. .margin_start = box_state.margin_left,
  188. .margin_end = box_state.margin_right,
  189. };
  190. add_extra_box_model_metrics_to_item(item, true, true);
  191. return item;
  192. }
  193. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  194. {
  195. bool do_collapse = true;
  196. bool do_wrap_lines = true;
  197. bool do_respect_linebreaks = false;
  198. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  199. do_collapse = true;
  200. do_wrap_lines = false;
  201. do_respect_linebreaks = false;
  202. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  203. do_collapse = false;
  204. do_wrap_lines = false;
  205. do_respect_linebreaks = true;
  206. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  207. do_collapse = true;
  208. do_wrap_lines = true;
  209. do_respect_linebreaks = true;
  210. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  211. do_collapse = false;
  212. do_wrap_lines = true;
  213. do_respect_linebreaks = true;
  214. }
  215. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  216. do_collapse = false;
  217. // FIXME: The const_cast here is gross.
  218. const_cast<TextNode&>(text_node).compute_text_for_rendering(do_collapse);
  219. m_text_node_context = TextNodeContext {
  220. .do_collapse = do_collapse,
  221. .do_wrap_lines = do_wrap_lines,
  222. .do_respect_linebreaks = do_respect_linebreaks,
  223. .is_first_chunk = true,
  224. .is_last_chunk = false,
  225. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), do_wrap_lines, do_respect_linebreaks, text_node.is_generated() && text_node.text_for_rendering().is_empty() },
  226. };
  227. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  228. }
  229. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  230. {
  231. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  232. item.margin_start += m_extra_leading_metrics->margin;
  233. item.border_start += m_extra_leading_metrics->border;
  234. item.padding_start += m_extra_leading_metrics->padding;
  235. m_extra_leading_metrics = {};
  236. }
  237. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  238. item.margin_end += m_extra_trailing_metrics->margin;
  239. item.border_end += m_extra_trailing_metrics->border;
  240. item.padding_end += m_extra_trailing_metrics->padding;
  241. m_extra_trailing_metrics = {};
  242. }
  243. }
  244. }