TreeBuilder.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Optional.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Element.h>
  12. #include <LibWeb/DOM/ParentNode.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/Dump.h>
  15. #include <LibWeb/HTML/HTMLInputElement.h>
  16. #include <LibWeb/HTML/HTMLProgressElement.h>
  17. #include <LibWeb/Layout/InitialContainingBlock.h>
  18. #include <LibWeb/Layout/ListItemBox.h>
  19. #include <LibWeb/Layout/ListItemMarkerBox.h>
  20. #include <LibWeb/Layout/Node.h>
  21. #include <LibWeb/Layout/Progress.h>
  22. #include <LibWeb/Layout/TableBox.h>
  23. #include <LibWeb/Layout/TableCellBox.h>
  24. #include <LibWeb/Layout/TableRowBox.h>
  25. #include <LibWeb/Layout/TextNode.h>
  26. #include <LibWeb/Layout/TreeBuilder.h>
  27. #include <LibWeb/SVG/SVGForeignObjectElement.h>
  28. namespace Web::Layout {
  29. TreeBuilder::TreeBuilder() = default;
  30. static bool has_inline_or_in_flow_block_children(Layout::Node const& layout_node)
  31. {
  32. for (auto child = layout_node.first_child(); child; child = child->next_sibling()) {
  33. if (child->is_inline())
  34. return true;
  35. if (!child->is_floating() && !child->is_absolutely_positioned())
  36. return true;
  37. }
  38. return false;
  39. }
  40. static bool has_in_flow_block_children(Layout::Node const& layout_node)
  41. {
  42. if (layout_node.children_are_inline())
  43. return false;
  44. for (auto child = layout_node.first_child(); child; child = child->next_sibling()) {
  45. if (child->is_inline())
  46. continue;
  47. if (!child->is_floating() && !child->is_absolutely_positioned())
  48. return true;
  49. }
  50. return false;
  51. }
  52. // The insertion_parent_for_*() functions maintain the invariant that the in-flow children of
  53. // block-level boxes must be either all block-level or all inline-level.
  54. static Layout::Node& insertion_parent_for_inline_node(Layout::NodeWithStyle& layout_parent)
  55. {
  56. if (layout_parent.display().is_inline_outside() && layout_parent.display().is_flow_inside())
  57. return layout_parent;
  58. if (layout_parent.display().is_flex_inside()) {
  59. layout_parent.append_child(layout_parent.create_anonymous_wrapper());
  60. return *layout_parent.last_child();
  61. }
  62. if (!has_in_flow_block_children(layout_parent) || layout_parent.children_are_inline())
  63. return layout_parent;
  64. // Parent has block-level children, insert into an anonymous wrapper block (and create it first if needed)
  65. if (!layout_parent.last_child()->is_anonymous() || !layout_parent.last_child()->children_are_inline()) {
  66. layout_parent.append_child(layout_parent.create_anonymous_wrapper());
  67. }
  68. return *layout_parent.last_child();
  69. }
  70. static Layout::Node& insertion_parent_for_block_node(Layout::NodeWithStyle& layout_parent, Layout::Node& layout_node)
  71. {
  72. if (!has_inline_or_in_flow_block_children(layout_parent)) {
  73. // Parent block has no children, insert this block into parent.
  74. return layout_parent;
  75. }
  76. if (!layout_parent.children_are_inline()) {
  77. // Parent block has block-level children, insert this block into parent.
  78. return layout_parent;
  79. }
  80. if (layout_node.is_absolutely_positioned() || layout_node.is_floating()) {
  81. // Block is out-of-flow, it can have inline siblings if necessary.
  82. return layout_parent;
  83. }
  84. // Parent block has inline-level children (our siblings).
  85. // First move these siblings into an anonymous wrapper block.
  86. Vector<JS::Handle<Layout::Node>> children;
  87. while (JS::GCPtr<Layout::Node> child = layout_parent.first_child()) {
  88. layout_parent.remove_child(*child);
  89. children.append(*child);
  90. }
  91. layout_parent.append_child(layout_parent.create_anonymous_wrapper());
  92. layout_parent.set_children_are_inline(false);
  93. for (auto& child : children) {
  94. layout_parent.last_child()->append_child(*child);
  95. }
  96. layout_parent.last_child()->set_children_are_inline(true);
  97. // Then it's safe to insert this block into parent.
  98. return layout_parent;
  99. }
  100. void TreeBuilder::insert_node_into_inline_or_block_ancestor(Layout::Node& node, CSS::Display display, AppendOrPrepend mode)
  101. {
  102. if (display.is_inline_outside()) {
  103. // Inlines can be inserted into the nearest ancestor.
  104. auto& insertion_point = insertion_parent_for_inline_node(m_ancestor_stack.last());
  105. if (mode == AppendOrPrepend::Prepend)
  106. insertion_point.prepend_child(node);
  107. else
  108. insertion_point.append_child(node);
  109. insertion_point.set_children_are_inline(true);
  110. } else {
  111. // Non-inlines can't be inserted into an inline parent, so find the nearest non-inline ancestor.
  112. auto& nearest_non_inline_ancestor = [&]() -> Layout::NodeWithStyle& {
  113. for (auto& ancestor : m_ancestor_stack.in_reverse()) {
  114. if (!ancestor.display().is_inline_outside())
  115. return ancestor;
  116. if (!ancestor.display().is_flow_inside())
  117. return ancestor;
  118. if (ancestor.dom_node() && is<SVG::SVGForeignObjectElement>(*ancestor.dom_node()))
  119. return ancestor;
  120. }
  121. VERIFY_NOT_REACHED();
  122. }();
  123. auto& insertion_point = insertion_parent_for_block_node(nearest_non_inline_ancestor, node);
  124. if (mode == AppendOrPrepend::Prepend)
  125. insertion_point.prepend_child(node);
  126. else
  127. insertion_point.append_child(node);
  128. // After inserting an in-flow block-level box into a parent, mark the parent as having non-inline children.
  129. if (!node.is_floating() && !node.is_absolutely_positioned())
  130. insertion_point.set_children_are_inline(false);
  131. }
  132. }
  133. void TreeBuilder::create_pseudo_element_if_needed(DOM::Element& element, CSS::Selector::PseudoElement pseudo_element, AppendOrPrepend mode)
  134. {
  135. auto& document = element.document();
  136. auto& style_computer = document.style_computer();
  137. auto pseudo_element_style = style_computer.compute_style(element, pseudo_element);
  138. auto pseudo_element_content = pseudo_element_style->content();
  139. auto pseudo_element_display = pseudo_element_style->display();
  140. // ::before and ::after only exist if they have content. `content: normal` computes to `none` for them.
  141. // We also don't create them if they are `display: none`.
  142. if (pseudo_element_display.is_none()
  143. || pseudo_element_content.type == CSS::ContentData::Type::Normal
  144. || pseudo_element_content.type == CSS::ContentData::Type::None)
  145. return;
  146. auto pseudo_element_node = DOM::Element::create_layout_node_for_display_type(document, pseudo_element_display, pseudo_element_style, nullptr);
  147. if (!pseudo_element_node)
  148. return;
  149. pseudo_element_node->set_generated(true);
  150. // FIXME: Handle images, and multiple values
  151. if (pseudo_element_content.type == CSS::ContentData::Type::String) {
  152. auto* text = document.heap().allocate<DOM::Text>(document.realm(), document, pseudo_element_content.data);
  153. auto text_node = document.heap().allocate_without_realm<Layout::TextNode>(document, *text);
  154. text_node->set_generated(true);
  155. push_parent(verify_cast<NodeWithStyle>(*pseudo_element_node));
  156. insert_node_into_inline_or_block_ancestor(*text_node, text_node->display(), AppendOrPrepend::Append);
  157. pop_parent();
  158. } else {
  159. TODO();
  160. }
  161. element.set_pseudo_element_node({}, pseudo_element, pseudo_element_node);
  162. insert_node_into_inline_or_block_ancestor(*pseudo_element_node, pseudo_element_display, mode);
  163. }
  164. void TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder::Context& context)
  165. {
  166. // If the parent doesn't have a layout node, we don't need one either.
  167. if (dom_node.parent_or_shadow_host() && !dom_node.parent_or_shadow_host()->layout_node())
  168. return;
  169. Optional<TemporaryChange<bool>> has_svg_root_change;
  170. if (dom_node.is_svg_container()) {
  171. has_svg_root_change.emplace(context.has_svg_root, true);
  172. } else if (dom_node.requires_svg_container() && !context.has_svg_root) {
  173. return;
  174. }
  175. auto& document = dom_node.document();
  176. auto& style_computer = document.style_computer();
  177. JS::GCPtr<Layout::Node> layout_node;
  178. RefPtr<CSS::StyleProperties> style;
  179. CSS::Display display;
  180. if (is<DOM::Element>(dom_node)) {
  181. auto& element = static_cast<DOM::Element&>(dom_node);
  182. element.clear_pseudo_element_nodes({});
  183. VERIFY(!element.needs_style_update());
  184. style = element.computed_css_values();
  185. display = style->display();
  186. if (display.is_none())
  187. return;
  188. layout_node = element.create_layout_node(*style);
  189. } else if (is<DOM::Document>(dom_node)) {
  190. style = style_computer.create_document_style();
  191. display = style->display();
  192. layout_node = document.heap().allocate_without_realm<Layout::InitialContainingBlock>(static_cast<DOM::Document&>(dom_node), *style);
  193. } else if (is<DOM::Text>(dom_node)) {
  194. layout_node = document.heap().allocate_without_realm<Layout::TextNode>(document, static_cast<DOM::Text&>(dom_node));
  195. display = CSS::Display(CSS::Display::Outside::Inline, CSS::Display::Inside::Flow);
  196. } else if (is<DOM::ShadowRoot>(dom_node)) {
  197. layout_node = document.heap().allocate_without_realm<Layout::BlockContainer>(document, &static_cast<DOM::ShadowRoot&>(dom_node), CSS::ComputedValues {});
  198. display = CSS::Display(CSS::Display::Outside::Block, CSS::Display::Inside::FlowRoot);
  199. }
  200. if (!layout_node)
  201. return;
  202. if (!dom_node.parent_or_shadow_host()) {
  203. m_layout_root = layout_node;
  204. } else if (layout_node->is_svg_box()) {
  205. m_ancestor_stack.last().append_child(*layout_node);
  206. } else {
  207. insert_node_into_inline_or_block_ancestor(*layout_node, display, AppendOrPrepend::Append);
  208. }
  209. auto* shadow_root = is<DOM::Element>(dom_node) ? verify_cast<DOM::Element>(dom_node).shadow_root() : nullptr;
  210. if ((dom_node.has_children() || shadow_root) && layout_node->can_have_children()) {
  211. push_parent(verify_cast<NodeWithStyle>(*layout_node));
  212. if (shadow_root)
  213. create_layout_tree(*shadow_root, context);
  214. verify_cast<DOM::ParentNode>(dom_node).for_each_child([&](auto& dom_child) {
  215. create_layout_tree(dom_child, context);
  216. });
  217. pop_parent();
  218. }
  219. // Add nodes for the ::before and ::after pseudo-elements.
  220. if (is<DOM::Element>(dom_node)) {
  221. auto& element = static_cast<DOM::Element&>(dom_node);
  222. push_parent(verify_cast<NodeWithStyle>(*layout_node));
  223. create_pseudo_element_if_needed(element, CSS::Selector::PseudoElement::Before, AppendOrPrepend::Prepend);
  224. create_pseudo_element_if_needed(element, CSS::Selector::PseudoElement::After, AppendOrPrepend::Append);
  225. pop_parent();
  226. }
  227. if (is<ListItemBox>(*layout_node)) {
  228. auto& element = static_cast<DOM::Element&>(dom_node);
  229. int child_index = layout_node->parent()->index_of_child<ListItemBox>(*layout_node).value();
  230. auto marker_style = style_computer.compute_style(element, CSS::Selector::PseudoElement::Marker);
  231. auto list_item_marker = document.heap().allocate_without_realm<ListItemMarkerBox>(document, layout_node->computed_values().list_style_type(), child_index + 1, *marker_style);
  232. static_cast<ListItemBox&>(*layout_node).set_marker(list_item_marker);
  233. element.set_pseudo_element_node({}, CSS::Selector::PseudoElement::Marker, list_item_marker);
  234. layout_node->append_child(*list_item_marker);
  235. }
  236. if (is<HTML::HTMLProgressElement>(dom_node)) {
  237. auto& progress = static_cast<HTML::HTMLProgressElement&>(dom_node);
  238. if (!progress.using_system_appearance()) {
  239. auto bar_style = style_computer.compute_style(progress, CSS::Selector::PseudoElement::ProgressBar);
  240. bar_style->set_property(CSS::PropertyID::Display, CSS::IdentifierStyleValue::create(CSS::ValueID::InlineBlock));
  241. auto value_style = style_computer.compute_style(progress, CSS::Selector::PseudoElement::ProgressValue);
  242. value_style->set_property(CSS::PropertyID::Display, CSS::IdentifierStyleValue::create(CSS::ValueID::Block));
  243. auto position = progress.position();
  244. value_style->set_property(CSS::PropertyID::Width, CSS::PercentageStyleValue::create(CSS::Percentage(position >= 0 ? round_to<int>(100 * position) : 0)));
  245. auto bar_display = bar_style->display();
  246. auto value_display = value_style->display();
  247. auto progress_bar = DOM::Element::create_layout_node_for_display_type(document, bar_display, bar_style, nullptr);
  248. auto progress_value = DOM::Element::create_layout_node_for_display_type(document, value_display, value_style, nullptr);
  249. push_parent(verify_cast<NodeWithStyle>(*layout_node));
  250. push_parent(verify_cast<NodeWithStyle>(*progress_bar));
  251. insert_node_into_inline_or_block_ancestor(*progress_value, value_display, AppendOrPrepend::Append);
  252. pop_parent();
  253. insert_node_into_inline_or_block_ancestor(*progress_bar, bar_display, AppendOrPrepend::Append);
  254. pop_parent();
  255. progress.set_pseudo_element_node({}, CSS::Selector::PseudoElement::ProgressBar, progress_bar);
  256. progress.set_pseudo_element_node({}, CSS::Selector::PseudoElement::ProgressValue, progress_value);
  257. }
  258. }
  259. if (is<HTML::HTMLInputElement>(dom_node)) {
  260. auto& input_element = static_cast<HTML::HTMLInputElement&>(dom_node);
  261. if (auto placeholder_value = input_element.placeholder_value(); placeholder_value.has_value()) {
  262. auto placeholder_style = style_computer.compute_style(input_element, CSS::Selector::PseudoElement::Placeholder);
  263. auto placeholder = DOM::Element::create_layout_node_for_display_type(document, placeholder_style->display(), placeholder_style, nullptr);
  264. auto* text = document.heap().allocate<DOM::Text>(document.realm(), document, *placeholder_value);
  265. auto* text_node = document.heap().allocate_without_realm<Layout::TextNode>(document, *text);
  266. text_node->set_generated(true);
  267. push_parent(verify_cast<NodeWithStyle>(*layout_node));
  268. push_parent(verify_cast<NodeWithStyle>(*placeholder));
  269. insert_node_into_inline_or_block_ancestor(*text_node, text_node->display(), AppendOrPrepend::Append);
  270. pop_parent();
  271. insert_node_into_inline_or_block_ancestor(*placeholder, placeholder->display(), AppendOrPrepend::Append);
  272. pop_parent();
  273. input_element.set_pseudo_element_node({}, CSS::Selector::PseudoElement::Placeholder, placeholder);
  274. }
  275. }
  276. }
  277. JS::GCPtr<Layout::Node> TreeBuilder::build(DOM::Node& dom_node)
  278. {
  279. VERIFY(dom_node.is_document());
  280. Context context;
  281. create_layout_tree(dom_node, context);
  282. if (auto* root = dom_node.document().layout_node())
  283. fixup_tables(*root);
  284. return move(m_layout_root);
  285. }
  286. template<CSS::Display::Internal internal, typename Callback>
  287. void TreeBuilder::for_each_in_tree_with_internal_display(NodeWithStyle& root, Callback callback)
  288. {
  289. root.for_each_in_inclusive_subtree_of_type<Box>([&](auto& box) {
  290. auto const display = box.display();
  291. if (display.is_internal() && display.internal() == internal)
  292. callback(box);
  293. return IterationDecision::Continue;
  294. });
  295. }
  296. template<CSS::Display::Inside inside, typename Callback>
  297. void TreeBuilder::for_each_in_tree_with_inside_display(NodeWithStyle& root, Callback callback)
  298. {
  299. root.for_each_in_inclusive_subtree_of_type<Box>([&](auto& box) {
  300. auto const display = box.display();
  301. if (display.is_outside_and_inside() && display.inside() == inside)
  302. callback(box);
  303. return IterationDecision::Continue;
  304. });
  305. }
  306. void TreeBuilder::fixup_tables(NodeWithStyle& root)
  307. {
  308. remove_irrelevant_boxes(root);
  309. generate_missing_child_wrappers(root);
  310. generate_missing_parents(root);
  311. }
  312. void TreeBuilder::remove_irrelevant_boxes(NodeWithStyle& root)
  313. {
  314. // The following boxes are discarded as if they were display:none:
  315. Vector<JS::Handle<Node>> to_remove;
  316. // Children of a table-column.
  317. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableColumn>(root, [&](Box& table_column) {
  318. table_column.for_each_child([&](auto& child) {
  319. to_remove.append(child);
  320. });
  321. });
  322. // Children of a table-column-group which are not a table-column.
  323. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableColumnGroup>(root, [&](Box& table_column_group) {
  324. table_column_group.for_each_child([&](auto& child) {
  325. if (child.display().is_table_column())
  326. to_remove.append(child);
  327. });
  328. });
  329. // FIXME:
  330. // Anonymous inline boxes which contain only white space and are between two immediate siblings each of which is a table-non-root box.
  331. // Anonymous inline boxes which meet all of the following criteria:
  332. // - they contain only white space
  333. // - they are the first and/or last child of a tabular container
  334. // - whose immediate sibling, if any, is a table-non-root box
  335. for (auto& box : to_remove)
  336. box->parent()->remove_child(*box);
  337. }
  338. static bool is_table_track(CSS::Display display)
  339. {
  340. return display.is_table_row() || display.is_table_column();
  341. }
  342. static bool is_table_track_group(CSS::Display display)
  343. {
  344. // Unless explicitly mentioned otherwise, mentions of table-row-groups in this spec also encompass the specialized
  345. // table-header-groups and table-footer-groups.
  346. return display.is_table_row_group()
  347. || display.is_table_header_group()
  348. || display.is_table_footer_group()
  349. || display.is_table_column_group();
  350. }
  351. static bool is_not_proper_table_child(Node const& node)
  352. {
  353. if (!node.has_style())
  354. return true;
  355. auto const display = node.display();
  356. return !is_table_track_group(display) && !is_table_track(display) && !display.is_table_caption();
  357. }
  358. static bool is_not_table_row(Node const& node)
  359. {
  360. if (!node.has_style())
  361. return true;
  362. auto const display = node.display();
  363. return !display.is_table_row();
  364. }
  365. static bool is_not_table_cell(Node const& node)
  366. {
  367. if (!node.has_style())
  368. return true;
  369. auto const display = node.display();
  370. return !display.is_table_cell();
  371. }
  372. static bool is_ignorable_whitespace(Layout::Node const& node)
  373. {
  374. if (node.is_text_node() && static_cast<TextNode const&>(node).text_for_rendering().is_whitespace())
  375. return true;
  376. if (node.is_anonymous() && node.is_block_container() && static_cast<BlockContainer const&>(node).children_are_inline()) {
  377. bool contains_only_white_space = true;
  378. node.for_each_in_inclusive_subtree_of_type<TextNode>([&contains_only_white_space](auto& text_node) {
  379. if (!text_node.text_for_rendering().is_whitespace()) {
  380. contains_only_white_space = false;
  381. return IterationDecision::Break;
  382. }
  383. return IterationDecision::Continue;
  384. });
  385. if (contains_only_white_space)
  386. return true;
  387. }
  388. return false;
  389. }
  390. template<typename Matcher, typename Callback>
  391. static void for_each_sequence_of_consecutive_children_matching(NodeWithStyle& parent, Matcher matcher, Callback callback)
  392. {
  393. Vector<JS::Handle<Node>> sequence;
  394. auto sequence_is_all_ignorable_whitespace = [&]() -> bool {
  395. for (auto& node : sequence) {
  396. if (!is_ignorable_whitespace(*node))
  397. return false;
  398. }
  399. return true;
  400. };
  401. for (auto child = parent.first_child(); child; child = child->next_sibling()) {
  402. if (matcher(*child)) {
  403. sequence.append(*child);
  404. } else {
  405. if (!sequence.is_empty()) {
  406. if (!sequence_is_all_ignorable_whitespace())
  407. callback(sequence, child);
  408. sequence.clear();
  409. }
  410. }
  411. }
  412. if (!sequence.is_empty() && !sequence_is_all_ignorable_whitespace())
  413. callback(sequence, nullptr);
  414. }
  415. template<typename WrapperBoxType>
  416. static void wrap_in_anonymous(Vector<JS::Handle<Node>>& sequence, Node* nearest_sibling)
  417. {
  418. VERIFY(!sequence.is_empty());
  419. auto& parent = *sequence.first()->parent();
  420. auto computed_values = parent.computed_values().clone_inherited_values();
  421. static_cast<CSS::MutableComputedValues&>(computed_values).set_display(WrapperBoxType::static_display());
  422. auto wrapper = parent.heap().template allocate_without_realm<WrapperBoxType>(parent.document(), nullptr, move(computed_values));
  423. for (auto& child : sequence) {
  424. parent.remove_child(*child);
  425. wrapper->append_child(*child);
  426. }
  427. if (nearest_sibling)
  428. parent.insert_before(*wrapper, *nearest_sibling);
  429. else
  430. parent.append_child(*wrapper);
  431. }
  432. void TreeBuilder::generate_missing_child_wrappers(NodeWithStyle& root)
  433. {
  434. // 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.
  435. for_each_in_tree_with_inside_display<CSS::Display::Inside::Table>(root, [&](auto& parent) {
  436. for_each_sequence_of_consecutive_children_matching(parent, is_not_proper_table_child, [&](auto sequence, auto nearest_sibling) {
  437. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  438. });
  439. });
  440. // 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.
  441. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableRowGroup>(root, [&](auto& parent) {
  442. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  443. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  444. });
  445. });
  446. // Unless explicitly mentioned otherwise, mentions of table-row-groups in this spec also encompass the specialized
  447. // table-header-groups and table-footer-groups.
  448. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableHeaderGroup>(root, [&](auto& parent) {
  449. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  450. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  451. });
  452. });
  453. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableFooterGroup>(root, [&](auto& parent) {
  454. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_row, [&](auto& sequence, auto nearest_sibling) {
  455. wrap_in_anonymous<TableRowBox>(sequence, nearest_sibling);
  456. });
  457. });
  458. // 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
  459. for_each_in_tree_with_internal_display<CSS::Display::Internal::TableRow>(root, [&](auto& parent) {
  460. for_each_sequence_of_consecutive_children_matching(parent, is_not_table_cell, [&](auto& sequence, auto nearest_sibling) {
  461. wrap_in_anonymous<TableCellBox>(sequence, nearest_sibling);
  462. });
  463. });
  464. }
  465. void TreeBuilder::generate_missing_parents(NodeWithStyle&)
  466. {
  467. // FIXME: Implement.
  468. }
  469. }