Skip to content

Commit

Permalink
Merge r221931 - [css-grid] Stretching auto tracks should be done as p…
Browse files Browse the repository at this point in the history
…art of the track sizing algorithm

https://bugs.webkit.org/show_bug.cgi?id=176783

Reviewed by Sergio Villar Senin.

LayoutTests/imported/w3c:

Import changes on the test related to the new behavior.

* web-platform-tests/css/css-grid-1/grid-items/grid-minimum-size-grid-items-021.html:

Source/WebCore:

CSS WG has agreed to modify the track sizing algorithm to include
a new step: https://drafts.csswg.org/css-grid/#algo-stretch
We used to do the stretch of the "auto" tracks at the end of
the track sizing algorithm, however this change integrates it
into the algorithm itself as the last step.
See: w3c/csswg-drafts#1150

The patch moves the method
RenderGrid::applyStretchAlignmentToTracksIfNeeded() to
GridTrackSizingAlgorithm::stretchAutoTracks().
And then modifies the grid track sizing algorithm to execute
the new step.

This patch uses the WPT test updated to check the new behavior.

* rendering/GridTrackSizingAlgorithm.cpp:
(WebCore::GridTrackSizingAlgorithm::initializeTrackSizes): Initialize
the list of auto tracks.
(WebCore::GridTrackSizingAlgorithm::stretchFlexibleTracks): Add
early return if there are not flexible tracks.
(WebCore::GridTrackSizingAlgorithm::stretchAutoTracks): Code moved from
RenderGrid::applyStretchAlignmentToTracksIfNeeded().
(WebCore::GridTrackSizingAlgorithm::setup): Reset list of auto tracks.
(WebCore::GridTrackSizingAlgorithm::run): Add new step
stretchAutoTracks().
(WebCore::GridTrackSizingAlgorithm::reset): Reset auto tracks.
* rendering/GridTrackSizingAlgorithm.h: Add m_autoSizedTracksIndex.
* rendering/RenderGrid.cpp:
(WebCore::RenderGrid::layoutBlock): Avoid applying stretch here.
(WebCore::RenderGrid::contentAlignmentNormalBehaviorGrid): Make public.
(WebCore::RenderGrid::applyStretchAlignmentToTracksIfNeeded): Moved to
GridTrackSizingAlgorithm::stretchAutoTracks().
* rendering/RenderGrid.h:
  • Loading branch information
mrego authored and carlosgcampos committed Oct 16, 2017
1 parent 2dcc37b commit 3a9d8ac
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 43 deletions.
11 changes: 11 additions & 0 deletions LayoutTests/imported/w3c/ChangeLog
@@ -1,3 +1,14 @@
2017-09-12 Manuel Rego Casasnovas <rego@igalia.com>

[css-grid] Stretching auto tracks should be done as part of the track sizing algorithm
https://bugs.webkit.org/show_bug.cgi?id=176783

Reviewed by Sergio Villar Senin.

Import changes on the test related to the new behavior.

* web-platform-tests/css/css-grid-1/grid-items/grid-minimum-size-grid-items-021.html:

2017-09-12 Manuel Rego Casasnovas <rego@igalia.com>

