TreeBuilder.cpp 28 KB

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