Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Unified Diff: third_party/WebKit/Source/core/layout/LayoutGrid.cpp

Issue 1895443002: [css-grid] Implement auto-repeat computation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Missing tests. Applied suggested changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/layout/LayoutGrid.cpp
diff --git a/third_party/WebKit/Source/core/layout/LayoutGrid.cpp b/third_party/WebKit/Source/core/layout/LayoutGrid.cpp
index cfde1c770390d00cb5a418ebb7f1d54fd22443fa..ebd706079a81331945454b659f20ac3d16dbcccf 100644
--- a/third_party/WebKit/Source/core/layout/LayoutGrid.cpp
+++ b/third_party/WebKit/Source/core/layout/LayoutGrid.cpp
@@ -301,7 +301,8 @@ void LayoutGrid::styleDidChange(StyleDifference diff, const ComputedStyle* oldSt
if (explicitGridDidResize(*oldStyle)
|| namedGridLinesDefinitionDidChange(*oldStyle)
- || oldStyle->getGridAutoFlow() != styleRef().getGridAutoFlow())
+ || oldStyle->getGridAutoFlow() != styleRef().getGridAutoFlow()
+ || (diff.needsLayout() && (styleRef().gridAutoRepeatColumns().size() || styleRef().gridAutoRepeatRows().size())))
Manuel Rego 2016/04/19 17:22:15 We still don't have tests for this.
svillar 2016/04/20 08:24:18 Acknowledged.
dirtyGrid();
}
@@ -310,7 +311,9 @@ bool LayoutGrid::explicitGridDidResize(const ComputedStyle& oldStyle) const
return oldStyle.gridTemplateColumns().size() != styleRef().gridTemplateColumns().size()
|| oldStyle.gridTemplateRows().size() != styleRef().gridTemplateRows().size()
|| oldStyle.namedGridAreaColumnCount() != styleRef().namedGridAreaColumnCount()
- || oldStyle.namedGridAreaRowCount() != styleRef().namedGridAreaRowCount();
+ || oldStyle.namedGridAreaRowCount() != styleRef().namedGridAreaRowCount()
+ || oldStyle.gridAutoRepeatColumns().size() != styleRef().gridAutoRepeatColumns().size()
+ || oldStyle.gridAutoRepeatRows().size() != styleRef().gridAutoRepeatRows().size();
Manuel Rego 2016/04/19 17:22:15 Ditto.
svillar 2016/04/20 08:24:18 Acknowledged.
}
bool LayoutGrid::namedGridLinesDefinitionDidChange(const ComputedStyle& oldStyle) const
@@ -694,13 +697,36 @@ static bool shouldClearOverrideContainingBlockContentSizeForChild(const LayoutBo
return child.hasRelativeLogicalHeight() || child.styleRef().logicalHeight().isIntrinsicOrAuto();
}
+const GridTrackSize& LayoutGrid::unresolvedGridTrackSize(GridTrackSizingDirection direction, size_t untranslatedIndex) const
Manuel Rego 2016/04/19 17:22:16 I've just realized that "untranslatedIndex" should
svillar 2016/04/20 08:24:18 Perhaps the name is not good enough but we're pass
Manuel Rego 2016/04/20 09:57:44 Yeah, so I got confused because the names are (and
+{
+ bool isRowAxis = direction == ForColumns;
+ const Vector<GridTrackSize>& trackStyles = isRowAxis ? styleRef().gridTemplateColumns() : styleRef().gridTemplateRows();
+ const Vector<GridTrackSize>& autoRepeatTrackStyles = isRowAxis ? styleRef().gridAutoRepeatColumns() : styleRef().gridAutoRepeatRows();
+ const GridTrackSize& autoTrackSize = isRowAxis ? styleRef().gridAutoColumns() : styleRef().gridAutoRows();
+ size_t insertionPoint = isRowAxis ? styleRef().gridAutoRepeatColumnsInsertionPoint() : styleRef().gridAutoRepeatRowsInsertionPoint();
+ size_t repetitions = autoRepeatCountForDirection(direction);
+
+ // We should not use GridPositionsResolver::explicitGridXXXCount() for this because the
+ // explicit grid might be larger than the number of tracks in grid-template-rows|columns (if
+ // grid-template-areas is specified for example).
+ size_t explicitTracksCount = trackStyles.size() + repetitions;
+
+ int translatedIndex = untranslatedIndex + (isRowAxis ? m_smallestColumnStart : m_smallestRowStart);
+ if (translatedIndex < 0 || translatedIndex >= explicitTracksCount)
+ return autoTrackSize;
+
+ if (LIKELY(!repetitions) || translatedIndex < insertionPoint)
+ return trackStyles[translatedIndex];
+
+ if (translatedIndex < (insertionPoint + repetitions))
+ return autoRepeatTrackStyles[0];
+
+ return trackStyles[translatedIndex - repetitions];
+}
+
GridTrackSize LayoutGrid::gridTrackSize(GridTrackSizingDirection direction, size_t i) const
{
- bool isForColumns = direction == ForColumns;
- const Vector<GridTrackSize>& trackStyles = isForColumns ? style()->gridTemplateColumns() : style()->gridTemplateRows();
- const GridTrackSize& autoTrackSize = isForColumns ? style()->gridAutoColumns() : style()->gridAutoRows();
- int translatedIndex = i + (isForColumns ? m_smallestColumnStart : m_smallestRowStart);
- const GridTrackSize& trackSize = (translatedIndex < 0 || translatedIndex >= static_cast<int>(trackStyles.size())) ? autoTrackSize : trackStyles[translatedIndex];
+ const GridTrackSize& trackSize = unresolvedGridTrackSize(direction, i);
GridLength minTrackBreadth = trackSize.minTrackBreadth();
GridLength maxTrackBreadth = trackSize.maxTrackBreadth();
@@ -1182,8 +1208,79 @@ void LayoutGrid::insertItemIntoGrid(LayoutBox& child, const GridArea& area)
size_t LayoutGrid::computeAutoRepeatTracksCount(GridTrackSizingDirection direction) const
{
- // TODO(svillar): implement the algorithm to compute the number of auto repeat tracks.
- return 0;
+ bool isRowAxis = direction == ForColumns;
+ const auto& autoRepeatTracks = isRowAxis ? styleRef().gridAutoRepeatColumns() : styleRef().gridAutoRepeatRows();
+
+ if (!autoRepeatTracks.size())
+ return 0;
+
+ DCHECK_EQ(autoRepeatTracks.size(), 1);
+ auto autoTrackSize = autoRepeatTracks.at(0);
+ DCHECK(autoTrackSize.minTrackBreadth().isLength());
+ DCHECK(!autoTrackSize.minTrackBreadth().isContentSized());
+
+ LayoutUnit availableSize = isRowAxis ? availableLogicalWidth() : computeContentLogicalHeight(MainOrPreferredSize, styleRef().logicalHeight(), LayoutUnit(-1));
Manuel Rego 2016/04/19 17:22:16 Couldn't you use availableLogicalHeight()?
svillar 2016/04/20 08:24:18 No because it does not return -1 for indefinite he
+ if (availableSize == -1) {
+ const Length& maxLength = isRowAxis ? styleRef().logicalMaxWidth() : styleRef().logicalMaxHeight();
+ if (!maxLength.isMaxSizeNone()) {
+ LayoutUnit maxSize = isRowAxis
+ ? computeLogicalWidthUsing(MaxSize, maxLength, containingBlockLogicalWidthForContent(), containingBlock())
+ : computeContentLogicalHeight(MaxSize, maxLength, LayoutUnit(-1));
+ if (maxSize != -1)
+ availableSize = std::min(availableSize, maxSize);
+ }
+ } else {
+ availableSize = isRowAxis
+ ? constrainLogicalWidthByMinMax(availableSize, availableLogicalWidth(), containingBlock())
+ : constrainLogicalHeightByMinMax(availableSize, LayoutUnit(-1));
+ }
+
+ bool needsToFulfillMinimumSize = false;
+ bool indefiniteMainAndMaxSizes = availableSize == LayoutUnit(-1);
+ if (indefiniteMainAndMaxSizes) {
+ const Length& minSize = isRowAxis ? styleRef().logicalMinWidth() : styleRef().logicalMinHeight();
+ if (!minSize.isSpecified())
+ return 1;
+
+ LayoutUnit containingBlockAvailableSize = isRowAxis ? containingBlockLogicalWidthForContent() : containingBlockLogicalHeightForContent(ExcludeMarginBorderPadding);
+ availableSize = valueForLength(minSize, containingBlockAvailableSize);
Manuel Rego 2016/04/19 17:22:16 Why the difference to get the max vs min size. A
svillar 2016/04/20 08:24:18 I'm mostly doing the same. I could have used compu
+ needsToFulfillMinimumSize = true;
+ }
+
+ bool hasDefiniteMaxTrackSizingFunction = autoTrackSize.maxTrackBreadth().isLength() && !autoTrackSize.maxTrackBreadth().isContentSized();
+ const Length trackLength = hasDefiniteMaxTrackSizingFunction ? autoTrackSize.maxTrackBreadth().length() : autoTrackSize.minTrackBreadth().length();
+ // For the purpose of finding the number of auto-repeated tracks, the UA must floor the track size to a UA-specified
+ // value to avoid division by zero. It is suggested that this floor be 1px.
+ LayoutUnit autoRepeatTrackSize = std::max<LayoutUnit>(LayoutUnit(1), valueForLength(trackLength, availableSize));
+
+ // There will be always at least 1 auto-repeat track, so take it already into account when computing the total track size
Manuel Rego 2016/04/19 17:22:16 Nit: Missing dot at the end.
svillar 2016/04/20 08:24:18 Acknowledged.
+ LayoutUnit tracksSize = autoRepeatTrackSize;
+ const Vector<GridTrackSize>& trackSizes = isRowAxis ? styleRef().gridTemplateColumns() : styleRef().gridTemplateRows();
+
+ for (const auto& track : trackSizes) {
+ bool hasDefiniteMaxTrackBreadth = track.maxTrackBreadth().isLength() && !track.maxTrackBreadth().isContentSized();
+ DCHECK(hasDefiniteMaxTrackBreadth || (track.minTrackBreadth().isLength() && !track.minTrackBreadth().isContentSized()));
+ tracksSize += valueForLength(hasDefiniteMaxTrackBreadth ? track.maxTrackBreadth().length() : track.minTrackBreadth().length(), availableSize);
+ }
+
+ // Add gutters as if there where only 1 auto repeat track. Gaps between auto repeat tracks will be added later when
+ // computing the repetitions.
+ LayoutUnit gapSize = guttersSize(direction, 2);
+ tracksSize += gapSize * trackSizes.size();
+
+ LayoutUnit freeSpace = availableSize - tracksSize;
+ if (freeSpace <= 0)
+ return 1;
+
+ size_t repetitions = 1 + (freeSpace / (autoRepeatTrackSize + gapSize)).toInt();
+
+ // Otherwise, if the grid container has a definite min size in the relevant axis, the
+ // number of repetitions is the largest possible positive integer that fulfills that
+ // minimum requirement.
+ if (needsToFulfillMinimumSize)
+ ++repetitions;
+
+ return repetitions;
}
void LayoutGrid::placeItemsOnGrid()

Powered by Google App Engine
This is Rietveld 408576698