1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/rendering/style/GridResolvedPosition.h"
7
8 #include "core/rendering/RenderBox.h"
9 #include "core/rendering/style/GridCoordinate.h"
10
11 namespace blink {
12
gridLinesForSide(const RenderStyle & style,GridPositionSide side)13 static const NamedGridLinesMap& gridLinesForSide(const RenderStyle& style, GridPositionSide side)
14 {
15 return (side == ColumnStartSide || side == ColumnEndSide) ? style.namedGridColumnLines() : style.namedGridRowLines();
16 }
17
implicitNamedGridLineForSide(const String & lineName,GridPositionSide side)18 static inline String implicitNamedGridLineForSide(const String& lineName, GridPositionSide side)
19 {
20 return lineName + ((side == ColumnStartSide || side == RowStartSide) ? "-start" : "-end");
21 }
22
isValidNamedLineOrArea(const String & lineName,const RenderStyle & style,GridPositionSide side)23 static bool isValidNamedLineOrArea(const String& lineName, const RenderStyle& style, GridPositionSide side)
24 {
25 const NamedGridLinesMap& gridLineNames = gridLinesForSide(style, side);
26
27 return gridLineNames.contains(implicitNamedGridLineForSide(lineName, side)) || gridLineNames.contains(lineName);
28 }
29
calculateInitialPositionSide(GridTrackSizingDirection direction)30 static GridPositionSide calculateInitialPositionSide(GridTrackSizingDirection direction)
31 {
32 return (direction == ForColumns) ? ColumnStartSide : RowStartSide;
33 }
34
calculateFinalPositionSide(GridTrackSizingDirection direction)35 static GridPositionSide calculateFinalPositionSide(GridTrackSizingDirection direction)
36 {
37 return (direction == ForColumns) ? ColumnEndSide : RowEndSide;
38 }
39
initialAndFinalPositionsFromStyle(const RenderStyle & gridContainerStyle,const RenderBox & gridItem,GridTrackSizingDirection direction,GridPosition & initialPosition,GridPosition & finalPosition)40 void GridResolvedPosition::initialAndFinalPositionsFromStyle(const RenderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirection direction, GridPosition& initialPosition, GridPosition& finalPosition)
41 {
42 initialPosition = (direction == ForColumns) ? gridItem.style()->gridColumnStart() : gridItem.style()->gridRowStart();
43 finalPosition = (direction == ForColumns) ? gridItem.style()->gridColumnEnd() : gridItem.style()->gridRowEnd();
44 GridPositionSide initialPositionSide = calculateInitialPositionSide(direction);
45 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction);
46
47 // We must handle the placement error handling code here instead of in the StyleAdjuster because we don't want to
48 // overwrite the specified values.
49 if (initialPosition.isSpan() && finalPosition.isSpan())
50 finalPosition.setAutoPosition();
51
52 // Try to early detect the case of non existing named grid lines. This way we could assume later that
53 // GridResolvedPosition::resolveGrisPositionFromStyle() always return a valid resolved position.
54 if (initialPosition.isNamedGridArea() && !isValidNamedLineOrArea(initialPosition.namedGridLine(), gridContainerStyle, initialPositionSide))
55 initialPosition.setAutoPosition();
56
57 if (finalPosition.isNamedGridArea() && !isValidNamedLineOrArea(finalPosition.namedGridLine(), gridContainerStyle, finalPositionSide))
58 finalPosition.setAutoPosition();
59
60 // If the grid item has an automatic position and a grid span for a named line in a given dimension, instead treat the grid span as one.
61 if (initialPosition.isAuto() && finalPosition.isSpan() && !finalPosition.namedGridLine().isNull())
62 finalPosition.setSpanPosition(1, String());
63 if (finalPosition.isAuto() && initialPosition.isSpan() && !initialPosition.namedGridLine().isNull())
64 initialPosition.setSpanPosition(1, String());
65 }
66
resolveGridPositionsFromAutoPlacementPosition(const RenderStyle & gridContainerStyle,const RenderBox & gridItem,GridTrackSizingDirection direction,const GridResolvedPosition & resolvedInitialPosition)67 GridSpan GridResolvedPosition::resolveGridPositionsFromAutoPlacementPosition(const RenderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirection direction, const GridResolvedPosition& resolvedInitialPosition)
68 {
69 GridPosition initialPosition, finalPosition;
70 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, initialPosition, finalPosition);
71
72 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction);
73
74 // This method will only be used when both positions need to be resolved against the opposite one.
75 ASSERT(initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPosition.shouldBeResolvedAgainstOppositePosition());
76
77 GridResolvedPosition resolvedFinalPosition = resolvedInitialPosition;
78
79 if (initialPosition.isSpan()) {
80 resolvedFinalPosition = resolveGridPositionAgainstOppositePosition(gridContainerStyle, resolvedInitialPosition, initialPosition, finalPositionSide)->resolvedFinalPosition;
81 } else if (finalPosition.isSpan()) {
82 resolvedFinalPosition = resolveGridPositionAgainstOppositePosition(gridContainerStyle, resolvedInitialPosition, finalPosition, finalPositionSide)->resolvedFinalPosition;
83 }
84
85 return GridSpan(resolvedInitialPosition, resolvedFinalPosition);
86 }
87
resolveGridPositionsFromStyle(const RenderStyle & gridContainerStyle,const RenderBox & gridItem,GridTrackSizingDirection direction)88 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionsFromStyle(const RenderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirection direction)
89 {
90 GridPosition initialPosition, finalPosition;
91 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, initialPosition, finalPosition);
92
93 GridPositionSide initialPositionSide = calculateInitialPositionSide(direction);
94 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction);
95
96 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPosition.shouldBeResolvedAgainstOppositePosition()) {
97 // FIXME: Implement properly "stack" value in auto-placement algorithm.
98 if (gridContainerStyle.isGridAutoFlowAlgorithmStack())
99 return adoptPtr(new GridSpan(0, 0));
100
101 // We can't get our grid positions without running the auto placement algorithm.
102 return nullptr;
103 }
104
105 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) {
106 // Infer the position from the final position ('auto / 1' or 'span 2 / 3' case).
107 GridResolvedPosition finalResolvedPosition = resolveGridPositionFromStyle(gridContainerStyle, finalPosition, finalPositionSide);
108 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, finalResolvedPosition, initialPosition, initialPositionSide);
109 }
110
111 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) {
112 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case).
113 GridResolvedPosition initialResolvedPosition = resolveGridPositionFromStyle(gridContainerStyle, initialPosition, initialPositionSide);
114 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, initialResolvedPosition, finalPosition, finalPositionSide);
115 }
116
117 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle(gridContainerStyle, initialPosition, initialPositionSide);
118 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(gridContainerStyle, finalPosition, finalPositionSide);
119
120 // If 'grid-after' specifies a line at or before that specified by 'grid-before', it computes to 'span 1'.
121 if (resolvedFinalPosition < resolvedInitialPosition)
122 resolvedFinalPosition = resolvedInitialPosition;
123
124 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition));
125 }
126
explicitGridColumnCount(const RenderStyle & gridContainerStyle)127 size_t GridResolvedPosition::explicitGridColumnCount(const RenderStyle& gridContainerStyle)
128 {
129 return gridContainerStyle.gridTemplateColumns().size();
130 }
131
explicitGridRowCount(const RenderStyle & gridContainerStyle)132 size_t GridResolvedPosition::explicitGridRowCount(const RenderStyle& gridContainerStyle)
133 {
134 return gridContainerStyle.gridTemplateRows().size();
135 }
136
explicitGridSizeForSide(const RenderStyle & gridContainerStyle,GridPositionSide side)137 size_t GridResolvedPosition::explicitGridSizeForSide(const RenderStyle& gridContainerStyle, GridPositionSide side)
138 {
139 return (side == ColumnStartSide || side == ColumnEndSide) ? explicitGridColumnCount(gridContainerStyle) : explicitGridRowCount(gridContainerStyle);
140 }
141
resolveNamedGridLinePositionFromStyle(const RenderStyle & gridContainerStyle,const GridPosition & position,GridPositionSide side)142 GridResolvedPosition GridResolvedPosition::resolveNamedGridLinePositionFromStyle(const RenderStyle& gridContainerStyle, const GridPosition& position, GridPositionSide side)
143 {
144 ASSERT(!position.namedGridLine().isNull());
145
146 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyle, side);
147 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGridLine());
148 if (it == gridLinesNames.end()) {
149 if (position.isPositive())
150 return GridResolvedPosition(0);
151 const size_t lastLine = explicitGridSizeForSide(gridContainerStyle, side);
152 return adjustGridPositionForSide(lastLine, side);
153 }
154
155 size_t namedGridLineIndex;
156 if (position.isPositive())
157 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->value.size()) - 1;
158 else
159 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integerPosition()), 0);
160 return adjustGridPositionForSide(it->value[namedGridLineIndex], side);
161 }
162
resolveGridPositionFromStyle(const RenderStyle & gridContainerStyle,const GridPosition & position,GridPositionSide side)163 GridResolvedPosition GridResolvedPosition::resolveGridPositionFromStyle(const RenderStyle& gridContainerStyle, const GridPosition& position, GridPositionSide side)
164 {
165 switch (position.type()) {
166 case ExplicitPosition: {
167 ASSERT(position.integerPosition());
168
169 if (!position.namedGridLine().isNull())
170 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, position, side);
171
172 // Handle <integer> explicit position.
173 if (position.isPositive())
174 return adjustGridPositionForSide(position.integerPosition() - 1, side);
175
176 size_t resolvedPosition = abs(position.integerPosition()) - 1;
177 const size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, side);
178
179 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line.
180 if (endOfTrack < resolvedPosition)
181 return GridResolvedPosition(0);
182
183 return adjustGridPositionForSide(endOfTrack - resolvedPosition, side);
184 }
185 case NamedGridAreaPosition:
186 {
187 // First attempt to match the grid area’s edge to a named grid area: if there is a named line with the name
188 // ''<custom-ident>-start (for grid-*-start) / <custom-ident>-end'' (for grid-*-end), contributes the first such
189 // line to the grid item’s placement.
190 String namedGridLine = position.namedGridLine();
191 ASSERT(isValidNamedLineOrArea(namedGridLine, gridContainerStyle, side));
192
193 const NamedGridLinesMap& gridLineNames = gridLinesForSide(gridContainerStyle, side);
194 NamedGridLinesMap::const_iterator implicitLineIter = gridLineNames.find(implicitNamedGridLineForSide(namedGridLine, side));
195 if (implicitLineIter != gridLineNames.end())
196 return adjustGridPositionForSide(implicitLineIter->value[0], side);
197
198 // Otherwise, if there is a named line with the specified name, contributes the first such line to the grid
199 // item’s placement.
200 NamedGridLinesMap::const_iterator explicitLineIter = gridLineNames.find(namedGridLine);
201 if (explicitLineIter != gridLineNames.end())
202 return adjustGridPositionForSide(explicitLineIter->value[0], side);
203
204 // If none of the above works specs mandate us to treat it as auto BUT we should have detected it before calling
205 // this function in GridResolvedPosition::resolveGridPositionsFromStyle(). We should be also covered by the
206 // ASSERT at the beginning of this block.
207 ASSERT_NOT_REACHED();
208 return GridResolvedPosition(0);
209 }
210 case AutoPosition:
211 case SpanPosition:
212 // 'auto' and span depend on the opposite position for resolution (e.g. grid-row: auto / 1 or grid-column: span 3 / "myHeader").
213 ASSERT_NOT_REACHED();
214 return GridResolvedPosition(0);
215 }
216 ASSERT_NOT_REACHED();
217 return GridResolvedPosition(0);
218 }
219
resolveGridPositionAgainstOppositePosition(const RenderStyle & gridContainerStyle,const GridResolvedPosition & resolvedOppositePosition,const GridPosition & position,GridPositionSide side)220 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionAgainstOppositePosition(const RenderStyle& gridContainerStyle, const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
221 {
222 if (position.isAuto())
223 return GridSpan::create(resolvedOppositePosition, resolvedOppositePosition);
224
225 ASSERT(position.isSpan());
226 ASSERT(position.spanPosition() > 0);
227
228 if (!position.namedGridLine().isNull()) {
229 // span 2 'c' -> we need to find the appropriate grid line before / after our opposite position.
230 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainerStyle, resolvedOppositePosition, position, side);
231 }
232
233 return GridSpan::createWithSpanAgainstOpposite(resolvedOppositePosition, position, side);
234 }
235
resolveNamedGridLinePositionAgainstOppositePosition(const RenderStyle & gridContainerStyle,const GridResolvedPosition & resolvedOppositePosition,const GridPosition & position,GridPositionSide side)236 PassOwnPtr<GridSpan> GridResolvedPosition::resolveNamedGridLinePositionAgainstOppositePosition(const RenderStyle& gridContainerStyle, const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
237 {
238 ASSERT(position.isSpan());
239 ASSERT(!position.namedGridLine().isNull());
240 // Negative positions are not allowed per the specification and should have been handled during parsing.
241 ASSERT(position.spanPosition() > 0);
242
243 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyle, side);
244 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGridLine());
245
246 // If there is no named grid line of that name, we resolve the position to 'auto' (which is equivalent to 'span 1' in this case).
247 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html.
248 if (it == gridLinesNames.end())
249 return GridSpan::create(resolvedOppositePosition, resolvedOppositePosition);
250
251 return GridSpan::createWithNamedSpanAgainstOpposite(resolvedOppositePosition, position, side, it->value);
252 }
253
254 } // namespace blink
255