Registered custom properties should support syntax parameter for <length> and *
[WebKit-https.git] / Source / WebCore / css / parser / CSSPropertyParser.cpp
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Copyright (C) 2016 Apple Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //    * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //    * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //    * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "config.h"
31 #include "CSSPropertyParser.h"
32
33 #include "CSSAspectRatioValue.h"
34 #include "CSSBasicShapes.h"
35 #include "CSSBorderImage.h"
36 #include "CSSBorderImageSliceValue.h"
37 #include "CSSContentDistributionValue.h"
38 #include "CSSCursorImageValue.h"
39 #include "CSSCustomIdentValue.h"
40 #include "CSSCustomPropertyValue.h"
41 #include "CSSFontFaceSrcValue.h"
42 #include "CSSFontFeatureValue.h"
43 #if ENABLE(VARIATION_FONTS)
44 #include "CSSFontVariationValue.h"
45 #endif
46 #include "CSSFontStyleRangeValue.h"
47 #include "CSSFontStyleValue.h"
48 #include "CSSFunctionValue.h"
49 #include "CSSGridAutoRepeatValue.h"
50 #include "CSSGridLineNamesValue.h"
51 #include "CSSGridTemplateAreasValue.h"
52 #include "CSSLineBoxContainValue.h"
53 #include "CSSParserFastPaths.h"
54 #include "CSSParserIdioms.h"
55 #include "CSSPendingSubstitutionValue.h"
56 #include "CSSPrimitiveValueMappings.h"
57 #include "CSSPropertyParserHelpers.h"
58 #include "CSSReflectValue.h"
59 #include "CSSShadowValue.h"
60 #include "CSSTimingFunctionValue.h"
61 #include "CSSUnicodeRangeValue.h"
62 #include "CSSVariableParser.h"
63 #include "CSSVariableReferenceValue.h"
64 #include "Counter.h"
65 #if ENABLE(DASHBOARD_SUPPORT)
66 #include "DashboardRegion.h"
67 #endif
68 #include "FontFace.h"
69 #include "HashTools.h"
70 // FIXME-NEWPARSER: Replace Pair and Rect with actual CSSValue subclasses (CSSValuePair and CSSQuadValue).
71 #include "Pair.h"
72 #include "Rect.h"
73 #include "RenderTheme.h"
74 #include "RuntimeEnabledFeatures.h"
75 #include "SVGPathByteStream.h"
76 #include "SVGPathUtilities.h"
77 #include "StyleBuilderConverter.h"
78 #include "StylePropertyShorthand.h"
79 #include "StylePropertyShorthandFunctions.h"
80 #include "StyleResolver.h"
81 #include <bitset>
82 #include <memory>
83 #include <wtf/text/StringBuilder.h>
84
85
86 namespace WebCore {
87 using namespace WTF;
88
89     
90 bool isCustomPropertyName(const String& propertyName)
91 {
92     return propertyName.length() > 2 && propertyName.characterAt(0) == '-' && propertyName.characterAt(1) == '-';
93 }
94
95 static bool hasPrefix(const char* string, unsigned length, const char* prefix)
96 {
97     for (unsigned i = 0; i < length; ++i) {
98         if (!prefix[i])
99             return true;
100         if (string[i] != prefix[i])
101             return false;
102     }
103     return false;
104 }
105
106 #if PLATFORM(IOS_FAMILY)
107 void cssPropertyNameIOSAliasing(const char* propertyName, const char*& propertyNameAlias, unsigned& newLength)
108 {
109     if (!strcmp(propertyName, "-webkit-hyphenate-locale")) {
110         // Worked in iOS 4.2.
111         static const char webkitLocale[] = "-webkit-locale";
112         propertyNameAlias = webkitLocale;
113         newLength = strlen(webkitLocale);
114     }
115 }
116 #endif
117
118 template <typename CharacterType>
119 static CSSPropertyID cssPropertyID(const CharacterType* propertyName, unsigned length)
120 {
121     char buffer[maxCSSPropertyNameLength + 1 + 1]; // 1 to turn "apple"/"khtml" into "webkit", 1 for null character
122     
123     for (unsigned i = 0; i != length; ++i) {
124         CharacterType c = propertyName[i];
125         if (!c || c >= 0x7F)
126             return CSSPropertyInvalid; // illegal character
127         buffer[i] = toASCIILower(c);
128     }
129     buffer[length] = '\0';
130     
131     const char* name = buffer;
132     if (buffer[0] == '-') {
133 #if ENABLE(LEGACY_CSS_VENDOR_PREFIXES)
134         // If the prefix is -apple- or -khtml-, change it to -webkit-.
135         // This makes the string one character longer.
136         if (RuntimeEnabledFeatures::sharedFeatures().legacyCSSVendorPrefixesEnabled()
137             && (hasPrefix(buffer, length, "-apple-") || hasPrefix(buffer, length, "-khtml-"))) {
138             memmove(buffer + 7, buffer + 6, length + 1 - 6);
139             memcpy(buffer, "-webkit", 7);
140             ++length;
141         }
142 #endif
143 #if PLATFORM(IOS_FAMILY)
144         cssPropertyNameIOSAliasing(buffer, name, length);
145 #endif
146     }
147     
148     const Property* hashTableEntry = findProperty(name, length);
149     return hashTableEntry ? static_cast<CSSPropertyID>(hashTableEntry->id) : CSSPropertyInvalid;
150 }
151
152 static bool isAppleLegacyCssValueKeyword(const char* valueKeyword, unsigned length)
153 {
154     static const char applePrefix[] = "-apple-";
155     static const char appleSystemPrefix[] = "-apple-system";
156     static const char applePayPrefix[] = "-apple-pay";
157     static const char* appleWirelessPlaybackTargetActive = getValueName(CSSValueAppleWirelessPlaybackTargetActive);
158     
159     return hasPrefix(valueKeyword, length, applePrefix)
160     && !hasPrefix(valueKeyword, length, appleSystemPrefix)
161     && !hasPrefix(valueKeyword, length, applePayPrefix)
162     && !WTF::equal(reinterpret_cast<const LChar*>(valueKeyword), reinterpret_cast<const LChar*>(appleWirelessPlaybackTargetActive), length);
163 }
164
165 template <typename CharacterType>
166 static CSSValueID cssValueKeywordID(const CharacterType* valueKeyword, unsigned length)
167 {
168     char buffer[maxCSSValueKeywordLength + 1 + 1]; // 1 to turn "apple"/"khtml" into "webkit", 1 for null character
169     
170     for (unsigned i = 0; i != length; ++i) {
171         CharacterType c = valueKeyword[i];
172         if (!c || c >= 0x7F)
173             return CSSValueInvalid; // illegal keyword.
174         buffer[i] = WTF::toASCIILower(c);
175     }
176     buffer[length] = '\0';
177     
178     if (buffer[0] == '-') {
179         // If the prefix is -apple- or -khtml-, change it to -webkit-.
180         // This makes the string one character longer.
181         // On iOS we don't want to change values starting with -apple-system to -webkit-system.
182         // FIXME: Remove this mangling without breaking the web.
183         if (isAppleLegacyCssValueKeyword(buffer, length) || hasPrefix(buffer, length, "-khtml-")) {
184             memmove(buffer + 7, buffer + 6, length + 1 - 6);
185             memcpy(buffer, "-webkit", 7);
186             ++length;
187         }
188     }
189     
190     const Value* hashTableEntry = findValue(buffer, length);
191     return hashTableEntry ? static_cast<CSSValueID>(hashTableEntry->id) : CSSValueInvalid;
192 }
193
194 CSSValueID cssValueKeywordID(StringView string)
195 {
196     unsigned length = string.length();
197     if (!length)
198         return CSSValueInvalid;
199     if (length > maxCSSValueKeywordLength)
200         return CSSValueInvalid;
201     
202     return string.is8Bit() ? cssValueKeywordID(string.characters8(), length) : cssValueKeywordID(string.characters16(), length);
203 }
204
205 CSSPropertyID cssPropertyID(StringView string)
206 {
207     unsigned length = string.length();
208     
209     if (!length)
210         return CSSPropertyInvalid;
211     if (length > maxCSSPropertyNameLength)
212         return CSSPropertyInvalid;
213     
214     return string.is8Bit() ? cssPropertyID(string.characters8(), length) : cssPropertyID(string.characters16(), length);
215 }
216     
217 using namespace CSSPropertyParserHelpers;
218
219 CSSPropertyParser::CSSPropertyParser(const CSSParserTokenRange& range, const CSSParserContext& context, Vector<CSSProperty, 256>* parsedProperties, bool consumeWhitespace)
220     : m_range(range)
221     , m_context(context)
222     , m_parsedProperties(parsedProperties)
223 {
224     if (consumeWhitespace)
225         m_range.consumeWhitespace();
226 }
227
228 void CSSPropertyParser::addProperty(CSSPropertyID property, CSSPropertyID currentShorthand, Ref<CSSValue>&& value, bool important, bool implicit)
229 {
230     int shorthandIndex = 0;
231     bool setFromShorthand = false;
232
233     if (currentShorthand) {
234         auto shorthands = matchingShorthandsForLonghand(property);
235         setFromShorthand = true;
236         if (shorthands.size() > 1)
237             shorthandIndex = indexOfShorthandForLonghand(currentShorthand, shorthands);
238     }
239
240     m_parsedProperties->append(CSSProperty(property, WTFMove(value), important, setFromShorthand, shorthandIndex, implicit));
241 }
242
243 void CSSPropertyParser::addExpandedPropertyForValue(CSSPropertyID property, Ref<CSSValue>&& value, bool important)
244 {
245     const StylePropertyShorthand& shorthand = shorthandForProperty(property);
246     unsigned shorthandLength = shorthand.length();
247     ASSERT(shorthandLength);
248     const CSSPropertyID* longhands = shorthand.properties();
249     for (unsigned i = 0; i < shorthandLength; ++i)
250         addProperty(longhands[i], property, value.copyRef(), important);
251 }
252
253 bool CSSPropertyParser::parseValue(CSSPropertyID propertyID, bool important, const CSSParserTokenRange& range, const CSSParserContext& context, ParsedPropertyVector& parsedProperties, StyleRule::Type ruleType)
254 {
255     int parsedPropertiesSize = parsedProperties.size();
256
257     CSSPropertyParser parser(range, context, &parsedProperties);
258     bool parseSuccess;
259
260 #if ENABLE(CSS_DEVICE_ADAPTATION)
261     if (ruleType == StyleRule::Viewport)
262         parseSuccess = parser.parseViewportDescriptor(propertyID, important);
263     else
264 #endif
265     if (ruleType == StyleRule::FontFace)
266         parseSuccess = parser.parseFontFaceDescriptor(propertyID);
267     else
268         parseSuccess = parser.parseValueStart(propertyID, important);
269
270     if (!parseSuccess)
271         parsedProperties.shrink(parsedPropertiesSize);
272
273     return parseSuccess;
274 }
275
276 RefPtr<CSSValue> CSSPropertyParser::parseSingleValue(CSSPropertyID property, const CSSParserTokenRange& range, const CSSParserContext& context)
277 {
278     CSSPropertyParser parser(range, context, nullptr);
279     RefPtr<CSSValue> value = parser.parseSingleValue(property);
280     if (!value || !parser.m_range.atEnd())
281         return nullptr;
282     return value;
283 }
284
285 bool CSSPropertyParser::canParseTypedCustomPropertyValue(const String& syntax, const CSSParserTokenRange& tokens, const CSSParserContext& context)
286 {
287     CSSPropertyParser parser(tokens, context, nullptr);
288     return parser.canParseTypedCustomPropertyValue(syntax);
289 }
290
291 RefPtr<CSSCustomPropertyValue> CSSPropertyParser::parseTypedCustomPropertyValue(const String& name, const String& syntax, const CSSParserTokenRange& tokens, const StyleResolver& styleResolver, const CSSParserContext& context)
292 {
293     CSSPropertyParser parser(tokens, context, nullptr, false);
294     RefPtr<CSSCustomPropertyValue> value = parser.parseTypedCustomPropertyValue(name, syntax, styleResolver);
295     if (!value || !parser.m_range.atEnd())
296         return nullptr;
297     return value;
298 }
299
300 void CSSPropertyParser::collectParsedCustomPropertyValueDependencies(const String& syntax, bool isRoot, HashSet<CSSPropertyID>& dependencies, const CSSParserTokenRange& tokens, const CSSParserContext& context)
301 {
302     CSSPropertyParser parser(tokens, context, nullptr);
303     parser.collectParsedCustomPropertyValueDependencies(syntax, isRoot, dependencies);
304 }
305
306 static bool isLegacyBreakProperty(CSSPropertyID propertyID)
307 {
308     switch (propertyID) {
309     case CSSPropertyPageBreakAfter:
310     case CSSPropertyPageBreakBefore:
311     case CSSPropertyPageBreakInside:
312     case CSSPropertyWebkitColumnBreakAfter:
313     case CSSPropertyWebkitColumnBreakBefore:
314     case CSSPropertyWebkitColumnBreakInside:
315         return true;
316     default:
317         break;
318     }
319     return false;
320 }
321
322 bool CSSPropertyParser::parseValueStart(CSSPropertyID propertyID, bool important)
323 {
324     if (consumeCSSWideKeyword(propertyID, important))
325         return true;
326
327     CSSParserTokenRange originalRange = m_range;
328     bool isShorthand = isShorthandCSSProperty(propertyID);
329
330     if (isShorthand) {
331         // Variable references will fail to parse here and will fall out to the variable ref parser below.
332         if (parseShorthand(propertyID, important))
333             return true;
334     } else if (isLegacyBreakProperty(propertyID)) {
335         // FIXME-NEWPARSER: Can turn this into a shorthand once old parser is gone, and then
336         // we don't need the special case.
337         if (consumeLegacyBreakProperty(propertyID, important))
338             return true;
339     } else {
340         RefPtr<CSSValue> parsedValue = parseSingleValue(propertyID);
341         if (parsedValue && m_range.atEnd()) {
342             addProperty(propertyID, CSSPropertyInvalid, *parsedValue, important);
343             return true;
344         }
345     }
346
347     if (CSSVariableParser::containsValidVariableReferences(originalRange, m_context)) {
348         RefPtr<CSSVariableReferenceValue> variable = CSSVariableReferenceValue::create(originalRange);
349
350         if (isShorthand) {
351             RefPtr<CSSPendingSubstitutionValue> pendingValue = CSSPendingSubstitutionValue::create(propertyID, variable.releaseNonNull());
352             addExpandedPropertyForValue(propertyID, pendingValue.releaseNonNull(), important);
353         } else
354             addProperty(propertyID, CSSPropertyInvalid, variable.releaseNonNull(), important);
355         return true;
356     }
357
358     return false;
359 }
360  
361 bool CSSPropertyParser::consumeCSSWideKeyword(CSSPropertyID propertyID, bool important)
362 {
363     CSSParserTokenRange rangeCopy = m_range;
364     CSSValueID valueID = rangeCopy.consumeIncludingWhitespace().id();
365     if (!rangeCopy.atEnd())
366         return false;
367
368     RefPtr<CSSValue> value;
369     if (valueID == CSSValueInherit)
370         value = CSSValuePool::singleton().createInheritedValue();
371     else if (valueID == CSSValueInitial)
372         value = CSSValuePool::singleton().createExplicitInitialValue();
373     else if (valueID == CSSValueUnset)
374         value = CSSValuePool::singleton().createUnsetValue();
375     else if (valueID == CSSValueRevert)
376         value = CSSValuePool::singleton().createRevertValue();
377     else
378         return false;
379     
380     const StylePropertyShorthand& shorthand = shorthandForProperty(propertyID);
381     if (!shorthand.length()) {
382         if (CSSProperty::isDescriptorOnly(propertyID))
383             return false;
384         addProperty(propertyID, CSSPropertyInvalid, value.releaseNonNull(), important);
385     } else
386         addExpandedPropertyForValue(propertyID, value.releaseNonNull(), important);
387     m_range = rangeCopy;
388     return true;
389 }
390
391 bool CSSPropertyParser::consumeTransformOrigin(bool important)
392 {
393     RefPtr<CSSPrimitiveValue> resultX;
394     RefPtr<CSSPrimitiveValue> resultY;
395     if (consumeOneOrTwoValuedPosition(m_range, m_context.mode, UnitlessQuirk::Forbid, resultX, resultY)) {
396         m_range.consumeWhitespace();
397         bool atEnd = m_range.atEnd();
398         RefPtr<CSSPrimitiveValue> resultZ = consumeLength(m_range, m_context.mode, ValueRangeAll);
399         bool hasZ = resultZ;
400         if (!hasZ && !atEnd)
401             return false;
402         addProperty(CSSPropertyTransformOriginX, CSSPropertyTransformOrigin, resultX.releaseNonNull(), important);
403         addProperty(CSSPropertyTransformOriginY, CSSPropertyTransformOrigin, resultY.releaseNonNull(), important);
404         addProperty(CSSPropertyTransformOriginZ, CSSPropertyTransformOrigin, resultZ ? resultZ.releaseNonNull() : CSSValuePool::singleton().createValue(0, CSSPrimitiveValue::UnitType::CSS_PX), important, !hasZ);
405         
406         return true;
407     }
408     return false;
409 }
410
411 bool CSSPropertyParser::consumePerspectiveOrigin(bool important)
412 {
413     RefPtr<CSSPrimitiveValue> resultX;
414     RefPtr<CSSPrimitiveValue> resultY;
415     if (consumePosition(m_range, m_context.mode, UnitlessQuirk::Forbid, resultX, resultY)) {
416         addProperty(CSSPropertyPerspectiveOriginX, CSSPropertyPerspectiveOrigin, resultX.releaseNonNull(), important);
417         addProperty(CSSPropertyPerspectiveOriginY, CSSPropertyPerspectiveOrigin, resultY.releaseNonNull(), important);
418         return true;
419     }
420     return false;
421 }
422
423 // Methods for consuming non-shorthand properties starts here.
424 static RefPtr<CSSValue> consumeWillChange(CSSParserTokenRange& range)
425 {
426     if (range.peek().id() == CSSValueAuto)
427         return consumeIdent(range);
428
429     RefPtr<CSSValueList> values = CSSValueList::createCommaSeparated();
430     // Every comma-separated list of identifiers is a valid will-change value,
431     // unless the list includes an explicitly disallowed identifier.
432     while (true) {
433         if (range.peek().type() != IdentToken)
434             return nullptr;
435         CSSPropertyID propertyID = cssPropertyID(range.peek().value());
436         if (propertyID != CSSPropertyInvalid) {
437             // Now "all" is used by both CSSValue and CSSPropertyValue.
438             // Need to return nullptr when currentValue is CSSPropertyAll.
439             if (propertyID == CSSPropertyWillChange || propertyID == CSSPropertyAll)
440                 return nullptr;
441             // FIXME-NEWPARSER: Use CSSCustomIdentValue someday.
442             values->append(CSSValuePool::singleton().createIdentifierValue(propertyID));
443             range.consumeIncludingWhitespace();
444         } else {
445             switch (range.peek().id()) {
446             case CSSValueNone:
447             case CSSValueAll:
448             case CSSValueAuto:
449             case CSSValueDefault:
450             case CSSValueInitial:
451             case CSSValueInherit:
452                 return nullptr;
453             case CSSValueContents:
454             case CSSValueScrollPosition:
455                 values->append(consumeIdent(range).releaseNonNull());
456                 break;
457             default:
458                 // Append properties we don't recognize, but that are legal, as strings.
459                 values->append(consumeCustomIdent(range).releaseNonNull());
460                 break;
461             }
462         }
463
464         if (range.atEnd())
465             break;
466         if (!consumeCommaIncludingWhitespace(range))
467             return nullptr;
468     }
469
470     return values;
471 }
472
473 static RefPtr<CSSFontFeatureValue> consumeFontFeatureTag(CSSParserTokenRange& range)
474 {
475     // Feature tag name consists of 4-letter characters.
476     static const unsigned tagNameLength = 4;
477
478     const CSSParserToken& token = range.consumeIncludingWhitespace();
479     // Feature tag name comes first
480     if (token.type() != StringToken)
481         return nullptr;
482     if (token.value().length() != tagNameLength)
483         return nullptr;
484     
485     FontTag tag;
486     for (unsigned i = 0; i < tag.size(); ++i) {
487         // Limits the range of characters to 0x20-0x7E, following the tag name rules defiend in the OpenType specification.
488         UChar character = token.value()[i];
489         if (character < 0x20 || character > 0x7E)
490             return nullptr;
491         tag[i] = toASCIILower(character);
492     }
493
494     int tagValue = 1;
495     if (!range.atEnd() && range.peek().type() != CommaToken) {
496         // Feature tag values could follow: <integer> | on | off
497         if (auto primitiveValue = consumeInteger(range, 0))
498             tagValue = primitiveValue->intValue();
499         else if (range.peek().id() == CSSValueOn || range.peek().id() == CSSValueOff)
500             tagValue = range.consumeIncludingWhitespace().id() == CSSValueOn;
501         else
502             return nullptr;
503     }
504     return CSSFontFeatureValue::create(WTFMove(tag), tagValue);
505 }
506
507 static RefPtr<CSSValue> consumeFontFeatureSettings(CSSParserTokenRange& range)
508 {
509     if (range.peek().id() == CSSValueNormal)
510         return consumeIdent(range);
511     RefPtr<CSSValueList> settings = CSSValueList::createCommaSeparated();
512     do {
513         RefPtr<CSSFontFeatureValue> fontFeatureValue = consumeFontFeatureTag(range);
514         if (!fontFeatureValue)
515             return nullptr;
516         settings->append(fontFeatureValue.releaseNonNull());
517     } while (consumeCommaIncludingWhitespace(range));
518     return settings;
519 }
520
521 #if ENABLE(VARIATION_FONTS)
522 static RefPtr<CSSValue> consumeFontVariationTag(CSSParserTokenRange& range)
523 {
524     if (range.peek().type() != StringToken)
525         return nullptr;
526     
527     auto string = range.consumeIncludingWhitespace().value().toString();
528     
529     FontTag tag;
530     if (string.length() != tag.size())
531         return nullptr;
532     for (unsigned i = 0; i < tag.size(); ++i) {
533         // Limits the range of characters to 0x20-0x7E, following the tag name rules defiend in the OpenType specification.
534         UChar character = string[i];
535         if (character < 0x20 || character > 0x7E)
536             return nullptr;
537         tag[i] = character;
538     }
539     
540     if (range.atEnd())
541         return nullptr;
542
543     double tagValue = 0;
544     auto success = consumeNumberRaw(range, tagValue);
545     if (!success)
546         return nullptr;
547     
548     return CSSFontVariationValue::create(tag, tagValue);
549 }
550     
551 static RefPtr<CSSValue> consumeFontVariationSettings(CSSParserTokenRange& range)
552 {
553     if (range.peek().id() == CSSValueNormal)
554         return consumeIdent(range);
555     
556     auto settings = CSSValueList::createCommaSeparated();
557     do {
558         RefPtr<CSSValue> variationValue = consumeFontVariationTag(range);
559         if (!variationValue)
560             return nullptr;
561         settings->append(variationValue.releaseNonNull());
562     } while (consumeCommaIncludingWhitespace(range));
563     
564     if (!settings->length())
565         return nullptr;
566
567     return WTFMove(settings);
568 }
569 #endif // ENABLE(VARIATION_FONTS)
570
571 static RefPtr<CSSValue> consumePage(CSSParserTokenRange& range)
572 {
573     if (range.peek().id() == CSSValueAuto)
574         return consumeIdent(range);
575     return consumeCustomIdent(range);
576 }
577
578 static RefPtr<CSSValue> consumeQuotes(CSSParserTokenRange& range)
579 {
580     if (range.peek().id() == CSSValueNone)
581         return consumeIdent(range);
582     RefPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
583     while (!range.atEnd()) {
584         RefPtr<CSSPrimitiveValue> parsedValue = consumeString(range);
585         if (!parsedValue)
586             return nullptr;
587         values->append(parsedValue.releaseNonNull());
588     }
589     if (values->length() && values->length() % 2 == 0)
590         return values;
591     return nullptr;
592 }
593
594 class FontVariantLigaturesParser {
595 public:
596     FontVariantLigaturesParser()
597         : m_sawCommonLigaturesValue(false)
598         , m_sawDiscretionaryLigaturesValue(false)
599         , m_sawHistoricalLigaturesValue(false)
600         , m_sawContextualLigaturesValue(false)
601         , m_result(CSSValueList::createSpaceSeparated())
602     {
603     }
604
605     enum class ParseResult {
606         ConsumedValue,
607         DisallowedValue,
608         UnknownValue
609     };
610
611     ParseResult consumeLigature(CSSParserTokenRange& range)
612     {
613         CSSValueID valueID = range.peek().id();
614         switch (valueID) {
615         case CSSValueNoCommonLigatures:
616         case CSSValueCommonLigatures:
617             if (m_sawCommonLigaturesValue)
618                 return ParseResult::DisallowedValue;
619             m_sawCommonLigaturesValue = true;
620             break;
621         case CSSValueNoDiscretionaryLigatures:
622         case CSSValueDiscretionaryLigatures:
623             if (m_sawDiscretionaryLigaturesValue)
624                 return ParseResult::DisallowedValue;
625             m_sawDiscretionaryLigaturesValue = true;
626             break;
627         case CSSValueNoHistoricalLigatures:
628         case CSSValueHistoricalLigatures:
629             if (m_sawHistoricalLigaturesValue)
630                 return ParseResult::DisallowedValue;
631             m_sawHistoricalLigaturesValue = true;
632             break;
633         case CSSValueNoContextual:
634         case CSSValueContextual:
635             if (m_sawContextualLigaturesValue)
636                 return ParseResult::DisallowedValue;
637             m_sawContextualLigaturesValue = true;
638             break;
639         default:
640             return ParseResult::UnknownValue;
641         }
642         m_result->append(consumeIdent(range).releaseNonNull());
643         return ParseResult::ConsumedValue;
644     }
645
646     RefPtr<CSSValue> finalizeValue()
647     {
648         if (!m_result->length())
649             return CSSValuePool::singleton().createIdentifierValue(CSSValueNormal);
650         return WTFMove(m_result);
651     }
652
653 private:
654     bool m_sawCommonLigaturesValue;
655     bool m_sawDiscretionaryLigaturesValue;
656     bool m_sawHistoricalLigaturesValue;
657     bool m_sawContextualLigaturesValue;
658     RefPtr<CSSValueList> m_result;
659 };
660
661 static RefPtr<CSSValue> consumeFontVariantLigatures(CSSParserTokenRange& range)
662 {
663     if (range.peek().id() == CSSValueNormal || range.peek().id() == CSSValueNone)
664         return consumeIdent(range);
665
666     FontVariantLigaturesParser ligaturesParser;
667     do {
668         if (ligaturesParser.consumeLigature(range) !=
669             FontVariantLigaturesParser::ParseResult::ConsumedValue)
670             return nullptr;
671     } while (!range.atEnd());
672
673     return ligaturesParser.finalizeValue();
674 }
675
676 static RefPtr<CSSValue> consumeFontVariantEastAsian(CSSParserTokenRange& range)
677 {
678     if (range.peek().id() == CSSValueNormal)
679         return consumeIdent(range);
680     
681     RefPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
682     FontVariantEastAsianVariant variant = FontVariantEastAsianVariant::Normal;
683     FontVariantEastAsianWidth width = FontVariantEastAsianWidth::Normal;
684     FontVariantEastAsianRuby ruby = FontVariantEastAsianRuby::Normal;
685     
686     while (!range.atEnd()) {
687         if (range.peek().type() != IdentToken)
688             return nullptr;
689         
690         auto id = range.peek().id();
691         
692         switch (id) {
693         case CSSValueJis78:
694             variant = FontVariantEastAsianVariant::Jis78;
695             break;
696         case CSSValueJis83:
697             variant = FontVariantEastAsianVariant::Jis83;
698             break;
699         case CSSValueJis90:
700             variant = FontVariantEastAsianVariant::Jis90;
701             break;
702         case CSSValueJis04:
703             variant = FontVariantEastAsianVariant::Jis04;
704             break;
705         case CSSValueSimplified:
706             variant = FontVariantEastAsianVariant::Simplified;
707             break;
708         case CSSValueTraditional:
709             variant = FontVariantEastAsianVariant::Traditional;
710             break;
711         case CSSValueFullWidth:
712             width = FontVariantEastAsianWidth::Full;
713             break;
714         case CSSValueProportionalWidth:
715             width = FontVariantEastAsianWidth::Proportional;
716             break;
717         case CSSValueRuby:
718             ruby = FontVariantEastAsianRuby::Yes;
719             break;
720         default:
721             return nullptr;
722         }
723         
724         range.consumeIncludingWhitespace();
725     }
726         
727     switch (variant) {
728     case FontVariantEastAsianVariant::Normal:
729         break;
730     case FontVariantEastAsianVariant::Jis78:
731         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueJis78));
732         break;
733     case FontVariantEastAsianVariant::Jis83:
734         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueJis83));
735         break;
736     case FontVariantEastAsianVariant::Jis90:
737         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueJis90));
738         break;
739     case FontVariantEastAsianVariant::Jis04:
740         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueJis04));
741         break;
742     case FontVariantEastAsianVariant::Simplified:
743         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueSimplified));
744         break;
745     case FontVariantEastAsianVariant::Traditional:
746         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueTraditional));
747         break;
748     }
749         
750     switch (width) {
751     case FontVariantEastAsianWidth::Normal:
752         break;
753     case FontVariantEastAsianWidth::Full:
754         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueFullWidth));
755         break;
756     case FontVariantEastAsianWidth::Proportional:
757         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueProportionalWidth));
758         break;
759     }
760         
761     switch (ruby) {
762     case FontVariantEastAsianRuby::Normal:
763         break;
764     case FontVariantEastAsianRuby::Yes:
765         values->append(CSSValuePool::singleton().createIdentifierValue(CSSValueRuby));
766     }
767
768     if (!values->length())
769         return nullptr;
770
771     return values;
772 }
773     
774 static RefPtr<CSSPrimitiveValue> consumeFontVariantCaps(CSSParserTokenRange& range)
775 {
776     return consumeIdent<CSSValueNormal, CSSValueSmallCaps, CSSValueAllSmallCaps,
777         CSSValuePetiteCaps, CSSValueAllPetiteCaps,
778         CSSValueUnicase, CSSValueTitlingCaps>(range);
779 }
780
781 static RefPtr<CSSPrimitiveValue> consumeFontVariantAlternates(CSSParserTokenRange& range)
782 {
783     return consumeIdent<CSSValueNormal, CSSValueHistoricalForms>(range);
784 }
785
786 static RefPtr<CSSPrimitiveValue> consumeFontVariantPosition(CSSParserTokenRange& range)
787 {
788     return consumeIdent<CSSValueNormal, CSSValueSub, CSSValueSuper>(range);
789 }
790
791 class FontVariantNumericParser {
792 public:
793     FontVariantNumericParser()
794         : m_sawNumericFigureValue(false)
795         , m_sawNumericSpacingValue(false)
796         , m_sawNumericFractionValue(false)
797         , m_sawOrdinalValue(false)
798         , m_sawSlashedZeroValue(false)
799         , m_result(CSSValueList::createSpaceSeparated())
800     {
801     }
802
803     enum class ParseResult {
804         ConsumedValue,
805         DisallowedValue,
806         UnknownValue
807     };
808
809     ParseResult consumeNumeric(CSSParserTokenRange& range)
810     {
811         CSSValueID valueID = range.peek().id();
812         switch (valueID) {
813         case CSSValueLiningNums:
814         case CSSValueOldstyleNums:
815             if (m_sawNumericFigureValue)
816                 return ParseResult::DisallowedValue;
817             m_sawNumericFigureValue = true;
818             break;
819         case CSSValueProportionalNums:
820         case CSSValueTabularNums:
821             if (m_sawNumericSpacingValue)
822                 return ParseResult::DisallowedValue;
823             m_sawNumericSpacingValue = true;
824             break;
825         case CSSValueDiagonalFractions:
826         case CSSValueStackedFractions:
827             if (m_sawNumericFractionValue)
828                 return ParseResult::DisallowedValue;
829             m_sawNumericFractionValue = true;
830             break;
831         case CSSValueOrdinal:
832             if (m_sawOrdinalValue)
833                 return ParseResult::DisallowedValue;
834             m_sawOrdinalValue = true;
835             break;
836         case CSSValueSlashedZero:
837             if (m_sawSlashedZeroValue)
838                 return ParseResult::DisallowedValue;
839             m_sawSlashedZeroValue = true;
840             break;
841         default:
842             return ParseResult::UnknownValue;
843         }
844         m_result->append(consumeIdent(range).releaseNonNull());
845         return ParseResult::ConsumedValue;
846     }
847
848     RefPtr<CSSValue> finalizeValue()
849     {
850         if (!m_result->length())
851             return CSSValuePool::singleton().createIdentifierValue(CSSValueNormal);
852         return WTFMove(m_result);
853     }
854
855
856 private:
857     bool m_sawNumericFigureValue;
858     bool m_sawNumericSpacingValue;
859     bool m_sawNumericFractionValue;
860     bool m_sawOrdinalValue;
861     bool m_sawSlashedZeroValue;
862     RefPtr<CSSValueList> m_result;
863 };
864
865 static RefPtr<CSSValue> consumeFontVariantNumeric(CSSParserTokenRange& range)
866 {
867     if (range.peek().id() == CSSValueNormal)
868         return consumeIdent(range);
869
870     FontVariantNumericParser numericParser;
871     do {
872         if (numericParser.consumeNumeric(range) !=
873             FontVariantNumericParser::ParseResult::ConsumedValue)
874             return nullptr;
875     } while (!range.atEnd());
876
877     return numericParser.finalizeValue();
878 }
879
880 static RefPtr<CSSPrimitiveValue> consumeFontVariantCSS21(CSSParserTokenRange& range)
881 {
882     return consumeIdent<CSSValueNormal, CSSValueSmallCaps>(range);
883 }
884
885 static RefPtr<CSSPrimitiveValue> consumeFontWeightKeywordValue(CSSParserTokenRange& range)
886 {
887     return consumeIdent<CSSValueNormal, CSSValueBold, CSSValueBolder, CSSValueLighter>(range);
888 }
889
890 static RefPtr<CSSPrimitiveValue> consumeFontWeight(CSSParserTokenRange& range)
891 {
892     if (auto result = consumeFontWeightKeywordValue(range))
893         return result;
894     return consumeFontWeightNumber(range);
895 }
896
897 #if ENABLE(VARIATION_FONTS)
898 static RefPtr<CSSValue> consumeFontWeightRange(CSSParserTokenRange& range)
899 {
900     if (auto result = consumeFontWeightKeywordValue(range))
901         return result;
902     auto firstNumber = consumeFontWeightNumber(range);
903     if (!firstNumber)
904         return nullptr;
905     if (range.atEnd())
906         return firstNumber;
907     auto secondNumber = consumeFontWeightNumber(range);
908     if (!secondNumber || firstNumber->floatValue() > secondNumber->floatValue())
909         return nullptr;
910     auto result = CSSValueList::createSpaceSeparated();
911     result->append(firstNumber.releaseNonNull());
912     result->append(secondNumber.releaseNonNull());
913     return RefPtr<CSSValue>(WTFMove(result));
914 }
915 #endif
916
917 static RefPtr<CSSPrimitiveValue> consumeFontStretchKeywordValue(CSSParserTokenRange& range)
918 {
919     return consumeIdent<CSSValueUltraCondensed, CSSValueExtraCondensed, CSSValueCondensed, CSSValueSemiCondensed, CSSValueNormal, CSSValueSemiExpanded, CSSValueExpanded, CSSValueExtraExpanded, CSSValueUltraExpanded>(range);
920 }
921
922 #if ENABLE(VARIATION_FONTS)
923 static bool fontStretchIsWithinRange(float stretch)
924 {
925     return stretch > 0;
926 }
927 #endif
928
929 static RefPtr<CSSPrimitiveValue> consumeFontStretch(CSSParserTokenRange& range)
930 {
931     if (auto result = consumeFontStretchKeywordValue(range))
932         return result;
933 #if ENABLE(VARIATION_FONTS)
934     if (auto percent = consumePercent(range, ValueRangeNonNegative))
935         return fontStretchIsWithinRange(percent->value<float>()) ? percent : nullptr;
936 #endif
937     return nullptr;
938 }
939
940 #if ENABLE(VARIATION_FONTS)
941 static RefPtr<CSSValue> consumeFontStretchRange(CSSParserTokenRange& range)
942 {
943     if (auto result = consumeFontStretchKeywordValue(range))
944         return result;
945     auto firstPercent = consumePercent(range, ValueRangeNonNegative);
946     if (!firstPercent || !fontStretchIsWithinRange(firstPercent->value<float>()))
947         return nullptr;
948     if (range.atEnd())
949         return firstPercent;
950     auto secondPercent = consumePercent(range, ValueRangeNonNegative);
951     if (!secondPercent || !fontStretchIsWithinRange(secondPercent->value<float>()) || firstPercent->floatValue() > secondPercent->floatValue())
952         return nullptr;
953     auto result = CSSValueList::createSpaceSeparated();
954     result->append(firstPercent.releaseNonNull());
955     result->append(secondPercent.releaseNonNull());
956     return RefPtr<CSSValue>(WTFMove(result));
957 }
958 #endif
959
960 static RefPtr<CSSPrimitiveValue> consumeFontStyleKeywordValue(CSSParserTokenRange& range)
961 {
962     return consumeIdent<CSSValueNormal, CSSValueItalic, CSSValueOblique>(range);
963 }
964
965 #if ENABLE(VARIATION_FONTS)
966 static bool fontStyleIsWithinRange(float oblique)
967 {
968     return oblique > -90 && oblique < 90;
969 }
970 #endif
971
972 static RefPtr<CSSFontStyleValue> consumeFontStyle(CSSParserTokenRange& range, CSSParserMode cssParserMode)
973 {
974     auto result = consumeFontStyleKeywordValue(range);
975     if (!result)
976         return nullptr;
977
978     auto valueID = result->valueID();
979     if (valueID == CSSValueNormal || valueID == CSSValueItalic)
980         return CSSFontStyleValue::create(CSSValuePool::singleton().createIdentifierValue(valueID));
981     ASSERT(result->valueID() == CSSValueOblique);
982 #if ENABLE(VARIATION_FONTS)
983     if (!range.atEnd()) {
984         if (auto angle = consumeAngle(range, cssParserMode)) {
985             if (fontStyleIsWithinRange(angle->value<float>(CSSPrimitiveValue::CSS_DEG)))
986                 return CSSFontStyleValue::create(CSSValuePool::singleton().createIdentifierValue(CSSValueOblique), WTFMove(angle));
987             return nullptr;
988         }
989     }
990 #else
991     UNUSED_PARAM(cssParserMode);
992 #endif
993     return CSSFontStyleValue::create(CSSValuePool::singleton().createIdentifierValue(CSSValueOblique));
994 }
995
996 #if ENABLE(VARIATION_FONTS)
997 static RefPtr<CSSFontStyleRangeValue> consumeFontStyleRange(CSSParserTokenRange& range, CSSParserMode cssParserMode)
998 {
999     auto keyword = consumeFontStyleKeywordValue(range);
1000     if (!keyword)
1001         return nullptr;
1002
1003     if (keyword->valueID() != CSSValueOblique || range.atEnd())
1004         return CSSFontStyleRangeValue::create(keyword.releaseNonNull());
1005
1006     if (auto firstAngle = consumeAngle(range, cssParserMode)) {
1007         if (!fontStyleIsWithinRange(firstAngle->value<float>(CSSPrimitiveValue::CSS_DEG)))
1008             return nullptr;
1009         if (range.atEnd()) {
1010             auto result = CSSValueList::createSpaceSeparated();
1011             result->append(firstAngle.releaseNonNull());
1012             return CSSFontStyleRangeValue::create(keyword.releaseNonNull(), WTFMove(result));
1013         }
1014         auto secondAngle = consumeAngle(range, cssParserMode);
1015         if (!secondAngle || !fontStyleIsWithinRange(secondAngle->value<float>(CSSPrimitiveValue::CSS_DEG)) || firstAngle->floatValue(CSSPrimitiveValue::CSS_DEG) > secondAngle->floatValue(CSSPrimitiveValue::CSS_DEG))
1016             return nullptr;
1017         auto result = CSSValueList::createSpaceSeparated();
1018         result->append(firstAngle.releaseNonNull());
1019         result->append(secondAngle.releaseNonNull());
1020         return CSSFontStyleRangeValue::create(keyword.releaseNonNull(), WTFMove(result));
1021     }
1022
1023     return nullptr;
1024 }
1025 #endif
1026
1027 static String concatenateFamilyName(CSSParserTokenRange& range)
1028 {
1029     StringBuilder builder;
1030     bool addedSpace = false;
1031     const CSSParserToken& firstToken = range.peek();
1032     while (range.peek().type() == IdentToken) {
1033         if (!builder.isEmpty()) {
1034             builder.append(' ');
1035             addedSpace = true;
1036         }
1037         builder.append(range.consumeIncludingWhitespace().value());
1038     }
1039     if (!addedSpace && isCSSWideKeyword(firstToken.id()))
1040         return String();
1041     return builder.toString();
1042 }
1043
1044 static RefPtr<CSSValue> consumeFamilyName(CSSParserTokenRange& range)
1045 {
1046     if (range.peek().type() == StringToken)
1047         return CSSValuePool::singleton().createFontFamilyValue(range.consumeIncludingWhitespace().value().toString());
1048     if (range.peek().type() != IdentToken)
1049         return nullptr;
1050     String familyName = concatenateFamilyName(range);
1051     if (familyName.isNull())
1052         return nullptr;
1053     return CSSValuePool::singleton().createFontFamilyValue(familyName);
1054 }
1055
1056 static RefPtr<CSSValue> consumeGenericFamily(CSSParserTokenRange& range)
1057 {
1058     return consumeIdentRange(range, CSSValueSerif, CSSValueWebkitBody);
1059 }
1060
1061 static RefPtr<CSSValueList> consumeFontFamily(CSSParserTokenRange& range)
1062 {
1063     RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
1064     do {
1065         RefPtr<CSSValue> parsedValue = consumeGenericFamily(range);
1066         if (parsedValue) {
1067             list->append(parsedValue.releaseNonNull());
1068         } else {
1069             parsedValue = consumeFamilyName(range);
1070             if (parsedValue) {
1071                 list->append(parsedValue.releaseNonNull());
1072             } else {
1073                 return nullptr;
1074             }
1075         }
1076     } while (consumeCommaIncludingWhitespace(range));
1077     return list;
1078 }
1079
1080 static RefPtr<CSSValueList> consumeFontFamilyDescriptor(CSSParserTokenRange& range)
1081 {
1082     // FIXME-NEWPARSER: For compatibility with the old parser, we have to make
1083     // a list here, even though the list always contains only a single family name.
1084     // Once the old parser is gone, we can delete this function, make the caller
1085     // use consumeFamilyName instead, and then patch the @font-face code to
1086     // not expect a list with a single name in it.
1087     RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
1088     RefPtr<CSSValue> parsedValue = consumeFamilyName(range);
1089     if (parsedValue)
1090         list->append(parsedValue.releaseNonNull());
1091     
1092     if (!range.atEnd() || !list->length())
1093         return nullptr;
1094
1095     return list;
1096 }
1097
1098 static RefPtr<CSSValue> consumeFontSynthesis(CSSParserTokenRange& range)
1099 {
1100     // none | [ weight || style || small-caps ]
1101     CSSValueID id = range.peek().id();
1102     if (id == CSSValueNone)
1103         return consumeIdent(range);
1104     
1105     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1106     while (true) {
1107         auto ident = consumeIdent<CSSValueWeight, CSSValueStyle, CSSValueSmallCaps>(range);
1108         if (!ident)
1109             break;
1110         if (list->hasValue(ident.get()))
1111             return nullptr;
1112         list->append(ident.releaseNonNull());
1113     }
1114     
1115     if (!list->length())
1116         return nullptr;
1117     return list;
1118 }
1119
1120 static RefPtr<CSSValue> consumeLetterSpacing(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1121 {
1122     if (range.peek().id() == CSSValueNormal)
1123         return consumeIdent(range);
1124     
1125     return consumeLength(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
1126 }
1127
1128 static RefPtr<CSSValue> consumeWordSpacing(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1129 {
1130     if (range.peek().id() == CSSValueNormal)
1131         return consumeIdent(range);
1132     
1133     return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
1134 }
1135     
1136 static RefPtr<CSSValue> consumeTabSize(CSSParserTokenRange& range, CSSParserMode)
1137 {
1138     return consumeInteger(range, 0);
1139 }
1140
1141 #if ENABLE(TEXT_AUTOSIZING)
1142 static RefPtr<CSSValue> consumeTextSizeAdjust(CSSParserTokenRange& range, CSSParserMode /* cssParserMode */)
1143 {
1144     if (range.peek().id() == CSSValueAuto)
1145         return consumeIdent(range);
1146     if (range.peek().id() == CSSValueNone)
1147         return consumeIdent(range);
1148     return consumePercent(range, ValueRangeNonNegative);
1149 }
1150 #endif
1151
1152 static RefPtr<CSSValue> consumeFontSize(CSSParserTokenRange& range, CSSParserMode cssParserMode, UnitlessQuirk unitless = UnitlessQuirk::Forbid)
1153 {
1154     if (range.peek().id() >= CSSValueXxSmall && range.peek().id() <= CSSValueLarger)
1155         return consumeIdent(range);
1156     return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative, unitless);
1157 }
1158
1159 static RefPtr<CSSPrimitiveValue> consumeLineHeight(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1160 {
1161     if (range.peek().id() == CSSValueNormal)
1162         return consumeIdent(range);
1163
1164     RefPtr<CSSPrimitiveValue> lineHeight = consumeNumber(range, ValueRangeNonNegative);
1165     if (lineHeight)
1166         return lineHeight;
1167     return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
1168 }
1169
1170 template<typename... Args>
1171 static Ref<CSSPrimitiveValue> createPrimitiveValuePair(Args&&... args)
1172 {
1173     return CSSValuePool::singleton().createValue(Pair::create(std::forward<Args>(args)...));
1174 }
1175
1176
1177 static RefPtr<CSSValue> consumeCounter(CSSParserTokenRange& range, int defaultValue)
1178 {
1179     if (range.peek().id() == CSSValueNone)
1180         return consumeIdent(range);
1181
1182     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1183     do {
1184         RefPtr<CSSPrimitiveValue> counterName = consumeCustomIdent(range);
1185         if (!counterName)
1186             return nullptr;
1187         int i = defaultValue;
1188         if (RefPtr<CSSPrimitiveValue> counterValue = consumeInteger(range))
1189             i = counterValue->intValue();
1190         list->append(createPrimitiveValuePair(counterName.releaseNonNull(), CSSPrimitiveValue::create(i, CSSPrimitiveValue::UnitType::CSS_NUMBER), Pair::IdenticalValueEncoding::Coalesce));
1191     } while (!range.atEnd());
1192     return list;
1193 }
1194
1195 static RefPtr<CSSValue> consumePageSize(CSSParserTokenRange& range)
1196 {
1197     return consumeIdent<CSSValueA3, CSSValueA4, CSSValueA5, CSSValueB4, CSSValueB5, CSSValueLedger, CSSValueLegal, CSSValueLetter>(range);
1198 }
1199
1200 static RefPtr<CSSValueList> consumeSize(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1201 {
1202     RefPtr<CSSValueList> result = CSSValueList::createSpaceSeparated();
1203
1204     if (range.peek().id() == CSSValueAuto) {
1205         result->append(consumeIdent(range).releaseNonNull());
1206         return result;
1207     }
1208
1209     if (RefPtr<CSSValue> width = consumeLength(range, cssParserMode, ValueRangeNonNegative)) {
1210         RefPtr<CSSValue> height = consumeLength(range, cssParserMode, ValueRangeNonNegative);
1211         result->append(width.releaseNonNull());
1212         if (height)
1213             result->append(height.releaseNonNull());
1214         return result;
1215     }
1216
1217     RefPtr<CSSValue> pageSize = consumePageSize(range);
1218     RefPtr<CSSValue> orientation = consumeIdent<CSSValuePortrait, CSSValueLandscape>(range);
1219     if (!pageSize)
1220         pageSize = consumePageSize(range);
1221
1222     if (!orientation && !pageSize)
1223         return nullptr;
1224     if (pageSize)
1225         result->append(pageSize.releaseNonNull());
1226     if (orientation)
1227         result->append(orientation.releaseNonNull());
1228     return result;
1229 }
1230
1231 static RefPtr<CSSValue> consumeTextIndent(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1232 {
1233     // [ <length> | <percentage> ] && hanging? && each-line?
1234     // Keywords only allowed when css3Text is enabled.
1235     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1236
1237     bool hasLengthOrPercentage = false;
1238 //    bool hasEachLine = false;
1239     bool hasHanging = false;
1240
1241     do {
1242         if (!hasLengthOrPercentage) {
1243             if (RefPtr<CSSValue> textIndent = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow)) {
1244                 list->append(*textIndent);
1245                 hasLengthOrPercentage = true;
1246                 continue;
1247             }
1248         }
1249
1250         CSSValueID id = range.peek().id();
1251  /* FIXME-NEWPARSER: We don't support this yet.
1252         if (!hasEachLine && id == CSSValueEachLine) {
1253             list->append(*consumeIdent(range));
1254             hasEachLine = true;
1255             continue;
1256         }
1257 */
1258         
1259         if (!hasHanging && id == CSSValueHanging) {
1260             list->append(consumeIdent(range).releaseNonNull());
1261             hasHanging = true;
1262             continue;
1263         }
1264         
1265         return nullptr;
1266     } while (!range.atEnd());
1267
1268     if (!hasLengthOrPercentage)
1269         return nullptr;
1270
1271     return list;
1272 }
1273
1274 static bool validWidthOrHeightKeyword(CSSValueID id, const CSSParserContext& /*context*/)
1275 {
1276     if (id == CSSValueIntrinsic || id == CSSValueMinIntrinsic || id == CSSValueMinContent || id == CSSValueWebkitMinContent || id == CSSValueMaxContent || id == CSSValueWebkitMaxContent || id == CSSValueWebkitFillAvailable || id == CSSValueFitContent || id == CSSValueWebkitFitContent) {
1277         return true;
1278     }
1279     return false;
1280 }
1281
1282 static RefPtr<CSSValue> consumeMaxWidthOrHeight(CSSParserTokenRange& range, const CSSParserContext& context, UnitlessQuirk unitless = UnitlessQuirk::Forbid)
1283 {
1284     if (range.peek().id() == CSSValueNone || validWidthOrHeightKeyword(range.peek().id(), context))
1285         return consumeIdent(range);
1286     return consumeLengthOrPercent(range, context.mode, ValueRangeNonNegative, unitless);
1287 }
1288
1289 static RefPtr<CSSValue> consumeWidthOrHeight(CSSParserTokenRange& range, const CSSParserContext& context, UnitlessQuirk unitless = UnitlessQuirk::Forbid)
1290 {
1291     if (range.peek().id() == CSSValueAuto || validWidthOrHeightKeyword(range.peek().id(), context))
1292         return consumeIdent(range);
1293     return consumeLengthOrPercent(range, context.mode, ValueRangeNonNegative, unitless);
1294 }
1295
1296 static RefPtr<CSSValue> consumeMarginOrOffset(CSSParserTokenRange& range, CSSParserMode cssParserMode, UnitlessQuirk unitless)
1297 {
1298     if (range.peek().id() == CSSValueAuto)
1299         return consumeIdent(range);
1300     return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, unitless);
1301 }
1302
1303 static RefPtr<CSSPrimitiveValue> consumeClipComponent(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1304 {
1305     if (range.peek().id() == CSSValueAuto)
1306         return consumeIdent(range);
1307     return consumeLength(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
1308 }
1309
1310 static RefPtr<CSSValue> consumeClip(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1311 {
1312     if (range.peek().id() == CSSValueAuto)
1313         return consumeIdent(range);
1314
1315     if (range.peek().functionId() != CSSValueRect)
1316         return nullptr;
1317
1318     CSSParserTokenRange args = consumeFunction(range);
1319     // rect(t, r, b, l) || rect(t r b l)
1320     RefPtr<CSSPrimitiveValue> top = consumeClipComponent(args, cssParserMode);
1321     if (!top)
1322         return nullptr;
1323     bool needsComma = consumeCommaIncludingWhitespace(args);
1324     RefPtr<CSSPrimitiveValue> right = consumeClipComponent(args, cssParserMode);
1325     if (!right || (needsComma && !consumeCommaIncludingWhitespace(args)))
1326         return nullptr;
1327     RefPtr<CSSPrimitiveValue> bottom = consumeClipComponent(args, cssParserMode);
1328     if (!bottom || (needsComma && !consumeCommaIncludingWhitespace(args)))
1329         return nullptr;
1330     RefPtr<CSSPrimitiveValue> left = consumeClipComponent(args, cssParserMode);
1331     if (!left || !args.atEnd())
1332         return nullptr;
1333     
1334     auto rect = Rect::create();
1335     rect->setLeft(left.releaseNonNull());
1336     rect->setTop(top.releaseNonNull());
1337     rect->setRight(right.releaseNonNull());
1338     rect->setBottom(bottom.releaseNonNull());
1339     return CSSValuePool::singleton().createValue(WTFMove(rect));
1340 }
1341
1342 #if ENABLE(TOUCH_EVENTS)
1343 static RefPtr<CSSValue> consumeTouchAction(CSSParserTokenRange& range)
1344 {
1345     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1346     CSSValueID id = range.peek().id();
1347     if (id == CSSValueAuto || id == CSSValueNone || id == CSSValueManipulation) {
1348         list->append(consumeIdent(range).releaseNonNull());
1349         return list;
1350     }
1351     // FIXME-NEWPARSER: Support pan.
1352     return nullptr;
1353 }
1354 #endif
1355
1356 static RefPtr<CSSPrimitiveValue> consumeLineClamp(CSSParserTokenRange& range)
1357 {
1358     RefPtr<CSSPrimitiveValue> clampValue = consumePercent(range, ValueRangeNonNegative);
1359     if (clampValue)
1360         return clampValue;
1361     // When specifying number of lines, don't allow 0 as a valid value.
1362     return consumePositiveInteger(range);
1363 }
1364
1365 static RefPtr<CSSValue> consumeAutoOrString(CSSParserTokenRange& range)
1366 {
1367     if (range.peek().id() == CSSValueAuto)
1368         return consumeIdent(range);
1369     return consumeString(range);
1370 }
1371
1372 static RefPtr<CSSValue> consumeHyphenateLimit(CSSParserTokenRange& range, CSSValueID valueID)
1373 {
1374     if (range.peek().id() == valueID)
1375         return consumeIdent(range);
1376     return consumeNumber(range, ValueRangeNonNegative);
1377 }
1378
1379 static RefPtr<CSSValue> consumeColumnWidth(CSSParserTokenRange& range)
1380 {
1381     if (range.peek().id() == CSSValueAuto)
1382         return consumeIdent(range);
1383     // Always parse lengths in strict mode here, since it would be ambiguous otherwise when used in
1384     // the 'columns' shorthand property.
1385     RefPtr<CSSPrimitiveValue> columnWidth = consumeLength(range, HTMLStandardMode, ValueRangeNonNegative);
1386     if (!columnWidth || (!columnWidth->isCalculated() && !columnWidth->doubleValue()) || (columnWidth->cssCalcValue() && !columnWidth->cssCalcValue()->doubleValue()))
1387         return nullptr;
1388     return columnWidth;
1389 }
1390
1391 static RefPtr<CSSValue> consumeColumnCount(CSSParserTokenRange& range)
1392 {
1393     if (range.peek().id() == CSSValueAuto)
1394         return consumeIdent(range);
1395     return consumePositiveInteger(range);
1396 }
1397
1398 static RefPtr<CSSValue> consumeGapLength(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1399 {
1400     if (range.peek().id() == CSSValueNormal)
1401         return consumeIdent(range);
1402     return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
1403 }
1404
1405 static RefPtr<CSSValue> consumeColumnSpan(CSSParserTokenRange& range)
1406 {
1407     return consumeIdent<CSSValueAll, CSSValueNone>(range);
1408 }
1409
1410 static RefPtr<CSSValue> consumeZoom(CSSParserTokenRange& range, const CSSParserContext& /*context*/)
1411 {
1412     const CSSParserToken& token = range.peek();
1413     RefPtr<CSSPrimitiveValue> zoom;
1414     if (token.type() == IdentToken)
1415         zoom = consumeIdent<CSSValueNormal, CSSValueReset, CSSValueDocument>(range);
1416     else {
1417         zoom = consumePercent(range, ValueRangeNonNegative);
1418         if (!zoom)
1419             zoom = consumeNumber(range, ValueRangeNonNegative);
1420     }
1421     return zoom;
1422 }
1423
1424 static RefPtr<CSSValue> consumeAnimationIterationCount(CSSParserTokenRange& range)
1425 {
1426     if (range.peek().id() == CSSValueInfinite)
1427         return consumeIdent(range);
1428     return consumeNumber(range, ValueRangeNonNegative);
1429 }
1430
1431 static RefPtr<CSSValue> consumeAnimationName(CSSParserTokenRange& range)
1432 {
1433     if (range.peek().id() == CSSValueNone)
1434         return consumeIdent(range);
1435
1436     if (range.peek().type() == StringToken) {
1437         const CSSParserToken& token = range.consumeIncludingWhitespace();
1438         if (equalIgnoringASCIICase(token.value(), "none"))
1439             return CSSValuePool::singleton().createIdentifierValue(CSSValueNone);
1440         // FIXME-NEWPARSER: Want to use a CSSCustomIdentValue here eventually.
1441         return CSSValuePool::singleton().createValue(token.value().toString(), CSSPrimitiveValue::UnitType::CSS_STRING);
1442     }
1443
1444     return consumeCustomIdent(range);
1445 }
1446
1447 static RefPtr<CSSValue> consumeTransitionProperty(CSSParserTokenRange& range)
1448 {
1449     const CSSParserToken& token = range.peek();
1450     if (token.type() != IdentToken)
1451         return nullptr;
1452     if (token.id() == CSSValueNone)
1453         return consumeIdent(range);
1454
1455     if (CSSPropertyID property = token.parseAsCSSPropertyID()) {
1456         range.consumeIncludingWhitespace();
1457         
1458         // FIXME-NEWPARSER: No reason why we can't use the "all" property now that it exists.
1459         // The old parser used a value keyword for "all", though, since it predated support for
1460         // the property.
1461         if (property == CSSPropertyAll)
1462             return CSSValuePool::singleton().createIdentifierValue(CSSValueAll);
1463
1464         // FIXME-NEWPARSER: Want to use a CSSCustomIdentValue here eventually.
1465         return CSSValuePool::singleton().createIdentifierValue(property);
1466     }
1467     return consumeCustomIdent(range);
1468 }
1469
1470     
1471 static RefPtr<CSSValue> consumeSteps(CSSParserTokenRange& range)
1472 {
1473     ASSERT(range.peek().functionId() == CSSValueSteps);
1474     CSSParserTokenRange rangeCopy = range;
1475     CSSParserTokenRange args = consumeFunction(rangeCopy);
1476     
1477     RefPtr<CSSPrimitiveValue> steps = consumePositiveInteger(args);
1478     if (!steps)
1479         return nullptr;
1480     
1481     // FIXME-NEWPARSER: Support the middle value and change from a boolean to an enum.
1482     bool stepAtStart = false;
1483     if (consumeCommaIncludingWhitespace(args)) {
1484         switch (args.consumeIncludingWhitespace().id()) {
1485             case CSSValueStart:
1486                 stepAtStart = true;
1487             break;
1488             case CSSValueEnd:
1489                 stepAtStart = false;
1490                 break;
1491             default:
1492                 return nullptr;
1493         }
1494     }
1495     
1496     if (!args.atEnd())
1497         return nullptr;
1498     
1499     range = rangeCopy;
1500     return CSSStepsTimingFunctionValue::create(steps->intValue(), stepAtStart);
1501 }
1502
1503 static RefPtr<CSSValue> consumeCubicBezier(CSSParserTokenRange& range)
1504 {
1505     ASSERT(range.peek().functionId() == CSSValueCubicBezier);
1506     CSSParserTokenRange rangeCopy = range;
1507     CSSParserTokenRange args = consumeFunction(rangeCopy);
1508
1509     double x1, y1, x2, y2;
1510     if (consumeNumberRaw(args, x1)
1511         && x1 >= 0 && x1 <= 1
1512         && consumeCommaIncludingWhitespace(args)
1513         && consumeNumberRaw(args, y1)
1514         && consumeCommaIncludingWhitespace(args)
1515         && consumeNumberRaw(args, x2)
1516         && x2 >= 0 && x2 <= 1
1517         && consumeCommaIncludingWhitespace(args)
1518         && consumeNumberRaw(args, y2)
1519         && args.atEnd()) {
1520         range = rangeCopy;
1521         return CSSCubicBezierTimingFunctionValue::create(x1, y1, x2, y2);
1522     }
1523
1524     return nullptr;
1525 }
1526
1527 static RefPtr<CSSValue> consumeSpringFunction(CSSParserTokenRange& range)
1528 {
1529     ASSERT(range.peek().functionId() == CSSValueSpring);
1530     CSSParserTokenRange rangeCopy = range;
1531     CSSParserTokenRange args = consumeFunction(rangeCopy);
1532
1533     // Mass must be greater than 0.
1534     double mass;
1535     if (!consumeNumberRaw(args, mass) || mass <= 0)
1536         return nullptr;
1537     
1538     // Stiffness must be greater than 0.
1539     double stiffness;
1540     if (!consumeNumberRaw(args, stiffness) || stiffness <= 0)
1541         return nullptr;
1542     
1543     // Damping coefficient must be greater than or equal to 0.
1544     double damping;
1545     if (!consumeNumberRaw(args, damping) || damping < 0)
1546         return nullptr;
1547     
1548     // Initial velocity may have any value.
1549     double initialVelocity;
1550     if (!consumeNumberRaw(args, initialVelocity))
1551         return nullptr;
1552
1553     if (!args.atEnd())
1554         return nullptr;
1555
1556     range = rangeCopy;
1557
1558     return CSSSpringTimingFunctionValue::create(mass, stiffness, damping, initialVelocity);
1559 }
1560
1561 static RefPtr<CSSValue> consumeAnimationTimingFunction(CSSParserTokenRange& range, const CSSParserContext& context)
1562 {
1563     CSSValueID id = range.peek().id();
1564     if (id == CSSValueEase || id == CSSValueLinear || id == CSSValueEaseIn
1565         || id == CSSValueEaseOut || id == CSSValueEaseInOut || id == CSSValueStepStart || id == CSSValueStepEnd)
1566         return consumeIdent(range);
1567
1568     CSSValueID function = range.peek().functionId();
1569     if (function == CSSValueCubicBezier)
1570         return consumeCubicBezier(range);
1571     if (function == CSSValueSteps)
1572         return consumeSteps(range);
1573     if (context.springTimingFunctionEnabled && function == CSSValueSpring)
1574         return consumeSpringFunction(range);
1575     return nullptr;
1576 }
1577
1578 static RefPtr<CSSValue> consumeAnimationValue(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context)
1579 {
1580     switch (property) {
1581     case CSSPropertyAnimationDelay:
1582     case CSSPropertyTransitionDelay:
1583         return consumeTime(range, context.mode, ValueRangeAll, UnitlessQuirk::Forbid);
1584     case CSSPropertyAnimationDirection:
1585         return consumeIdent<CSSValueNormal, CSSValueAlternate, CSSValueReverse, CSSValueAlternateReverse>(range);
1586     case CSSPropertyAnimationDuration:
1587     case CSSPropertyTransitionDuration:
1588         return consumeTime(range, context.mode, ValueRangeNonNegative, UnitlessQuirk::Forbid);
1589     case CSSPropertyAnimationFillMode:
1590         return consumeIdent<CSSValueNone, CSSValueForwards, CSSValueBackwards, CSSValueBoth>(range);
1591     case CSSPropertyAnimationIterationCount:
1592         return consumeAnimationIterationCount(range);
1593     case CSSPropertyAnimationName:
1594         return consumeAnimationName(range);
1595     case CSSPropertyAnimationPlayState:
1596         return consumeIdent<CSSValueRunning, CSSValuePaused>(range);
1597     case CSSPropertyTransitionProperty:
1598         return consumeTransitionProperty(range);
1599     case CSSPropertyAnimationTimingFunction:
1600     case CSSPropertyTransitionTimingFunction:
1601         return consumeAnimationTimingFunction(range, context);
1602     default:
1603         ASSERT_NOT_REACHED();
1604         return nullptr;
1605     }
1606 }
1607
1608 static bool isValidAnimationPropertyList(CSSPropertyID property, const CSSValueList& valueList)
1609 {
1610     if (property != CSSPropertyTransitionProperty || valueList.length() < 2)
1611         return true;
1612     for (auto& value : valueList) {
1613         if (value->isPrimitiveValue() && downcast<CSSPrimitiveValue>(value.get()).isValueID()
1614             && downcast<CSSPrimitiveValue>(value.get()).valueID() == CSSValueNone)
1615             return false;
1616     }
1617     return true;
1618 }
1619
1620 static RefPtr<CSSValue> consumeAnimationPropertyList(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context)
1621 {
1622     RefPtr<CSSValueList> list;
1623     RefPtr<CSSValue> singleton;
1624     do {
1625         RefPtr<CSSValue> currentValue = consumeAnimationValue(property, range, context);
1626         if (!currentValue)
1627             return nullptr;
1628         
1629         if (singleton && !list) {
1630             list = CSSValueList::createCommaSeparated();
1631             list->append(singleton.releaseNonNull());
1632         }
1633         
1634         if (list)
1635             list->append(currentValue.releaseNonNull());
1636         else
1637             singleton = WTFMove(currentValue);
1638         
1639     } while (consumeCommaIncludingWhitespace(range));
1640
1641     if (list) {
1642         if (!isValidAnimationPropertyList(property, *list))
1643             return nullptr;
1644     
1645         ASSERT(list->length());
1646         return list;
1647     }
1648     
1649     return singleton;
1650 }
1651
1652 bool CSSPropertyParser::consumeAnimationShorthand(const StylePropertyShorthand& shorthand, bool important)
1653 {
1654     const unsigned longhandCount = shorthand.length();
1655     RefPtr<CSSValueList> longhands[8];
1656     ASSERT(longhandCount <= 8);
1657     for (size_t i = 0; i < longhandCount; ++i)
1658         longhands[i] = CSSValueList::createCommaSeparated();
1659
1660     do {
1661         bool parsedLonghand[8] = { false };
1662         do {
1663             bool foundProperty = false;
1664             for (size_t i = 0; i < longhandCount; ++i) {
1665                 if (parsedLonghand[i])
1666                     continue;
1667
1668                 if (RefPtr<CSSValue> value = consumeAnimationValue(shorthand.properties()[i], m_range, m_context)) {
1669                     parsedLonghand[i] = true;
1670                     foundProperty = true;
1671                     longhands[i]->append(*value);
1672                     break;
1673                 }
1674             }
1675             if (!foundProperty)
1676                 return false;
1677         } while (!m_range.atEnd() && m_range.peek().type() != CommaToken);
1678
1679         // FIXME: This will make invalid longhands, see crbug.com/386459
1680         for (size_t i = 0; i < longhandCount; ++i) {
1681             if (!parsedLonghand[i])
1682                 longhands[i]->append(CSSValuePool::singleton().createImplicitInitialValue());
1683             parsedLonghand[i] = false;
1684         }
1685     } while (consumeCommaIncludingWhitespace(m_range));
1686
1687     for (size_t i = 0; i < longhandCount; ++i) {
1688         if (!isValidAnimationPropertyList(shorthand.properties()[i], *longhands[i]))
1689             return false;
1690     }
1691
1692     for (size_t i = 0; i < longhandCount; ++i)
1693         addProperty(shorthand.properties()[i], shorthand.id(), *longhands[i], important);
1694
1695     return m_range.atEnd();
1696 }
1697
1698 static RefPtr<CSSValue> consumeZIndex(CSSParserTokenRange& range)
1699 {
1700     if (range.peek().id() == CSSValueAuto)
1701         return consumeIdent(range);
1702     return consumeInteger(range);
1703 }
1704
1705 static RefPtr<CSSValue> consumeShadow(CSSParserTokenRange& range, CSSParserMode cssParserMode, bool isBoxShadowProperty)
1706 {
1707     if (range.peek().id() == CSSValueNone)
1708         return consumeIdent(range);
1709
1710     RefPtr<CSSValueList> shadowValueList = CSSValueList::createCommaSeparated();
1711     do {
1712         if (RefPtr<CSSShadowValue> shadowValue = consumeSingleShadow(range, cssParserMode, isBoxShadowProperty, isBoxShadowProperty))
1713             shadowValueList->append(*shadowValue);
1714         else
1715             return nullptr;
1716     } while (consumeCommaIncludingWhitespace(range));
1717     return shadowValueList;
1718 }
1719
1720 static RefPtr<CSSValue> consumeTextDecorationLine(CSSParserTokenRange& range)
1721 {
1722     CSSValueID id = range.peek().id();
1723     if (id == CSSValueNone)
1724         return consumeIdent(range);
1725
1726     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1727     while (true) {
1728 #if ENABLE(LETTERPRESS)
1729         RefPtr<CSSPrimitiveValue> ident = consumeIdent<CSSValueBlink, CSSValueUnderline, CSSValueOverline, CSSValueLineThrough, CSSValueWebkitLetterpress>(range);
1730 #else
1731         RefPtr<CSSPrimitiveValue> ident = consumeIdent<CSSValueBlink, CSSValueUnderline, CSSValueOverline, CSSValueLineThrough>(range);
1732 #endif
1733         if (!ident)
1734             break;
1735         if (list->hasValue(ident.get()))
1736             return nullptr;
1737         list->append(ident.releaseNonNull());
1738     }
1739
1740     if (!list->length())
1741         return nullptr;
1742     return list;
1743 }
1744
1745 static RefPtr<CSSValue> consumeTextDecorationSkip(CSSParserTokenRange& range)
1746 {
1747     CSSValueID id = range.peek().id();
1748     if (id == CSSValueNone)
1749         return consumeIdent(range);
1750
1751     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1752     while (true) {
1753         auto ident = consumeIdent<CSSValueAuto, CSSValueInk, CSSValueObjects>(range);
1754         if (!ident)
1755             break;
1756         if (list->hasValue(ident.get()))
1757             return nullptr;
1758         list->append(ident.releaseNonNull());
1759     }
1760
1761     if (!list->length())
1762         return nullptr;
1763     return list;
1764 }
1765
1766 static RefPtr<CSSValue> consumeTextEmphasisStyle(CSSParserTokenRange& range)
1767 {
1768     CSSValueID id = range.peek().id();
1769     if (id == CSSValueNone)
1770         return consumeIdent(range);
1771
1772     if (RefPtr<CSSValue> textEmphasisStyle = consumeString(range))
1773         return textEmphasisStyle;
1774
1775     RefPtr<CSSPrimitiveValue> fill = consumeIdent<CSSValueFilled, CSSValueOpen>(range);
1776     RefPtr<CSSPrimitiveValue> shape = consumeIdent<CSSValueDot, CSSValueCircle, CSSValueDoubleCircle, CSSValueTriangle, CSSValueSesame>(range);
1777     if (!fill)
1778         fill = consumeIdent<CSSValueFilled, CSSValueOpen>(range);
1779     if (fill && shape) {
1780         RefPtr<CSSValueList> parsedValues = CSSValueList::createSpaceSeparated();
1781         parsedValues->append(fill.releaseNonNull());
1782         parsedValues->append(shape.releaseNonNull());
1783         return parsedValues;
1784     }
1785     if (fill)
1786         return fill;
1787     if (shape)
1788         return shape;
1789     return nullptr;
1790 }
1791
1792 static RefPtr<CSSPrimitiveValue> consumeCaretColor(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1793 {
1794     if (range.peek().id() == CSSValueAuto)
1795         return consumeIdent(range);
1796     return consumeColor(range, cssParserMode);
1797 }
1798
1799 static RefPtr<CSSValue> consumeOutlineColor(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1800 {
1801     // Allow the special focus color even in HTML Standard parsing mode.
1802     if (range.peek().id() == CSSValueWebkitFocusRingColor)
1803         return consumeIdent(range);
1804     return consumeColor(range, cssParserMode);
1805 }
1806
1807 static RefPtr<CSSPrimitiveValue> consumeLineWidth(CSSParserTokenRange& range, CSSParserMode cssParserMode, UnitlessQuirk unitless)
1808 {
1809     CSSValueID id = range.peek().id();
1810     if (id == CSSValueThin || id == CSSValueMedium || id == CSSValueThick)
1811         return consumeIdent(range);
1812     return consumeLength(range, cssParserMode, ValueRangeNonNegative, unitless);
1813 }
1814
1815 static RefPtr<CSSPrimitiveValue> consumeBorderWidth(CSSParserTokenRange& range, CSSParserMode cssParserMode, UnitlessQuirk unitless)
1816 {
1817     return consumeLineWidth(range, cssParserMode, unitless);
1818 }
1819
1820 static RefPtr<CSSPrimitiveValue> consumeTextStrokeWidth(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1821 {
1822     return consumeLineWidth(range, cssParserMode, UnitlessQuirk::Forbid);
1823 }
1824
1825 static RefPtr<CSSPrimitiveValue> consumeColumnRuleWidth(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1826 {
1827     return consumeLineWidth(range, cssParserMode, UnitlessQuirk::Forbid);
1828 }
1829
1830 static bool consumeTranslate3d(CSSParserTokenRange& args, CSSParserMode cssParserMode, RefPtr<CSSFunctionValue>& transformValue)
1831 {
1832     unsigned numberOfArguments = 2;
1833     RefPtr<CSSValue> parsedValue;
1834     do {
1835         parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeAll);
1836         if (!parsedValue)
1837             return false;
1838         transformValue->append(*parsedValue);
1839         if (!consumeCommaIncludingWhitespace(args))
1840             return false;
1841     } while (--numberOfArguments);
1842     parsedValue = consumeLength(args, cssParserMode, ValueRangeAll);
1843     if (!parsedValue)
1844         return false;
1845     transformValue->append(*parsedValue);
1846     return true;
1847 }
1848
1849 static bool consumeNumbers(CSSParserTokenRange& args, RefPtr<CSSFunctionValue>& transformValue, unsigned numberOfArguments)
1850 {
1851     do {
1852         RefPtr<CSSPrimitiveValue> parsedValue = consumeNumber(args, ValueRangeAll);
1853         if (!parsedValue)
1854             return false;
1855         transformValue->append(parsedValue.releaseNonNull());
1856         if (--numberOfArguments && !consumeCommaIncludingWhitespace(args))
1857             return false;
1858     } while (numberOfArguments);
1859     return true;
1860 }
1861
1862 static bool consumePerspective(CSSParserTokenRange& args, CSSParserMode cssParserMode, RefPtr<CSSFunctionValue>& transformValue)
1863 {
1864     RefPtr<CSSPrimitiveValue> parsedValue = consumeLength(args, cssParserMode, ValueRangeNonNegative);
1865     if (!parsedValue) {
1866         double perspective;
1867         if (!consumeNumberRaw(args, perspective) || perspective < 0)
1868             return false;
1869         parsedValue = CSSPrimitiveValue::create(perspective, CSSPrimitiveValue::UnitType::CSS_PX);
1870     }
1871     if (!parsedValue)
1872         return false;
1873     transformValue->append(parsedValue.releaseNonNull());
1874     return true;
1875 }
1876
1877 static RefPtr<CSSValue> consumeTransformValue(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1878 {
1879     CSSValueID functionId = range.peek().functionId();
1880     if (functionId == CSSValueInvalid)
1881         return nullptr;
1882     CSSParserTokenRange args = consumeFunction(range);
1883     if (args.atEnd())
1884         return nullptr;
1885     
1886     RefPtr<CSSFunctionValue> transformValue = CSSFunctionValue::create(functionId);
1887     RefPtr<CSSValue> parsedValue;
1888     switch (functionId) {
1889     case CSSValueRotate:
1890     case CSSValueRotateX:
1891     case CSSValueRotateY:
1892     case CSSValueRotateZ:
1893     case CSSValueSkewX:
1894     case CSSValueSkewY:
1895     case CSSValueSkew:
1896         parsedValue = consumeAngle(args, cssParserMode, UnitlessQuirk::Forbid);
1897         if (!parsedValue)
1898             return nullptr;
1899         if (functionId == CSSValueSkew && consumeCommaIncludingWhitespace(args)) {
1900             transformValue->append(*parsedValue);
1901             parsedValue = consumeAngle(args, cssParserMode, UnitlessQuirk::Forbid);
1902             if (!parsedValue)
1903                 return nullptr;
1904         }
1905         break;
1906     case CSSValueScaleX:
1907     case CSSValueScaleY:
1908     case CSSValueScaleZ:
1909     case CSSValueScale:
1910         parsedValue = consumeNumber(args, ValueRangeAll);
1911         if (!parsedValue)
1912             return nullptr;
1913         if (functionId == CSSValueScale && consumeCommaIncludingWhitespace(args)) {
1914             transformValue->append(*parsedValue);
1915             parsedValue = consumeNumber(args, ValueRangeAll);
1916             if (!parsedValue)
1917                 return nullptr;
1918         }
1919         break;
1920     case CSSValuePerspective:
1921         if (!consumePerspective(args, cssParserMode, transformValue))
1922             return nullptr;
1923         break;
1924     case CSSValueTranslateX:
1925     case CSSValueTranslateY:
1926     case CSSValueTranslate:
1927         parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeAll);
1928         if (!parsedValue)
1929             return nullptr;
1930         if (functionId == CSSValueTranslate && consumeCommaIncludingWhitespace(args)) {
1931             transformValue->append(*parsedValue);
1932             parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeAll);
1933             if (!parsedValue)
1934                 return nullptr;
1935         }
1936         break;
1937     case CSSValueTranslateZ:
1938         parsedValue = consumeLength(args, cssParserMode, ValueRangeAll);
1939         break;
1940     case CSSValueMatrix:
1941     case CSSValueMatrix3d:
1942         if (!consumeNumbers(args, transformValue, (functionId == CSSValueMatrix3d) ? 16 : 6))
1943             return nullptr;
1944         break;
1945     case CSSValueScale3d:
1946         if (!consumeNumbers(args, transformValue, 3))
1947             return nullptr;
1948         break;
1949     case CSSValueRotate3d:
1950         if (!consumeNumbers(args, transformValue, 3) || !consumeCommaIncludingWhitespace(args))
1951             return nullptr;
1952         parsedValue = consumeAngle(args, cssParserMode, UnitlessQuirk::Forbid);
1953         if (!parsedValue)
1954             return nullptr;
1955         break;
1956     case CSSValueTranslate3d:
1957         if (!consumeTranslate3d(args, cssParserMode, transformValue))
1958             return nullptr;
1959         break;
1960     default:
1961         return nullptr;
1962     }
1963     if (parsedValue)
1964         transformValue->append(*parsedValue);
1965     if (!args.atEnd())
1966         return nullptr;
1967     return transformValue;
1968 }
1969
1970 static RefPtr<CSSValue> consumeTransform(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1971 {
1972     if (range.peek().id() == CSSValueNone)
1973         return consumeIdent(range);
1974
1975     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
1976     do {
1977         RefPtr<CSSValue> parsedTransformValue = consumeTransformValue(range, cssParserMode);
1978         if (!parsedTransformValue)
1979             return nullptr;
1980         list->append(parsedTransformValue.releaseNonNull());
1981     } while (!range.atEnd());
1982
1983     return list;
1984 }
1985
1986 template <CSSValueID start, CSSValueID end>
1987 static RefPtr<CSSPrimitiveValue> consumePositionLonghand(CSSParserTokenRange& range, CSSParserMode cssParserMode)
1988 {
1989     if (range.peek().type() == IdentToken) {
1990         CSSValueID id = range.peek().id();
1991         int percent;
1992         if (id == start)
1993             percent = 0;
1994         else if (id == CSSValueCenter)
1995             percent = 50;
1996         else if (id == end)
1997             percent = 100;
1998         else
1999             return nullptr;
2000         range.consumeIncludingWhitespace();
2001         return CSSPrimitiveValue::create(percent, CSSPrimitiveValue::UnitType::CSS_PERCENTAGE);
2002     }
2003     return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll);
2004 }
2005
2006 static RefPtr<CSSPrimitiveValue> consumePositionX(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2007 {
2008     return consumePositionLonghand<CSSValueLeft, CSSValueRight>(range, cssParserMode);
2009 }
2010
2011 static RefPtr<CSSPrimitiveValue> consumePositionY(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2012 {
2013     return consumePositionLonghand<CSSValueTop, CSSValueBottom>(range, cssParserMode);
2014 }
2015
2016 static RefPtr<CSSValue> consumePaintStroke(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2017 {
2018     if (range.peek().id() == CSSValueNone)
2019         return consumeIdent(range);
2020     RefPtr<CSSPrimitiveValue> url = consumeUrl(range);
2021     if (url) {
2022         RefPtr<CSSValue> parsedValue;
2023         if (range.peek().id() == CSSValueNone)
2024             parsedValue = consumeIdent(range);
2025         else
2026             parsedValue = consumeColor(range, cssParserMode);
2027         if (parsedValue) {
2028             RefPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
2029             values->append(url.releaseNonNull());
2030             values->append(parsedValue.releaseNonNull());
2031             return values;
2032         }
2033         return url;
2034     }
2035     return consumeColor(range, cssParserMode);
2036 }
2037
2038 static RefPtr<CSSValue> consumeGlyphOrientation(CSSParserTokenRange& range, CSSParserMode mode, CSSPropertyID property)
2039 {
2040     if (range.peek().id() == CSSValueAuto) {
2041         if (property == CSSPropertyGlyphOrientationVertical)
2042             return consumeIdent(range);
2043         return nullptr;
2044     }
2045     
2046     return consumeAngle(range, mode, UnitlessQuirk::Allow);
2047 }
2048
2049 static RefPtr<CSSValue> consumePaintOrder(CSSParserTokenRange& range)
2050 {
2051     if (range.peek().id() == CSSValueNormal)
2052         return consumeIdent(range);
2053
2054     Vector<CSSValueID, 3> paintTypeList;
2055     RefPtr<CSSPrimitiveValue> fill;
2056     RefPtr<CSSPrimitiveValue> stroke;
2057     RefPtr<CSSPrimitiveValue> markers;
2058     do {
2059         CSSValueID id = range.peek().id();
2060         if (id == CSSValueFill && !fill)
2061             fill = consumeIdent(range);
2062         else if (id == CSSValueStroke && !stroke)
2063             stroke = consumeIdent(range);
2064         else if (id == CSSValueMarkers && !markers)
2065             markers = consumeIdent(range);
2066         else
2067             return nullptr;
2068         paintTypeList.append(id);
2069     } while (!range.atEnd());
2070
2071     // After parsing we serialize the paint-order list. Since it is not possible to
2072     // pop a last list items from CSSValueList without bigger cost, we create the
2073     // list after parsing.
2074     CSSValueID firstPaintOrderType = paintTypeList.at(0);
2075     RefPtr<CSSValueList> paintOrderList = CSSValueList::createSpaceSeparated();
2076     switch (firstPaintOrderType) {
2077     case CSSValueFill:
2078     case CSSValueStroke:
2079         paintOrderList->append(firstPaintOrderType == CSSValueFill ? fill.releaseNonNull() : stroke.releaseNonNull());
2080         if (paintTypeList.size() > 1) {
2081             if (paintTypeList.at(1) == CSSValueMarkers)
2082                 paintOrderList->append(markers.releaseNonNull());
2083         }
2084         break;
2085     case CSSValueMarkers:
2086         paintOrderList->append(markers.releaseNonNull());
2087         if (paintTypeList.size() > 1) {
2088             if (paintTypeList.at(1) == CSSValueStroke)
2089                 paintOrderList->append(stroke.releaseNonNull());
2090         }
2091         break;
2092     default:
2093         ASSERT_NOT_REACHED();
2094     }
2095
2096     return paintOrderList;
2097 }
2098
2099 static RefPtr<CSSValue> consumeNoneOrURI(CSSParserTokenRange& range)
2100 {
2101     if (range.peek().id() == CSSValueNone)
2102         return consumeIdent(range);
2103     return consumeUrl(range);
2104 }
2105
2106 static RefPtr<CSSValue> consumeFlexBasis(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2107 {
2108     // FIXME: Support intrinsic dimensions too.
2109     if (range.peek().id() == CSSValueAuto)
2110         return consumeIdent(range);
2111     return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
2112 }
2113
2114 static RefPtr<CSSValue> consumeKerning(CSSParserTokenRange& range, CSSParserMode mode)
2115 {
2116     RefPtr<CSSValue> result = consumeIdent<CSSValueAuto, CSSValueNormal>(range);
2117     if (result)
2118         return result;
2119     return consumeLength(range, mode, ValueRangeAll, UnitlessQuirk::Allow);
2120 }
2121
2122 static RefPtr<CSSValue> consumeStrokeDasharray(CSSParserTokenRange& range)
2123 {
2124     CSSValueID id = range.peek().id();
2125     if (id == CSSValueNone)
2126         return consumeIdent(range);
2127
2128     RefPtr<CSSValueList> dashes = CSSValueList::createCommaSeparated();
2129     do {
2130         RefPtr<CSSPrimitiveValue> dash = consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeNonNegative);
2131         if (!dash || (consumeCommaIncludingWhitespace(range) && range.atEnd()))
2132             return nullptr;
2133         dashes->append(dash.releaseNonNull());
2134     } while (!range.atEnd());
2135     return dashes;
2136 }
2137
2138 static RefPtr<CSSPrimitiveValue> consumeBaselineShift(CSSParserTokenRange& range)
2139 {
2140     CSSValueID id = range.peek().id();
2141     if (id == CSSValueBaseline || id == CSSValueSub || id == CSSValueSuper)
2142         return consumeIdent(range);
2143     return consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeAll);
2144 }
2145
2146 static RefPtr<CSSPrimitiveValue> consumeRxOrRy(CSSParserTokenRange& range)
2147 {
2148     // FIXME-NEWPARSER: We don't support auto values when mapping, so for now turn this
2149     // off until we can figure out if we're even supposed to support it.
2150     // if (range.peek().id() == CSSValueAuto)
2151     //     return consumeIdent(range);
2152     return consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeAll, UnitlessQuirk::Forbid);
2153 }
2154
2155 static RefPtr<CSSValue> consumeCursor(CSSParserTokenRange& range, const CSSParserContext& context, bool inQuirksMode)
2156 {
2157     RefPtr<CSSValueList> list;
2158     while (RefPtr<CSSValue> image = consumeImage(range, context, ConsumeGeneratedImage::Forbid)) {
2159         double num;
2160         IntPoint hotSpot(-1, -1);
2161         bool hotSpotSpecified = false;
2162         if (consumeNumberRaw(range, num)) {
2163             hotSpot.setX(int(num));
2164             if (!consumeNumberRaw(range, num))
2165                 return nullptr;
2166             hotSpot.setY(int(num));
2167             hotSpotSpecified = true;
2168         }
2169
2170         if (!list)
2171             list = CSSValueList::createCommaSeparated();
2172
2173         list->append(CSSCursorImageValue::create(image.releaseNonNull(), hotSpotSpecified, hotSpot));
2174         if (!consumeCommaIncludingWhitespace(range))
2175             return nullptr;
2176     }
2177
2178     CSSValueID id = range.peek().id();
2179     RefPtr<CSSValue> cursorType;
2180     if (id == CSSValueHand) {
2181         if (!inQuirksMode) // Non-standard behavior
2182             return nullptr;
2183         cursorType = CSSValuePool::singleton().createIdentifierValue(CSSValuePointer);
2184         range.consumeIncludingWhitespace();
2185     } else if ((id >= CSSValueAuto && id <= CSSValueWebkitZoomOut) || id == CSSValueCopy || id == CSSValueNone) {
2186         cursorType = consumeIdent(range);
2187     } else {
2188         return nullptr;
2189     }
2190
2191     if (!list)
2192         return cursorType;
2193     list->append(cursorType.releaseNonNull());
2194     return list;
2195 }
2196
2197 static RefPtr<CSSValue> consumeAttr(CSSParserTokenRange args, CSSParserContext context)
2198 {
2199     if (args.peek().type() != IdentToken)
2200         return nullptr;
2201     
2202     CSSParserToken token = args.consumeIncludingWhitespace();
2203     auto attrName = token.value().toAtomicString();
2204     if (context.isHTMLDocument)
2205         attrName = attrName.convertToASCIILowercase();
2206
2207     if (!args.atEnd())
2208         return nullptr;
2209
2210     // FIXME-NEWPARSER: We want to use a CSSCustomIdentValue here eventually for the attrName.
2211     // FIXME-NEWPARSER: We want to use a CSSFunctionValue rather than relying on a custom
2212     // attr() primitive value.
2213     return CSSValuePool::singleton().createValue(attrName, CSSPrimitiveValue::CSS_ATTR);
2214 }
2215
2216 static RefPtr<CSSValue> consumeCounterContent(CSSParserTokenRange args, bool counters)
2217 {
2218     RefPtr<CSSPrimitiveValue> identifier = consumeCustomIdent(args);
2219     if (!identifier)
2220         return nullptr;
2221
2222     RefPtr<CSSPrimitiveValue> separator;
2223     if (!counters)
2224         separator = CSSPrimitiveValue::create(String(), CSSPrimitiveValue::UnitType::CSS_STRING);
2225     else {
2226         if (!consumeCommaIncludingWhitespace(args) || args.peek().type() != StringToken)
2227             return nullptr;
2228         separator = CSSPrimitiveValue::create(args.consumeIncludingWhitespace().value().toString(), CSSPrimitiveValue::UnitType::CSS_STRING);
2229     }
2230
2231     RefPtr<CSSPrimitiveValue> listStyle;
2232     if (consumeCommaIncludingWhitespace(args)) {
2233         CSSValueID id = args.peek().id();
2234         if ((id != CSSValueNone && (id < CSSValueDisc || id > CSSValueKatakanaIroha)))
2235             return nullptr;
2236         listStyle = consumeIdent(args);
2237     } else
2238         listStyle = CSSValuePool::singleton().createIdentifierValue(CSSValueDecimal);
2239
2240     if (!args.atEnd())
2241         return nullptr;
2242     
2243     // FIXME-NEWPARSER: Should just have a CSSCounterValue.
2244     return CSSValuePool::singleton().createValue(Counter::create(identifier.releaseNonNull(), listStyle.releaseNonNull(), separator.releaseNonNull()));
2245 }
2246
2247 static RefPtr<CSSValue> consumeContent(CSSParserTokenRange& range, CSSParserContext context)
2248 {
2249     if (identMatches<CSSValueNone, CSSValueNormal>(range.peek().id()))
2250         return consumeIdent(range);
2251
2252     RefPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
2253
2254     do {
2255         RefPtr<CSSValue> parsedValue = consumeImage(range, context);
2256         if (!parsedValue)
2257             parsedValue = consumeIdent<CSSValueOpenQuote, CSSValueCloseQuote, CSSValueNoOpenQuote, CSSValueNoCloseQuote>(range);
2258         if (!parsedValue)
2259             parsedValue = consumeString(range);
2260         if (!parsedValue) {
2261             if (range.peek().functionId() == CSSValueAttr)
2262                 parsedValue = consumeAttr(consumeFunction(range), context);
2263             else if (range.peek().functionId() == CSSValueCounter)
2264                 parsedValue = consumeCounterContent(consumeFunction(range), false);
2265             else if (range.peek().functionId() == CSSValueCounters)
2266                 parsedValue = consumeCounterContent(consumeFunction(range), true);
2267             if (!parsedValue)
2268                 return nullptr;
2269         }
2270         values->append(parsedValue.releaseNonNull());
2271     } while (!range.atEnd());
2272
2273     return values;
2274 }
2275
2276 static RefPtr<CSSPrimitiveValue> consumePerspective(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2277 {
2278     if (range.peek().id() == CSSValueNone)
2279         return consumeIdent(range);
2280     RefPtr<CSSPrimitiveValue> parsedValue = consumeLength(range, cssParserMode, ValueRangeAll);
2281     if (!parsedValue) {
2282         // FIXME: Make this quirk only apply to the webkit prefixed version of the property.
2283         double perspective;
2284         if (!consumeNumberRaw(range, perspective))
2285             return nullptr;
2286         parsedValue = CSSPrimitiveValue::create(perspective, CSSPrimitiveValue::UnitType::CSS_PX);
2287     }
2288     if (parsedValue && (parsedValue->isCalculated() || parsedValue->doubleValue() > 0))
2289         return parsedValue;
2290     return nullptr;
2291 }
2292
2293 #if ENABLE(CSS_SCROLL_SNAP)
2294
2295 static RefPtr<CSSValueList> consumeScrollSnapAlign(CSSParserTokenRange& range)
2296 {
2297     RefPtr<CSSValueList> alignmentValue = CSSValueList::createSpaceSeparated();
2298     if (RefPtr<CSSPrimitiveValue> firstValue = consumeIdent<CSSValueNone, CSSValueStart, CSSValueCenter, CSSValueEnd>(range)) {
2299         alignmentValue->append(firstValue.releaseNonNull());
2300         if (auto secondValue = consumeIdent<CSSValueNone, CSSValueStart, CSSValueCenter, CSSValueEnd>(range))
2301             alignmentValue->append(secondValue.releaseNonNull());
2302     }
2303     return alignmentValue->length() ? alignmentValue : nullptr;
2304 }
2305
2306 static RefPtr<CSSValueList> consumeScrollSnapType(CSSParserTokenRange& range)
2307 {
2308     RefPtr<CSSValueList> typeValue = CSSValueList::createSpaceSeparated();
2309     RefPtr<CSSPrimitiveValue> secondValue;
2310
2311     auto firstValue = consumeIdent<CSSValueX, CSSValueY, CSSValueBlock, CSSValueInline, CSSValueBoth>(range);
2312     if (firstValue)
2313         secondValue = consumeIdent<CSSValueProximity, CSSValueMandatory>(range);
2314     else
2315         firstValue = consumeIdent<CSSValueNone, CSSValueProximity, CSSValueMandatory>(range);
2316
2317     if (!firstValue)
2318         return nullptr;
2319
2320     typeValue->append(firstValue.releaseNonNull());
2321     if (secondValue)
2322         typeValue->append(secondValue.releaseNonNull());
2323
2324     return typeValue;
2325 }
2326
2327 #endif
2328
2329 static RefPtr<CSSValue> consumeBorderRadiusCorner(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2330 {
2331     RefPtr<CSSPrimitiveValue> parsedValue1 = consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
2332     if (!parsedValue1)
2333         return nullptr;
2334     RefPtr<CSSPrimitiveValue> parsedValue2 = consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
2335     if (!parsedValue2)
2336         parsedValue2 = parsedValue1;
2337     return createPrimitiveValuePair(parsedValue1.releaseNonNull(), parsedValue2.releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce);
2338 }
2339
2340 static RefPtr<CSSPrimitiveValue> consumeVerticalAlign(CSSParserTokenRange& range, CSSParserMode cssParserMode)
2341 {
2342     RefPtr<CSSPrimitiveValue> parsedValue = consumeIdentRange(range, CSSValueBaseline, CSSValueWebkitBaselineMiddle);
2343     if (!parsedValue)
2344         parsedValue = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
2345     return parsedValue;
2346 }
2347
2348 static RefPtr<CSSPrimitiveValue> consumeShapeRadius(CSSParserTokenRange& args, CSSParserMode cssParserMode)
2349 {
2350     if (identMatches<CSSValueClosestSide, CSSValueFarthestSide>(args.peek().id()))
2351         return consumeIdent(args);
2352     return consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative);
2353 }
2354
2355 static RefPtr<CSSBasicShapeCircle> consumeBasicShapeCircle(CSSParserTokenRange& args, const CSSParserContext& context)
2356 {
2357     // spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes
2358     // circle( [<shape-radius>]? [at <position>]? )
2359     RefPtr<CSSBasicShapeCircle> shape = CSSBasicShapeCircle::create();
2360     if (RefPtr<CSSPrimitiveValue> radius = consumeShapeRadius(args, context.mode))
2361         shape->setRadius(radius.releaseNonNull());
2362     if (consumeIdent<CSSValueAt>(args)) {
2363         RefPtr<CSSPrimitiveValue> centerX;
2364         RefPtr<CSSPrimitiveValue> centerY;
2365         if (!consumePosition(args, context.mode, UnitlessQuirk::Forbid, centerX, centerY))
2366             return nullptr;
2367         shape->setCenterX(centerX.releaseNonNull());
2368         shape->setCenterY(centerY.releaseNonNull());
2369     }
2370     return shape;
2371 }
2372
2373 static RefPtr<CSSBasicShapeEllipse> consumeBasicShapeEllipse(CSSParserTokenRange& args, const CSSParserContext& context)
2374 {
2375     // spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes
2376     // ellipse( [<shape-radius>{2}]? [at <position>]? )
2377     RefPtr<CSSBasicShapeEllipse> shape = CSSBasicShapeEllipse::create();
2378     if (RefPtr<CSSPrimitiveValue> radiusX = consumeShapeRadius(args, context.mode)) {
2379         shape->setRadiusX(radiusX.releaseNonNull());
2380         if (RefPtr<CSSPrimitiveValue> radiusY = consumeShapeRadius(args, context.mode))
2381             shape->setRadiusY(radiusY.releaseNonNull());
2382     }
2383     if (consumeIdent<CSSValueAt>(args)) {
2384         RefPtr<CSSPrimitiveValue> centerX;
2385         RefPtr<CSSPrimitiveValue> centerY;
2386         if (!consumePosition(args, context.mode, UnitlessQuirk::Forbid, centerX, centerY))
2387             return nullptr;
2388         shape->setCenterX(centerX.releaseNonNull());
2389         shape->setCenterY(centerY.releaseNonNull());
2390     }
2391     return shape;
2392 }
2393
2394 static RefPtr<CSSBasicShapePolygon> consumeBasicShapePolygon(CSSParserTokenRange& args, const CSSParserContext& context)
2395 {
2396     RefPtr<CSSBasicShapePolygon> shape = CSSBasicShapePolygon::create();
2397     if (identMatches<CSSValueEvenodd, CSSValueNonzero>(args.peek().id())) {
2398         shape->setWindRule(args.consumeIncludingWhitespace().id() == CSSValueEvenodd ? WindRule::EvenOdd : WindRule::NonZero);
2399         if (!consumeCommaIncludingWhitespace(args))
2400             return nullptr;
2401     }
2402
2403     do {
2404         RefPtr<CSSPrimitiveValue> xLength = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2405         if (!xLength)
2406             return nullptr;
2407         RefPtr<CSSPrimitiveValue> yLength = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2408         if (!yLength)
2409             return nullptr;
2410         shape->appendPoint(xLength.releaseNonNull(), yLength.releaseNonNull());
2411     } while (consumeCommaIncludingWhitespace(args));
2412     return shape;
2413 }
2414
2415 static RefPtr<CSSBasicShapePath> consumeBasicShapePath(CSSParserTokenRange& args)
2416 {
2417     WindRule windRule = WindRule::NonZero;
2418     if (identMatches<CSSValueEvenodd, CSSValueNonzero>(args.peek().id())) {
2419         windRule = args.consumeIncludingWhitespace().id() == CSSValueEvenodd ? WindRule::EvenOdd : WindRule::NonZero;
2420         if (!consumeCommaIncludingWhitespace(args))
2421             return nullptr;
2422     }
2423
2424     if (args.peek().type() != StringToken)
2425         return nullptr;
2426     
2427     auto byteStream = std::make_unique<SVGPathByteStream>();
2428     if (!buildSVGPathByteStreamFromString(args.consumeIncludingWhitespace().value().toString(), *byteStream, UnalteredParsing))
2429         return nullptr;
2430     
2431     auto shape = CSSBasicShapePath::create(WTFMove(byteStream));
2432     shape->setWindRule(windRule);
2433     
2434     return WTFMove(shape);
2435 }
2436
2437 static void complete4Sides(RefPtr<CSSPrimitiveValue> side[4])
2438 {
2439     if (side[3])
2440         return;
2441     if (!side[2]) {
2442         if (!side[1])
2443             side[1] = side[0];
2444         side[2] = side[0];
2445     }
2446     side[3] = side[1];
2447 }
2448
2449 static bool consumeRadii(RefPtr<CSSPrimitiveValue> horizontalRadii[4], RefPtr<CSSPrimitiveValue> verticalRadii[4], CSSParserTokenRange& range, CSSParserMode cssParserMode, bool useLegacyParsing)
2450 {
2451     unsigned i = 0;
2452     for (; i < 4 && !range.atEnd() && range.peek().type() != DelimiterToken; ++i) {
2453         horizontalRadii[i] = consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
2454         if (!horizontalRadii[i])
2455             return false;
2456     }
2457     if (!horizontalRadii[0])
2458         return false;
2459     if (range.atEnd()) {
2460         // Legacy syntax: -webkit-border-radius: l1 l2; is equivalent to border-radius: l1 / l2;
2461         if (useLegacyParsing && i == 2) {
2462             verticalRadii[0] = horizontalRadii[1];
2463             horizontalRadii[1] = nullptr;
2464         } else {
2465             complete4Sides(horizontalRadii);
2466             for (unsigned i = 0; i < 4; ++i)
2467                 verticalRadii[i] = horizontalRadii[i];
2468             return true;
2469         }
2470     } else {
2471         if (!consumeSlashIncludingWhitespace(range))
2472             return false;
2473         for (i = 0; i < 4 && !range.atEnd(); ++i) {
2474             verticalRadii[i] = consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
2475             if (!verticalRadii[i])
2476                 return false;
2477         }
2478         if (!verticalRadii[0] || !range.atEnd())
2479             return false;
2480     }
2481     complete4Sides(horizontalRadii);
2482     complete4Sides(verticalRadii);
2483     return true;
2484 }
2485
2486 static RefPtr<CSSBasicShapeInset> consumeBasicShapeInset(CSSParserTokenRange& args, const CSSParserContext& context)
2487 {
2488     RefPtr<CSSBasicShapeInset> shape = CSSBasicShapeInset::create();
2489     RefPtr<CSSPrimitiveValue> top = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2490     if (!top)
2491         return nullptr;
2492     RefPtr<CSSPrimitiveValue> right = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2493     RefPtr<CSSPrimitiveValue> bottom;
2494     RefPtr<CSSPrimitiveValue> left;
2495     if (right) {
2496         bottom = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2497         if (bottom)
2498             left = consumeLengthOrPercent(args, context.mode, ValueRangeAll);
2499     }
2500     if (left)
2501         shape->updateShapeSize4Values(top.releaseNonNull(), right.releaseNonNull(), bottom.releaseNonNull(), left.releaseNonNull());
2502     else if (bottom)
2503         shape->updateShapeSize3Values(top.releaseNonNull(), right.releaseNonNull(), bottom.releaseNonNull());
2504     else if (right)
2505         shape->updateShapeSize2Values(top.releaseNonNull(), right.releaseNonNull());
2506     else
2507         shape->updateShapeSize1Value(top.releaseNonNull());
2508
2509     if (consumeIdent<CSSValueRound>(args)) {
2510         RefPtr<CSSPrimitiveValue> horizontalRadii[4] = { 0 };
2511         RefPtr<CSSPrimitiveValue> verticalRadii[4] = { 0 };
2512         if (!consumeRadii(horizontalRadii, verticalRadii, args, context.mode, false))
2513             return nullptr;
2514         shape->setTopLeftRadius(createPrimitiveValuePair(horizontalRadii[0].releaseNonNull(), verticalRadii[0].releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce));
2515         shape->setTopRightRadius(createPrimitiveValuePair(horizontalRadii[1].releaseNonNull(), verticalRadii[1].releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce));
2516         shape->setBottomRightRadius(createPrimitiveValuePair(horizontalRadii[2].releaseNonNull(), verticalRadii[2].releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce));
2517         shape->setBottomLeftRadius(createPrimitiveValuePair(horizontalRadii[3].releaseNonNull(), verticalRadii[3].releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce));
2518     }
2519     return shape;
2520 }
2521
2522 static RefPtr<CSSValue> consumeBasicShape(CSSParserTokenRange& range, const CSSParserContext& context)
2523 {
2524     RefPtr<CSSValue> result;
2525     if (range.peek().type() != FunctionToken)
2526         return nullptr;
2527     CSSValueID id = range.peek().functionId();
2528     CSSParserTokenRange rangeCopy = range;
2529     CSSParserTokenRange args = consumeFunction(rangeCopy);
2530     
2531     // FIXME-NEWPARSER: CSSBasicShape should be a CSSValue, and shapes should not be primitive values.
2532     RefPtr<CSSBasicShape> shape;
2533     if (id == CSSValueCircle)
2534         shape = consumeBasicShapeCircle(args, context);
2535     else if (id == CSSValueEllipse)
2536         shape = consumeBasicShapeEllipse(args, context);
2537     else if (id == CSSValuePolygon)
2538         shape = consumeBasicShapePolygon(args, context);
2539     else if (id == CSSValueInset)
2540         shape = consumeBasicShapeInset(args, context);
2541     else if (id == CSSValuePath)
2542         shape = consumeBasicShapePath(args);
2543     if (!shape)
2544         return nullptr;
2545     range = rangeCopy;
2546     
2547     if (!args.atEnd())
2548         return nullptr;
2549
2550     return CSSValuePool::singleton().createValue(shape.releaseNonNull());
2551 }
2552
2553 static RefPtr<CSSValue> consumeBasicShapeOrBox(CSSParserTokenRange& range, const CSSParserContext& context)
2554 {
2555     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
2556     bool shapeFound = false;
2557     bool boxFound = false;
2558     while (!range.atEnd() && !(shapeFound && boxFound)) {
2559         RefPtr<CSSValue> componentValue;
2560         if (range.peek().type() == FunctionToken && !shapeFound) {
2561             componentValue = consumeBasicShape(range, context);
2562             shapeFound = true;
2563         } else if (range.peek().type() == IdentToken && !boxFound) {
2564             componentValue = consumeIdent<CSSValueContentBox, CSSValuePaddingBox, CSSValueBorderBox, CSSValueMarginBox, CSSValueFillBox, CSSValueStrokeBox, CSSValueViewBox>(range);
2565             boxFound = true;
2566         }
2567         if (!componentValue)
2568             return nullptr;
2569         list->append(componentValue.releaseNonNull());
2570     }
2571     
2572     if (!range.atEnd() || !list->length())
2573         return nullptr;
2574     
2575     return list;
2576 }
2577     
2578 static RefPtr<CSSValue> consumeWebkitClipPath(CSSParserTokenRange& range, const CSSParserContext& context)
2579 {
2580     if (range.peek().id() == CSSValueNone)
2581         return consumeIdent(range);
2582     if (RefPtr<CSSPrimitiveValue> url = consumeUrl(range))
2583         return url;
2584     return consumeBasicShapeOrBox(range, context);
2585 }
2586
2587 static RefPtr<CSSValue> consumeShapeOutside(CSSParserTokenRange& range, const CSSParserContext& context)
2588 {
2589     if (RefPtr<CSSValue> imageValue = consumeImageOrNone(range, context))
2590         return imageValue;
2591     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
2592     if (RefPtr<CSSValue> boxValue = consumeIdent<CSSValueContentBox, CSSValuePaddingBox, CSSValueBorderBox, CSSValueMarginBox>(range))
2593         list->append(boxValue.releaseNonNull());
2594     if (RefPtr<CSSValue> shapeValue = consumeBasicShape(range, context)) {
2595         list->append(shapeValue.releaseNonNull());
2596         if (list->length() < 2) {
2597             if (RefPtr<CSSValue> boxValue = consumeIdent<CSSValueContentBox, CSSValuePaddingBox, CSSValueBorderBox, CSSValueMarginBox>(range))
2598                 list->append(boxValue.releaseNonNull());
2599         }
2600     }
2601     if (!list->length())
2602         return nullptr;
2603     return list;
2604 }
2605
2606 static bool isAuto(CSSValueID id)
2607 {
2608     return identMatches<CSSValueAuto>(id);
2609 }
2610
2611 static bool isNormalOrStretch(CSSValueID id)
2612 {
2613     return identMatches<CSSValueNormal, CSSValueStretch>(id);
2614 }
2615
2616 static bool isLeftOrRightKeyword(CSSValueID id)
2617 {
2618     return identMatches<CSSValueLeft, CSSValueRight>(id);
2619 }
2620
2621 static bool isContentDistributionKeyword(CSSValueID id)
2622 {
2623     return identMatches<CSSValueSpaceBetween, CSSValueSpaceAround, CSSValueSpaceEvenly, CSSValueStretch>(id);
2624 }
2625
2626 static bool isContentPositionKeyword(CSSValueID id)
2627 {
2628     return identMatches<CSSValueStart, CSSValueEnd, CSSValueCenter, CSSValueFlexStart, CSSValueFlexEnd>(id);
2629 }
2630
2631 static bool isContentPositionOrLeftOrRightKeyword(CSSValueID id)
2632 {
2633     return isContentPositionKeyword(id) || isLeftOrRightKeyword(id);
2634 }
2635
2636 static bool isOverflowKeyword(CSSValueID id)
2637 {
2638     return CSSPropertyParserHelpers::identMatches<CSSValueUnsafe, CSSValueSafe>(id);
2639 }
2640
2641 static bool isBaselineKeyword(CSSValueID id)
2642 {
2643     return identMatches<CSSValueFirst, CSSValueLast, CSSValueBaseline>(id);
2644 }
2645
2646 static RefPtr<CSSPrimitiveValue> consumeOverflowPositionKeyword(CSSParserTokenRange& range)
2647 {
2648     return isOverflowKeyword(range.peek().id()) ? consumeIdent(range) : nullptr;
2649 }
2650
2651 static CSSValueID getBaselineKeyword(RefPtr<CSSValue> value)
2652 {
2653     auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
2654     if (primitiveValue.pairValue()) {
2655         ASSERT(primitiveValue.pairValue()->first()->valueID() == CSSValueLast);
2656         ASSERT(primitiveValue.pairValue()->second()->valueID() == CSSValueBaseline);
2657         return CSSValueLastBaseline;
2658     }
2659     ASSERT(primitiveValue.valueID() == CSSValueBaseline);
2660     return CSSValueBaseline;
2661 }
2662
2663 static RefPtr<CSSValue> consumeBaselineKeyword(CSSParserTokenRange& range)
2664 {
2665     RefPtr<CSSPrimitiveValue> preference = consumeIdent<CSSValueFirst, CSSValueLast>(range);
2666     RefPtr<CSSPrimitiveValue> baseline = consumeIdent<CSSValueBaseline>(range);
2667     if (!baseline)
2668         return nullptr;
2669     if (preference && preference->valueID() == CSSValueLast)
2670         return createPrimitiveValuePair(preference.releaseNonNull(), baseline.releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce);
2671     return baseline;
2672 }
2673
2674 using IsPositionKeyword = bool (*)(CSSValueID);
2675
2676 static RefPtr<CSSValue> consumeContentDistributionOverflowPosition(CSSParserTokenRange& range, IsPositionKeyword isPositionKeyword)
2677 {
2678     ASSERT(isPositionKeyword);
2679     CSSValueID id = range.peek().id();
2680     if (identMatches<CSSValueNormal>(id))
2681         return CSSContentDistributionValue::create(CSSValueInvalid, range.consumeIncludingWhitespace().id(), CSSValueInvalid);
2682
2683     if (isBaselineKeyword(id)) {
2684         RefPtr<CSSValue> baseline = consumeBaselineKeyword(range);
2685         if (!baseline)
2686             return nullptr;
2687         return CSSContentDistributionValue::create(CSSValueInvalid, getBaselineKeyword(baseline), CSSValueInvalid);
2688     }
2689
2690     if (isContentDistributionKeyword(id))
2691         return CSSContentDistributionValue::create(range.consumeIncludingWhitespace().id(), CSSValueInvalid, CSSValueInvalid);
2692
2693     CSSValueID overflow = isOverflowKeyword(id) ? range.consumeIncludingWhitespace().id() : CSSValueInvalid;
2694     if (isPositionKeyword(range.peek().id()))
2695         return CSSContentDistributionValue::create(CSSValueInvalid, range.consumeIncludingWhitespace().id(), overflow);
2696
2697     return nullptr;
2698 }
2699
2700 static RefPtr<CSSPrimitiveValue> consumeBorderImageRepeatKeyword(CSSParserTokenRange& range)
2701 {
2702     return consumeIdent<CSSValueStretch, CSSValueRepeat, CSSValueSpace, CSSValueRound>(range);
2703 }
2704
2705 static RefPtr<CSSValue> consumeBorderImageRepeat(CSSParserTokenRange& range)
2706 {
2707     RefPtr<CSSPrimitiveValue> horizontal = consumeBorderImageRepeatKeyword(range);
2708     if (!horizontal)
2709         return nullptr;
2710     RefPtr<CSSPrimitiveValue> vertical = consumeBorderImageRepeatKeyword(range);
2711     if (!vertical)
2712         vertical = horizontal;
2713     return createPrimitiveValuePair(horizontal.releaseNonNull(), vertical.releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce);
2714 }
2715
2716 static RefPtr<CSSValue> consumeBorderImageSlice(CSSPropertyID property, CSSParserTokenRange& range)
2717 {
2718     bool fill = consumeIdent<CSSValueFill>(range);
2719     RefPtr<CSSPrimitiveValue> slices[4] = { 0 };
2720
2721     for (size_t index = 0; index < 4; ++index) {
2722         RefPtr<CSSPrimitiveValue> value = consumePercent(range, ValueRangeNonNegative);
2723         if (!value)
2724             value = consumeNumber(range, ValueRangeNonNegative);
2725         if (!value)
2726             break;
2727         slices[index] = value;
2728     }
2729     if (!slices[0])
2730         return nullptr;
2731     if (consumeIdent<CSSValueFill>(range)) {
2732         if (fill)
2733             return nullptr;
2734         fill = true;
2735     }
2736     complete4Sides(slices);
2737     // FIXME: For backwards compatibility, -webkit-border-image, -webkit-mask-box-image and -webkit-box-reflect have to do a fill by default.
2738     // FIXME: What do we do with -webkit-box-reflect and -webkit-mask-box-image? Probably just have to leave them filling...
2739     if (property == CSSPropertyWebkitBorderImage || property == CSSPropertyWebkitMaskBoxImage || property == CSSPropertyWebkitBoxReflect)
2740         fill = true;
2741     
2742     // Now build a rect value to hold all four of our primitive values.
2743     // FIXME-NEWPARSER: Should just have a CSSQuadValue.
2744     auto quad = Quad::create();
2745     quad->setTop(slices[0].releaseNonNull());
2746     quad->setRight(slices[1].releaseNonNull());
2747     quad->setBottom(slices[2].releaseNonNull());
2748     quad->setLeft(slices[3].releaseNonNull());
2749     
2750     // Make our new border image value now.
2751     return CSSBorderImageSliceValue::create(CSSValuePool::singleton().createValue(WTFMove(quad)), fill);
2752 }
2753
2754 static RefPtr<CSSValue> consumeBorderImageOutset(CSSParserTokenRange& range)
2755 {
2756     RefPtr<CSSPrimitiveValue> outsets[4] = { 0 };
2757
2758     RefPtr<CSSPrimitiveValue> value;
2759     for (size_t index = 0; index < 4; ++index) {
2760         value = consumeNumber(range, ValueRangeNonNegative);
2761         if (!value)
2762             value = consumeLength(range, HTMLStandardMode, ValueRangeNonNegative);
2763         if (!value)
2764             break;
2765         outsets[index] = value;
2766     }
2767     if (!outsets[0])
2768         return nullptr;
2769     complete4Sides(outsets);
2770     
2771     // FIXME-NEWPARSER: Should just have a CSSQuadValue.
2772     auto quad = Quad::create();
2773     quad->setTop(outsets[0].releaseNonNull());
2774     quad->setRight(outsets[1].releaseNonNull());
2775     quad->setBottom(outsets[2].releaseNonNull());
2776     quad->setLeft(outsets[3].releaseNonNull());
2777     
2778     return CSSValuePool::singleton().createValue(WTFMove(quad));
2779 }
2780
2781 static RefPtr<CSSValue> consumeBorderImageWidth(CSSParserTokenRange& range)
2782 {
2783     RefPtr<CSSPrimitiveValue> widths[4];
2784
2785     RefPtr<CSSPrimitiveValue> value;
2786     for (size_t index = 0; index < 4; ++index) {
2787         value = consumeNumber(range, ValueRangeNonNegative);
2788         if (!value)
2789             value = consumeLengthOrPercent(range, HTMLStandardMode, ValueRangeNonNegative, UnitlessQuirk::Forbid);
2790         if (!value)
2791             value = consumeIdent<CSSValueAuto>(range);
2792         if (!value)
2793             break;
2794         widths[index] = value;
2795     }
2796     if (!widths[0])
2797         return nullptr;
2798     complete4Sides(widths);
2799     
2800     // FIXME-NEWPARSER: Should just have a CSSQuadValue.
2801     auto quad = Quad::create();
2802     quad->setTop(widths[0].releaseNonNull());
2803     quad->setRight(widths[1].releaseNonNull());
2804     quad->setBottom(widths[2].releaseNonNull());
2805     quad->setLeft(widths[3].releaseNonNull());
2806     
2807     return CSSValuePool::singleton().createValue(WTFMove(quad));
2808 }
2809
2810 static bool consumeBorderImageComponents(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context, RefPtr<CSSValue>& source,
2811     RefPtr<CSSValue>& slice, RefPtr<CSSValue>& width, RefPtr<CSSValue>& outset, RefPtr<CSSValue>& repeat)
2812 {
2813     do {
2814         if (!source) {
2815             source = consumeImageOrNone(range, context);
2816             if (source)
2817                 continue;
2818         }
2819         if (!repeat) {
2820             repeat = consumeBorderImageRepeat(range);
2821             if (repeat)
2822                 continue;
2823         }
2824         if (!slice) {
2825             slice = consumeBorderImageSlice(property, range);
2826             if (slice) {
2827                 ASSERT(!width && !outset);
2828                 if (consumeSlashIncludingWhitespace(range)) {
2829                     width = consumeBorderImageWidth(range);
2830                     if (consumeSlashIncludingWhitespace(range)) {
2831                         outset = consumeBorderImageOutset(range);
2832                         if (!outset)
2833                             return false;
2834                     } else if (!width) {
2835                         return false;
2836                     }
2837                 }
2838             } else {
2839                 return false;
2840             }
2841         } else {
2842             return false;
2843         }
2844     } while (!range.atEnd());
2845     return true;
2846 }
2847
2848 static RefPtr<CSSValue> consumeWebkitBorderImage(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context)
2849 {
2850     RefPtr<CSSValue> source;
2851     RefPtr<CSSValue> slice;
2852     RefPtr<CSSValue> width;
2853     RefPtr<CSSValue> outset;
2854     RefPtr<CSSValue> repeat;
2855     if (consumeBorderImageComponents(property, range, context, source, slice, width, outset, repeat))
2856         return createBorderImageValue(WTFMove(source), WTFMove(slice), WTFMove(width), WTFMove(outset), WTFMove(repeat));
2857     return nullptr;
2858 }
2859
2860 static RefPtr<CSSValue> consumeReflect(CSSParserTokenRange& range, const CSSParserContext& context)
2861 {
2862     if (range.peek().id() == CSSValueNone)
2863         return consumeIdent(range);
2864     
2865     RefPtr<CSSPrimitiveValue> direction = consumeIdent<CSSValueAbove, CSSValueBelow, CSSValueLeft, CSSValueRight>(range);
2866     if (!direction)
2867         return nullptr;
2868
2869     RefPtr<CSSPrimitiveValue> offset;
2870     if (range.atEnd())
2871         offset = CSSValuePool::singleton().createValue(0, CSSPrimitiveValue::UnitType::CSS_PX);
2872     else {
2873         offset = consumeLengthOrPercent(range, context.mode, ValueRangeAll, UnitlessQuirk::Forbid);
2874         if (!offset)
2875             return nullptr;
2876     }
2877
2878     RefPtr<CSSValue> mask;
2879     if (!range.atEnd()) {
2880         mask = consumeWebkitBorderImage(CSSPropertyWebkitBoxReflect, range, context);
2881         if (!mask)
2882             return nullptr;
2883     }
2884     return CSSReflectValue::create(direction.releaseNonNull(), offset.releaseNonNull(), WTFMove(mask));
2885 }
2886
2887 #if ENABLE(CSS_IMAGE_ORIENTATION)
2888 static RefPtr<CSSValue> consumeImageOrientation(CSSParserTokenRange& range, CSSParserMode cssParserMode, UnitlessQuirk unitless = UnitlessQuirk::Forbid)
2889 {
2890     if (range.peek().type() != NumberToken) {
2891         RefPtr<CSSPrimitiveValue> angle = consumeAngle(range, cssParserMode, unitless);
2892         if (angle && angle->doubleValue() == 0)
2893             return angle;
2894     }
2895     return nullptr;
2896 }
2897 #endif
2898
2899 static RefPtr<CSSPrimitiveValue> consumeBackgroundBlendMode(CSSParserTokenRange& range)
2900 {
2901     CSSValueID id = range.peek().id();
2902     if (id == CSSValueNormal || id == CSSValueOverlay || (id >= CSSValueMultiply && id <= CSSValueLuminosity))
2903         return consumeIdent(range);
2904     return nullptr;
2905 }
2906
2907 static RefPtr<CSSPrimitiveValue> consumeBackgroundAttachment(CSSParserTokenRange& range)
2908 {
2909     return consumeIdent<CSSValueScroll, CSSValueFixed, CSSValueLocal>(range);
2910 }
2911
2912 static RefPtr<CSSPrimitiveValue> consumeBackgroundBox(CSSParserTokenRange& range)
2913 {
2914     return consumeIdent<CSSValueBorderBox, CSSValuePaddingBox, CSSValueContentBox, CSSValueWebkitText>(range);
2915 }
2916
2917 static RefPtr<CSSPrimitiveValue> consumeBackgroundComposite(CSSParserTokenRange& range)
2918 {
2919     return consumeIdentRange(range, CSSValueClear, CSSValuePlusLighter);
2920 }
2921
2922 static RefPtr<CSSPrimitiveValue> consumeWebkitMaskSourceType(CSSParserTokenRange& range)
2923 {
2924     return consumeIdent<CSSValueAuto, CSSValueAlpha, CSSValueLuminance>(range);
2925 }
2926
2927 static RefPtr<CSSPrimitiveValue> consumePrefixedBackgroundBox(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& /*context*/)
2928 {
2929     // The values 'border', 'padding' and 'content' are deprecated and do not apply to the version of the property that has the -webkit- prefix removed.
2930     if (RefPtr<CSSPrimitiveValue> value = consumeIdentRange(range, CSSValueBorder, CSSValuePaddingBox))
2931         return value;
2932     if (range.peek().id() == CSSValueWebkitText || ((property == CSSPropertyWebkitBackgroundClip || property == CSSPropertyWebkitMaskClip) && range.peek().id() == CSSValueText))
2933         return consumeIdent(range);
2934     return nullptr;
2935 }
2936
2937 static RefPtr<CSSPrimitiveValue> consumeBackgroundSize(CSSPropertyID property, CSSParserTokenRange& range, CSSParserMode cssParserMode)
2938 {
2939     if (identMatches<CSSValueContain, CSSValueCover>(range.peek().id()))
2940         return consumeIdent(range);
2941
2942     // FIXME: We're allowing the unitless quirk on this property because our
2943     // tests assume that. Other browser engines don't allow it though.
2944     RefPtr<CSSPrimitiveValue> horizontal = consumeIdent<CSSValueAuto>(range);
2945     if (!horizontal)
2946         horizontal = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
2947
2948     RefPtr<CSSPrimitiveValue> vertical;
2949     if (!range.atEnd()) {
2950         if (range.peek().id() == CSSValueAuto) // `auto' is the default
2951             range.consumeIncludingWhitespace();
2952         else
2953             vertical = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow);
2954     } else if (!vertical && property == CSSPropertyWebkitBackgroundSize) {
2955         // Legacy syntax: "-webkit-background-size: 10px" is equivalent to "background-size: 10px 10px".
2956         vertical = horizontal;
2957     }
2958     if (!vertical)
2959         return horizontal;
2960     return createPrimitiveValuePair(horizontal.releaseNonNull(), vertical.releaseNonNull(), property == CSSPropertyWebkitBackgroundSize ? Pair::IdenticalValueEncoding::Coalesce : Pair::IdenticalValueEncoding::DoNotCoalesce);
2961 }
2962
2963 static RefPtr<CSSValueList> consumeGridAutoFlow(CSSParserTokenRange& range)
2964 {
2965     RefPtr<CSSPrimitiveValue> rowOrColumnValue = consumeIdent<CSSValueRow, CSSValueColumn>(range);
2966     RefPtr<CSSPrimitiveValue> denseAlgorithm = consumeIdent<CSSValueDense>(range);
2967     if (!rowOrColumnValue) {
2968         rowOrColumnValue = consumeIdent<CSSValueRow, CSSValueColumn>(range);
2969         if (!rowOrColumnValue && !denseAlgorithm)
2970             return nullptr;
2971     }
2972     RefPtr<CSSValueList> parsedValues = CSSValueList::createSpaceSeparated();
2973     if (rowOrColumnValue)
2974         parsedValues->append(rowOrColumnValue.releaseNonNull());
2975     if (denseAlgorithm)
2976         parsedValues->append(denseAlgorithm.releaseNonNull());
2977     return parsedValues;
2978 }
2979
2980 static RefPtr<CSSValue> consumeBackgroundComponent(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context)
2981 {
2982     switch (property) {
2983     case CSSPropertyBackgroundClip:
2984         return consumeBackgroundBox(range);
2985     case CSSPropertyBackgroundBlendMode:
2986         return consumeBackgroundBlendMode(range);
2987     case CSSPropertyBackgroundAttachment:
2988         return consumeBackgroundAttachment(range);
2989     case CSSPropertyBackgroundOrigin:
2990         return consumeBackgroundBox(range);
2991     case CSSPropertyWebkitMaskComposite:
2992     case CSSPropertyWebkitBackgroundComposite:
2993         return consumeBackgroundComposite(range);
2994     case CSSPropertyWebkitBackgroundClip:
2995     case CSSPropertyWebkitBackgroundOrigin:
2996     case CSSPropertyWebkitMaskClip:
2997     case CSSPropertyWebkitMaskOrigin:
2998         return consumePrefixedBackgroundBox(property, range, context);
2999     case CSSPropertyBackgroundImage:
3000     case CSSPropertyWebkitMaskImage:
3001         return consumeImageOrNone(range, context);
3002     case CSSPropertyWebkitMaskSourceType:
3003         return consumeWebkitMaskSourceType(range);
3004     case CSSPropertyBackgroundPositionX:
3005     case CSSPropertyWebkitMaskPositionX:
3006         return consumePositionX(range, context.mode);
3007     case CSSPropertyBackgroundPositionY:
3008     case CSSPropertyWebkitMaskPositionY:
3009         return consumePositionY(range, context.mode);
3010     case CSSPropertyBackgroundSize:
3011     case CSSPropertyWebkitBackgroundSize:
3012     case CSSPropertyWebkitMaskSize:
3013         return consumeBackgroundSize(property, range, context.mode);
3014     case CSSPropertyBackgroundColor:
3015         return consumeColor(range, context.mode);
3016     default:
3017         break;
3018     };
3019     return nullptr;
3020 }
3021
3022 static void addBackgroundValue(RefPtr<CSSValue>& list, Ref<CSSValue>&& value)
3023 {
3024     if (list) {
3025         if (!list->isBaseValueList()) {
3026             RefPtr<CSSValue> firstValue = list;
3027             list = CSSValueList::createCommaSeparated();
3028             downcast<CSSValueList>(*list).append(firstValue.releaseNonNull());
3029         }
3030         downcast<CSSValueList>(*list).append(WTFMove(value));
3031     } else {
3032         // To conserve memory we don't actually wrap a single value in a list.
3033         list = WTFMove(value);
3034     }
3035 }
3036
3037 static RefPtr<CSSValue> consumeCommaSeparatedBackgroundComponent(CSSPropertyID property, CSSParserTokenRange& range, const CSSParserContext& context)
3038 {
3039     RefPtr<CSSValue> result;
3040     do {
3041         RefPtr<CSSValue> value = consumeBackgroundComponent(property, range, context);
3042         if (!value)
3043             return nullptr;
3044         addBackgroundValue(result, value.releaseNonNull());
3045     } while (consumeCommaIncludingWhitespace(range));
3046     return result;
3047 }
3048
3049 static bool isSelfPositionKeyword(CSSValueID id)
3050 {
3051     return identMatches<CSSValueStart, CSSValueEnd, CSSValueCenter, CSSValueSelfStart, CSSValueSelfEnd, CSSValueFlexStart, CSSValueFlexEnd>(id);
3052 }
3053
3054 static bool isSelfPositionOrLeftOrRightKeyword(CSSValueID id)
3055 {
3056     return isSelfPositionKeyword(id) || isLeftOrRightKeyword(id);
3057 }
3058
3059 static RefPtr<CSSValue> consumeSelfPositionOverflowPosition(CSSParserTokenRange& range, IsPositionKeyword isPositionKeyword)
3060 {
3061     ASSERT(isPositionKeyword);
3062     CSSValueID id = range.peek().id();
3063     if (isAuto(id) || isNormalOrStretch(id))
3064         return consumeIdent(range);
3065
3066     if (isBaselineKeyword(id))
3067         return consumeBaselineKeyword(range);
3068
3069     RefPtr<CSSPrimitiveValue> overflowPosition = consumeOverflowPositionKeyword(range);
3070     if (!isPositionKeyword(range.peek().id()))
3071         return nullptr;
3072     RefPtr<CSSPrimitiveValue> selfPosition = consumeIdent(range);
3073     if (overflowPosition)
3074         return createPrimitiveValuePair(overflowPosition.releaseNonNull(), selfPosition.releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce);
3075     return selfPosition;
3076 }
3077
3078 static RefPtr<CSSValue> consumeAlignItems(CSSParserTokenRange& range)
3079 {
3080     // align-items property does not allow the 'auto' value.
3081     if (identMatches<CSSValueAuto>(range.peek().id()))
3082         return nullptr;
3083     return consumeSelfPositionOverflowPosition(range, isSelfPositionKeyword);
3084 }
3085
3086 static RefPtr<CSSValue> consumeJustifyItems(CSSParserTokenRange& range)
3087 {
3088     // justify-items property does not allow the 'auto' value.
3089     if (identMatches<CSSValueAuto>(range.peek().id()))
3090         return nullptr;
3091     CSSParserTokenRange rangeCopy = range;
3092     RefPtr<CSSPrimitiveValue> legacy = consumeIdent<CSSValueLegacy>(rangeCopy);
3093     RefPtr<CSSPrimitiveValue> positionKeyword = consumeIdent<CSSValueCenter, CSSValueLeft, CSSValueRight>(rangeCopy);
3094     if (!legacy)
3095         legacy = consumeIdent<CSSValueLegacy>(rangeCopy);
3096     if (legacy) {
3097         range = rangeCopy;
3098         if (positionKeyword)
3099             return createPrimitiveValuePair(legacy.releaseNonNull(), positionKeyword.releaseNonNull(), Pair::IdenticalValueEncoding::Coalesce);
3100         return legacy;
3101     }
3102     return consumeSelfPositionOverflowPosition(range, isSelfPositionOrLeftOrRightKeyword);
3103 }
3104
3105 static RefPtr<CSSValue> consumeFitContent(CSSParserTokenRange& range, CSSParserMode cssParserMode)
3106 {
3107     CSSParserTokenRange rangeCopy = range;
3108     CSSParserTokenRange args = consumeFunction(rangeCopy);
3109     RefPtr<CSSPrimitiveValue> length = consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative, UnitlessQuirk::Allow);
3110     if (!length || !args.atEnd())
3111         return nullptr;
3112     range = rangeCopy;
3113     RefPtr<CSSFunctionValue> result = CSSFunctionValue::create(CSSValueFitContent);
3114     result->append(length.releaseNonNull());
3115     return result;
3116 }
3117
3118 static RefPtr<CSSPrimitiveValue> consumeCustomIdentForGridLine(CSSParserTokenRange& range)
3119 {
3120     if (range.peek().id() == CSSValueAuto || range.peek().id() == CSSValueSpan)
3121         return nullptr;
3122     return consumeCustomIdent(range);
3123 }
3124
3125 static RefPtr<CSSValue> consumeGridLine(CSSParserTokenRange& range)
3126 {
3127     if (range.peek().id() == CSSValueAuto)
3128         return consumeIdent(range);
3129
3130     RefPtr<CSSPrimitiveValue> spanValue;
3131     RefPtr<CSSPrimitiveValue> gridLineName;
3132     RefPtr<CSSPrimitiveValue> numericValue = consumeInteger(range);
3133     if (numericValue) {
3134         gridLineName = consumeCustomIdentForGridLine(range);
3135         spanValue = consumeIdent<CSSValueSpan>(range);
3136     } else {
3137         spanValue = consumeIdent<CSSValueSpan>(range);
3138         if (spanValue) {
3139             numericValue = consumeInteger(range);
3140             gridLineName = consumeCustomIdentForGridLine(range);
3141             if (!numericValue)
3142                 numericValue = consumeInteger(range);
3143         } else {
3144             gridLineName = consumeCustomIdentForGridLine(range);
3145             if (gridLineName) {
3146                 numericValue = consumeInteger(range);
3147                 spanValue = consumeIdent<CSSValueSpan>(range);
3148                 if (!spanValue && !numericValue)
3149                     return gridLineName;
3150             } else {
3151                 return nullptr;
3152             }
3153         }
3154     }
3155
3156     if (spanValue && !numericValue && !gridLineName)
3157         return nullptr; // "span" keyword alone is invalid.
3158     if (spanValue && numericValue && numericValue->intValue() < 0)
3159         return nullptr; // Negative numbers are not allowed for span.
3160     if (numericValue && numericValue->intValue() == 0)
3161         return nullptr; // An <integer> value of zero makes the declaration invalid.
3162
3163     RefPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
3164     if (spanValue)
3165         values->append(spanValue.releaseNonNull());
3166     if (numericValue)
3167         values->append(numericValue.releaseNonNull());
3168     if (gridLineName)
3169         values->append(gridLineName.releaseNonNull());
3170     ASSERT(values->length());
3171     return values;
3172 }
3173
3174 static bool isGridTrackFixedSized(const CSSPrimitiveValue& primitiveValue)
3175 {
3176     CSSValueID valueID = primitiveValue.valueID();
3177     if (valueID == CSSValueMinContent || valueID == CSSValueWebkitMinContent || valueID == CSSValueMaxContent || valueID == CSSValueWebkitMaxContent || valueID == CSSValueAuto || primitiveValue.isFlex())
3178         return false;
3179
3180     return true;
3181 }
3182
3183 static bool isGridTrackFixedSized(const CSSValue& value)
3184 {
3185     if (value.isPrimitiveValue())
3186         return isGridTrackFixedSized(downcast<CSSPrimitiveValue>(value));
3187
3188     ASSERT(value.isFunctionValue());
3189     auto& function = downcast<CSSFunctionValue>(value);
3190     if (function.name() == CSSValueFitContent || function.length() < 2)
3191         return false;
3192
3193     const CSSValue* minPrimitiveValue = downcast<CSSPrimitiveValue>(function.item(0));
3194     const CSSValue* maxPrimitiveValue = downcast<CSSPrimitiveValue>(function.item(1));
3195     return isGridTrackFixedSized(*minPrimitiveValue) || isGridTrackFixedSized(*maxPrimitiveValue);
3196 }
3197
3198 static Vector<String> parseGridTemplateAreasColumnNames(const String& gridRowNames)
3199 {
3200     ASSERT(!gridRowNames.isEmpty());
3201     Vector<String> columnNames;
3202     // Using StringImpl to avoid checks and indirection in every call to String::operator[].
3203     StringImpl& text = *gridRowNames.impl();
3204
3205     StringBuilder areaName;
3206     for (unsigned i = 0; i < text.length(); ++i) {
3207         if (isCSSSpace(text[i])) {
3208             if (!areaName.isEmpty()) {
3209                 columnNames.append(areaName.toString());
3210                 areaName.clear();
3211             }
3212             continue;
3213         }
3214         if (text[i] == '.') {
3215             if (areaName == ".")
3216                 continue;
3217             if (!areaName.isEmpty()) {
3218                 columnNames.append(areaName.toString());
3219                 areaName.clear();
3220             }
3221         } else {
3222             if (!isNameCodePoint(text[i]))
3223                 return Vector<String>();
3224             if (areaName == ".") {
3225                 columnNames.append(areaName.toString());
3226                 areaName.clear();
3227             }
3228         }
3229
3230         areaName.append(text[i]);
3231     }
3232
3233     if (!areaName.isEmpty())
3234         columnNames.append(areaName.toString());
3235
3236     return columnNames;
3237 }
3238
3239 static bool parseGridTemplateAreasRow(const String& gridRowNames, NamedGridAreaMap& gridAreaMap, const size_t rowCount, size_t& columnCount)
3240 {
3241     if (gridRowNames.isAllSpecialCharacters<isCSSSpace>())
3242         return false;
3243
3244     Vector<String> columnNames = parseGridTemplateAreasColumnNames(gridRowNames);
3245     if (rowCount == 0) {
3246         columnCount = columnNames.size();
3247         if (columnCount == 0)
3248             return false;
3249     } else if (columnCount != columnNames.size()) {
3250         // The declaration is invalid if all the rows don't have the number of columns.
3251         return false;
3252     }
3253
3254     for (size_t currentColumn = 0; currentColumn < columnCount; ++currentColumn) {
3255         const String& gridAreaName = columnNames[currentColumn];
3256
3257         // Unamed areas are always valid (we consider them to be 1x1).
3258         if (gridAreaName == ".")
3259             continue;
3260
3261         size_t lookAheadColumn = currentColumn + 1;
3262         while (lookAheadColumn < columnCount && columnNames[lookAheadColumn] == gridAreaName)
3263             lookAheadColumn++;
3264
3265         NamedGridAreaMap::iterator gridAreaIt = gridAreaMap.find(gridAreaName);
3266         if (gridAreaIt == gridAreaMap.end()) {
3267             gridAreaMap.add(gridAreaName, GridArea(GridSpan::translatedDefiniteGridSpan(rowCount, rowCount + 1), GridSpan::translatedDefiniteGridSpan(currentColumn, lookAheadColumn)));
3268         } else {
3269             GridArea& gridArea = gridAreaIt->value;
3270
3271             // The following checks test that the grid area is a single filled-in rectangle.
3272             // 1. The new row is adjacent to the previously parsed row.
3273             if (rowCount != gridArea.rows.endLine())
3274                 return false;
3275
3276             // 2. The new area starts at the same position as the previously parsed area.
3277             if (currentColumn != gridArea.columns.startLine())
3278                 return false;
3279
3280             // 3. The new area ends at the same position as the previously parsed area.
3281             if (lookAheadColumn != gridArea.columns.endLine())
3282                 return false;
3283
3284             gridArea.rows = GridSpan::translatedDefiniteGridSpan(gridArea.rows.startLine(), gridArea.rows.endLine() + 1);
3285         }
3286         currentColumn = lookAheadColumn - 1;
3287     }
3288
3289     return true;
3290 }
3291
3292 static RefPtr<CSSPrimitiveValue> consumeGridBreadth(CSSParserTokenRange& range, CSSParserMode cssParserMode)
3293 {
3294     const CSSParserToken& token = range.peek();
3295     if (identMatches<CSSValueMinContent, CSSValueWebkitMinContent, CSSValueMaxContent, CSSValueWebkitMaxContent, CSSValueAuto>(token.id()))
3296         return consumeIdent(range);
3297     if (token.type() == DimensionToken && token.unitType() == CSSPrimitiveValue::UnitType::CSS_FR) {
3298         if (range.peek().numericValue() < 0)
3299             return nullptr;
3300         return CSSPrimitiveValue::create(range.consumeIncludingWhitespace().numericValue(), CSSPrimitiveValue::UnitType::CSS_FR);
3301     }
3302     return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative, UnitlessQuirk::Allow);
3303 }
3304
3305 static RefPtr<CSSValue> consumeGridTrackSize(CSSParserTokenRange& range, CSSParserMode cssParserMode)
3306 {
3307     const CSSParserToken& token = range.peek();
3308     if (identMatches<CSSValueAuto>(token.id()))
3309         return consumeIdent(range);
3310
3311     if (token.functionId() == CSSValueMinmax) {
3312         CSSParserTokenRange rangeCopy = range;
3313         CSSParserTokenRange args = consumeFunction(rangeCopy);
3314         RefPtr<CSSPrimitiveValue> minTrackBreadth = consumeGridBreadth(args, cssParserMode);
3315         if (!minTrackBreadth || minTrackBreadth->isFlex() || !consumeCommaIncludingWhitespace(args))
3316             return nullptr;
3317         RefPtr<CSSPrimitiveValue> maxTrackBreadth = consumeGridBreadth(args, cssParserMode);
3318         if (!maxTrackBreadth || !args.atEnd())
3319             return nullptr;
3320         range = rangeCopy;
3321         RefPtr<CSSFunctionValue> result = CSSFunctionValue::create(CSSValueMinmax);
3322         result->append(minTrackBreadth.releaseNonNull());
3323         result->append(maxTrackBreadth.releaseNonNull());
3324         return result;
3325     }
3326
3327     if (token.functionId() == CSSValueFitContent)
3328         return consumeFitContent(range, cssParserMode);
3329
3330     return consumeGridBreadth(range, cssParserMode);
3331 }
3332
3333 // Appends to the passed in CSSGridLineNamesValue if any, otherwise creates a new one.
3334 static RefPtr<CSSGridLineNamesValue> consumeGridLineNames(CSSParserTokenRange& range, CSSGridLineNamesValue* lineNames = nullptr)
3335 {
3336     CSSParserTokenRange rangeCopy = range;
3337     if (rangeCopy.consumeIncludingWhitespace().type() != LeftBracketToken)
3338         return nullptr;
3339     
3340     RefPtr<CSSGridLineNamesValue> result = lineNames;
3341     if (!result)
3342         result = CSSGridLineNamesValue::create();
3343     while (RefPtr<CSSPrimitiveValue> lineName = consumeCustomIdentForGridLine(rangeCopy))
3344         result->append(lineName.releaseNonNull());
3345     if (rangeCopy.consumeIncludingWhitespace().type() != RightBracketToken)
3346         return nullptr;
3347     range = rangeCopy;
3348     return result;
3349 }
3350
3351 static bool consumeGridTrackRepeatFunction(CSSParserTokenRange& range, CSSParserMode cssParserMode, CSSValueList& list, bool& isAutoRepeat, bool& allTracksAreFixedSized)
3352 {