Skip to content

Truly independent web browser

LibWeb: Skip block-in-inline lines in an inline containing block

sideshowbarker90dbbc52d7fb
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 6ec47ad6993..d8e7722d24f 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -544,21 +544,21 @@ pub(crate) fn compute(
.expect("a line interrupted for a block-level box keeps the box")
.cells;
let collapsed = block.uses_collapsing_borders_model;
- let (border_box_inline_low, border_box_block_low, border_box_inline_size, border_box_block_size) = (
- block.border_box_left(collapsed),
- block.border_box_top(collapsed),
- block.border_box_inline_size(collapsed),
- block.border_box_block_size(collapsed),
- );
- let extent_inline_start = inline_start - border_box_inline_low;
- let extent_block_start = block_start - border_box_block_low;
+ let extent_block_start = block_start - block.border_box_top(collapsed);
+ // NB: A block-level box inside an inline box adds nothing to the inline extent of that inline box, so
+ // on the block's line, the inline box takes no more room than an empty piece at the line's start.
+ let line_start = if context.facts(context.containing_block).inline_axis_is_reverse() {
+ context.inline_size_of_block_container_holding_lines()
+ } else {
+ CssPixels::default()
+ };
Some((
position,
CandidateLineCorners {
- inline_start: extent_inline_start,
- inline_end: extent_inline_start + border_box_inline_size,
+ inline_start: line_start,
+ inline_end: line_start,
block_start: extent_block_start,
- block_end: extent_block_start + border_box_block_size,
+ block_end: extent_block_start + block.border_box_block_size(collapsed),
},
))
} else {
@@ -683,6 +683,9 @@ pub(crate) fn compute(
.fragments
.is_some_and(|fragments| fragments.any_pending_abspos_names_inline_containing_block(node));
let mut corners = FirstAndLastContentLineCorners::default();
+ // NB: Lines that hold only a block-level box inside the inline box don't count as its first or last line. They
+ // stand in for it only where it has no other lines.
+ let mut corners_from_block_lines = FirstAndLastContentLineCorners::default();
for line in lines {
let inline_box_anchor = || {
@@ -696,8 +699,8 @@ pub(crate) fn compute(
let Some((contributions_inline_start, contributions_inline_end)) = line.contributions_inline_range else {
if let Some((position, extent)) = line.interrupting_block {
if node_is_inline_containing_block {
- corners.first.get_or_insert(extent);
- corners.last = Some(extent);
+ corners_from_block_lines.first.get_or_insert(extent);
+ corners_from_block_lines.last = Some(extent);
}
let (inline_box_baseline, inline_box_vertical_shift) = inline_box_anchor();
staged.push(StagedPiece {
@@ -809,6 +812,9 @@ pub(crate) fn compute(
}
}
+ if corners.first.is_none() {
+ corners = corners_from_block_lines;
+ }
if node_is_inline_containing_block
&& let Some(rect) = padding_box_rect_spanning_first_and_last_content_lines(
&corners,
diff --git a/Tests/LibWeb/Text/expected/abspos-in-block-inside-relpos-inline.txt b/Tests/LibWeb/Text/expected/abspos-in-block-inside-relpos-inline.txt
new file mode 100644
index 00000000000..cb63c54fb6d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-in-block-inside-relpos-inline.txt
@@ -0,0 +1,2 @@
+with-text: abspos at (20, 20), inline starts at (20, 20)
+block-only: abspos at (20, 68), inline starts at (20, 68)
diff --git a/Tests/LibWeb/Text/expected/css/abspos-block-in-relative-inline.txt b/Tests/LibWeb/Text/expected/css/abspos-block-in-relative-inline.txt
new file mode 100644
index 00000000000..b1e29398b11
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/abspos-block-in-relative-inline.txt
@@ -0,0 +1,13 @@
+block only: left 20, width 0
+before block: left 20, width 40
+after block: left 20, width 80
+around block: left 20, width 80
+relative offset: left 31, width 0
+fixed-width block: left 20, width 0
+block padding and border: left 20, width 0
+inline decoration: left 23, width 7
+nested block: left 20, width 0
+positioned block: left 120, width 200
+block container: left 20, width 300
+pseudo-element at inline origin: true
+pseudo-element after margin change: true
diff --git a/Tests/LibWeb/Text/expected/css/abspos-cb-inline-relative-with-block-descendant.txt b/Tests/LibWeb/Text/expected/css/abspos-cb-inline-relative-with-block-descendant.txt
index e18b5d2dbf0..58194fcc3a9 100644
--- a/Tests/LibWeb/Text/expected/css/abspos-cb-inline-relative-with-block-descendant.txt
+++ b/Tests/LibWeb/Text/expected/css/abspos-cb-inline-relative-with-block-descendant.txt
@@ -1,15 +1,15 @@
-text-abs: 110,110 400x66
-empty-abs: 110,296 400x50
-multi-abs: 110,466 400x120
-bp-abs: 110,706 400x60
-nested-abs: 110,886 400x40
+text-abs: 110,110 79.921875x16
+empty-abs: 110,296 0x50
+multi-abs: 110,466 0x120
+bp-abs: 110,706 0x60
+nested-abs: 110,886 0x40
sized-abs: 120,1146 100x40
auto-abs: 110,1306 50x30
fixed-abs: 0,0 200x30
transform-abs: 100,1596 420x120
contain-abs: 100,1816 420x120
-filter-abs: 110,2046 400x30
-wc-pos-abs: 110,2196 400x30
+filter-abs: 110,2046 0x30
+wc-pos-abs: 110,2196 0x30
wc-tx-abs: 100,2336 420x120
contain-layout-abs: 100,2556 420x120
-bf-abs: 110,2786 400x30
+bf-abs: 110,2786 0x30
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.txt b/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.txt
new file mode 100644
index 00000000000..83f719c127b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Block-in-inline alone should not contribute to inline containing block width
+Pass Block-in-inline at the end should not contribute to inline containing block width
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.txt b/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.txt
new file mode 100644
index 00000000000..a27216a9ca7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass position:absolute should be sized by the right ancestor
+Pass position:fixed should be sized by the right ancestor
diff --git a/Tests/LibWeb/Text/input/abspos-in-block-inside-relpos-inline.html b/Tests/LibWeb/Text/input/abspos-in-block-inside-relpos-inline.html
new file mode 100644
index 00000000000..b068952b2c8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-in-block-inside-relpos-inline.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 20px; }
+ span { position: relative; }
+ .block { margin-left: 100px; }
+ .abspos { position: absolute; left: 0; top: 0; }
+</style>
+<span id="with-text">before<div class="block"><div class="abspos" id="in-block-with-text">X</div>text</div>after</span>
+<span id="block-only"><div class="block"><div class="abspos" id="in-block-only">X</div>text</div></span>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ // The relatively positioned inline is the containing block, so left: 0 is at its start, not at the block's.
+ for (const [inlineId, absposId] of [["with-text", "in-block-with-text"], ["block-only", "in-block-only"]]) {
+ const box = document.getElementById(absposId).getBoundingClientRect();
+ const inline = document.getElementById(inlineId).getClientRects()[0];
+ println(`${inlineId}: abspos at (${box.left}, ${box.top}), inline starts at (${inline.left}, ${inline.top})`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/abspos-block-in-relative-inline.html b/Tests/LibWeb/Text/input/css/abspos-block-in-relative-inline.html
new file mode 100644
index 00000000000..931a826c744
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/abspos-block-in-relative-inline.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 20px; }
+ .container { width: 300px; font: 16px/20px monospace; }
+ .relative { position: relative; }
+ .block { margin-left: 100px; height: 40px; background: lightgray; }
+ .target { position: absolute; left: 0; width: 100%; height: 10px; background: lime; }
+ .before { display: inline-block; width: 40px; height: 10px; }
+ .after { display: inline-block; width: 80px; height: 10px; }
+ .pseudo::before { content: ""; position: absolute; left: 0; width: 12px; height: 10px; background: lime; }
+</style>
+<script>
+ test(() => {
+ function check(label, before, after, inlineStyle = "", blockStyle = "", nested = false) {
+ const container = document.createElement("div");
+ container.className = "container";
+ container.innerHTML = `<span class="relative" style="${inlineStyle}">${before}<div class="block" style="${blockStyle}">${nested ? '<div style="margin-left: 30px">' : ""}<div class="target"></div>text${nested ? "</div>" : ""}</div>${after}</span>`;
+ document.body.append(container);
+ const target = container.querySelector(".target");
+ const rect = target.getBoundingClientRect();
+ println(`${label}: left ${rect.left}, width ${rect.width}`);
+ container.remove();
+ }
+ check("block only", "", "");
+ check("before block", '<span class="before"></span>', "");
+ check("after block", "", '<span class="after"></span>');
+ check("around block", '<span class="before"></span>', '<span class="after"></span>');
+ check("relative offset", "", "", "left: 11px");
+ check("fixed-width block", "", "", "", "width: 70px");
+ check("block padding and border", "", "", "", "padding: 9px; border: 6px solid");
+ check("inline decoration", "", "", "padding: 10px; border: 3px solid");
+ check("nested block", "", "", "", "", true);
+ check("positioned block", "", "", "", "position: relative");
+ check("block container", "", "", "display: block");
+
+ const container = document.createElement("div");
+ container.className = "container";
+ container.innerHTML = '<span class="relative"><div class="block pseudo">text</div></span>';
+ document.body.append(container);
+ const block = container.querySelector(".block");
+ const hitsInlineOrigin = () => document.elementFromPoint(25, container.getBoundingClientRect().top + 5) === block;
+ println(`pseudo-element at inline origin: ${hitsInlineOrigin()}`);
+ block.style.marginLeft = "30px";
+ println(`pseudo-element after margin change: ${hitsInlineOrigin()}`);
+ container.remove();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/abspos-cb-inline-relative-with-block-descendant.html b/Tests/LibWeb/Text/input/css/abspos-cb-inline-relative-with-block-descendant.html
index e7e5c20d7e0..2b15bcb3247 100644
--- a/Tests/LibWeb/Text/input/css/abspos-cb-inline-relative-with-block-descendant.html
+++ b/Tests/LibWeb/Text/input/css/abspos-cb-inline-relative-with-block-descendant.html
@@ -6,9 +6,9 @@
.abs { position: absolute; inset: 0; display: block; }
</style>
<body style="margin: 0;">
- <!-- 1. Inline text plus a block descendant. The CB rect spans from the
- first content line (the text) to the last one: the line generated by
- the block, whose border box the inline's fragment wraps. -->
+ <!-- 1. Inline text plus a block descendant. The block's line doesn't
+ count as one of the inline's lines, so the CB rect is the text's line
+ alone. -->
<div class="frame">
<span style="display: inline; position: relative;">inline-text<span
id="text-abs" class="abs">abs</span><div class="reset"
@@ -16,15 +16,16 @@
</div>
<!-- 2. Empty inline + block descendant: the block's line is the only
- content line, so the rect is the block's border box. -->
+ line, so the rect is a zero-width piece at the line's start, as tall
+ as the block's border box. -->
<div class="frame">
<span style="display: inline; position: relative;"><span
id="empty-abs" class="abs">abs</span><div class="reset"
style="height: 50px;">block-sibling</div></span>
</div>
- <!-- 3. Multiple block descendants: the rect spans from the first block's
- border-box top to the last block's border-box bottom. -->
+ <!-- 3. Multiple block descendants: the zero-width rect spans from the
+ first block's border-box top to the last block's border-box bottom. -->
<div class="frame">
<span style="display: inline; position: relative;"><span
id="multi-abs" class="abs">abs</span><div class="reset"
@@ -33,8 +34,8 @@
style="height: 50px;">third</div></span>
</div>
- <!-- 4. A block descendant with border + padding contributes its border
- box. -->
+ <!-- 4. A block descendant with border + padding: the zero-width rect is
+ as tall as its border box. -->
<div class="frame">
<span style="display: inline; position: relative;"><span
id="bp-abs" class="abs">abs</span><div class="reset"
@@ -42,7 +43,8 @@
</div>
<!-- 5. Nested inline-relatives: innermost is the abspos's CB; its only
- content line is the inner block's, so the rect is that border box. -->
+ line is the inner block's, so the rect is a zero-width piece as tall
+ as that border box. -->
<div class="frame">
<span style="display: inline; position: relative;"><span
style="display: inline; position: relative;"><span
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.html b/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.html
new file mode 100644
index 00000000000..c7de9352a0f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline-002.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<link rel="help" href="https://www.w3.org/TR/CSS22/visudet.html#containing-block-details">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level">
+<link rel="help" href="https://crbug.com/40910996">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<style>
+body { margin: 0; }
+.rel { position: relative; }
+.inline-block { display: inline-block; width: 100px; height: 10px; }
+.block { display: block; height: 50px; }
+.abs { position: absolute; inset: 0; width: 100%; }
+</style>
+<div style="width: 500px;">
+ <!-- Only a block-in-inline and abspos inside a relpos inline -->
+ <span class="rel">
+ <span class="block"></span>
+ <span id="target1" class="abs"></span>
+ </span>
+
+ <!-- Inline content only before the block-in-inline -->
+ <span class="rel">
+ <span class="inline-block"></span>
+ <span class="block"></span>
+ <span id="target2" class="abs"></span>
+ </span>
+</div>
+<script>
+test(() => {
+ assert_equals(document.getElementById("target1").offsetWidth, 0);
+}, "Block-in-inline alone should not contribute to inline containing block width");
+
+test(() => {
+ assert_equals(document.getElementById("target2").offsetWidth, 100);
+}, "Block-in-inline at the end should not contribute to inline containing block width");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.html b/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.html
new file mode 100644
index 00000000000..d03c349b07b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/CSS2/abspos/abspos-in-block-in-inline-in-relpos-inline.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<link rel="author" title="Morten Stenshorne" href="[email protected]">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visudet.html#containing-block-details">
+<link rel="help" href="https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level">
+<style>
+ body { margin: 0px;}
+ .rel { position:relative; }
+ .inline-block { display:inline-block; width:100px; height:1px; }
+ .inline-block.large { width:200px; }
+ #target { position:absolute; width:100%; height:100px; background:green; }
+ #target-fixed { position:fixed; width:100%; height:100px; background:yellow; }
+</style>
+<p>There should be a green square, and a yellow rectangle below.</p>
+<div style="height:200px;">
+ <span class="rel" id="notContainingBlockOfTarget">
+ <div class="large inline-block"></div>
+ <br>
+ <span id="containingBlockOfTarget" class="rel">
+ <div class="inline-block"></div>
+ <span>
+ <div>
+ <div id="target-fixed"></div>
+ <div id="target"></div>
+ </div>
+ </span>
+ <div class="inline-block"></div></span><br>
+ <div class="large inline-block"></div>
+ </span>
+</div>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script>
+ test(()=> {
+ assert_equals(document.getElementById("target").offsetWidth,
+ // `offsetWidth` is not interoperable for block-in-inline.
+ // Use the size of ".inline-block" to avoid this.
+ document.querySelector("#containingBlockOfTarget .inline-block").offsetWidth);
+ }, "position:absolute should be sized by the right ancestor");
+ test(()=> {
+ assert_equals(document.getElementById("target-fixed").offsetWidth,
+ document.body.offsetWidth);
+ }, "position:fixed should be sized by the right ancestor");
+</script>