Skip to content

Truly independent web browser

LibWeb: Do not subgrid grids that establish independent contexts

Andreas Klingb441477522dc
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index 54c21858cb1..27acc6d643f 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -1284,6 +1284,34 @@ impl<'a> ComputedValuesView<'a> {
)
}
+ /// Whether this box's own style forces it to establish an independent formatting context even where it would
+ /// otherwise share one with its parent, as a subgrid does.
+ ///
+ /// https://drafts.csswg.org/css-grid-2/#subgrid-listing
+ /// If there is no parent grid, or if the grid container is otherwise forced to establish an independent formatting
+ /// context (for example, due to layout containment [CSS-CONTAIN-2] or absolute positioning [CSS-POSITION-3]), the
+ /// used value is the initial value, none, and the grid container is not a subgrid.
+ pub(crate) fn is_forced_to_establish_independent_formatting_context(self) -> bool {
+ if self.is_absolutely_positioned() {
+ return true;
+ }
+ let box_values = self.box_values();
+ // https://drafts.csswg.org/css-contain-2/#containment-types
+ // 1. The layout containment box establishes an independent formatting context.
+ // 4. The paint containment box establishes an independent formatting context.
+ let content_visibility_forces_containment =
+ self.misc_layout().content_visibility == crate::css::css_enums::content_visibility::AUTO;
+ if (box_values.layout_containment || box_values.paint_containment || content_visibility_forces_containment)
+ && containment_applies_to_principal_box(box_values.display)
+ {
+ return true;
+ }
+ // https://drafts.csswg.org/css-conditional-5/#valdef-container-type-size
+ // Applies style containment and size containment to the principal box, and establishes an independent
+ // formatting context.
+ box_values.is_size_container || box_values.is_inline_size_container
+ }
+
// https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
// The computed-style-only half of the block-formatting-context predicate;
// node_creates_block_formatting_context adds the terms that need the node
@@ -1328,21 +1356,7 @@ impl<'a> ComputedValuesView<'a> {
return true;
}
- // https://drafts.csswg.org/css-contain-2/#containment-types
- // 1. The layout containment box establishes an independent formatting context.
- // 4. The paint containment box establishes an independent formatting context.
- let content_visibility_forces_containment =
- self.misc_layout().content_visibility == crate::css::css_enums::content_visibility::AUTO;
- if (box_values.layout_containment || box_values.paint_containment || content_visibility_forces_containment)
- && containment_applies_to_principal_box(display)
- {
- return true;
- }
-
- // https://drafts.csswg.org/css-conditional-5/#valdef-container-type-size
- // Applies style containment and size containment to the principal box, and establishes an independent
- // formatting context.
- if box_values.is_size_container || box_values.is_inline_size_container {
+ if self.is_forced_to_establish_independent_formatting_context() {
return true;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index eb295f276d9..8044f085f0e 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -1627,8 +1627,9 @@ impl<'pass> GridFormattingContext<'pass> {
// to establish an independent formatting context, the used value is
// the initial value, grid-template-rows/none, and the grid container is not
// a subgrid.
- // FIXME: Also reject subgrid here when the grid container is forced to
- // establish an independent formatting context.
+ if !self.can_be_subgrid(self.run.box_) {
+ return false;
+ }
let list = self.select_grid_axis(axis, grid_style.template_columns, grid_style.template_rows);
// https://drafts.csswg.org/css-grid-3/#subgrids
// If the parent's corresponding axis is a grid axis, the subgridded axis is taken from the parent container as
@@ -1638,6 +1639,16 @@ impl<'pass> GridFormattingContext<'pass> {
list.is_subgrid && self.parent_grid_placement().is_some()
}
+ /// Whether a grid container with a subgrid track list is a subgrid, rather than being forced to establish an
+ /// independent formatting context, which makes its track list none.
+ fn can_be_subgrid(&self, node: Node) -> bool {
+ !self
+ .run
+ .pass
+ .style_view(node)
+ .is_forced_to_establish_independent_formatting_context()
+ }
+
fn container_is_subgridded(&self, axis: Axis) -> bool {
axis.select(self.subgridded_columns, self.subgridded_rows)
}
@@ -2169,12 +2180,11 @@ impl<'pass> GridFormattingContext<'pass> {
child_grid_style.template_columns,
child_grid_style.template_rows,
);
- let column_subgrid_span = child_columns
- .is_subgrid
+ let child_can_be_subgrid = self.can_be_subgrid(child);
+ let column_subgrid_span = (child_columns.is_subgrid && child_can_be_subgrid)
.then(|| automatic_subgrid_span(source, child_columns));
- let row_subgrid_span = child_rows
- .is_subgrid
- .then(|| automatic_subgrid_span(source, child_rows));
+ let row_subgrid_span =
+ (child_rows.is_subgrid && child_can_be_subgrid).then(|| automatic_subgrid_span(source, child_rows));
let (row_start, row_end) = self.select_grid_axis(
Axis::Row,
(child_grid_style.column_start, child_grid_style.column_end),
@@ -2219,7 +2229,7 @@ impl<'pass> GridFormattingContext<'pass> {
fn new_grid_item(&self, collected_index: usize) -> GridItem {
let box_ = self.collected_items[collected_index].box_;
- let is_column_only_subgrid = self.facts(box_).display().is_grid_inside() && {
+ let is_column_only_subgrid = self.facts(box_).display().is_grid_inside() && self.can_be_subgrid(box_) && {
let grid_style = self.grid_style(box_);
self.select_item_grid_axis(
box_,
@@ -3620,10 +3630,9 @@ impl<'pass> GridFormattingContext<'pass> {
}
fn node_is_subgridded(&self, node: Node, axis: Axis) -> bool {
- let facts = self.facts(node);
- if !facts.display().is_grid_inside()
- || facts.is_absolutely_positioned()
+ if !self.facts(node).display().is_grid_inside()
|| self.is_grid_lanes_stacking_axis(axis)
+ || !self.can_be_subgrid(node)
{
return false;
}
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/independent-formatting-context-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/independent-formatting-context-ref.html
new file mode 100644
index 00000000000..63bff59dab7
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-grid/subgrid/independent-formatting-context-ref.html
@@ -0,0 +1,61 @@
+<!DOCTYPE HTML>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html><head>
+ <meta charset="utf-8">
+ <title>Reference: subgrid disabled if grid item establishes independent formatting context</title>
+ <link rel="author" title="Matt Woodrow" href="https://bugs.webkit.org/show_bug.cgi?id=237692">
+ <link rel="help" href="https://drafts.csswg.org/css-grid-2/#subgrid-listing">
+ <style>
+
+html,body {
+ font:16px/1 monospace; padding:0; margin:0; line-height: 0;
+}
+
+.grid {
+ display: inline-block;
+ background-color: green;
+ width: 100px;
+ height: 100px;
+ margin-right: 20px;
+ margin-bottom: 20px;
+ vertical-align: top;
+}
+
+ </style>
+</head>
+<body>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+
+<div class="grid">
+</div>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/independent-formatting-context.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/independent-formatting-context.html
new file mode 100644
index 00000000000..34e1004b08e
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/subgrid/independent-formatting-context.html
@@ -0,0 +1,115 @@
+<!DOCTYPE HTML>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html><head>
+ <meta charset="utf-8">
+ <title>Test: subgrid disabled if grid item establishes independent formatting context</title>
+ <link rel="author" title="Matt Woodrow" href="https://bugs.webkit.org/show_bug.cgi?id=237692">
+ <link rel="help" href="https://drafts.csswg.org/css-grid-2/#subgrid-listing">
+ <link rel="match" href="../../../../../expected/wpt-import/css/css-grid/subgrid/independent-formatting-context-ref.html">
+ <style>
+
+html,body {
+ font:16px/1 monospace; padding:0; margin:0; line-height: 0;
+}
+
+.grid {
+ position: relative;
+ display: inline-grid;
+ grid-template-rows: 0px 100px;
+ width: 100px;
+ margin-right: 20px;
+ margin-bottom: 20px;
+ vertical-align: top;
+}
+
+.subgrid {
+ display: grid;
+ grid-template-rows: subgrid;
+ grid-row: 1 / 3;
+ width: 100px;
+ height: 100px;
+ background-color: red;
+}
+
+.first {
+ grid-row: 1;
+ background-color: green;
+}
+
+.second {
+ grid-row: 2;
+ background-color: green;
+}
+
+ </style>
+</head>
+<body>
+<!-- The subgrid's style attribute establishes an independent formatting
+ context and makes them no longer eligible as subgrids.-->
+<div class="grid">
+ <div class="subgrid" style="position: absolute">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="container-type: size">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="container-type: inline-size">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: layout">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: paint">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: strict">
+ <div class="first"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: content">
+ <div class="first"></div>
+ </div>
+</div>
+
+<!-- Only layout and paint containment establish an independent formatting
+ context. These tests validate that other types of containment maintain
+ subgrid layout.-->
+<div class="grid">
+ <div class="subgrid" style="contain: none">
+ <div class="second"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: size">
+ <div class="second"></div>
+ </div>
+</div>
+
+<div class="grid">
+ <div class="subgrid" style="contain: style">
+ <div class="second"></div>
+ </div>
+</div>
+</body>
+</html>