Skip to content

Truly independent web browser

LibWeb: Resolve abspos replaced margins against the inline size

sideshowbarker171acc222276
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 2e9791bf62b..c9213158614 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -2371,19 +2371,14 @@ impl AbsposEngine<'_> {
let sizing = self.sizing();
let is_replaced = sizing.box_is_sized_as_replaced_element(sized.sized_box(), available_space, constraints);
let containing_block_block_size = available_space.block_size.to_px_or_zero();
- // NB: The margins of a non-replaced box resolve against the inline size of its containing block, and those of a
- // replaced box against its block size.
- let margin_basis = if is_replaced && !self.facts(sized.node).is_native_form_control_box() {
- containing_block_block_size
- } else {
- available_space.inline_size.to_px_or_zero()
- };
+ // https://drafts.csswg.org/css-box-4/#margin-physical
+ // Percentages refer to the logical width of the containing block.
let equation = self.axis_equation(
sized,
inputs,
SizingAxis::Block,
containing_block_block_size,
- margin_basis,
+ available_space.inline_size.to_px_or_zero(),
auto_margin_overflow,
);
let is_replaced = is_replaced && !self.form_control_stretches(sized, equation, SizingAxis::Block);
diff --git a/Tests/LibWeb/Text/expected/abspos-percentage-margin-basis.txt b/Tests/LibWeb/Text/expected/abspos-percentage-margin-basis.txt
new file mode 100644
index 00000000000..5ea35a0ab29
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-percentage-margin-basis.txt
@@ -0,0 +1,8 @@
+img top: 0; left: 0: 30,30 20x10
+img inset: 0: 30,30 20x10
+canvas top: 0; left: 0: 30,30 20x10
+input type=checkbox top: 0; left: 0: 30,30 13x13
+input top: 0; left: 0; width: 20px: 30,30 30x20
+div top: 0; left: 0; width: 20px; height: 10px: 30,30 20x10
+img bottom: 0; right: 0: 250,10 20x10
+img top: 0; left: 0; writing-mode: vertical-lr: 30,30 20x10
diff --git a/Tests/LibWeb/Text/input/abspos-percentage-margin-basis.html b/Tests/LibWeb/Text/input/abspos-percentage-margin-basis.html
new file mode 100644
index 00000000000..28ad7b58826
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-percentage-margin-basis.html
@@ -0,0 +1,52 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ .container {
+ position: relative;
+ width: 300px;
+ height: 50px;
+ }
+ .container > * {
+ position: absolute;
+ margin: 10%;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ const image = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"></svg>';
+ const cases = [
+ ["img", "top: 0; left: 0"],
+ ["img", "inset: 0"],
+ ["canvas", "top: 0; left: 0"],
+ ["input type=checkbox", "top: 0; left: 0"],
+ ["input", "top: 0; left: 0; width: 20px"],
+ ["div", "top: 0; left: 0; width: 20px; height: 10px"],
+ ["img", "bottom: 0; right: 0"],
+ ["img", "top: 0; left: 0; writing-mode: vertical-lr"],
+ ];
+ for (const [tag, style] of cases) {
+ const container = document.createElement("div");
+ container.className = "container";
+ const [name, type] = tag.split(" type=");
+ const element = document.createElement(name);
+ if (type) element.type = type;
+ element.style.cssText = style;
+ if (name === "canvas") {
+ element.width = 20;
+ element.height = 10;
+ }
+ container.append(element);
+ document.body.append(container);
+ if (name === "img") {
+ await new Promise(resolve => {
+ element.onload = resolve;
+ element.src = image;
+ });
+ }
+ const outer = container.getBoundingClientRect();
+ const rect = element.getBoundingClientRect();
+ println(`${tag} ${style}: ${rect.x - outer.x},${rect.y - outer.y} ${rect.width}x${rect.height}`);
+ container.remove();
+ }
+ });
+</script>