Skip to content

Truly independent web browser

LibWeb: Replay flex and grid item runs across percentage bases

Aliaksandr Kalenik85cda7edb80a
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index d6e1d2c7ea5..6a2b116cf26 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -3657,14 +3657,17 @@ fn table_in_wrapper_never_observes_percentage_inline_basis(run: &FormattingConte
true
}
-/// Whether the run is of an item block container whose content resolves percentages against the container itself.
+/// Whether the run is of an item block, flex or grid container whose content resolves percentages against the
+/// container itself.
fn item_run_never_observes_percentage_inline_basis(
run: &FormattingContextRun,
fc_type: FormattingContextType,
input: &LayoutInput,
) -> bool {
- fc_type == FormattingContextType::Block
- && parent_owns_root_input_cells(&run.pass, run.box_, input)
+ matches!(
+ fc_type,
+ FormattingContextType::Block | FormattingContextType::Flex | FormattingContextType::Grid
+ ) && parent_owns_root_input_cells(&run.pass, run.box_, input)
&& !run.sizing().measurement_root_observes_percentage_inline_basis(run.box_)
}
diff --git a/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-percentage-inline-basis-independent.txt b/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-percentage-inline-basis-independent.txt
index 91c13b83a29..e9ddb818d09 100644
--- a/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-percentage-inline-basis-independent.txt
+++ b/Tests/LibWeb/Text/expected/intrinsic-measurement-cache-percentage-inline-basis-independent.txt
@@ -1,3 +1,3 @@
measured again: nothing
-answered from the cache: Box<DIV>#inner, BlockContainer<DIV>#alpha.item, BlockContainer<DIV>#gamma.item
+answered from the cache: Box<DIV>#inner
inner width: 154.6875
diff --git a/Tests/LibWeb/Text/expected/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.txt b/Tests/LibWeb/Text/expected/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.txt
new file mode 100644
index 00000000000..4e8ffa99977
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.txt
@@ -0,0 +1,2 @@
+PASS: a narrower flex container replays its fixed-size flex and grid items (work + checks)
+PASS: a narrower grid container replays its fixed-size flex and grid items (work + checks)
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.html b/Tests/LibWeb/Text/input/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.html
new file mode 100644
index 00000000000..79e312dd63c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-run-cache-flex-and-grid-items-ignore-unobserved-percentage-inline-basis.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ body { margin: 0; }
+ #flex { display: flex; width: 300px; height: 40px; }
+ #grid { display: grid; grid-template-columns: 50% 25% 25%; width: 300px; height: 40px; }
+ .flex-item { display: flex; width: 40px; height: 20px; }
+ .grid-item { display: grid; width: 40px; height: 20px; }
+ .padded { padding-left: 10%; }
+ .half { width: 50%; height: 10px; }
+</style>
+<div id="flex"><div class="flex-item" id="flexInFlex"><div class="half" id="flexInFlexHalf"></div></div><div class="grid-item" id="gridInFlex"><div class="half" id="gridInFlexHalf"></div></div><div class="flex-item padded" id="paddedFlexInFlex"><div class="half" id="paddedFlexInFlexHalf"></div></div></div>
+<div id="grid"><div class="flex-item" id="flexInGrid"><div class="half" id="flexInGridHalf"></div></div><div class="grid-item" id="gridInGrid"><div class="half" id="gridInGridHalf"></div></div><div class="grid-item padded" id="paddedGridInGrid"><div class="half" id="paddedGridInGridHalf"></div></div></div>
+<script>
+ layoutTest({
+ steps: [
+ {
+ name: "a narrower flex container replays its fixed-size flex and grid items",
+ mutate: () => {
+ flex.style.width = "200px";
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#flex/flex RUN (cache=miss)
+ Box<DIV>#flexInFlex.flex-item/flex REUSE SUBTREE
+ Box<DIV>#gridInFlex.grid-item/grid REUSE SUBTREE
+ Box<DIV>#paddedFlexInFlex.flex-item.padded/flex RUN (cache=miss)
+ BlockContainer<DIV>#paddedFlexInFlexHalf.half/block REUSE SUBTREE
+ Box<DIV>#grid/grid REUSE SUBTREE
+ `,
+ check: equal => {
+ equal(flexInFlexHalf.getBoundingClientRect().width, 20, "flex item child width");
+ equal(gridInFlexHalf.getBoundingClientRect().width, 20, "grid item child width");
+ equal(paddedFlexInFlex.getBoundingClientRect().width, 60, "padded item width");
+ equal(paddedFlexInFlexHalf.getBoundingClientRect().left, 100, "padded item child left");
+ },
+ },
+ {
+ name: "a narrower grid container replays its fixed-size flex and grid items",
+ mutate: () => {
+ grid.style.width = "200px";
+ },
+ expect: `
+ layout FULL
+ @viewport/block RUN (cache=bypass)
+ BlockContainer<HTML>/block RUN (cache=miss)
+ Box<DIV>#flex/flex REUSE SUBTREE
+ Box<DIV>#grid/grid RUN (cache=miss)
+ Box<DIV>#flexInGrid.flex-item/flex REUSE SUBTREE
+ Box<DIV>#gridInGrid.grid-item/grid REUSE SUBTREE
+ Box<DIV>#paddedGridInGrid.grid-item.padded/grid RUN (cache=miss)
+ BlockContainer<DIV>#paddedGridInGridHalf.half/block REUSE SUBTREE
+ `,
+ check: equal => {
+ equal(flexInGridHalf.getBoundingClientRect().width, 20, "flex item child width");
+ equal(gridInGridHalf.getBoundingClientRect().width, 20, "grid item child width");
+ equal(paddedGridInGrid.getBoundingClientRect().width, 45, "padded item width");
+ equal(paddedGridInGridHalf.getBoundingClientRect().left, 155, "padded item child left");
+ },
+ },
+ ],
+ });
+</script>