TreeBuilder.cpp 16 KB

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