[CSS Parser] Clean up new parser's grid layout ifdefs/runtime checking
[WebKit-https.git] / Source / WebCore / css / parser / CSSPropertyParser.cpp
index 9ec1c36..e0c5aa2 100644 (file)
@@ -364,7 +364,11 @@ bool CSSPropertyParser::consumeTransformOrigin(bool important)
     RefPtr<CSSPrimitiveValue> resultX;
     RefPtr<CSSPrimitiveValue> resultY;
     if (consumeOneOrTwoValuedPosition(m_range, m_context.mode, UnitlessQuirk::Forbid, resultX, resultY)) {
+        m_range.consumeWhitespace();
+        bool atEnd = m_range.atEnd();
         RefPtr<CSSPrimitiveValue> resultZ = consumeLength(m_range, m_context.mode, ValueRangeAll);
+        if (!resultZ && !atEnd)
+            return false;
         if (!resultZ)
             resultZ = CSSValuePool::singleton().createValue(0, CSSPrimitiveValue::UnitTypes::CSS_PX);
         addProperty(CSSPropertyTransformOriginX, CSSPropertyTransformOrigin, resultX.releaseNonNull(), important);
@@ -1099,7 +1103,8 @@ static RefPtr<CSSValue> consumeAnimationName(CSSParserTokenRange& range)
         const CSSParserToken& token = range.consumeIncludingWhitespace();
         if (equalIgnoringASCIICase(token.value(), "none"))
             return CSSValuePool::singleton().createIdentifierValue(CSSValueNone);
-        return CSSCustomIdentValue::create(token.value().toString());
+        // FIXME-NEWPARSER: Want to use a CSSCustomIdentValue here eventually.
+        return CSSValuePool::singleton().createValue(token.value().toString(), CSSPrimitiveValue::UnitTypes::CSS_STRING);
     }
 
     return consumeCustomIdent(range);
@@ -1115,7 +1120,9 @@ static RefPtr<CSSValue> consumeTransitionProperty(CSSParserTokenRange& range)
 
     if (CSSPropertyID property = token.parseAsCSSPropertyID()) {
         range.consumeIncludingWhitespace();
-        return CSSCustomIdentValue::create(property);
+        
+        // FIXME-NEWPARSER: Want to use a CSSCustomIdentValue here eventually.
+        return CSSValuePool::singleton().createIdentifierValue(property);
     }
     return consumeCustomIdent(range);
 }