[css-grid] Use transferred size over content size for automatic minimum size
Expand Down
Expand Up @@ -68,14 +68,14 @@
function runTests() {
checkGridSizeTracksAndImageSize("grid-1", "img-1", "200px", "200px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-2", "img-2", "10px", "10px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-3", "img-3", "200px", "50px", "200px", "50px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-4", "img-4", "200px", "10px", "200px", "50px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-3", "img-3", "200px", "200px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-4", "img-4", "200px", "10px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-5", "img-5", "200px", "50px", "50px", "50px", "50px", "50px");
checkGridSizeTracksAndImageSize("grid-6", "img-6", "200px", "10px", "50px", "50px", "50px", "50px");
checkGridSizeTracksAndImageSize("grid-7", "img-7", "200px", "225px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-8", "img-8", "10px", "10px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-9", "img-9", "200px", "125px", "200px", "100px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-10", "img-10", "200px", "10px", "200px", "100px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-9", "img-9", "200px", "225px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-10", "img-10", "200px", "10px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-11", "img-11", "200px", "125px", "100px", "100px 25px", "100px", "100px");
checkGridSizeTracksAndImageSize("grid-12", "img-12", "200px", "10px", "100px", "100px 25px", "100px", "100px");
checkGridSizeTracksAndImageSize("grid-13", "img-13", "200px", "200px", "200px", "200px", "200px", "200px");
Expand Down
41 changes: 41 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,44 @@
2017-09-12 Manuel Rego Casasnovas <rego@igalia.com>

[css-grid] Stretching auto tracks should be done as part of the track sizing algorithm
https://bugs.webkit.org/show_bug.cgi?id=176783

Reviewed by Sergio Villar Senin.

CSS WG has agreed to modify the track sizing algorithm to include
a new step: https://drafts.csswg.org/css-grid/#algo-stretch
We used to do the stretch of the "auto" tracks at the end of
the track sizing algorithm, however this change integrates it
into the algorithm itself as the last step.
See: https://github.com/w3c/csswg-drafts/issues/1150

The patch moves the method
RenderGrid::applyStretchAlignmentToTracksIfNeeded() to
GridTrackSizingAlgorithm::stretchAutoTracks().
And then modifies the grid track sizing algorithm to execute
the new step.

This patch uses the WPT test updated to check the new behavior.

* rendering/GridTrackSizingAlgorithm.cpp:
(WebCore::GridTrackSizingAlgorithm::initializeTrackSizes): Initialize
the list of auto tracks.
(WebCore::GridTrackSizingAlgorithm::stretchFlexibleTracks): Add
early return if there are not flexible tracks.
(WebCore::GridTrackSizingAlgorithm::stretchAutoTracks): Code moved from
RenderGrid::applyStretchAlignmentToTracksIfNeeded().
(WebCore::GridTrackSizingAlgorithm::setup): Reset list of auto tracks.
(WebCore::GridTrackSizingAlgorithm::run): Add new step
stretchAutoTracks().
(WebCore::GridTrackSizingAlgorithm::reset): Reset auto tracks.
* rendering/GridTrackSizingAlgorithm.h: Add m_autoSizedTracksIndex.
* rendering/RenderGrid.cpp:
(WebCore::RenderGrid::layoutBlock): Avoid applying stretch here.
(WebCore::RenderGrid::contentAlignmentNormalBehaviorGrid): Make public.
(WebCore::RenderGrid::applyStretchAlignmentToTracksIfNeeded): Moved to
GridTrackSizingAlgorithm::stretchAutoTracks().
* rendering/RenderGrid.h:

2017-09-12 Chris Fleizach <cfleizach@apple.com>

AX: On generic container elements, WebKit should distinguish between tooltip (e.g. @title) and label (e.g. @aria-label) attributes
Expand Down
36 changes: 33 additions & 3 deletions Source/WebCore/rendering/GridTrackSizingAlgorithm.cpp
Expand Up @@ -970,6 +970,7 @@ void GridTrackSizingAlgorithm::initializeTrackSizes()
{
ASSERT(m_contentSizedTracksIndex.isEmpty());
ASSERT(m_flexibleSizedTracksIndex.isEmpty());
ASSERT(m_autoSizedTracksIndex.isEmpty());

Vector<GridTrack>& allTracks = tracks(m_direction);
const bool hasDefiniteFreeSpace = !!availableSpace();
Expand All @@ -992,6 +993,8 @@ void GridTrackSizingAlgorithm::initializeTrackSizes()
m_contentSizedTracksIndex.append(i);
if (trackSize.maxTrackBreadth().isFlex())
m_flexibleSizedTracksIndex.append(i);
if (trackSize.hasAutoMaxTrackBreadth())
m_autoSizedTracksIndex.append(i);
}
}

