Bug 1654541 - fit-content percentage should only clamp when definite. r=emilio,layout-grid-reviewers,TYLin

Differential Revision: https://phabricator.services.mozilla.com/D226204
This commit is contained in:
Connor Pearson
2024-10-30 15:40:34 +00:00
parent e5b0c8f554
commit 4acf8e5dc8
2 changed files with 31 additions and 25 deletions

View File

@@ -449,7 +449,9 @@ TrackSize::StateBits nsGridContainerFrame::TrackSize::Initialize(
if (aSize.IsFitContent()) {
// In layout, fit-content(size) behaves as minmax(auto, max-content), with
// 'size' as an additional upper-bound.
mState = eFitContent;
if (!::IsPercentOfIndefiniteSize(aSize.AsFitContent(), aPercentageBasis)) {
mState = eFitContent;
}
minSizeTag = Tag::Auto;
maxSizeTag = Tag::MaxContent;
}

View File

@@ -36,30 +36,34 @@ function clamp(value, min, max) {
}
const minContent = 50;
const maxContent = 100;
for (const use_calc of [false, true]) {
for (const percentage of [0, 50, 75, 100, 150]) {
const value = use_calc ? `fit-content(calc(0px + ${percentage}%))`
: `fit-content(${percentage}%)`;
const container = document.createElement("div");
container.className = "container";
document.body.appendChild(container);
const grid = document.createElement("div");
grid.className = "grid";
grid.style.gridTemplateColumns = value;
container.appendChild(grid);
const item = document.createElement("div");
item.className = "item";
grid.appendChild(item);
test(function() {
const colSize = clamp(percentage * maxContent / 100, minContent, maxContent);
const height = colSize < maxContent ? maxContent : minContent;
assert_equals(item.offsetWidth, colSize, "Grid item width");
assert_equals(item.offsetHeight, height, "Grid item height");
assert_equals(grid.offsetWidth, maxContent, "Grid container width");
assert_equals(grid.offsetHeight, height, "Grid container height");
assert_equals(getComputedStyle(grid).gridTemplateColumns, colSize + "px",
"Grid column size");
}, value);
for (const use_min_width of [false, true]) {
for (const use_calc of [false, true]) {
for (const percentage of [0, 50, 75, 100, 150]) {
const value = use_calc ? `fit-content(calc(0px + ${percentage}%))`
: `fit-content(${percentage}%)`;
const container = document.createElement("div");
container.className = "container";
document.body.appendChild(container);
const grid = document.createElement("div");
grid.className = "grid";
grid.style.gridTemplateColumns = value;
container.appendChild(grid);
const item = document.createElement("div");
item.className = "item";
item.style.minWidth = use_min_width ? "auto" : "0px";
grid.appendChild(item);
test(function() {
const minWidth = use_min_width ? minContent : 0;
const colSize = clamp(percentage * maxContent / 100, minWidth, maxContent);
const height = colSize < maxContent ? maxContent : minContent;
assert_equals(item.offsetWidth, colSize, "Grid item width");
assert_equals(item.offsetHeight, height, "Grid item height");
assert_equals(grid.offsetWidth, maxContent, "Grid container width");
assert_equals(grid.offsetHeight, height, "Grid container height");
assert_equals(getComputedStyle(grid).gridTemplateColumns, colSize + "px",
"Grid column size");
}, value + "; min-width: " + item.style.minWidth);
}
}
}
</script>