TreeBuilder.cpp 12 KB

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