Skip to content

Truly independent web browser

LibWeb: Limit abspos widths around centered static positions

sideshowbarkeree3d41ecfee6
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 37c0afbd650..c39b066092f 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1926,8 +1926,13 @@ impl AbsposEngine<'_> {
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 placement = axis.select(
+ inputs.containing_block_info.inline_placement,
+ inputs.containing_block_info.block_placement,
+ );
+ if equation.start.is_none()
+ && equation.end.is_none()
+ && placement.mode == abspos_inputs::AbsposAxisMode::StaticPosition
{
let static_position = static_position_rect_for_sizing(inputs);
let rect = static_position.rect;
@@ -1944,8 +1949,11 @@ impl AbsposEngine<'_> {
- axis.select(rect.size.inline_size, rect.size.block_size),
);
}
- // FIXME: Implement the center alignment case.
- AxisEdge::Center => {}
+ AxisEdge::Center => {
+ let center = offset + axis.select(rect.size.inline_size, rect.size.block_size) / 2;
+ equation.start = Some((center * 2 - equation.containing_block_size).max(CssPixels::default()));
+ equation.end = Some((equation.containing_block_size - center * 2).max(CssPixels::default()));
+ }
}
}
equation.stretch_fit_size()
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-001.html
new file mode 100644
index 00000000000..aab01d1fb6c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-001.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/4659">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#abspos-sizing">
+<link rel="match" href="../../../../expected/wpt-import/css/css-position/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert" content="The available space for a box with center alignment is double the distance between the center of the static position rectangle and the closest edge of the containing block in the relevant axis.">
+<style>
+#containing-block {
+ position: relative;
+ width: 100px;
+ height: 100px;
+ background: red;
+ display: flex;
+}
+
+#containing-block > div {
+ flex-grow: 1;
+}
+
+#inner-flex {
+ display: flex;
+ justify-content: center;
+}
+
+span {
+ display: inline-block;
+ inline-size: 50px;
+ block-size: 10px;
+}
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div id="containing-block" style="flex-direction: row;">
+ <div id="inner-flex" style="margin: 10px; height: 100px;">
+ <div style="position: absolute; top: 0; height: 100px; background: green;">
+ <span></span>
+ <span></span>
+ </div>
+ </div>
+ <div style="height: 100px; background: green;"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-002.html
new file mode 100644
index 00000000000..344d14b2acf
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-position/position-absolute-center-002.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/4659">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#abspos-sizing">
+<link rel="match" href="../../../../expected/wpt-import/css/css-position/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert" content="The available space for a box with center alignment is double the distance between the center of the static position rectangle and the closest edge of the containing block in the relevant axis.">
+<style>
+#containing-block {
+ position: relative;
+ width: 100px;
+ height: 100px;
+ background: red;
+ display: flex;
+}
+
+#containing-block > div {
+ flex-grow: 1;
+}
+
+#inner-flex {
+ display: flex;
+ align-items: center;
+}
+
+span {
+ display: inline-block;
+ inline-size: 50px;
+ block-size: 10px;
+}
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div id="containing-block" style="flex-direction: column;">
+ <div id="inner-flex" style="margin: 10px; width: 100px;">
+ <div style="position: absolute; left: 0; width: 100px; background: green; writing-mode: vertical-rl;">
+ <span></span>
+ <span></span>
+ </div>
+ </div>
+ <div style="width: 100px; background: green;"></div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/abspos-centered-static-position-width.txt b/Tests/LibWeb/Text/expected/abspos-centered-static-position-width.txt
new file mode 100644
index 00000000000..4d37ac224f7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-centered-static-position-width.txt
@@ -0,0 +1,10 @@
+ltr offset 0: width 160, left 0
+ltr offset 70: width 240, left 30
+ltr offset 140: width 160, left 140
+rtl offset 0: width 160, left 140
+rtl offset 70: width 240, left 30
+rtl offset 140: width 160, left 0
+ltr grid column 1: width 100, left 0
+ltr grid column 2: width 150, left 100
+rtl grid column 1: width 100, left 200
+rtl grid column 2: width 150, left 50
diff --git a/Tests/LibWeb/Text/input/abspos-centered-static-position-width.html b/Tests/LibWeb/Text/input/abspos-centered-static-position-width.html
new file mode 100644
index 00000000000..78bf69b1aa8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-centered-static-position-width.html
@@ -0,0 +1,66 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ .container {
+ position: relative;
+ width: 300px;
+ height: 100px;
+ }
+ .flex {
+ display: flex;
+ justify-content: center;
+ width: 160px;
+ height: 60px;
+ }
+ .box {
+ position: absolute;
+ font-size: 0;
+ line-height: 0;
+ }
+ .word {
+ display: inline-block;
+ width: 40px;
+ height: 20px;
+ }
+</style>
+<script>
+ test(() => {
+ for (const direction of ["ltr", "rtl"]) {
+ for (const offset of [0, 70, 140]) {
+ const container = document.createElement("div");
+ container.className = "container";
+ container.style.direction = direction;
+ const flex = document.createElement("div");
+ flex.className = "flex";
+ flex.style.marginInlineStart = `${offset}px`;
+ const box = document.createElement("div");
+ box.className = "box";
+ box.innerHTML = '<span class="word"></span><wbr>'.repeat(6);
+ flex.append(box);
+ container.append(flex);
+ document.body.append(container);
+ const rect = box.getBoundingClientRect();
+ println(
+ `${direction} offset ${offset}: width ${rect.width}, left ${rect.x - container.getBoundingClientRect().x}`
+ );
+ }
+ }
+ for (const direction of ["ltr", "rtl"]) {
+ for (const column of [1, 2]) {
+ const grid = document.createElement("div");
+ grid.className = "container";
+ grid.style.cssText = `display: grid; direction: ${direction}; grid-template-columns: 100px 150px; grid-template-rows: 100px`;
+ const box = document.createElement("div");
+ box.className = "box";
+ box.style.cssText = `grid-column: ${column} / ${column + 1}; justify-self: center`;
+ box.innerHTML = '<span class="word"></span><wbr>'.repeat(6);
+ grid.append(box);
+ document.body.append(grid);
+ const rect = box.getBoundingClientRect();
+ println(
+ `${direction} grid column ${column}: width ${rect.width}, left ${rect.x - grid.getBoundingClientRect().x}`
+ );
+ }
+ }
+ });
+</script>