OLD | NEW |
---|---|
(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" | |
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& style, const RenderBox* gridItem, GridTrackSizingDirection direction ) | |
Julien - ping for review
2014/03/21 23:49:27
Nit: s/style/gridContainerStyle/
Also shouldn't |
| |
20 { | |
21 const GridPosition& initialPosition = (direction == ForColumns) ? gridItem-> style()->gridColumnStart() : gridItem->style()->gridRowStart(); | |
22 const GridPositionSide initialPositionSide = (direction == ForColumns) ? Col umnStartSide : RowStartSide; | |
23 const GridPosition& finalPosition = (direction == ForColumns) ? gridItem->st yle()->gridColumnEnd() : gridItem->style()->gridRowEnd(); | |
24 const GridPositionSide finalPositionSide = (direction == ForColumns) ? Colum nEndSide : RowEndSide; | |
25 | |
26 // We should NEVER see both spans as they should have been handled during st yle resolve. | |
27 ASSERT(!initialPosition.isSpan() || !finalPosition.isSpan()); | |
28 | |
29 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi on.shouldBeResolvedAgainstOppositePosition()) { | |
30 if (style.gridAutoFlow() == AutoFlowNone) | |
31 return adoptPtr(new GridSpan(0, 0)); | |
32 | |
33 // We can't get our grid positions without running the auto placement al gorithm. | |
34 return nullptr; | |
35 } | |
36 | |
37 if (initialPosition.shouldBeResolvedAgainstOppositePosition()) { | |
38 // Infer the position from the final position ('auto / 1' or 'span 2 / 3 ' case). | |
39 GridResolvedPosition finalResolvedPosition = resolveGridPositionFromStyl e(style, finalPosition, finalPositionSide); | |
40 return resolveGridPositionAgainstOppositePosition(style, finalResolvedPo sition, initialPosition, initialPositionSide); | |
41 } | |
42 | |
43 if (finalPosition.shouldBeResolvedAgainstOppositePosition()) { | |
44 // Infer our position from the initial position ('1 / auto' or '3 / span 2' case). | |
45 GridResolvedPosition initialResolvedPosition = resolveGridPositionFromSt yle(style, initialPosition, initialPositionSide); | |
46 return resolveGridPositionAgainstOppositePosition(style, initialResolved Position, finalPosition, finalPositionSide); | |
47 } | |
48 | |
49 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle( style, initialPosition, initialPositionSide); | |
50 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(st yle, finalPosition, finalPositionSide); | |
51 | |
52 // If 'grid-after' specifies a line at or before that specified by 'grid-bef ore', it computes to 'span 1'. | |
53 if (resolvedFinalPosition < resolvedInitialPosition) | |
54 resolvedFinalPosition = resolvedInitialPosition; | |
55 | |
56 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition) ); | |
57 } | |
58 | |
59 size_t GridResolvedPosition::explicitGridColumnCount(const RenderStyle& style) | |
60 { | |
61 return style.gridTemplateColumns().size(); | |
62 } | |
63 | |
64 size_t GridResolvedPosition::explicitGridRowCount(const RenderStyle& style) | |
65 { | |
66 return style.gridTemplateRows().size(); | |
67 } | |
68 | |
69 size_t GridResolvedPosition::explicitGridSizeForSide(const RenderStyle& style, G ridPositionSide side) | |
70 { | |
71 return (side == ColumnStartSide || side == ColumnEndSide) ? explicitGridColu mnCount(style) : explicitGridRowCount(style); | |
72 } | |
73 | |
74 GridResolvedPosition GridResolvedPosition::resolveNamedGridLinePositionFromStyle (const RenderStyle& style, const GridPosition& position, GridPositionSide side) | |
75 { | |
76 ASSERT(!position.namedGridLine().isNull()); | |
77 | |
78 const NamedGridLinesMap& gridLinesNames = (side == ColumnStartSide || side = = ColumnEndSide) ? style.namedGridColumnLines() : style.namedGridRowLines(); | |
79 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine()); | |
80 if (it == gridLinesNames.end()) { | |
81 if (position.isPositive()) | |
82 return GridResolvedPosition(0); | |
83 const size_t lastLine = explicitGridSizeForSide(style, side); | |
84 return adjustGridPositionForSide(lastLine, side); | |
85 } | |
86 | |
87 size_t namedGridLineIndex; | |
88 if (position.isPositive()) | |
89 namedGridLineIndex = std::min<size_t>(position.integerPosition(), it->va lue.size()) - 1; | |
90 else | |
91 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ erPosition()), 0); | |
92 return adjustGridPositionForSide(it->value[namedGridLineIndex], side); | |
93 } | |
94 | |
95 GridResolvedPosition GridResolvedPosition::resolveGridPositionFromStyle(const Re nderStyle& style, const GridPosition& position, GridPositionSide side) | |
96 { | |
97 switch (position.type()) { | |
98 case ExplicitPosition: { | |
99 ASSERT(position.integerPosition()); | |
100 | |
101 if (!position.namedGridLine().isNull()) | |
102 return resolveNamedGridLinePositionFromStyle(style, position, side); | |
103 | |
104 // Handle <integer> explicit position. | |
105 if (position.isPositive()) | |
106 return adjustGridPositionForSide(position.integerPosition() - 1, sid e); | |
107 | |
108 size_t resolvedPosition = abs(position.integerPosition()) - 1; | |
109 const size_t endOfTrack = explicitGridSizeForSide(style, side); | |
110 | |
111 // Per http://lists.w3.org/Archives/Public/www-style/2013Mar/0589.html, we clamp negative value to the first line. | |
112 if (endOfTrack < resolvedPosition) | |
113 return GridResolvedPosition(0); | |
114 | |
115 return adjustGridPositionForSide(endOfTrack - resolvedPosition, side); | |
116 } | |
117 case NamedGridAreaPosition: | |
118 { | |
119 NamedGridAreaMap::const_iterator it = style.namedGridArea().find(positio n.namedGridLine()); | |
120 // Unknown grid area should have been computed to 'auto' by now. | |
121 ASSERT_WITH_SECURITY_IMPLICATION(it != style.namedGridArea().end()); | |
122 const GridCoordinate& gridAreaCoordinate = it->value; | |
123 switch (side) { | |
124 case ColumnStartSide: | |
125 return gridAreaCoordinate.columns.resolvedInitialPosition; | |
126 case ColumnEndSide: | |
127 return gridAreaCoordinate.columns.resolvedFinalPosition; | |
128 case RowStartSide: | |
129 return gridAreaCoordinate.rows.resolvedInitialPosition; | |
130 case RowEndSide: | |
131 return GridResolvedPosition(gridAreaCoordinate.rows.resolvedFinalPos ition); | |
132 } | |
133 ASSERT_NOT_REACHED(); | |
134 return GridResolvedPosition(0); | |
135 } | |
136 case AutoPosition: | |
137 case SpanPosition: | |
138 // 'auto' and span depend on the opposite position for resolution (e.g. grid-row: auto / 1 or grid-column: span 3 / "myHeader"). | |
139 ASSERT_NOT_REACHED(); | |
140 return GridResolvedPosition(0); | |
141 } | |
142 ASSERT_NOT_REACHED(); | |
143 return GridResolvedPosition(0); | |
144 } | |
145 | |
146 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionAgainstOppositePos ition(const RenderStyle& style, const GridResolvedPosition& resolvedOppositePosi tion, const GridPosition& position, GridPositionSide side) | |
147 { | |
148 if (position.isAuto()) | |
149 return GridSpan::create(resolvedOppositePosition, resolvedOppositePositi on); | |
150 | |
151 ASSERT(position.isSpan()); | |
152 ASSERT(position.spanPosition() > 0); | |
153 | |
154 if (!position.namedGridLine().isNull()) { | |
155 // span 2 'c' -> we need to find the appropriate grid line before / afte r our opposite position. | |
156 return resolveNamedGridLinePositionAgainstOppositePosition(style, resolv edOppositePosition, position, side); | |
157 } | |
158 | |
159 return GridSpan::createWithSpanAgainstOpposite(resolvedOppositePosition, pos ition, side); | |
160 } | |
161 | |
162 PassOwnPtr<GridSpan> GridResolvedPosition::resolveNamedGridLinePositionAgainstOp positePosition(const RenderStyle& style, const GridResolvedPosition& resolvedOpp ositePosition, const GridPosition& position, GridPositionSide side) | |
163 { | |
164 ASSERT(position.isSpan()); | |
165 ASSERT(!position.namedGridLine().isNull()); | |
166 // Negative positions are not allowed per the specification and should have been handled during parsing. | |
167 ASSERT(position.spanPosition() > 0); | |
168 | |
169 const NamedGridLinesMap& gridLinesNames = (side == ColumnStartSide || side = = ColumnEndSide) ? style.namedGridColumnLines() : style.namedGridRowLines(); | |
170 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri dLine()); | |
171 | |
172 // 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). | |
173 // See http://lists.w3.org/Archives/Public/www-style/2013Jun/0394.html. | |
174 if (it == gridLinesNames.end()) | |
175 return GridSpan::create(resolvedOppositePosition, resolvedOppositePositi on); | |
176 | |
177 return GridSpan::createWithNamedSpanAgainstOpposite(resolvedOppositePosition , position, side, it->value); | |
178 } | |
Julien - ping for review
2014/03/21 23:49:27
It really seems like a lot of this code should be
| |
179 | |
180 } // namespace WebCore | |
OLD | NEW |