| 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/RenderBox.h" | |
| 9 #include "core/rendering/style/GridCoordinate.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 static const NamedGridLinesMap& gridLinesForSide(const RenderStyle& style, GridP
ositionSide side) | |
| 14 { | |
| 15 return (side == ColumnStartSide || side == ColumnEndSide) ? style.namedGridC
olumnLines() : style.namedGridRowLines(); | |
| 16 } | |
| 17 | |
| 18 static inline String implicitNamedGridLineForSide(const String& lineName, GridPo
sitionSide side) | |
| 19 { | |
| 20 return lineName + ((side == ColumnStartSide || side == RowStartSide) ? "-sta
rt" : "-end"); | |
| 21 } | |
| 22 | |
| 23 static bool isValidNamedLineOrArea(const String& lineName, const RenderStyle& st
yle, GridPositionSide side) | |
| 24 { | |
| 25 const NamedGridLinesMap& gridLineNames = gridLinesForSide(style, side); | |
| 26 | |
| 27 return gridLineNames.contains(implicitNamedGridLineForSide(lineName, side))
|| gridLineNames.contains(lineName); | |
| 28 } | |
| 29 | |
| 30 static GridPositionSide calculateInitialPositionSide(GridTrackSizingDirection di
rection) | |
| 31 { | |
| 32 return (direction == ForColumns) ? ColumnStartSide : RowStartSide; | |
| 33 } | |
| 34 | |
| 35 static GridPositionSide calculateFinalPositionSide(GridTrackSizingDirection dire
ction) | |
| 36 { | |
| 37 return (direction == ForColumns) ? ColumnEndSide : RowEndSide; | |
| 38 } | |
| 39 | |
| 40 void GridResolvedPosition::initialAndFinalPositionsFromStyle(const RenderStyle&
gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirection directio
n, GridPosition& initialPosition, GridPosition& finalPosition) | |
| 41 { | |
| 42 initialPosition = (direction == ForColumns) ? gridItem.style()->gridColumnSt
art() : gridItem.style()->gridRowStart(); | |
| 43 finalPosition = (direction == ForColumns) ? gridItem.style()->gridColumnEnd(
) : gridItem.style()->gridRowEnd(); | |
| 44 GridPositionSide initialPositionSide = calculateInitialPositionSide(directio
n); | |
| 45 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction); | |
| 46 | |
| 47 // We must handle the placement error handling code here instead of in the S
tyleAdjuster 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 w
e could assume later that | |
| 53 // GridResolvedPosition::resolveGrisPositionFromStyle() always return a vali
d resolved position. | |
| 54 if (initialPosition.isNamedGridArea() && !isValidNamedLineOrArea(initialPosi
tion.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 li
ne in a given dimension, instead treat the grid span as one. | |
| 61 if (initialPosition.isAuto() && finalPosition.isSpan() && !finalPosition.nam
edGridLine().isNull()) | |
| 62 finalPosition.setSpanPosition(1, String()); | |
| 63 if (finalPosition.isAuto() && initialPosition.isSpan() && !initialPosition.n
amedGridLine().isNull()) | |
| 64 initialPosition.setSpanPosition(1, String()); | |
| 65 } | |
| 66 | |
| 67 GridSpan GridResolvedPosition::resolveGridPositionsFromAutoPlacementPosition(con
st RenderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDi
rection direction, const GridResolvedPosition& resolvedInitialPosition) | |
| 68 { | |
| 69 GridPosition initialPosition, finalPosition; | |
| 70 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i
nitialPosition, finalPosition); | |
| 71 | |
| 72 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction); | |
| 73 | |
| 74 // This method will only be used when both positions need to be resolved aga
inst the opposite one. | |
| 75 ASSERT(initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPos
ition.shouldBeResolvedAgainstOppositePosition()); | |
| 76 | |
| 77 GridResolvedPosition resolvedFinalPosition = resolvedInitialPosition; | |
| 78 | |
| 79 if (initialPosition.isSpan()) { | |
| 80 resolvedFinalPosition = resolveGridPositionAgainstOppositePosition(gridC
ontainerStyle, resolvedInitialPosition, initialPosition, finalPositionSide)->res
olvedFinalPosition; | |
| 81 } else if (finalPosition.isSpan()) { | |
| 82 resolvedFinalPosition = resolveGridPositionAgainstOppositePosition(gridC
ontainerStyle, resolvedInitialPosition, finalPosition, finalPositionSide)->resol
vedFinalPosition; | |
| 83 } | |
| 84 | |
| 85 return GridSpan(resolvedInitialPosition, resolvedFinalPosition); | |
| 86 } | |
| 87 | |
| 88 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionsFromStyle(const R
enderStyle& gridContainerStyle, const RenderBox& gridItem, GridTrackSizingDirect
ion direction) | |
| 89 { | |
| 90 GridPosition initialPosition, finalPosition; | |
| 91 initialAndFinalPositionsFromStyle(gridContainerStyle, gridItem, direction, i
nitialPosition, finalPosition); | |
| 92 | |
| 93 GridPositionSide initialPositionSide = calculateInitialPositionSide(directio
n); | |
| 94 GridPositionSide finalPositionSide = calculateFinalPositionSide(direction); | |
| 95 | |
| 96 if (initialPosition.shouldBeResolvedAgainstOppositePosition() && finalPositi
on.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 al
gorithm. | |
| 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 = resolveGridPositionFromStyl
e(gridContainerStyle, finalPosition, finalPositionSide); | |
| 108 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, fi
nalResolvedPosition, 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 = resolveGridPositionFromSt
yle(gridContainerStyle, initialPosition, initialPositionSide); | |
| 114 return resolveGridPositionAgainstOppositePosition(gridContainerStyle, in
itialResolvedPosition, finalPosition, finalPositionSide); | |
| 115 } | |
| 116 | |
| 117 GridResolvedPosition resolvedInitialPosition = resolveGridPositionFromStyle(
gridContainerStyle, initialPosition, initialPositionSide); | |
| 118 GridResolvedPosition resolvedFinalPosition = resolveGridPositionFromStyle(gr
idContainerStyle, finalPosition, finalPositionSide); | |
| 119 | |
| 120 // If 'grid-after' specifies a line at or before that specified by 'grid-bef
ore', it computes to 'span 1'. | |
| 121 if (resolvedFinalPosition < resolvedInitialPosition) | |
| 122 resolvedFinalPosition = resolvedInitialPosition; | |
| 123 | |
| 124 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosition)
); | |
| 125 } | |
| 126 | |
| 127 size_t GridResolvedPosition::explicitGridColumnCount(const RenderStyle& gridCont
ainerStyle) | |
| 128 { | |
| 129 return gridContainerStyle.gridTemplateColumns().size(); | |
| 130 } | |
| 131 | |
| 132 size_t GridResolvedPosition::explicitGridRowCount(const RenderStyle& gridContain
erStyle) | |
| 133 { | |
| 134 return gridContainerStyle.gridTemplateRows().size(); | |
| 135 } | |
| 136 | |
| 137 size_t GridResolvedPosition::explicitGridSizeForSide(const RenderStyle& gridCont
ainerStyle, GridPositionSide side) | |
| 138 { | |
| 139 return (side == ColumnStartSide || side == ColumnEndSide) ? explicitGridColu
mnCount(gridContainerStyle) : explicitGridRowCount(gridContainerStyle); | |
| 140 } | |
| 141 | |
| 142 GridResolvedPosition GridResolvedPosition::resolveNamedGridLinePositionFromStyle
(const RenderStyle& gridContainerStyle, const GridPosition& position, GridPositi
onSide side) | |
| 143 { | |
| 144 ASSERT(!position.namedGridLine().isNull()); | |
| 145 | |
| 146 const NamedGridLinesMap& gridLinesNames = gridLinesForSide(gridContainerStyl
e, side); | |
| 147 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri
dLine()); | |
| 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->va
lue.size()) - 1; | |
| 158 else | |
| 159 namedGridLineIndex = std::max<int>(it->value.size() - abs(position.integ
erPosition()), 0); | |
| 160 return adjustGridPositionForSide(it->value[namedGridLineIndex], side); | |
| 161 } | |
| 162 | |
| 163 GridResolvedPosition GridResolvedPosition::resolveGridPositionFromStyle(const Re
nderStyle& gridContainerStyle, const GridPosition& position, GridPositionSide si
de) | |
| 164 { | |
| 165 switch (position.type()) { | |
| 166 case ExplicitPosition: { | |
| 167 ASSERT(position.integerPosition()); | |
| 168 | |
| 169 if (!position.namedGridLine().isNull()) | |
| 170 return resolveNamedGridLinePositionFromStyle(gridContainerStyle, pos
ition, side); | |
| 171 | |
| 172 // Handle <integer> explicit position. | |
| 173 if (position.isPositive()) | |
| 174 return adjustGridPositionForSide(position.integerPosition() - 1, sid
e); | |
| 175 | |
| 176 size_t resolvedPosition = abs(position.integerPosition()) - 1; | |
| 177 const size_t endOfTrack = explicitGridSizeForSide(gridContainerStyle, si
de); | |
| 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(gridContainerS
tyle, 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, contribu
tes 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 w
e 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 | |
| 220 PassOwnPtr<GridSpan> GridResolvedPosition::resolveGridPositionAgainstOppositePos
ition(const RenderStyle& gridContainerStyle, const GridResolvedPosition& resolve
dOppositePosition, const GridPosition& position, GridPositionSide side) | |
| 221 { | |
| 222 if (position.isAuto()) | |
| 223 return GridSpan::create(resolvedOppositePosition, resolvedOppositePositi
on); | |
| 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 / afte
r our opposite position. | |
| 230 return resolveNamedGridLinePositionAgainstOppositePosition(gridContainer
Style, resolvedOppositePosition, position, side); | |
| 231 } | |
| 232 | |
| 233 return GridSpan::createWithSpanAgainstOpposite(resolvedOppositePosition, pos
ition, side); | |
| 234 } | |
| 235 | |
| 236 PassOwnPtr<GridSpan> GridResolvedPosition::resolveNamedGridLinePositionAgainstOp
positePosition(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(gridContainerStyl
e, side); | |
| 244 NamedGridLinesMap::const_iterator it = gridLinesNames.find(position.namedGri
dLine()); | |
| 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, resolvedOppositePositi
on); | |
| 250 | |
| 251 return GridSpan::createWithNamedSpanAgainstOpposite(resolvedOppositePosition
, position, side, it->value); | |
| 252 } | |
| 253 | |
| 254 } // namespace blink | |
| OLD | NEW |