Skip to content

Commit 13cbbd2

Browse files
committed
[css-sizing-4] Add support for fit-content(<length-percentage>) in the sizing properties
https://bugs.webkit.org/show_bug.cgi?id=330787 <rdar://problem/192283145> Reviewed by NOBODY (OOPS!). https://drafts.csswg.org/css-sizing-4/#funcdef-width-fit-content "Use the fit-content formula with the available space replaced by stretch() into the specified argument, i.e. min(max-content, max(min-content, stretch(<length-percentage>)))", where stretch() fits the margin box to the argument, treating auto margins as zero (resolved by the CSSWG on October 7, 2026 in w3c/csswg-drafts#5715). fit-content() parses and answers to the fit-content keyword since the style patch, so layout sized it as the keyword. Resolving the size now clamps stretch() into the argument between the min-content and max-content sizes, and the stretch() goes through the same box-sizing conversions as a stretch() size does (https://bugs.webkit.org/show_bug.cgi?id=330814): its margins come off the argument, resolved against the containing block's inline size. Where layout has no available space to replace, the value needs its own rule: - A fixed argument gives a size that doesn't depend on the available space, so it is also the box's min-content and max-content contribution, with its margins resolved against zero as the contributions resolve them. - A percentage argument is cyclic in the contributions, and "For fit-content(), if the <length-percentage> expression on its own would resolve to the property's initial value, then instead: For a maximum size property, treat it as max-content. For a minimum size property, treat it as min-content. For a preferred size property, treat it as fit-content." - An out-of-flow box resolves the argument against its containing block, not the space the insets leave. In the block axis min-content and max-content are both the content size, so fit-content() resolves as the keyword does there. A fit-content() flex basis needs nothing more: with the main axis in the item's inline axis, step A of https://drafts.csswg.org/css-flexbox/#algo-main-item resolves it against the flex container's main size, and in the block axis the item's content size is its fit-content size. Tests: imported/w3c/web-platform-tests/css/css-flexbox/flex-basis-fit-content-function-001.html imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-abspos-001.html imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-box-sizing-001.html imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-contribution-margins-001.html imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-margins-001.html * LayoutTests/TestExpectations: The rest of the fit-content-length-percentage-0* tests pass now. * LayoutTests/imported/w3c/web-platform-tests/css/css-flexbox/flex-basis-fit-content-function-001-expected.xht: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-flexbox/flex-basis-fit-content-function-001.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-abspos-001-expected.xht: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-abspos-001.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-box-sizing-001-expected.xht: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-box-sizing-001.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-contribution-margins-001-expected.xht: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-contribution-margins-001.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-margins-001-expected.xht: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-margins-001.html: Added. * Source/WebCore/rendering/RenderBlock.cpp: (WebCore::RenderBlock::logicalWidthContributionsForSize const): * Source/WebCore/rendering/RenderBox.cpp: (WebCore::RenderBox::computeFitContentFunctionLogicalWidthUsing const): (WebCore::RenderBox::fitContentFunctionContentLogicalWidthContribution const): (WebCore::RenderBox::computeSizingKeywordLogicalWidthUsingGeneric const): (WebCore::RenderBox::constrainIntrinsicLogicalWidthContributionsByMinMax const): (WebCore::RenderBox::computeOutOfFlowPositionedLogicalWidthUsing const): * Source/WebCore/rendering/RenderBox.h: * Source/WebCore/rendering/RenderReplaced.cpp: (WebCore::RenderReplaced::computeReplacedLogicalWidthUsing const):
1 parent 029ebd1 commit 13cbbd2

15 files changed

Lines changed: 346 additions & 18 deletions

