18629b80795f681fd66171a26cf6a42a02eaaefc
[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 void RenderGrid::resolveContentBasedTrackSizingFunctions(TrackSizingDirection direction, Vector<GridTrack>& columnTracks, Vector<GridTrack>& rowTracks, LayoutUnit& availableLogicalSpace)
244 {
245     // FIXME: Split the grid tracks once we support spanning or fractions (step 1 and 2 of the algorithm).
246
247     const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
248     Vector<GridTrack>& tracks = (direction == ForColumns) ? columnTracks : rowTracks;
249
250     for (size_t i = 0; i < tracks.size(); ++i) {
251         GridTrack& track = tracks[i];
252         // FIXME: Add support MaxContent.
253         const Length& minTrackBreadth = trackStyles[i].minTrackBreadth();
254         if (minTrackBreadth.isMinContent()) {
255             // FIXME: The specification factors this logic into resolveContentBasedTrackSizingFunctionsForItems
256             // to reuse code between the branches and also calls distributeSpaceToTracks.
257             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
258                 size_t cellIndex = resolveGridPosition(direction, child);
259                 if (cellIndex != i)
260                     continue;
261
262                 LayoutUnit additionalBreadthSpace = minContentForChild(child, direction, columnTracks) - track.m_usedBreadth;
263                 ASSERT(additionalBreadthSpace >= 0);
264                 track.m_usedBreadth += additionalBreadthSpace;
265                 availableLogicalSpace -= additionalBreadthSpace;
266             }
267         }
268
269         const Length& maxTrackBreadth = trackStyles[i].maxTrackBreadth();
270         if (maxTrackBreadth.isMinContent()) {
271             for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
272                 size_t cellIndex = resolveGridPosition(direction, child);
273                 if (cellIndex != i)
274                     continue;
275
276                 LayoutUnit additionalBreadthSpace = minContentForChild(child, direction, columnTracks) - track.maxBreadthIfNotInfinite();
277                 LayoutUnit share = std::min(additionalBreadthSpace, tracks[i].m_maxBreadth - track.maxBreadthIfNotInfinite());
278                 if (track.m_maxBreadth == infinity)
279                     track.m_maxBreadth = track.m_usedBreadth + share;
280                 else
281                     track.m_maxBreadth += share;
282             }
283         }
284     }
285
286     // FIXME: The spec says to update maxBreadth if it is Infinity.
287 }
288
289 void RenderGrid::distributeSpaceToTracks(TrackSizingDirection, Vector<GridTrack>& tracks, LayoutUnit availableLogicalSpace)
290 {
291     const size_t tracksSize = tracks.size();
292     Vector<GridTrack*> sortedTracks(tracksSize);
293     for (size_t i = 0; i < tracksSize; ++i)
294         sortedTracks[i] = tracks.data() + i;
295
296     std::sort(sortedTracks.begin(), sortedTracks.end(), sortByGridTrackGrowthPotential);
297
298     for (size_t i = 0; i < tracksSize; ++i) {
299         GridTrack& track = *sortedTracks[i];
300         LayoutUnit availableLogicalSpaceShare = availableLogicalSpace / (tracksSize - i);
301         // We never shrink the used breadth by clamping the difference between max and used breadth. The spec uses
302         // 2 extra-variables and 2 extra iterations over the tracks to achieve that (because distributeSpaceToTracks
303         // is shared with other methods). If we start sharing the method, we should probably remove this clamping.
304         LayoutUnit growthShare = std::min(availableLogicalSpaceShare, std::max(LayoutUnit(0), track.m_maxBreadth - track.m_usedBreadth));
305         track.m_usedBreadth += growthShare;
306         availableLogicalSpace -= growthShare;
307     }
308
309     // FIXME: We don't implement the last 2 steps of the algorithm as we don't implement content based sizing.
310 }
311
312 #ifndef NDEBUG
313 bool RenderGrid::tracksAreWiderThanMinTrackBreadth(TrackSizingDirection direction, const Vector<GridTrack>& tracks)
314 {
315     const Vector<GridTrackSize>& trackStyles = (direction == ForColumns) ? style()->gridColumns() : style()->gridRows();
316     for (size_t i = 0; i < trackStyles.size(); ++i) {
317         const Length& minTrackBreadth = trackStyles[i].minTrackBreadth();
318         if (computeUsedBreadthOfMinLength(direction, minTrackBreadth) > tracks[i].m_usedBreadth)
319             return false;
320     }
321     return true;
322 }
323 #endif
324
325 void RenderGrid::layoutGridItems()
326 {
327     Vector<GridTrack> columnTracks, rowTracks;
328     computedUsedBreadthOfGridTracks(ForColumns, columnTracks, rowTracks);
329     ASSERT(tracksAreWiderThanMinTrackBreadth(ForColumns, columnTracks));
330     computedUsedBreadthOfGridTracks(ForRows, columnTracks, rowTracks);
331     ASSERT(tracksAreWiderThanMinTrackBreadth(ForRows, rowTracks));
332
333     for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
334         LayoutPoint childPosition = findChildLogicalPosition(child, columnTracks, rowTracks);
335
336         size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
337         size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
338
339         // FIXME: Properly support implicit rows and columns (bug 103573).
340         if (columnTrack < columnTracks.size() && rowTrack < rowTracks.size()) {
341             // Because the grid area cannot be styled, we don't need to adjust
342             // the grid breadth to account for 'box-sizing'.
343             LayoutUnit oldOverrideContainingBlockContentLogicalWidth = child->hasOverrideContainingBlockLogicalWidth() ? child->overrideContainingBlockContentLogicalWidth() : LayoutUnit();
344             LayoutUnit oldOverrideContainingBlockContentLogicalHeight = child->hasOverrideContainingBlockLogicalHeight() ? child->overrideContainingBlockContentLogicalHeight() : LayoutUnit();
345
346             if (oldOverrideContainingBlockContentLogicalWidth != columnTracks[columnTrack].m_usedBreadth || oldOverrideContainingBlockContentLogicalHeight != rowTracks[rowTrack].m_usedBreadth)
347                 child->setNeedsLayout(true, MarkOnlyThis);
348
349             child->setOverrideContainingBlockContentLogicalWidth(columnTracks[columnTrack].m_usedBreadth);
350             child->setOverrideContainingBlockContentLogicalHeight(rowTracks[rowTrack].m_usedBreadth);
351
352         }
353
354         // FIXME: Grid items should stretch to fill their cells. Once we
355         // implement grid-{column,row}-align, we can also shrink to fit. For
356         // now, just size as if we were a regular child.
357         child->layoutIfNeeded();
358
359         // FIXME: Handle border & padding on the grid element.
360         child->setLogicalLocation(childPosition);
361     }
362
363     for (size_t i = 0; i < rowTracks.size(); ++i)
364         setLogicalHeight(logicalHeight() + rowTracks[i].m_usedBreadth);
365
366     // FIXME: We should handle min / max logical height.
367
368     setLogicalHeight(logicalHeight() + borderAndPaddingLogicalHeight());
369 }
370
371 size_t RenderGrid::resolveGridPosition(TrackSizingDirection direction, const RenderObject* gridItem) const
372 {
373     const GridPosition& position = (direction == ForColumns) ? gridItem->style()->gridItemColumn() : gridItem->style()->gridItemRow();
374     return resolveGridPosition(position);
375 }
376
377 size_t RenderGrid::resolveGridPosition(const GridPosition& position) const
378 {
379     // FIXME: Handle other values for grid-{row,column} like ranges or line names.
380     switch (position.type()) {
381     case IntegerPosition:
382         // FIXME: What does a non-positive integer mean for a column/row?
383         if (!position.isPositive())
384             return 0;
385
386         return position.integerPosition() - 1;
387     case AutoPosition:
388         // FIXME: We should follow 'grid-auto-flow' for resolution.
389         // Until then, we use the 'grid-auto-flow: none' behavior (which is the default)
390         // and resolve 'auto' as the first row / column.
391         return 0;
392     }
393     ASSERT_NOT_REACHED();
394     return 0;
395 }
396
397 LayoutPoint RenderGrid::findChildLogicalPosition(RenderBox* child, const Vector<GridTrack>& columnTracks, const Vector<GridTrack>& rowTracks)
398 {
399     size_t columnTrack = resolveGridPosition(child->style()->gridItemColumn());
400     size_t rowTrack = resolveGridPosition(child->style()->gridItemRow());
401
402     LayoutPoint offset;
403     // FIXME: |columnTrack| and |rowTrack| should be smaller than our column / row count.
404     for (size_t i = 0; i < columnTrack && i < columnTracks.size(); ++i)
405         offset.setX(offset.x() + columnTracks[i].m_usedBreadth);
406     for (size_t i = 0; i < rowTrack && i < rowTracks.size(); ++i)
407         offset.setY(offset.y() + rowTracks[i].m_usedBreadth);
408
409     // FIXME: Handle margins on the grid item.
410     return offset;
411 }
412
413 const char* RenderGrid::renderName() const
414 {
415     if (isFloating())
416         return "RenderGrid (floating)";
417     if (isOutOfFlowPositioned())
418         return "RenderGrid (positioned)";
419     if (isAnonymous())
420         return "RenderGrid (generated)";
421     if (isRelPositioned())
422         return "RenderGrid (relative positioned)";
423     return "RenderGrid";
424 }
425
426 } // namespace WebCore