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

Unified Diff: Source/core/css/resolver/StyleAdjuster.cpp

Issue 148293008: [CSS Grid Layout] Add support to place items using named grid lines (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Improved readability of adjustNamedGridItemPosition Created 6 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/css/resolver/StyleAdjuster.h ('k') | Source/core/css/resolver/StyleBuilderCustom.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/css/resolver/StyleAdjuster.cpp
diff --git a/Source/core/css/resolver/StyleAdjuster.cpp b/Source/core/css/resolver/StyleAdjuster.cpp
index 7e8b3ee4309acba66f1e09e6ec046b116d58e71b..bfc2e9d4cd16b3e8a024c4f332ac6781fd3a2883 100644
--- a/Source/core/css/resolver/StyleAdjuster.cpp
+++ b/Source/core/css/resolver/StyleAdjuster.cpp
@@ -377,6 +377,83 @@ void StyleAdjuster::adjustRenderStyle(RenderStyle* style, RenderStyle* parentSty
}
}
+static inline bool gridLineDefinedBeforeGridArea(const String& gridLineName, const String& gridAreaName, const NamedGridAreaMap& gridAreaMap, const NamedGridLinesMap& namedLinesMap, GridPositionSide side)
+{
+ ASSERT(namedLinesMap.contains(gridLineName));
+ // Grid line indexes are inserted in order.
+ size_t namedGridLineFirstDefinition = GridPosition::adjustGridPositionForSide(namedLinesMap.get(gridLineName)[0], side);
+
+ ASSERT(gridAreaMap.contains(gridAreaName));
+ const GridCoordinate& gridAreaCoordinates = gridAreaMap.get(gridAreaName);
+
+ // GridCoordinate refers to tracks while the indexes in namedLinesMap refer to lines, that's why we need to add 1 to
+ // the grid coordinate to get the end line index.
+ switch (side) {
+ case ColumnStartSide:
+ return namedGridLineFirstDefinition < gridAreaCoordinates.columns.initialPositionIndex;
+ case ColumnEndSide:
+ return namedGridLineFirstDefinition < gridAreaCoordinates.columns.finalPositionIndex;
+ case RowStartSide:
+ return namedGridLineFirstDefinition < gridAreaCoordinates.rows.initialPositionIndex;
+ case RowEndSide:
+ return namedGridLineFirstDefinition < gridAreaCoordinates.rows.finalPositionIndex;
+ default:
+ ASSERT_NOT_REACHED();
+ return false;
+ }
+}
+
+PassOwnPtr<GridPosition> StyleAdjuster::adjustNamedGridItemPosition(const NamedGridAreaMap& gridAreaMap, const NamedGridLinesMap& namedLinesMap, const GridPosition& position, GridPositionSide side) const
+{
+ ASSERT(position.isNamedGridArea());
+ // The StyleBuilder always treats <custom-ident> as a named grid area. We must decide here if they are going to be resolved
+ // to either a grid area or a grid line.
+
+ String namedGridAreaOrGridLine = position.namedGridLine();
+ bool hasStartSuffix = namedGridAreaOrGridLine.endsWith("-start");
+ bool hasEndSuffix = namedGridAreaOrGridLine.endsWith("-end");
+ bool isStartSide = side == ColumnStartSide || side == RowStartSide;
Julien - ping for review 2014/03/05 01:12:35 This could probably be an helper function (not rea
+ bool hasStartSuffixForStartSide = hasStartSuffix && isStartSide;
+ bool hasEndSuffixForEndSide = hasEndSuffix && !isStartSide;
+ size_t suffixLength = hasStartSuffix ? strlen("-start") : strlen("-end");
+ String gridAreaName = hasStartSuffixForStartSide || hasEndSuffixForEndSide ? namedGridAreaOrGridLine.substring(0, namedGridAreaOrGridLine.length() - suffixLength) : namedGridAreaOrGridLine;
+
+ if (gridAreaMap.contains(gridAreaName)) {
+ String gridLineName;
+ if (isStartSide && !hasStartSuffix)
Julien - ping for review 2014/03/05 01:12:35 I think I get why we need this now, thanks for the
+ gridLineName = namedGridAreaOrGridLine + "-start";
+ else if (!isStartSide && !hasEndSuffix)
+ gridLineName = namedGridAreaOrGridLine + "-end";
+ else
+ gridLineName = namedGridAreaOrGridLine;
+
+ if (namedLinesMap.contains(gridLineName) && gridLineDefinedBeforeGridArea(gridLineName, gridAreaName, gridAreaMap, namedLinesMap, side)) {
+ // Use the explicitly defined grid line defined before the grid area instead of the grid area.
+ OwnPtr<GridPosition> adjustedPosition = adoptPtr(new GridPosition());
+ adjustedPosition->setExplicitPosition(1, gridLineName);
+ return adjustedPosition.release();
+ }
+
+ if (hasStartSuffixForStartSide || hasEndSuffixForEndSide) {
+ // Renderer expects the grid area name instead of the implicit grid line name.
+ OwnPtr<GridPosition> adjustedPosition = adoptPtr(new GridPosition());
+ adjustedPosition->setNamedGridArea(gridAreaName);
+ return adjustedPosition.release();
+ }
+
+ return nullptr;
+ }
+
+ if (namedLinesMap.contains(namedGridAreaOrGridLine)) {
+ OwnPtr<GridPosition> adjustedPosition = adoptPtr(new GridPosition());
+ adjustedPosition->setExplicitPosition(1, namedGridAreaOrGridLine);
+ return adjustedPosition.release();
+ }
+
+ // We need to clear unknown named grid areas
+ return adoptPtr(new GridPosition());
+}
+
void StyleAdjuster::adjustGridItemPosition(RenderStyle* style, RenderStyle* parentStyle) const
{
const GridPosition& columnStartPosition = style->gridColumnStart();
@@ -395,17 +472,28 @@ void StyleAdjuster::adjustGridItemPosition(RenderStyle* style, RenderStyle* pare
style->setGridRowEnd(GridPosition());
}
- // Unknown named grid area compute to 'auto'.
- const NamedGridAreaMap& map = parentStyle->namedGridArea();
-
-#define CLEAR_UNKNOWN_NAMED_AREA(prop, Prop) \
- if (prop.isNamedGridArea() && !map.contains(prop.namedGridLine())) \
- style->setGrid##Prop(GridPosition());
+ // If the grid position is a single <ident> then the spec mandates us to resolve it following this steps:
+ // * If there is a named grid area called <ident> resolve the position to the area's corresponding edge.
+ // * If a grid area was found with that name, check that there is no <ident>-start or <ident>-end (depending
+ // on the css property being defined) specified before the grid area. If that's the case resolve to that grid line.
+ // * Otherwise check if there is a grid line named <ident>.
+ // * Otherwise treat it as auto.
+
+ const NamedGridLinesMap& namedGridColumnLines = parentStyle->namedGridColumnLines();
+ const NamedGridLinesMap& namedGridRowLines = parentStyle->namedGridRowLines();
+ const NamedGridAreaMap& gridAreaMap = parentStyle->namedGridArea();
+
+#define ADJUST_GRID_POSITION_MAYBE(position, Prop, namedGridLines, side) \
+ if (position.isNamedGridArea()) { \
+ OwnPtr<GridPosition> adjustedPosition = adjustNamedGridItemPosition(gridAreaMap, namedGridLines, position, side); \
+ if (adjustedPosition) \
+ style->setGrid##Prop(*adjustedPosition); \
+ }
- CLEAR_UNKNOWN_NAMED_AREA(columnStartPosition, ColumnStart);
- CLEAR_UNKNOWN_NAMED_AREA(columnEndPosition, ColumnEnd);
- CLEAR_UNKNOWN_NAMED_AREA(rowStartPosition, RowStart);
- CLEAR_UNKNOWN_NAMED_AREA(rowEndPosition, RowEnd);
+ ADJUST_GRID_POSITION_MAYBE(columnStartPosition, ColumnStart, namedGridColumnLines, ColumnStartSide);
+ ADJUST_GRID_POSITION_MAYBE(columnEndPosition, ColumnEnd, namedGridColumnLines, ColumnEndSide);
+ ADJUST_GRID_POSITION_MAYBE(rowStartPosition, RowStart, namedGridRowLines, RowStartSide);
+ ADJUST_GRID_POSITION_MAYBE(rowEndPosition, RowEnd, namedGridRowLines, RowEndSide);
}
}
« no previous file with comments | « Source/core/css/resolver/StyleAdjuster.h ('k') | Source/core/css/resolver/StyleBuilderCustom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698