Skip to content

Truly independent web browser

LibWeb: Size subgrid edge tracks to fit the subgrid's box edges

Andreas Klinge67170c1cada
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 1eba98b589c..04bf3cc0fd1 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -3754,6 +3754,39 @@ impl<'pass> GridFormattingContext<'pass> {
context.items = items;
let mut contributions = context.item_contributions_to_track_sizing(axis);
+ // https://drafts.csswg.org/css-grid-2/#subgrid-edge-placeholders
+ // For each edge of a non-empty subgrid, to account for the subgrid's margin/border/padding (and any
+ // scrollbar gutter) at that edge, a hypothetical item is contributed to the track sizing algorithm for each
+ // span size in the set of items spanning into the occupied track closest to that edge of the subgrid.
+ // INTEROP: Like other engines, we contribute one hypothetical item to each edge track of every subgrid,
+ // empty or not, sized to the subgrid's margin/border/padding at that edge, so that the edge tracks
+ // fit them even when no item is placed in them.
+ if let Some((start_margin, end_margin)) = context.subgrid_edge_extra_margins(axis) {
+ let track_count = subgrid.span(axis);
+ let hypothetical_item = |track: usize, size: CssPixels| ItemContribution {
+ spanned_tracks: Self::interleaved_index_of_track(track)
+ ..Self::interleaved_index_of_track(track) + 1,
+ span: 1,
+ minimum: size,
+ min_content: size,
+ limited_min_content: size,
+ max_content: size,
+ limited_max_content: size,
+ };
+ if track_count == 1 {
+ let size = start_margin + end_margin;
+ if size > CssPixels::default() {
+ contributions.push(hypothetical_item(0, size));
+ }
+ } else if track_count > 1 {
+ if start_margin > CssPixels::default() {
+ contributions.push(hypothetical_item(0, start_margin));
+ }
+ if end_margin > CssPixels::default() {
+ contributions.push(hypothetical_item(track_count - 1, end_margin));
+ }
+ }
+ }
let interleaved_index_offset_in_parent =
Self::interleaved_index_of_track(subgrid.position(axis).max(0) as usize);
// NB: A subgrid that runs against its parent grid numbers the tracks it shares with it from the other end.
diff --git a/Tests/LibWeb/Layout/expected/grid/subgrid-row-contribution-uses-resolved-fr-column-width.txt b/Tests/LibWeb/Layout/expected/grid/subgrid-row-contribution-uses-resolved-fr-column-width.txt
index f623cad7659..8b175ba0fed 100644
--- a/Tests/LibWeb/Layout/expected/grid/subgrid-row-contribution-uses-resolved-fr-column-width.txt
+++ b/Tests/LibWeb/Layout/expected/grid/subgrid-row-contribution-uses-resolved-fr-column-width.txt
@@ -1,8 +1,8 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [1,1] [0+1+0 798 0+1+0] [0+1+0 63 0+1+0] [BFC] children: not-inline
- BlockContainer <body> at [2,2] [0+1+0 796 0+1+0] [0+1+0 61 0+1+0] children: not-inline
- Box <div.outer> at [3,3] [0+1+0 766 0+1+28] [0+1+0 59 0+1+0] [GFC] children: not-inline
- Box <div.card> at [4,4] [0+1+0 245.328125 0+1+0] [0+1+0 57 0+1+0] [GFC] children: not-inline
+ BlockContainer <html> at [1,1] [0+1+0 798 0+1+0] [0+1+0 64 0+1+0] [BFC] children: not-inline
+ BlockContainer <body> at [2,2] [0+1+0 796 0+1+0] [0+1+0 62 0+1+0] children: not-inline
+ Box <div.outer> at [3,3] [0+1+0 766 0+1+28] [0+1+0 60 0+1+0] [GFC] children: not-inline
+ Box <div.card> at [4,4] [0+1+0 245.328125 0+1+0] [0+1+0 58 0+1+0] [GFC] children: not-inline
BlockContainer <p> at [5,5] [0+1+0 243.328125 0+1+0] [0+1+0 32 0+1+0] [BFC] children: inline
frag 0 from TextNode start: 0, length: 26, rect: [5,5 222.375x16] baseline: 13
"Some long text that should"
@@ -11,4 +11,4 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [1,1 798x63] (z-index: auto)
+ SC for BlockContainer<HTML> [1,1 798x64] (z-index: auto)
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/grid-gap-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/grid-gap-002-ref.html
new file mode 100644
index 00000000000..7bed37d4738
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/grid-gap-002-ref.html
@@ -0,0 +1,65 @@
+<!DOCTYPE HTML>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html><head>
+ <meta charset="utf-8">
+ <title>CSS Grid Test: subgrid with border-bottom</title>
+ <link rel="author" title="Mats Palmgren" href="mailto:[email protected]">
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css">
+ <style>
+html,body {
+ color:black; background-color:white; font:24px/1 Ahem; padding:0; margin:0;
+}
+
+.grid {
+ display: grid;
+ grid: repeat(4, auto) / repeat(5, auto);
+ place-content: start;
+ border: 1px solid;
+}
+
+.subgrid {
+ display: grid;
+ grid: subgrid / auto;
+ gap: 10px 20px;
+ background: lightgrey;
+ grid-column: 2 / span 5;
+ grid-row: 2 / span 5;
+ min-width:10px;
+ min-height:0;
+ background: yellow;
+}
+
+.subgrid > * { background: lightgrey; }
+
+x {
+ min-width:10px;
+ min-height:0px;
+}
+x:nth-child(2n+1) { background: silver; }
+x:nth-child(2n+2) { background: magenta; }
+x:nth-child(2n+3) { background: pink; }
+x:nth-child(2n+4) { background: grey; }
+
+e {
+ border: 0 solid lightblue;
+ border-bottom-width: 40px;
+ margin: -5px 0;
+}
+ </style>
+</head>
+<body>
+
+<div class="grid">
+<x style="grid-row:1"></x><x style="grid-row:2"></x><x style="grid-row:3"></x><x style="grid-row:4"></x><x style="grid-row:5"></x><x style="grid-row:6"></x>
+<div class="subgrid">
+<c>c</c>
+<d>d</d>
+<e></e>
+</div>
+</div>
+
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/grid-gap-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/grid-gap-002.html
new file mode 100644
index 00000000000..0af0e470210
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/grid-gap-002.html
@@ -0,0 +1,63 @@
+<!DOCTYPE HTML>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html><head>
+ <meta charset="utf-8">
+ <title>CSS Grid Test: subgrid with border-bottom</title>
+ <link rel="author" title="Mats Palmgren" href="mailto:[email protected]">
+ <link rel="help" href="https://drafts.csswg.org/css-grid-2">
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css">
+ <link rel="match" href="../../../../../expected/wpt-import/css/css-grid/subgrid/grid-gap-002-ref.html">
+ <style>
+html,body {
+ color:black; background-color:white; font:24px/1 Ahem; padding:0; margin:0;
+}
+
+.grid {
+ display: grid;
+ grid: repeat(4, auto) / repeat(5, auto);
+ place-content: start;
+ border: 1px solid;
+}
+
+.subgrid {
+ display: grid;
+ grid: subgrid / auto;
+ gap: 10px 20px;
+ background: lightgrey;
+ grid-column: 2 / span 5;
+ grid-row: 2 / span 5;
+ min-width:10px;
+ min-height:0;
+ border: 0 solid lightblue;
+ border-bottom-width: 40px;
+ background: yellow;
+}
+
+.subgrid > * { background: lightgrey; }
+
+x {
+ min-width:10px;
+ min-height:0px;
+}
+x:nth-child(2n+1) { background: silver; }
+x:nth-child(2n+2) { background: magenta; }
+x:nth-child(2n+3) { background: pink; }
+x:nth-child(2n+4) { background: grey; }
+
+ </style>
+</head>
+<body>
+
+<div class="grid">
+<x style="grid-row:1"></x><x style="grid-row:2"></x><x style="grid-row:3"></x><x style="grid-row:4"></x><x style="grid-row:5"></x><x style="grid-row:6"></x>
+<div class="subgrid">
+<c>c</c>
+<d>d</d>
+</div>
+</div>
+
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/subgrid-no-items-on-edges-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/subgrid-no-items-on-edges-001.html
new file mode 100644
index 00000000000..50e391df7cc
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/subgrid-no-items-on-edges-001.html
@@ -0,0 +1,47 @@
+<!DOCTYPE HTML>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html>
+<head>
+<meta charset="utf-8">
+<title>CSS Grid Test: Subgrid with no grid items accommodates border and padding</title>
+<link rel="author" title="Ethan Jimenez" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-grid-2/#subgrid-edge-placeholders">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-grid/subgrid/../../reference/ref-filled-green-100px-square.xht">
+<style>
+.wrapper {
+ width: 100px;
+ height: 100px;
+ background: red;
+}
+.grid {
+ display: inline-grid;
+ grid: repeat(3, auto) / repeat(5, auto);
+ background: red;
+}
+.subgrid {
+ display: grid;
+ grid-row: 1 / -1;
+ grid-column: 1 / -1;
+ grid-template: subgrid / subgrid;
+ background: green;
+ padding: 11px 7px 20px 13px;
+ border-width: 14px 18px 5px 12px;
+ border-style: solid;
+ border-color: green;
+}
+</style>
+</head>
+<body>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div class="wrapper">
+ <div class="grid">
+ <div class="subgrid">
+ <div class="subgrid"></div>
+ </div>
+ </div>
+</div>
+</body>
+</html>