TreeBuilder.cpp 12 KB

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