Commit bb05a60c authored by Manuel Rego Casasnovas's avatar Manuel Rego Casasnovas Committed by Commit Bot

[css-grid] Move stretch of "auto" tracks into the track sizing algorithm

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
LayoutGrid::ApplyStretchAlignmentToTracksIfNeeded() to
GridTrackSizingAlgorithm::StretchAutoTracksIfNeeded().
And then modifies the grid track sizing algorithm to execute
the new step.

This also modifies the test to check the new expected behavior.

BUG=763386
TEST=external/wpt/css/css-grid-1/grid-items/grid-minimum-size-grid-items-021.html

Change-Id: Ieb31e822989ae4655d0e632885215e4a032dd445
Reviewed-on: https://chromium-review.googlesource.com/657578
Commit-Queue: Manuel Rego Casasnovas <rego@igalia.com>
Reviewed-by: default avatarSergio Villar <svillar@igalia.com>
Reviewed-by: default avatarJavier Fernandez <jfernandez@igalia.com>
Cr-Commit-Position: refs/heads/master@{#501249}
parent 48c811b2
...@@ -68,14 +68,14 @@ ...@@ -68,14 +68,14 @@
function runTests() { function runTests() {
checkGridSizeTracksAndImageSize("grid-1", "img-1", "200px", "200px", "200px", "200px", "200px", "200px"); checkGridSizeTracksAndImageSize("grid-1", "img-1", "200px", "200px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-2", "img-2", "10px", "10px", "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-3", "img-3", "200px", "200px", "200px", "200px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-4", "img-4", "200px", "10px", "200px", "50px", "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-5", "img-5", "200px", "50px", "50px", "50px", "50px", "50px");
checkGridSizeTracksAndImageSize("grid-6", "img-6", "200px", "10px", "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-7", "img-7", "200px", "225px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-8", "img-8", "10px", "10px", "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-9", "img-9", "200px", "225px", "200px", "200px 25px", "200px", "200px");
checkGridSizeTracksAndImageSize("grid-10", "img-10", "200px", "10px", "200px", "100px 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-11", "img-11", "200px", "125px", "100px", "100px 25px", "100px", "100px");
checkGridSizeTracksAndImageSize("grid-12", "img-12", "200px", "10px", "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"); checkGridSizeTracksAndImageSize("grid-13", "img-13", "200px", "200px", "200px", "200px", "200px", "200px");
......
...@@ -726,6 +726,7 @@ LayoutUnit GridTrackSizingAlgorithm::InitialGrowthLimit( ...@@ -726,6 +726,7 @@ LayoutUnit GridTrackSizingAlgorithm::InitialGrowthLimit(
void GridTrackSizingAlgorithm::InitializeTrackSizes() { void GridTrackSizingAlgorithm::InitializeTrackSizes() {
DCHECK(content_sized_tracks_index_.IsEmpty()); DCHECK(content_sized_tracks_index_.IsEmpty());
DCHECK(flexible_sized_tracks_index_.IsEmpty()); DCHECK(flexible_sized_tracks_index_.IsEmpty());
DCHECK(auto_sized_tracks_index_.IsEmpty());
Vector<GridTrack>& track_list = Tracks(direction_); Vector<GridTrack>& track_list = Tracks(direction_);
bool has_definite_free_space = !!AvailableSpace(); bool has_definite_free_space = !!AvailableSpace();
size_t num_tracks = track_list.size(); size_t num_tracks = track_list.size();
...@@ -748,6 +749,8 @@ void GridTrackSizingAlgorithm::InitializeTrackSizes() { ...@@ -748,6 +749,8 @@ void GridTrackSizingAlgorithm::InitializeTrackSizes() {
content_sized_tracks_index_.push_back(i); content_sized_tracks_index_.push_back(i);
if (track_size.MaxTrackBreadth().IsFlex()) if (track_size.MaxTrackBreadth().IsFlex())
flexible_sized_tracks_index_.push_back(i); flexible_sized_tracks_index_.push_back(i);
if (track_size.HasAutoMaxTrackBreadth())
auto_sized_tracks_index_.push_back(i);
} }
} }
...@@ -1300,6 +1303,9 @@ void GridTrackSizingAlgorithm::ComputeFlexSizedTracksGrowth( ...@@ -1300,6 +1303,9 @@ void GridTrackSizingAlgorithm::ComputeFlexSizedTracksGrowth(
void GridTrackSizingAlgorithm::StretchFlexibleTracks( void GridTrackSizingAlgorithm::StretchFlexibleTracks(
Optional<LayoutUnit> free_space) { Optional<LayoutUnit> free_space) {
if (flexible_sized_tracks_index_.IsEmpty())
return;
double flex_fraction = strategy_->FindUsedFlexFraction( double flex_fraction = strategy_->FindUsedFlexFraction(
flexible_sized_tracks_index_, direction_, free_space); flexible_sized_tracks_index_, direction_, free_space);
...@@ -1329,6 +1335,34 @@ void GridTrackSizingAlgorithm::StretchFlexibleTracks( ...@@ -1329,6 +1335,34 @@ void GridTrackSizingAlgorithm::StretchFlexibleTracks(
max_content_size_ += total_growth; max_content_size_ += total_growth;
} }
void GridTrackSizingAlgorithm::StretchAutoTracks() {
if (auto_sized_tracks_index_.IsEmpty())
return;
Optional<LayoutUnit> free_space = FreeSpace(direction_);
if (!free_space || free_space.value() <= 0 ||
(direction_ == kForColumns &&
layout_grid_->StyleRef().ResolvedJustifyContentDistribution(
layout_grid_->ContentAlignmentNormalBehavior()) !=
kContentDistributionStretch) ||
(direction_ == kForRows &&
layout_grid_->StyleRef().ResolvedAlignContentDistribution(
layout_grid_->ContentAlignmentNormalBehavior()) !=
kContentDistributionStretch))
return;
unsigned number_of_auto_sized_tracks = auto_sized_tracks_index_.size();
LayoutUnit size_to_increase =
free_space.value() / number_of_auto_sized_tracks;
Vector<GridTrack>& all_tracks = Tracks(direction_);
for (const auto& track_index : auto_sized_tracks_index_) {
auto& track = all_tracks[track_index];
LayoutUnit base_size = track.BaseSize() + size_to_increase;
track.SetBaseSize(base_size);
}
SetFreeSpace(direction_, LayoutUnit());
}
void GridTrackSizingAlgorithm::AdvanceNextState() { void GridTrackSizingAlgorithm::AdvanceNextState() {
switch (sizing_state_) { switch (sizing_state_) {
case kColumnSizingFirstIteration: case kColumnSizingFirstIteration:
...@@ -1379,6 +1413,7 @@ void GridTrackSizingAlgorithm::Setup(GridTrackSizingDirection direction, ...@@ -1379,6 +1413,7 @@ void GridTrackSizingAlgorithm::Setup(GridTrackSizingDirection direction,
content_sized_tracks_index_.Shrink(0); content_sized_tracks_index_.Shrink(0);
flexible_sized_tracks_index_.Shrink(0); flexible_sized_tracks_index_.Shrink(0);
auto_sized_tracks_index_.Shrink(0);
SetFreeSpace(direction, free_space); SetFreeSpace(direction, free_space);
Tracks(direction).resize(num_tracks); Tracks(direction).resize(num_tracks);
...@@ -1417,11 +1452,11 @@ void GridTrackSizingAlgorithm::Run() { ...@@ -1417,11 +1452,11 @@ void GridTrackSizingAlgorithm::Run() {
? free_space_columns_ ? free_space_columns_
: free_space_rows_); : free_space_rows_);
if (flexible_sized_tracks_index_.IsEmpty())
return;
// Step 4. // Step 4.
StretchFlexibleTracks(initial_free_space); StretchFlexibleTracks(initial_free_space);
// Step 5.
StretchAutoTracks();
} }
void GridTrackSizingAlgorithm::Reset() { void GridTrackSizingAlgorithm::Reset() {
...@@ -1430,6 +1465,7 @@ void GridTrackSizingAlgorithm::Reset() { ...@@ -1430,6 +1465,7 @@ void GridTrackSizingAlgorithm::Reset() {
rows_.Shrink(0); rows_.Shrink(0);
content_sized_tracks_index_.Shrink(0); content_sized_tracks_index_.Shrink(0);
flexible_sized_tracks_index_.Shrink(0); flexible_sized_tracks_index_.Shrink(0);
auto_sized_tracks_index_.Shrink(0);
SetAvailableSpace(kForRows, WTF::nullopt); SetAvailableSpace(kForRows, WTF::nullopt);
SetAvailableSpace(kForColumns, WTF::nullopt); SetAvailableSpace(kForColumns, WTF::nullopt);
} }
......
...@@ -166,6 +166,7 @@ class GridTrackSizingAlgorithm final { ...@@ -166,6 +166,7 @@ class GridTrackSizingAlgorithm final {
void InitializeTrackSizes(); void InitializeTrackSizes();
void ResolveIntrinsicTrackSizes(); void ResolveIntrinsicTrackSizes();
void StretchFlexibleTracks(Optional<LayoutUnit> free_space); void StretchFlexibleTracks(Optional<LayoutUnit> free_space);
void StretchAutoTracks();
// State machine. // State machine.
void AdvanceNextState(); void AdvanceNextState();
...@@ -185,6 +186,7 @@ class GridTrackSizingAlgorithm final { ...@@ -185,6 +186,7 @@ class GridTrackSizingAlgorithm final {
Vector<GridTrack> rows_; Vector<GridTrack> rows_;
Vector<size_t> content_sized_tracks_index_; Vector<size_t> content_sized_tracks_index_;
Vector<size_t> flexible_sized_tracks_index_; Vector<size_t> flexible_sized_tracks_index_;
Vector<size_t> auto_sized_tracks_index_;
GridTrackSizingDirection direction_; GridTrackSizingDirection direction_;
......
...@@ -404,9 +404,6 @@ void LayoutGrid::UpdateBlockLayout(bool relayout_children) { ...@@ -404,9 +404,6 @@ void LayoutGrid::UpdateBlockLayout(bool relayout_children) {
SetLogicalHeight( SetLogicalHeight(
std::max(LogicalHeight(), MinimumLogicalHeightForEmptyLine())); std::max(LogicalHeight(), MinimumLogicalHeightForEmptyLine()));
ApplyStretchAlignmentToTracksIfNeeded(kForColumns);
ApplyStretchAlignmentToTracksIfNeeded(kForRows);
LayoutGridItems(); LayoutGridItems();
track_sizing_algorithm_.Reset(); track_sizing_algorithm_.Reset();
...@@ -1172,44 +1169,6 @@ const StyleContentAlignmentData& LayoutGrid::ContentAlignmentNormalBehavior() { ...@@ -1172,44 +1169,6 @@ const StyleContentAlignmentData& LayoutGrid::ContentAlignmentNormalBehavior() {
return kNormalBehavior; return kNormalBehavior;
} }
void LayoutGrid::ApplyStretchAlignmentToTracksIfNeeded(
GridTrackSizingDirection direction) {
Optional<LayoutUnit> free_space =
track_sizing_algorithm_.FreeSpace(direction);
if (!free_space || free_space.value() <= 0 ||
(direction == kForColumns &&
StyleRef().ResolvedJustifyContentDistribution(
ContentAlignmentNormalBehavior()) != kContentDistributionStretch) ||
(direction == kForRows &&
StyleRef().ResolvedAlignContentDistribution(
ContentAlignmentNormalBehavior()) != kContentDistributionStretch))
return;
// Spec defines auto-sized tracks as the ones with an 'auto' max-sizing
// function.
Vector<GridTrack>& all_tracks = track_sizing_algorithm_.Tracks(direction);
Vector<unsigned> auto_sized_tracks_index;
for (unsigned i = 0; i < all_tracks.size(); ++i) {
const GridTrackSize& track_size =
track_sizing_algorithm_.GetGridTrackSize(direction, i);
if (track_size.HasAutoMaxTrackBreadth())
auto_sized_tracks_index.push_back(i);
}
unsigned number_of_auto_sized_tracks = auto_sized_tracks_index.size();
if (number_of_auto_sized_tracks < 1)
return;
LayoutUnit size_to_increase =
free_space.value() / number_of_auto_sized_tracks;
for (const auto& track_index : auto_sized_tracks_index) {
GridTrack* track = all_tracks.data() + track_index;
LayoutUnit base_size = track->BaseSize() + size_to_increase;
track->SetBaseSize(base_size);
}
track_sizing_algorithm_.SetFreeSpace(direction, LayoutUnit());
}
void LayoutGrid::LayoutGridItems() { void LayoutGrid::LayoutGridItems() {
PopulateGridPositionsForDirection(kForColumns); PopulateGridPositionsForDirection(kForColumns);
PopulateGridPositionsForDirection(kForRows); PopulateGridPositionsForDirection(kForRows);
......
...@@ -106,6 +106,8 @@ class LayoutGrid final : public LayoutBlock { ...@@ -106,6 +106,8 @@ class LayoutGrid final : public LayoutBlock {
LayoutUnit GridGap(GridTrackSizingDirection) const; LayoutUnit GridGap(GridTrackSizingDirection) const;
LayoutUnit GridItemOffset(GridTrackSizingDirection) const; LayoutUnit GridItemOffset(GridTrackSizingDirection) const;
static const StyleContentAlignmentData& ContentAlignmentNormalBehavior();
protected: protected:
ItemPosition SelfAlignmentNormalBehavior( ItemPosition SelfAlignmentNormalBehavior(
const LayoutBox* child = nullptr) const override { const LayoutBox* child = nullptr) const override {
...@@ -211,8 +213,6 @@ class LayoutGrid final : public LayoutBlock { ...@@ -211,8 +213,6 @@ class LayoutGrid final : public LayoutBlock {
const LayoutBox&, const LayoutBox&,
GridTrackSizingDirection) const; GridTrackSizingDirection) const;
void ApplyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection);
void PaintChildren(const PaintInfo&, const LayoutPoint&) const override; void PaintChildren(const PaintInfo&, const LayoutPoint&) const override;
LayoutUnit AvailableAlignmentSpaceForChildBeforeStretching( LayoutUnit AvailableAlignmentSpaceForChildBeforeStretching(
...@@ -297,7 +297,6 @@ class LayoutGrid final : public LayoutBlock { ...@@ -297,7 +297,6 @@ class LayoutGrid final : public LayoutBlock {
LineDirectionMode); LineDirectionMode);
static LayoutUnit SynthesizedBaselineFromBorderBox(const LayoutBox&, static LayoutUnit SynthesizedBaselineFromBorderBox(const LayoutBox&,
LineDirectionMode); LineDirectionMode);
static const StyleContentAlignmentData& ContentAlignmentNormalBehavior();
typedef HashMap<unsigned, typedef HashMap<unsigned,
std::unique_ptr<BaselineContext>, std::unique_ptr<BaselineContext>,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment