InlineLevelIterator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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().to_px(node, m_container_state.content_width());
  31. used_values.border_left = computed_values.border_left().width;
  32. used_values.padding_left = computed_values.padding().left().to_px(node, m_container_state.content_width());
  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. // Now's our chance to resolve the inset properties for this node.
  37. m_inline_formatting_context.compute_inset(node);
  38. m_box_model_node_stack.append(node);
  39. }
  40. void InlineLevelIterator::exit_node_with_box_model_metrics()
  41. {
  42. if (!m_extra_trailing_metrics.has_value())
  43. m_extra_trailing_metrics = ExtraBoxMetrics {};
  44. auto& node = m_box_model_node_stack.last();
  45. auto& used_values = m_layout_state.get_mutable(node);
  46. auto const& computed_values = node->computed_values();
  47. used_values.margin_right = computed_values.margin().right().to_px(node, m_container_state.content_width());
  48. used_values.border_right = computed_values.border_right().width;
  49. used_values.padding_right = computed_values.padding().right().to_px(node, m_container_state.content_width());
  50. m_extra_trailing_metrics->margin += used_values.margin_right;
  51. m_extra_trailing_metrics->border += used_values.border_right;
  52. m_extra_trailing_metrics->padding += used_values.padding_right;
  53. m_box_model_node_stack.take_last();
  54. }
  55. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  56. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  57. {
  58. if (current.first_child()
  59. && current.first_child()->display().is_inline_outside()
  60. && current.display().is_flow_inside()
  61. && !current.is_replaced_box()) {
  62. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  63. return current.first_child();
  64. }
  65. Layout::Node const* node = &current;
  66. Layout::Node const* next = nullptr;
  67. while (!(next = node->next_sibling())) {
  68. node = node->parent();
  69. // If node is the last node on the "box model node stack", pop it off.
  70. if (!m_box_model_node_stack.is_empty()
  71. && m_box_model_node_stack.last() == node) {
  72. exit_node_with_box_model_metrics();
  73. }
  74. if (!node || node == stay_within)
  75. return nullptr;
  76. }
  77. // If node is the last node on the "box model node stack", pop it off.
  78. if (!m_box_model_node_stack.is_empty()
  79. && m_box_model_node_stack.last() == node) {
  80. exit_node_with_box_model_metrics();
  81. }
  82. return next;
  83. }
  84. void InlineLevelIterator::compute_next()
  85. {
  86. if (m_next_node == nullptr)
  87. return;
  88. do {
  89. m_next_node = next_inline_node_in_pre_order(*m_next_node, m_container);
  90. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  91. }
  92. void InlineLevelIterator::skip_to_next()
  93. {
  94. if (m_next_node
  95. && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node)
  96. && m_next_node->display().is_flow_inside()
  97. && !m_next_node->is_out_of_flow(m_inline_formatting_context)
  98. && !m_next_node->is_replaced_box())
  99. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  100. m_current_node = m_next_node;
  101. compute_next();
  102. }
  103. Optional<InlineLevelIterator::Item> InlineLevelIterator::next()
  104. {
  105. if (m_lookahead_items.is_empty())
  106. return next_without_lookahead();
  107. return m_lookahead_items.dequeue();
  108. }
  109. CSSPixels InlineLevelIterator::next_non_whitespace_sequence_width()
  110. {
  111. CSSPixels next_width = 0;
  112. for (;;) {
  113. auto next_item_opt = next_without_lookahead();
  114. if (!next_item_opt.has_value())
  115. break;
  116. m_lookahead_items.enqueue(next_item_opt.release_value());
  117. auto& next_item = m_lookahead_items.tail();
  118. if (next_item.type == InlineLevelIterator::Item::Type::ForcedBreak)
  119. break;
  120. if (next_item.node->computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  121. if (next_item.type != InlineLevelIterator::Item::Type::Text)
  122. break;
  123. if (next_item.is_collapsible_whitespace)
  124. break;
  125. auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
  126. auto next_view = next_text_node.text_for_rendering().substring_view(next_item.offset_in_node, next_item.length_in_node);
  127. if (next_view.is_whitespace())
  128. break;
  129. }
  130. next_width += next_item.border_box_width();
  131. }
  132. return next_width;
  133. }
  134. Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead()
  135. {
  136. if (!m_current_node)
  137. return {};
  138. if (is<Layout::TextNode>(*m_current_node)) {
  139. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  140. if (!m_text_node_context.has_value())
  141. enter_text_node(text_node);
  142. auto chunk_opt = m_text_node_context->next_chunk;
  143. if (!chunk_opt.has_value()) {
  144. m_text_node_context = {};
  145. skip_to_next();
  146. return next_without_lookahead();
  147. }
  148. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  149. if (!m_text_node_context->next_chunk.has_value())
  150. m_text_node_context->is_last_chunk = true;
  151. auto& chunk = chunk_opt.value();
  152. CSSPixels chunk_width = CSSPixels::nearest_value_for(text_node.font().width(chunk.view) + text_node.font().glyph_spacing());
  153. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  154. return Item {
  155. .type = Item::Type::ForcedBreak,
  156. };
  157. }
  158. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  159. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  160. Item item {
  161. .type = Item::Type::Text,
  162. .node = &text_node,
  163. .offset_in_node = chunk.start,
  164. .length_in_node = chunk.length,
  165. .width = chunk_width,
  166. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  167. };
  168. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  169. return item;
  170. }
  171. if (m_current_node->is_absolutely_positioned()) {
  172. auto& node = *m_current_node;
  173. skip_to_next();
  174. return Item {
  175. .type = Item::Type::AbsolutelyPositionedElement,
  176. .node = &node,
  177. };
  178. }
  179. if (m_current_node->is_floating()) {
  180. auto& node = *m_current_node;
  181. skip_to_next();
  182. return Item {
  183. .type = Item::Type::FloatingElement,
  184. .node = &node,
  185. };
  186. }
  187. if (is<Layout::BreakNode>(*m_current_node)) {
  188. auto& node = *m_current_node;
  189. skip_to_next();
  190. return Item {
  191. .type = Item::Type::ForcedBreak,
  192. .node = &node,
  193. };
  194. }
  195. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  196. skip_to_next();
  197. return next_without_lookahead();
  198. }
  199. if (!is<Layout::Box>(*m_current_node)) {
  200. skip_to_next();
  201. return next_without_lookahead();
  202. }
  203. if (is<Layout::ReplacedBox>(*m_current_node)) {
  204. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  205. // FIXME: This const_cast is gross.
  206. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  207. }
  208. auto& box = verify_cast<Layout::Box>(*m_current_node);
  209. auto& box_state = m_layout_state.get(box);
  210. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  211. skip_to_next();
  212. auto item = Item {
  213. .type = Item::Type::Element,
  214. .node = &box,
  215. .offset_in_node = 0,
  216. .length_in_node = 0,
  217. .width = box_state.content_width(),
  218. .padding_start = box_state.padding_left,
  219. .padding_end = box_state.padding_right,
  220. .border_start = box_state.border_left,
  221. .border_end = box_state.border_right,
  222. .margin_start = box_state.margin_left,
  223. .margin_end = box_state.margin_right,
  224. };
  225. add_extra_box_model_metrics_to_item(item, true, true);
  226. return item;
  227. }
  228. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  229. {
  230. bool do_collapse = true;
  231. bool do_wrap_lines = true;
  232. bool do_respect_linebreaks = false;
  233. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  234. do_collapse = true;
  235. do_wrap_lines = false;
  236. do_respect_linebreaks = false;
  237. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  238. do_collapse = false;
  239. do_wrap_lines = false;
  240. do_respect_linebreaks = true;
  241. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  242. do_collapse = true;
  243. do_wrap_lines = true;
  244. do_respect_linebreaks = true;
  245. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  246. do_collapse = false;
  247. do_wrap_lines = true;
  248. do_respect_linebreaks = true;
  249. }
  250. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  251. do_collapse = false;
  252. m_text_node_context = TextNodeContext {
  253. .do_collapse = do_collapse,
  254. .do_wrap_lines = do_wrap_lines,
  255. .do_respect_linebreaks = do_respect_linebreaks,
  256. .is_first_chunk = true,
  257. .is_last_chunk = false,
  258. .chunk_iterator = TextNode::ChunkIterator { text_node.text_for_rendering(), do_wrap_lines, do_respect_linebreaks },
  259. };
  260. m_text_node_context->next_chunk = m_text_node_context->chunk_iterator.next();
  261. }
  262. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  263. {
  264. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  265. item.margin_start += m_extra_leading_metrics->margin;
  266. item.border_start += m_extra_leading_metrics->border;
  267. item.padding_start += m_extra_leading_metrics->padding;
  268. m_extra_leading_metrics = {};
  269. }
  270. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  271. item.margin_end += m_extra_trailing_metrics->margin;
  272. item.border_end += m_extra_trailing_metrics->border;
  273. item.padding_end += m_extra_trailing_metrics->padding;
  274. m_extra_trailing_metrics = {};
  275. }
  276. }
  277. }