Chromium Code Reviews| Index: third_party/WebKit/Source/core/css/parser/CSSPropertyParser.cpp |
| diff --git a/third_party/WebKit/Source/core/css/parser/CSSPropertyParser.cpp b/third_party/WebKit/Source/core/css/parser/CSSPropertyParser.cpp |
| index 3dc334d768e21a641e0969afb2f8cc8dad524a95..4a486f00460d45a66b6f034979816770b079f756 100644 |
| --- a/third_party/WebKit/Source/core/css/parser/CSSPropertyParser.cpp |
| +++ b/third_party/WebKit/Source/core/css/parser/CSSPropertyParser.cpp |
| @@ -3109,18 +3109,20 @@ static RawPtr<CSSValue> consumeGridTrackSize(CSSParserTokenRange& range, CSSPars |
| return consumeGridBreadth(range, cssParserMode, restriction); |
| } |
| -static RawPtr<CSSGridLineNamesValue> consumeGridLineNames(CSSParserTokenRange& range) |
| +// Appends to the passed in CSSGridLineNamesValue if any, otherwise creates a new one. |
| +static CSSGridLineNamesValue* consumeGridLineNames(CSSParserTokenRange& range, CSSGridLineNamesValue* lineNames = nullptr) |
| { |
| CSSParserTokenRange rangeCopy = range; |
| if (rangeCopy.consumeIncludingWhitespace().type() != LeftBracketToken) |
| return nullptr; |
| - RawPtr<CSSGridLineNamesValue> lineNames = CSSGridLineNamesValue::create(); |
| + if (!lineNames) |
| + lineNames = CSSGridLineNamesValue::create(); |
| while (RawPtr<CSSCustomIdentValue> lineName = consumeCustomIdentForGridLine(rangeCopy)) |
| lineNames->append(lineName.release()); |
| if (rangeCopy.consumeIncludingWhitespace().type() != RightBracketToken) |
| return nullptr; |
| range = rangeCopy; |
| - return lineNames.release(); |
| + return lineNames; |
| } |
| static bool consumeGridTrackRepeatFunction(CSSParserTokenRange& range, CSSParserMode cssParserMode, CSSValueList& list, bool& isAutoRepeat) |
| @@ -3178,6 +3180,28 @@ static bool consumeGridTrackRepeatFunction(CSSParserTokenRange& range, CSSParser |
| return true; |
| } |
| +static bool allTracksAreFixedSized(CSSValueList& valueList) |
| +{ |
| + for (auto value : valueList) { |
| + if (value->isGridLineNamesValue()) |
| + continue; |
| + // The auto-repeat value holds a <fixed-size> = <fixed-breadth> | minmax( <fixed-breadth>, <track-breadth> ) |
| + if (value->isGridAutoRepeatValue()) { |
| + if (!allTracksAreFixedSized(toCSSValueList(*value))) |
| + return false; |
| + continue; |
| + } |
| + ASSERT(value->isPrimitiveValue() || (value->isFunctionValue() && toCSSFunctionValue(*value).item(0))); |
| + const CSSPrimitiveValue& primitiveValue = value->isPrimitiveValue() |
| + ? toCSSPrimitiveValue(*value) |
| + : toCSSPrimitiveValue(*toCSSFunctionValue(*value).item(0)); |
| + CSSValueID valueID = primitiveValue.getValueID(); |
| + if (valueID == CSSValueMinContent || valueID == CSSValueMaxContent || valueID == CSSValueAuto || primitiveValue.isFlex()) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| static RawPtr<CSSValue> consumeGridTrackList(CSSParserTokenRange& range, CSSParserMode cssParserMode) |
| { |
| RawPtr<CSSValueList> values = CSSValueList::createSpaceSeparated(); |
| @@ -3218,6 +3242,100 @@ static RawPtr<CSSValue> consumeGridTemplatesRowsOrColumns(CSSParserTokenRange& r |
| return consumeGridTrackList(range, cssParserMode); |
| } |
| +static Vector<String> consumeGridTemplateAreasColumnNames(const String& gridRowNames) |
| +{ |
| + ASSERT(!gridRowNames.isEmpty()); |
| + Vector<String> columnNames; |
| + // Using StringImpl to avoid checks and indirection in every call to String::operator[]. |
| + StringImpl& text = *gridRowNames.impl(); |
| + |
| + StringBuilder areaName; |
| + for (unsigned i = 0; i < text.length(); ++i) { |
| + if (text[i] == ' ') { |
| + if (!areaName.isEmpty()) { |
| + columnNames.append(areaName.toString()); |
| + areaName.clear(); |
| + } |
| + continue; |
| + } |
| + if (text[i] == '.') { |
| + if (areaName == ".") |
| + continue; |
| + if (!areaName.isEmpty()) { |
| + columnNames.append(areaName.toString()); |
| + areaName.clear(); |
| + } |
| + } else { |
| + if (areaName == ".") { |
| + columnNames.append(areaName.toString()); |
| + areaName.clear(); |
| + } |
| + } |
| + |
| + areaName.append(text[i]); |
| + } |
| + |
| + if (!areaName.isEmpty()) |
| + columnNames.append(areaName.toString()); |
| + |
| + return columnNames; |
| +} |
| + |
| +static bool consumeGridTemplateAreasRow(const String& gridRowNames, NamedGridAreaMap& gridAreaMap, const size_t rowCount, size_t& columnCount) |
| +{ |
| + if (gridRowNames.isEmpty() || gridRowNames.containsOnlyWhitespace()) |
| + return false; |
| + |
| + Vector<String> columnNames = consumeGridTemplateAreasColumnNames(gridRowNames); |
| + if (!columnCount) { |
| + columnCount = columnNames.size(); |
| + ASSERT(columnCount); |
| + } else if (columnCount != columnNames.size()) { |
| + // The declaration is invalid is all the rows don't have the number of columns. |
| + return false; |
| + } |
| + |
| + for (size_t currentCol = 0; currentCol < columnCount; ++currentCol) { |
| + const String& gridAreaName = columnNames[currentCol]; |
| + |
| + // Unamed areas are always valid (we consider them to be 1x1). |
| + if (gridAreaName == ".") |
| + continue; |
| + |
| + // We handle several grid areas with the same name at once to simplify the validation code. |
| + size_t lookAheadCol; |
| + for (lookAheadCol = currentCol + 1; lookAheadCol < columnCount; ++lookAheadCol) { |
| + if (columnNames[lookAheadCol] != gridAreaName) |
| + break; |
| + } |
| + |
| + NamedGridAreaMap::iterator gridAreaIt = gridAreaMap.find(gridAreaName); |
| + if (gridAreaIt == gridAreaMap.end()) { |
| + gridAreaMap.add(gridAreaName, GridArea(GridSpan::translatedDefiniteGridSpan(rowCount, rowCount + 1), GridSpan::translatedDefiniteGridSpan(currentCol, lookAheadCol))); |
| + } else { |
| + GridArea& gridArea = gridAreaIt->value; |
| + |
| + // The following checks test that the grid area is a single filled-in rectangle. |
| + // 1. The new row is adjacent to the previously parsed row. |
| + if (rowCount != gridArea.rows.endLine()) |
| + return false; |
| + |
| + // 2. The new area starts at the same position as the previously parsed area. |
| + if (currentCol != gridArea.columns.startLine()) |
| + return false; |
| + |
| + // 3. The new area ends at the same position as the previously parsed area. |
| + if (lookAheadCol != gridArea.columns.endLine()) |
| + return false; |
| + |
| + gridArea.rows = GridSpan::translatedDefiniteGridSpan(gridArea.rows.startLine(), gridArea.rows.endLine() + 1); |
| + } |
| + currentCol = lookAheadCol - 1; |
| + } |
| + |
| + return true; |
| +} |
| + |
| static RawPtr<CSSValue> consumeGridTemplateAreas(CSSParserTokenRange& range) |
| { |
| if (range.peek().id() == CSSValueNone) |
| @@ -3228,7 +3346,7 @@ static RawPtr<CSSValue> consumeGridTemplateAreas(CSSParserTokenRange& range) |
| size_t columnCount = 0; |
| while (range.peek().type() == StringToken) { |
| - if (!parseGridTemplateAreasRow(range.consumeIncludingWhitespace().value(), gridAreaMap, rowCount, columnCount)) |
| + if (!consumeGridTemplateAreasRow(range.consumeIncludingWhitespace().value(), gridAreaMap, rowCount, columnCount)) |
| return nullptr; |
| ++rowCount; |
| } |
| @@ -4438,6 +4556,95 @@ bool CSSPropertyParser::consumeGridAreaShorthand(bool important) |
| return true; |
| } |
| +bool CSSPropertyParser::consumeGridTemplateRowsAndAreasAndColumns(bool important) |
| +{ |
| + NamedGridAreaMap gridAreaMap; |
| + size_t rowCount = 0; |
| + size_t columnCount = 0; |
| + RawPtr<CSSValueList> templateRows = CSSValueList::createSpaceSeparated(); |
| + |
| + // Persists between loop iterations so we can use the same value for |
| + // consecutive <line-names> values |
| + CSSGridLineNamesValue* lineNames = nullptr; |
| + |
| + do { |
| + // Handle leading <custom-ident>*. |
| + bool hasPreviousLineNames = lineNames; |
| + lineNames = consumeGridLineNames(m_range, lineNames); |
| + if (lineNames && !hasPreviousLineNames) |
| + templateRows->append(lineNames); |
| + |
| + // Handle a template-area's row. |
| + if (m_range.peek().type() != StringToken || !consumeGridTemplateAreasRow(m_range.consumeIncludingWhitespace().value(), gridAreaMap, rowCount, columnCount)) |
| + return false; |
| + ++rowCount; |
| + |
| + // Handle template-rows's track-size. |
| + RawPtr<CSSValue> value = consumeGridTrackSize(m_range, m_context.mode()); |
| + if (!value) |
| + value = cssValuePool().createIdentifierValue(CSSValueAuto); |
| + templateRows->append(value.release()); |
| + |
| + // This will handle the trailing/leading <custom-ident>* in the grammar. |
| + lineNames = consumeGridLineNames(m_range); |
| + if (lineNames) |
| + templateRows->append(lineNames); |
| + } while (!m_range.atEnd() && !(m_range.peek().type() == DelimiterToken && m_range.peek().delimiter() == '/')); |
| + |
| + RawPtr<CSSValue> columnsValue = nullptr; |
| + if (!m_range.atEnd()) { |
| + if (!consumeSlashIncludingWhitespace(m_range)) |
| + return false; |
| + columnsValue = consumeGridTrackList(m_range, m_context.mode()); |
| + if (!columnsValue || !m_range.atEnd()) |
| + return false; |
| + } else { |
| + columnsValue = cssValuePool().createIdentifierValue(CSSValueNone); |
| + } |
| + addProperty(CSSPropertyGridTemplateRows, templateRows.release(), important); |
| + addProperty(CSSPropertyGridTemplateColumns, columnsValue.release(), important); |
| + addProperty(CSSPropertyGridTemplateAreas, CSSGridTemplateAreasValue::create(gridAreaMap, rowCount, columnCount), important); |
| + return true; |
| +} |
| + |
| +bool CSSPropertyParser::consumeGridTemplateShorthand(bool important) |
| +{ |
| + ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled()); |
| + ASSERT(gridTemplateShorthand().length() == 3); |
| + |
| + CSSParserTokenRange rangeCopy = m_range; |
| + RawPtr<CSSValue> rowsValue = consumeIdent<CSSValueNone>(m_range); |
| + |
| + // 1- 'none' case. |
| + if (rowsValue && m_range.atEnd()) { |
| + addProperty(CSSPropertyGridTemplateRows, cssValuePool().createIdentifierValue(CSSValueNone), important); |
| + addProperty(CSSPropertyGridTemplateColumns, cssValuePool().createIdentifierValue(CSSValueNone), important); |
| + addProperty(CSSPropertyGridTemplateAreas, cssValuePool().createIdentifierValue(CSSValueNone), important); |
| + return true; |
| + } |
| + |
| + // 2- <grid-template-rows> / <grid-template-columns> |
| + if (!rowsValue) |
| + rowsValue = consumeGridTrackList(m_range, m_context.mode()); |
| + |
| + if (rowsValue) { |
| + if (!consumeSlashIncludingWhitespace(m_range)) |
| + return false; |
| + RawPtr<CSSValue> columnsValue = consumeGridTemplatesRowsOrColumns(m_range, m_context.mode()); |
| + if (!columnsValue || !m_range.atEnd()) |
| + return false; |
| + |
| + addProperty(CSSPropertyGridTemplateRows, rowsValue.release(), important); |
| + addProperty(CSSPropertyGridTemplateColumns, columnsValue.release(), important); |
| + addProperty(CSSPropertyGridTemplateAreas, cssValuePool().createIdentifierValue(CSSValueNone), important); |
| + return true; |
| + } |
| + |
| + // 3- [<line-names>? <string> <track-size>? <line-names>? ]+ syntax. |
|
Timothy Loh
2016/04/07 06:48:22
might as well just write it out completely
[ <lin
rwlbuis
2016/04/07 19:51:22
Done.
|
| + m_range = rangeCopy; |
| + return consumeGridTemplateRowsAndAreasAndColumns(important); |
| +} |
| + |
| bool CSSPropertyParser::parseShorthand(CSSPropertyID unresolvedProperty, bool important) |
| { |
| CSSPropertyID property = resolveCSSPropertyID(unresolvedProperty); |
| @@ -4620,6 +4827,8 @@ bool CSSPropertyParser::parseShorthand(CSSPropertyID unresolvedProperty, bool im |
| return consumeGridItemPositionShorthand(property, important); |
| case CSSPropertyGridArea: |
| return consumeGridAreaShorthand(important); |
| + case CSSPropertyGridTemplate: |
| + return consumeGridTemplateShorthand(important); |
| default: |
| m_currentShorthand = oldShorthand; |
| CSSParserValueList valueList(m_range); |