blob: 1d6cbb43041bfb82347db13a8a4dc151c12f76ea [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/parser/CSSPropertyParser.h"
#include "core/StylePropertyShorthand.h"
#include "core/css/CSSBasicShapeValues.h"
#include "core/css/CSSBorderImage.h"
#include "core/css/CSSContentDistributionValue.h"
#include "core/css/CSSCounterValue.h"
#include "core/css/CSSCursorImageValue.h"
#include "core/css/CSSCustomIdentValue.h"
#include "core/css/CSSFontFaceSrcValue.h"
#include "core/css/CSSFontFamilyValue.h"
#include "core/css/CSSFontFeatureValue.h"
#include "core/css/CSSFontVariationValue.h"
#include "core/css/CSSFunctionValue.h"
#include "core/css/CSSGridAutoRepeatValue.h"
#include "core/css/CSSGridLineNamesValue.h"
#include "core/css/CSSGridTemplateAreasValue.h"
#include "core/css/CSSIdentifierValue.h"
#include "core/css/CSSInheritedValue.h"
#include "core/css/CSSInitialValue.h"
#include "core/css/CSSPathValue.h"
#include "core/css/CSSPendingSubstitutionValue.h"
#include "core/css/CSSPrimitiveValueMappings.h"
#include "core/css/CSSQuadValue.h"
#include "core/css/CSSReflectValue.h"
#include "core/css/CSSShadowValue.h"
#include "core/css/CSSStringValue.h"
#include "core/css/CSSTimingFunctionValue.h"
#include "core/css/CSSURIValue.h"
#include "core/css/CSSUnicodeRangeValue.h"
#include "core/css/CSSUnsetValue.h"
#include "core/css/CSSValuePair.h"
#include "core/css/CSSVariableReferenceValue.h"
#include "core/css/FontFace.h"
#include "core/css/HashTools.h"
#include "core/css/parser/CSSParserFastPaths.h"
#include "core/css/parser/CSSParserIdioms.h"
#include "core/css/parser/CSSPropertyParserHelpers.h"
#include "core/css/parser/CSSVariableParser.h"
#include "core/css/properties/CSSPropertyDescriptor.h"
#include "core/frame/UseCounter.h"
#include "core/layout/LayoutTheme.h"
#include "core/svg/SVGPathUtilities.h"
#include "wtf/text/StringBuilder.h"
#include <memory>
namespace blink {
using namespace CSSPropertyParserHelpers;
CSSPropertyParser::CSSPropertyParser(
const CSSParserTokenRange& range,
const CSSParserContext& context,
HeapVector<CSSProperty, 256>* parsedProperties)
: m_range(range), m_context(context), m_parsedProperties(parsedProperties) {
m_range.consumeWhitespace();
}
void CSSPropertyParser::addProperty(CSSPropertyID property,
CSSPropertyID currentShorthand,
const CSSValue& value,
bool important,
bool implicit) {
ASSERT(!isPropertyAlias(property));
int shorthandIndex = 0;
bool setFromShorthand = false;
if (currentShorthand) {
Vector<StylePropertyShorthand, 4> shorthands;
getMatchingShorthandsForLonghand(property, &shorthands);
setFromShorthand = true;
if (shorthands.size() > 1)
shorthandIndex =
indexOfShorthandForLonghand(currentShorthand, shorthands);
}
m_parsedProperties->push_back(CSSProperty(
property, value, important, setFromShorthand, shorthandIndex, implicit));
}
void CSSPropertyParser::addExpandedPropertyForValue(CSSPropertyID property,
const CSSValue& value,
bool important) {
const StylePropertyShorthand& shorthand = shorthandForProperty(property);
unsigned shorthandLength = shorthand.length();
ASSERT(shorthandLength);
const CSSPropertyID* longhands = shorthand.properties();
for (unsigned i = 0; i < shorthandLength; ++i)
addProperty(longhands[i], property, value, important);
}
bool CSSPropertyParser::parseValue(
CSSPropertyID unresolvedProperty,
bool important,
const CSSParserTokenRange& range,
const CSSParserContext& context,
HeapVector<CSSProperty, 256>& parsedProperties,
StyleRule::RuleType ruleType) {
int parsedPropertiesSize = parsedProperties.size();
CSSPropertyParser parser(range, context, &parsedProperties);
CSSPropertyID resolvedProperty = resolveCSSPropertyID(unresolvedProperty);
bool parseSuccess;
if (ruleType == StyleRule::Viewport) {
parseSuccess = (RuntimeEnabledFeatures::cssViewportEnabled() ||
isUASheetBehavior(context.mode())) &&
parser.parseViewportDescriptor(resolvedProperty, important);
} else if (ruleType == StyleRule::FontFace) {
parseSuccess = parser.parseFontFaceDescriptor(resolvedProperty);
} else {
parseSuccess = parser.parseValueStart(unresolvedProperty, important);
}
// This doesn't count UA style sheets
if (parseSuccess && context.useCounter())
context.useCounter()->count(context.mode(), unresolvedProperty);
if (!parseSuccess)
parsedProperties.shrink(parsedPropertiesSize);
return parseSuccess;
}
const CSSValue* CSSPropertyParser::parseSingleValue(
CSSPropertyID property,
const CSSParserTokenRange& range,
const CSSParserContext& context) {
CSSPropertyParser parser(range, context, nullptr);
const CSSValue* value = parser.parseSingleValue(property);
if (!value || !parser.m_range.atEnd())
return nullptr;
return value;
}
bool CSSPropertyParser::parseValueStart(CSSPropertyID unresolvedProperty,
bool important) {
if (consumeCSSWideKeyword(unresolvedProperty, important))
return true;
CSSParserTokenRange originalRange = m_range;
CSSPropertyID propertyId = resolveCSSPropertyID(unresolvedProperty);
bool isShorthand = isShorthandProperty(propertyId);
if (isShorthand) {
// Variable references will fail to parse here and will fall out to the
// variable ref parser below.
if (parseShorthand(unresolvedProperty, important))
return true;
} else {
if (const CSSValue* parsedValue = parseSingleValue(unresolvedProperty)) {
if (m_range.atEnd()) {
addProperty(propertyId, CSSPropertyInvalid, *parsedValue, important);
return true;
}
}
}
if (CSSVariableParser::containsValidVariableReferences(originalRange)) {
bool isAnimationTainted = false;
CSSVariableReferenceValue* variable = CSSVariableReferenceValue::create(
CSSVariableData::create(originalRange, isAnimationTainted, true));
if (isShorthand) {
const CSSPendingSubstitutionValue& pendingValue =
*CSSPendingSubstitutionValue::create(propertyId, variable);
addExpandedPropertyForValue(propertyId, pendingValue, important);
} else {
addProperty(propertyId, CSSPropertyInvalid, *variable, important);
}
return true;
}
return false;
}
template <typename CharacterType>
static CSSPropertyID unresolvedCSSPropertyID(const CharacterType* propertyName,
unsigned length) {
if (length == 0)
return CSSPropertyInvalid;
if (length >= 2 && propertyName[0] == '-' && propertyName[1] == '-')
return CSSPropertyVariable;
if (length > maxCSSPropertyNameLength)
return CSSPropertyInvalid;
char buffer[maxCSSPropertyNameLength + 1]; // 1 for null character
for (unsigned i = 0; i != length; ++i) {
CharacterType c = propertyName[i];
if (c == 0 || c >= 0x7F)
return CSSPropertyInvalid; // illegal character
buffer[i] = toASCIILower(c);
}
buffer[length] = '\0';
const char* name = buffer;
const Property* hashTableEntry = findProperty(name, length);
if (!hashTableEntry)
return CSSPropertyInvalid;
CSSPropertyID property = static_cast<CSSPropertyID>(hashTableEntry->id);
if (!CSSPropertyMetadata::isEnabledProperty(property))
return CSSPropertyInvalid;
return property;
}
CSSPropertyID unresolvedCSSPropertyID(const String& string) {
unsigned length = string.length();
return string.is8Bit()
? unresolvedCSSPropertyID(string.characters8(), length)
: unresolvedCSSPropertyID(string.characters16(), length);
}
CSSPropertyID unresolvedCSSPropertyID(StringView string) {
unsigned length = string.length();
return string.is8Bit()
? unresolvedCSSPropertyID(string.characters8(), length)
: unresolvedCSSPropertyID(string.characters16(), length);
}
template <typename CharacterType>
static CSSValueID cssValueKeywordID(const CharacterType* valueKeyword,
unsigned length) {
char buffer[maxCSSValueKeywordLength + 1]; // 1 for null character
for (unsigned i = 0; i != length; ++i) {
CharacterType c = valueKeyword[i];
if (c == 0 || c >= 0x7F)
return CSSValueInvalid; // illegal character
buffer[i] = WTF::toASCIILower(c);
}
buffer[length] = '\0';
const Value* hashTableEntry = findValue(buffer, length);
return hashTableEntry ? static_cast<CSSValueID>(hashTableEntry->id)
: CSSValueInvalid;
}
CSSValueID cssValueKeywordID(StringView string) {
unsigned length = string.length();
if (!length)
return CSSValueInvalid;
if (length > maxCSSValueKeywordLength)
return CSSValueInvalid;
return string.is8Bit() ? cssValueKeywordID(string.characters8(), length)
: cssValueKeywordID(string.characters16(), length);
}
bool CSSPropertyParser::consumeCSSWideKeyword(CSSPropertyID unresolvedProperty,
bool important) {
CSSParserTokenRange rangeCopy = m_range;
CSSValueID id = rangeCopy.consumeIncludingWhitespace().id();
if (!rangeCopy.atEnd())
return false;
CSSValue* value = nullptr;
if (id == CSSValueInitial)
value = CSSInitialValue::create();
else if (id == CSSValueInherit)
value = CSSInheritedValue::create();
else if (id == CSSValueUnset)
value = CSSUnsetValue::create();
else
return false;
CSSPropertyID property = resolveCSSPropertyID(unresolvedProperty);
const StylePropertyShorthand& shorthand = shorthandForProperty(property);
if (!shorthand.length()) {
if (CSSPropertyMetadata::isDescriptorOnly(unresolvedProperty))
return false;
addProperty(property, CSSPropertyInvalid, *value, important);
} else {
addExpandedPropertyForValue(property, *value, important);
}
m_range = rangeCopy;
return true;
}
static CSSFontFeatureValue* consumeFontFeatureTag(CSSParserTokenRange& range) {
// Feature tag name consists of 4-letter characters.
static const unsigned tagNameLength = 4;
const CSSParserToken& token = range.consumeIncludingWhitespace();
// Feature tag name comes first
if (token.type() != StringToken)
return nullptr;
if (token.value().length() != tagNameLength)
return nullptr;
AtomicString tag = token.value().toAtomicString();
for (unsigned i = 0; i < tagNameLength; ++i) {
// Limits the range of characters to 0x20-0x7E, following the tag name rules
// defined in the OpenType specification.
UChar character = tag[i];
if (character < 0x20 || character > 0x7E)
return nullptr;
}
int tagValue = 1;
// Feature tag values could follow: <integer> | on | off
if (range.peek().type() == NumberToken &&
range.peek().numericValueType() == IntegerValueType &&
range.peek().numericValue() >= 0) {
tagValue = clampTo<int>(range.consumeIncludingWhitespace().numericValue());
if (tagValue < 0)
return nullptr;
} else if (range.peek().id() == CSSValueOn ||
range.peek().id() == CSSValueOff) {
tagValue = range.consumeIncludingWhitespace().id() == CSSValueOn;
}
return CSSFontFeatureValue::create(tag, tagValue);
}
static CSSValue* consumeFontFeatureSettings(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
CSSValueList* settings = CSSValueList::createCommaSeparated();
do {
CSSFontFeatureValue* fontFeatureValue = consumeFontFeatureTag(range);
if (!fontFeatureValue)
return nullptr;
settings->append(*fontFeatureValue);
} while (consumeCommaIncludingWhitespace(range));
return settings;
}
static CSSFontVariationValue* consumeFontVariationTag(
CSSParserTokenRange& range) {
// Feature tag name consists of 4-letter characters.
static const unsigned tagNameLength = 4;
const CSSParserToken& token = range.consumeIncludingWhitespace();
// Feature tag name comes first
if (token.type() != StringToken)
return nullptr;
if (token.value().length() != tagNameLength)
return nullptr;
AtomicString tag = token.value().toAtomicString();
for (unsigned i = 0; i < tagNameLength; ++i) {
// Limits the range of characters to 0x20-0x7E, following the tag name rules
// defined in the OpenType specification.
UChar character = tag[i];
if (character < 0x20 || character > 0x7E)
return nullptr;
}
double tagValue = 0;
if (!consumeNumberRaw(range, tagValue))
return nullptr;
return CSSFontVariationValue::create(tag, clampTo<float>(tagValue));
}
static CSSValue* consumeFontVariationSettings(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
CSSValueList* variationSettings = CSSValueList::createCommaSeparated();
do {
CSSFontVariationValue* fontVariationValue = consumeFontVariationTag(range);
if (!fontVariationValue)
return nullptr;
variationSettings->append(*fontVariationValue);
} while (consumeCommaIncludingWhitespace(range));
return variationSettings;
}
static CSSValue* consumePage(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeCustomIdent(range);
}
static CSSValue* consumeQuotes(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSValueList* values = CSSValueList::createSpaceSeparated();
while (!range.atEnd()) {
CSSStringValue* parsedValue = consumeString(range);
if (!parsedValue)
return nullptr;
values->append(*parsedValue);
}
if (values->length() && values->length() % 2 == 0)
return values;
return nullptr;
}
static CSSValue* consumeWebkitHighlight(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
return consumeString(range);
}
class FontVariantLigaturesParser {
STACK_ALLOCATED();
public:
FontVariantLigaturesParser()
: m_sawCommonLigaturesValue(false),
m_sawDiscretionaryLigaturesValue(false),
m_sawHistoricalLigaturesValue(false),
m_sawContextualLigaturesValue(false),
m_result(CSSValueList::createSpaceSeparated()) {}
enum class ParseResult { ConsumedValue, DisallowedValue, UnknownValue };
ParseResult consumeLigature(CSSParserTokenRange& range) {
CSSValueID valueID = range.peek().id();
switch (valueID) {
case CSSValueNoCommonLigatures:
case CSSValueCommonLigatures:
if (m_sawCommonLigaturesValue)
return ParseResult::DisallowedValue;
m_sawCommonLigaturesValue = true;
break;
case CSSValueNoDiscretionaryLigatures:
case CSSValueDiscretionaryLigatures:
if (m_sawDiscretionaryLigaturesValue)
return ParseResult::DisallowedValue;
m_sawDiscretionaryLigaturesValue = true;
break;
case CSSValueNoHistoricalLigatures:
case CSSValueHistoricalLigatures:
if (m_sawHistoricalLigaturesValue)
return ParseResult::DisallowedValue;
m_sawHistoricalLigaturesValue = true;
break;
case CSSValueNoContextual:
case CSSValueContextual:
if (m_sawContextualLigaturesValue)
return ParseResult::DisallowedValue;
m_sawContextualLigaturesValue = true;
break;
default:
return ParseResult::UnknownValue;
}
m_result->append(*consumeIdent(range));
return ParseResult::ConsumedValue;
}
CSSValue* finalizeValue() {
if (!m_result->length())
return CSSIdentifierValue::create(CSSValueNormal);
return m_result.release();
}
private:
bool m_sawCommonLigaturesValue;
bool m_sawDiscretionaryLigaturesValue;
bool m_sawHistoricalLigaturesValue;
bool m_sawContextualLigaturesValue;
Member<CSSValueList> m_result;
};
static CSSValue* consumeFontVariantLigatures(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNormal || range.peek().id() == CSSValueNone)
return consumeIdent(range);
FontVariantLigaturesParser ligaturesParser;
do {
if (ligaturesParser.consumeLigature(range) !=
FontVariantLigaturesParser::ParseResult::ConsumedValue)
return nullptr;
} while (!range.atEnd());
return ligaturesParser.finalizeValue();
}
static CSSIdentifierValue* consumeFontVariantCaps(CSSParserTokenRange& range) {
return consumeIdent<CSSValueNormal, CSSValueSmallCaps, CSSValueAllSmallCaps,
CSSValuePetiteCaps, CSSValueAllPetiteCaps,
CSSValueUnicase, CSSValueTitlingCaps>(range);
}
class FontVariantNumericParser {
STACK_ALLOCATED();
public:
FontVariantNumericParser()
: m_sawNumericFigureValue(false),
m_sawNumericSpacingValue(false),
m_sawNumericFractionValue(false),
m_sawOrdinalValue(false),
m_sawSlashedZeroValue(false),
m_result(CSSValueList::createSpaceSeparated()) {}
enum class ParseResult { ConsumedValue, DisallowedValue, UnknownValue };
ParseResult consumeNumeric(CSSParserTokenRange& range) {
CSSValueID valueID = range.peek().id();
switch (valueID) {
case CSSValueLiningNums:
case CSSValueOldstyleNums:
if (m_sawNumericFigureValue)
return ParseResult::DisallowedValue;
m_sawNumericFigureValue = true;
break;
case CSSValueProportionalNums:
case CSSValueTabularNums:
if (m_sawNumericSpacingValue)
return ParseResult::DisallowedValue;
m_sawNumericSpacingValue = true;
break;
case CSSValueDiagonalFractions:
case CSSValueStackedFractions:
if (m_sawNumericFractionValue)
return ParseResult::DisallowedValue;
m_sawNumericFractionValue = true;
break;
case CSSValueOrdinal:
if (m_sawOrdinalValue)
return ParseResult::DisallowedValue;
m_sawOrdinalValue = true;
break;
case CSSValueSlashedZero:
if (m_sawSlashedZeroValue)
return ParseResult::DisallowedValue;
m_sawSlashedZeroValue = true;
break;
default:
return ParseResult::UnknownValue;
}
m_result->append(*consumeIdent(range));
return ParseResult::ConsumedValue;
}
CSSValue* finalizeValue() {
if (!m_result->length())
return CSSIdentifierValue::create(CSSValueNormal);
return m_result.release();
}
private:
bool m_sawNumericFigureValue;
bool m_sawNumericSpacingValue;
bool m_sawNumericFractionValue;
bool m_sawOrdinalValue;
bool m_sawSlashedZeroValue;
Member<CSSValueList> m_result;
};
static CSSValue* consumeFontVariantNumeric(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
FontVariantNumericParser numericParser;
do {
if (numericParser.consumeNumeric(range) !=
FontVariantNumericParser::ParseResult::ConsumedValue)
return nullptr;
} while (!range.atEnd());
return numericParser.finalizeValue();
}
static CSSIdentifierValue* consumeFontVariantCSS21(CSSParserTokenRange& range) {
return consumeIdent<CSSValueNormal, CSSValueSmallCaps>(range);
}
static CSSValue* consumeFontVariantList(CSSParserTokenRange& range) {
CSSValueList* values = CSSValueList::createCommaSeparated();
do {
if (range.peek().id() == CSSValueAll) {
// FIXME: CSSPropertyParser::parseFontVariant() implements
// the old css3 draft:
// http://www.w3.org/TR/2002/WD-css3-webfonts-20020802/#font-variant
// 'all' is only allowed in @font-face and with no other values.
if (values->length())
return nullptr;
return consumeIdent(range);
}
CSSIdentifierValue* fontVariant = consumeFontVariantCSS21(range);
if (fontVariant)
values->append(*fontVariant);
} while (consumeCommaIncludingWhitespace(range));
if (values->length())
return values;
return nullptr;
}
static CSSIdentifierValue* consumeFontWeight(CSSParserTokenRange& range) {
const CSSParserToken& token = range.peek();
if (token.id() >= CSSValueNormal && token.id() <= CSSValueLighter)
return consumeIdent(range);
if (token.type() != NumberToken ||
token.numericValueType() != IntegerValueType)
return nullptr;
int weight = static_cast<int>(token.numericValue());
if ((weight % 100) || weight < 100 || weight > 900)
return nullptr;
range.consumeIncludingWhitespace();
return CSSIdentifierValue::create(
static_cast<CSSValueID>(CSSValue100 + weight / 100 - 1));
}
static String concatenateFamilyName(CSSParserTokenRange& range) {
StringBuilder builder;
bool addedSpace = false;
const CSSParserToken& firstToken = range.peek();
while (range.peek().type() == IdentToken) {
if (!builder.isEmpty()) {
builder.append(' ');
addedSpace = true;
}
builder.append(range.consumeIncludingWhitespace().value());
}
if (!addedSpace && isCSSWideKeyword(firstToken.value()))
return String();
return builder.toString();
}
static CSSValue* consumeFamilyName(CSSParserTokenRange& range) {
if (range.peek().type() == StringToken)
return CSSFontFamilyValue::create(
range.consumeIncludingWhitespace().value().toString());
if (range.peek().type() != IdentToken)
return nullptr;
String familyName = concatenateFamilyName(range);
if (familyName.isNull())
return nullptr;
return CSSFontFamilyValue::create(familyName);
}
static CSSValue* consumeGenericFamily(CSSParserTokenRange& range) {
return consumeIdentRange(range, CSSValueSerif, CSSValueWebkitBody);
}
static CSSValueList* consumeFontFamily(CSSParserTokenRange& range) {
CSSValueList* list = CSSValueList::createCommaSeparated();
do {
CSSValue* parsedValue = consumeGenericFamily(range);
if (parsedValue) {
list->append(*parsedValue);
} else {
parsedValue = consumeFamilyName(range);
if (parsedValue) {
list->append(*parsedValue);
} else {
return nullptr;
}
}
} while (consumeCommaIncludingWhitespace(range));
return list;
}
static CSSValue* consumeSpacing(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
// TODO(timloh): allow <percentage>s in word-spacing.
return consumeLength(range, cssParserMode, ValueRangeAll,
UnitlessQuirk::Allow);
}
static CSSValue* consumeTabSize(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSPrimitiveValue* parsedValue = consumeInteger(range, 0);
if (parsedValue)
return parsedValue;
return consumeLength(range, cssParserMode, ValueRangeNonNegative);
}
static CSSValue* consumeTextSizeAdjust(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
return consumePercent(range, ValueRangeNonNegative);
}
static CSSValue* consumeFontSize(
CSSParserTokenRange& range,
CSSParserMode cssParserMode,
UnitlessQuirk unitless = UnitlessQuirk::Forbid) {
if (range.peek().id() >= CSSValueXxSmall &&
range.peek().id() <= CSSValueLarger)
return consumeIdent(range);
return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative,
unitless);
}
static CSSValue* consumeLineHeight(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
CSSPrimitiveValue* lineHeight = consumeNumber(range, ValueRangeNonNegative);
if (lineHeight)
return lineHeight;
return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
}
static CSSValueList* consumeRotation(CSSParserTokenRange& range) {
ASSERT(RuntimeEnabledFeatures::cssIndependentTransformPropertiesEnabled());
CSSValueList* list = CSSValueList::createSpaceSeparated();
for (unsigned i = 0; i < 3; i++) { // 3 dimensions of rotation
CSSValue* dimension = consumeNumber(range, ValueRangeAll);
if (!dimension) {
if (i == 0)
break;
return nullptr;
}
list->append(*dimension);
}
CSSValue* rotation = consumeAngle(range);
if (!rotation)
return nullptr;
list->append(*rotation);
return list;
}
static CSSValueList* consumeScale(CSSParserTokenRange& range) {
ASSERT(RuntimeEnabledFeatures::cssIndependentTransformPropertiesEnabled());
CSSValue* scale = consumeNumber(range, ValueRangeAll);
if (!scale)
return nullptr;
CSSValueList* list = CSSValueList::createSpaceSeparated();
list->append(*scale);
scale = consumeNumber(range, ValueRangeAll);
if (scale) {
list->append(*scale);
scale = consumeNumber(range, ValueRangeAll);
if (scale)
list->append(*scale);
}
return list;
}
static CSSValue* consumeCounter(CSSParserTokenRange& range, int defaultValue) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
do {
CSSCustomIdentValue* counterName = consumeCustomIdent(range);
if (!counterName)
return nullptr;
int i = defaultValue;
if (CSSPrimitiveValue* counterValue = consumeInteger(range))
i = clampTo<int>(counterValue->getDoubleValue());
list->append(*CSSValuePair::create(
counterName,
CSSPrimitiveValue::create(i, CSSPrimitiveValue::UnitType::Integer),
CSSValuePair::DropIdenticalValues));
} while (!range.atEnd());
return list;
}
static CSSValue* consumePageSize(CSSParserTokenRange& range) {
return consumeIdent<CSSValueA3, CSSValueA4, CSSValueA5, CSSValueB4,
CSSValueB5, CSSValueLedger, CSSValueLegal,
CSSValueLetter>(range);
}
static CSSValueList* consumeSize(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSValueList* result = CSSValueList::createSpaceSeparated();
if (range.peek().id() == CSSValueAuto) {
result->append(*consumeIdent(range));
return result;
}
if (CSSValue* width =
consumeLength(range, cssParserMode, ValueRangeNonNegative)) {
CSSValue* height =
consumeLength(range, cssParserMode, ValueRangeNonNegative);
result->append(*width);
if (height)
result->append(*height);
return result;
}
CSSValue* pageSize = consumePageSize(range);
CSSValue* orientation =
consumeIdent<CSSValuePortrait, CSSValueLandscape>(range);
if (!pageSize)
pageSize = consumePageSize(range);
if (!orientation && !pageSize)
return nullptr;
if (pageSize)
result->append(*pageSize);
if (orientation)
result->append(*orientation);
return result;
}
static CSSValue* consumeSnapHeight(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSPrimitiveValue* unit =
consumeLength(range, cssParserMode, ValueRangeNonNegative);
if (!unit)
return nullptr;
CSSValueList* list = CSSValueList::createSpaceSeparated();
list->append(*unit);
if (CSSPrimitiveValue* position = consumePositiveInteger(range)) {
if (position->getIntValue() > 100)
return nullptr;
list->append(*position);
}
return list;
}
static CSSValue* consumeTextIndent(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
// [ <length> | <percentage> ] && hanging? && each-line?
// Keywords only allowed when css3Text is enabled.
CSSValueList* list = CSSValueList::createSpaceSeparated();
bool hasLengthOrPercentage = false;
bool hasEachLine = false;
bool hasHanging = false;
do {
if (!hasLengthOrPercentage) {
if (CSSValue* textIndent = consumeLengthOrPercent(
range, cssParserMode, ValueRangeAll, UnitlessQuirk::Allow)) {
list->append(*textIndent);
hasLengthOrPercentage = true;
continue;
}
}
if (RuntimeEnabledFeatures::css3TextEnabled()) {
CSSValueID id = range.peek().id();
if (!hasEachLine && id == CSSValueEachLine) {
list->append(*consumeIdent(range));
hasEachLine = true;
continue;
}
if (!hasHanging && id == CSSValueHanging) {
list->append(*consumeIdent(range));
hasHanging = true;
continue;
}
}
return nullptr;
} while (!range.atEnd());
if (!hasLengthOrPercentage)
return nullptr;
return list;
}
static bool validWidthOrHeightKeyword(CSSValueID id,
const CSSParserContext& context) {
if (id == CSSValueWebkitMinContent || id == CSSValueWebkitMaxContent ||
id == CSSValueWebkitFillAvailable || id == CSSValueWebkitFitContent ||
id == CSSValueMinContent || id == CSSValueMaxContent ||
id == CSSValueFitContent) {
if (context.useCounter()) {
switch (id) {
case CSSValueWebkitMinContent:
context.useCounter()->count(UseCounter::CSSValuePrefixedMinContent);
break;
case CSSValueWebkitMaxContent:
context.useCounter()->count(UseCounter::CSSValuePrefixedMaxContent);
break;
case CSSValueWebkitFillAvailable:
context.useCounter()->count(
UseCounter::CSSValuePrefixedFillAvailable);
break;
case CSSValueWebkitFitContent:
context.useCounter()->count(UseCounter::CSSValuePrefixedFitContent);
break;
default:
break;
}
}
return true;
}
return false;
}
static CSSValue* consumeMaxWidthOrHeight(
CSSParserTokenRange& range,
const CSSParserContext& context,
UnitlessQuirk unitless = UnitlessQuirk::Forbid) {
if (range.peek().id() == CSSValueNone ||
validWidthOrHeightKeyword(range.peek().id(), context))
return consumeIdent(range);
return consumeLengthOrPercent(range, context.mode(), ValueRangeNonNegative,
unitless);
}
static CSSValue* consumeWidthOrHeight(
CSSParserTokenRange& range,
const CSSParserContext& context,
UnitlessQuirk unitless = UnitlessQuirk::Forbid) {
if (range.peek().id() == CSSValueAuto ||
validWidthOrHeightKeyword(range.peek().id(), context))
return consumeIdent(range);
return consumeLengthOrPercent(range, context.mode(), ValueRangeNonNegative,
unitless);
}
static CSSValue* consumeMarginOrOffset(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
UnitlessQuirk unitless) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll, unitless);
}
static CSSValue* consumeClipComponent(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeLength(range, cssParserMode, ValueRangeAll,
UnitlessQuirk::Allow);
}
static CSSValue* consumeClip(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
if (range.peek().functionId() != CSSValueRect)
return nullptr;
CSSParserTokenRange args = consumeFunction(range);
// rect(t, r, b, l) || rect(t r b l)
CSSValue* top = consumeClipComponent(args, cssParserMode);
if (!top)
return nullptr;
bool needsComma = consumeCommaIncludingWhitespace(args);
CSSValue* right = consumeClipComponent(args, cssParserMode);
if (!right || (needsComma && !consumeCommaIncludingWhitespace(args)))
return nullptr;
CSSValue* bottom = consumeClipComponent(args, cssParserMode);
if (!bottom || (needsComma && !consumeCommaIncludingWhitespace(args)))
return nullptr;
CSSValue* left = consumeClipComponent(args, cssParserMode);
if (!left || !args.atEnd())
return nullptr;
return CSSQuadValue::create(top, right, bottom, left,
CSSQuadValue::SerializeAsRect);
}
static bool consumePan(CSSParserTokenRange& range,
CSSValue*& panX,
CSSValue*& panY,
CSSValue*& pinchZoom) {
CSSValueID id = range.peek().id();
if ((id == CSSValuePanX || id == CSSValuePanRight || id == CSSValuePanLeft) &&
!panX) {
if (id != CSSValuePanX &&
!RuntimeEnabledFeatures::cssTouchActionPanDirectionsEnabled())
return false;
panX = consumeIdent(range);
} else if ((id == CSSValuePanY || id == CSSValuePanDown ||
id == CSSValuePanUp) &&
!panY) {
if (id != CSSValuePanY &&
!RuntimeEnabledFeatures::cssTouchActionPanDirectionsEnabled())
return false;
panY = consumeIdent(range);
} else if (id == CSSValuePinchZoom && !pinchZoom &&
RuntimeEnabledFeatures::cssTouchActionPinchZoomEnabled()) {
pinchZoom = consumeIdent(range);
} else {
return false;
}
return true;
}
static CSSValue* consumeTouchAction(CSSParserTokenRange& range) {
CSSValueList* list = CSSValueList::createSpaceSeparated();
CSSValueID id = range.peek().id();
if (id == CSSValueAuto || id == CSSValueNone || id == CSSValueManipulation) {
list->append(*consumeIdent(range));
return list;
}
CSSValue* panX = nullptr;
CSSValue* panY = nullptr;
CSSValue* pinchZoom = nullptr;
if (!consumePan(range, panX, panY, pinchZoom))
return nullptr;
if (!range.atEnd() && !consumePan(range, panX, panY, pinchZoom))
return nullptr;
if (!range.atEnd() && !consumePan(range, panX, panY, pinchZoom))
return nullptr;
if (panX)
list->append(*panX);
if (panY)
list->append(*panY);
if (pinchZoom)
list->append(*pinchZoom);
return list;
}
static CSSPrimitiveValue* consumeLineClamp(CSSParserTokenRange& range) {
if (range.peek().type() != PercentageToken &&
range.peek().type() != NumberToken)
return nullptr;
CSSPrimitiveValue* clampValue = consumePercent(range, ValueRangeNonNegative);
if (clampValue)
return clampValue;
// When specifying number of lines, don't allow 0 as a valid value.
return consumePositiveInteger(range);
}
static CSSValue* consumeLocale(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeString(range);
}
static CSSValue* consumeColumnWidth(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
// Always parse lengths in strict mode here, since it would be ambiguous
// otherwise when used in the 'columns' shorthand property.
CSSPrimitiveValue* columnWidth =
consumeLength(range, HTMLStandardMode, ValueRangeNonNegative);
if (!columnWidth ||
(!columnWidth->isCalculated() && columnWidth->getDoubleValue() == 0))
return nullptr;
return columnWidth;
}
static CSSValue* consumeColumnCount(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumePositiveInteger(range);
}
static CSSValue* consumeColumnGap(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
return consumeLength(range, cssParserMode, ValueRangeNonNegative);
}
static CSSValue* consumeColumnSpan(CSSParserTokenRange& range) {
return consumeIdent<CSSValueAll, CSSValueNone>(range);
}
static CSSValue* consumeZoom(CSSParserTokenRange& range,
const CSSParserContext& context) {
const CSSParserToken& token = range.peek();
CSSValue* zoom = nullptr;
if (token.type() == IdentToken) {
zoom = consumeIdent<CSSValueNormal, CSSValueReset, CSSValueDocument>(range);
} else {
zoom = consumePercent(range, ValueRangeNonNegative);
if (!zoom)
zoom = consumeNumber(range, ValueRangeNonNegative);
}
if (zoom && context.useCounter()) {
if (!(token.id() == CSSValueNormal ||
(token.type() == NumberToken &&
toCSSPrimitiveValue(zoom)->getDoubleValue() == 1) ||
(token.type() == PercentageToken &&
toCSSPrimitiveValue(zoom)->getDoubleValue() == 100)))
context.useCounter()->count(UseCounter::CSSZoomNotEqualToOne);
if (token.id() == CSSValueReset)
context.useCounter()->count(UseCounter::CSSZoomReset);
if (token.id() == CSSValueDocument)
context.useCounter()->count(UseCounter::CSSZoomDocument);
}
return zoom;
}
static CSSValue* consumeAnimationIterationCount(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueInfinite)
return consumeIdent(range);
return consumeNumber(range, ValueRangeNonNegative);
}
static CSSValue* consumeAnimationName(CSSParserTokenRange& range,
const CSSParserContext& context,
bool allowQuotedName) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
if (allowQuotedName && range.peek().type() == StringToken) {
// Legacy support for strings in prefixed animations.
if (context.useCounter())
context.useCounter()->count(UseCounter::QuotedAnimationName);
const CSSParserToken& token = range.consumeIncludingWhitespace();
if (equalIgnoringASCIICase(token.value(), "none"))
return CSSIdentifierValue::create(CSSValueNone);
return CSSCustomIdentValue::create(token.value().toAtomicString());
}
return consumeCustomIdent(range);
}
static CSSValue* consumeTransitionProperty(CSSParserTokenRange& range) {
const CSSParserToken& token = range.peek();
if (token.type() != IdentToken)
return nullptr;
if (token.id() == CSSValueNone)
return consumeIdent(range);
CSSPropertyID unresolvedProperty = token.parseAsUnresolvedCSSPropertyID();
if (unresolvedProperty != CSSPropertyInvalid &&
unresolvedProperty != CSSPropertyVariable) {
DCHECK(CSSPropertyMetadata::isEnabledProperty(unresolvedProperty));
range.consumeIncludingWhitespace();
return CSSCustomIdentValue::create(unresolvedProperty);
}
return consumeCustomIdent(range);
}
static CSSValue* consumeSteps(CSSParserTokenRange& range) {
ASSERT(range.peek().functionId() == CSSValueSteps);
CSSParserTokenRange rangeCopy = range;
CSSParserTokenRange args = consumeFunction(rangeCopy);
CSSPrimitiveValue* steps = consumePositiveInteger(args);
if (!steps)
return nullptr;
StepsTimingFunction::StepPosition position =
StepsTimingFunction::StepPosition::END;
if (consumeCommaIncludingWhitespace(args)) {
switch (args.consumeIncludingWhitespace().id()) {
case CSSValueMiddle:
if (!RuntimeEnabledFeatures::webAnimationsAPIEnabled())
return nullptr;
position = StepsTimingFunction::StepPosition::MIDDLE;
break;
case CSSValueStart:
position = StepsTimingFunction::StepPosition::START;
break;
case CSSValueEnd:
position = StepsTimingFunction::StepPosition::END;
break;
default:
return nullptr;
}
}
if (!args.atEnd())
return nullptr;
range = rangeCopy;
return CSSStepsTimingFunctionValue::create(steps->getIntValue(), position);
}
static CSSValue* consumeCubicBezier(CSSParserTokenRange& range) {
ASSERT(range.peek().functionId() == CSSValueCubicBezier);
CSSParserTokenRange rangeCopy = range;
CSSParserTokenRange args = consumeFunction(rangeCopy);
double x1, y1, x2, y2;
if (consumeNumberRaw(args, x1) && x1 >= 0 && x1 <= 1 &&
consumeCommaIncludingWhitespace(args) && consumeNumberRaw(args, y1) &&
consumeCommaIncludingWhitespace(args) && consumeNumberRaw(args, x2) &&
x2 >= 0 && x2 <= 1 && consumeCommaIncludingWhitespace(args) &&
consumeNumberRaw(args, y2) && args.atEnd()) {
range = rangeCopy;
return CSSCubicBezierTimingFunctionValue::create(x1, y1, x2, y2);
}
return nullptr;
}
static CSSValue* consumeAnimationTimingFunction(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueEase || id == CSSValueLinear || id == CSSValueEaseIn ||
id == CSSValueEaseOut || id == CSSValueEaseInOut ||
id == CSSValueStepStart || id == CSSValueStepEnd ||
id == CSSValueStepMiddle)
return consumeIdent(range);
CSSValueID function = range.peek().functionId();
if (function == CSSValueSteps)
return consumeSteps(range);
if (function == CSSValueCubicBezier)
return consumeCubicBezier(range);
return nullptr;
}
static CSSValue* consumeAnimationValue(CSSPropertyID property,
CSSParserTokenRange& range,
const CSSParserContext& context,
bool useLegacyParsing) {
switch (property) {
case CSSPropertyAnimationDelay:
case CSSPropertyTransitionDelay:
return consumeTime(range, ValueRangeAll);
case CSSPropertyAnimationDirection:
return consumeIdent<CSSValueNormal, CSSValueAlternate, CSSValueReverse,
CSSValueAlternateReverse>(range);
case CSSPropertyAnimationDuration:
case CSSPropertyTransitionDuration:
return consumeTime(range, ValueRangeNonNegative);
case CSSPropertyAnimationFillMode:
return consumeIdent<CSSValueNone, CSSValueForwards, CSSValueBackwards,
CSSValueBoth>(range);
case CSSPropertyAnimationIterationCount:
return consumeAnimationIterationCount(range);
case CSSPropertyAnimationName:
return consumeAnimationName(range, context, useLegacyParsing);
case CSSPropertyAnimationPlayState:
return consumeIdent<CSSValueRunning, CSSValuePaused>(range);
case CSSPropertyTransitionProperty:
return consumeTransitionProperty(range);
case CSSPropertyAnimationTimingFunction:
case CSSPropertyTransitionTimingFunction:
return consumeAnimationTimingFunction(range);
default:
ASSERT_NOT_REACHED();
return nullptr;
}
}
static bool isValidAnimationPropertyList(CSSPropertyID property,
const CSSValueList& valueList) {
if (property != CSSPropertyTransitionProperty || valueList.length() < 2)
return true;
for (auto& value : valueList) {
if (value->isIdentifierValue() &&
toCSSIdentifierValue(*value).getValueID() == CSSValueNone)
return false;
}
return true;
}
static CSSValueList* consumeAnimationPropertyList(
CSSPropertyID property,
CSSParserTokenRange& range,
const CSSParserContext& context,
bool useLegacyParsing) {
CSSValueList* list = CSSValueList::createCommaSeparated();
do {
CSSValue* value =
consumeAnimationValue(property, range, context, useLegacyParsing);
if (!value)
return nullptr;
list->append(*value);
} while (consumeCommaIncludingWhitespace(range));
if (!isValidAnimationPropertyList(property, *list))
return nullptr;
ASSERT(list->length());
return list;
}
bool CSSPropertyParser::consumeAnimationShorthand(
const StylePropertyShorthand& shorthand,
bool useLegacyParsing,
bool important) {
const unsigned longhandCount = shorthand.length();
CSSValueList* longhands[8];
ASSERT(longhandCount <= 8);
for (size_t i = 0; i < longhandCount; ++i)
longhands[i] = CSSValueList::createCommaSeparated();
do {
bool parsedLonghand[8] = {false};
do {
bool foundProperty = false;
for (size_t i = 0; i < longhandCount; ++i) {
if (parsedLonghand[i])
continue;
if (CSSValue* value =
consumeAnimationValue(shorthand.properties()[i], m_range,
m_context, useLegacyParsing)) {
parsedLonghand[i] = true;
foundProperty = true;
longhands[i]->append(*value);
break;
}
}
if (!foundProperty)
return false;
} while (!m_range.atEnd() && m_range.peek().type() != CommaToken);
// TODO(timloh): This will make invalid longhands, see crbug.com/386459
for (size_t i = 0; i < longhandCount; ++i) {
if (!parsedLonghand[i])
longhands[i]->append(*CSSInitialValue::create());
parsedLonghand[i] = false;
}
} while (consumeCommaIncludingWhitespace(m_range));
for (size_t i = 0; i < longhandCount; ++i) {
if (!isValidAnimationPropertyList(shorthand.properties()[i], *longhands[i]))
return false;
}
for (size_t i = 0; i < longhandCount; ++i)
addProperty(shorthand.properties()[i], shorthand.id(), *longhands[i],
important);
return m_range.atEnd();
}
static CSSValue* consumeZIndex(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeInteger(range);
}
static CSSShadowValue* parseSingleShadow(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
bool allowInset,
bool allowSpread) {
CSSIdentifierValue* style = nullptr;
CSSValue* color = nullptr;
if (range.atEnd())
return nullptr;
if (range.peek().id() == CSSValueInset) {
if (!allowInset)
return nullptr;
style = consumeIdent(range);
}
color = consumeColor(range, cssParserMode);
CSSPrimitiveValue* horizontalOffset =
consumeLength(range, cssParserMode, ValueRangeAll);
if (!horizontalOffset)
return nullptr;
CSSPrimitiveValue* verticalOffset =
consumeLength(range, cssParserMode, ValueRangeAll);
if (!verticalOffset)
return nullptr;
CSSPrimitiveValue* blurRadius =
consumeLength(range, cssParserMode, ValueRangeAll);
CSSPrimitiveValue* spreadDistance = nullptr;
if (blurRadius) {
// Blur radius must be non-negative.
if (blurRadius->getDoubleValue() < 0)
return nullptr;
if (allowSpread)
spreadDistance = consumeLength(range, cssParserMode, ValueRangeAll);
}
if (!range.atEnd()) {
if (!color)
color = consumeColor(range, cssParserMode);
if (range.peek().id() == CSSValueInset) {
if (!allowInset || style)
return nullptr;
style = consumeIdent(range);
}
}
return CSSShadowValue::create(horizontalOffset, verticalOffset, blurRadius,
spreadDistance, style, color);
}
static CSSValue* consumeShadow(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
bool isBoxShadowProperty) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSValueList* shadowValueList = CSSValueList::createCommaSeparated();
do {
if (CSSShadowValue* shadowValue = parseSingleShadow(
range, cssParserMode, isBoxShadowProperty, isBoxShadowProperty))
shadowValueList->append(*shadowValue);
else
return nullptr;
} while (consumeCommaIncludingWhitespace(range));
return shadowValueList;
}
static CSSFunctionValue* consumeFilterFunction(
CSSParserTokenRange& range,
const CSSParserContext& context) {
CSSValueID filterType = range.peek().functionId();
if (filterType < CSSValueInvert || filterType > CSSValueDropShadow)
return nullptr;
CSSParserTokenRange args = consumeFunction(range);
CSSFunctionValue* filterValue = CSSFunctionValue::create(filterType);
CSSValue* parsedValue = nullptr;
if (filterType == CSSValueDropShadow) {
parsedValue = parseSingleShadow(args, context.mode(), false, false);
} else {
if (args.atEnd()) {
if (context.useCounter())
context.useCounter()->count(UseCounter::CSSFilterFunctionNoArguments);
return filterValue;
}
if (filterType == CSSValueBrightness) {
// FIXME (crbug.com/397061): Support calc expressions like calc(10% + 0.5)
parsedValue = consumePercent(args, ValueRangeAll);
if (!parsedValue)
parsedValue = consumeNumber(args, ValueRangeAll);
} else if (filterType == CSSValueHueRotate) {
parsedValue = consumeAngle(args);
} else if (filterType == CSSValueBlur) {
parsedValue =
consumeLength(args, HTMLStandardMode, ValueRangeNonNegative);
} else {
// FIXME (crbug.com/397061): Support calc expressions like calc(10% + 0.5)
parsedValue = consumePercent(args, ValueRangeNonNegative);
if (!parsedValue)
parsedValue = consumeNumber(args, ValueRangeNonNegative);
if (parsedValue && filterType != CSSValueSaturate &&
filterType != CSSValueContrast) {
bool isPercentage = toCSSPrimitiveValue(parsedValue)->isPercentage();
double maxAllowed = isPercentage ? 100.0 : 1.0;
if (toCSSPrimitiveValue(parsedValue)->getDoubleValue() > maxAllowed) {
parsedValue = CSSPrimitiveValue::create(
maxAllowed, isPercentage ? CSSPrimitiveValue::UnitType::Percentage
: CSSPrimitiveValue::UnitType::Number);
}
}
}
}
if (!parsedValue || !args.atEnd())
return nullptr;
filterValue->append(*parsedValue);
return filterValue;
}
static CSSValue* consumeFilter(CSSParserTokenRange& range,
const CSSParserContext& context) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
do {
CSSValue* filterValue = consumeUrl(range);
if (!filterValue) {
filterValue = consumeFilterFunction(range, context);
if (!filterValue)
return nullptr;
}
list->append(*filterValue);
} while (!range.atEnd());
return list;
}
static CSSValue* consumeTextDecorationLine(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueNone)
return consumeIdent(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
while (true) {
CSSIdentifierValue* ident =
consumeIdent<CSSValueBlink, CSSValueUnderline, CSSValueOverline,
CSSValueLineThrough>(range);
if (!ident)
break;
if (list->hasValue(*ident))
return nullptr;
list->append(*ident);
}
if (!list->length())
return nullptr;
return list;
}
static CSSValue* consumeTextDecorationSkip(CSSParserTokenRange& range) {
CSSValueList* list = CSSValueList::createSpaceSeparated();
while (true) {
CSSIdentifierValue* ident =
consumeIdent<CSSValueObjects, CSSValueInk>(range);
if (!ident)
break;
if (list->hasValue(*ident))
return nullptr;
list->append(*ident);
}
if (!list->length())
return nullptr;
return list;
}
// none | strict | content | [ layout || style || paint || size ]
static CSSValue* consumeContain(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueNone)
return consumeIdent(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
if (id == CSSValueStrict || id == CSSValueContent) {
list->append(*consumeIdent(range));
return list;
}
while (true) {
CSSIdentifierValue* ident =
consumeIdent<CSSValuePaint, CSSValueLayout, CSSValueStyle,
CSSValueSize>(range);
if (!ident)
break;
if (list->hasValue(*ident))
return nullptr;
list->append(*ident);
}
if (!list->length())
return nullptr;
return list;
}
static CSSValue* consumePath(CSSParserTokenRange& range) {
// FIXME: Add support for <url>, <basic-shape>, <geometry-box>.
if (range.peek().functionId() != CSSValuePath)
return nullptr;
CSSParserTokenRange functionRange = range;
CSSParserTokenRange functionArgs = consumeFunction(functionRange);
if (functionArgs.peek().type() != StringToken)
return nullptr;
String pathString =
functionArgs.consumeIncludingWhitespace().value().toString();
std::unique_ptr<SVGPathByteStream> byteStream = SVGPathByteStream::create();
if (buildByteStreamFromString(pathString, *byteStream) !=
SVGParseStatus::NoError ||
!functionArgs.atEnd())
return nullptr;
range = functionRange;
if (byteStream->isEmpty())
return CSSIdentifierValue::create(CSSValueNone);
return CSSPathValue::create(std::move(byteStream));
}
static CSSValue* consumePathOrNone(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueNone)
return consumeIdent(range);
return consumePath(range);
}
static CSSValue* consumeOffsetRotate(CSSParserTokenRange& range) {
CSSValue* angle = consumeAngle(range);
CSSValue* keyword = consumeIdent<CSSValueAuto, CSSValueReverse>(range);
if (!angle && !keyword)
return nullptr;
if (!angle)
angle = consumeAngle(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
if (keyword)
list->append(*keyword);
if (angle)
list->append(*angle);
return list;
}
static CSSValue* consumeOffsetAnchor(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSValueID id = range.peek().id();
if (id == CSSValueAuto)
return consumeIdent(range);
return consumePosition(range, cssParserMode, UnitlessQuirk::Forbid);
}
static CSSValue* consumeOffsetPosition(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
UseCounter* useCounter) {
CSSValueID id = range.peek().id();
if (id == CSSValueAuto)
return consumeIdent(range);
CSSValue* value =
consumePosition(range, cssParserMode, UnitlessQuirk::Forbid);
// Count when we receive a valid position other than 'auto'.
if (useCounter && value && value->isValuePair())
useCounter->count(UseCounter::CSSOffsetInEffect);
return value;
}
static CSSValue* consumeOffsetPath(CSSParserTokenRange& range,
UseCounter* useCounter,
bool isMotionPath) {
CSSValue* value = consumePathOrNone(range);
// Count when we receive a valid path other than 'none'.
if (useCounter && value && !value->isIdentifierValue()) {
if (isMotionPath)
useCounter->count(UseCounter::CSSMotionInEffect);
else
useCounter->count(UseCounter::CSSOffsetInEffect);
}
return value;
}
// offset: <offset-path> <offset-distance> <offset-rotation>
bool CSSPropertyParser::consumeOffsetShorthand(bool important) {
const CSSValue* offsetPath =
consumeOffsetPath(m_range, m_context.useCounter(), false);
const CSSValue* offsetDistance =
consumeLengthOrPercent(m_range, m_context.mode(), ValueRangeAll);
const CSSValue* offsetRotation = consumeOffsetRotate(m_range);
if (!offsetPath || !offsetDistance || !offsetRotation || !m_range.atEnd())
return false;
addProperty(CSSPropertyOffsetPath, CSSPropertyOffset, *offsetPath, important);
addProperty(CSSPropertyOffsetDistance, CSSPropertyOffset, *offsetDistance,
important);
addProperty(CSSPropertyOffsetRotation, CSSPropertyOffset, *offsetRotation,
important);
return true;
}
static CSSValue* consumeTextEmphasisStyle(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueNone)
return consumeIdent(range);
if (CSSValue* textEmphasisStyle = consumeString(range))
return textEmphasisStyle;
CSSIdentifierValue* fill = consumeIdent<CSSValueFilled, CSSValueOpen>(range);
CSSIdentifierValue* shape =
consumeIdent<CSSValueDot, CSSValueCircle, CSSValueDoubleCircle,
CSSValueTriangle, CSSValueSesame>(range);
if (!fill)
fill = consumeIdent<CSSValueFilled, CSSValueOpen>(range);
if (fill && shape) {
CSSValueList* parsedValues = CSSValueList::createSpaceSeparated();
parsedValues->append(*fill);
parsedValues->append(*shape);
return parsedValues;
}
if (fill)
return fill;
if (shape)
return shape;
return nullptr;
}
static CSSValue* consumeOutlineColor(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
// Allow the special focus color even in HTML Standard parsing mode.
if (range.peek().id() == CSSValueWebkitFocusRingColor)
return consumeIdent(range);
return consumeColor(range, cssParserMode);
}
static CSSValue* consumeLineWidth(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
UnitlessQuirk unitless) {
CSSValueID id = range.peek().id();
if (id == CSSValueThin || id == CSSValueMedium || id == CSSValueThick)
return consumeIdent(range);
return consumeLength(range, cssParserMode, ValueRangeNonNegative, unitless);
}
static CSSValue* consumeBorderWidth(CSSParserTokenRange& range,
CSSParserMode cssParserMode,
UnitlessQuirk unitless) {
return consumeLineWidth(range, cssParserMode, unitless);
}
static CSSValue* consumeTextStrokeWidth(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
return consumeLineWidth(range, cssParserMode, UnitlessQuirk::Forbid);
}
static CSSValue* consumeColumnRuleWidth(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
return consumeLineWidth(range, cssParserMode, UnitlessQuirk::Forbid);
}
static bool consumeTranslate3d(CSSParserTokenRange& args,
CSSParserMode cssParserMode,
CSSFunctionValue*& transformValue) {
unsigned numberOfArguments = 2;
CSSValue* parsedValue = nullptr;
do {
parsedValue = consumeLengthOrPercent(args, cssParserMode, ValueRangeAll);
if (!parsedValue)
return false;
transformValue->append(*parsedValue);
if (!consumeCommaIncludingWhitespace(args))
return false;
} while (--numberOfArguments);
parsedValue = consumeLength(args, cssParserMode, ValueRangeAll);
if (!parsedValue)
return false;
transformValue->append(*parsedValue);
return true;
}
static bool consumeNumbers(CSSParserTokenRange& args,
CSSFunctionValue*& transformValue,
unsigned numberOfArguments) {
do {
CSSValue* parsedValue = consumeNumber(args, ValueRangeAll);
if (!parsedValue)
return false;
transformValue->append(*parsedValue);
if (--numberOfArguments && !consumeCommaIncludingWhitespace(args))
return false;
} while (numberOfArguments);
return true;
}
static bool consumePerspective(CSSParserTokenRange& args,
const CSSParserContext& context,
CSSFunctionValue*& transformValue,
bool useLegacyParsing) {
CSSPrimitiveValue* parsedValue =
consumeLength(args, context.mode(), ValueRangeNonNegative);
if (!parsedValue && useLegacyParsing) {
double perspective;
if (!consumeNumberRaw(args, perspective) || perspective < 0)
return false;
if (context.useCounter()) {
context.useCounter()->count(
UseCounter::UnitlessPerspectiveInTransformProperty);
}
parsedValue = CSSPrimitiveValue::create(
perspective, CSSPrimitiveValue::UnitType::Pixels);
}
if (!parsedValue)
return false;
transformValue->append(*parsedValue);
return true;
}
static CSSValue* consumeTransformValue(CSSParserTokenRange& range,
const CSSParserContext& context,
bool useLegacyParsing) {
CSSValueID functionId = range.peek().functionId();
if (functionId == CSSValueInvalid)
return nullptr;
CSSParserTokenRange args = consumeFunction(range);
if (args.atEnd())
return nullptr;
CSSFunctionValue* transformValue = CSSFunctionValue::create(functionId);
CSSValue* parsedValue = nullptr;
switch (functionId) {
case CSSValueRotate:
case CSSValueRotateX:
case CSSValueRotateY:
case CSSValueRotateZ:
case CSSValueSkewX:
case CSSValueSkewY:
case CSSValueSkew:
parsedValue = consumeAngle(args);
if (!parsedValue)
return nullptr;
if (functionId == CSSValueSkew && consumeCommaIncludingWhitespace(args)) {
transformValue->append(*parsedValue);
parsedValue = consumeAngle(args);
if (!parsedValue)
return nullptr;
}
break;
case CSSValueScaleX:
case CSSValueScaleY:
case CSSValueScaleZ:
case CSSValueScale:
parsedValue = consumeNumber(args, ValueRangeAll);
if (!parsedValue)
return nullptr;
if (functionId == CSSValueScale &&
consumeCommaIncludingWhitespace(args)) {
transformValue->append(*parsedValue);
parsedValue = consumeNumber(args, ValueRangeAll);
if (!parsedValue)
return nullptr;
}
break;
case CSSValuePerspective:
if (!consumePerspective(args, context, transformValue, useLegacyParsing))
return nullptr;
break;
case CSSValueTranslateX:
case CSSValueTranslateY:
case CSSValueTranslate:
parsedValue = consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (!parsedValue)
return nullptr;
if (functionId == CSSValueTranslate &&
consumeCommaIncludingWhitespace(args)) {
transformValue->append(*parsedValue);
parsedValue =
consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (!parsedValue)
return nullptr;
}
break;
case CSSValueTranslateZ:
parsedValue = consumeLength(args, context.mode(), ValueRangeAll);
break;
case CSSValueMatrix:
case CSSValueMatrix3d:
if (!consumeNumbers(args, transformValue,
(functionId == CSSValueMatrix3d) ? 16 : 6))
return nullptr;
break;
case CSSValueScale3d:
if (!consumeNumbers(args, transformValue, 3))
return nullptr;
break;
case CSSValueRotate3d:
if (!consumeNumbers(args, transformValue, 3) ||
!consumeCommaIncludingWhitespace(args))
return nullptr;
parsedValue = consumeAngle(args);
if (!parsedValue)
return nullptr;
break;
case CSSValueTranslate3d:
if (!consumeTranslate3d(args, context.mode(), transformValue))
return nullptr;
break;
default:
return nullptr;
}
if (parsedValue)
transformValue->append(*parsedValue);
if (!args.atEnd())
return nullptr;
return transformValue;
}
static CSSValue* consumeTransform(CSSParserTokenRange& range,
const CSSParserContext& context,
bool useLegacyParsing) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSValueList* list = CSSValueList::createSpaceSeparated();
do {
CSSValue* parsedTransformValue =
consumeTransformValue(range, context, useLegacyParsing);
if (!parsedTransformValue)
return nullptr;
list->append(*parsedTransformValue);
} while (!range.atEnd());
return list;
}
template <CSSValueID start, CSSValueID end>
static CSSValue* consumePositionLonghand(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().type() == IdentToken) {
CSSValueID id = range.peek().id();
int percent;
if (id == start)
percent = 0;
else if (id == CSSValueCenter)
percent = 50;
else if (id == end)
percent = 100;
else
return nullptr;
range.consumeIncludingWhitespace();
return CSSPrimitiveValue::create(percent,
CSSPrimitiveValue::UnitType::Percentage);
}
return consumeLengthOrPercent(range, cssParserMode, ValueRangeAll);
}
static CSSValue* consumePositionX(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
return consumePositionLonghand<CSSValueLeft, CSSValueRight>(range,
cssParserMode);
}
static CSSValue* consumePositionY(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
return consumePositionLonghand<CSSValueTop, CSSValueBottom>(range,
cssParserMode);
}
static CSSValue* consumePaintStroke(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSURIValue* url = consumeUrl(range);
if (url) {
CSSValue* parsedValue = nullptr;
if (range.peek().id() == CSSValueNone)
parsedValue = consumeIdent(range);
else
parsedValue = consumeColor(range, cssParserMode);
if (parsedValue) {
CSSValueList* values = CSSValueList::createSpaceSeparated();
values->append(*url);
values->append(*parsedValue);
return values;
}
return url;
}
return consumeColor(range, cssParserMode);
}
static CSSValue* consumePaintOrder(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNormal)
return consumeIdent(range);
Vector<CSSValueID, 3> paintTypeList;
CSSIdentifierValue* fill = nullptr;
CSSIdentifierValue* stroke = nullptr;
CSSIdentifierValue* markers = nullptr;
do {
CSSValueID id = range.peek().id();
if (id == CSSValueFill && !fill)
fill = consumeIdent(range);
else if (id == CSSValueStroke && !stroke)
stroke = consumeIdent(range);
else if (id == CSSValueMarkers && !markers)
markers = consumeIdent(range);
else
return nullptr;
paintTypeList.push_back(id);
} while (!range.atEnd());
// After parsing we serialize the paint-order list. Since it is not possible
// to pop a last list items from CSSValueList without bigger cost, we create
// the list after parsing.
CSSValueID firstPaintOrderType = paintTypeList.at(0);
CSSValueList* paintOrderList = CSSValueList::createSpaceSeparated();
switch (firstPaintOrderType) {
case CSSValueFill:
case CSSValueStroke:
paintOrderList->append(firstPaintOrderType == CSSValueFill ? *fill
: *stroke);
if (paintTypeList.size() > 1) {
if (paintTypeList.at(1) == CSSValueMarkers)
paintOrderList->append(*markers);
}
break;
case CSSValueMarkers:
paintOrderList->append(*markers);
if (paintTypeList.size() > 1) {
if (paintTypeList.at(1) == CSSValueStroke)
paintOrderList->append(*stroke);
}
break;
default:
ASSERT_NOT_REACHED();
}
return paintOrderList;
}
static CSSValue* consumeNoneOrURI(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
return consumeUrl(range);
}
static CSSValue* consumeFlexBasis(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
// FIXME: Support intrinsic dimensions too.
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
}
static CSSValue* consumeStrokeDasharray(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueNone)
return consumeIdent(range);
CSSValueList* dashes = CSSValueList::createCommaSeparated();
do {
CSSPrimitiveValue* dash =
consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeNonNegative);
if (!dash || (consumeCommaIncludingWhitespace(range) && range.atEnd()))
return nullptr;
dashes->append(*dash);
} while (!range.atEnd());
return dashes;
}
static CSSValue* consumeBaselineShift(CSSParserTokenRange& range) {
CSSValueID id = range.peek().id();
if (id == CSSValueBaseline || id == CSSValueSub || id == CSSValueSuper)
return consumeIdent(range);
return consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeAll);
}
static CSSValue* consumeRxOrRy(CSSParserTokenRange& range) {
if (range.peek().id() == CSSValueAuto)
return consumeIdent(range);
return consumeLengthOrPercent(range, SVGAttributeMode, ValueRangeAll,
UnitlessQuirk::Forbid);
}
static CSSValue* consumeCursor(CSSParserTokenRange& range,
const CSSParserContext& context,
bool inQuirksMode) {
CSSValueList* list = nullptr;
while (CSSValue* image = consumeImage(range, context,
ConsumeGeneratedImagePolicy::Forbid)) {
double num;
IntPoint hotSpot(-1, -1);
bool hotSpotSpecified = false;
if (consumeNumberRaw(range, num)) {
hotSpot.setX(int(num));
if (!consumeNumberRaw(range, num))
return nullptr;
hotSpot.setY(int(num));
hotSpotSpecified = true;
}
if (!list)
list = CSSValueList::createCommaSeparated();
list->append(
*CSSCursorImageValue::create(*image, hotSpotSpecified, hotSpot));
if (!consumeCommaIncludingWhitespace(range))
return nullptr;
}
CSSValueID id = range.peek().id();
if (!range.atEnd() && context.useCounter()) {
if (id == CSSValueWebkitZoomIn)
context.useCounter()->count(UseCounter::PrefixedCursorZoomIn);
else if (id == CSSValueWebkitZoomOut)
context.useCounter()->count(UseCounter::PrefixedCursorZoomOut);
}
CSSValue* cursorType = nullptr;
if (id == CSSValueHand) {
if (!inQuirksMode) // Non-standard behavior
return nullptr;
cursorType = CSSIdentifierValue::create(CSSValuePointer);
range.consumeIncludingWhitespace();
} else if ((id >= CSSValueAuto && id <= CSSValueWebkitZoomOut) ||
id == CSSValueCopy || id == CSSValueNone) {
cursorType = consumeIdent(range);
} else {
return nullptr;
}
if (!list)
return cursorType;
list->append(*cursorType);
return list;
}
static CSSValue* consumeAttr(CSSParserTokenRange args,
const CSSParserContext& context) {
if (args.peek().type() != IdentToken)
return nullptr;
AtomicString attrName =
args.consumeIncludingWhitespace().value().toAtomicString();
if (!args.atEnd())
return nullptr;
// TODO(esprehn): This should be lowerASCII().
if (context.isHTMLDocument())
attrName = attrName.lower();
CSSFunctionValue* attrValue = CSSFunctionValue::create(CSSValueAttr);
attrValue->append(*CSSCustomIdentValue::create(attrName));
return attrValue;
}
static CSSValue* consumeCounterContent(CSSParserTokenRange args,
bool counters) {
CSSCustomIdentValue* identifier = consumeCustomIdent(args);
if (!identifier)
return nullptr;
CSSStringValue* separator = nullptr;
if (!counters) {
separator = CSSStringValue::create(String());
} else {
if (!consumeCommaIncludingWhitespace(args) ||
args.peek().type() != StringToken)
return nullptr;
separator = CSSStringValue::create(
args.consumeIncludingWhitespace().value().toString());
}
CSSIdentifierValue* listStyle = nullptr;
if (consumeCommaIncludingWhitespace(args)) {
CSSValueID id = args.peek().id();
if ((id != CSSValueNone &&
(id < CSSValueDisc || id > CSSValueKatakanaIroha)))
return nullptr;
listStyle = consumeIdent(args);
} else {
listStyle = CSSIdentifierValue::create(CSSValueDecimal);
}
if (!args.atEnd())
return nullptr;
return CSSCounterValue::create(identifier, listStyle, separator);
}
static CSSValue* consumeContent(CSSParserTokenRange& range,
const CSSParserContext& context) {
if (identMatches<CSSValueNone, CSSValueNormal>(range.peek().id()))
return consumeIdent(range);
CSSValueList* values = CSSValueList::createSpaceSeparated();
do {
CSSValue* parsedValue = consumeImage(range, context);
if (!parsedValue)
parsedValue =
consumeIdent<CSSValueOpenQuote, CSSValueCloseQuote,
CSSValueNoOpenQuote, CSSValueNoCloseQuote>(range);
if (!parsedValue)
parsedValue = consumeString(range);
if (!parsedValue) {
if (range.peek().functionId() == CSSValueAttr)
parsedValue = consumeAttr(consumeFunction(range), context);
else if (range.peek().functionId() == CSSValueCounter)
parsedValue = consumeCounterContent(consumeFunction(range), false);
else if (range.peek().functionId() == CSSValueCounters)
parsedValue = consumeCounterContent(consumeFunction(range), true);
if (!parsedValue)
return nullptr;
}
values->append(*parsedValue);
} while (!range.atEnd());
return values;
}
static CSSValue* consumePerspective(CSSParserTokenRange& range,
const CSSParserContext& context,
CSSPropertyID unresolvedProperty) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
CSSPrimitiveValue* parsedValue =
consumeLength(range, context.mode(), ValueRangeAll);
if (!parsedValue &&
(unresolvedProperty == CSSPropertyAliasWebkitPerspective)) {
double perspective;
if (!consumeNumberRaw(range, perspective))
return nullptr;
if (context.useCounter()) {
context.useCounter()->count(
UseCounter::UnitlessPerspectiveInPerspectiveProperty);
}
parsedValue = CSSPrimitiveValue::create(
perspective, CSSPrimitiveValue::UnitType::Pixels);
}
if (parsedValue &&
(parsedValue->isCalculated() || parsedValue->getDoubleValue() > 0))
return parsedValue;
return nullptr;
}
static CSSValueList* consumePositionList(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSValueList* positions = CSSValueList::createCommaSeparated();
do {
CSSValue* position =
consumePosition(range, cssParserMode, UnitlessQuirk::Forbid);
if (!position)
return nullptr;
positions->append(*position);
} while (consumeCommaIncludingWhitespace(range));
return positions;
}
static CSSValue* consumeScrollSnapCoordinate(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
return consumePositionList(range, cssParserMode);
}
static CSSValue* consumeScrollSnapPoints(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
if (range.peek().functionId() == CSSValueRepeat) {
CSSParserTokenRange args = consumeFunction(range);
CSSPrimitiveValue* parsedValue =
consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative);
if (args.atEnd() && parsedValue &&
(parsedValue->isCalculated() || parsedValue->getDoubleValue() > 0)) {
CSSFunctionValue* result = CSSFunctionValue::create(CSSValueRepeat);
result->append(*parsedValue);
return result;
}
}
return nullptr;
}
static CSSValue* consumeBorderRadiusCorner(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSValue* parsedValue1 =
consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
if (!parsedValue1)
return nullptr;
CSSValue* parsedValue2 =
consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
if (!parsedValue2)
parsedValue2 = parsedValue1;
return CSSValuePair::create(parsedValue1, parsedValue2,
CSSValuePair::DropIdenticalValues);
}
static CSSValue* consumeVerticalAlign(CSSParserTokenRange& range,
CSSParserMode cssParserMode) {
CSSValue* parsedValue =
consumeIdentRange(range, CSSValueBaseline, CSSValueWebkitBaselineMiddle);
if (!parsedValue)
parsedValue = consumeLengthOrPercent(range, cssParserMode, ValueRangeAll,
UnitlessQuirk::Allow);
return parsedValue;
}
static CSSValue* consumeShapeRadius(CSSParserTokenRange& args,
CSSParserMode cssParserMode) {
if (identMatches<CSSValueClosestSide, CSSValueFarthestSide>(args.peek().id()))
return consumeIdent(args);
return consumeLengthOrPercent(args, cssParserMode, ValueRangeNonNegative);
}
static CSSBasicShapeCircleValue* consumeBasicShapeCircle(
CSSParserTokenRange& args,
const CSSParserContext& context) {
// spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes
// circle( [<shape-radius>]? [at <position>]? )
CSSBasicShapeCircleValue* shape = CSSBasicShapeCircleValue::create();
if (CSSValue* radius = consumeShapeRadius(args, context.mode()))
shape->setRadius(radius);
if (consumeIdent<CSSValueAt>(args)) {
CSSValue* centerX = nullptr;
CSSValue* centerY = nullptr;
if (!consumePosition(args, context.mode(), UnitlessQuirk::Forbid, centerX,
centerY))
return nullptr;
shape->setCenterX(centerX);
shape->setCenterY(centerY);
}
return shape;
}
static CSSBasicShapeEllipseValue* consumeBasicShapeEllipse(
CSSParserTokenRange& args,
const CSSParserContext& context) {
// spec: https://drafts.csswg.org/css-shapes/#supported-basic-shapes
// ellipse( [<shape-radius>{2}]? [at <position>]? )
CSSBasicShapeEllipseValue* shape = CSSBasicShapeEllipseValue::create();
if (CSSValue* radiusX = consumeShapeRadius(args, context.mode())) {
shape->setRadiusX(radiusX);
if (CSSValue* radiusY = consumeShapeRadius(args, context.mode()))
shape->setRadiusY(radiusY);
}
if (consumeIdent<CSSValueAt>(args)) {
CSSValue* centerX = nullptr;
CSSValue* centerY = nullptr;
if (!consumePosition(args, context.mode(), UnitlessQuirk::Forbid, centerX,
centerY))
return nullptr;
shape->setCenterX(centerX);
shape->setCenterY(centerY);
}
return shape;
}
static CSSBasicShapePolygonValue* consumeBasicShapePolygon(
CSSParserTokenRange& args,
const CSSParserContext& context) {
CSSBasicShapePolygonValue* shape = CSSBasicShapePolygonValue::create();
if (identMatches<CSSValueEvenodd, CSSValueNonzero>(args.peek().id())) {
shape->setWindRule(args.consumeIncludingWhitespace().id() == CSSValueEvenodd
? RULE_EVENODD
: RULE_NONZERO);
if (!consumeCommaIncludingWhitespace(args))
return nullptr;
}
do {
CSSPrimitiveValue* xLength =
consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (!xLength)
return nullptr;
CSSPrimitiveValue* yLength =
consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (!yLength)
return nullptr;
shape->appendPoint(xLength, yLength);
} while (consumeCommaIncludingWhitespace(args));
return shape;
}
static void complete4Sides(CSSValue* side[4]) {
if (side[3])
return;
if (!side[2]) {
if (!side[1])
side[1] = side[0];
side[2] = side[0];
}
side[3] = side[1];
}
static bool consumeRadii(CSSValue* horizontalRadii[4],
CSSValue* verticalRadii[4],
CSSParserTokenRange& range,
CSSParserMode cssParserMode,
bool useLegacyParsing) {
unsigned i = 0;
for (; i < 4 && !range.atEnd() && range.peek().type() != DelimiterToken;
++i) {
horizontalRadii[i] =
consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
if (!horizontalRadii[i])
return false;
}
if (!horizontalRadii[0])
return false;
if (range.atEnd()) {
// Legacy syntax: -webkit-border-radius: l1 l2; is equivalent to
// border-radius: l1 / l2;
if (useLegacyParsing && i == 2) {
verticalRadii[0] = horizontalRadii[1];
horizontalRadii[1] = nullptr;
} else {
complete4Sides(horizontalRadii);
for (unsigned i = 0; i < 4; ++i)
verticalRadii[i] = horizontalRadii[i];
return true;
}
} else {
if (!consumeSlashIncludingWhitespace(range))
return false;
for (i = 0; i < 4 && !range.atEnd(); ++i) {
verticalRadii[i] =
consumeLengthOrPercent(range, cssParserMode, ValueRangeNonNegative);
if (!verticalRadii[i])
return false;
}
if (!verticalRadii[0] || !range.atEnd())
return false;
}
complete4Sides(horizontalRadii);
complete4Sides(verticalRadii);
return true;
}
static CSSBasicShapeInsetValue* consumeBasicShapeInset(
CSSParserTokenRange& args,
const CSSParserContext& context) {
CSSBasicShapeInsetValue* shape = CSSBasicShapeInsetValue::create();
CSSPrimitiveValue* top =
consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (!top)
return nullptr;
CSSPrimitiveValue* right =
consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
CSSPrimitiveValue* bottom = nullptr;
CSSPrimitiveValue* left = nullptr;
if (right) {
bottom = consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
if (bottom)
left = consumeLengthOrPercent(args, context.mode(), ValueRangeAll);
}
if (left)
shape->updateShapeSize4Values(top, right, bottom, left);
else if (bottom)
shape->updateShapeSize3Values(top, right, bottom);
else if (right)
shape->updateShapeSize2Values(top, right);
else
shape->updateShapeSize1Value(top);
if (consumeIdent<CSSValueRound>(args)) {
CSSValue* horizontalRadii[4] = {0};
CSSValue* verticalRadii[4] = {0};
if (!consumeRadii(horizontalRadii, verticalRadii, args, context.mode(),
false))
return nullptr;
shape->setTopLeftRadius(
CSSValuePair::create(horizontalRadii[0], verticalRadii[0],
CSSValuePair::DropIdenticalValues));
shape->setTopRightRadius(
CSSValuePair::create(horizontalRadii[1], verticalRadii[1],
CSSValuePair::DropIdenticalValues));
shape->setBottomRightRadius(
CSSValuePair::create(horizontalRadii[2], verticalRadii[2],
CSSValuePair::DropIdenticalValues));
shape->setBottomLeftRadius(
CSSValuePair::create(horizontalRadii[3], verticalRadii[3],
CSSValuePair::DropIdenticalValues));
}
return shape;
}
static CSSValue* consumeBasicShape(CSSParserTokenRange& range,
const CSSParserContext& context) {
CSSValue* shape = nullptr;
if (range.peek().type() != FunctionToken)
return nullptr;
CSSValueID id = range.peek().functionId();
CSSParserTokenRange rangeCopy = range;
CSSParserTokenRange args = consumeFunction(rangeCopy);
if (id == CSSValueCircle)
shape = consumeBasicShapeCircle(args, context);
else if (id == CSSValueEllipse)
shape = consumeBasicShapeEllipse(args, context);
else if (id == CSSValuePolygon)
shape = consumeBasicShapePolygon(args, context);
else if (id == CSSValueInset)
shape = consumeBasicShapeInset(args, context);
if (!shape || !args.atEnd())
return nullptr;
range = rangeCopy;
return shape;
}
static CSSValue* consumeClipPath(CSSParserTokenRange& range,
const CSSParserContext& context) {
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
if (CSSURIValue* url = consumeUrl(range))
return url;
return consumeBasicShape(range, context);
}
static CSSValue* consumeShapeOutside(CSSParserTokenRange& range,
const CSSParserContext& context) {
if (CSSValue* imageValue = consumeImageOrNone(range, context))
return imageValue;
CSSValueList* list = CSSValueList::createSpaceSeparated();
if (CSSValue* boxValue = consumeShapeBox(range))
list->append(*boxValue);
if (CSSValue* shapeValue = consumeBasicShape(range, context)) {
list->append(*shapeValue);
if (list->length() < 2) {
if (CSSValue* boxValue = consumeShapeBox(range))
list->append(*boxValue);
}
}
if (!list->length())
return nullptr;
return list;
}
static CSSValue* consumeContentDistributionOverflowPosition(
CSSParserTokenRange& range) {
if (identMatches<CSSValueNormal, CSSValueBaseline, CSSValueLastBaseline>(
range.peek().id()))
return CSSContentDistributionValue::create(
CSSValueInvalid, range.consumeIncludingWhitespace().id(),
CSSValueInvalid);
CSSValueID distribution = CSSValueInvalid;
CSSValueID position = CSSValueInvalid;
CSSValueID overflow =