TreeBuilder.cpp 27 KB

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