@@ -1368,7 +1375,7 @@ static RefPtr<CSSFunctionValue> consumeFilterFunction(CSSParserTokenRange& range
     if (filterType < CSSValueInvert || filterType > CSSValueDropShadow)
         return nullptr;
     CSSParserTokenRange args = consumeFunction(range);
-    RefPtr<CSSFunctionValue>filterValue = CSSFunctionValue::create(filterType);
+    RefPtr<CSSFunctionValue> filterValue = CSSFunctionValue::create(filterType);
     RefPtr<CSSValue> parsedValue;
 
     if (filterType == CSSValueDropShadow)
@@ -1712,7 +1719,7 @@ static RefPtr<CSSValue> consumeTransform(CSSParserTokenRange& range, CSSParserMo
 }
 
 template <CSSValueID start, CSSValueID end>
-static RefPtr<CSSValue> consumePositionLonghand(CSSParserTokenRange& range, CSSParserMode cssParserMode)
+static RefPtr<CSSPrimitiveValue> consumePositionLonghand(CSSParserTokenRange& range, CSSParserMode cssParserMode)
 {
     if (range.peek().type() == IdentToken) {
         CSSValueID id = range.peek().id();
@@ -1731,12 +1738,12 @@ static RefPtr<CSSValue> consumePositionLonghand(CSSParserTokenRange& range, CSSP
     return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll);
 }
 
-static RefPtr<CSSValue> consumePositionX(CSSParserTokenRange& range, CSSParserMode cssParserMode)
+static RefPtr<CSSPrimitiveValue> consumePositionX(CSSParserTokenRange& range, CSSParserMode cssParserMode)
 {
     return consumePositionLonghand<CSSValueLeft, CSSValueRight>(range, cssParserMode);
 }
 
-static RefPtr<CSSValue> consumePositionY(CSSParserTokenRange& range, CSSParserMode cssParserMode)
+static RefPtr<CSSPrimitiveValue> consumePositionY(CSSParserTokenRange& range, CSSParserMode cssParserMode)
 {
     return consumePositionLonghand<CSSValueTop, CSSValueBottom>(range, cssParserMode);
 }
@@ -1914,9 +1921,10 @@ static RefPtr<CSSValue> consumeAttr(CSSParserTokenRange args, CSSParserContext c
     if (!args.atEnd())
         return nullptr;
 
-    RefPtr<CSSFunctionValue> attrValue = CSSFunctionValue::create(CSSValueAttr);
-    attrValue->append(CSSCustomIdentValue::create(attrName));
-    return attrValue;
+    // FIXME-NEWPARSER: We want to use a CSSCustomIdentValue here eventually for the attrName.
+    // FIXME-NEWPARSER: We want to use a CSSFunctionValue rather than relying on a custom
+    // attr() primitive value.
+    return CSSValuePool::singleton().createValue(attrName, CSSPrimitiveValue::CSS_ATTR);
 }
 
 static RefPtr<CSSValue> consumeCounterContent(CSSParserTokenRange args, bool counters)
@@ -1998,15 +2006,20 @@ static RefPtr<CSSPrimitiveValue> consumePerspective(CSSParserTokenRange& range,
 
 #if ENABLE(CSS_SCROLL_SNAP)
 
-static RefPtr<CSSValueList> consumePositionList(CSSParserTokenRange& range, CSSParserMode cssParserMode)
+static RefPtr<CSSValueList> consumeSnapPointCoordinateList(CSSParserTokenRange& range, CSSParserMode cssParserMode)
 {
-    RefPtr<CSSValueList> positions = CSSValueList::createCommaSeparated();
+    RefPtr<CSSValueList> positions = CSSValueList::createSpaceSeparated();
     do {
-        RefPtr<CSSValue> position = consumePosition(range, cssParserMode, UnitlessQuirk::Forbid);
-        if (!position)
+        
+        RefPtr<CSSPrimitiveValue> first = consumePositionX(range, cssParserMode);
+        if (!first || range.atEnd())
+            return nullptr;
+        RefPtr<CSSPrimitiveValue> second = consumePositionY(range, cssParserMode);
+        if (!second)
             return nullptr;
+        RefPtr<CSSValue> position = createPrimitiveValuePair(first.releaseNonNull(), second.releaseNonNull(), Pair::IdenticalValueEncoding::DoNotCoalesce);
         positions->append(position.releaseNonNull());
-    } while (consumeCommaIncludingWhitespace(range));
+    } while (!range.atEnd());
     return positions;
 }
 
@@ -2014,23 +2027,45 @@ static RefPtr<CSSValue> consumeScrollSnapCoordinate(CSSParserTokenRange& range,
 {
     if (range.peek().id() == CSSValueNone)
         return consumeIdent(range);
-    return consumePositionList(range, cssParserMode);
+    return consumeSnapPointCoordinateList(range, cssParserMode);
+}
+
+static RefPtr<CSSValue> consumeScrollSnapDestination(CSSParserTokenRange& range, CSSParserMode cssParserMode)
+{
+    RefPtr<CSSPrimitiveValue> first = consumePositionX(range, cssParserMode);
+    if (!first || range.atEnd())
+        return nullptr;
+    RefPtr<CSSPrimitiveValue> second = consumePositionY(range, cssParserMode);
+    if (!second)
+        return nullptr;
+    return createPrimitiveValuePair(first.releaseNonNull(), second.releaseNonNull(), Pair::IdenticalValueEncoding::DoNotCoalesce);
 }
 
 static RefPtr<CSSValue> consumeScrollSnapPoints(CSSParserTokenRange& range, CSSParserMode cssParserMode)
 {
-    if (range.peek().id() == CSSValueNone)
+    if (range.peek().id() == CSSValueNone || range.peek().id() == CSSValueElements)
         return consumeIdent(range);
-    if (range.peek().functionId() == CSSValueRepeat) {
-        CSSParserTokenRange args = consumeFunction(range);
-        RefPtr<CSSPrimitiveValue> parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative);
-        if (args.atEnd() && parsedValue && (parsedValue->isCalculated() || parsedValue->doubleValue() > 0)) {
-            RefPtr<CSSFunctionValue> result = CSSFunctionValue::create(CSSValueRepeat);
-            result->append(parsedValue.releaseNonNull());
-            return result;
+    
+    RefPtr<CSSValueList> points = CSSValueList::createSpaceSeparated();
+    do {
+        if (range.peek().functionId() == CSSValueRepeat) {
+            CSSParserTokenRange args = consumeFunction(range);
+            RefPtr<CSSPrimitiveValue> parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative);
+            if (args.atEnd() && parsedValue && (parsedValue->isCalculated() || parsedValue->doubleValue() > 0)) {
+                Ref<CSSFunctionValue> result = CSSFunctionValue::create(CSSValueRepeat);
+                result->append(parsedValue.releaseNonNull());
+                points->append(WTFMove(result));
+            }
+        } else {
+            RefPtr<CSSValue> length = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Forbid);
+            if (!length)
+                return nullptr;
+            points->append(length.releaseNonNull());
         }
-    }
-    return nullptr;
+    } while (!range.atEnd());
+    
+    
+    return points;
 }
 
 #endif
@@ -2558,16 +2593,18 @@ static RefPtr<CSSPrimitiveValue> consumeBackgroundSize(CSSPropertyID property, C
     if (identMatches<CSSValueContain, CSSValueCover>(range.peek().id()))
         return consumeIdent(range);
 
+    // FIXME: We're allowing the unitless quirk on this property because our
+    // tests assume that. Other browser engines don't allow it though.
     RefPtr<CSSPrimitiveValue> horizontal = consumeIdent<CSSValueAuto>(range);
     if (!horizontal)
-        horizontal = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Forbid);
+        horizontal = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
 
     RefPtr<CSSPrimitiveValue> vertical;
     if (!range.atEnd()) {
         if (range.peek().id() == CSSValueAuto) // `auto' is the default
             range.consumeIncludingWhitespace();
         else
-            vertical = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Forbid);
+            vertical = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
     } else if (!vertical && property == CSSPropertyWebkitBackgroundSize) {
         // Legacy syntax: "-webkit-background-size: 10px" is equivalent to "background-size: 10px 10px".
         vertical = horizontal;
@@ -2661,6 +2698,7 @@ static RefPtr<CSSValue> consumeCommaSeparatedBackgroundComponent(CSSPropertyID p
     return result;
 }
 
+#if ENABLE(CSS_GRID_LAYOUT)
 static RefPtr<CSSPrimitiveValue> consumeSelfPositionKeyword(CSSParserTokenRange& range)
 {
     CSSValueID id = range.peek().id();
@@ -2986,7 +3024,7 @@ static bool consumeGridTrackRepeatFunction(CSSParserTokenRange& range, CSSParser
             return false;
         if (allTracksAreFixedSized)
             allTracksAreFixedSized = isGridTrackFixedSized(*trackSize);
-        repeatedValues->append(*trackSize);
+        repeatedValues->append(trackSize.releaseNonNull());
         ++numberOfTracks;
         lineNames = consumeGridLineNames(args);
         if (lineNames)
@@ -3003,7 +3041,7 @@ static bool consumeGridTrackRepeatFunction(CSSParserTokenRange& range, CSSParser
         repetitions = std::min(repetitions, kGridMaxTracks / numberOfTracks);
         for (size_t i = 0; i < repetitions; ++i) {
             for (size_t j = 0; j < repeatedValues->length(); ++j)
-                list.append(adoptRef(*repeatedValues->item(j)));
+                list.append(adoptRef(*repeatedValues->itemWithoutBoundsCheck(j)));
         }
     }
     return true;
@@ -3048,7 +3086,7 @@ static RefPtr<CSSValue> consumeGridTrackList(CSSParserTokenRange& range, CSSPars
         if (lineNames) {
             if (!allowGridLineNames)
                 return nullptr;
-            values->append(*lineNames);
+            values->append(lineNames.releaseNonNull());
         }
     } while (!range.atEnd() && range.peek().type() != DelimiterToken);
     return values;
@@ -3081,6 +3119,7 @@ static RefPtr<CSSValue> consumeGridTemplateAreas(CSSParserTokenRange& range)
     ASSERT(columnCount);
     return CSSGridTemplateAreasValue::create(gridAreaMap, rowCount, columnCount);
 }
+#endif
 
 #if ENABLE(CSS_REGIONS)
 static RefPtr<CSSValue> consumeFlowProperty(CSSParserTokenRange& range)
@@ -3262,6 +3301,53 @@ static RefPtr<CSSValue> consumeWebkitAspectRatio(CSSParserTokenRange& range)
     return CSSAspectRatioValue::create(leftValue->floatValue(), rightValue->floatValue());
 }
 
+static RefPtr<CSSValue> consumeTextEmphasisPosition(CSSParserTokenRange& range)
+{
+    bool foundOverOrUnder = false;
+    CSSValueID overUnderValueID = CSSValueOver;
+    bool foundLeftOrRight = false;
+    CSSValueID leftRightValueID = CSSValueRight;
+    while (!range.atEnd()) {
+        switch (range.peek().id()) {
+        case CSSValueOver:
+            if (foundOverOrUnder)
+                return nullptr;
+            foundOverOrUnder = true;
+            overUnderValueID = CSSValueOver;
+            break;
+        case CSSValueUnder:
+            if (foundOverOrUnder)
+                return nullptr;
+            foundOverOrUnder = true;
+            overUnderValueID = CSSValueUnder;
+            break;
+        case CSSValueLeft:
+            if (foundLeftOrRight)
+                return nullptr;
+            foundLeftOrRight = true;
+            leftRightValueID = CSSValueLeft;
+            break;
+        case CSSValueRight:
+            if (foundLeftOrRight)
+                return nullptr;
+            foundLeftOrRight = true;
+            leftRightValueID = CSSValueRight;
+            break;
+        default:
+            return nullptr;
+        }
+        
+        range.consumeIncludingWhitespace();
+    }
+    if (!foundOverOrUnder)
+        return nullptr;
+    RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
+    list->append(CSSValuePool::singleton().createIdentifierValue(overUnderValueID));
+    if (foundLeftOrRight)
+        list->append(CSSValuePool::singleton().createIdentifierValue(leftRightValueID));
+    return list;
+}
+    
 RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSSPropertyID currentShorthand)
 {
     if (CSSParserFastPaths::isKeywordPropertyID(property)) {
@@ -3461,7 +3547,8 @@ RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSS
         return consumeZIndex(m_range);
     case CSSPropertyTextShadow: // CSS2 property, dropped in CSS2.1, back in CSS3, so treat as CSS3
     case CSSPropertyBoxShadow:
-        return consumeShadow(m_range, m_context.mode, property == CSSPropertyBoxShadow);
+    case CSSPropertyWebkitBoxShadow:
+        return consumeShadow(m_range, m_context.mode, property == CSSPropertyBoxShadow || property == CSSPropertyWebkitBoxShadow);
     case CSSPropertyFilter:
 #if ENABLE(FILTERS_LEVEL_2)
     case CSSPropertyWebkitBackdropFilter:
@@ -3544,6 +3631,8 @@ RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSS
 #if ENABLE(CSS_SCROLL_SNAP)
     case CSSPropertyWebkitScrollSnapCoordinate:
         return consumeScrollSnapCoordinate(m_range, m_context.mode);
+    case CSSPropertyWebkitScrollSnapDestination:
+        return consumeScrollSnapDestination(m_range, m_context.mode);
     case CSSPropertyWebkitScrollSnapPointsX:
     case CSSPropertyWebkitScrollSnapPointsY:
         return consumeScrollSnapPoints(m_range, m_context.mode);
@@ -3603,6 +3692,7 @@ RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSS
     case CSSPropertyWebkitBackgroundClip:
     case CSSPropertyWebkitBackgroundOrigin:
     case CSSPropertyWebkitBackgroundComposite:
+    case CSSPropertyWebkitBackgroundSize:
     case CSSPropertyWebkitMaskClip:
     case CSSPropertyWebkitMaskComposite:
     case CSSPropertyWebkitMaskImage:
@@ -3614,28 +3704,46 @@ RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSS
     case CSSPropertyWebkitMaskRepeatX:
     case CSSPropertyWebkitMaskRepeatY:
         return nullptr;
+#if ENABLE(CSS_GRID_LAYOUT)
     case CSSPropertyAlignItems:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeAlignItems(m_range);
     case CSSPropertyJustifySelf:
     case CSSPropertyAlignSelf:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeSelfPositionOverflowPosition(m_range);
     case CSSPropertyJustifyItems:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeJustifyItems(m_range);
     case CSSPropertyGridColumnEnd:
     case CSSPropertyGridColumnStart:
     case CSSPropertyGridRowEnd:
     case CSSPropertyGridRowStart:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeGridLine(m_range);
     case CSSPropertyGridAutoColumns:
     case CSSPropertyGridAutoRows:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeGridTrackList(m_range, m_context.mode, GridAuto);
     case CSSPropertyGridTemplateColumns:
     case CSSPropertyGridTemplateRows:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeGridTemplatesRowsOrColumns(m_range, m_context.mode);
     case CSSPropertyGridTemplateAreas:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeGridTemplateAreas(m_range);
     case CSSPropertyGridAutoFlow:
+        if (!m_context.cssGridLayoutEnabled)
+            return nullptr;
         return consumeGridAutoFlow(m_range);
+#endif
 #if ENABLE(CSS_REGIONS)
     case CSSPropertyWebkitFlowInto:
     case CSSPropertyWebkitFlowFrom:
@@ -3657,6 +3765,8 @@ RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, CSS
         return consumeAlt(m_range, m_context);
     case CSSPropertyWebkitAspectRatio:
         return consumeWebkitAspectRatio(m_range);
+    case CSSPropertyWebkitTextEmphasisPosition:
+        return consumeTextEmphasisPosition(m_range);
     default:
         return nullptr;
     }