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

Side by Side Diff: Source/core/rendering/style/GridResolvedPosition.cpp

Issue 166623002: [CSS Grid Layout] Introduce an explicit type for resolved grid positions (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Apply suggested changes Created 6 years, 9 months 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
(Empty)
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/RenderGrid.h"
Julien - ping for review 2014/04/03 22:26:19 This doesn't seem like the right #include to me as
9
10 namespace WebCore {
11
12 GridSpan GridResolvedPosition::resolveGridPositionsFromAutoPlacementPosition(con st RenderBox&, GridTrackSizingDirection, const GridResolvedPosition& initialPosi tion)
13 {
14 // FIXME: We don't support spanning with auto positions yet. Once we do, thi s is wrong. Also we should make
15 // sure the grid can accomodate the new item as we only grow 1 position in a given direction.
16 return GridSpan(initialPosition, initialPosition);
17 }
18
19 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionsFromStyle(const R enderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirect ion direction)
20 {
21 GridPosition initialPosition = (direction == ForColumns) ? gridItem.style()- >gridColumnStart() : gridItem.style()->gridRowStart();
22 const GridPositionSide initialPositionSide = (direction == ForColumns) ? Col umnStartSide : RowStartSide;
23 GridPosition finalPosition = (direction == ForColumns) ? gridItem.style()->g ridColumnEnd() : gridItem.style()->gridRowEnd();
24 const GridPositionSide finalPositionSide = (direction == ForColumns) ? Colum nEndSide : RowEndSide;
25
26 // We must handle the placement error handling code here instead of in the S tyleAdjuster because we don't want to
27 // overwrite the specified values.
28 if (initialPosition.isSpan() && finalPosition.isSpan())
29 finalPosition.setAutoPosition();
30
31 if (initialPosition.isNamedGridArea() && !gridContainerStyle.namedGridArea() .contains(initialPosition.namedGridLine()))
32 initialPosition.setAutoPosition();
33
34 if (finalPosition.isNamedGridArea() && !gridContainerStyle.namedGridArea().c ontains(finalPosition.namedGridLine()))
35 finalPosition.setAutoPosition();
36
37 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) {
38 if (gridContainerStyle.gridAutoFlow() == AutoFlowNone)
39 return adoptPtr(new GridSpan(0, 0));
40
41 // We can't get our grid positions without running the auto placement al gorithm.
42 return nullptr;
43 }
44
45 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) {
46 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case).
47 GridResolvedPosition finalResolvedPosition = resolveGridPositionFromStyl e(gridContainerStyle, finalPosition, finalPositionSide);
48 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi nalResolvedPosition, initialPosition, initialPositionSide);
49 }
50
51 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) {
52 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case).
53 GridResolvedPosition initialResolvedPosition = resolveGridPositionFromSt yle(gridContainerStyle, initialPosition, initialPositionSide);
54 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in itialResolvedPosition, finalPosition, finalPositionSide);
55 }
56
57 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle( gridContainerStyle, initialPosition, initialPositionSide);
58 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(gr idContainerStyle, finalPosition, finalPositionSide);
59
60 // If 'grid-after' specifies a line at or before that specified by 'grid-bef ore', it computes to 'span 1'.
61 if (resolvedFinalPosition < resolvedInitialPosition)
62 resolvedFinalPosition = resolvedInitialPosition;
63
64 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition) );
65 }
66
67 size_t GridResolvedPosition::explicitGridColumnCount(const RenderStyle& gridCont ainerStyle)
68 {
69 return gridContainerStyle.gridTemplateColumns().size();
70 }
71
72 size_t GridResolvedPosition::explicitGridRowCount(const RenderStyle& gridContain erStyle)
73 {
74 return gridContainerStyle.gridTemplateRows().size();
75 }
76
77 size_t GridResolvedPosition::explicitGridSizeForSide(const RenderStyle& gridCont ainerStyle, GridPositionSide side)
78 {
79 return (side == ColumnStartSide || side == ColumnEndSide) ? explicitGridColu mnCount(gridContainerStyle) : explicitGridRowCount(gridContainerStyle);
80 }
81
82 GridResolvedPosition GridResolvedPosition::resolveNamedGridLinePositionFromStyle (const RenderStyle& gridContainerStyle, const GridPosition& position, GridPositi onSide side)
83 {
84 ASSERT(!position.namedGridLine().isNull());
85
86 const NamedGridLinesMap& gridLinesNames = (side == ColumnStartSide || side = = ColumnEndSide) ? gridContainerStyle.namedGridColumnLines() : gridContainerStyl e.namedGridRowLines();
87 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
88 if (it == gridLinesNames.end()) {
89 if (position.isPositive())
90 return GridResolvedPosition(0);
91 const size_t lastLine = explicitGridSizeForSide(gridContainerStyle, side );
92 return adjustGridPositionForSide(lastLine, side);
93 }
94
95 size_t namedGridLineIndex;
96 if (position.isPositive())
97 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1;
98 else
99 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0);
100 return adjustGridPositionForSide(it->value[namedGridLineIndex], side);
101 }
102
103 GridResolvedPosition GridResolvedPosition::resolveGridPositionFromStyle(const Re nderStyle& gridContainerStyle, const GridPosition& position, GridPositionSide si de)
104 {
105 switch (position.type()) {
106 case ExplicitPosition: {
107 ASSERT(position.integerPosition());
108
109 if (!position.namedGridLine().isNull())
110 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos ition, side);
111
112 // Handle <integer> explicit position.
113 if (position.isPositive())
114 return adjustGridPositionForSide(position.integerPosition() - 1, sid e);
115
116 size_t resolvedPosition = abs(position.integerPosition()) - 1;
117 const size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, si de);
118
119 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line.
120 if (endOfTrack < resolvedPosition)
121 return GridResolvedPosition(0);
122
123 return adjustGridPositionForSide(endOfTrack - resolvedPosition, side);
124 }
125 case NamedGridAreaPosition:
126 {
127 NamedGridAreaMap::const_iterator it = gridContainerStyle.namedGridArea() .find(position.namedGridLine());
128 // Unknown grid area should have been computed to 'auto' by now.
129 ASSERT_WITH_SECURITY_IMPLICATION(it != gridContainerStyle.namedGridArea( ).end());
130 const GridCoordinate& gridAreaCoordinate = it->value;
131 switch (side) {
132 case ColumnStartSide:
133 return gridAreaCoordinate.columns.resolvedInitialPosition;
134 case ColumnEndSide:
135 return gridAreaCoordinate.columns.resolvedFinalPosition;
136 case RowStartSide:
137 return gridAreaCoordinate.rows.resolvedInitialPosition;
138 case RowEndSide:
139 return GridResolvedPosition(gridAreaCoordinate.rows.resolvedFinalPos ition);
140 }
141 ASSERT_NOT_REACHED();
142 return GridResolvedPosition(0);
143 }
144 case AutoPosition:
145 case SpanPosition:
146 // 'auto' and span depend on the opposite position for resolution (e.g. grid-row: auto / 1 or grid-column: span 3 / "myHeader").
147 ASSERT_NOT_REACHED();
148 return GridResolvedPosition(0);
149 }
150 ASSERT_NOT_REACHED();
151 return GridResolvedPosition(0);
152 }
153
154 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionAgainstOppositePos ition(const RenderStyle& gridContainerStyle, const GridResolvedPosition& resolve dOppositePosition, const GridPosition& position, GridPositionSide side)
155 {
156 if (position.isAuto())
157 return GridSpan::create(resolvedOppositePosition, resolvedOppositePositi on);
158
159 ASSERT(position.isSpan());
160 ASSERT(position.spanPosition() > 0);
161
162 if (!position.namedGridLine().isNull()) {
163 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position.
164 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer Style, resolvedOppositePosition, position, side);
165 }
166
167 return GridSpan::createWithSpanAgainstOpposite(resolvedOppositePosition, pos ition, side);
168 }
169
170 PassOwnPtr<GridSpan> GridResolvedPosition::resolveNamedGridLinePositionAgainstOp positePosition(const RenderStyle& gridContainerStyle, const GridResolvedPosition & resolvedOppositePosition, const GridPosition& position, GridPositionSide side)
171 {
172 ASSERT(position.isSpan());
173 ASSERT(!position.namedGridLine().isNull());
174 // Negative positions are not allowed per the specification and should have been handled during parsing.
175 ASSERT(position.spanPosition() > 0);
176
177 const NamedGridLinesMap& gridLinesNames = (side == ColumnStartSide || side = = ColumnEndSide) ? gridContainerStyle.namedGridColumnLines() : gridContainerStyl e.namedGridRowLines();
178 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine());
179
180 // 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).
181 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html.
182 if (it == gridLinesNames.end())
183 return GridSpan::create(resolvedOppositePosition, resolvedOppositePositi on);
184
185 return GridSpan::createWithNamedSpanAgainstOpposite(resolvedOppositePosition , position, side, it->value);
186 }
187
188 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698