Skip to content

Truly independent web browser

LibWeb: Keep anchor-centered form controls at their natural size

sideshowbarkerb71079f102c9
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index c39b066092f..e5688583ed7 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1909,7 +1909,13 @@ impl AbsposEngine<'_> {
/// fill the space between them.
/// https://drafts.csswg.org/css-position-3/#abspos-auto-size
fn sizes_to_fit_content(&self, sized: OutOfFlowBox<'_>, equation: AxisEquation, inline_axis: bool) -> bool {
- !equation.has_both_insets() || self.own_self_alignment_keyword(sized, inline_axis).is_some()
+ let style = *self.style_of(sized);
+ let (keyword, _) = if inline_axis {
+ SelfKeyword::justify_self_of(style, None)
+ } else {
+ SelfKeyword::align_self_of(style, None)
+ };
+ !equation.has_both_insets() || !matches!(keyword, SelfKeyword::Normal | SelfKeyword::Stretch)
}
// https://drafts.csswg.org/css-position-3/#abspos-auto-size
@@ -2721,8 +2727,8 @@ impl<'pass> AbsposEngine<'pass> {
}
}
- /// The keyword and safety of the box's own self-alignment in an axis of its containing block's frame, or None where
- /// the box fills its inset-modified containing block rather than take its fit-content size there.
+ /// The keyword and safety for ordinary self-alignment in an axis of the containing block's frame. Anchor-center falls
+ /// back to center when no anchor placement is supplied. Stretch sizing is handled separately from self-alignment.
/// https://drafts.csswg.org/css-align-3/#justify-abspos
/// https://drafts.csswg.org/css-align-3/#align-abspos
fn own_self_alignment_keyword(
@@ -2736,11 +2742,7 @@ impl<'pass> AbsposEngine<'pass> {
} else {
SelfKeyword::align_self_of(style, None)
};
- (!matches!(
- keyword,
- SelfKeyword::Normal | SelfKeyword::Stretch | SelfKeyword::AnchorCenter
- ))
- .then_some((keyword, safety))
+ (!matches!(keyword, SelfKeyword::Normal | SelfKeyword::Stretch)).then_some((keyword, safety))
}
/// The box's own self-alignment in an axis of its containing block's frame, where it takes its fit-content size in
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/anchor-center-no-default-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/anchor-center-no-default-ref.html
new file mode 100644
index 00000000000..0bd08fc0556
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/anchor-center-no-default-ref.html
@@ -0,0 +1,3 @@
+<!DOCTYPE html>
+<title>CSS Test Reference</title>
+<div style="position:absolute;align-self:center">This text should not be centered in the viewport</div>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/no-anchor-anchor-center-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/no-anchor-anchor-center-ref.html
new file mode 100644
index 00000000000..0dfdc0272ef
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-anchor-position/no-anchor-anchor-center-ref.html
@@ -0,0 +1,30 @@
+<!doctype html>
+<title>Tests that 'anchor-center' behaves as 'center' when no anchor in containing block</title>
+<style>
+.anchor {
+ background: orange;
+ width: 100px;
+ background-color: orange;
+}
+.container {
+ position: fixed;
+ top: 20px;
+ left: 50px;
+ width: 300px;
+ height: 200px;
+ display: flex;
+ background: green;
+ justify-content: center;
+}
+.target {
+ position: absolute;
+ margin: 0 auto;
+ width: 80px;
+ border: 1px #000 solid;
+ background: lime;
+}
+</style>
+<div class="anchor">anchor</div>
+<div class="container">
+ <div class="target">target 1</div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/anchor-center-no-default.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/anchor-center-no-default.html
new file mode 100644
index 00000000000..38c4e94ef27
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/anchor-center-no-default.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<title>CSS Anchor Positioning: 'anchor-center' behaves as 'center' with no default anchor</title>
+<link rel="help" href="https://drafts.csswg.org/css-anchor-position-1/#anchor-center">
+<link rel="match" href="../../../../expected/wpt-import/css/css-anchor-position/anchor-center-no-default-ref.html">
+<style>
+ #centered {
+ position: absolute;
+ align-self: anchor-center;
+ }
+</style>
+<div id="centered">This text should not be centered in the viewport</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/no-anchor-anchor-center.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/no-anchor-anchor-center.html
new file mode 100644
index 00000000000..c66326dff57
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-anchor-position/no-anchor-anchor-center.html
@@ -0,0 +1,35 @@
+<!doctype html>
+<title>Tests that 'anchor-center' behaves as 'center' when no anchor in containing block</title>
+<link rel="match" href="../../../../expected/wpt-import/css/css-anchor-position/no-anchor-anchor-center-ref.html">
+<link rel="help" href="https://drafts.csswg.org/css-anchor-position-1/#valdef-justify-self-anchor-center">
+<style>
+.anchor {
+ anchor-name: --dropdownAnchor;
+ background: orange;
+ width: 100px;
+ background-color: orange;
+}
+.container {
+ position: fixed;
+ top: 20px;
+ left: 50px;
+ width: 300px;
+ height: 200px;
+ background: green;
+}
+.target {
+ position-anchor: --dropdownAnchor;
+ display: flow;
+ left: 10px;
+ right: 10px;
+ position: absolute;
+ justify-self: anchor-center;
+ width: 80px;
+ border: 1px #000 solid;
+ background: lime;
+}
+</style>
+<div class="anchor">anchor</div>
+<div class="container">
+ <div class="target">target 1</div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/abspos-anchor-center-form-controls.txt b/Tests/LibWeb/Text/expected/abspos-anchor-center-form-controls.txt
new file mode 100644
index 00000000000..43ed5ebd15c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-anchor-center-form-controls.txt
@@ -0,0 +1,16 @@
+input inline: natural size true
+input inline: centered true
+input inline, missing anchor: natural size true
+input inline, missing anchor: centered true
+input block: natural size true
+input block: centered true
+input block, missing anchor: natural size true
+input block, missing anchor: centered true
+textarea inline: natural size true
+textarea inline: centered true
+textarea inline, missing anchor: natural size true
+textarea inline, missing anchor: centered true
+textarea block: natural size true
+textarea block: centered true
+textarea block, missing anchor: natural size true
+textarea block, missing anchor: centered true
diff --git a/Tests/LibWeb/Text/input/abspos-anchor-center-form-controls.html b/Tests/LibWeb/Text/input/abspos-anchor-center-form-controls.html
new file mode 100644
index 00000000000..3bf16a79134
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-anchor-center-form-controls.html
@@ -0,0 +1,73 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ .container {
+ position: relative;
+ width: 400px;
+ height: 160px;
+ }
+ .container::before {
+ content: "";
+ display: block;
+ height: 20px;
+ }
+ .anchor {
+ width: 40px;
+ height: 40px;
+ margin-left: 80px;
+ }
+ input,
+ textarea {
+ position: absolute;
+ margin: 0;
+ padding: 0;
+ border: 0;
+ font: 20px/1 monospace;
+ left: 0;
+ top: 0;
+ }
+</style>
+<script>
+ test(() => {
+ for (const tag of ["input", "textarea"]) {
+ for (const axis of ["inline", "block"]) {
+ const container = document.createElement("div");
+ container.className = "container";
+ const anchor = document.createElement("div");
+ anchor.className = "anchor";
+ anchor.style.anchorName = `--${tag}-${axis}`;
+ const reference = document.createElement(tag);
+ reference.setAttribute("size", "4");
+ reference.setAttribute("cols", "4");
+ reference.setAttribute("rows", "2");
+ const target = reference.cloneNode();
+ target.style.positionAnchor = anchor.style.anchorName;
+ if (axis === "inline") {
+ target.style.justifySelf = "anchor-center";
+ target.style.right = "0";
+ } else {
+ target.style.alignSelf = "anchor-center";
+ target.style.bottom = "0";
+ }
+ container.append(anchor, reference, target);
+ document.body.append(container);
+ const natural = reference.getBoundingClientRect();
+ const actual = target.getBoundingClientRect();
+ const anchorRect = anchor.getBoundingClientRect();
+ const dimension = axis === "inline" ? "width" : "height";
+ const position = axis === "inline" ? "x" : "y";
+ println(`${tag} ${axis}: natural size ${actual[dimension] === natural[dimension]}`);
+ println(
+ `${tag} ${axis}: centered ${actual[position] + actual[dimension] / 2 === anchorRect[position] + anchorRect[dimension] / 2}`
+ );
+ target.style.positionAnchor = "--missing";
+ const fallback = target.getBoundingClientRect();
+ const containerRect = container.getBoundingClientRect();
+ println(`${tag} ${axis}, missing anchor: natural size ${fallback[dimension] === natural[dimension]}`);
+ println(
+ `${tag} ${axis}, missing anchor: centered ${fallback[position] + fallback[dimension] / 2 === containerRect[position] + containerRect[dimension] / 2}`
+ );
+ }
+ }
+ });
+</script>