‎LayoutTests/TestExpectations‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4664,20 +4664,6 @@ imported/w3c/web-platform-tests/css/css-values/urls/referrer-policy/unsafe-url/u
46644664
# wpt css-sizing failures
46654665
webkit.org/b/203583 imported/w3c/web-platform-tests/css/css-sizing/aspect-ratio-affects-container-width-when-height-changes.html [ Pass Failure ]
46664666
webkit.org/b/214463 imported/w3c/web-platform-tests/css/css-sizing/image-fractional-height-with-wide-aspect-ratio.html [ ImageOnlyFailure ]
4667-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-001.html [ ImageOnlyFailure ]
4668-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-002.html [ ImageOnlyFailure ]
4669-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-004.html [ ImageOnlyFailure ]
4670-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-005.html [ ImageOnlyFailure ]
4671-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-007.html [ ImageOnlyFailure ]
4672-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-008.html [ ImageOnlyFailure ]
4673-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-009.html [ ImageOnlyFailure ]
4674-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-010.html [ ImageOnlyFailure ]
4675-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-011.html [ ImageOnlyFailure ]
4676-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-012.html [ ImageOnlyFailure ]
4677-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-013.html [ ImageOnlyFailure ]
4678-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-014.html [ ImageOnlyFailure ]
4679-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-015.html [ ImageOnlyFailure ]
4680-
imported/w3c/web-platform-tests/css/css-sizing/fit-content-length-percentage-016.html [ ImageOnlyFailure ]
46814667
imported/w3c/web-platform-tests/css/css-sizing/max-content-input-001.html [ ImageOnlyFailure ]
46824668
# These references give min-height: min-content the initial value (min-height: 0), but in the block axis it is the automatic size
46834669
# (https://drafts.csswg.org/css-sizing-3/#valdef-width-min-content), and the compressed min-content contribution of a percentage-sized
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>CSS Reftest Reference</title>
5+
<link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
6+
<style type="text/css"><![CDATA[
7+
div
8+
{
9+
background-color: green;
10+
height: 100px;
11+
width: 100px;
12+
}
13+
]]></style>
14+
</head>
15+
<body>
16+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
17+
<div></div>
18+
</body>
19+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<title>CSS Flexbox: a fit-content() flex basis</title>
3+
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#flex-basis-property">
4+
<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#algo-main-item">
5+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-fit-content">
6+
<link rel="match" href="../reference/ref-filled-green-100px-square.xht" />
7+
<meta name="assert" content="A fit-content() flex basis or main size sizes the item into the available space with its argument in place of the available space.">
8+
<style>
9+
#reference-overlapped-red {
10+
position: absolute;
11+
background-color: red;
12+
width: 100px;
13+
height: 100px;
14+
z-index: -1;
15+
}
16+
.flex {
17+
display: flex;
18+
width: 300px;
19+
height: 25px;
20+
}
21+
.item {
22+
flex: none;
23+
background: green;
24+
font-size: 0;
25+
}
26+
.item > div {
27+
display: inline-block;
28+
width: 60px;
29+
}
30+
</style>
31+
32+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
33+
34+
<div id="reference-overlapped-red"></div>
35+
<div class="flex"><div class="item" style="flex-basis: fit-content(100px);"><div></div><div></div></div></div>
36+
<div class="flex"><div class="item" style="flex-basis: fit-content(calc(100% / 3));"><div></div><div></div></div></div>
37+
<div class="flex"><div class="item" style="flex: 0 0 fit-content(110px); margin-right: 10px;"><div></div><div></div></div></div>
38+
<div class="flex"><div class="item" style="width: fit-content(100px);"><div></div><div></div></div></div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>CSS Reftest Reference</title>
5+
<link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
6+
<style type="text/css"><![CDATA[
7+
div
8+
{
9+
background-color: green;
10+
height: 100px;
11+
width: 100px;
12+
}
13+
]]></style>
14+
</head>
15+
<body>
16+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
17+
<div></div>
18+
</body>
19+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<title>CSS fit-content(): a percentage argument of an absolutely positioned box resolves against its containing block</title>
3+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-fit-content">
4+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-stretch">
5+
<link rel="match" href="../reference/ref-filled-green-100px-square.xht" />
6+
<meta name="assert" content="The argument of stretch() is resolved exactly as for length-percentage values standing alone, so the insets don't change what a percentage refers to.">
7+
<style>
8+
#reference-overlapped-red {
9+
position: absolute;
10+
background-color: red;
11+
width: 100px;
12+
height: 100px;
13+
z-index: -1;
14+
}
15+
.container {
16+
position: relative;
17+
width: 300px;
18+
height: 100px;
19+
}
20+
.test {
21+
position: absolute;
22+
top: 0;
23+
left: 0;
24+
right: 150px;
25+
width: fit-content(calc(100% / 3));
26+
height: 100px;
27+
background: green;
28+
font-size: 0;
29+
}
30+
.test > div {
31+
display: inline-block;
32+
width: 60px;
33+
}
34+
</style>
35+
36+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
37+
38+
<div id="reference-overlapped-red"></div>
39+
<div class="container">
40+
<div class="test"><div></div><div></div></div>
41+
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>CSS Reftest Reference</title>
5+
<link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
6+
<style type="text/css"><![CDATA[
7+
div
8+
{
9+
background-color: green;
10+
height: 100px;
11+
width: 100px;
12+
}
13+
]]></style>
14+
</head>
15+
<body>
16+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
17+
<div></div>
18+
</body>
19+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<title>CSS fit-content(): the argument is the size of the margin box whatever the box-sizing</title>
3+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-fit-content">
4+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-stretch">
5+
<link rel="match" href="../reference/ref-filled-green-100px-square.xht" />
6+
<meta name="assert" content="fit-content() replaces the available space with stretch() into its argument, which fits the margin box to it regardless of box-sizing.">
7+
<style>
8+
#reference-overlapped-red {
9+
position: absolute;
10+
background-color: red;
11+
width: 100px;
12+
height: 100px;
13+
z-index: -1;
14+
}
15+
.test {
16+
height: 25px;
17+
background: green;
18+
font-size: 0;
19+
}
20+
.test > div {
21+
display: inline-block;
22+
width: 60px;
23+
}
24+
</style>
25+
26+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
27+
28+
<div id="reference-overlapped-red"></div>
29+
<div class="test" style="width: fit-content(100px); padding-right: 20px;"><div></div><div></div></div>
30+
<div class="test" style="width: fit-content(100px); padding-right: 20px; box-sizing: border-box;"><div></div><div></div></div>
31+
<div class="test" style="width: fit-content(100px); border-right: 20px solid green;"><div></div><div></div></div>
32+
<div class="test" style="width: fit-content(70px); padding-right: 40px;"><div></div><div></div></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>CSS Reftest Reference</title>
5+
<link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
6+
<style type="text/css"><![CDATA[
7+
div
8+
{
9+
background-color: green;
10+
height: 100px;
11+
width: 100px;
12+
}
13+
]]></style>
14+
</head>
15+
<body>
16+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
17+
<div></div>
18+
</body>
19+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<title>CSS fit-content(): the intrinsic size contribution of a box with a fixed argument</title>
3+
<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#funcdef-width-fit-content">
4+
<link rel="help" href="https://drafts.csswg.org/css-sizing-3/#intrinsic-contribution">
5+
<link rel="match" href="../reference/ref-filled-green-100px-square.xht" />
6+
<meta name="assert" content="A fixed fit-content() argument is the size of the margin box, so the box contributes it to its container's max-content size margins included.">
7+
<style>
8+
#reference-overlapped-red {
9+
position: absolute;
10+
background-color: red;
11+
width: 100px;
12+
height: 100px;
13+
z-index: -1;
14+
}
15+
.container {
16+
width: max-content;
17+
background: green;
18+
}
19+
.test {
20+
width: fit-content(100px);
21+
margin-right: 10px;
22+
height: 100px;
23+
font-size: 0;
24+
}
25+
.test > div {
26+
display: inline-block;
27+
width: 60px;
28+
}
29+
</style>
30+
31+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
32+
33+
<div id="reference-overlapped-red"></div>
34+
<div class="container">
35+
<div class="test"><div></div><div></div></div>
36+
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>CSS Reftest Reference</title>
5+
<link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />
6+
<style type="text/css"><![CDATA[
7+
div
8+
{
9+
background-color: green;
10+
height: 100px;
11+
width: 100px;
12+
}
13+
]]></style>
14+
</head>
15+
<body>
16+
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
17+
<div></div>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo