Floating box is misplaced after content change.
[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 struct ContentAlignmentData;
44
45 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
46
47 class RenderGrid final : public RenderBlock {
48 public:
49     RenderGrid(Element&, Ref<RenderStyle>&&);
50     virtual ~RenderGrid();
51
52     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
53
54     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
55     virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
56
57     virtual bool avoidsFloats() const override { return true; }
58     virtual bool canDropAnonymousBlockChild() const override { return false; }
59
60     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
61     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
62
63     LayoutUnit guttersSize(GridTrackSizingDirection, size_t span) const;
64
65 private:
66     virtual const char* renderName() const override;
67     virtual bool isRenderGrid() const override { return true; }
68     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
69
70     class GridIterator;
71     class GridSizingData;
72     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&);
73     bool gridElementIsShrinkToFit();
74     LayoutUnit computeUsedBreadthOfMinLength(const GridLength&, LayoutUnit maxSize) const;
75     LayoutUnit computeUsedBreadthOfMaxLength(const GridLength&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
76     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&);
77
78     void ensureGridSize(unsigned maximumRowIndex, unsigned maximumColumnIndex);
79     void insertItemIntoGrid(RenderBox&, const GridCoordinate&);
80     void placeItemsOnGrid();
81     void populateExplicitGridAndOrderIterator();
82     std::unique_ptr<GridCoordinate> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
83     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
84     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
85     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
86     void placeAutoMajorAxisItemOnGrid(RenderBox&, AutoPlacementCursor&);
87     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
88     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
89
90     void layoutGridItems();
91     void populateGridPositions(GridSizingData&);
92     void clearGrid();
93
94     enum TrackSizeRestriction {
95         AllowInfinity,
96         ForbidInfinity,
97     };
98     enum TrackSizeComputationPhase {
99         ResolveIntrinsicMinimums,
100         ResolveContentBasedMinimums,
101         ResolveMaxContentMinimums,
102         ResolveIntrinsicMaximums,
103         ResolveMaxContentMaximums,
104         MaximizeTracks,
105     };
106     static const LayoutUnit& trackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&, TrackSizeRestriction);
107     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
108     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
109     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
110     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
111     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
112
113     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
114     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridCoordinate&, RenderBox& gridItem, GridTrack&, Vector<GridTrack>& columnTracks);
115     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&);
116     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, const Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace);
117
118     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
119     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
120     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, LayoutUnit spaceToFill) const;
121
122     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned) const;
123
124     LayoutUnit logicalContentHeightForChild(RenderBox&, Vector<GridTrack>&);
125     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
126     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
127     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
128     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
129     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
130     LayoutUnit columnAxisOffsetForChild(const RenderBox&) const;
131     LayoutUnit rowAxisOffsetForChild(const RenderBox&) const;
132     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
133     LayoutPoint findChildLogicalPosition(const RenderBox&) const;
134     GridCoordinate cachedGridCoordinate(const RenderBox&) const;
135
136
137     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const Vector<GridTrack>&) const;
138     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
139
140     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
141
142     virtual void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
143     bool needToStretchChildLogicalHeight(const RenderBox&) const;
144     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
145     LayoutUnit computeMarginLogicalHeightForChild(const RenderBox&) const;
146     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
147     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
148     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
149     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
150     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
151     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
152     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
153
154 #ifndef NDEBUG
155     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, const Vector<GridTrack>&);
156 #endif
157
158     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
159
160     bool spanningItemCrossesFlexibleSizedTracks(const GridCoordinate&, GridTrackSizingDirection) const;
161
162     unsigned gridColumnCount() const
163     {
164         ASSERT(gridWasPopulated());
165         return m_grid[0].size();
166     }
167     unsigned gridRowCount() const
168     {
169         ASSERT(gridWasPopulated());
170         return m_grid.size();
171     }
172
173     bool hasDefiniteLogicalSize(GridTrackSizingDirection) const;
174
175     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
176     Vector<LayoutUnit> m_columnPositions;
177     Vector<LayoutUnit> m_rowPositions;
178     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
179     OrderIterator m_orderIterator;
180 };
181
182 } // namespace WebCore
183
184 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
185
186 #endif /* ENABLE(CSS_GRID_LAYOUT) */
187
188 #endif // RenderGrid_h