blob: d33cdb368ee412e79d7b1d115b3cbe50844354ca [file] [log] [blame]
/*
* Copyright (C) 2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
* Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
* Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#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/CSSCrossfadeValue.h"
#include "core/css/CSSCustomIdentValue.h"
#include "core/css/CSSFunctionValue.h"
#include "core/css/CSSGridLineNamesValue.h"
#include "core/css/CSSImageSetValue.h"
#include "core/css/CSSPrimitiveValueMappings.h"
#include "core/css/CSSProperty.h"
#include "core/css/CSSPropertyMetadata.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/CSSValuePair.h"
#include "core/css/CSSValuePool.h"
#include "core/css/CSSVariableReferenceValue.h"
#include "core/css/HashTools.h"
#include "core/css/parser/CSSParserFastPaths.h"
#include "core/css/parser/CSSParserValues.h"
#include "core/frame/UseCounter.h"
#include "core/style/GridCoordinate.h"
#include "platform/RuntimeEnabledFeatures.h"
namespace blink {
template <unsigned N>
static bool equalIgnoringCase(const CSSParserString& a, const char (&b)[N])
{
unsigned length = N - 1; // Ignore the trailing null character
if (a.length() != length)
return false;
return a.is8Bit() ? WTF::equalIgnoringCase(b, a.characters8(), length) : WTF::equalIgnoringCase(b, a.characters16(), length);
}
void CSSPropertyParser::addProperty(CSSPropertyID propId, PassRefPtrWillBeRawPtr<CSSValue> value, bool important, bool implicit)
{
ASSERT(!isPropertyAlias(propId));
int shorthandIndex = 0;
bool setFromShorthand = false;
if (m_currentShorthand) {
Vector<StylePropertyShorthand, 4> shorthands;
getMatchingShorthandsForLonghand(propId, &shorthands);
setFromShorthand = true;
if (shorthands.size() > 1)
shorthandIndex = indexOfShorthandForLonghand(m_currentShorthand, shorthands);
}
m_parsedProperties.append(CSSProperty(propId, value, important, setFromShorthand, shorthandIndex, m_implicitShorthand || implicit));
}
void CSSPropertyParser::rollbackLastProperties(int num)
{
ASSERT(num >= 0);
ASSERT(m_parsedProperties.size() >= static_cast<unsigned>(num));
m_parsedProperties.shrink(m_parsedProperties.size() - num);
}
KURL CSSPropertyParser::completeURL(const String& url) const
{
return m_context.completeURL(url);
}
bool CSSPropertyParser::validCalculationUnit(CSSParserValue* value, Units unitflags, ReleaseParsedCalcValueCondition releaseCalc)
{
bool mustBeNonNegative = unitflags & (FNonNeg | FPositiveInteger);
if (!parseCalculation(value, mustBeNonNegative ? ValueRangeNonNegative : ValueRangeAll))
return false;
bool b = false;
switch (m_parsedCalculation->category()) {
case CalcLength:
b = (unitflags & FLength);
break;
case CalcNumber:
b = (unitflags & FNumber);
if (!b && (unitflags & (FInteger | FPositiveInteger)) && m_parsedCalculation->isInt())
b = true;
if (b && mustBeNonNegative && m_parsedCalculation->isNegative())
b = false;
// Always resolve calc() to a UnitType::Number in the CSSParserValue if there are no non-numbers specified in the unitflags.
if (b && !(unitflags & ~(FInteger | FNumber | FPositiveInteger | FNonNeg))) {
double number = m_parsedCalculation->doubleValue();
if ((unitflags & FPositiveInteger) && number <= 0) {
b = false;
} else {
delete value->calcFunction;
value->setUnit(CSSPrimitiveValue::UnitType::Number);
value->fValue = number;
value->isInt = m_parsedCalculation->isInt();
}
m_parsedCalculation.release();
return b;
}
break;
case CalcPercent:
b = (unitflags & FPercent);
if (b && mustBeNonNegative && m_parsedCalculation->isNegative())
b = false;
break;
case CalcPercentLength:
b = (unitflags & FPercent) && (unitflags & FLength);
break;
case CalcPercentNumber:
b = (unitflags & FPercent) && (unitflags & FNumber);
break;
case CalcAngle:
b = (unitflags & FAngle);
break;
case CalcTime:
b = (unitflags & FTime);
break;
case CalcFrequency:
b = (unitflags & FFrequency);
break;
case CalcOther:
break;
}
if (!b || releaseCalc == ReleaseParsedCalcValue)
m_parsedCalculation.release();
return b;
}
inline bool CSSPropertyParser::shouldAcceptUnitLessValues(CSSParserValue* value, Units unitflags, CSSParserMode cssParserMode)
{
// Quirks mode for certain properties and presentation attributes accept unit-less values for certain units.
return (unitflags & (FLength | FAngle))
&& (!value->fValue // 0 can always be unitless.
|| isUnitLessLengthParsingEnabledForMode(cssParserMode) // HTML and SVG attribute values can always be unitless.
|| (cssParserMode == HTMLQuirksMode && (unitflags & FUnitlessQuirk)));
}
inline bool isCalculation(CSSParserValue* value)
{
return value->m_unit == CSSParserValue::CalcFunction;
}
bool CSSPropertyParser::validUnit(CSSParserValue* value, Units unitflags, CSSParserMode cssParserMode, ReleaseParsedCalcValueCondition releaseCalc)
{
if (isCalculation(value))
return validCalculationUnit(value, unitflags, releaseCalc);
if (unitflags & FNonNeg && value->fValue < 0)
return false;
switch (value->unit()) {
case CSSPrimitiveValue::UnitType::Number:
if (unitflags & FNumber)
return true;
if (shouldAcceptUnitLessValues(value, unitflags, cssParserMode)) {
value->setUnit((unitflags & FLength) ? CSSPrimitiveValue::UnitType::Pixels : CSSPrimitiveValue::UnitType::Degrees);
return true;
}
if ((unitflags & FInteger) && value->isInt)
return true;
if ((unitflags & FPositiveInteger) && value->isInt && value->fValue > 0)
return true;
return false;
case CSSPrimitiveValue::UnitType::Percentage:
return unitflags & FPercent;
case CSSPrimitiveValue::UnitType::QuirkyEms:
if (cssParserMode != UASheetMode)
return false;
/* fallthrough intentional */
case CSSPrimitiveValue::UnitType::Ems:
case CSSPrimitiveValue::UnitType::Rems:
case CSSPrimitiveValue::UnitType::Chs:
case CSSPrimitiveValue::UnitType::Exs:
case CSSPrimitiveValue::UnitType::Pixels:
case CSSPrimitiveValue::UnitType::Centimeters:
case CSSPrimitiveValue::UnitType::Millimeters:
case CSSPrimitiveValue::UnitType::Inches:
case CSSPrimitiveValue::UnitType::Points:
case CSSPrimitiveValue::UnitType::Picas:
case CSSPrimitiveValue::UnitType::UserUnits:
case CSSPrimitiveValue::UnitType::ViewportWidth:
case CSSPrimitiveValue::UnitType::ViewportHeight:
case CSSPrimitiveValue::UnitType::ViewportMin:
case CSSPrimitiveValue::UnitType::ViewportMax:
return unitflags & FLength;
case CSSPrimitiveValue::UnitType::Milliseconds:
case CSSPrimitiveValue::UnitType::Seconds:
return unitflags & FTime;
case CSSPrimitiveValue::UnitType::Degrees:
case CSSPrimitiveValue::UnitType::Radians:
case CSSPrimitiveValue::UnitType::Gradians:
case CSSPrimitiveValue::UnitType::Turns:
return unitflags & FAngle;
case CSSPrimitiveValue::UnitType::DotsPerPixel:
case CSSPrimitiveValue::UnitType::DotsPerInch:
case CSSPrimitiveValue::UnitType::DotsPerCentimeter:
return unitflags & FResolution;
default:
return false;
}
}
PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::createPrimitiveNumericValue(CSSParserValue* value)
{
if (m_parsedCalculation) {
ASSERT(isCalculation(value));
return CSSPrimitiveValue::create(m_parsedCalculation.release());
}
ASSERT((value->unit() >= CSSPrimitiveValue::UnitType::Number && value->unit() <= CSSPrimitiveValue::UnitType::Kilohertz)
|| (value->unit() >= CSSPrimitiveValue::UnitType::Turns && value->unit() <= CSSPrimitiveValue::UnitType::Chs)
|| (value->unit() >= CSSPrimitiveValue::UnitType::ViewportWidth && value->unit() <= CSSPrimitiveValue::UnitType::ViewportMax)
|| (value->unit() >= CSSPrimitiveValue::UnitType::DotsPerPixel && value->unit() <= CSSPrimitiveValue::UnitType::DotsPerCentimeter));
return cssValuePool().createValue(value->fValue, value->unit());
}
inline PassRefPtrWillBeRawPtr<CSSStringValue> CSSPropertyParser::createPrimitiveStringValue(CSSParserValue* value)
{
ASSERT(value->m_unit == CSSParserValue::String || value->m_unit == CSSParserValue::Identifier);
return CSSStringValue::create(value->string);
}
inline PassRefPtrWillBeRawPtr<CSSCustomIdentValue> CSSPropertyParser::createPrimitiveCustomIdentValue(CSSParserValue* value)
{
ASSERT(value->m_unit == CSSParserValue::String || value->m_unit == CSSParserValue::Identifier);
return CSSCustomIdentValue::create(value->string);
}
static inline bool isComma(CSSParserValue* value)
{
ASSERT(value);
return value->m_unit == CSSParserValue::Operator && value->iValue == ',';
}
static bool consumeComma(CSSParserValueList* valueList)
{
CSSParserValue* value = valueList->current();
if (!value || !isComma(value))
return false;
valueList->next();
return true;
}
static inline bool isForwardSlashOperator(CSSParserValue* value)
{
ASSERT(value);
return value->m_unit == CSSParserValue::Operator && value->iValue == '/';
}
static bool isGeneratedImageValue(CSSValueID id)
{
return id == CSSValueLinearGradient || id == CSSValueRadialGradient
|| id == CSSValueRepeatingLinearGradient || id == CSSValueRepeatingRadialGradient
|| id == CSSValueWebkitLinearGradient || id == CSSValueWebkitRadialGradient
|| id == CSSValueWebkitRepeatingLinearGradient || id == CSSValueWebkitRepeatingRadialGradient
|| id == CSSValueWebkitGradient || id == CSSValueWebkitCrossFade;
}
inline PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::parseValidPrimitive(CSSValueID identifier, CSSParserValue* value)
{
if (identifier)
return cssValuePool().createIdentifierValue(identifier);
if (value->unit() >= CSSPrimitiveValue::UnitType::Number && value->unit() <= CSSPrimitiveValue::UnitType::Kilohertz)
return createPrimitiveNumericValue(value);
if (value->unit() >= CSSPrimitiveValue::UnitType::Turns && value->unit() <= CSSPrimitiveValue::UnitType::Chs)
return createPrimitiveNumericValue(value);
if (value->unit() >= CSSPrimitiveValue::UnitType::ViewportWidth && value->unit() <= CSSPrimitiveValue::UnitType::ViewportMax)
return createPrimitiveNumericValue(value);
if (value->unit() >= CSSPrimitiveValue::UnitType::DotsPerPixel && value->unit() <= CSSPrimitiveValue::UnitType::DotsPerCentimeter)
return createPrimitiveNumericValue(value);
if (value->unit() == CSSPrimitiveValue::UnitType::QuirkyEms)
return CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::UnitType::QuirkyEms);
if (isCalculation(value))
return CSSPrimitiveValue::create(m_parsedCalculation.release());
return nullptr;
}
void CSSPropertyParser::addExpandedPropertyForValue(CSSPropertyID propId, PassRefPtrWillBeRawPtr<CSSValue> prpValue, bool important)
{
const StylePropertyShorthand& shorthand = shorthandForProperty(propId);
unsigned shorthandLength = shorthand.length();
if (!shorthandLength) {
addProperty(propId, prpValue, important);
return;
}
RefPtrWillBeRawPtr<CSSValue> value = prpValue;
ShorthandScope scope(this, propId);
const CSSPropertyID* longhands = shorthand.properties();
for (unsigned i = 0; i < shorthandLength; ++i)
addProperty(longhands[i], value, important);
}
bool CSSPropertyParser::parseValue(CSSPropertyID unresolvedProperty, bool important)
{
CSSPropertyID propId = resolveCSSPropertyID(unresolvedProperty);
CSSParserValue* value = m_valueList->current();
// Note: m_parsedCalculation is used to pass the calc value to validUnit and then cleared at the end of this function.
// FIXME: This is to avoid having to pass parsedCalc to all validUnit callers.
ASSERT(!m_parsedCalculation);
CSSValueID id = value->id;
// TODO(timloh): Move to parseSingleValue
if (CSSParserFastPaths::isKeywordPropertyID(propId)) {
if (!CSSParserFastPaths::isValidKeywordPropertyAndValue(propId, id))
return false;
if (m_valueList->next() && !inShorthand())
return false;
addProperty(propId, cssValuePool().createIdentifierValue(id), important);
return true;
}
bool validPrimitive = false;
Units unitless = FUnknown;
RefPtrWillBeRawPtr<CSSValue> parsedValue = nullptr;
switch (propId) {
case CSSPropertyContent: // [ <string> | <uri> | <counter> | attr(X) | open-quote |
// close-quote | no-open-quote | no-close-quote ]+ | inherit
parsedValue = parseContent();
break;
/* Start of supported CSS properties with validation. This is needed for parseShorthand to work
* correctly and allows optimization in blink::applyRule(..)
*/
case CSSPropertyBackgroundColor: // <color> | inherit
case CSSPropertyBorderTopColor: // <color> | inherit
case CSSPropertyBorderRightColor:
case CSSPropertyBorderBottomColor:
case CSSPropertyBorderLeftColor:
parsedValue = parseColor(m_valueList->current(), inQuirksMode() && (!inShorthand() || m_currentShorthand == CSSPropertyBorderColor));
if (parsedValue)
m_valueList->next();
break;
case CSSPropertyImageOrientation:
if (RuntimeEnabledFeatures::imageOrientationEnabled())
validPrimitive = value->id == CSSValueFromImage || (value->unit() != CSSPrimitiveValue::UnitType::Number && validUnit(value, FAngle) && value->fValue == 0);
break;
case CSSPropertyBackgroundBlendMode:
case CSSPropertyBackgroundAttachment:
case CSSPropertyBackgroundClip:
case CSSPropertyWebkitBackgroundClip:
case CSSPropertyWebkitBackgroundComposite:
case CSSPropertyBackgroundImage:
case CSSPropertyBackgroundOrigin:
case CSSPropertyMaskSourceType:
case CSSPropertyWebkitBackgroundOrigin:
case CSSPropertyBackgroundPosition:
case CSSPropertyBackgroundPositionX:
case CSSPropertyBackgroundPositionY:
case CSSPropertyBackgroundSize:
case CSSPropertyBackgroundRepeat:
case CSSPropertyWebkitMaskClip:
case CSSPropertyWebkitMaskComposite:
case CSSPropertyWebkitMaskImage:
case CSSPropertyWebkitMaskOrigin:
case CSSPropertyWebkitMaskPosition:
case CSSPropertyWebkitMaskPositionX:
case CSSPropertyWebkitMaskPositionY:
case CSSPropertyWebkitMaskSize:
case CSSPropertyWebkitMaskRepeat:
case CSSPropertyWebkitMaskRepeatX:
case CSSPropertyWebkitMaskRepeatY:
{
RefPtrWillBeRawPtr<CSSValue> val1 = nullptr;
RefPtrWillBeRawPtr<CSSValue> val2 = nullptr;
CSSPropertyID propId1, propId2;
bool result = false;
if (parseFillProperty(unresolvedProperty, propId1, propId2, val1, val2)) {
if (propId == CSSPropertyBackgroundPosition ||
propId == CSSPropertyBackgroundRepeat ||
propId == CSSPropertyWebkitMaskPosition ||
propId == CSSPropertyWebkitMaskRepeat) {
ShorthandScope scope(this, propId);
addProperty(propId1, val1.release(), important);
if (val2)
addProperty(propId2, val2.release(), important);
} else {
addProperty(propId1, val1.release(), important);
if (val2)
addProperty(propId2, val2.release(), important);
}
result = true;
}
m_implicitShorthand = false;
return result;
}
case CSSPropertyListStyleImage: // <uri> | none | inherit
case CSSPropertyBorderImageSource:
case CSSPropertyWebkitMaskBoxImageSource:
if (parseFillImage(m_valueList, parsedValue))
m_valueList->next();
break;
case CSSPropertyBorderTopWidth: //// <border-width> | inherit
case CSSPropertyBorderRightWidth: // Which is defined as
case CSSPropertyBorderBottomWidth: // thin | medium | thick | <length>
case CSSPropertyBorderLeftWidth:
if (!inShorthand() || m_currentShorthand == CSSPropertyBorderWidth)
unitless = FUnitlessQuirk;
if (id == CSSValueThin || id == CSSValueMedium || id == CSSValueThick)
validPrimitive = true;
else
validPrimitive = validUnit(value, FLength | FNonNeg | unitless);
break;
case CSSPropertyVerticalAlign:
// baseline | sub | super | top | text-top | middle | bottom | text-bottom |
// <percentage> | <length> | inherit
if (id >= CSSValueBaseline && id <= CSSValueWebkitBaselineMiddle)
validPrimitive = true;
else
validPrimitive = validUnit(value, FLength | FPercent | FUnitlessQuirk);
break;
case CSSPropertyBottom: // <length> | <percentage> | auto | inherit
case CSSPropertyLeft: // <length> | <percentage> | auto | inherit
case CSSPropertyRight: // <length> | <percentage> | auto | inherit
case CSSPropertyTop: // <length> | <percentage> | auto | inherit
if (id == CSSValueAuto)
validPrimitive = true;
else
validPrimitive = validUnit(value, FLength | FPercent | FUnitlessQuirk);
break;
case CSSPropertyTextUnderlinePosition:
// auto | [ under || [ left | right ] ], but we only support auto | under for now
ASSERT(RuntimeEnabledFeatures::css3TextDecorationsEnabled());
validPrimitive = (id == CSSValueAuto || id == CSSValueUnder);
break;
case CSSPropertySrc:
case CSSPropertyUnicodeRange:
/* @font-face only descriptors */
break;
/* CSS3 properties */
case CSSPropertyBorderImage:
case CSSPropertyWebkitMaskBoxImage:
return parseBorderImageShorthand(propId, important);
case CSSPropertyWebkitBorderImage: {
if (RefPtrWillBeRawPtr<CSSValue> result = parseBorderImage(propId)) {
addProperty(propId, result, important);
return true;
}
return false;
}
case CSSPropertyBorderImageOutset:
case CSSPropertyWebkitMaskBoxImageOutset: {
RefPtrWillBeRawPtr<CSSQuadValue> result = nullptr;
if (parseBorderImageOutset(result)) {
addProperty(propId, result, important);
return true;
}
break;
}
case CSSPropertyBorderImageRepeat:
case CSSPropertyWebkitMaskBoxImageRepeat: {
RefPtrWillBeRawPtr<CSSValue> result = nullptr;
if (parseBorderImageRepeat(result)) {
addProperty(propId, result, important);
return true;
}
break;
}
case CSSPropertyBorderImageSlice:
case CSSPropertyWebkitMaskBoxImageSlice: {
RefPtrWillBeRawPtr<CSSBorderImageSliceValue> result = nullptr;
if (parseBorderImageSlice(propId, result)) {
addProperty(propId, result, important);
return true;
}
break;
}
case CSSPropertyBorderImageWidth:
case CSSPropertyWebkitMaskBoxImageWidth: {
RefPtrWillBeRawPtr<CSSQuadValue> result = nullptr;
if (parseBorderImageWidth(result)) {
addProperty(propId, result, important);
return true;
}
break;
}
case CSSPropertyBorderTopRightRadius:
case CSSPropertyBorderTopLeftRadius:
case CSSPropertyBorderBottomLeftRadius:
case CSSPropertyBorderBottomRightRadius: {
validPrimitive = validUnit(value, FLength | FPercent | FNonNeg);
if (!validPrimitive)
return false;
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue1 = createPrimitiveNumericValue(value);
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue2 = nullptr;
value = m_valueList->next();
if (value) {
validPrimitive = validUnit(value, FLength | FPercent | FNonNeg);
if (!validPrimitive)
return false;
parsedValue2 = createPrimitiveNumericValue(value);
} else
parsedValue2 = parsedValue1;
if (m_valueList->next())
return false;
addProperty(propId, CSSValuePair::create(parsedValue1.release(), parsedValue2.release(), CSSValuePair::DropIdenticalValues), important);
return true;
}
case CSSPropertyBorderRadius:
case CSSPropertyAliasWebkitBorderRadius:
return parseBorderRadius(unresolvedProperty, important);
case CSSPropertyWebkitBoxReflect:
if (id == CSSValueNone)
validPrimitive = true;
else
parsedValue = parseReflect();
break;
case CSSPropertyFontSizeAdjust: // none | <number>
ASSERT(RuntimeEnabledFeatures::cssFontSizeAdjustEnabled());
validPrimitive = (id == CSSValueNone) ? true : validUnit(value, FNumber | FNonNeg);
break;
case CSSPropertyOpacity:
case CSSPropertyWebkitBoxFlex:
validPrimitive = validUnit(value, FNumber);
break;
case CSSPropertyWebkitBoxFlexGroup:
validPrimitive = validUnit(value, FInteger | FNonNeg);
break;
case CSSPropertyWebkitBoxOrdinalGroup:
validPrimitive = validUnit(value, FInteger | FNonNeg) && value->fValue;
break;
case CSSPropertyOrder:
validPrimitive = validUnit(value, FInteger);
break;
case CSSPropertyWebkitPerspectiveOriginX:
case CSSPropertyWebkitTransformOriginX:
parsedValue = parseFillPositionX(m_valueList);
if (parsedValue)
m_valueList->next();
break;
case CSSPropertyWebkitPerspectiveOriginY:
case CSSPropertyWebkitTransformOriginY:
parsedValue = parseFillPositionY(m_valueList);
if (parsedValue)
m_valueList->next();
break;
case CSSPropertyWebkitTransformOriginZ:
validPrimitive = validUnit(value, FLength);
break;
case CSSPropertyPerspective:
if (id == CSSValueNone) {
validPrimitive = true;
} else if (validUnit(value, FLength) && (m_parsedCalculation || value->fValue > 0)) {
validPrimitive = true;
} else if (unresolvedProperty == CSSPropertyAliasWebkitPerspective && validUnit(value, FNumber) && value->fValue > 0) {
value->setUnit(CSSPrimitiveValue::UnitType::Pixels);
validPrimitive = true;
} else {
return false;
}
break;
case CSSPropertyJustifyContent:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseContentDistributionOverflowPosition();
break;
case CSSPropertyJustifySelf:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseItemPositionOverflowPosition(propId, important);
case CSSPropertyJustifyItems:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
if (parseLegacyPosition(propId, important))
return true;
m_valueList->setCurrentIndex(0);
return parseItemPositionOverflowPosition(propId, important);
case CSSPropertyGridAutoFlow:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseGridAutoFlow(*m_valueList);
break;
case CSSPropertyGridAutoColumns:
case CSSPropertyGridAutoRows:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseGridTrackSize(*m_valueList);
break;
case CSSPropertyGridTemplateColumns:
case CSSPropertyGridTemplateRows:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseGridTrackList();
break;
case CSSPropertyGridColumnEnd:
case CSSPropertyGridColumnStart:
case CSSPropertyGridRowEnd:
case CSSPropertyGridRowStart:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseGridPosition();
break;
case CSSPropertyGridColumnGap:
case CSSPropertyGridRowGap:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
validPrimitive = validUnit(value, FLength | FNonNeg);
break;
case CSSPropertyGridGap:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseGridGapShorthand(important);
case CSSPropertyGridColumn:
case CSSPropertyGridRow:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseGridItemPositionShorthand(propId, important);
case CSSPropertyGridArea:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseGridAreaShorthand(important);
case CSSPropertyGridTemplateAreas:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseGridTemplateAreas();
break;
case CSSPropertyGridTemplate:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseGridTemplateShorthand(important);
case CSSPropertyGrid:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseGridShorthand(important);
/* shorthand properties */
case CSSPropertyBackground: {
// Position must come before color in this array because a plain old "0" is a legal color
// in quirks mode but it's usually the X coordinate of a position.
const CSSPropertyID properties[] = { CSSPropertyBackgroundImage, CSSPropertyBackgroundRepeat,
CSSPropertyBackgroundAttachment, CSSPropertyBackgroundPosition, CSSPropertyBackgroundOrigin,
CSSPropertyBackgroundClip, CSSPropertyBackgroundColor, CSSPropertyBackgroundSize };
return parseFillShorthand(propId, properties, WTF_ARRAY_LENGTH(properties), important);
}
case CSSPropertyWebkitMask: {
const CSSPropertyID properties[] = { CSSPropertyWebkitMaskImage, CSSPropertyWebkitMaskRepeat,
CSSPropertyWebkitMaskPosition, CSSPropertyWebkitMaskOrigin, CSSPropertyWebkitMaskClip, CSSPropertyWebkitMaskSize };
return parseFillShorthand(propId, properties, WTF_ARRAY_LENGTH(properties), important);
}
case CSSPropertyBorder:
// [ 'border-width' || 'border-style' || <color> ] | inherit
{
if (parseShorthand(propId, borderShorthandForParsing(), important)) {
// The CSS3 Borders and Backgrounds specification says that border also resets border-image. It's as
// though a value of none was specified for the image.
addExpandedPropertyForValue(CSSPropertyBorderImage, cssValuePool().createImplicitInitialValue(), important);
return true;
}
return false;
}
case CSSPropertyBorderTop:
// [ 'border-top-width' || 'border-style' || <color> ] | inherit
return parseShorthand(propId, borderTopShorthand(), important);
case CSSPropertyBorderRight:
// [ 'border-right-width' || 'border-style' || <color> ] | inherit
return parseShorthand(propId, borderRightShorthand(), important);
case CSSPropertyBorderBottom:
// [ 'border-bottom-width' || 'border-style' || <color> ] | inherit
return parseShorthand(propId, borderBottomShorthand(), important);
case CSSPropertyBorderLeft:
// [ 'border-left-width' || 'border-style' || <color> ] | inherit
return parseShorthand(propId, borderLeftShorthand(), important);
case CSSPropertyBorderColor:
// <color>{1,4} | inherit
return parse4Values(propId, borderColorShorthand().properties(), important);
case CSSPropertyBorderWidth:
// <border-width>{1,4} | inherit
return parse4Values(propId, borderWidthShorthand().properties(), important);
case CSSPropertyBorderStyle:
// <border-style>{1,4} | inherit
return parse4Values(propId, borderStyleShorthand().properties(), important);
case CSSPropertyListStyle:
return parseShorthand(propId, listStyleShorthand(), important);
case CSSPropertyInvalid:
return false;
case CSSPropertyWebkitClipPath:
if (id == CSSValueNone) {
validPrimitive = true;
} else if (value->m_unit == CSSParserValue::Function) {
parsedValue = parseBasicShape();
} else if (value->m_unit == CSSParserValue::URI) {
parsedValue = CSSURIValue::create(value->string);
addProperty(propId, parsedValue.release(), important);
return true;
}
break;
case CSSPropertyShapeOutside:
if (id == CSSValueNone)
validPrimitive = true;
else
parsedValue = parseShapeProperty(propId);
break;
case CSSPropertyShapeMargin:
validPrimitive = validUnit(value, FLength | FPercent | FNonNeg);
break;
case CSSPropertyShapeImageThreshold:
validPrimitive = validUnit(value, FNumber);
break;
case CSSPropertyAlignContent:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
parsedValue = parseContentDistributionOverflowPosition();
break;
case CSSPropertyAlignSelf:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseItemPositionOverflowPosition(propId, important);
case CSSPropertyAlignItems:
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
return parseItemPositionOverflowPosition(propId, important);
// Properties below are validated inside parseViewportProperty, because we
// check for parser state. We need to invalidate if someone adds them outside
// a @viewport rule.
case CSSPropertyMaxZoom:
case CSSPropertyMinZoom:
case CSSPropertyOrientation:
case CSSPropertyUserZoom:
validPrimitive = false;
break;
// These were not accepted by the new path above so we should return false.
case CSSPropertyWebkitMarginCollapse:
case CSSPropertyWillChange:
case CSSPropertyPage:
case CSSPropertyOverflow:
case CSSPropertyQuotes:
case CSSPropertyWebkitHighlight:
case CSSPropertyFontVariantLigatures:
case CSSPropertyFontFeatureSettings:
case CSSPropertyFontVariant:
case CSSPropertyFontFamily:
case CSSPropertyFontWeight:
case CSSPropertyLetterSpacing:
case CSSPropertyWordSpacing:
case CSSPropertyTabSize:
case CSSPropertyFontSize:
case CSSPropertyLineHeight:
case CSSPropertyRotate:
case CSSPropertyFont:
case CSSPropertyWebkitBorderHorizontalSpacing:
case CSSPropertyWebkitBorderVerticalSpacing:
case CSSPropertyBorderSpacing:
case CSSPropertyCounterIncrement:
case CSSPropertyCounterReset:
case CSSPropertySize:
case CSSPropertyTextIndent:
case CSSPropertyMaxWidth:
case CSSPropertyMaxHeight:
case CSSPropertyWebkitMaxLogicalWidth:
case CSSPropertyWebkitMaxLogicalHeight:
case CSSPropertyMinWidth:
case CSSPropertyMinHeight:
case CSSPropertyWidth:
case CSSPropertyHeight:
case CSSPropertyWebkitMinLogicalWidth:
case CSSPropertyWebkitMinLogicalHeight:
case CSSPropertyWebkitLogicalWidth:
case CSSPropertyWebkitLogicalHeight:
case CSSPropertyObjectPosition:
case CSSPropertyPerspectiveOrigin:
case CSSPropertyClip:
case CSSPropertyTouchAction:
case CSSPropertyWebkitLineClamp:
case CSSPropertyWebkitFontSizeDelta:
case CSSPropertyWebkitHyphenateCharacter:
case CSSPropertyWebkitLocale:
case CSSPropertyWebkitColumnWidth:
case CSSPropertyWebkitColumnCount:
case CSSPropertyWebkitColumns:
case CSSPropertyWebkitColumnGap:
case CSSPropertyWebkitColumnSpan:
case CSSPropertyZoom:
case CSSPropertyAnimation:
case CSSPropertyTransition:
case CSSPropertyAnimationDelay:
case CSSPropertyTransitionDelay:
case CSSPropertyAnimationDirection:
case CSSPropertyAnimationDuration:
case CSSPropertyTransitionDuration:
case CSSPropertyAnimationFillMode:
case CSSPropertyAnimationIterationCount:
case CSSPropertyAnimationName:
case CSSPropertyAnimationPlayState:
case CSSPropertyAnimationTimingFunction:
case CSSPropertyTransitionTimingFunction:
case CSSPropertyTransitionProperty:
case CSSPropertyOrphans:
case CSSPropertyWidows:
case CSSPropertyWebkitTapHighlightColor:
case CSSPropertyWebkitTextFillColor:
case CSSPropertyColor:
case CSSPropertyZIndex:
case CSSPropertyTextShadow:
case CSSPropertyBoxShadow:
case CSSPropertyWebkitFilter:
case CSSPropertyBackdropFilter:
case CSSPropertyTextDecorationColor:
case CSSPropertyWebkitTextDecorationsInEffect:
case CSSPropertyTextDecorationLine:
case CSSPropertyTextDecoration:
case CSSPropertyMotionPath:
case CSSPropertyMotionOffset:
case CSSPropertyMotionRotation:
case CSSPropertyMotion:
case CSSPropertyWebkitTextEmphasisColor:
case CSSPropertyWebkitTextEmphasisStyle:
case CSSPropertyWebkitTextEmphasis:
case CSSPropertyOutline:
case CSSPropertyOutlineColor:
case CSSPropertyOutlineWidth:
case CSSPropertyOutlineOffset:
case CSSPropertyWebkitBorderStartColor:
case CSSPropertyWebkitBorderEndColor:
case CSSPropertyWebkitBorderBeforeColor:
case CSSPropertyWebkitBorderAfterColor:
case CSSPropertyWebkitBorderStartWidth:
case CSSPropertyWebkitBorderEndWidth:
case CSSPropertyWebkitBorderBeforeWidth:
case CSSPropertyWebkitBorderAfterWidth:
case CSSPropertyWebkitBorderStart:
case CSSPropertyWebkitBorderEnd:
case CSSPropertyWebkitBorderBefore:
case CSSPropertyWebkitBorderAfter:
case CSSPropertyWebkitTextStroke:
case CSSPropertyWebkitTextStrokeColor:
case CSSPropertyWebkitTextStrokeWidth:
case CSSPropertyTransform:
case CSSPropertyFill:
case CSSPropertyStroke:
case CSSPropertyStopColor:
case CSSPropertyFloodColor:
case CSSPropertyLightingColor:
case CSSPropertyPaintOrder:
case CSSPropertyMarginTop:
case CSSPropertyMarginRight:
case CSSPropertyMarginBottom:
case CSSPropertyMarginLeft:
case CSSPropertyMargin:
case CSSPropertyWebkitMarginStart:
case CSSPropertyWebkitMarginEnd:
case CSSPropertyWebkitMarginBefore:
case CSSPropertyWebkitMarginAfter:
case CSSPropertyPaddingTop:
case CSSPropertyPaddingRight:
case CSSPropertyPaddingBottom:
case CSSPropertyPaddingLeft:
case CSSPropertyPadding:
case CSSPropertyWebkitPaddingStart:
case CSSPropertyWebkitPaddingEnd:
case CSSPropertyWebkitPaddingBefore:
case CSSPropertyWebkitPaddingAfter:
case CSSPropertyMarker:
case CSSPropertyMarkerStart:
case CSSPropertyMarkerMid:
case CSSPropertyMarkerEnd:
case CSSPropertyFlex:
case CSSPropertyFlexBasis:
case CSSPropertyFlexGrow:
case CSSPropertyFlexShrink:
case CSSPropertyFlexFlow:
case CSSPropertyStrokeDasharray:
case CSSPropertyWebkitColumnRule:
case CSSPropertyWebkitColumnRuleColor:
case CSSPropertyWebkitColumnRuleWidth:
case CSSPropertyClipPath:
case CSSPropertyFilter:
case CSSPropertyMask:
case CSSPropertyStrokeOpacity:
case CSSPropertyFillOpacity:
case CSSPropertyStopOpacity:
case CSSPropertyFloodOpacity:
case CSSPropertyBaselineShift:
case CSSPropertyStrokeMiterlimit:
case CSSPropertyStrokeWidth:
case CSSPropertyStrokeDashoffset:
case CSSPropertyCx:
case CSSPropertyCy:
case CSSPropertyX:
case CSSPropertyY:
case CSSPropertyR:
case CSSPropertyRx:
case CSSPropertyRy:
case CSSPropertyScale:
case CSSPropertyTranslate:
case CSSPropertyCursor:
case CSSPropertyTransformOrigin:
validPrimitive = false;
break;
case CSSPropertyScrollSnapPointsX:
case CSSPropertyScrollSnapPointsY:
parsedValue = parseScrollSnapPoints();
break;
case CSSPropertyScrollSnapCoordinate:
parsedValue = parseScrollSnapCoordinate();
break;
case CSSPropertyScrollSnapDestination:
parsedValue = parsePosition(m_valueList);
break;
default:
// If you crash here, it's because you added a css property and are not handling it
// in either this switch statement or the one in CSSPropertyParser::parseSingleValue.
ASSERT_WITH_MESSAGE(0, "unimplemented propertyID: %d", propId);
return false;
}
if (validPrimitive) {
parsedValue = parseValidPrimitive(id, value);
m_valueList->next();
}
ASSERT(!m_parsedCalculation);
if (parsedValue) {
if (!m_valueList->current() || inShorthand()) {
addProperty(propId, parsedValue.release(), important);
return true;
}
}
return false;
}
void CSSPropertyParser::addFillValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval)
{
if (lval) {
if (lval->isBaseValueList())
toCSSValueList(lval.get())->append(rval);
else {
PassRefPtrWillBeRawPtr<CSSValue> oldlVal(lval.release());
PassRefPtrWillBeRawPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
list->append(oldlVal);
list->append(rval);
lval = list;
}
}
else
lval = rval;
}
static bool parseBackgroundClip(CSSParserValue* parserValue, RefPtrWillBeRawPtr<CSSValue>& cssValue)
{
if (parserValue->id == CSSValueBorderBox || parserValue->id == CSSValuePaddingBox
|| parserValue->id == CSSValueContentBox || parserValue->id == CSSValueWebkitText) {
cssValue = cssValuePool().createIdentifierValue(parserValue->id);
return true;
}
return false;
}
const int cMaxFillProperties = 9;
bool CSSPropertyParser::parseFillShorthand(CSSPropertyID propId, const CSSPropertyID* properties, int numProperties, bool important)
{
ASSERT(numProperties <= cMaxFillProperties);
if (numProperties > cMaxFillProperties)
return false;
ShorthandScope scope(this, propId);
bool parsedProperty[cMaxFillProperties] = { false };
RefPtrWillBeRawPtr<CSSValue> values[cMaxFillProperties];
#if ENABLE(OILPAN)
// Zero initialize the array of raw pointers.
memset(&values, 0, sizeof(values));
#endif
RefPtrWillBeRawPtr<CSSValue> clipValue = nullptr;
RefPtrWillBeRawPtr<CSSValue> positionYValue = nullptr;
RefPtrWillBeRawPtr<CSSValue> repeatYValue = nullptr;
bool foundClip = false;
int i;
bool foundPositionCSSProperty = false;
while (m_valueList->current()) {
CSSParserValue* val = m_valueList->current();
if (isComma(val)) {
// We hit the end. Fill in all remaining values with the initial value.
m_valueList->next();
for (i = 0; i < numProperties; ++i) {
if (properties[i] == CSSPropertyBackgroundColor && parsedProperty[i])
// Color is not allowed except as the last item in a list for backgrounds.
// Reject the entire property.
return false;
if (!parsedProperty[i] && properties[i] != CSSPropertyBackgroundColor) {
addFillValue(values[i], cssValuePool().createImplicitInitialValue());
if (properties[i] == CSSPropertyBackgroundPosition || properties[i] == CSSPropertyWebkitMaskPosition)
addFillValue(positionYValue, cssValuePool().createImplicitInitialValue());
if (properties[i] == CSSPropertyBackgroundRepeat || properties[i] == CSSPropertyWebkitMaskRepeat)
addFillValue(repeatYValue, cssValuePool().createImplicitInitialValue());
if ((properties[i] == CSSPropertyBackgroundOrigin || properties[i] == CSSPropertyWebkitMaskOrigin) && !parsedProperty[i]) {
// If background-origin wasn't present, then reset background-clip also.
addFillValue(clipValue, cssValuePool().createImplicitInitialValue());
}
}
parsedProperty[i] = false;
}
if (!m_valueList->current())
break;
}
bool sizeCSSPropertyExpected = false;
if (isForwardSlashOperator(val) && foundPositionCSSProperty) {
sizeCSSPropertyExpected = true;
m_valueList->next();
}
foundPositionCSSProperty = false;
bool found = false;
for (i = 0; !found && i < numProperties; ++i) {
if (sizeCSSPropertyExpected && (properties[i] != CSSPropertyBackgroundSize && properties[i] != CSSPropertyWebkitMaskSize))
continue;
if (!sizeCSSPropertyExpected && (properties[i] == CSSPropertyBackgroundSize || properties[i] == CSSPropertyWebkitMaskSize))
continue;
if (!parsedProperty[i]) {
RefPtrWillBeRawPtr<CSSValue> val1 = nullptr;
RefPtrWillBeRawPtr<CSSValue> val2 = nullptr;
CSSPropertyID propId1, propId2;
CSSParserValue* parserValue = m_valueList->current();
// parseFillProperty() may modify m_implicitShorthand, so we MUST reset it
// before EACH return below.
if (parserValue && parseFillProperty(properties[i], propId1, propId2, val1, val2)) {
parsedProperty[i] = found = true;
addFillValue(values[i], val1.release());
if (properties[i] == CSSPropertyBackgroundPosition || properties[i] == CSSPropertyWebkitMaskPosition)
addFillValue(positionYValue, val2.release());
if (properties[i] == CSSPropertyBackgroundRepeat || properties[i] == CSSPropertyWebkitMaskRepeat)
addFillValue(repeatYValue, val2.release());
if (properties[i] == CSSPropertyBackgroundOrigin || properties[i] == CSSPropertyWebkitMaskOrigin) {
// Reparse the value as a clip, and see if we succeed.
if (parseBackgroundClip(parserValue, val1))
addFillValue(clipValue, val1.release()); // The property parsed successfully.
else
addFillValue(clipValue, cssValuePool().createImplicitInitialValue()); // Some value was used for origin that is not supported by clip. Just reset clip instead.
}
if (properties[i] == CSSPropertyBackgroundClip || properties[i] == CSSPropertyWebkitMaskClip) {
// Update clipValue
addFillValue(clipValue, val1.release());
foundClip = true;
}
if (properties[i] == CSSPropertyBackgroundPosition || properties[i] == CSSPropertyWebkitMaskPosition)
foundPositionCSSProperty = true;
}
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found) {
m_implicitShorthand = false;
return false;
}
}
// Now add all of the properties we found.
for (i = 0; i < numProperties; i++) {
// Fill in any remaining properties with the initial value.
if (!parsedProperty[i]) {
addFillValue(values[i], cssValuePool().createImplicitInitialValue());
if (properties[i] == CSSPropertyBackgroundPosition || properties[i] == CSSPropertyWebkitMaskPosition)
addFillValue(positionYValue, cssValuePool().createImplicitInitialValue());
if (properties[i] == CSSPropertyBackgroundRepeat || properties[i] == CSSPropertyWebkitMaskRepeat)
addFillValue(repeatYValue, cssValuePool().createImplicitInitialValue());
if (properties[i] == CSSPropertyBackgroundOrigin || properties[i] == CSSPropertyWebkitMaskOrigin) {
// If background-origin wasn't present, then reset background-clip also.
addFillValue(clipValue, cssValuePool().createImplicitInitialValue());
}
}
if (properties[i] == CSSPropertyBackgroundPosition) {
addProperty(CSSPropertyBackgroundPositionX, values[i].release(), important);
// it's OK to call positionYValue.release() since we only see CSSPropertyBackgroundPosition once
addProperty(CSSPropertyBackgroundPositionY, positionYValue.release(), important);
} else if (properties[i] == CSSPropertyWebkitMaskPosition) {
addProperty(CSSPropertyWebkitMaskPositionX, values[i].release(), important);
// it's OK to call positionYValue.release() since we only see CSSPropertyWebkitMaskPosition once
addProperty(CSSPropertyWebkitMaskPositionY, positionYValue.release(), important);
} else if (properties[i] == CSSPropertyBackgroundRepeat) {
addProperty(CSSPropertyBackgroundRepeatX, values[i].release(), important);
// it's OK to call repeatYValue.release() since we only see CSSPropertyBackgroundPosition once
addProperty(CSSPropertyBackgroundRepeatY, repeatYValue.release(), important);
} else if (properties[i] == CSSPropertyWebkitMaskRepeat) {
addProperty(CSSPropertyWebkitMaskRepeatX, values[i].release(), important);
// it's OK to call repeatYValue.release() since we only see CSSPropertyBackgroundPosition once
addProperty(CSSPropertyWebkitMaskRepeatY, repeatYValue.release(), important);
} else if ((properties[i] == CSSPropertyBackgroundClip || properties[i] == CSSPropertyWebkitMaskClip) && !foundClip)
// Value is already set while updating origin
continue;
else if (properties[i] == CSSPropertyBackgroundSize && !parsedProperty[i] && m_context.useLegacyBackgroundSizeShorthandBehavior())
continue;
else
addProperty(properties[i], values[i].release(), important);
// Add in clip values when we hit the corresponding origin property.
if (properties[i] == CSSPropertyBackgroundOrigin && !foundClip)
addProperty(CSSPropertyBackgroundClip, clipValue.release(), important);
else if (properties[i] == CSSPropertyWebkitMaskOrigin && !foundClip)
addProperty(CSSPropertyWebkitMaskClip, clipValue.release(), important);
}
m_implicitShorthand = false;
return true;
}
bool CSSPropertyParser::parseShorthand(CSSPropertyID propId, const StylePropertyShorthand& shorthand, bool important)
{
// We try to match as many properties as possible
// We set up an array of booleans to mark which property has been found,
// and we try to search for properties until it makes no longer any sense.
ShorthandScope scope(this, propId);
bool found = false;
unsigned propertiesParsed = 0;
bool propertyFound[6] = { false, false, false, false, false, false }; // 6 is enough size.
while (m_valueList->current()) {
found = false;
for (unsigned propIndex = 0; !found && propIndex < shorthand.length(); ++propIndex) {
if (!propertyFound[propIndex] && parseValue(shorthand.properties()[propIndex], important)) {
propertyFound[propIndex] = found = true;
propertiesParsed++;
}
}
// if we didn't find at least one match, this is an
// invalid shorthand and we have to ignore it
if (!found)
return false;
}
if (propertiesParsed == shorthand.length())
return true;
// Fill in any remaining properties with the initial value.
ImplicitScope implicitScope(this);
const StylePropertyShorthand* const* const propertiesForInitialization = shorthand.propertiesForInitialization();
for (unsigned i = 0; i < shorthand.length(); ++i) {
if (propertyFound[i])
continue;
if (propertiesForInitialization) {
const StylePropertyShorthand& initProperties = *(propertiesForInitialization[i]);
for (unsigned propIndex = 0; propIndex < initProperties.length(); ++propIndex)
addProperty(initProperties.properties()[propIndex], cssValuePool().createImplicitInitialValue(), important);
} else
addProperty(shorthand.properties()[i], cssValuePool().createImplicitInitialValue(), important);
}
return true;
}
bool CSSPropertyParser::parse4Values(CSSPropertyID propId, const CSSPropertyID *properties, bool important)
{
/* From the CSS 2 specs, 8.3
* If there is only one value, it applies to all sides. If there are two values, the top and
* bottom margins are set to the first value and the right and left margins are set to the second.
* If there are three values, the top is set to the first value, the left and right are set to the
* second, and the bottom is set to the third. If there are four values, they apply to the top,
* right, bottom, and left, respectively.
*/
int num = inShorthand() ? 1 : m_valueList->size();
ShorthandScope scope(this, propId);
// the order is top, right, bottom, left
switch (num) {
case 1: {
if (!parseValue(properties[0], important))
return false;
CSSValue* value = m_parsedProperties.last().value();
ImplicitScope implicitScope(this);
addProperty(properties[1], value, important);
addProperty(properties[2], value, important);
addProperty(properties[3], value, important);
break;
}
case 2: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important))
return false;
CSSValue* value = m_parsedProperties[m_parsedProperties.size() - 2].value();
ImplicitScope implicitScope(this);
addProperty(properties[2], value, important);
value = m_parsedProperties[m_parsedProperties.size() - 2].value();
addProperty(properties[3], value, important);
break;
}
case 3: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) || !parseValue(properties[2], important))
return false;
CSSValue* value = m_parsedProperties[m_parsedProperties.size() - 2].value();
ImplicitScope implicitScope(this);
addProperty(properties[3], value, important);
break;
}
case 4: {
if (!parseValue(properties[0], important) || !parseValue(properties[1], important) ||
!parseValue(properties[2], important) || !parseValue(properties[3], important))
return false;
break;
}
default: {
return false;
}
}
return true;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseScrollSnapPoints()
{
CSSParserValue* value = m_valueList->current();
if (value->id == CSSValueNone) {
m_valueList->next();
return cssValuePool().createIdentifierValue(CSSValueNone);
}
if (value->m_unit == CSSParserValue::Function && value->function->id == CSSValueRepeat) {
// The spec defines the following grammar: repeat( <length>)
CSSParserValueList* arguments = value->function->args.get();
if (!arguments || arguments->size() != 1)
return nullptr;
CSSParserValue* repeatValue = arguments->valueAt(0);
if (validUnit(repeatValue, FNonNeg | FLength | FPercent) && (m_parsedCalculation || repeatValue->fValue > 0)) {
RefPtrWillBeRawPtr<CSSFunctionValue> result = CSSFunctionValue::create(CSSValueRepeat);
result->append(parseValidPrimitive(repeatValue->id, repeatValue));
m_valueList->next();
return result.release();
}
}
return nullptr;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseScrollSnapCoordinate()
{
if (m_valueList->current()->id == CSSValueNone) {
m_valueList->next();
return cssValuePool().createIdentifierValue(CSSValueNone);
}
return parsePositionList(m_valueList);
}
// [ <string> | <uri> | <counter> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
// in CSS 2.1 this got somewhat reduced:
// [ <string> | attr(X) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
PassRefPtrWillBeRawPtr<CSSValueList> CSSPropertyParser::parseContent()
{
RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
while (CSSParserValue* val = m_valueList->current()) {
RefPtrWillBeRawPtr<CSSValue> parsedValue = nullptr;
if (val->m_unit == CSSParserValue::URI) {
// url
parsedValue = createCSSImageValueWithReferrer(val->string, completeURL(val->string));
} else if (val->m_unit == CSSParserValue::Function) {
// attr(X) | counter(X [,Y]) | counters(X, Y, [,Z]) | -webkit-gradient(...)
CSSParserValueList* args = val->function->args.get();
if (!args)
return nullptr;
if (val->function->id == CSSValueAttr) {
parsedValue = parseAttr(args);
} else if (val->function->id == CSSValueCounter) {
parsedValue = parseCounterContent(args, false);
} else if (val->function->id == CSSValueCounters) {
parsedValue = parseCounterContent(args, true);
} else if (val->function->id == CSSValueWebkitImageSet) {
parsedValue = parseImageSet(m_valueList);
} else if (isGeneratedImageValue(val->function->id)) {
if (!parseGeneratedImage(m_valueList, parsedValue))
return nullptr;
}
} else if (val->m_unit == CSSParserValue::Identifier) {
switch (val->id) {
case CSSValueOpenQuote:
case CSSValueCloseQuote:
case CSSValueNoOpenQuote:
case CSSValueNoCloseQuote:
case CSSValueNone:
case CSSValueNormal:
parsedValue = cssValuePool().createIdentifierValue(val->id);
default:
break;
}
} else if (val->m_unit == CSSParserValue::String) {
parsedValue = createPrimitiveStringValue(val);
}
if (!parsedValue)
return nullptr;
values->append(parsedValue.release());
m_valueList->next();
}
return values.release();
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseAttr(CSSParserValueList* args)
{
if (args->size() != 1)
return nullptr;
CSSParserValue* a = args->current();
if (a->m_unit != CSSParserValue::Identifier)
return nullptr;
String attrName = a->string;
// CSS allows identifiers with "-" at the start, like "-webkit-mask-image".
// But HTML attribute names can't have those characters, and we should not
// even parse them inside attr().
if (attrName[0] == '-')
return nullptr;
if (m_context.isHTMLDocument())
attrName = attrName.lower();
RefPtrWillBeRawPtr<CSSFunctionValue> attrValue = CSSFunctionValue::create(CSSValueAttr);
attrValue->append(CSSCustomIdentValue::create(attrName));
return attrValue.release();
}
bool CSSPropertyParser::isColorKeyword(CSSValueID id)
{
// Named colors and color keywords:
//
// <named-color>
// 'aqua', 'black', 'blue', ..., 'yellow' (CSS3: "basic color keywords")
// 'aliceblue', ..., 'yellowgreen' (CSS3: "extended color keywords")
// 'transparent'
//
// 'currentcolor'
//
// <deprecated-system-color>
// 'ActiveBorder', ..., 'WindowText'
//
// WebKit proprietary/internal:
// '-webkit-link'
// '-webkit-activelink'
// '-internal-active-list-box-selection'
// '-internal-active-list-box-selection-text'
// '-internal-inactive-list-box-selection'
// '-internal-inactive-list-box-selection-text'
// '-webkit-focus-ring-color'
// '-webkit-text'
//
return (id >= CSSValueAqua && id <= CSSValueWebkitText)
|| (id >= CSSValueAliceblue && id <= CSSValueYellowgreen)
|| id == CSSValueMenu;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseColor(const CSSParserValue* value, bool acceptQuirkyColors)
{
CSSValueID id = value->id;
if (isColorKeyword(id)) {
if (!isValueAllowedInMode(id, m_context.mode()))
return nullptr;
if (id == CSSValueWebkitText && m_context.useCounter())
m_context.useCounter()->count(UseCounter::WebkitTextInColorProperty);
return cssValuePool().createIdentifierValue(id);
}
RGBA32 c = Color::transparent;
if (!parseColorFromValue(value, c, acceptQuirkyColors))
return nullptr;
return cssValuePool().createColorValue(c);
}
bool CSSPropertyParser::parseFillImage(CSSParserValueList* valueList, RefPtrWillBeRawPtr<CSSValue>& value)
{
if (valueList->current()->id == CSSValueNone) {
value = cssValuePool().createIdentifierValue(CSSValueNone);
return true;
}
if (valueList->current()->m_unit == CSSParserValue::URI) {
value = createCSSImageValueWithReferrer(valueList->current()->string, completeURL(valueList->current()->string));
return true;
}
if (valueList->current()->m_unit == CSSParserValue::Function) {
if (isGeneratedImageValue(valueList->current()->function->id))
return parseGeneratedImage(valueList, value);
if (valueList->current()->function->id == CSSValueWebkitImageSet) {
value = parseImageSet(m_valueList);
if (value)
return true;
}
}
return false;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseFillPositionX(CSSParserValueList* valueList)
{
int id = valueList->current()->id;
if (id == CSSValueLeft || id == CSSValueRight || id == CSSValueCenter) {
int percent = 0;
if (id == CSSValueRight)
percent = 100;
else if (id == CSSValueCenter)
percent = 50;
return cssValuePool().createValue(percent, CSSPrimitiveValue::UnitType::Percentage);
}
if (validUnit(valueList->current(), FPercent | FLength))
return createPrimitiveNumericValue(valueList->current());
return nullptr;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseFillPositionY(CSSParserValueList* valueList)
{
int id = valueList->current()->id;
if (id == CSSValueTop || id == CSSValueBottom || id == CSSValueCenter) {
int percent = 0;
if (id == CSSValueBottom)
percent = 100;
else if (id == CSSValueCenter)
percent = 50;
return cssValuePool().createValue(percent, CSSPrimitiveValue::UnitType::Percentage);
}
if (validUnit(valueList->current(), FPercent | FLength))
return createPrimitiveNumericValue(valueList->current());
return nullptr;
}
PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPropertyParser::parseFillPositionComponent(CSSParserValueList* valueList, unsigned& cumulativeFlags, FillPositionFlag& individualFlag, FillPositionParsingMode parsingMode, Units unitless)
{
CSSValueID id = valueList->current()->id;
if (id == CSSValueLeft || id == CSSValueTop || id == CSSValueRight || id == CSSValueBottom || id == CSSValueCenter) {
int percent = 0;
if (id == CSSValueLeft || id == CSSValueRight) {
if (cumulativeFlags & XFillPosition)
return nullptr;
cumulativeFlags |= XFillPosition;
individualFlag = XFillPosition;
if (id == CSSValueRight)
percent = 100;
}
else if (id == CSSValueTop || id == CSSValueBottom) {
if (cumulativeFlags & YFillPosition)
return nullptr;
cumulativeFlags |= YFillPosition;
individualFlag = YFillPosition;
if (id == CSSValueBottom)
percent = 100;
} else if (id == CSSValueCenter) {
// Center is ambiguous, so we're not sure which position we've found yet, an x or a y.
percent = 50;
cumulativeFlags |= AmbiguousFillPosition;
individualFlag = AmbiguousFillPosition;
}
if (parsingMode == ResolveValuesAsKeyword)
return cssValuePool().createIdentifierValue(id);
return cssValuePool().createValue(percent, CSSPrimitiveValue::UnitType::Percentage);
}
if (validUnit(valueList->current(), FPercent | FLength | unitless)) {
if (!cumulativeFlags) {
cumulativeFlags |= XFillPosition;
individualFlag = XFillPosition;
} else if (cumulativeFlags & (XFillPosition | AmbiguousFillPosition)) {
cumulativeFlags |= YFillPosition;
individualFlag = YFillPosition;
} else {
if (m_parsedCalculation)
m_parsedCalculation.release();
return nullptr;
}
return createPrimitiveNumericValue(valueList->current());
}
return nullptr;
}
static bool isValueConflictingWithCurrentEdge(int value1, int value2)
{
if ((value1 == CSSValueLeft || value1 == CSSValueRight) && (value2 == CSSValueLeft || value2 == CSSValueRight))
return true;
if ((value1 == CSSValueTop || value1 == CSSValueBottom) && (value2 == CSSValueTop || value2 == CSSValueBottom))
return true;
return false;
}
static bool isFillPositionKeyword(CSSValueID value)
{
return value == CSSValueLeft || value == CSSValueTop || value == CSSValueBottom || value == CSSValueRight || value == CSSValueCenter;
}
void CSSPropertyParser::parse4ValuesFillPosition(CSSParserValueList* valueList, RefPtrWillBeRawPtr<CSSValue>& value1, RefPtrWillBeRawPtr<CSSValue>& value2, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue1, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue2)
{
// [ left | right ] [ <percentage] | <length> ] && [ top | bottom ] [ <percentage> | <length> ]
// In the case of 4 values <position> requires the second value to be a length or a percentage.
if (isFillPositionKeyword(parsedValue2->getValueID()))
return;
unsigned cumulativeFlags = 0;
FillPositionFlag value3Flag = InvalidFillPosition;
RefPtrWillBeRawPtr<CSSPrimitiveValue> value3 = parseFillPositionComponent(valueList, cumulativeFlags, value3Flag, ResolveValuesAsKeyword);
if (!value3)
return;
CSSValueID ident1 = parsedValue1->getValueID();
CSSValueID ident3 = value3->getValueID();
if (ident1 == CSSValueCenter)
return;
if (!isFillPositionKeyword(ident3) || ident3 == CSSValueCenter)
return;
// We need to check if the values are not conflicting, e.g. they are not on the same edge. It is
// needed as the second call to parseFillPositionComponent was on purpose not checking it. In the
// case of two values top 20px is invalid but in the case of 4 values it becomes valid.
if (isValueConflictingWithCurrentEdge(ident1, ident3))
return;
valueList->next();
cumulativeFlags = 0;
FillPositionFlag value4Flag = InvalidFillPosition;
RefPtrWillBeRawPtr<CSSPrimitiveValue> value4 = parseFillPositionComponent(valueList, cumulativeFlags, value4Flag, ResolveValuesAsKeyword);
if (!value4)
return;
// 4th value must be a length or a percentage.
if (isFillPositionKeyword(value4->getValueID()))
return;
value1 = CSSValuePair::create(parsedValue1, parsedValue2, CSSValuePair::DropIdenticalValues);
value2 = CSSValuePair::create(value3, value4, CSSValuePair::DropIdenticalValues);
if (ident1 == CSSValueTop || ident1 == CSSValueBottom)
value1.swap(value2);
valueList->next();
}
void CSSPropertyParser::parse3ValuesFillPosition(CSSParserValueList* valueList, RefPtrWillBeRawPtr<CSSValue>& value1, RefPtrWillBeRawPtr<CSSValue>& value2, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue1, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue2)
{
unsigned cumulativeFlags = 0;
FillPositionFlag value3Flag = InvalidFillPosition;
RefPtrWillBeRawPtr<CSSPrimitiveValue> value3 = parseFillPositionComponent(valueList, cumulativeFlags, value3Flag, ResolveValuesAsKeyword);
// value3 is not an expected value, we return.
if (!value3)
return;
valueList->next();
bool swapNeeded = false;
CSSValueID ident1 = parsedValue1->getValueID();
CSSValueID ident2 = parsedValue2->getValueID();
CSSValueID ident3 = value3->getValueID();
CSSValueID firstPositionKeyword;
CSSValueID secondPositionKeyword;
if (ident1 == CSSValueCenter) {
// <position> requires the first 'center' to be followed by a keyword.
if (!isFillPositionKeyword(ident2))
return;
// If 'center' is the first keyword then the last one needs to be a length.
if (isFillPositionKeyword(ident3))
return;
firstPositionKeyword = CSSValueLeft;
if (ident2 == CSSValueLeft || ident2 == CSSValueRight) {
firstPositionKeyword = CSSValueTop;
swapNeeded = true;
}
value1 = CSSValuePair::create(cssValuePool().createIdentifierValue(firstPositionKeyword), cssValuePool().createValue(50, CSSPrimitiveValue::UnitType::Percentage), CSSValuePair::DropIdenticalValues);
value2 = CSSValuePair::create(parsedValue2, value3, CSSValuePair::DropIdenticalValues);
} else if (ident3 == CSSValueCenter) {
if (isFillPositionKeyword(ident2))
return;
secondPositionKeyword = CSSValueTop;
if (ident1 == CSSValueTop || ident1 == CSSValueBottom) {
secondPositionKeyword = CSSValueLeft;
swapNeeded = true;
}
value1 = CSSValuePair::create(parsedValue1, parsedValue2, CSSValuePair::DropIdenticalValues);
value2 = CSSValuePair::create(cssValuePool().createIdentifierValue(secondPositionKeyword), cssValuePool().createValue(50, CSSPrimitiveValue::UnitType::Percentage), CSSValuePair::DropIdenticalValues);
} else {
RefPtrWillBeRawPtr<CSSPrimitiveValue> firstPositionValue = nullptr;
RefPtrWillBeRawPtr<CSSPrimitiveValue> secondPositionValue = nullptr;
if (isFillPositionKeyword(ident2)) {
// To match CSS grammar, we should only accept: [ center | left | right | bottom | top ] [ left | right | top | bottom ] [ <percentage> | <length> ].
ASSERT(ident2 != CSSValueCenter);
if (isFillPositionKeyword(ident3))
return;
secondPositionValue = value3;
secondPositionKeyword = ident2;
firstPositionValue = cssValuePool().createValue(0, CSSPrimitiveValue::UnitType::Percentage);
} else {
// Per CSS, we should only accept: [ right | left | top | bottom ] [ <percentage> | <length> ] [ center | left | right | bottom | top ].
if (!isFillPositionKeyword(ident3))
return;
firstPositionValue = parsedValue2;
secondPositionKeyword = ident3;
secondPositionValue = cssValuePool().createValue(0, CSSPrimitiveValue::UnitType::Percentage);
}
if (isValueConflictingWithCurrentEdge(ident1, secondPositionKeyword))
return;
value1 = CSSValuePair::create(parsedValue1, firstPositionValue, CSSValuePair::DropIdenticalValues);
value2 = CSSValuePair::create(cssValuePool().createIdentifierValue(secondPositionKeyword), secondPositionValue, CSSValuePair::DropIdenticalValues);
}
if (ident1 == CSSValueTop || ident1 == CSSValueBottom || swapNeeded)
value1.swap(value2);
#if ENABLE(ASSERT)
const CSSValuePair& first = toCSSValuePair(*value1);
const CSSValuePair& second = toCSSValuePair(*value2);
ident1 = toCSSPrimitiveValue(first.first()).getValueID();
ident2 = toCSSPrimitiveValue(second.first()).getValueID();
ASSERT(ident1 == CSSValueLeft || ident1 == CSSValueRight);
ASSERT(ident2 == CSSValueBottom || ident2 == CSSValueTop);
#endif
}
inline bool CSSPropertyParser::isPotentialPositionValue(CSSParserValue* value)
{
return isFillPositionKeyword(value->id) || validUnit(value, FPercent | FLength, ReleaseParsedCalcValue);
}
void CSSPropertyParser::parseFillPosition(CSSParserValueList* valueList, RefPtrWillBeRawPtr<CSSValue>& value1, RefPtrWillBeRawPtr<CSSValue>& value2, Units unitless)
{
unsigned numberOfValues = 0;
for (unsigned i = valueList->currentIndex(); i < valueList->size(); ++i, ++numberOfValues) {
CSSParserValue* current = valueList->valueAt(i);
if (!current || isComma(current) || isForwardSlashOperator(current) || !isPotentialPositionValue(current))
break;
}
if (numberOfValues > 4)
return;
// If we are parsing two values, we can safely call the CSS 2.1 parsing function and return.
if (numberOfValues <= 2) {
parse2ValuesFillPosition(valueList, value1, value2, unitless);
return;
}
ASSERT(numberOfValues > 2 && numberOfValues <= 4);
CSSParserValue* value = valueList->current();
// <position> requires the first value to be a background keyword.
if (!isFillPositionKeyword(value->id))
return;
// Parse the first value. We're just making sure that it is one of the valid keywords or a percentage/length.
unsigned cumulativeFlags = 0;
FillPositionFlag value1Flag = InvalidFillPosition;
FillPositionFlag value2Flag = InvalidFillPosition;
value1 = parseFillPositionComponent(valueList, cumulativeFlags, value1Flag, ResolveValuesAsKeyword);
if (!value1)
return;
valueList->next();
// In case we are parsing more than two values, relax the check inside of parseFillPositionComponent. top 20px is
// a valid start for <position>.
cumulativeFlags = AmbiguousFillPosition;
value2 = parseFillPositionComponent(valueList, cumulativeFlags, value2Flag, ResolveValuesAsKeyword);
if (value2)
valueList->next();
else {
value1.clear();
return;
}
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue1 = toCSSPrimitiveValue(value1.get());
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue2 = toCSSPrimitiveValue(value2.get());
value1.clear();
value2.clear();
// Per CSS3 syntax, <position> can't have 'center' as its second keyword as we have more arguments to follow.
if (parsedValue2->getValueID() == CSSValueCenter)
return;
if (numberOfValues == 3)
parse3ValuesFillPosition(valueList, value1, value2, parsedValue1.release(), parsedValue2.release());
else
parse4ValuesFillPosition(valueList, value1, value2, parsedValue1.release(), parsedValue2.release());
}
void CSSPropertyParser::parse2ValuesFillPosition(CSSParserValueList* valueList, RefPtrWillBeRawPtr<CSSValue>& value1, RefPtrWillBeRawPtr<CSSValue>& value2, Units unitless)
{
// Parse the first value. We're just making sure that it is one of the valid keywords or a percentage/length.
unsigned cumulativeFlags = 0;
FillPositionFlag value1Flag = InvalidFillPosition;
FillPositionFlag value2Flag = InvalidFillPosition;
value1 = parseFillPositionComponent(valueList, cumulativeFlags, value1Flag, ResolveValuesAsPercent, unitless);
if (!value1)
return;
// It only takes one value for background-position to be correctly parsed if it was specified in a shorthand (since we
// can assume that any other values belong to the rest of the shorthand). If we're not parsing a shorthand, though, the
// value was explicitly specified for our property.
CSSParserValue* value = valueList->next();
// First check for the comma. If so, we are finished parsing this value or value pair.
if (value && isComma(value))
value = 0;
if (value) {
value2 = parseFillPositionComponent(valueList, cumulativeFlags, value2Flag, ResolveValuesAsPercent, unitless);
if (value2)
valueList->next();
else {
if (!inShorthand()) {
value1.clear();
return;
}
}
}
if (!value2) {
// Only one value was specified. If that value was not a keyword, then it sets the x position, and the y position
// is simply 50%. This is our default.
// For keywords, the keyword was either an x-keyword (left/right), a y-keyword (top/bottom), or an ambiguous keyword (center).
// For left/right/center, the default of 50% in the y is still correct.
value2 = cssValuePool().createValue(50, CSSPrimitiveValue::UnitType::Percentage);
}
if (value1Flag == YFillPosition || value2Flag == XFillPosition)
value1.swap(value2);
}
void CSSPropertyParser::parseFillRepeat(RefPtrWillBeRawPtr<CSSValue>& value1, RefPtrWillBeRawPtr<CSSValue>& value2)
{
CSSValueID id = m_valueList->current()->id;
if (id == CSSValueRepeatX) {
m_implicitShorthand = true;
value1 = cssValuePool().createIdentifierValue(CSSValueRepeat);
value2 = cssValuePool().createIdentifierValue(CSSValueNoRepeat);
m_valueList->next();
return;
}
if (id == CSSValueRepeatY) {
m_implicitShorthand = true;
value1 = cssValuePool().createIdentifierValue(CSSValueNoRepeat);
value2 = cssValuePool().createIdentifierValue(CSSValueRepeat);
m_valueList->next();
return;
}
if (id == CSSValueRepeat || id == CSSValueNoRepeat || id == CSSValueRound || id == CSSValueSpace)
value1 = cssValuePool().createIdentifierValue(id);
else {
value1 = nullptr;
return;
}
CSSParserValue* value = m_valueList->next();
// Parse the second value if one is available
if (value && !isComma(value)) {
id = value->id;
if (id == CSSValueRepeat || id == CSSValueNoRepeat || id == CSSValueRound || id == CSSValueSpace) {
value2 = cssValuePool().createIdentifierValue(id);
m_valueList->next();
return;
}
}
// If only one value was specified, value2 is the same as value1.
m_implicitShorthand = true;
value2 = cssValuePool().createIdentifierValue(toCSSPrimitiveValue(value1.get())->getValueID());
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseFillSize(CSSPropertyID unresolvedProperty)
{
CSSParserValue* value = m_valueList->current();
m_valueList->next();
if (value->id == CSSValueContain || value->id == CSSValueCover)
return cssValuePool().createIdentifierValue(value->id);
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue1 = nullptr;
if (value->id == CSSValueAuto)
parsedValue1 = cssValuePool().createIdentifierValue(CSSValueAuto);
else {
if (!validUnit(value, FLength | FPercent))
return nullptr;
parsedValue1 = createPrimitiveNumericValue(value);
}
RefPtrWillBeRawPtr<CSSPrimitiveValue> parsedValue2 = nullptr;
value = m_valueList->current();
if (value) {
if (value->id == CSSValueAuto) {
// `auto' is the default
m_valueList->next();
} else if (validUnit(value, FLength | FPercent)) {
parsedValue2 = createPrimitiveNumericValue(value);
m_valueList->next();
}
} else if (unresolvedProperty == CSSPropertyAliasWebkitBackgroundSize) {
// For backwards compatibility we set the second value to the first if it is omitted.
// We only need to do this for -webkit-background-size. It should be safe to let masks match
// the real property.
parsedValue2 = parsedValue1;
}
if (!parsedValue2)
return parsedValue1;
return CSSValuePair::create(parsedValue1.release(), parsedValue2.release(), CSSValuePair::KeepIdenticalValues);
}
bool CSSPropertyParser::parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2,
RefPtrWillBeRawPtr<CSSValue>& retValue1, RefPtrWillBeRawPtr<CSSValue>& retValue2)
{
// We initially store the first value in value/value2, and only create
// CSSValueLists if we have more values.
RefPtrWillBeRawPtr<CSSValueList> values = nullptr;
RefPtrWillBeRawPtr<CSSValueList> values2 = nullptr;
RefPtrWillBeRawPtr<CSSValue> value = nullptr;
RefPtrWillBeRawPtr<CSSValue> value2 = nullptr;
retValue1 = retValue2 = nullptr;
propId1 = resolveCSSPropertyID(propId);
propId2 = propId1;
if (propId == CSSPropertyBackgroundPosition) {
propId1 = CSSPropertyBackgroundPositionX;
propId2 = CSSPropertyBackgroundPositionY;
} else if (propId == CSSPropertyWebkitMaskPosition) {
propId1 = CSSPropertyWebkitMaskPositionX;
propId2 = CSSPropertyWebkitMaskPositionY;
} else if (propId == CSSPropertyBackgroundRepeat) {
propId1 = CSSPropertyBackgroundRepeatX;
propId2 = CSSPropertyBackgroundRepeatY;
} else if (propId == CSSPropertyWebkitMaskRepeat) {
propId1 = CSSPropertyWebkitMaskRepeatX;
propId2 = CSSPropertyWebkitMaskRepeatY;
}
while (true) {
RefPtrWillBeRawPtr<CSSValue> currValue = nullptr;
RefPtrWillBeRawPtr<CSSValue> currValue2 = nullptr;
Units unitless = FUnknown;
CSSParserValue* val = m_valueList->current();
ASSERT(val);
switch (propId) {
case CSSPropertyBackgroundColor:
currValue = parseColor(val);
if (currValue)
m_valueList->next();
break;
case CSSPropertyBackgroundAttachment:
if (val->id == CSSValueScroll || val->id == CSSValueFixed || val->id == CSSValueLocal) {
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
}
break;
case CSSPropertyBackgroundImage:
case CSSPropertyWebkitMaskImage:
if (parseFillImage(m_valueList, currValue))
m_valueList->next();
break;
case CSSPropertyWebkitBackgroundClip:
case CSSPropertyWebkitBackgroundOrigin:
case CSSPropertyWebkitMaskClip:
case CSSPropertyWebkitMaskOrigin:
// The first three values here are deprecated and do not apply to the version of the property that has
// the -webkit- prefix removed.
if (val->id == CSSValueBorder || val->id == CSSValuePadding || val->id == CSSValueContent
|| val->id == CSSValueBorderBox || val->id == CSSValuePaddingBox || val->id == CSSValueContentBox
|| ((propId == CSSPropertyWebkitBackgroundClip || propId == CSSPropertyWebkitMaskClip)
&& (val->id == CSSValueText || val->id == CSSValueWebkitText))) {
if (val->id == CSSValueWebkitText && m_context.useCounter())
m_context.useCounter()->count(UseCounter::WebkitTextInClipProperty);
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
}
break;
case CSSPropertyBackgroundClip:
if (parseBackgroundClip(val, currValue)) {
if (val->id == CSSValueWebkitText && m_context.useCounter())
m_context.useCounter()->count(UseCounter::WebkitTextInClipProperty);
m_valueList->next();
}
break;
case CSSPropertyBackgroundOrigin:
if (val->id == CSSValueBorderBox || val->id == CSSValuePaddingBox || val->id == CSSValueContentBox) {
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
}
break;
case CSSPropertyBackgroundPosition:
if (!inShorthand())
unitless = FUnitlessQuirk;
// fall-through
case CSSPropertyWebkitMaskPosition:
parseFillPosition(m_valueList, currValue, currValue2, unitless);
// parseFillPosition advances the m_valueList pointer.
break;
case CSSPropertyBackgroundPositionX:
case CSSPropertyWebkitMaskPositionX: {
currValue = parseFillPositionX(m_valueList);
if (currValue)
m_valueList->next();
break;
}
case CSSPropertyBackgroundPositionY:
case CSSPropertyWebkitMaskPositionY: {
currValue = parseFillPositionY(m_valueList);
if (currValue)
m_valueList->next();
break;
}
case CSSPropertyWebkitBackgroundComposite:
case CSSPropertyWebkitMaskComposite:
if (val->id >= CSSValueClear && val->id <= CSSValuePlusLighter) {
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
}
break;
case CSSPropertyBackgroundBlendMode:
if (val->id == CSSValueNormal || val->id == CSSValueMultiply
|| val->id == CSSValueScreen || val->id == CSSValueOverlay || val->id == CSSValueDarken
|| val->id == CSSValueLighten || val->id == CSSValueColorDodge || val->id == CSSValueColorBurn
|| val->id == CSSValueHardLight || val->id == CSSValueSoftLight || val->id == CSSValueDifference
|| val->id == CSSValueExclusion || val->id == CSSValueHue || val->id == CSSValueSaturation
|| val->id == CSSValueColor || val->id == CSSValueLuminosity) {
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
}
break;
case CSSPropertyBackgroundRepeat:
case CSSPropertyWebkitMaskRepeat:
parseFillRepeat(currValue, currValue2);
// parseFillRepeat advances the m_valueList pointer
break;
case CSSPropertyBackgroundSize:
case CSSPropertyAliasWebkitBackgroundSize:
case CSSPropertyWebkitMaskSize: {
currValue = parseFillSize(propId);
break;
}
case CSSPropertyMaskSourceType: {
ASSERT(RuntimeEnabledFeatures::cssMaskSourceTypeEnabled());
if (val->id == CSSValueAuto || val->id == CSSValueAlpha || val->id == CSSValueLuminance) {
currValue = cssValuePool().createIdentifierValue(val->id);
m_valueList->next();
} else {
currValue = nullptr;
}
break;
}
default:
break;
}
if (!currValue)
return false;
if (value && !values) {
values = CSSValueList::createCommaSeparated();
values->append(value.release());
}
if (value2 && !values2) {
values2 = CSSValueList::createCommaSeparated();
values2->append(value2.release());
}
if (values)
values->append(currValue.release());
else
value = currValue.release();
if (currValue2) {
if (values2)
values2->append(currValue2.release());
else
value2 = currValue2.release();
}
// When parsing any fill shorthand property, we let it handle building up the lists for all
// properties.
if (inShorthand())
break;
if (!m_valueList->current())
break;
if (!consumeComma(m_valueList) || !m_valueList->current())
return false;
}
if (values) {
ASSERT(values->length());
retValue1 = values.release();
if (values2) {
ASSERT(values2->length());
retValue2 = values2.release();
}
} else {
ASSERT(value);
retValue1 = value.release();
retValue2 = value2.release();
}
return true;
}
static inline bool isCSSWideKeyword(const CSSParserValue& value)
{
return value.id == CSSValueInitial || value.id == CSSValueInherit || value.id == CSSValueUnset || value.id == CSSValueDefault;
}
static inline bool isValidCustomIdentForGridPositions(const CSSParserValue& value)
{
// FIXME: we need a more general solution for <custom-ident> in all properties.
return value.m_unit == CSSParserValue::Identifier && value.id != CSSValueSpan && value.id != CSSValueAuto && !isCSSWideKeyword(value);
}
// The function parses [ <integer> || <custom-ident> ] in <grid-line> (which can be stand alone or with 'span').
bool CSSPropertyParser::parseIntegerOrCustomIdentFromGridPosition(RefPtrWillBeRawPtr<CSSPrimitiveValue>& numericValue, RefPtrWillBeRawPtr<CSSCustomIdentValue>& gridLineName)
{
CSSParserValue* value = m_valueList->current();
if (validUnit(value, FInteger) && value->fValue) {
numericValue = createPrimitiveNumericValue(value);
value = m_valueList->next();
if (value && isValidCustomIdentForGridPositions(*value)) {
gridLineName = createPrimitiveCustomIdentValue(m_valueList->current());
m_valueList->next();
}
return true;
}
if (isValidCustomIdentForGridPositions(*value)) {
gridLineName = createPrimitiveCustomIdentValue(m_valueList->current());
value = m_valueList->next();
if (value && validUnit(value, FInteger) && value->fValue) {
numericValue = createPrimitiveNumericValue(value);
m_valueList->next();
}
return true;
}
return false;
}
PassRefPtrWillBeRawPtr<CSSValue> CSSPropertyParser::parseGridPosition()
{
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
CSSParserValue* value = m_valueList->current();
if (value->id == CSSValueAuto) {
m_valueList->next();
return cssValuePool().createIdentifierValue(CSSValueAuto);
}
RefPtrWillBeRawPtr<CSSPrimitiveValue> numericValue = nullptr;
RefPtrWillBeRawPtr<CSSCustomIdentValue> gridLineName = nullptr;
bool hasSeenSpanKeyword = false;
if (parseIntegerOrCustomIdentFromGridPosition(numericValue, gridLineName)) {
value = m_valueList->current();
if (value && value->id == CSSValueSpan) {
hasSeenSpanKeyword = true;
m_valueList->next();
}
} else if (value->id == CSSValueSpan) {
hasSeenSpanKeyword = true;
if (CSSParserValue* nextValue = m_valueList->next()) {
if (!isForwardSlashOperator(nextValue) && !parseIntegerOrCustomIdentFromGridPosition(numericValue, gridLineName))
return nullptr;
}
}
// Check that we have consumed all the value list. For shorthands, the parser will pass
// the whole value list (including the opposite position).
if (m_valueList->current() && !isForwardSlashOperator(m_valueList->current()))
return nullptr;
// If we didn't parse anything, this is not a valid grid position.
if (!hasSeenSpanKeyword && !gridLineName && !numericValue)
return nullptr;
// Negative numbers are not allowed for span (but are for <integer>).
if (hasSeenSpanKeyword && numericValue && numericValue->getIntValue() < 0)
return nullptr;
// For the <custom-ident> case.
if (gridLineName && !numericValue && !hasSeenSpanKeyword)
return CSSCustomIdentValue::create(gridLineName->value());
RefPtrWillBeRawPtr<CSSValueList> values = CSSValueList::createSpaceSeparated();
if (hasSeenSpanKeyword)
values->append(cssValuePool().createIdentifierValue(CSSValueSpan));
if (numericValue)
values->append(numericValue.release());
if (gridLineName)
values->append(gridLineName.release());
ASSERT(values->length());
return values.release();
}
static PassRefPtrWillBeRawPtr<CSSValue> gridMissingGridPositionValue(CSSValue* value)
{
if (value->isCustomIdentValue())
return value;
return cssValuePool().createIdentifierValue(CSSValueAuto);
}
bool CSSPropertyParser::parseGridItemPositionShorthand(CSSPropertyID shorthandId, bool important)
{
ShorthandScope scope(this, shorthandId);
const StylePropertyShorthand& shorthand = shorthandForProperty(shorthandId);
ASSERT(shorthand.length() == 2);
RefPtrWillBeRawPtr<CSSValue> startValue = parseGridPosition();
if (!startValue)
return false;
RefPtrWillBeRawPtr<CSSValue> endValue = nullptr;
if (m_valueList->current()) {
if (!isForwardSlashOperator(m_valueList->current()))
return false;
if (!m_valueList->next())
return false;
endValue = parseGridPosition();
if (!endValue || m_valueList->current())
return false;
} else {
endValue = gridMissingGridPositionValue(startValue.get());
}
addProperty(shorthand.properties()[0], startValue, important);
addProperty(shorthand.properties()[1], endValue, important);
return true;
}
bool CSSPropertyParser::parseGridGapShorthand(bool important)
{
ShorthandScope scope(this, CSSPropertyGridGap);
ASSERT(shorthandForProperty(CSSPropertyGridGap).length() == 2);
CSSParserValue* value = m_valueList->current();
if (!value)
return false;
if (!validUnit(value, FLength | FNonNeg))
return false;
RefPtrWillBeRawPtr<CSSPrimitiveValue> columnGap = createPrimitiveNumericValue(value);
RefPtrWillBeRawPtr<CSSPrimitiveValue> rowGap = nullptr;
value = m_valueList->next();
if (value) {
if (!validUnit(value, FLength | FNonNeg))
return false;
rowGap = createPrimitiveNumericValue(value);
if (m_valueList->next())
return false;
} else {
rowGap = columnGap;
}
addProperty(CSSPropertyGridColumnGap, columnGap, important);
addProperty(CSSPropertyGridRowGap, rowGap, important);
return true;
}
bool CSSPropertyParser::parseGridTemplateRowsAndAreas(PassRefPtrWillBeRawPtr<CSSValue> templateColumns, bool important)
{
NamedGridAreaMap gridAreaMap;
size_t rowCount = 0;
size_t columnCount = 0;
bool trailingIdentWasAdded = false;
RefPtrWillBeRawPtr<CSSValueList> templateRows = CSSValueList::createSpaceSeparated();
// At least template-areas strings must be defined.
if (!m_valueList->current())
return false;
while (m_valueList->current()) {
// Handle leading <custom-ident>*.
if (!parseGridLineNames(*m_valueList, *templateRows, trailingIdentWasAdded ? toCSSGridLineNamesValue(templateRows->item(templateRows->length() - 1)) : nullptr))
return false;
// Handle a template-area's row.
if (!parseGridTemplateAreasRow(gridAreaMap, rowCount, columnCount))
return false;
++rowCount;
// Handle template-rows's track-size.
if (m_valueList->current() && m_valueList->current()->m_unit != CSSParserValue::String) {
RefPtrWillBeRawPtr<CSSValue> value = parseGridTrackSize(*m_valueList);
if (!value)
return false;
templateRows->append(value);
} else {
templateRows->append(cssValuePool().createIdentifierValue(CSSValueAuto));
}
// This will handle the trailing/leading <custom-ident>* in the grammar.
if (!parseGridLineNames(*m_valueList, *templateRows))
return false;
trailingIdentWasAdded = templateRows->item(templateRows->length() - 1)->isGridLineNamesValue();
}
// [<track-list> /]?
if (templateColumns)
addProperty(CSSPropertyGridTemplateColumns, templateColumns, important);
else
addProperty(CSSPropertyGridTemplateColumns, cssValuePool().createIdentifierValue(CSSValueNone), important);
// [<line-names>? <string> [<track-size> <line-names>]? ]+
RefPtrWillBeRawPtr<CSSValue> templateAreas = CSSGridTemplateAreasValue::create(gridAreaMap, rowCount, columnCount);
addProperty(CSSPropertyGridTemplateAreas, templateAreas.release(), important);
addProperty(CSSPropertyGridTemplateRows, templateRows.release(), important);
return true;
}
bool CSSPropertyParser::parseGridTemplateShorthand(bool important)
{
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
ShorthandScope scope(this, CSSPropertyGridTemplate);
ASSERT(gridTemplateShorthand().length() == 3);
// At least "none" must be defined.
if (!m_valueList->current())
return false;
bool firstValueIsNone = m_valueList->current()->id == CSSValueNone;
// 1- 'none' case.
if (firstValueIsNone && !m_valueList->next()) {
addProperty(CSSPropertyGridTemplateColumns, cssValuePool().createIdentifierValue(CSSValueNone), important);
addProperty(CSSPropertyGridTemplateRows, cssValuePool().createIdentifierValue(CSSValueNone), important);
addProperty(CSSPropertyGridTemplateAreas, cssValuePool().createIdentifierValue(CSSValueNone), important);
return true;
}
unsigned index = 0;
RefPtrWillBeRawPtr<CSSValue> columnsValue = nullptr;
if (firstValueIsNone) {
columnsValue = cssValuePool().createIdentifierValue(CSSValueNone);
} else {
columnsValue = parseGridTrackList();
}
// 2- <grid-template-columns> / <grid-template-columns> syntax.
if (columnsValue) {
if (!(m_valueList->current() && isForwardSlashOperator(m_valueList->current()) && m_valueList->next()))
return false;
index = m_valueList->currentIndex();
if (RefPtrWillBeRawPtr<CSSValue> rowsValue = parseGridTrackList()) {
if (m_valueList->current())
return false;
addProperty(CSSPropertyGridTemplateColumns, columnsValue, important);
addProperty(CSSPropertyGridTemplateRows, rowsValue, important);
addProperty(CSSPropertyGridTemplateAreas, cssValuePool().createIdentifierValue(CSSValueNone), important);
return true;
}
}
// 3- [<track-list> /]? [<line-names>? <string> [<track-size> <line-names>]? ]+ syntax.
// The template-columns <track-list> can't be 'none'.
if (firstValueIsNone)
return false;
// It requires to rewind parsing due to previous syntax failures.
m_valueList->setCurrentIndex(index);
return parseGridTemplateRowsAndAreas(columnsValue, important);
}
bool CSSPropertyParser::parseGridShorthand(bool important)
{
ShorthandScope scope(this, CSSPropertyGrid);
ASSERT(shorthandForProperty(CSSPropertyGrid).length() == 6);
// 1- <grid-template>
if (parseGridTemplateShorthand(important)) {
// It can only be specified the explicit or the implicit grid properties in a single grid declaration.
// The sub-properties not specified are set to their initial value, as normal for shorthands.
addProperty(CSSPropertyGridAutoFlow, cssValuePool().createImplicitInitialValue(), important);
addProperty(CSSPropertyGridAutoColumns, cssValuePool().createImplicitInitialValue(), important);
addProperty(CSSPropertyGridAutoRows, cssValuePool().createImplicitInitialValue(), important);
return true;
}
// Need to rewind parsing to explore the alternative syntax of this shorthand.
m_valueList->setCurrentIndex(0);
// 2- <grid-auto-flow> [ <grid-auto-columns> [ / <grid-auto-rows> ]? ]
if (!parseValue(CSSPropertyGridAutoFlow, important))
return false;
RefPtrWillBeRawPtr<CSSValue> autoColumnsValue = nullptr;
RefPtrWillBeRawPtr<CSSValue> autoRowsValue = nullptr;
if (m_valueList->current()) {
autoColumnsValue = parseGridTrackSize(*m_valueList);
if (!autoColumnsValue)
return false;
if (m_valueList->current()) {
if (!isForwardSlashOperator(m_valueList->current()) || !m_valueList->next())
return false;
autoRowsValue = parseGridTrackSize(*m_valueList);
if (!autoRowsValue)
return false;
}
if (m_valueList->current())
return false;
} else {
// Other omitted values are set to their initial values.
autoColumnsValue = cssValuePool().createImplicitInitialValue();
autoRowsValue = cssValuePool().createImplicitInitialValue();
}
// if <grid-auto-rows> value is omitted, it is set to the value specified for grid-auto-columns.
if (!autoRowsValue)
autoRowsValue = autoColumnsValue;
addProperty(CSSPropertyGridAutoColumns, autoColumnsValue, important);
addProperty(CSSPropertyGridAutoRows, autoRowsValue, important);
// It can only be specified the explicit or the implicit grid properties in a single grid declaration.
// The sub-properties not specified are set to their initial value, as normal for shorthands.
addProperty(CSSPropertyGridTemplateColumns, cssValuePool().createImplicitInitialValue(), important);
addProperty(CSSPropertyGridTemplateRows, cssValuePool().createImplicitInitialValue(), important);
addProperty(CSSPropertyGridTemplateAreas, cssValuePool().createImplicitInitialValue(), important);
return true;
}
bool CSSPropertyParser::parseGridAreaShorthand(bool important)
{
ASSERT(RuntimeEnabledFeatures::cssGridLayoutEnabled());
ShorthandScope scope(this, CSSPropertyGridArea);
const StylePropertyShorthand& shorthand = gridAreaShorthand();
ASSERT_UNUSED(shorthand, shorthand.length() == 4);
RefPtrWillBeRawPtr<CSSValue> rowStartValue = parseGridPosition();
if (!rowStartValue)
return false;
RefPtrWillBeRawPtr<CSSValue> columnStartValue = nullptr;
if (!parseSingleGridAreaLonghand(columnStartValue))
return false;
RefPtrWillBeRawPtr<CSSValue> rowEndValue = nullptr;
if (!parseSingleGridAreaLonghand(rowEndValue))
return false;
RefPtrWillBeRawPtr<CSSValue> columnEndValue = nullptr;
if (!parseSingleGridAreaLonghand(columnEndValue))
return false;
if (!columnStartValue)
columnStartValue = gridMissingGridPositionValue(rowStartValue.get());
if (!rowEndValue)
rowEndValue = gridMissingGridPositionValue(rowStartValue.get());
if (!columnEndValue)
columnEndValue = gridMissingGridPositionValue(columnStartValue.get());
addProperty(CSSPropertyGridRowStart, rowStartValue, important);
addProperty(CSSPropertyGridColumnStart, columnStartValue, important);
addProperty(CSSPropertyGridRowEnd, rowEndValue, important);
addProperty(CSSPropertyGridColumnEnd, columnEndValue, important);
return true;
}
bool CSSPropertyParser::parseSingleGridAreaLonghand(RefPtrWillBeRawPtr<CSSValue>& property)
{
if (!m_valueList->current())
return true;
if (!isForwardSlashOperator(m_valueList->current()))
return false;
if (!m_valueList->next())
return false;