[CSS Grid Layout] Tracks growing beyond limits when they should not
[WebKit-https.git] / Source / WebCore / rendering / RenderGrid.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2013, 2014 Igalia S.L.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef RenderGrid_h
28 #define RenderGrid_h
29
30 #if ENABLE(CSS_GRID_LAYOUT)
31
32 #include "GridResolvedPosition.h"
33 #include "OrderIterator.h"
34 #include "RenderBlock.h"
35
36 namespace WebCore {
37
38 class GridCoordinate;
39 class GridSpan;
40 class GridTrack;
41 class GridItemWithSpan;
42
43 class RenderGrid final : public RenderBlock {
44 public:
45     RenderGrid(Element&, Ref<RenderStyle>&&);
46     virtual ~RenderGrid();
47
48     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
49
50     virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
51
52     virtual bool avoidsFloats() const override { return true; }
53     virtual bool canCollapseAnonymousBlockChild() const override { return false; }
54
55     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
56     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
57
58 private:
59     virtual const char* renderName() const override;
60     virtual bool isRenderGrid() const override { return true; }
61     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
62     virtual void computePreferredLogicalWidths() override;
63
64     class GridIterator;
65     class GridSizingData;
66     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&);
67     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& availableLogicalSpace);
68     bool gridElementIsShrinkToFit();
69     LayoutUnit computeUsedBreadthOfMinLength(GridTrackSizingDirection, const GridLength&) const;
70     LayoutUnit computeUsedBreadthOfMaxLength(GridTrackSizingDirection, const GridLength&, LayoutUnit usedBreadth) const;
71     LayoutUnit computeUsedBreadthOfSpecifiedLength(GridTrackSizingDirection, const Length&) const;
72     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&);
73
74     void ensureGridSize(unsigned maximumRowIndex, unsigned maximumColumnIndex);
75     void insertItemIntoGrid(RenderBox&, const GridCoordinate&);
76     void placeItemsOnGrid();
77     void populateExplicitGridAndOrderIterator();
78     std::unique_ptr<GridCoordinate> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
79     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
80     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
81     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
82     void placeAutoMajorAxisItemOnGrid(RenderBox&, AutoPlacementCursor&);
83     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
84     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
85
86     void layoutGridItems();
87     void populateGridPositions(const GridSizingData&);
88     void clearGrid();
89
90     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>&);
91     typedef const LayoutUnit& (GridTrack::* AccumulatorGetter)() const;
92     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
93     typedef bool (GridTrackSize::* FilterFunction)() const;
94     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridCoordinate&, RenderBox& gridItem, GridTrack&, Vector<GridTrack>& columnTracks);
95     void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, GridItemWithSpan&, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction, FilterFunction growAboveMaxBreadthFilterFunction = nullptr);
96     void distributeSpaceToTracks(Vector<GridTrack*>&, const Vector<GridTrack*>* growBeyondGrowthLimitsTracks, AccumulatorGetter, AccumulatorGrowFunction, LayoutUnit& availableLogicalSpace);
97
98     double computeNormalizedFractionBreadth(Vector<GridTrack>&, const GridSpan& tracksSpan, GridTrackSizingDirection, LayoutUnit availableLogicalSpace) const;
99
100     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned) const;
101
102     LayoutUnit logicalContentHeightForChild(RenderBox&, Vector<GridTrack>&);
103     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
104     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
105     LayoutPoint findChildLogicalPosition(RenderBox&, const GridSizingData&);
106     GridCoordinate cachedGridCoordinate(const RenderBox&) const;
107
108     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const Vector<GridTrack>&) const;
109
110     virtual void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
111
112 #ifndef NDEBUG
113     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, const Vector<GridTrack>&);
114 #endif
115
116     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
117
118     bool spanningItemCrossesFlexibleSizedTracks(const GridCoordinate&, GridTrackSizingDirection) const;
119
120     unsigned gridColumnCount() const
121     {
122         ASSERT(gridWasPopulated());
123         return m_grid[0].size();
124     }
125     unsigned gridRowCount() const
126     {
127         ASSERT(gridWasPopulated());
128         return m_grid.size();
129     }
130
131     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
132     Vector<LayoutUnit> m_columnPositions;
133     Vector<LayoutUnit> m_rowPositions;
134     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
135     OrderIterator m_orderIterator;
136 };
137
138 } // namespace WebCore
139
140 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
141
142 #endif /* ENABLE(CSS_GRID_LAYOUT) */
143
144 #endif // RenderGrid_h