Skip to content

Truly independent web browser

LibWeb: Keep abspos pseudo-elements inside their inline parent

Andreas Kling3852746bf5b4
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index 7d962e22640..1b8c024eb4b 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -4127,15 +4127,20 @@ fn insertion_parent_for_block_node(
// Inline is fine for in-flow block children (interrupting blocks) and for out-of-flow children;
// the inline formatting context emits items for both.
- // Block-level pseudo-element boxes climb out of inline ancestors instead (see below). A table-internal
- // pseudo-element box has to stay a child of its originating inline box though: table fixup generates one anonymous
- // inline-table around the consecutive table-internal children of the inline, so e.g. a `display: table-cell`
- // ::after joins the table of the table-cell siblings in the inline instead of starting a separate block-level
- // table outside of it.
- // https://drafts.csswg.org/css-tables-3/#fixup-algorithm
- let is_table_internal_pseudo_element_box = node_facts::has_flag(host.data(node), NodeFlag::Anonymous)
- && is_table_non_root_box_with_display(display_for_table_fixup(host, node));
- if (!node_facts::has_flag(host.data(node), NodeFlag::Anonymous) || is_table_internal_pseudo_element_box)
+ // In-flow block-level pseudo-element boxes climb out of inline ancestors instead (see below). Two kinds of
+ // pseudo-element box stay a child of their originating inline box like a DOM child would:
+ // - A table-internal one: table fixup generates one anonymous inline-table around the consecutive table-internal
+ // children of the inline, so e.g. a `display: table-cell` ::after joins the table of the table-cell siblings in
+ // the inline instead of starting a separate block-level table outside of it.
+ // https://drafts.csswg.org/css-tables-3/#fixup-algorithm
+ // - An absolutely positioned one: its static position is inside the inline's content, not after its inline-end
+ // edges.
+ let is_anonymous = node_facts::has_flag(host.data(node), NodeFlag::Anonymous);
+ let is_table_internal_pseudo_element_box =
+ is_anonymous && is_table_non_root_box_with_display(display_for_table_fixup(host, node));
+ let is_absolutely_positioned_pseudo_element_box = node_is_generated_for_pseudo_element(host.data(node))
+ && host.style(node).is_some_and(|style| style.is_absolutely_positioned());
+ if (!is_anonymous || is_table_internal_pseudo_element_box || is_absolutely_positioned_pseudo_element_box)
&& node_is_inline_outside(host, parent)
&& host.style(parent).is_some_and(|style| style.display().is_flow_inside())
{
diff --git a/Tests/LibWeb/Layout/expected/abspos-pseudo-element-with-inline-as-abspos-containing-block.txt b/Tests/LibWeb/Layout/expected/abspos-pseudo-element-with-inline-as-abspos-containing-block.txt
index 666d1c6f52f..f9e7f2f91f9 100644
--- a/Tests/LibWeb/Layout/expected/abspos-pseudo-element-with-inline-as-abspos-containing-block.txt
+++ b/Tests/LibWeb/Layout/expected/abspos-pseudo-element-with-inline-as-abspos-containing-block.txt
@@ -5,7 +5,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 8, rect: [8,8 74.125x16] baseline: 12.796875
"Features"
TextNode <#text> (not painted)
- BlockContainer <(anonymous)> at [85.625,15.1875] positioned [0+0+0 8 0+0+0] [0+0+0 5 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [85.625,15.1875] positioned [0+0+0 8 0+0+0] [0+0+0 5 0+0+0] [BFC] children: not-inline
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/block-and-inline/span-pseudo-abspos-after.txt b/Tests/LibWeb/Layout/expected/block-and-inline/span-pseudo-abspos-after.txt
index 2551b0074da..d7ec82fcf52 100644
--- a/Tests/LibWeb/Layout/expected/block-and-inline/span-pseudo-abspos-after.txt
+++ b/Tests/LibWeb/Layout/expected/block-and-inline/span-pseudo-abspos-after.txt
@@ -6,10 +6,10 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 4, rect: [0,5 40x10] baseline: 8
"text"
TextNode <#text> (not painted)
- BlockContainer <(anonymous)> at [40,0] positioned [0+0+0 10 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline
- frag 0 from GeneratedTextNode start: 0, length: 1, rect: [40,5 10x10] baseline: 8
- "X"
- GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <(anonymous)> at [40,0] positioned [0+0+0 10 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline
+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [40,5 10x10] baseline: 8
+ "X"
+ GeneratedTextNode <(anonymous)> (not painted)
BlockContainer <(anonymous)> at [0,20] [0+0+0 800 0+0+0] [0+0+0 0 0+0+0] children: inline
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/expected/pseudo-element-position-absolute.txt b/Tests/LibWeb/Layout/expected/pseudo-element-position-absolute.txt
index a2429d74dc8..1630bffd657 100644
--- a/Tests/LibWeb/Layout/expected/pseudo-element-position-absolute.txt
+++ b/Tests/LibWeb/Layout/expected/pseudo-element-position-absolute.txt
@@ -6,10 +6,10 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 6, rect: [8,8 54.796875x16] baseline: 12.796875
"foobar"
TextNode <#text> (not painted)
- BlockContainer <(anonymous)> at [8,24] positioned [0+0+0 50 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
- frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,24 27.203125x16] baseline: 12.796875
- "baz"
- GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <(anonymous)> at [8,24] positioned [0+0+0 50 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,24 27.203125x16] baseline: 12.796875
+ "baz"
+ GeneratedTextNode <(anonymous)> (not painted)
TextNode <#text> (not painted)
BlockContainer <div> at [8,24] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
frag 0 from TextNode start: 0, length: 11, rect: [8,24 97.640625x16] baseline: 12.796875
@@ -21,10 +21,10 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 6, rect: [8,40 54.796875x16] baseline: 12.796875
"foobar"
TextNode <#text> (not painted)
- BlockContainer <(anonymous)> at [8,56] positioned [0+0+0 50 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
- frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,56 27.203125x16] baseline: 12.796875
- "baz"
- GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <(anonymous)> at [8,56] positioned [0+0+0 50 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,56 27.203125x16] baseline: 12.796875
+ "baz"
+ GeneratedTextNode <(anonymous)> (not painted)
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
diff --git a/Tests/LibWeb/Ref/data/abspos-pseudo-element-in-inline-static-position.css b/Tests/LibWeb/Ref/data/abspos-pseudo-element-in-inline-static-position.css
new file mode 100644
index 00000000000..1f5abe5daf3
--- /dev/null
+++ b/Tests/LibWeb/Ref/data/abspos-pseudo-element-in-inline-static-position.css
@@ -0,0 +1,22 @@
+body {
+ margin: 0;
+ font: 20px/30px serif;
+}
+div {
+ margin: 10px;
+}
+.narrow {
+ width: 120px;
+}
+.external {
+ position: relative;
+ padding-left: 20px;
+ padding-right: 30px;
+ background: lightblue;
+}
+.box {
+ display: inline-block;
+ width: 80px;
+ height: 20px;
+ background: blue;
+}
diff --git a/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-dynamic-ref.html b/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-dynamic-ref.html
new file mode 100644
index 00000000000..c50be9b8573
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-dynamic-ref.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<link rel="stylesheet" href="../data/abspos-pseudo-element-in-inline-static-position.css">
+<style>
+ .icon {
+ position: absolute;
+ margin-left: 4px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+</style>
+<div><span class="external"><span class="box"></span><span class="icon"></span></span><span class="box"></span></div>
+<div><span class="external"><span class="box"></span> <span class="box"></span><span class="icon"></span></span><span class="box"></span></div>
+<div><span class="external">x<span class="box"></span><span class="icon"></span></span><span class="box"></span></div>
diff --git a/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-ref.html b/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-ref.html
new file mode 100644
index 00000000000..af4a1f655b7
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/abspos-pseudo-element-in-inline-static-position-ref.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<link rel="stylesheet" href="../data/abspos-pseudo-element-in-inline-static-position.css">
+<style>
+ .icon {
+ position: absolute;
+ margin-left: 4px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+ .corner-icon {
+ position: absolute;
+ top: 0;
+ right: 0;
+ margin-left: 4px;
+ width: 6px;
+ height: 6px;
+ background: orange;
+ }
+ .leading-icon {
+ position: absolute;
+ margin-left: -14px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+</style>
+<div><span class="external"><span class="box"></span><span class="icon"></span></span><span class="box"></span></div>
+<div><span class="external"><span class="box"></span><span class="icon"></span></span>,<span class="box"></span></div>
+<div class="narrow"><span class="external"><span class="box"></span> <span class="box"></span><span class="icon"></span></span></div>
+<div><span class="external"><span class="box"></span><span class="corner-icon"></span></span></div>
+<div><span class="external"><span class="leading-icon"></span><span class="box"></span><span class="icon"></span></span></div>
diff --git a/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position-dynamic.html b/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position-dynamic.html
new file mode 100644
index 00000000000..07eec5c9341
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position-dynamic.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/abspos-pseudo-element-in-inline-static-position-dynamic-ref.html">
+<link rel="stylesheet" href="../data/abspos-pseudo-element-in-inline-static-position.css">
+<style>
+ .external::after {
+ content: "\200b";
+ position: absolute;
+ margin-left: 4px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+ .external.in-flow::after {
+ position: static;
+ display: inline-block;
+ }
+</style>
+<div><span class="external in-flow" id="toggle"><span class="box"></span></span><span class="box"></span></div>
+<div><span class="external" id="append"><span class="box"></span></span><span class="box"></span></div>
+<div><span class="external" id="insert"><span class="box"></span></span><span class="box"></span></div>
+<script>
+ window.onload = () => {
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.getElementById("toggle").classList.remove("in-flow");
+
+ const box = document.createElement("span");
+ box.className = "box";
+ document.getElementById("append").append(" ", box);
+
+ document.getElementById("insert").prepend("x");
+
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+ };
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position.html b/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position.html
new file mode 100644
index 00000000000..7bc6bfb92cb
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/abspos-pseudo-element-in-inline-static-position.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/abspos-pseudo-element-in-inline-static-position-ref.html">
+<link rel="stylesheet" href="../data/abspos-pseudo-element-in-inline-static-position.css">
+<style>
+ .external::after {
+ content: "\200b";
+ position: absolute;
+ margin-left: 4px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+ .corner::after {
+ content: "";
+ position: absolute;
+ top: 0;
+ right: 0;
+ width: 6px;
+ height: 6px;
+ background: orange;
+ }
+ .leading::before {
+ content: "";
+ position: absolute;
+ margin-left: -14px;
+ width: 10px;
+ height: 10px;
+ background: green;
+ }
+</style>
+<div><span class="external"><span class="box"></span></span><span class="box"></span></div>
+<div><span class="external"><span class="box"></span></span>,<span class="box"></span></div>
+<div class="narrow"><span class="external"><span class="box"></span> <span class="box"></span></span></div>
+<div><span class="external corner"><span class="box"></span></span></div>
+<div><span class="external leading"><span class="box"></span></span></div>