Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: third_party/WebKit/Source/core/style/GridResolvedPosition.cpp

Issue 1477203002: [css-grid] Simplify GridResolvedPosition interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/style/GridResolvedPosition.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/style/GridResolvedPosition.h" 6 #include "core/style/GridResolvedPosition.h"
7 7
8 #include "core/layout/LayoutBox.h" 8 #include "core/layout/LayoutBox.h"
9 #include "core/style/GridCoordinate.h" 9 #include "core/style/GridCoordinate.h"
10 10
(...skipping 19 matching lines...) Expand all
30 GridPositionSide GridResolvedPosition::initialPositionSide(GridTrackSizingDirect ion direction) 30 GridPositionSide GridResolvedPosition::initialPositionSide(GridTrackSizingDirect ion direction)
31 { 31 {
32 return (direction == ForColumns) ? ColumnStartSide : RowStartSide; 32 return (direction == ForColumns) ? ColumnStartSide : RowStartSide;
33 } 33 }
34 34
35 GridPositionSide GridResolvedPosition::finalPositionSide(GridTrackSizingDirectio n direction) 35 GridPositionSide GridResolvedPosition::finalPositionSide(GridTrackSizingDirectio n direction)
36 { 36 {
37 return (direction == ForColumns) ? ColumnEndSide : RowEndSide; 37 return (direction == ForColumns) ? ColumnEndSide : RowEndSide;
38 } 38 }
39 39
40 void GridResolvedPosition::initialAndFinalPositionsFromStyle(const ComputedStyle & gridContainerStyle, const LayoutBox& gridItem, GridTrackSizingDirection direct ion, GridPosition& initialPosition, GridPosition& finalPosition) 40 static void initialAndFinalPositionsFromStyle(const ComputedStyle& gridContainer Style, const LayoutBox& gridItem, GridTrackSizingDirection direction, GridPositi on& initialPosition, GridPosition& finalPosition)
41 { 41 {
42 initialPosition = (direction == ForColumns) ? gridItem.style()->gridColumnSt art() : gridItem.style()->gridRowStart(); 42 initialPosition = (direction == ForColumns) ? gridItem.style()->gridColumnSt art() : gridItem.style()->gridRowStart();
43 finalPosition = (direction == ForColumns) ? gridItem.style()->gridColumnEnd( ) : gridItem.style()->gridRowEnd(); 43 finalPosition = (direction == ForColumns) ? gridItem.style()->gridColumnEnd( ) : gridItem.style()->gridRowEnd();
44 44
45 // We must handle the placement error handling code here instead of in the S tyleAdjuster because we don't want to 45 // We must handle the placement error handling code here instead of in the S tyleAdjuster because we don't want to
46 // overwrite the specified values. 46 // overwrite the specified values.
47 if (initialPosition.isSpan() && finalPosition.isSpan()) 47 if (initialPosition.isSpan() && finalPosition.isSpan())
48 finalPosition.setAutoPosition(); 48 finalPosition.setAutoPosition();
49 49
50 // Try to early detect the case of non existing named grid lines. This way w e could assume later that 50 // Try to early detect the case of non existing named grid lines. This way w e could assume later that
51 // GridResolvedPosition::resolveGrisPositionFromStyle() always return a vali d resolved position. 51 // GridResolvedPosition::resolveGrisPositionFromStyle() always return a vali d resolved position.
52 if (initialPosition.isNamedGridArea() && !isValidNamedLineOrArea(initialPosi tion.namedGridLine(), gridContainerStyle, initialPositionSide(direction))) 52 if (initialPosition.isNamedGridArea() && !GridResolvedPosition::isValidNamed LineOrArea(initialPosition.namedGridLine(), gridContainerStyle, GridResolvedPosi tion::initialPositionSide(direction)))
53 initialPosition.setAutoPosition(); 53 initialPosition.setAutoPosition();
54 54
55 if (finalPosition.isNamedGridArea() && !isValidNamedLineOrArea(finalPosition .namedGridLine(), gridContainerStyle, finalPositionSide(direction))) 55 if (finalPosition.isNamedGridArea() && !GridResolvedPosition::isValidNamedLi neOrArea(finalPosition.namedGridLine(), gridContainerStyle, GridResolvedPosition ::finalPositionSide(direction)))
56 finalPosition.setAutoPosition(); 56 finalPosition.setAutoPosition();
57 57
58 // If the grid item has an automatic position and a grid span for a named li ne in a given dimension, instead treat the grid span as one. 58 // If the grid item has an automatic position and a grid span for a named li ne in a given dimension, instead treat the grid span as one.
59 if (initialPosition.isAuto() && finalPosition.isSpan() && !finalPosition.nam edGridLine().isNull()) 59 if (initialPosition.isAuto() && finalPosition.isSpan() && !finalPosition.nam edGridLine().isNull())
60 finalPosition.setSpanPosition(1, String()); 60 finalPosition.setSpanPosition(1, String());
61 if (finalPosition.isAuto() && initialPosition.isSpan() && !initialPosition.n amedGridLine().isNull()) 61 if (finalPosition.isAuto() && initialPosition.isSpan() && !initialPosition.n amedGridLine().isNull())
62 initialPosition.setSpanPosition(1, String()); 62 initialPosition.setSpanPosition(1, String());
63 } 63 }
64 64
65 static GridSpan definiteGridSpanWithInitialNamedSpanAgainstOpposite(const GridRe solvedPosition& resolvedOppositePosition, const GridPosition& position, const Ve ctor<size_t>& gridLines)
66 {
67 if (resolvedOppositePosition == 0)
68 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition.next());
69
70 size_t firstLineBeforeOppositePositionIndex = 0;
71 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLines.b egin(), gridLines.end(), resolvedOppositePosition.toInt());
72 if (firstLineBeforeOppositePosition != gridLines.end())
73 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePosition - gridLines.begin();
74 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionIndex - position.spanPosition());
75 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gridLin es[gridLineIndex]);
76 if (resolvedGridLinePosition >= resolvedOppositePosition)
77 resolvedGridLinePosition = resolvedOppositePosition.prev();
78 return GridSpan::definiteGridSpan(resolvedGridLinePosition, resolvedOpposite Position);
79 }
80
81 static GridSpan definiteGridSpanWithFinalNamedSpanAgainstOpposite(const GridReso lvedPosition& resolvedOppositePosition, const GridPosition& position, const Vect or<size_t>& gridLines)
82 {
83 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1;
84 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLines.be gin(), gridLines.end(), resolvedOppositePosition.toInt());
85 if (firstLineAfterOppositePosition != gridLines.end())
86 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - g ridLines.begin();
87 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOpposite PositionIndex + position.spanPosition() - 1);
88 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex];
89 if (resolvedGridLinePosition <= resolvedOppositePosition)
90 resolvedGridLinePosition = resolvedOppositePosition.next();
91
92 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedGridLine Position);
93 }
94
95 static GridSpan definiteGridSpanWithNamedSpanAgainstOpposite(const GridResolvedP osition& resolvedOppositePosition, const GridPosition& position, GridPositionSid e side, const Vector<size_t>& gridLines)
96 {
97 if (side == RowStartSide || side == ColumnStartSide)
98 return definiteGridSpanWithInitialNamedSpanAgainstOpposite(resolvedOppos itePosition, position, gridLines);
99
100 return definiteGridSpanWithFinalNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines);
101 }
102
103 static GridSpan resolveNamedGridLinePositionAgainstOppositePosition(const Comput edStyle& gridContainerStyle, const GridResolvedPosition& resolvedOppositePositio n, const GridPosition& position, GridPositionSide side)
104 {
105 ASSERT(position.isSpan());
106 ASSERT(!position.namedGridLine().isNull());
107 // Negative positions are not allowed per the specification and should have been handled during parsing.
108 ASSERT(position.spanPosition() > 0);
109
110 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side);
111 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
112
113 // 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).
114 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html.
115 if (it == gridLinesNames.end()) {
116 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition.toInt())
117 return GridSpan::definiteGridSpan(resolvedOppositePosition.prev(), r esolvedOppositePosition);
118 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition.next());
119 }
120
121 return definiteGridSpanWithNamedSpanAgainstOpposite(resolvedOppositePosition , position, side, it->value);
122 }
123
124 static GridSpan definiteGridSpanWithSpanAgainstOpposite(const GridResolvedPositi on& resolvedOppositePosition, const GridPosition& position, GridPositionSide sid e)
125 {
126 size_t positionOffset = position.spanPosition();
127 if (side == ColumnStartSide || side == RowStartSide) {
128 if (resolvedOppositePosition == 0)
129 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolved OppositePosition.next());
130
131 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(std: :max<int>(0, resolvedOppositePosition.toInt() - positionOffset));
132 return GridSpan::definiteGridSpan(initialResolvedPosition, resolvedOppos itePosition);
133 }
134
135 return GridSpan::definiteGridSpan(resolvedOppositePosition, GridResolvedPosi tion(resolvedOppositePosition.toInt() + positionOffset));
136 }
137
138 static GridSpan resolveGridPositionAgainstOppositePosition(const ComputedStyle& gridContainerStyle, const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
139 {
140 if (position.isAuto()) {
141 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition.toInt())
142 return GridSpan::definiteGridSpan(resolvedOppositePosition.prev(), r esolvedOppositePosition);
143 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition.next());
144 }
145
146 ASSERT(position.isSpan());
147 ASSERT(position.spanPosition() > 0);
148
149 if (!position.namedGridLine().isNull()) {
150 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position.
151 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer Style, resolvedOppositePosition, position, side);
152 }
153
154 return definiteGridSpanWithSpanAgainstOpposite(resolvedOppositePosition, pos ition, side);
155 }
156
65 GridSpan GridResolvedPosition::resolveGridPositionsFromAutoPlacementPosition(con st ComputedStyle& gridContainerStyle, const LayoutBox& gridItem, GridTrackSizing Direction direction, const GridResolvedPosition& resolvedInitialPosition) 157 GridSpan GridResolvedPosition::resolveGridPositionsFromAutoPlacementPosition(con st ComputedStyle& gridContainerStyle, const LayoutBox& gridItem, GridTrackSizing Direction direction, const GridResolvedPosition& resolvedInitialPosition)
66 { 158 {
67 GridPosition initialPosition, finalPosition; 159 GridPosition initialPosition, finalPosition;
68 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i nitialPosition, finalPosition); 160 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i nitialPosition, finalPosition);
69 161
70 GridPositionSide finalSide = finalPositionSide(direction); 162 GridPositionSide finalSide = finalPositionSide(direction);
71 163
72 // This method will only be used when both positions need to be resolved aga inst the opposite one. 164 // This method will only be used when both positions need to be resolved aga inst the opposite one.
73 ASSERT(initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPos ition.shouldBeResolvedAgainstOppositePosition()); 165 ASSERT(initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPos ition.shouldBeResolvedAgainstOppositePosition());
74 166
75 GridResolvedPosition resolvedFinalPosition = resolvedInitialPosition.next(); 167 GridResolvedPosition resolvedFinalPosition = resolvedInitialPosition.next();
76 168
77 if (initialPosition.isSpan()) 169 if (initialPosition.isSpan())
78 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, re solvedInitialPosition, initialPosition, finalSide); 170 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, re solvedInitialPosition, initialPosition, finalSide);
79 if (finalPosition.isSpan()) 171 if (finalPosition.isSpan())
80 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, re solvedInitialPosition, finalPosition, finalSide); 172 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, re solvedInitialPosition, finalPosition, finalSide);
81 173
82 return GridSpan::definiteGridSpan(resolvedInitialPosition, resolvedFinalPosi tion); 174 return GridSpan::definiteGridSpan(resolvedInitialPosition, resolvedFinalPosi tion);
83 } 175 }
84
85 GridSpan GridResolvedPosition::resolveGridPositionsFromStyle(const ComputedStyle & gridContainerStyle, const LayoutBox& gridItem, GridTrackSizingDirection direct ion)
86 {
87 GridPosition initialPosition, finalPosition;
88 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i nitialPosition, finalPosition);
89
90 GridPositionSide initialSide = initialPositionSide(direction);
91 GridPositionSide finalSide = finalPositionSide(direction);
92
93 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) {
94 // We can't get our grid positions without running the auto placement al gorithm.
95 return GridSpan::indefiniteGridSpan();
96 }
97
98 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) {
99 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case).
100 GridResolvedPosition finalResolvedPosition = resolveGridPositionFromStyl e(gridContainerStyle, finalPosition, finalSide);
101 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi nalResolvedPosition, initialPosition, initialSide);
102 }
103
104 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) {
105 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case).
106 GridResolvedPosition initialResolvedPosition = resolveGridPositionFromSt yle(gridContainerStyle, initialPosition, initialSide);
107 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in itialResolvedPosition, finalPosition, finalSide);
108 }
109
110 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle( gridContainerStyle, initialPosition, initialSide);
111 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(gr idContainerStyle, finalPosition, finalSide);
112
113 if (resolvedFinalPosition < resolvedInitialPosition)
114 std::swap(resolvedFinalPosition, resolvedInitialPosition);
115 else if (resolvedFinalPosition == resolvedInitialPosition)
116 resolvedFinalPosition = resolvedInitialPosition.next();
117
118 return GridSpan::definiteGridSpan(resolvedInitialPosition, resolvedFinalPosi tion);
119 }
120 176
121 size_t GridResolvedPosition::explicitGridColumnCount(const ComputedStyle& gridCo ntainerStyle) 177 size_t GridResolvedPosition::explicitGridColumnCount(const ComputedStyle& gridCo ntainerStyle)
122 { 178 {
123 return std::min(gridContainerStyle.gridTemplateColumns().size(), kGridMaxTra cks); 179 return std::min(gridContainerStyle.gridTemplateColumns().size(), kGridMaxTra cks);
124 } 180 }
125 181
126 size_t GridResolvedPosition::explicitGridRowCount(const ComputedStyle& gridConta inerStyle) 182 size_t GridResolvedPosition::explicitGridRowCount(const ComputedStyle& gridConta inerStyle)
127 { 183 {
128 return std::min(gridContainerStyle.gridTemplateRows().size(), kGridMaxTracks ); 184 return std::min(gridContainerStyle.gridTemplateRows().size(), kGridMaxTracks );
129 } 185 }
130 186
131 size_t GridResolvedPosition::explicitGridSizeForSide(const ComputedStyle& gridCo ntainerStyle, GridPositionSide side) 187 static size_t explicitGridSizeForSide(const ComputedStyle& gridContainerStyle, G ridPositionSide side)
132 { 188 {
133 return (side == ColumnStartSide || side == ColumnEndSide) ? explicitGridColu mnCount(gridContainerStyle) : explicitGridRowCount(gridContainerStyle); 189 return (side == ColumnStartSide || side == ColumnEndSide) ? GridResolvedPosi tion::explicitGridColumnCount(gridContainerStyle) : GridResolvedPosition::explic itGridRowCount(gridContainerStyle);
134 } 190 }
135 191
136 GridResolvedPosition GridResolvedPosition::resolveNamedGridLinePositionFromStyle (const ComputedStyle& gridContainerStyle, const GridPosition& position, GridPosi tionSide side) 192 static GridResolvedPosition resolveNamedGridLinePositionFromStyle(const Computed Style& gridContainerStyle, const GridPosition& position, GridPositionSide side)
137 { 193 {
138 ASSERT(!position.namedGridLine().isNull()); 194 ASSERT(!position.namedGridLine().isNull());
139 195
140 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side); 196 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side);
141 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine()); 197 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
142 if (it == gridLinesNames.end()) { 198 if (it == gridLinesNames.end()) {
143 if (position.isPositive()) 199 if (position.isPositive())
144 return GridResolvedPosition(0); 200 return GridResolvedPosition(0);
145 const size_t lastLine = explicitGridSizeForSide(gridContainerStyle, side ); 201 const size_t lastLine = explicitGridSizeForSide(gridContainerStyle, side );
146 return lastLine; 202 return lastLine;
147 } 203 }
148 204
149 size_t namedGridLineIndex; 205 size_t namedGridLineIndex;
150 if (position.isPositive()) 206 if (position.isPositive())
151 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1; 207 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1;
152 else 208 else
153 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0); 209 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0);
154 return it->value[namedGridLineIndex]; 210 return it->value[namedGridLineIndex];
155 } 211 }
156 212
157 GridResolvedPosition GridResolvedPosition::resolveGridPositionFromStyle(const Co mputedStyle& gridContainerStyle, const GridPosition& position, GridPositionSide side) 213 static GridResolvedPosition resolveGridPositionFromStyle(const ComputedStyle& gr idContainerStyle, const GridPosition& position, GridPositionSide side)
158 { 214 {
159 switch (position.type()) { 215 switch (position.type()) {
160 case ExplicitPosition: { 216 case ExplicitPosition: {
161 ASSERT(position.integerPosition()); 217 ASSERT(position.integerPosition());
162 218
163 if (!position.namedGridLine().isNull()) 219 if (!position.namedGridLine().isNull())
164 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos ition, side); 220 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos ition, side);
165 221
166 // Handle <integer> explicit position. 222 // Handle <integer> explicit position.
167 if (position.isPositive()) 223 if (position.isPositive())
168 return position.integerPosition() - 1; 224 return position.integerPosition() - 1;
169 225
170 size_t resolvedPosition = abs(position.integerPosition()) - 1; 226 size_t resolvedPosition = abs(position.integerPosition()) - 1;
171 const size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, si de); 227 const size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, si de);
172 228
173 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line. 229 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line.
174 if (endOfTrack < resolvedPosition) 230 if (endOfTrack < resolvedPosition)
175 return GridResolvedPosition(0); 231 return GridResolvedPosition(0);
176 232
177 return endOfTrack - resolvedPosition; 233 return endOfTrack - resolvedPosition;
178 } 234 }
179 case NamedGridAreaPosition: 235 case NamedGridAreaPosition:
180 { 236 {
181 // First attempt to match the grid area's edge to a named grid area: if there is a named line with the name 237 // First attempt to match the grid area's edge to a named grid area: if there is a named line with the name
182 // ''<custom-ident>-start (for grid-*-start) / <custom-ident>-end'' (for grid-*-end), contributes the first such 238 // ''<custom-ident>-start (for grid-*-start) / <custom-ident>-end'' (for grid-*-end), contributes the first such
183 // line to the grid item's placement. 239 // line to the grid item's placement.
184 String namedGridLine = position.namedGridLine(); 240 String namedGridLine = position.namedGridLine();
185 ASSERT(isValidNamedLineOrArea(namedGridLine, gridContainerStyle, side)); 241 ASSERT(GridResolvedPosition::isValidNamedLineOrArea(namedGridLine, gridC ontainerStyle, side));
186 242
187 const NamedGridLinesMap& gridLineNames = gridLinesForSide(gridContainerS tyle, side); 243 const NamedGridLinesMap& gridLineNames = gridLinesForSide(gridContainerS tyle, side);
188 NamedGridLinesMap::const_iterator implicitLineIter = gridLineNames.find( implicitNamedGridLineForSide(namedGridLine, side)); 244 NamedGridLinesMap::const_iterator implicitLineIter = gridLineNames.find( implicitNamedGridLineForSide(namedGridLine, side));
189 if (implicitLineIter != gridLineNames.end()) 245 if (implicitLineIter != gridLineNames.end())
190 return implicitLineIter->value[0]; 246 return implicitLineIter->value[0];
191 247
192 // Otherwise, if there is a named line with the specified name, contribu tes the first such line to the grid 248 // Otherwise, if there is a named line with the specified name, contribu tes the first such line to the grid
193 // item's placement. 249 // item's placement.
194 NamedGridLinesMap::const_iterator explicitLineIter = gridLineNames.find( namedGridLine); 250 NamedGridLinesMap::const_iterator explicitLineIter = gridLineNames.find( namedGridLine);
195 if (explicitLineIter != gridLineNames.end()) 251 if (explicitLineIter != gridLineNames.end())
196 return explicitLineIter->value[0]; 252 return explicitLineIter->value[0];
197 253
198 // If none of the above works specs mandate us to treat it as auto BUT w e should have detected it before calling 254 // If none of the above works specs mandate us to treat it as auto BUT w e should have detected it before calling
199 // this function in GridResolvedPosition::resolveGridPositionsFromStyle( ). We should be also covered by the 255 // this function in GridResolvedPosition::resolveGridPositionsFromStyle( ). We should be also covered by the
200 // ASSERT at the beginning of this block. 256 // ASSERT at the beginning of this block.
201 ASSERT_NOT_REACHED(); 257 ASSERT_NOT_REACHED();
202 return GridResolvedPosition(0); 258 return GridResolvedPosition(0);
203 } 259 }
204 case AutoPosition: 260 case AutoPosition:
205 case SpanPosition: 261 case SpanPosition:
206 // 'auto' and span depend on the opposite position for resolution (e.g. grid-row: auto / 1 or grid-column: span 3 / "myHeader"). 262 // 'auto' and span depend on the opposite position for resolution (e.g. grid-row: auto / 1 or grid-column: span 3 / "myHeader").
207 ASSERT_NOT_REACHED(); 263 ASSERT_NOT_REACHED();
208 return GridResolvedPosition(0); 264 return GridResolvedPosition(0);
209 } 265 }
210 ASSERT_NOT_REACHED(); 266 ASSERT_NOT_REACHED();
211 return GridResolvedPosition(0); 267 return GridResolvedPosition(0);
212 } 268 }
213 269
214 GridSpan GridResolvedPosition::resolveGridPositionAgainstOppositePosition(const ComputedStyle& gridContainerStyle, const GridResolvedPosition& resolvedOppositeP osition, const GridPosition& position, GridPositionSide side) 270 GridSpan GridResolvedPosition::resolveGridPositionsFromStyle(const ComputedStyle & gridContainerStyle, const LayoutBox& gridItem, GridTrackSizingDirection direct ion)
215 { 271 {
216 if (position.isAuto()) { 272 GridPosition initialPosition, finalPosition;
217 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition.toInt()) 273 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i nitialPosition, finalPosition);
218 return GridSpan::definiteGridSpan(resolvedOppositePosition.prev(), r esolvedOppositePosition); 274
219 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition.next()); 275 GridPositionSide initialSide = initialPositionSide(direction);
276 GridPositionSide finalSide = finalPositionSide(direction);
277
278 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) {
279 // We can't get our grid positions without running the auto placement al gorithm.
280 return GridSpan::indefiniteGridSpan();
220 } 281 }
221 282
222 ASSERT(position.isSpan()); 283 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) {
223 ASSERT(position.spanPosition() > 0); 284 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case).
224 285 GridResolvedPosition finalResolvedPosition = resolveGridPositionFromStyl e(gridContainerStyle, finalPosition, finalSide);
225 if (!position.namedGridLine().isNull()) { 286 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi nalResolvedPosition, initialPosition, initialSide);
226 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position.
227 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer Style, resolvedOppositePosition, position, side);
228 } 287 }
229 288
230 return GridSpan::definiteGridSpanWithSpanAgainstOpposite(resolvedOppositePos ition, position, side); 289 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) {
231 } 290 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case).
232 291 GridResolvedPosition initialResolvedPosition = resolveGridPositionFromSt yle(gridContainerStyle, initialPosition, initialSide);
233 GridSpan GridResolvedPosition::resolveNamedGridLinePositionAgainstOppositePositi on(const ComputedStyle& gridContainerStyle, const GridResolvedPosition& resolved OppositePosition, const GridPosition& position, GridPositionSide side) 292 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in itialResolvedPosition, finalPosition, finalSide);
234 {
235 ASSERT(position.isSpan());
236 ASSERT(!position.namedGridLine().isNull());
237 // Negative positions are not allowed per the specification and should have been handled during parsing.
238 ASSERT(position.spanPosition() > 0);
239
240 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side);
241 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
242
243 // 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).
244 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html.
245 if (it == gridLinesNames.end()) {
246 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition.toInt())
247 return GridSpan::definiteGridSpan(resolvedOppositePosition.prev(), r esolvedOppositePosition);
248 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition.next());
249 } 293 }
250 294
251 return GridSpan::definiteGridSpanWithNamedSpanAgainstOpposite(resolvedOpposi tePosition, position, side, it->value); 295 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle( gridContainerStyle, initialPosition, initialSide);
296 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(gr idContainerStyle, finalPosition, finalSide);
297
298 if (resolvedFinalPosition < resolvedInitialPosition)
299 std::swap(resolvedFinalPosition, resolvedInitialPosition);
300 else if (resolvedFinalPosition == resolvedInitialPosition)
301 resolvedFinalPosition = resolvedInitialPosition.next();
302
303 return GridSpan::definiteGridSpan(resolvedInitialPosition, resolvedFinalPosi tion);
252 } 304 }
253 305
254 } // namespace blink 306 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/style/GridResolvedPosition.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698