Truly independent web browser
LibWeb: Keep out-of-flow children of math boxes out of wrappers
Andreas Klinge80a0ce2d459
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex 2403b7263f7..cc436ab3ac8 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -3581,6 +3581,14 @@ fn node_is_out_of_flow_child_of(host: &TreeBuilderHost<'_>, parent: LayoutNode,!parent_lays_out_math || host.style(node).is_some_and(|style| style.is_absolutely_positioned())}+fn lays_out_each_child_as_its_own_box(host: &TreeBuilderHost<'_>, node: LayoutNode) -> bool {+ // NB: Flex, grid and MathML layout lay out each of their children as its own box.+ host.style(node).is_some_and(|style| {+ let display = style.display();+ display.is_flex_inside() || display.is_grid_inside() || display.is_math_inside()+ })+}+fn node_has_replaced_element_table_display_adjustment(host: &TreeBuilderHost<'_>, node: LayoutNode) -> bool {node_facts::has_flag(host.data(node), NodeFlag::IsReplacedElement)&& host.style(node).is_some_and(|style| {@@ -4229,11 +4237,8 @@ fn insertion_parent_for_block_node(}true}));- let new_parent_display = host.style(new_parent).map(|style| style.display());- // NB: Flex, grid and MathML layout lay out each of their children as its own box.if appends_after_existing_children- && !new_parent_display- .is_some_and(|display| display.is_flex_inside() || display.is_grid_inside() || display.is_math_inside())+ && !lays_out_each_child_as_its_own_box(host, new_parent)&& !node_is_generated_for_pseudo_element(last_child_data)&& node_facts::has_flag(last_child_data, NodeFlag::Anonymous)&& node_facts::has_flag(last_child_data, NodeFlag::ChildrenAreInline)@@ -4252,27 +4257,55 @@ fn insertion_parent_for_block_node(// Parent block has inline-level children (our siblings); wrap these siblings into an anonymous wrapper block.note_layout_tree_restructuring_at(host, state, new_parent);- let mut children_to_wrap = Vec::new();- let mut child = host.first_child(new_parent);- while !child.is_invalid() {- if !is_out_of_flow_table_internal_child_of_table_root(host, new_parent, child) {- children_to_wrap.push(child);+ if lays_out_each_child_as_its_own_box(host, new_parent) {+ // NB: An out-of-flow child of a parent that lays out each of its children as its own box stays a child of+ // that parent, so that it is positioned as the parent's own abspos child instead of becoming part of an+ // anonymous wrapper. Each run of in-flow siblings between them gets its own wrapper, in DOM order.+ let mut runs: Vec<Vec<LayoutNode>> = vec![Vec::new()];+ let mut child = host.first_child(new_parent);+ while !child.is_invalid() {+ if node_is_out_of_flow_child_of(host, new_parent, child) {+ runs.push(Vec::new());+ } else if let Some(run) = runs.last_mut() {+ run.push(child);+ }+ child = host.next_sibling(child);}- child = host.next_sibling(child);- }- let wrapper = host.create_anonymous_wrapper_box(new_parent);- let wrapper_slot = wrapper.slot();- host.set_children_are_inline(wrapper_slot, true);- for child in children_to_wrap {- host.move_child(child, wrapper_slot, NodeSlotId::INVALID);+ for run in runs.into_iter().filter(|run| !run.is_empty()) {+ wrap_children_in_anonymous_wrapper(host, new_parent, &run, run[0]);+ }+ } else {+ let mut children_to_wrap = Vec::new();+ let mut child = host.first_child(new_parent);+ while !child.is_invalid() {+ if !is_out_of_flow_table_internal_child_of_table_root(host, new_parent, child) {+ children_to_wrap.push(child);+ }+ child = host.next_sibling(child);+ }+ wrap_children_in_anonymous_wrapper(host, new_parent, &children_to_wrap, NodeSlotId::INVALID);}host.set_children_are_inline(new_parent, false);- host.attach_child(new_parent, wrapper, NodeSlotId::INVALID);// Then it's safe to insert this block into parent.new_parent}+fn wrap_children_in_anonymous_wrapper(+ host: &mut TreeBuilderHost<'_>,+ parent: LayoutNode,+ children: &[LayoutNode],+ before: LayoutNode,+) {+ let wrapper = host.create_anonymous_wrapper_box(parent);+ let wrapper_slot = wrapper.slot();+ host.set_children_are_inline(wrapper_slot, true);+ host.attach_child(parent, wrapper, before);+ for &child in children {+ host.move_child(child, wrapper_slot, NodeSlotId::INVALID);+ }+}+fn insert_child_in_dom_order(host: &TreeBuilderHost<'_>,parent: LayoutNode,diff --git a/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txt b/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txtnew file mode 100644index 00000000000..6b869f596de--- /dev/null+++ b/Tests/LibWeb/Layout/expected/mathml-out-of-flow-children-are-not-wrapped.txt@@ -0,0 +1,45 @@+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 36 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 20 0+0+8] children: inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [8,8 33.171875x20] baseline: 16+ BlockContainer <math> at [8,8] [0+0+0 33.171875 0+0+0] [0+0+0 20 0+0+0] [MathFC] children: not-inline+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <mn> at [8,8] positioned [0+0+0 12 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,8 12x20] baseline: 16+ "0"+ TextNode <#text> (not painted)+ BlockContainer <mn> at [8,8] positioned [0+0+0 7.9375 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,8 7.9375x20] baseline: 16+ "1"+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <mn> at [8,8] [0+0+0 11.015625 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [8,8 11.015625x20] baseline: 16+ "2"+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <mrow> at [23.453125,8] [0+0+0 13.28125 0+0+0] [0+0+0 20 0+0+0] [MathFC] children: not-inline+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <mn> at [23.453125,8] positioned [0+0+0 11.375 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [23.453125,8 11.375x20] baseline: 16+ "3"+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <mo> at [23.453125,8] [0+0+0 13.28125 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 1, rect: [23.453125,8 13.28125x20] baseline: 16+ "+"+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ BlockContainer <(anonymous)> (not painted) [BFC] children: inline+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x36] (z-index: auto)+ SC for BlockContainer<mn> [8,8 7.9375x20] (z-index: auto)diff --git a/Tests/LibWeb/Layout/input/mathml-out-of-flow-children-are-not-wrapped.html b/Tests/LibWeb/Layout/input/mathml-out-of-flow-children-are-not-wrapped.htmlnew file mode 100644index 00000000000..d40e2ffe117--- /dev/null+++ b/Tests/LibWeb/Layout/input/mathml-out-of-flow-children-are-not-wrapped.html@@ -0,0 +1,14 @@+<!DOCTYPE html>+<style>+ math {+ font-size: 20px;+ }+</style>+<math>+ <mn style="position: absolute">0</mn><mn style="position: fixed">1</mn>+ <mn>2</mn>+ <mrow>+ <mn style="position: absolute">3</mn>+ <mo>+</mo>+ </mrow>+</math>diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-001.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-001.txtindex bd9614c2c72..058a0a74538 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-001.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-001.txt@@ -2,30 +2,30 @@ Harness status: OKFound 40 tests-29 Pass-11 Fail+38 Pass+2 FailPass mrow (embellished operator)-Fail mrow (embellished operator, from in-flow children)+Pass mrow (embellished operator, from in-flow children)Pass mrow (not embellished operator)Pass mrow (not embellished operator, from in-flow children)Pass mstyle (embellished operator)-Fail mstyle (embellished operator, from in-flow children)+Pass mstyle (embellished operator, from in-flow children)Pass mstyle (not embellished operator)Pass mstyle (not embellished operator, from in-flow children)Pass mphantom (embellished operator)-Fail mphantom (embellished operator, from in-flow children)+Pass mphantom (embellished operator, from in-flow children)Pass mphantom (not embellished operator)Pass mphantom (not embellished operator, from in-flow children)Pass mpadded (embellished operator)-Fail mpadded (embellished operator, from in-flow children)+Pass mpadded (embellished operator, from in-flow children)Pass mpadded (not embellished operator)Pass mpadded (not embellished operator, from in-flow children)Pass merror (embellished operator)-Fail merror (embellished operator, from in-flow children)+Pass merror (embellished operator, from in-flow children)Pass merror (not embellished operator)Pass merror (not embellished operator, from in-flow children)Pass mprescripts (embellished operator)-Fail mprescripts (embellished operator, from in-flow children)+Pass mprescripts (embellished operator, from in-flow children)Pass mprescripts (not embellished operator)Pass mprescripts (not embellished operator, from in-flow children)Fail none (embellished operator)@@ -33,14 +33,14 @@ Fail none (embellished operator, from in-flow children)Pass none (not embellished operator)Pass none (not embellished operator, from in-flow children)Pass unknown (embellished operator)-Fail unknown (embellished operator, from in-flow children)+Pass unknown (embellished operator, from in-flow children)Pass unknown (not embellished operator)Pass unknown (not embellished operator, from in-flow children)Pass mfenced (embellished operator)-Fail mfenced (embellished operator, from in-flow children)+Pass mfenced (embellished operator, from in-flow children)Pass mfenced (not embellished operator)Pass mfenced (not embellished operator, from in-flow children)Pass a (embellished operator)-Fail a (embellished operator, from in-flow children)+Pass a (embellished operator, from in-flow children)Pass a (not embellished operator)Pass a (not embellished operator, from in-flow children)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-002.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-002.txtindex c483e6180de..49db960fa02 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-002.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/operators/embellished-operator-002.txt@@ -2,37 +2,36 @@ Harness status: OKFound 32 tests-24 Pass-8 Fail+32 PassPass msub (embellished operator)-Fail msub (embellished operator, from in-flow children)+Pass msub (embellished operator, from in-flow children)Pass msub (not embellished operator)Pass msub (not embellished operator, from in-flow children)Pass msup (embellished operator)-Fail msup (embellished operator, from in-flow children)+Pass msup (embellished operator, from in-flow children)Pass msup (not embellished operator)Pass msup (not embellished operator, from in-flow children)Pass msubsup (embellished operator)-Fail msubsup (embellished operator, from in-flow children)+Pass msubsup (embellished operator, from in-flow children)Pass msubsup (not embellished operator)Pass msubsup (not embellished operator, from in-flow children)Pass munder (embellished operator)-Fail munder (embellished operator, from in-flow children)+Pass munder (embellished operator, from in-flow children)Pass munder (not embellished operator)Pass munder (not embellished operator, from in-flow children)Pass mover (embellished operator)-Fail mover (embellished operator, from in-flow children)+Pass mover (embellished operator, from in-flow children)Pass mover (not embellished operator)Pass mover (not embellished operator, from in-flow children)Pass munderover (embellished operator)-Fail munderover (embellished operator, from in-flow children)+Pass munderover (embellished operator, from in-flow children)Pass munderover (not embellished operator)Pass munderover (not embellished operator, from in-flow children)Pass mmultiscripts (embellished operator)-Fail mmultiscripts (embellished operator, from in-flow children)+Pass mmultiscripts (embellished operator, from in-flow children)Pass mmultiscripts (not embellished operator)Pass mmultiscripts (not embellished operator, from in-flow children)Pass mfrac (embellished operator)-Fail mfrac (embellished operator, from in-flow children)+Pass mfrac (embellished operator, from in-flow children)Pass mfrac (not embellished operator)Pass mfrac (not embellished operator, from in-flow children)\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/spaces/space-like-004.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/spaces/space-like-004.txtindex 30d117df43d..52be246aa7f 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/spaces/space-like-004.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/spaces/space-like-004.txt@@ -2,9 +2,8 @@ Harness status: OKFound 4 tests-3 Pass-1 Fail+4 PassPass complex space-like subtree-Fail complex space-like subtree, from in-flow children+Pass complex space-like subtree, from in-flow childrenPass complex non-space-like subtreePass complex non-space-like subtree, from in-flow children\ No newline at end of file