TreeBuilder.cpp 27 KB

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