2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 - 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #ifndef CSSPropertyParser_h
24 #define CSSPropertyParser_h
26 // FIXME: Way too many.
27 #include "core/CSSPropertyNames.h"
28 #include "core/CSSValueKeywords.h"
29 #include "core/css/CSSCalculationValue.h"
30 #include "core/css/CSSFilterValue.h"
31 #include "core/css/CSSGradientValue.h"
32 #include "core/css/CSSGridTemplateAreasValue.h"
33 #include "core/css/CSSParserMode.h"
34 #include "core/css/CSSParserValues.h"
35 #include "core/css/CSSProperty.h"
36 #include "core/css/CSSPropertySourceData.h"
37 #include "core/css/CSSSelector.h"
38 #include "platform/graphics/Color.h"
39 #include "wtf/OwnPtr.h"
40 #include "wtf/Vector.h"
44 // FIXME: Many of these may not be used.
45 class CSSArrayFunctionValue;
46 class CSSBorderImageSliceValue;
47 class CSSPrimitiveValue;
48 class CSSSelectorList;
52 class CSSBasicShapeInset;
53 class CSSGridLineNamesValue;
56 class ImmutableStylePropertySet;
58 class StylePropertyShorthand;
62 // Inputs: PropertyID, isImportant bool, CSSParserValueList.
63 // Outputs: Vector of CSSProperties
65 class CSSPropertyParser {
68 CSSPropertyParser(OwnPtr<CSSParserValueList>&,
69 const CSSParserContext&, bool inViewport, bool savedImportant,
70 WillBeHeapVector<CSSProperty, 256>&, CSSRuleSourceData::Type);
73 // FIXME: Should this be on a separate ColorParser object?
74 template<typename StringType>
75 static bool fastParseColor(RGBA32&, const StringType&, bool strict);
77 bool parseValue(CSSPropertyID, bool important);
79 static bool isSystemColor(int id);
82 bool inShorthand() const { return m_inParseShorthand; }
83 bool inQuirksMode() const { return isQuirksModeBehavior(m_context.mode()); }
85 bool inViewport() const { return m_inViewport; }
86 bool parseViewportProperty(CSSPropertyID propId, bool important);
87 bool parseViewportShorthand(CSSPropertyID propId, CSSPropertyID first, CSSPropertyID second, bool important);
89 KURL completeURL(const String& url) const;
91 void addPropertyWithPrefixingVariant(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important, bool implicit = false);
92 void addProperty(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important, bool implicit = false);
93 void rollbackLastProperties(int num);
94 bool hasProperties() const { return !m_parsedProperties.isEmpty(); }
95 void addExpandedPropertyForValue(CSSPropertyID propId, PassRefPtrWillBeRawPtr<CSSValue>, bool);
97 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseValidPrimitive(CSSValueID ident, CSSParserValue*);
99 bool parseShorthand(CSSPropertyID, const StylePropertyShorthand&, bool important);
100 bool parse4Values(CSSPropertyID, const CSSPropertyID* properties, bool important);
101 bool parseContent(CSSPropertyID, bool important);
102 bool parseQuotes(CSSPropertyID, bool important);
104 PassRefPtrWillBeRawPtr<CSSValue> parseAttr(CSSParserValueList* args);
106 PassRefPtrWillBeRawPtr<CSSValue> parseBackgroundColor();
108 bool parseFillImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
110 enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
111 enum FillPositionParsingMode { ResolveValuesAsPercent = 0, ResolveValuesAsKeyword = 1 };
112 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag, FillPositionParsingMode = ResolveValuesAsPercent);
113 PassRefPtrWillBeRawPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
114 PassRefPtrWillBeRawPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
115 void parse2ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
116 bool isPotentialPositionValue(CSSParserValue*);
117 void parseFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
118 void parse3ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>);
119 void parse4ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>);
121 void parseFillRepeat(RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
122 PassRefPtrWillBeRawPtr<CSSValue> parseFillSize(CSSPropertyID, bool &allowComma);
124 bool parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
125 bool parseFillShorthand(CSSPropertyID, const CSSPropertyID* properties, int numProperties, bool important);
127 void addFillValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval);
129 void addAnimationValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval);
131 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDelay();
132 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDirection();
133 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDuration();
134 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationFillMode();
135 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationIterationCount();
136 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationName();
137 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationPlayState();
138 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationProperty();
139 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationTimingFunction();
141 bool parseWebkitTransformOriginShorthand(RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
142 bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
143 PassRefPtrWillBeRawPtr<CSSValue> parseAnimationProperty(CSSPropertyID);
144 PassRefPtrWillBeRawPtr<CSSValueList> parseAnimationPropertyList(CSSPropertyID);
145 bool parseTransitionShorthand(CSSPropertyID, bool important);
146 bool parseAnimationShorthand(CSSPropertyID, bool important);
148 PassRefPtrWillBeRawPtr<CSSValue> parseColumnWidth();
149 PassRefPtrWillBeRawPtr<CSSValue> parseColumnCount();
150 bool parseColumnsShorthand(bool important);
152 PassRefPtrWillBeRawPtr<CSSValue> parseGridPosition();
153 bool parseIntegerOrCustomIdentFromGridPosition(RefPtrWillBeRawPtr<CSSPrimitiveValue>& numericValue, RefPtrWillBeRawPtr<CSSPrimitiveValue>& gridLineName);
154 bool parseGridItemPositionShorthand(CSSPropertyID, bool important);
155 bool parseGridTemplateRowsAndAreas(PassRefPtrWillBeRawPtr<CSSValue>, bool important);
156 bool parseGridTemplateShorthand(bool important);
157 bool parseGridShorthand(bool important);
158 bool parseGridAreaShorthand(bool important);
159 bool parseSingleGridAreaLonghand(RefPtrWillBeRawPtr<CSSValue>&);
160 PassRefPtrWillBeRawPtr<CSSValue> parseGridTrackList(bool important);
161 bool parseGridTrackRepeatFunction(CSSValueList&);
162 PassRefPtrWillBeRawPtr<CSSValue> parseGridTrackSize(CSSParserValueList& inputList);
163 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseGridBreadth(CSSParserValue*);
164 bool parseGridTemplateAreasRow(NamedGridAreaMap&, const size_t, size_t&);
165 PassRefPtrWillBeRawPtr<CSSValue> parseGridTemplateAreas();
166 void parseGridLineNames(CSSParserValueList&, CSSValueList&, CSSGridLineNamesValue* = 0);
168 bool parseClipShape(CSSPropertyID, bool important);
170 bool parseItemPositionOverflowPosition(CSSPropertyID, bool important);
172 PassRefPtrWillBeRawPtr<CSSValue> parseShapeProperty(CSSPropertyID propId);
173 PassRefPtrWillBeRawPtr<CSSValue> parseBasicShapeAndOrBox();
174 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseBasicShape();
175 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseShapeRadius(CSSParserValue*);
177 PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeCircle(CSSParserValueList* args);
178 PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeEllipse(CSSParserValueList* args);
179 PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapePolygon(CSSParserValueList* args);
180 PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeInset(CSSParserValueList* args);
182 bool parseFont(bool important);
183 PassRefPtrWillBeRawPtr<CSSValueList> parseFontFamily();
185 bool parseCounter(CSSPropertyID, int defaultValue, bool important);
186 PassRefPtrWillBeRawPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
188 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
189 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
190 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
191 bool parseColorFromValue(CSSParserValue*, RGBA32&);
193 bool parseLineHeight(bool important);
194 bool parseFontSize(bool important);
195 bool parseFontVariant(bool important);
196 bool parseFontWeight(bool important);
197 bool parseFontFaceSrc();
198 bool parseFontFaceUnicodeRange();
200 bool parseSVGValue(CSSPropertyID propId, bool important);
201 PassRefPtrWillBeRawPtr<CSSValue> parseSVGPaint();
202 PassRefPtrWillBeRawPtr<CSSValue> parseSVGStrokeDasharray();
204 PassRefPtrWillBeRawPtr<CSSValue> parsePaintOrder() const;
206 // CSS3 Parsing Routines (for properties specific to CSS3)
207 PassRefPtrWillBeRawPtr<CSSValueList> parseShadow(CSSParserValueList*, CSSPropertyID);
208 bool parseBorderImageShorthand(CSSPropertyID, bool important);
209 PassRefPtrWillBeRawPtr<CSSValue> parseBorderImage(CSSPropertyID);
210 bool parseBorderImageRepeat(RefPtrWillBeRawPtr<CSSValue>&);
211 bool parseBorderImageSlice(CSSPropertyID, RefPtrWillBeRawPtr<CSSBorderImageSliceValue>&);
212 bool parseBorderImageWidth(RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
213 bool parseBorderImageOutset(RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
214 bool parseBorderRadius(CSSPropertyID, bool important);
216 bool parseAspectRatio(bool important);
218 bool parseReflect(CSSPropertyID, bool important);
220 bool parseFlex(CSSParserValueList* args, bool important);
222 bool parseObjectPosition(bool important);
225 bool parseCanvas(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
227 bool parseDeprecatedGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
228 bool parseDeprecatedLinearGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
229 bool parseDeprecatedRadialGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
230 bool parseLinearGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
231 bool parseRadialGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
232 bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
234 bool parseCrossfade(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
236 PassRefPtrWillBeRawPtr<CSSValue> parseImageSet(CSSParserValueList*);
238 bool parseWillChange(bool important);
240 PassRefPtrWillBeRawPtr<CSSValueList> parseFilter();
241 PassRefPtrWillBeRawPtr<CSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, CSSFilterValue::FilterOperationType);
243 PassRefPtrWillBeRawPtr<CSSValueList> parseTransformOrigin();
244 PassRefPtrWillBeRawPtr<CSSValueList> parseTransform(CSSPropertyID);
245 PassRefPtrWillBeRawPtr<CSSValue> parseTransformValue(CSSPropertyID, CSSParserValue*);
246 bool parseWebkitTransformOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, CSSPropertyID& propId3, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
247 bool parseWebkitPerspectiveOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
249 bool parseTextEmphasisStyle(bool important);
251 bool parseTouchAction(bool important);
253 void addTextDecorationProperty(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important);
254 bool parseTextDecoration(CSSPropertyID propId, bool important);
255 bool parseTextUnderlinePosition(bool important);
257 PassRefPtrWillBeRawPtr<CSSValue> parseTextIndent();
259 bool parseLineBoxContain(bool important);
260 bool parseCalculation(CSSParserValue*, ValueRange);
262 bool parseFontFeatureTag(CSSValueList*);
263 bool parseFontFeatureSettings(bool important);
265 bool parseFontVariantLigatures(bool important);
267 bool parseGeneratedImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
269 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
270 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
272 PassRefPtrWillBeRawPtr<CSSValue> createCSSImageValueWithReferrer(const String& rawValue, const KURL&);
274 bool validWidthOrHeight(CSSParserValue*);
276 PassRefPtrWillBeRawPtr<CSSBasicShape> parseInsetRoundedCorners(PassRefPtrWillBeRawPtr<CSSBasicShapeInset>, CSSParserValueList*);
278 enum SizeParameterType {
286 bool parsePage(CSSPropertyID propId, bool important);
287 bool parseSize(CSSPropertyID propId, bool important);
288 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue*, SizeParameterType prevParamType);
290 bool parseFontFaceSrcURI(CSSValueList*);
291 bool parseFontFaceSrcLocal(CSSValueList*);
298 class ImplicitScope {
300 WTF_MAKE_NONCOPYABLE(ImplicitScope);
302 ImplicitScope(CSSPropertyParser* parser, PropertyType propertyType)
305 m_parser->m_implicitShorthand = propertyType == CSSPropertyParser::PropertyImplicit;
310 m_parser->m_implicitShorthand = false;
314 CSSPropertyParser* m_parser;
317 class ShorthandScope {
320 ShorthandScope(CSSPropertyParser* parser, CSSPropertyID propId) : m_parser(parser)
322 if (!(m_parser->m_inParseShorthand++))
323 m_parser->m_currentShorthand = propId;
327 if (!(--m_parser->m_inParseShorthand))
328 m_parser->m_currentShorthand = CSSPropertyInvalid;
332 CSSPropertyParser* m_parser;
335 enum ReleaseParsedCalcValueCondition {
336 ReleaseParsedCalcValue,
337 DoNotReleaseParsedCalcValue
343 FNumber = 0x0002, // Real Numbers
349 FPositiveInteger = 0x0080,
351 FResolution = 0x0200,
355 friend inline Units operator|(Units a, Units b)
357 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
360 bool validCalculationUnit(CSSParserValue*, Units, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
362 bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
364 inline bool validUnit(CSSParserValue* value, Units unitflags, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue) { return validUnit(value, unitflags, m_context.mode(), releaseCalc); }
365 bool validUnit(CSSParserValue*, Units, CSSParserMode, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
367 bool parseBorderImageQuad(Units, RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
368 int colorIntFromValue(CSSParserValue*);
369 double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
370 bool isCalculation(CSSParserValue*);
374 // FIXME: This should not be an OwnPtr&, many callers will need to be changed.
375 const OwnPtr<CSSParserValueList>& m_valueList;
376 const CSSParserContext& m_context;
377 const bool m_inViewport;
378 const bool m_important; // FIXME: This is only used by font-face-src and unicode-range and undoubtably wrong!
381 WillBeHeapVector<CSSProperty, 256>& m_parsedProperties;
382 CSSRuleSourceData::Type m_ruleType;
384 // Locals during parsing:
385 int m_inParseShorthand;
386 CSSPropertyID m_currentShorthand;
387 bool m_implicitShorthand;
388 RefPtrWillBeMember<CSSCalcValue> m_parsedCalculation;
390 // FIXME: There is probably a small set of APIs we could expose for these
391 // classes w/o needing to make them friends.
392 friend class ShadowParseContext;
393 friend class BorderImageParseContext;
394 friend class BorderImageSliceParseContext;
395 friend class BorderImageQuadParseContext;
396 friend class TransformOperationInfo;
397 friend bool parseDeprecatedGradientColorStop(CSSPropertyParser*, CSSParserValue*, CSSGradientColorStop&);
398 friend PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseGradientColorOrKeyword(CSSPropertyParser*, CSSParserValue*);
401 CSSPropertyID cssPropertyID(const CSSParserString&);
402 CSSPropertyID cssPropertyID(const String&);
403 CSSValueID cssValueKeywordID(const CSSParserString&);
405 bool isKeywordPropertyID(CSSPropertyID);
406 bool isValidKeywordPropertyAndValue(CSSPropertyID, CSSValueID, const CSSParserContext&);
408 } // namespace WebCore
410 #endif // CSSPropertyParser_h