Expand Down Expand Up @@ -1039,6 +1042,9 @@ void GridTrackSizingAlgorithm::resolveIntrinsicTrackSizes()

void GridTrackSizingAlgorithm::stretchFlexibleTracks(std::optional<LayoutUnit> freeSpace)
{
if (m_flexibleSizedTracksIndex.isEmpty())
return;

double flexFraction = m_strategy->findUsedFlexFraction(m_flexibleSizedTracksIndex, m_direction, freeSpace);

LayoutUnit totalGrowth;
Expand All @@ -1063,6 +1069,28 @@ void GridTrackSizingAlgorithm::stretchFlexibleTracks(std::optional<LayoutUnit> f
m_maxContentSize += totalGrowth;
}

void GridTrackSizingAlgorithm::stretchAutoTracks()
{
if (m_autoSizedTracksIndex.isEmpty())
return;

auto currentFreeSpace = freeSpace(m_direction);
if (!currentFreeSpace
|| currentFreeSpace.value() <= 0
|| (m_direction == ForColumns && m_renderGrid->style().resolvedJustifyContentDistribution(m_renderGrid->contentAlignmentNormalBehaviorGrid()) != ContentDistributionStretch)
|| (m_direction == ForRows && m_renderGrid->style().resolvedAlignContentDistribution(m_renderGrid->contentAlignmentNormalBehaviorGrid()) != ContentDistributionStretch))
return;

Vector<GridTrack>& allTracks = tracks(m_direction);
unsigned numberOfAutoSizedTracks = m_autoSizedTracksIndex.size();
LayoutUnit sizeToIncrease = currentFreeSpace.value() / numberOfAutoSizedTracks;
for (const auto& trackIndex : m_autoSizedTracksIndex) {
auto& track = allTracks[trackIndex];
track.setBaseSize(track.baseSize() + sizeToIncrease);
}
setFreeSpace(m_direction, LayoutUnit());
}

void GridTrackSizingAlgorithm::advanceNextState()
{
switch (m_sizingState) {
Expand Down Expand Up @@ -1117,6 +1145,7 @@ void GridTrackSizingAlgorithm::setup(GridTrackSizingDirection direction, unsigne

m_contentSizedTracksIndex.shrink(0);
m_flexibleSizedTracksIndex.shrink(0);
m_autoSizedTracksIndex.shrink(0);

setFreeSpace(direction, freeSpace);
tracks(direction).resize(numTracks);
Expand Down Expand Up @@ -1150,11 +1179,11 @@ void GridTrackSizingAlgorithm::run()
// Step 3.
m_strategy->maximizeTracks(tracks(m_direction), m_direction == ForColumns ? m_freeSpaceColumns : m_freeSpaceRows);

if (m_flexibleSizedTracksIndex.isEmpty())
return;

// Step 4.
stretchFlexibleTracks(initialFreeSpace);

// Step 5.
stretchAutoTracks();
}

void GridTrackSizingAlgorithm::reset()
Expand All @@ -1164,6 +1193,7 @@ void GridTrackSizingAlgorithm::reset()
m_rows.shrink(0);
m_contentSizedTracksIndex.shrink(0);
m_flexibleSizedTracksIndex.shrink(0);
m_autoSizedTracksIndex.shrink(0);
setAvailableSpace(ForRows, std::nullopt);
setAvailableSpace(ForColumns, std::nullopt);
}
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/rendering/GridTrackSizingAlgorithm.h
Expand Up @@ -153,6 +153,7 @@ class GridTrackSizingAlgorithm final {
void initializeTrackSizes();
void resolveIntrinsicTrackSizes();
void stretchFlexibleTracks(std::optional<LayoutUnit> freeSpace);
void stretchAutoTracks();

// State machine.
void advanceNextState();
Expand All @@ -171,6 +172,7 @@ class GridTrackSizingAlgorithm final {
Vector<GridTrack> m_rows;
Vector<unsigned> m_contentSizedTracksIndex;
Vector<unsigned> m_flexibleSizedTracksIndex;
Vector<unsigned> m_autoSizedTracksIndex;

GridTrackSizingDirection m_direction;
SizingOperation m_sizingOperation;
Expand Down
35 changes: 1 addition & 34 deletions Source/WebCore/rendering/RenderGrid.cpp
Expand Up @@ -305,9 +305,6 @@ void RenderGrid::layoutBlock(bool relayoutChildren, LayoutUnit)
setLogicalHeight(std::max(logicalHeight(), minHeightForEmptyLine));
}

applyStretchAlignmentToTracksIfNeeded(ForColumns);
applyStretchAlignmentToTracksIfNeeded(ForRows);

layoutGridItems();
m_trackSizingAlgorithm.reset();

Expand Down Expand Up @@ -880,42 +877,12 @@ Vector<LayoutUnit> RenderGrid::trackSizesForComputedStyle(GridTrackSizingDirecti
return tracks;
}

static const StyleContentAlignmentData& contentAlignmentNormalBehaviorGrid()
const StyleContentAlignmentData& RenderGrid::contentAlignmentNormalBehaviorGrid()
{
static const StyleContentAlignmentData normalBehavior = {ContentPositionNormal, ContentDistributionStretch};
return normalBehavior;
}

void RenderGrid::applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection direction)
{
std::optional<LayoutUnit> freeSpace = m_trackSizingAlgorithm.freeSpace(direction);
if (!freeSpace
|| freeSpace.value() <= 0
|| (direction == ForColumns && style().resolvedJustifyContentDistribution(contentAlignmentNormalBehaviorGrid()) != ContentDistributionStretch)
|| (direction == ForRows && style().resolvedAlignContentDistribution(contentAlignmentNormalBehaviorGrid()) != ContentDistributionStretch))
return;

// Spec defines auto-sized tracks as the ones with an 'auto' max-sizing function.
Vector<GridTrack>& allTracks = m_trackSizingAlgorithm.tracks(direction);
Vector<unsigned> autoSizedTracksIndex;
for (unsigned i = 0; i < allTracks.size(); ++i) {
const GridTrackSize& trackSize = m_trackSizingAlgorithm.gridTrackSize(direction, i);
if (trackSize.hasAutoMaxTrackBreadth())
autoSizedTracksIndex.append(i);
}

unsigned numberOfAutoSizedTracks = autoSizedTracksIndex.size();
if (numberOfAutoSizedTracks < 1)
return;

LayoutUnit sizeToIncrease = freeSpace.value() / numberOfAutoSizedTracks;
for (const auto& trackIndex : autoSizedTracksIndex) {
auto& track = allTracks[trackIndex];
track.setBaseSize(track.baseSize() + sizeToIncrease);
}
m_trackSizingAlgorithm.setFreeSpace(direction, LayoutUnit());
}

void RenderGrid::layoutGridItems()
{
populateGridPositionsForDirection(ForColumns);
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/rendering/RenderGrid.h
Expand Up @@ -65,6 +65,8 @@ class RenderGrid final : public RenderBlock {
bool isOrthogonalChild(const RenderBox&) const;
LayoutUnit guttersSize(const Grid&, GridTrackSizingDirection, unsigned startLine, unsigned span, std::optional<LayoutUnit> availableSize) const;

static const StyleContentAlignmentData& contentAlignmentNormalBehaviorGrid();

protected:
ItemPosition selfAlignmentNormalBehavior(const RenderBox* child = nullptr) const override
{
Expand Down Expand Up @@ -132,8 +134,6 @@ class RenderGrid final : public RenderBlock {

LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection) const;

void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection);

void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
LayoutUnit marginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
LayoutUnit computeMarginLogicalSizeForChild(GridTrackSizingDirection, const RenderBox&) const;
Expand Down

0 comments on commit 3a9d8ac

Please sign in to comment.