TreeBuilder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <AK/TemporaryChange.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/ParentNode.h>
  11. #include <LibWeb/DOM/ShadowRoot.h>
  12. #include <LibWeb/Dump.h>
  13. #include <LibWeb/Layout/InitialContainingBlock.h>
  14. #include <LibWeb/Layout/Node.h>
  15. #include <LibWeb/Layout/TableBox.h>
  16. #include <LibWeb/Layout/TableCellBox.h>
  17. #include <LibWeb/Layout/TableRowBox.h>
  18. #include <LibWeb/Layout/TextNode.h>
  19. #include <LibWeb/Layout/TreeBuilder.h>
  20. namespace Web::Layout {
  21. TreeBuilder::TreeBuilder()
  22. {
  23. }
  24. // The insertion_parent_for_*() functions maintain the invariant that block-level boxes must have either
  25. // only block-level children or only inline-level children.
  26. static Layout::Node& insertion_parent_for_inline_node(Layout::NodeWithStyle& layout_parent)
  27. {
  28. if (layout_parent.is_inline() && !layout_parent.is_inline_block())
  29. return layout_parent;
  30. if (layout_parent.computed_values().display().is_flex_inside()) {
  31. layout_parent.append_child(layout_parent.create_anonymous_wrapper());
  32. }
  33. if (!layout_parent.has_children() || layout_parent.children_are_inline())
  34. return layout_parent;
  35. // Parent has block-level children, insert into an anonymous wrapper block (and create it first if needed)
  36. if (!layout_parent.last_child()->is_anonymous() || !layout_parent.last_child()->children_are_inline()) {
  37. layout_parent.append_child(layout_parent.create_anonymous_wrapper());
  38. }
  39. return *layout_parent.last_child();
  40. }
  41. static Layout::Node& insertion_parent_for_block_node(Layout::Node& layout_parent, Layout::Node& layout_node)
  42. {
  43. if (!layout_parent.has_children()) {
  44. // Parent block has no children, insert this block into parent.
  45. return layout_parent;
  46. }
  47. if (!layout_parent.children_are_inline()) {
  48. // Parent block has block-level children, insert this block into parent.
  49. return layout_parent;
  50. }
  51. // Parent block has inline-level children (our siblings).
  52. // First move these siblings into an anonymous wrapper block.
  53. NonnullRefPtrVector<Layout::Node> children;
  54. while (RefPtr<Layout::Node> child = layout_parent.first_child()) {
  55. layout_parent.remove_child(*child);
  56. children.append(child.release_nonnull());
  57. }
  58. layout_parent.append_child(adopt_ref(*new BlockContainer(layout_node.document(), nullptr, layout_parent.computed_values().clone_inherited_values())));
  59. layout_parent.set_children_are_inline(false);
  60. for (auto& child : children) {
  61. layout_parent.last_child()->append_child(child);
  62. }
  63. layout_parent.last_child()->set_children_are_inline(true);
  64. // Then it's safe to insert this block into parent.
  65. return layout_parent;
  66. }
  67. void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context& context)
  68. {
  69. // If the parent doesn't have a layout node, we don't need one either.
  70. if (dom_node.parent_or_shadow_host() && !dom_node.parent_or_shadow_host()->layout_node())
  71. return;
  72. Optional<TemporaryChange<bool>> has_svg_root_change;
  73. if (dom_node.is_svg_container()) {
  74. has_svg_root_change.emplace(context.has_svg_root, true);
  75. } else if (dom_node.requires_svg_container() && !context.has_svg_root) {
  76. return;
  77. }
  78. auto layout_node = dom_node.create_layout_node();
  79. if (!layout_node)
  80. return;
  81. if (!dom_node.parent_or_shadow_host()) {
  82. m_layout_root = layout_node;
  83. } else {
  84. if (layout_node->is_inline() && !(layout_node->is_inline_block() && m_parent_stack.last()->computed_values().display().is_flex_inside())) {
  85. // Inlines can be inserted into the nearest ancestor.
  86. auto& insertion_point = insertion_parent_for_inline_node(*m_parent_stack.last());
  87. insertion_point.append_child(*layout_node);
  88. insertion_point.set_children_are_inline(true);
  89. } else {
  90. // Non-inlines can't be inserted into an inline parent, so find the nearest non-inline ancestor.
  91. auto& nearest_non_inline_ancestor = [&]() -> Layout::Node& {
  92. for (ssize_t i = m_parent_stack.size() - 1; i >= 0; --i) {
  93. if (!m_parent_stack[i]->is_inline() || m_parent_stack[i]->is_inline_block())
  94. return *m_parent_stack[i];
  95. }
  96. VERIFY_NOT_REACHED();
  97. }();
  98. auto& insertion_point = insertion_parent_for_block_node(nearest_non_inline_ancestor, *layout_node);
  99. insertion_point.append_child(*layout_node);
  100. insertion_point.set_children_are_inline(false);
  101. }
  102. }
  103. auto* shadow_root = is<DOM::Element>(dom_node) ? verify_cast<DOM::Element>(dom_node).shadow_root() : nullptr;
  104. if ((dom_node.has_children() || shadow_root) && layout_node->can_have_children()) {
  105. push_parent(verify_cast<NodeWithStyle>(*layout_node));
  106. if (shadow_root)
  107. create_layout_tree(*shadow_root, context);
  108. verify_cast<DOM::ParentNode>(dom_node).for_each_child([&](auto& dom_child) {
  109. create_layout_tree(dom_child, context);
  110. });
  111. pop_parent();
  112. }
  113. }
  114. RefPtr<Node> TreeBuilder::build(DOM::Node& dom_node)
  115. {
  116. if (dom_node.parent()) {
  117. // We're building a partial layout tree, so start by building up the stack of parent layout nodes.
  118. for (auto* ancestor = dom_node.parent()->layout_node(); ancestor; ancestor = ancestor->parent())
  119. m_parent_stack.prepend(verify_cast<NodeWithStyle>(ancestor));
  120. }
  121. Context context;
  122. create_layout_tree(dom_node, context);
  123. if (auto* root = dom_node.document().layout_node())
  124. fixup_tables(*root);
  125. return move(m_layout_root);
  126. }
  127. template<CSS::Display::Internal internal, typename Callback>
  128. void TreeBuilder::for_each_in_tree_with_internal_display(NodeWithStyle& root, Callback callback)
  129. {
  130. root.for_each_in_inclusive_subtree_of_type<Box>([&](auto& box) {
  131. auto const& display = box.computed_values().display();
  132. if (display.is_internal() && display.internal() == internal)
  133. callback(box);
  134. return IterationDecision::Continue;
  135. });
  136. }
  137. template<CSS::Display::Inside inside, typename Callback>
  138. void TreeBuilder::for_each_in_tree_with_inside_display(NodeWithStyle& root, Callback callback)
  139. {
  140. root.for_each_in_inclusive_subtree_of_type<Box>([&](auto& box) {
  141. auto const& display = box.computed_values().display();
  142. if (display.it_outside_and_inside() && display.inside() == inside)
  143. callback(box);
  144. return IterationDecision::Continue;
  145. });
  146. }
  147. void TreeBuilder::fixup_tables(NodeWithStyle& root)
  148. {
  149. // NOTE: Even if we only do a partial build, we always do fixup from the root.
  150. remove_irrelevant_boxes(root);
  151. generate_missing_child_wrappers(root);
  152. generate_missing_parents(root);
  153. }
  154. void TreeBuilder::remove_irrelevant_boxes(NodeWithStyle& root)
  155. {
  156. // The following boxes are discarded as if they were display:none:
  157. NonnullRefPtrVector<Node> to_remove;
  158. // Children of a table-column.
  159. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableColumn>(root, [&](Box& table_column) {
  160. table_column.for_each_child([&](auto& child) {
  161. to_remove.append(child);
  162. });
  163. });
  164. // Children of a table-column-group which are not a table-column.
  165. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableColumnGroup>(root, [&](Box& table_column_group) {
  166. table_column_group.for_each_child([&](auto& child) {
  167. if (child.computed_values().display().is_table_column())
  168. to_remove.append(child);
  169. });
  170. });
  171. // FIXME:
  172. // Anonymous inline boxes which contain only white space and are between two immediate siblings each of which is a table-non-root box.
  173. // Anonymous inline boxes which meet all of the following criteria:
  174. // - they contain only white space
  175. // - they are the first and/or last child of a tabular container
  176. // - whose immediate sibling, if any, is a table-non-root box
  177. for (auto& box : to_remove)
  178. box.parent()->remove_child(box);
  179. }
  180. static bool is_table_track(CSS::Display display)
  181. {
  182. return display.is_table_row() || display.is_table_column();
  183. }
  184. static bool is_table_track_group(CSS::Display display)
  185. {
  186. // Unless explicitly mentioned otherwise, mentions of table-row-groups in this spec also encompass the specialized
  187. // table-header-groups and table-footer-groups.
  188. return display.is_table_row_group()
  189. || display.is_table_header_group()
  190. || display.is_table_footer_group()
  191. || display.is_table_column_group();
  192. }
  193. static bool is_not_proper_table_child(const Node& node)
  194. {
  195. if (!node.has_style())
  196. return true;
  197. auto display = node.computed_values().display();
  198. return !is_table_track_group(display) && !is_table_track(display) && !display.is_table_caption();
  199. }
  200. static bool is_not_table_row(const Node& node)
  201. {
  202. if (!node.has_style())
  203. return true;
  204. auto display = node.computed_values().display();
  205. return !display.is_table_row();
  206. }
  207. static bool is_not_table_cell(const Node& node)
  208. {
  209. if (!node.has_style())
  210. return true;
  211. auto display = node.computed_values().display();
  212. return !display.is_table_cell();
  213. }
  214. template<typename Matcher, typename Callback>
  215. static void for_each_sequence_of_consecutive_children_matching(NodeWithStyle& parent, Matcher matcher, Callback callback)
  216. {
  217. NonnullRefPtrVector<Node> sequence;
  218. Node* next_sibling = nullptr;
  219. for (auto* child = parent.first_child(); child; child = next_sibling) {
  220. next_sibling = child->next_sibling();
  221. if (matcher(*child)) {
  222. sequence.append(*child);
  223. } else {
  224. if (!sequence.is_empty()) {
  225. callback(sequence, next_sibling);
  226. sequence.clear();
  227. }
  228. }
  229. }
  230. if (!sequence.is_empty())
  231. callback(sequence, nullptr);
  232. }
  233. template<typename WrapperBoxType>
  234. static void wrap_in_anonymous(NonnullRefPtrVector<Node>& sequence, Node* nearest_sibling)
  235. {
  236. VERIFY(!sequence.is_empty());
  237. auto& parent = *sequence.first().parent();
  238. auto computed_values = parent.computed_values().clone_inherited_values();
  239. static_cast<CSS::MutableComputedValues&>(computed_values).set_display(WrapperBoxType::static_display());
  240. auto wrapper = adopt_ref(*new WrapperBoxType(parent.document(), nullptr, move(computed_values)));
  241. for (auto& child : sequence) {
  242. parent.remove_child(child);
  243. wrapper->append_child(child);
  244. }
  245. if (nearest_sibling)
  246. parent.insert_before(move(wrapper), *nearest_sibling);
  247. else
  248. parent.append_child(move(wrapper));
  249. }
  250. void TreeBuilder::generate_missing_child_wrappers(NodeWithStyle& root)
  251. {
  252. // An anonymous table-row box must be generated around each sequence of consecutive children of a table-root box which are not proper table child boxes.
  253. for_each_in_tree_with_inside_display<CSS::Display::Inside::Table>(root, [&](auto& parent) {
  254. for_each_sequence_of_consecutive_children_matching(parent, is_not_proper_table_child, [&](auto sequence, auto nearest_sibling) {
  255. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  256. });
  257. });
  258. // An anonymous table-row box must be generated around each sequence of consecutive children of a table-row-group box which are not table-row boxes.
  259. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableRowGroup>(root, [&](auto& parent) {
  260. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  261. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  262. });
  263. });
  264. // Unless explicitly mentioned otherwise, mentions of table-row-groups in this spec also encompass the specialized
  265. // table-header-groups and table-footer-groups.
  266. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableHeaderGroup>(root, [&](auto& parent) {
  267. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  268. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  269. });
  270. });
  271. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableFooterGroup>(root, [&](auto& parent) {
  272. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  273. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  274. });
  275. });
  276. // An anonymous table-cell box must be generated around each sequence of consecutive children of a table-row box which are not table-cell boxes. !Testcase
  277. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableRow>(root, [&](auto& parent) {
  278. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_cell, [&](auto& sequence, auto nearest_sibling) {
  279. wrap_in_anonymous<TableCellBox>(sequence, nearest_sibling);
  280. });
  281. });
  282. }
  283. void TreeBuilder::generate_missing_parents(NodeWithStyle&)
  284. {
  285. // FIXME: Implement.
  286. }
  287. }