[CSS Grid Layout] Add support for max-content
[WebKit-https.git] / Source / WebCore / rendering / RenderGrid.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "RenderGrid.h"
28
29 #include "LayoutRepainter.h"
30 #include "NotImplemented.h"
31 #include "RenderLayer.h"
32 #include "RenderView.h"
33
34 namespace WebCore {
35
36 static const int infinity = intMaxForLayoutUnit;
37
38 class GridTrack {
39 public:
40     GridTrack()
41         : m_usedBreadth(0)
42         , m_maxBreadth(0)
43     {
44     }
45
46     LayoutUnit maxBreadthIfNotInfinite() const
47     {
48         return (m_maxBreadth == infinity) ? m_usedBreadth : m_maxBreadth;
49     }
50
51     LayoutUnit m_usedBreadth;
52     LayoutUnit m_maxBreadth;
53 };
54
55 RenderGrid::RenderGrid(Element* element)
56     : RenderBlock(element)
57 {
58     // All of our children must be block level.
59     setChildrenInline(false);
60 }
61
62 RenderGrid::~RenderGrid()
63 {
64 }
65
66 void RenderGrid::layoutBlock(bool relayoutChildren, LayoutUnit)
67 {
68     ASSERT(needsLayout());
69
70     if (!relayoutChildren && simplifiedLayout())
71         return;
72
73     // FIXME: Much of this method is boiler plate that matches RenderBox::layoutBlock and Render*FlexibleBox::layoutBlock.
74     // It would be nice to refactor some of the duplicate code.
75     LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
76     LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
77
78     if (inRenderFlowThread()) {
79         // Regions changing widths can force us to relayout our children.
80         if (logicalWidthChangedInRegions())
81             relayoutChildren = true;
82     }
83     updateRegionsAndExclusionsLogicalSize();
84
85     LayoutSize previousSize = size();
86
87     setLogicalHeight(0);
88     updateLogicalWidth();
89
90     m_overflow.clear();
91
92     layoutGridItems();
93
94     LayoutUnit oldClientAfterEdge = clientLogicalBottom();
95     updateLogicalHeight();
96
97     if (size() != previousSize)
98         relayoutChildren = true;
99
100     layoutPositionedObjects(relayoutChildren || isRoot());
101
102     computeRegionRangeForBlock();
103
104     computeOverflow(oldClientAfterEdge);
105     statePusher.pop();
106
107     updateLayerTransform();
108
109     // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
110     // we overflow or not.
111     if (hasOverflowClip())
112         layer()->updateScrollInfoAfterLayout();
113
114     repainter.repaintAfterLayout();
115
116     setNeedsLayout(false);
117 }
118
119 void RenderGrid::computePreferredLogicalWidths()
120 {
121     ASSERT(preferredLogicalWidthsDirty());
122
123     m_minPreferredLogicalWidth = 0;
124     m_maxPreferredLogicalWidth = 0;
125
126     // FIXME: We don't take our own logical width into account.
127
128     const Vector<GridTrackSize>& trackStyles = style()->gridColumns();
129
130     for (size_t i = 0; i < trackStyles.size(); ++i) {
131         const Length& minTrackLength = trackStyles[i].minTrackBreadth();
132         const Length& maxTrackLength = trackStyles[i].maxTrackBreadth();
133         // FIXME: Handle only one fixed length properly (e.g minmax(100px, max-content)).
134         if (!minTrackLength.isFixed() || !maxTrackLength.isFixed()) {
135             notImplemented();
136             continue;
137         }
138
139         LayoutUnit minTrackBreadth = minTrackLength.intValue();
140         LayoutUnit maxTrackBreadth = maxTrackLength.intValue();
141
142         maxTrackBreadth = std::max(maxTrackBreadth, minTrackBreadth);
143
144         m_minPreferredLogicalWidth += minTrackBreadth;
145         m_maxPreferredLogicalWidth += maxTrackBreadth;
146
147         // FIXME: This should add in the scrollbarWidth (e.g. see RenderFlexibleBox).
148     }
149
150     // FIXME: We should account for min / max logical width.
151
152     LayoutUnit borderAndPaddingInInlineDirection = borderAndPaddingLogicalWidth();
153     m_minPreferredLogicalWidth += borderAndPaddingInInlineDirection;
154     m_maxPreferredLogicalWidth += borderAndPaddingInInlineDirection;
155
156     setPreferredLogicalWidthsDirty(false);
157 }
158
159 void RenderGrid::computedUsedBreadthOfGridTracks(TrackSizingDirection direction, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks)
160 {
161     const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
162     LayoutUnit availableLogicalSpace = (direction == ForColumns) ? availableLogicalWidth() : availableLogicalHeight(IncludeMarginBorderPadding);
163     Vector<GridTrack>& tracks = (direction == ForColumns) ? columnTracks : rowTracks;
164     for (size_t i = 0; i < trackStyles.size(); ++i) {
165         GridTrack track;
166         const Length& minTrackBreadth = trackStyles[i].minTrackBreadth();
167         const Length& maxTrackBreadth = trackStyles[i].maxTrackBreadth();
168
169         track.m_usedBreadth = computeUsedBreadthOfMinLength(direction, minTrackBreadth);
170         track.m_maxBreadth = computeUsedBreadthOfMaxLength(direction, maxTrackBreadth);
171
172         track.m_maxBreadth = std::max(track.m_maxBreadth, track.m_usedBreadth);
173
174         availableLogicalSpace -= track.m_usedBreadth;
175
176         tracks.append(track);
177     }
178
179     // FIXME: We shouldn't call resolveContentBasedTrackSizingFunctions if we have no min-content / max-content tracks.
180     resolveContentBasedTrackSizingFunctions(direction, columnTracks, rowTracks, availableLogicalSpace);
181
182     if (availableLogicalSpace <= 0)
183         return;
184
185     distributeSpaceToTracks(direction, tracks, availableLogicalSpace);
186 }
187
188 LayoutUnit RenderGrid::computeUsedBreadthOfMinLength(TrackSizingDirection direction, const Length& trackLength) const
189 {
190     if (trackLength.isFixed() || trackLength.isPercent() || trackLength.isViewportPercentage())
191         return computeUsedBreadthOfSpecifiedLength(direction, trackLength);
192
193     ASSERT(trackLength.isMinContent() || trackLength.isMaxContent() || trackLength.isAuto());
194     return 0;
195 }
196
197 LayoutUnit RenderGrid::computeUsedBreadthOfMaxLength(TrackSizingDirection direction, const Length& trackLength) const
198 {
199     if (trackLength.isFixed() || trackLength.isPercent() || trackLength.isViewportPercentage()) {
200         LayoutUnit computedBreadth = computeUsedBreadthOfSpecifiedLength(direction, trackLength);
201         // FIXME: We should ASSERT that computedBreadth cannot return infinity but it's currently
202         // possible. See https://bugs.webkit.org/show_bug.cgi?id=107053
203         return computedBreadth;
204     }
205
206     ASSERT(trackLength.isMinContent() || trackLength.isMaxContent() || trackLength.isAuto());
207     return infinity;
208 }
209
210 LayoutUnit RenderGrid::computeUsedBreadthOfSpecifiedLength(TrackSizingDirection direction, const Length& trackLength) const
211 {
212     // FIXME: We still need to support calc() here (https://webkit.org/b/103761).
213     ASSERT(trackLength.isFixed() || trackLength.isPercent() || trackLength.isViewportPercentage());
214     return valueForLength(trackLength, direction == ForColumns ? logicalWidth() : computeContentLogicalHeight(MainOrPreferredSize, style()->logicalHeight()), view());
215 }
216
217 static bool sortByGridTrackGrowthPotential(GridTrack* track1, GridTrack* track2)
218 {
219     return (track1->m_maxBreadth - track1->m_usedBreadth) <= (track2->m_maxBreadth - track2->m_usedBreadth);
220 }
221
222 LayoutUnit RenderGrid::minContentForChild(RenderBox* child, TrackSizingDirection direction, Vector<GridTrack>& columnTracks)
223 {
224     bool hasOrthogonalWritingMode = child->isHorizontalWritingMode() != isHorizontalWritingMode();
225     // FIXME: Properly support orthogonal writing mode.
226     if (hasOrthogonalWritingMode)
227         return 0;
228
229     if (direction == ForColumns) {
230         // FIXME: It's unclear if we should return the intrinsic width or the preferred width.
231         // See http://lists.w3.org/Archives/Public/www-style/2013Jan/0245.html
232         return child->minPreferredLogicalWidth();
233     }
234
235     if (child->needsLayout()) {
236         size_t columnTrack = resolveGridPosition(ForColumns, child);
237         child->setOverrideContainingBlockContentLogicalWidth(columnTracks[columnTrack].m_usedBreadth);
238         child->layout();
239     }
240     return child->logicalHeight();
241 }
242
243 LayoutUnit RenderGrid::maxContentForChild(RenderBox* child, TrackSizingDirection direction, Vector<GridTrack>& columnTracks)
244 {
245     bool hasOrthogonalWritingMode = child->isHorizontalWritingMode() != isHorizontalWritingMode();
246     // FIXME: Properly support orthogonal writing mode.
247     if (hasOrthogonalWritingMode)
248         return LayoutUnit();
249
250     if (direction == ForColumns) {
251         // FIXME: It's unclear if we should return the intrinsic width or the preferred width.
252         // See http://lists.w3.org/Archives/Public/www-style/2013Jan/0245.html
253         return child->maxPreferredLogicalWidth();
254     }
255
256     if (child->needsLayout()) {
257         size_t columnTrack = resolveGridPosition(ForColumns, child);
258         child->setOverrideContainingBlockContentLogicalWidth(columnTracks[columnTrack].m_usedBreadth);
259         child->layout();
260     }
261     return child->logicalHeight();
262 }
263
264 void RenderGrid::resolveContentBasedTrackSizingFunctions(TrackSizingDirection direction, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace)
265 {
266     // FIXME: Split the grid tracks once we support spanning or fractions (step 1 and 2 of the algorithm).
267
268     const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
269     Vector<GridTrack>& tracks = (direction == ForColumns) ? columnTracks : rowTracks;
270
271     for (size_t i = 0; i < tracks.size(); ++i) {
272         GridTrack& track = tracks[i];
273         const Length& minTrackBreadth = trackStyles[i].minTrackBreadth();
274         if (minTrackBreadth.isMinContent() || minTrackBreadth.isMaxContent()) {
275             // FIXME: The specification factors this logic into resolveContentBasedTrackSizingFunctionsForItems
276             // to reuse code between the branches and also calls distributeSpaceToTracks.
277             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
278                 size_t cellIndex = resolveGridPosition(direction, child);
279                 if (cellIndex != i)
280                     continue;
281
282                 LayoutUnit additionalBreadthSpace = minContentForChild(child, direction, columnTracks) - track.m_usedBreadth;
283                 ASSERT(additionalBreadthSpace >= 0);
284                 track.m_usedBreadth += additionalBreadthSpace;
285                 availableLogicalSpace -= additionalBreadthSpace;
286             }
287         }
288         if (minTrackBreadth.isMaxContent()) {
289             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
290                 size_t cellIndex = resolveGridPosition((direction == ForColumns) ? child->style()->gridItemColumn() : child->style()->gridItemRow());
291                 if (cellIndex != i)
292                     continue;
293
294                 LayoutUnit additionalBreadthSpace = maxContentForChild(child, direction, columnTracks) - track.m_usedBreadth;
295                 ASSERT(additionalBreadthSpace >= 0);
296                 track.m_usedBreadth += additionalBreadthSpace;
297                 availableLogicalSpace -= additionalBreadthSpace;
298             }
299         }
300
301         const Length& maxTrackBreadth = trackStyles[i].maxTrackBreadth();
302         if (maxTrackBreadth.isMinContent() || maxTrackBreadth.isMaxContent()) {
303             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
304                 size_t cellIndex = resolveGridPosition(direction, child);
305                 if (cellIndex != i)
306                     continue;
307
308                 LayoutUnit additionalBreadthSpace = minContentForChild(child, direction, columnTracks) - track.maxBreadthIfNotInfinite();
309                 LayoutUnit share = std::min(additionalBreadthSpace, tracks[i].m_maxBreadth - track.maxBreadthIfNotInfinite());
310                 if (track.m_maxBreadth == infinity)
311                     track.m_maxBreadth = track.m_usedBreadth + share;
312                 else
313                     track.m_maxBreadth += share;
314             }
315         }
316
317         if (maxTrackBreadth.isMaxContent()) {
318             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
319                 size_t cellIndex = resolveGridPosition((direction == ForColumns) ? child->style()->gridItemColumn() : child->style()->gridItemRow());
320                 if (cellIndex != i)
321                     continue;
322
323                 LayoutUnit additionalBreadthSpace = maxContentForChild(child, direction, columnTracks) - track.maxBreadthIfNotInfinite();
324                 LayoutUnit share = std::min(additionalBreadthSpace, tracks[i].m_maxBreadth - track.maxBreadthIfNotInfinite());
325                 if (track.m_maxBreadth == infinity)
326                     track.m_maxBreadth = track.m_usedBreadth + share;
327                 else
328                     track.m_maxBreadth += share;
329             }
330         }
331     }
332
333     // FIXME: The spec says to update maxBreadth if it is Infinity.
334 }
335
336 void RenderGrid::distributeSpaceToTracks(TrackSizingDirection, Vector<GridTrack>& tracks, LayoutUnit availableLogicalSpace)
337 {
338     const size_t tracksSize = tracks.size();
339     Vector<GridTrack*> sortedTracks(tracksSize);
340     for (size_t i = 0; i < tracksSize; ++i)
341         sortedTracks[i] = tracks.data() + i;
342
343     std::sort(sortedTracks.begin(), sortedTracks.end(), sortByGridTrackGrowthPotential);
344
345     for (size_t i = 0; i < tracksSize; ++i) {
346         GridTrack& track = *sortedTracks[i];
347         LayoutUnit availableLogicalSpaceShare = availableLogicalSpace / (tracksSize - i);
348         // We never shrink the used breadth by clamping the difference between max and used breadth. The spec uses
349         // 2 extra-variables and 2 extra iterations over the tracks to achieve that (because distributeSpaceToTracks
350         // is shared with other methods). If we start sharing the method, we should probably remove this clamping.
351         LayoutUnit growthShare = std::min(availableLogicalSpaceShare, std::max(LayoutUnit(0), track.m_maxBreadth - track.m_usedBreadth));
352         track.m_usedBreadth += growthShare;
353         availableLogicalSpace -= growthShare;
354     }
355
356     // FIXME: We don't implement the last 2 steps of the algorithm as we don't implement content based sizing.
357 }
358
359 #ifndef NDEBUG
360 bool RenderGrid::tracksAreWiderThanMinTrackBreadth(TrackSizingDirection direction, const Vector<GridTrack>& tracks)
361 {
362     const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
363     for (size_t i = 0; i < trackStyles.size(); ++i) {
364         const Length& minTrackBreadth = trackStyles[i].minTrackBreadth();
365         if (computeUsedBreadthOfMinLength(direction, minTrackBreadth) > tracks[i].m_usedBreadth)
366             return false;
367     }
368     return true;
369 }
370 #endif
371
372 void RenderGrid::layoutGridItems()
373 {
374     Vector<GridTrack> columnTracks, rowTracks;
375     computedUsedBreadthOfGridTracks(ForColumns, columnTracks, rowTracks);
376     ASSERT(tracksAreWiderThanMinTrackBreadth(ForColumns, columnTracks));
377     computedUsedBreadthOfGridTracks(ForRows, columnTracks, rowTracks);
378     ASSERT(tracksAreWiderThanMinTrackBreadth(ForRows, rowTracks));
379
380     for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
381         LayoutPoint childPosition = findChildLogicalPosition(child, columnTracks, rowTracks);
382
383         size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
384         size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
385
386         // FIXME: Properly support implicit rows and columns (bug 103573).
387         if (columnTrack < columnTracks.size() && rowTrack < rowTracks.size()) {
388             // Because the grid area cannot be styled, we don't need to adjust
389             // the grid breadth to account for 'box-sizing'.
390             LayoutUnit oldOverrideContainingBlockContentLogicalWidth = child->hasOverrideContainingBlockLogicalWidth() ? child->overrideContainingBlockContentLogicalWidth() : LayoutUnit();
391             LayoutUnit oldOverrideContainingBlockContentLogicalHeight = child->hasOverrideContainingBlockLogicalHeight() ? child->overrideContainingBlockContentLogicalHeight() : LayoutUnit();
392
393             if (oldOverrideContainingBlockContentLogicalWidth != columnTracks[columnTrack].m_usedBreadth || oldOverrideContainingBlockContentLogicalHeight != rowTracks[rowTrack].m_usedBreadth)
394                 child->setNeedsLayout(true, MarkOnlyThis);
395
396             child->setOverrideContainingBlockContentLogicalWidth(columnTracks[columnTrack].m_usedBreadth);
397             child->setOverrideContainingBlockContentLogicalHeight(rowTracks[rowTrack].m_usedBreadth);
398
399         }
400
401         // FIXME: Grid items should stretch to fill their cells. Once we
402         // implement grid-{column,row}-align, we can also shrink to fit. For
403         // now, just size as if we were a regular child.
404         child->layoutIfNeeded();
405
406         // FIXME: Handle border & padding on the grid element.
407         child->setLogicalLocation(childPosition);
408     }
409
410     for (size_t i = 0; i < rowTracks.size(); ++i)
411         setLogicalHeight(logicalHeight() + rowTracks[i].m_usedBreadth);
412
413     // FIXME: We should handle min / max logical height.
414
415     setLogicalHeight(logicalHeight() + borderAndPaddingLogicalHeight());
416 }
417
418 size_t RenderGrid::resolveGridPosition(TrackSizingDirection direction, const RenderObject* gridItem) const
419 {
420     const GridPosition& position = (direction == ForColumns) ? gridItem->style()->gridItemColumn() : gridItem->style()->gridItemRow();
421     return resolveGridPosition(position);
422 }
423
424 size_t RenderGrid::resolveGridPosition(const GridPosition& position) const
425 {
426     // FIXME: Handle other values for grid-{row,column} like ranges or line names.
427     switch (position.type()) {
428     case IntegerPosition:
429         // FIXME: What does a non-positive integer mean for a column/row?
430         if (!position.isPositive())
431             return 0;
432
433         return position.integerPosition() - 1;
434     case AutoPosition:
435         // FIXME: We should follow 'grid-auto-flow' for resolution.
436         // Until then, we use the 'grid-auto-flow: none' behavior (which is the default)
437         // and resolve 'auto' as the first row / column.
438         return 0;
439     }
440     ASSERT_NOT_REACHED();
441     return 0;
442 }
443
444 LayoutPoint RenderGrid::findChildLogicalPosition(RenderBox* child, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks)
445 {
446     size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
447     size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
448
449     LayoutPoint offset;
450     // FIXME: |columnTrack| and |rowTrack| should be smaller than our column / row count.
451     for (size_t i = 0; i < columnTrack && i < columnTracks.size(); ++i)
452         offset.setX(offset.x() + columnTracks[i].m_usedBreadth);
453     for (size_t i = 0; i < rowTrack && i < rowTracks.size(); ++i)
454         offset.setY(offset.y() + rowTracks[i].m_usedBreadth);
455
456     // FIXME: Handle margins on the grid item.
457     return offset;
458 }
459
460 const char* RenderGrid::renderName() const
461 {
462     if (isFloating())
463         return "RenderGrid (floating)";
464     if (isOutOfFlowPositioned())
465         return "RenderGrid (positioned)";
466     if (isAnonymous())
467         return "RenderGrid (generated)";
468     if (isRelPositioned())
469         return "RenderGrid (relative positioned)";
470     return "RenderGrid";
471 }
472
473 } // namespace WebCore