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

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

Issue 1529083006: [css-grid] Initial support for implicit grid before explicit grid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try to fix Windows build 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
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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(size_t resol vedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLin es) 65 static GridSpan definiteGridSpanWithInitialNamedSpanAgainstOpposite(size_t resol vedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLin es)
66 { 66 {
67 if (resolvedOppositePosition == 0) 67 if (resolvedOppositePosition == 0)
68 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition + 1); 68 return GridSpan::untranslatedGridSpan(resolvedOppositePosition, resolved OppositePosition + 1);
69 69
70 size_t firstLineBeforeOppositePositionIndex = 0; 70 size_t firstLineBeforeOppositePositionIndex = 0;
71 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLines.b egin(), gridLines.end(), resolvedOppositePosition); 71 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLines.b egin(), gridLines.end(), resolvedOppositePosition);
72 if (firstLineBeforeOppositePosition != gridLines.end()) 72 if (firstLineBeforeOppositePosition != gridLines.end())
73 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePosition - gridLines.begin(); 73 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePosition - gridLines.begin();
74 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionIndex - position.spanPosition()); 74 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionIndex - position.spanPosition());
75 size_t resolvedGridLinePosition = gridLines[gridLineIndex]; 75 size_t resolvedGridLinePosition = gridLines[gridLineIndex];
76 if (resolvedGridLinePosition >= resolvedOppositePosition) 76 if (resolvedGridLinePosition >= resolvedOppositePosition)
77 resolvedGridLinePosition = resolvedOppositePosition - 1; 77 resolvedGridLinePosition = resolvedOppositePosition - 1;
78 return GridSpan::definiteGridSpan(resolvedGridLinePosition, resolvedOpposite Position); 78 return GridSpan::untranslatedGridSpan(resolvedGridLinePosition, resolvedOppo sitePosition);
79 } 79 }
80 80
81 static GridSpan definiteGridSpanWithFinalNamedSpanAgainstOpposite(size_t resolve dOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines ) 81 static GridSpan definiteGridSpanWithFinalNamedSpanAgainstOpposite(size_t resolve dOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines )
82 { 82 {
83 ASSERT(gridLines.size()); 83 ASSERT(gridLines.size());
84 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; 84 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1;
85 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLines.be gin(), gridLines.end(), resolvedOppositePosition); 85 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLines.be gin(), gridLines.end(), resolvedOppositePosition);
86 if (firstLineAfterOppositePosition != gridLines.end()) 86 if (firstLineAfterOppositePosition != gridLines.end())
87 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - g ridLines.begin(); 87 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - g ridLines.begin();
88 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOpposite PositionIndex + position.spanPosition() - 1); 88 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOpposite PositionIndex + position.spanPosition() - 1);
89 size_t resolvedGridLinePosition = gridLines[gridLineIndex]; 89 size_t resolvedGridLinePosition = gridLines[gridLineIndex];
90 if (resolvedGridLinePosition <= resolvedOppositePosition) 90 if (resolvedGridLinePosition <= resolvedOppositePosition)
91 resolvedGridLinePosition = resolvedOppositePosition + 1; 91 resolvedGridLinePosition = resolvedOppositePosition + 1;
92 92
93 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedGridLine Position); 93 return GridSpan::untranslatedGridSpan(resolvedOppositePosition, resolvedGrid LinePosition);
94 } 94 }
95 95
96 static GridSpan definiteGridSpanWithNamedSpanAgainstOpposite(size_t resolvedOppo sitePosition, const GridPosition& position, GridPositionSide side, const Vector< size_t>& gridLines) 96 static GridSpan definiteGridSpanWithNamedSpanAgainstOpposite(size_t resolvedOppo sitePosition, const GridPosition& position, GridPositionSide side, const Vector< size_t>& gridLines)
97 { 97 {
98 if (side == RowStartSide || side == ColumnStartSide) 98 if (side == RowStartSide || side == ColumnStartSide)
99 return definiteGridSpanWithInitialNamedSpanAgainstOpposite(resolvedOppos itePosition, position, gridLines); 99 return definiteGridSpanWithInitialNamedSpanAgainstOpposite(resolvedOppos itePosition, position, gridLines);
100 100
101 return definiteGridSpanWithFinalNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines); 101 return definiteGridSpanWithFinalNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines);
102 } 102 }
103 103
104 static GridSpan resolveNamedGridLinePositionAgainstOppositePosition(const Comput edStyle& gridContainerStyle, size_t resolvedOppositePosition, const GridPosition & position, GridPositionSide side) 104 static GridSpan resolveNamedGridLinePositionAgainstOppositePosition(const Comput edStyle& gridContainerStyle, size_t resolvedOppositePosition, const GridPosition & position, GridPositionSide side)
105 { 105 {
106 ASSERT(position.isSpan()); 106 ASSERT(position.isSpan());
107 ASSERT(!position.namedGridLine().isNull()); 107 ASSERT(!position.namedGridLine().isNull());
108 // Negative positions are not allowed per the specification and should have been handled during parsing. 108 // Negative positions are not allowed per the specification and should have been handled during parsing.
109 ASSERT(position.spanPosition() > 0); 109 ASSERT(position.spanPosition() > 0);
110 110
111 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side); 111 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl e, side);
112 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine()); 112 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
113 113
114 // 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 // 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).
115 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html. 115 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html.
116 if (it == gridLinesNames.end()) { 116 if (it == gridLinesNames.end()) {
117 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition) 117 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition)
118 return GridSpan::definiteGridSpan(resolvedOppositePosition - 1, reso lvedOppositePosition); 118 return GridSpan::untranslatedGridSpan(resolvedOppositePosition - 1, resolvedOppositePosition);
119 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition + 1); 119 return GridSpan::untranslatedGridSpan(resolvedOppositePosition, resolved OppositePosition + 1);
120 } 120 }
121 121
122 return definiteGridSpanWithNamedSpanAgainstOpposite(resolvedOppositePosition , position, side, it->value); 122 return definiteGridSpanWithNamedSpanAgainstOpposite(resolvedOppositePosition , position, side, it->value);
123 } 123 }
124 124
125 static GridSpan definiteGridSpanWithSpanAgainstOpposite(size_t resolvedOppositeP osition, const GridPosition& position, GridPositionSide side) 125 static GridSpan definiteGridSpanWithSpanAgainstOpposite(size_t resolvedOppositeP osition, const GridPosition& position, GridPositionSide side)
126 { 126 {
127 size_t positionOffset = position.spanPosition(); 127 size_t positionOffset = position.spanPosition();
128 if (side == ColumnStartSide || side == RowStartSide) { 128 if (side == ColumnStartSide || side == RowStartSide)
129 if (resolvedOppositePosition == 0) 129 return GridSpan::untranslatedGridSpan(resolvedOppositePosition - positio nOffset, resolvedOppositePosition);
jfernandez 2015/12/18 14:33:21 Are we handling here the case where resolvedOpposi
Manuel Rego 2015/12/18 22:42:30 Precisely now we support negative values, so we do
130 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolved OppositePosition + 1);
131 130
132 size_t initialResolvedPosition = std::max<int>(0, resolvedOppositePositi on - positionOffset); 131 return GridSpan::untranslatedGridSpan(resolvedOppositePosition, resolvedOppo sitePosition + positionOffset);
133 return GridSpan::definiteGridSpan(initialResolvedPosition, resolvedOppos itePosition);
134 }
135
136 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOpposite Position + positionOffset);
137 } 132 }
138 133
139 static GridSpan resolveGridPositionAgainstOppositePosition(const ComputedStyle& gridContainerStyle, size_t resolvedOppositePosition, const GridPosition& positio n, GridPositionSide side) 134 static GridSpan resolveGridPositionAgainstOppositePosition(const ComputedStyle& gridContainerStyle, size_t resolvedOppositePosition, const GridPosition& positio n, GridPositionSide side)
140 { 135 {
141 if (position.isAuto()) { 136 if (position.isAuto()) {
142 if ((side == ColumnStartSide || side == RowStartSide) && resolvedOpposit ePosition) 137 if (side == ColumnStartSide || side == RowStartSide)
143 return GridSpan::definiteGridSpan(resolvedOppositePosition - 1, reso lvedOppositePosition); 138 return GridSpan::untranslatedGridSpan(resolvedOppositePosition - 1, resolvedOppositePosition);
144 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedOppo sitePosition + 1); 139 return GridSpan::untranslatedGridSpan(resolvedOppositePosition, resolved OppositePosition + 1);
145 } 140 }
146 141
147 ASSERT(position.isSpan()); 142 ASSERT(position.isSpan());
148 ASSERT(position.spanPosition() > 0); 143 ASSERT(position.spanPosition() > 0);
149 144
150 if (!position.namedGridLine().isNull()) { 145 if (!position.namedGridLine().isNull()) {
151 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position. 146 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position.
152 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer Style, resolvedOppositePosition, position, side); 147 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer Style, resolvedOppositePosition, position, side);
153 } 148 }
154 149
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } 196 }
202 197
203 size_t namedGridLineIndex; 198 size_t namedGridLineIndex;
204 if (position.isPositive()) 199 if (position.isPositive())
205 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1; 200 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1;
206 else 201 else
207 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0); 202 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0);
208 return it->value[namedGridLineIndex]; 203 return it->value[namedGridLineIndex];
209 } 204 }
210 205
211 static size_t resolveGridPositionFromStyle(const ComputedStyle& gridContainerSty le, const GridPosition& position, GridPositionSide side) 206 static int resolveGridPositionFromStyle(const ComputedStyle& gridContainerStyle, const GridPosition& position, GridPositionSide side)
212 { 207 {
213 switch (position.type()) { 208 switch (position.type()) {
214 case ExplicitPosition: { 209 case ExplicitPosition: {
215 ASSERT(position.integerPosition()); 210 ASSERT(position.integerPosition());
216 211
217 if (!position.namedGridLine().isNull()) 212 if (!position.namedGridLine().isNull())
218 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos ition, side); 213 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos ition, side);
219 214
220 // Handle <integer> explicit position. 215 // Handle <integer> explicit position.
221 if (position.isPositive()) 216 if (position.isPositive())
222 return position.integerPosition() - 1; 217 return position.integerPosition() - 1;
223 218
224 size_t resolvedPosition = abs(position.integerPosition()) - 1; 219 size_t resolvedPosition = abs(position.integerPosition()) - 1;
225 size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, side); 220 size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, side);
226 221
227 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line.
228 if (endOfTrack < resolvedPosition)
229 return 0;
230
231 return endOfTrack - resolvedPosition; 222 return endOfTrack - resolvedPosition;
232 } 223 }
233 case NamedGridAreaPosition: 224 case NamedGridAreaPosition:
234 { 225 {
235 // First attempt to match the grid area's edge to a named grid area: if there is a named line with the name 226 // First attempt to match the grid area's edge to a named grid area: if there is a named line with the name
236 // ''<custom-ident>-start (for grid-*-start) / <custom-ident>-end'' (for grid-*-end), contributes the first such 227 // ''<custom-ident>-start (for grid-*-start) / <custom-ident>-end'' (for grid-*-end), contributes the first such
237 // line to the grid item's placement. 228 // line to the grid item's placement.
238 String namedGridLine = position.namedGridLine(); 229 String namedGridLine = position.namedGridLine();
239 ASSERT(GridResolvedPosition::isValidNamedLineOrArea(namedGridLine, gridC ontainerStyle, side)); 230 ASSERT(GridResolvedPosition::isValidNamedLineOrArea(namedGridLine, gridC ontainerStyle, side));
240 231
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 GridPositionSide initialSide = initialPositionSide(direction); 264 GridPositionSide initialSide = initialPositionSide(direction);
274 GridPositionSide finalSide = finalPositionSide(direction); 265 GridPositionSide finalSide = finalPositionSide(direction);
275 266
276 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) { 267 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) {
277 // We can't get our grid positions without running the auto placement al gorithm. 268 // We can't get our grid positions without running the auto placement al gorithm.
278 return GridSpan::indefiniteGridSpan(); 269 return GridSpan::indefiniteGridSpan();
279 } 270 }
280 271
281 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) { 272 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) {
282 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case). 273 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case).
283 size_t finalResolvedPosition = resolveGridPositionFromStyle(gridContaine rStyle, finalPosition, finalSide); 274 int finalResolvedPosition = resolveGridPositionFromStyle(gridContainerSt yle, finalPosition, finalSide);
284 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi nalResolvedPosition, initialPosition, initialSide); 275 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi nalResolvedPosition, initialPosition, initialSide);
285 } 276 }
286 277
287 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) { 278 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) {
288 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case). 279 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case).
289 size_t initialResolvedPosition = resolveGridPositionFromStyle(gridContai nerStyle, initialPosition, initialSide); 280 int initialResolvedPosition = resolveGridPositionFromStyle(gridContainer Style, initialPosition, initialSide);
290 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in itialResolvedPosition, finalPosition, finalSide); 281 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in itialResolvedPosition, finalPosition, finalSide);
291 } 282 }
292 283
293 size_t resolvedInitialPosition = resolveGridPositionFromStyle(gridContainerS tyle, initialPosition, initialSide); 284 int resolvedInitialPosition = resolveGridPositionFromStyle(gridContainerStyl e, initialPosition, initialSide);
294 size_t resolvedFinalPosition = resolveGridPositionFromStyle(gridContainerSty le, finalPosition, finalSide); 285 int resolvedFinalPosition = resolveGridPositionFromStyle(gridContainerStyle, finalPosition, finalSide);
295 286
296 if (resolvedFinalPosition < resolvedInitialPosition) 287 if (resolvedFinalPosition < resolvedInitialPosition)
297 std::swap(resolvedFinalPosition, resolvedInitialPosition); 288 std::swap(resolvedFinalPosition, resolvedInitialPosition);
298 else if (resolvedFinalPosition == resolvedInitialPosition) 289 else if (resolvedFinalPosition == resolvedInitialPosition)
299 resolvedFinalPosition = resolvedInitialPosition + 1; 290 resolvedFinalPosition = resolvedInitialPosition + 1;
300 291
301 return GridSpan::definiteGridSpan(resolvedInitialPosition, resolvedFinalPosi tion); 292 return GridSpan::untranslatedGridSpan(resolvedInitialPosition, resolvedFinal Position);
302 } 293 }
303 294
304 } // namespace blink 295 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698