Skip to content

Truly independent web browser

LibWeb: Size abspos boxes from their static-position edge

sideshowbarkerd54124d67eb8
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 866311c622c..5b878812889 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1912,6 +1912,36 @@ impl AbsposEngine<'_> {
!equation.has_both_insets() || self.own_self_alignment_keyword(sized, inline_axis).is_some()
}
+ fn shrink_to_fit_available_size(
+ inputs: &abspos_inputs::AbsposLayoutInputs,
+ axis: SizingAxis,
+ mut equation: AxisEquation,
+ ) -> CssPixels {
+ let (anchor_aligns_inline, anchor_aligns_block) = inputs.containing_block_info.anchor_aligned_axes();
+ if equation.start.is_none() && equation.end.is_none() && !axis.select(anchor_aligns_inline, anchor_aligns_block)
+ {
+ let static_position = static_position_rect_for_sizing(inputs);
+ let rect = static_position.rect;
+ let offset = axis.select(rect.offset.inline_offset, rect.offset.block_offset);
+ // https://drafts.csswg.org/css-position-3/#resolving-inset-auto
+ // Resolve the static-position inset before measuring the available space, without depending on the size
+ // of the box being sized. The opposite inset and any auto margins contribute zero.
+ match axis.select(static_position.inline_alignment, static_position.block_alignment) {
+ AxisEdge::Start => equation.start = Some(offset),
+ AxisEdge::End => {
+ equation.end = Some(
+ equation.containing_block_size
+ - offset
+ - axis.select(rect.size.inline_size, rect.size.block_size),
+ );
+ }
+ // FIXME: Implement the center alignment case.
+ AxisEdge::Center => {}
+ }
+ }
+ equation.stretch_fit_size()
+ }
+
/// The content inline size of a non-replaced box: its specified size, or the size that auto gives it in the space
/// between its insets and margins, adjusted by calc-size() and its limits.
fn non_replaced_inline_size(
@@ -1942,10 +1972,16 @@ impl AbsposEngine<'_> {
inner_size(style.width())
} else if !self.sizes_to_fit_content(sized, equation, true) {
equation.stretch_fit_size()
+ } else if sizing.establishes_orthogonal_flow(node) && sized.cells.has_definite_block_size() {
+ sizing.calculate_max_content_inline_size_at_definite_block_size(
+ sized.sized_box(),
+ constraints,
+ sized.cells.content_block_size(),
+ )
} else {
// https://drafts.csswg.org/css-position-3/#abspos-auto-size
// For the purpose of computing these sizes, any auto margins are treated as zero.
- let available = equation.stretch_fit_size();
+ let available = Self::shrink_to_fit_available_size(inputs, SizingAxis::Inline, equation);
let preferred = sizing.calculate_max_content_inline_size(sized.sized_box(), constraints);
if preferred <= available {
preferred
@@ -2145,7 +2181,13 @@ impl AbsposEngine<'_> {
} else if !self.sizes_to_fit_content(sized, equation, false) {
Some(equation.stretch_fit_size())
} else {
- self.automatic_block_size(sized, available_space, constraints, pass, equation.stretch_fit_size())
+ self.automatic_block_size(
+ sized,
+ available_space,
+ constraints,
+ pass,
+ Self::shrink_to_fit_available_size(inputs, SizingAxis::Block, equation),
+ )
};
let minimum =
(!crate::layout::sizing_context::is_plain_auto(style.min_height())).then(|| inner_size(style.min_height()));
@@ -2348,6 +2390,23 @@ impl<'pass> AbsposEngine<'pass> {
self.auto_margin_overflow(inputs.containing_block, SizingAxis::Block),
);
+ if self.sizing().establishes_orthogonal_flow(node)
+ && style.width().is_auto()
+ && !self
+ .sizing()
+ .box_is_sized_as_replaced_element(sized.sized_box(), available_space, constraints)
+ {
+ // The frame's block size is the orthogonal box's inline size. Its content-based block size depends on
+ // wrapping at that resolved size.
+ self.compute_inline_size(
+ sized,
+ available_space,
+ constraints,
+ inputs,
+ self.auto_margin_overflow(inputs.containing_block, SizingAxis::Inline),
+ );
+ }
+
{
let used = sized.cells;
if !style.inset_left().is_auto() && !style.inset_right().is_auto() {
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-height-002-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-height-002-ref.xht
new file mode 100644
index 00000000000..944c0999de2
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-height-002-ref.xht
@@ -0,0 +1,29 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+
+ <head>
+
+ <title>CSS Reftest Reference</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+
+ <style type="text/css"><![CDATA[
+ div
+ {
+ border: black solid medium;
+ height: 200px;
+ width: 200px;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if a filled blue square is in the <strong>upper-left corner</strong> of an hollow black square and if there is <strong>no red</strong>.</p>
+
+ <div><img src="support/blue15x15.png" width="100" height="100" alt="Image download support must be enabled" /></div>
+
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002-ref.xht
new file mode 100644
index 00000000000..e88ac809839
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002-ref.xht
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+
+ <head>
+
+ <title>CSS Reftest Reference</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
+
+ <style type="text/css"><![CDATA[
+ div
+ {
+ border: black solid medium;
+ height: 200px;
+ padding-left: 100px;
+ width: 100px;
+ }
+ ]]></style>
+
+ </head>
+
+ <body>
+
+ <p>Test passes if a filled blue square is in the <strong>upper-right corner</strong> of an hollow black square and if there is <strong>no red</strong>.</p>
+
+ <div><img src="support/blue15x15.png" width="100" height="100" alt="Image download support must be enabled" /></div>
+
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/support/blue15x15.png b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/support/blue15x15.png
new file mode 100644
index 00000000000..89de32fdb8a
Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/positioning/support/blue15x15.png differ
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-001.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-001.xht
new file mode 100644
index 00000000000..057c7860a48
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-001.xht
@@ -0,0 +1,40 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Solving for 'right' on absolutely positioned non-replaced elements</title>
+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
+ <link rel="reviewer" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" /> <!-- 2012-08-31 -->
+ <link rel="help" href="http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width" />
+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-height-002-ref.xht" />
+
+ <meta name="flags" content="ahem" />
+ <meta name="assert" content="When direction is left-to-right and 'left', 'width' and 'right' are 'auto', the width becomes shrink-to-fit and then solve for 'right'." />
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+ <style type="text/css">
+ #div1
+ {
+ border: solid black;
+ direction: ltr;
+ height: 200px;
+ position: relative;
+ width: 200px;
+ }
+ div div
+ {
+ background: red;
+ color: blue;
+ font: 100px/1 Ahem;
+ left: auto;
+ position: absolute;
+ right: auto;
+ width: auto;
+ }
+ </style>
+ </head>
+ <body>
+ <p>Test passes if a filled blue square is in the <strong>upper-left corner</strong> of an hollow black square and if there is <strong>no red</strong>.</p>
+ <div id="div1">
+ <div>X</div>
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002.xht
new file mode 100644
index 00000000000..c3bd4f447d7
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002.xht
@@ -0,0 +1,40 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Solving for 'left' on absolutely positioned non-replaced elements</title>
+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
+ <link rel="reviewer" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" /> <!-- 2012-08-31 -->
+ <link rel="help" href="http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width" />
+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/positioning/absolute-non-replaced-width-002-ref.xht" />
+
+ <meta name="flags" content="ahem" />
+ <meta name="assert" content="When direction is right-to-left and 'left', 'width' and 'right' are 'auto', then set 'right' to the static position, then width becomes shrink-to-fit and then solve for 'left'." />
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+ <style type="text/css">
+ #div1
+ {
+ border: solid black;
+ direction: rtl;
+ height: 200px;
+ position: relative;
+ width: 200px;
+ }
+ div div
+ {
+ background: red;
+ color: blue;
+ font: 100px/1em Ahem;
+ left: auto;
+ position: absolute;
+ right: auto;
+ width: auto;
+ }
+ </style>
+ </head>
+ <body>
+ <p>Test passes if a filled blue square is in the <strong>upper-right corner</strong> of an hollow black square and if there is <strong>no red</strong>.</p>
+ <div id="div1">
+ <div>X</div>
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/abspos-static-position-available-size.txt b/Tests/LibWeb/Text/expected/abspos-static-position-available-size.txt
new file mode 100644
index 00000000000..6ade35f05f0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-static-position-available-size.txt
@@ -0,0 +1,22 @@
+horizontal-tb ltr: 200,0 200x40
+horizontal-tb rtl: 0,0 200x40
+vertical-rl ltr: 60,200 40x200
+vertical-rl rtl: 60,0 40x200
+vertical-lr ltr: 0,200 40x200
+vertical-lr rtl: 0,0 40x200
+opposing child direction: 200,0 200x40
+margins and edges: 210,0 180x70
+explicit start inset: 10,0 280x20
+explicit end inset: 110,0 280x20
+maximum width: 200,0 150x60
+minimum width: 200,0 250x40
+unbreakable content: 200,0 280x20
+containing block padding: 225,25 220x40
+nested parent offset: 200,0 200x40
+end-aligned parent: 400,0 80x60
+orthogonal vertical-rl: 0,200 40x200
+orthogonal vertical-lr: 0,200 40x200
+orthogonal horizontal: 0,0 200x40
+content-based block size: 0,80 80x60
+after containing block resize: 100,0 100x60
+after static position moves: 0,0 280x20
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-htb-vrl.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-htb-vrl.txt
index 49817cbea26..989ab518f1c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-htb-vrl.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-htb-vrl.txt
@@ -2,11 +2,11 @@ Harness status: OK
Found 12 tests
-6 Pass
-6 Fail
+7 Pass
+5 Fail
Pass .item 1
Pass .item 2
-Fail .item 3
+Pass .item 3
Pass .item 4
Pass .item 5
Pass .item 6
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-vrl-htb.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-vrl-htb.txt
index 49817cbea26..989ab518f1c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-vrl-htb.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-align/abspos/stretch-intrinsic-size-vrl-htb.txt
@@ -2,11 +2,11 @@ Harness status: OK
Found 12 tests
-6 Pass
-6 Fail
+7 Pass
+5 Fail
Pass .item 1
Pass .item 2
-Fail .item 3
+Pass .item 3
Pass .item 4
Pass .item 5
Pass .item 6
diff --git a/Tests/LibWeb/Text/input/abspos-static-position-available-size.html b/Tests/LibWeb/Text/input/abspos-static-position-available-size.html
new file mode 100644
index 00000000000..84a47b90fb4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-static-position-available-size.html
@@ -0,0 +1,95 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../Ref/data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ font: 20px/20px Ahem;
+ }
+ .containing-block {
+ position: relative;
+ inline-size: 400px;
+ block-size: 100px;
+ }
+ .parent {
+ text-align: center;
+ }
+ .target {
+ position: absolute;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ const cases = [];
+ function add(name, containerStyle = "", targetStyle = "", parentStyle = "", before = "") {
+ const container = document.createElement("div");
+ container.className = "containing-block";
+ container.style.cssText = containerStyle;
+ const parent = document.createElement("div");
+ parent.className = "parent";
+ parent.style.cssText = parentStyle;
+ parent.innerHTML = before;
+ const target = document.createElement("span");
+ target.className = "target";
+ target.style.cssText = targetStyle;
+ target.textContent = "XXXX XXXX XXXX";
+ parent.append(target);
+ container.append(parent);
+ document.body.append(container);
+ cases.push([name, container, target]);
+ }
+ for (const mode of ["horizontal-tb", "vertical-rl", "vertical-lr"]) {
+ for (const direction of ["ltr", "rtl"]) {
+ add(`${mode} ${direction}`, `writing-mode: ${mode}; direction: ${direction}`);
+ }
+ }
+ add("opposing child direction", "", "direction: rtl");
+ add("margins and edges", "", "margin-inline: 10px; padding-inline: 10px; border: 5px solid");
+ add("explicit start inset", "", "left: 10px");
+ add("explicit end inset", "", "right: 10px");
+ add("maximum width", "", "max-width: 150px");
+ add("minimum width", "", "min-width: 250px");
+ add("unbreakable content", "", "white-space: nowrap");
+ add("containing block padding", "padding: 20px; border: 5px solid");
+ add("nested parent offset", "", "", "margin-left: 100px; width: 200px");
+ add("end-aligned parent", "", "", "text-align: right");
+ add(
+ "orthogonal vertical-rl",
+ "height: 400px",
+ "writing-mode: vertical-rl",
+ "text-align: left",
+ '<div style="height: 200px"></div>'
+ );
+ add(
+ "orthogonal vertical-lr",
+ "height: 400px",
+ "writing-mode: vertical-lr",
+ "text-align: left",
+ '<div style="height: 200px"></div>'
+ );
+ add(
+ "orthogonal horizontal",
+ "writing-mode: vertical-rl; width: 400px",
+ "writing-mode: horizontal-tb",
+ "text-align: start",
+ '<div style="width: 200px"></div>'
+ );
+ add("content-based block size", "", "width: 80px", "text-align: left", '<div style="height: 80px"></div>');
+ await document.fonts.ready;
+ function report(name, container, target) {
+ const outer = container.getBoundingClientRect();
+ const rect = target.getBoundingClientRect();
+ println(`${name}: ${rect.x - outer.x},${rect.y - outer.y} ${rect.width}x${rect.height}`);
+ }
+ for (const entry of cases) report(...entry);
+ const [name, container, target] = cases[0];
+ container.style.width = "200px";
+ report("after containing block resize", container, target);
+ container.style.width = "400px";
+ target.parentElement.style.textAlign = "left";
+ report("after static position moves", container, target);
+ });
+</script>