Skip to content

Truly independent web browser

LibWeb: Resolve a grid item’s margins without its alignment offset

sideshowbarkerdd1b377c965b
diff --git a/Libraries/LibWeb/Rust/src/css/resolved_value.rs b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
index 7c08c125915..2cd9c2577b9 100644
--- a/Libraries/LibWeb/Rust/src/css/resolved_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
@@ -517,6 +517,19 @@ impl ResolvedValueBox<'_> {
self.principal_style.position(),
positioning::ABSOLUTE | positioning::FIXED
);
+ // NB: Grid layout aligns an item in its grid area through the item's used margins, which then hold the offset
+ // of its alignment, and the space its auto margins absorb, as well as its margins. So an auto margin of a
+ // grid item resolves to zero, and a length to itself, as other engines report them.
+ // FIXME: A percentage resolves against the item's grid area, which isn't known here, so it still resolves to
+ // the used value, alignment and all.
+ if !is_absolutely_positioned && containing_block_style.is_some_and(|style| style.display().is_grid_inside()) {
+ if margin.is_auto {
+ return length_value(0.0);
+ }
+ if let Some(pixels) = handle_value(&margin.value).and_then(absolute_length_px) {
+ return length_value(pixels);
+ }
+ }
if !self.principal_style.display().is_block_outside()
|| is_absolutely_positioned
|| containing_block_style.is_some_and(|style| {
diff --git a/Tests/LibWeb/Text/expected/css/getComputedStyle-margins-of-aligned-grid-items.txt b/Tests/LibWeb/Text/expected/css/getComputedStyle-margins-of-aligned-grid-items.txt
new file mode 100644
index 00000000000..6cf87bea3b8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/getComputedStyle-margins-of-aligned-grid-items.txt
@@ -0,0 +1,4 @@
+centered: 0px 0px 0px 0px
+end-aligned: 0px 0px 0px 0px
+with-margins: 5px 7px 5px 7px
+auto-margins: 0px 0px 0px 0px
diff --git a/Tests/LibWeb/Text/input/css/getComputedStyle-margins-of-aligned-grid-items.html b/Tests/LibWeb/Text/input/css/getComputedStyle-margins-of-aligned-grid-items.html
new file mode 100644
index 00000000000..c15ecea10e9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/getComputedStyle-margins-of-aligned-grid-items.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+.grid {
+ display: grid;
+ width: 300px;
+ height: 200px;
+}
+
+.item {
+ width: 100px;
+ height: 50px;
+}
+</style>
+<div class="grid" style="place-items: center"><div class="item" id="centered"></div></div>
+<div class="grid" style="justify-items: end; align-items: end"><div class="item" id="end-aligned"></div></div>
+<div class="grid" style="place-items: center"><div class="item" id="with-margins" style="margin: 5px 7px"></div></div>
+<div class="grid"><div class="item" id="auto-margins" style="margin: auto"></div></div>
+<script>
+ // The margins of a grid item are its own, wherever its alignment puts it in its grid area.
+ test(() => {
+ for (const id of ["centered", "end-aligned", "with-margins", "auto-margins"]) {
+ const style = getComputedStyle(document.getElementById(id));
+ println(`${id}: ${style.marginTop} ${style.marginRight} ${style.marginBottom} ${style.marginLeft}`);
+ }
+ });
+</script>