1/*
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.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23#ifndef CSSPropertyParser_h
24#define CSSPropertyParser_h
25
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/CSSProperty.h"
34#include "core/css/CSSPropertySourceData.h"
35#include "core/css/CSSSelector.h"
36#include "core/css/parser/CSSParserMode.h"
37#include "core/css/parser/CSSParserValues.h"
38#include "platform/graphics/Color.h"
39#include "wtf/OwnPtr.h"
40#include "wtf/Vector.h"
41
42namespace blink {
43
44class CSSBorderImageSliceValue;
45class CSSPrimitiveValue;
46class CSSValue;
47class CSSValueList;
48class CSSBasicShape;
49class CSSBasicShapeInset;
50class CSSGridLineNamesValue;
51class StylePropertyShorthand;
52
53// Inputs: PropertyID, isImportant bool, CSSParserValueList.
54// Outputs: Vector of CSSProperties
55
56class CSSPropertyParser {
57    STACK_ALLOCATED();
58public:
59    static bool parseValue(CSSPropertyID, bool important,
60        CSSParserValueList*, const CSSParserContext&, bool inViewport,
61        WillBeHeapVector<CSSProperty, 256>&, CSSRuleSourceData::Type);
62
63    // FIXME: Should this be on a separate ColorParser object?
64    template<typename StringType>
65    static bool fastParseColor(RGBA32&, const StringType&, bool strict);
66
67    static bool isSystemColor(int id);
68
69private:
70    CSSPropertyParser(CSSParserValueList*, const CSSParserContext&, bool inViewport,
71        WillBeHeapVector<CSSProperty, 256>&, CSSRuleSourceData::Type);
72
73    bool parseValue(CSSPropertyID, bool important);
74
75    bool inShorthand() const { return m_inParseShorthand; }
76    bool inQuirksMode() const { return isQuirksModeBehavior(m_context.mode()); }
77
78    bool inViewport() const { return m_inViewport; }
79    bool parseViewportProperty(CSSPropertyID propId, bool important);
80    bool parseViewportShorthand(CSSPropertyID propId, CSSPropertyID first, CSSPropertyID second, bool important);
81
82    KURL completeURL(const String& url) const;
83
84    void addPropertyWithPrefixingVariant(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important, bool implicit = false);
85    void addProperty(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important, bool implicit = false);
86    void rollbackLastProperties(int num);
87    void addExpandedPropertyForValue(CSSPropertyID propId, PassRefPtrWillBeRawPtr<CSSValue>, bool);
88
89    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseValidPrimitive(CSSValueID ident, CSSParserValue*);
90
91    bool parseShorthand(CSSPropertyID, const StylePropertyShorthand&, bool important);
92    bool parse4Values(CSSPropertyID, const CSSPropertyID* properties, bool important);
93    bool parseContent(CSSPropertyID, bool important);
94    PassRefPtrWillBeRawPtr<CSSValue> parseQuotes();
95
96    PassRefPtrWillBeRawPtr<CSSValue> parseAttr(CSSParserValueList* args);
97
98    PassRefPtrWillBeRawPtr<CSSValue> parseBackgroundColor();
99
100    bool parseFillImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
101
102    enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
103    enum FillPositionParsingMode { ResolveValuesAsPercent = 0, ResolveValuesAsKeyword = 1 };
104    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag, FillPositionParsingMode = ResolveValuesAsPercent);
105    PassRefPtrWillBeRawPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
106    PassRefPtrWillBeRawPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
107    void parse2ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
108    bool isPotentialPositionValue(CSSParserValue*);
109    void parseFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
110    void parse3ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>);
111    void parse4ValuesFillPosition(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, PassRefPtrWillBeRawPtr<CSSPrimitiveValue>);
112
113    void parseFillRepeat(RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
114    PassRefPtrWillBeRawPtr<CSSValue> parseFillSize(CSSPropertyID, bool &allowComma);
115
116    bool parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
117    bool parseFillShorthand(CSSPropertyID, const CSSPropertyID* properties, int numProperties, bool important);
118
119    void addFillValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval);
120
121    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDelay();
122    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDirection();
123    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationDuration();
124    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationFillMode();
125    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationIterationCount();
126    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationName();
127    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationPlayState();
128    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationProperty();
129    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationTimingFunction();
130
131    bool parseWebkitTransformOriginShorthand(bool important);
132    bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
133    PassRefPtrWillBeRawPtr<CSSValue> parseAnimationProperty(CSSPropertyID);
134    PassRefPtrWillBeRawPtr<CSSValueList> parseAnimationPropertyList(CSSPropertyID);
135    bool parseTransitionShorthand(CSSPropertyID, bool important);
136    bool parseAnimationShorthand(CSSPropertyID, bool important);
137
138    PassRefPtrWillBeRawPtr<CSSValue> parseColumnWidth();
139    PassRefPtrWillBeRawPtr<CSSValue> parseColumnCount();
140    bool parseColumnsShorthand(bool important);
141
142    PassRefPtrWillBeRawPtr<CSSValue> parseGridPosition();
143    bool parseIntegerOrCustomIdentFromGridPosition(RefPtrWillBeRawPtr<CSSPrimitiveValue>& numericValue, RefPtrWillBeRawPtr<CSSPrimitiveValue>& gridLineName);
144    bool parseGridItemPositionShorthand(CSSPropertyID, bool important);
145    bool parseGridTemplateRowsAndAreas(PassRefPtrWillBeRawPtr<CSSValue>, bool important);
146    bool parseGridTemplateShorthand(bool important);
147    bool parseGridShorthand(bool important);
148    bool parseGridAreaShorthand(bool important);
149    bool parseSingleGridAreaLonghand(RefPtrWillBeRawPtr<CSSValue>&);
150    PassRefPtrWillBeRawPtr<CSSValue> parseGridTrackList();
151    bool parseGridTrackRepeatFunction(CSSValueList&);
152    PassRefPtrWillBeRawPtr<CSSValue> parseGridTrackSize(CSSParserValueList& inputList);
153    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseGridBreadth(CSSParserValue*);
154    bool parseGridTemplateAreasRow(NamedGridAreaMap&, const size_t, size_t&);
155    PassRefPtrWillBeRawPtr<CSSValue> parseGridTemplateAreas();
156    bool parseGridLineNames(CSSParserValueList&, CSSValueList&, CSSGridLineNamesValue* = 0);
157    PassRefPtrWillBeRawPtr<CSSValue> parseGridAutoFlow(CSSParserValueList&);
158
159    bool parseClipShape(CSSPropertyID, bool important);
160
161    bool parseLegacyPosition(CSSPropertyID, bool important);
162    bool parseItemPositionOverflowPosition(CSSPropertyID, bool important);
163
164    PassRefPtrWillBeRawPtr<CSSValue> parseShapeProperty(CSSPropertyID propId);
165    PassRefPtrWillBeRawPtr<CSSValue> parseBasicShapeAndOrBox();
166    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseBasicShape();
167    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseShapeRadius(CSSParserValue*);
168
169    PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeCircle(CSSParserValueList* args);
170    PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeEllipse(CSSParserValueList* args);
171    PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapePolygon(CSSParserValueList* args);
172    PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeInset(CSSParserValueList* args);
173
174    bool parseFont(bool important);
175    PassRefPtrWillBeRawPtr<CSSValueList> parseFontFamily();
176
177    PassRefPtrWillBeRawPtr<CSSValue> parseCounter(int defaultValue);
178    PassRefPtrWillBeRawPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
179
180    bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
181    bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
182    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0, bool acceptQuirkyColors = false);
183    bool parseColorFromValue(CSSParserValue*, RGBA32&, bool acceptQuirkyColors = false);
184
185    bool parseLineHeight(bool important);
186    bool parseFontSize(bool important);
187    bool parseFontVariant(bool important);
188    bool parseFontWeight(bool important);
189    PassRefPtrWillBeRawPtr<CSSValueList> parseFontFaceSrc();
190    PassRefPtrWillBeRawPtr<CSSValueList> parseFontFaceUnicodeRange();
191
192    bool parseSVGValue(CSSPropertyID propId, bool important);
193    PassRefPtrWillBeRawPtr<CSSValue> parseSVGStrokeDasharray();
194
195    PassRefPtrWillBeRawPtr<CSSValue> parsePaintOrder() const;
196
197    // CSS3 Parsing Routines (for properties specific to CSS3)
198    PassRefPtrWillBeRawPtr<CSSValueList> parseShadow(CSSParserValueList*, CSSPropertyID);
199    bool parseBorderImageShorthand(CSSPropertyID, bool important);
200    PassRefPtrWillBeRawPtr<CSSValue> parseBorderImage(CSSPropertyID);
201    bool parseBorderImageRepeat(RefPtrWillBeRawPtr<CSSValue>&);
202    bool parseBorderImageSlice(CSSPropertyID, RefPtrWillBeRawPtr<CSSBorderImageSliceValue>&);
203    bool parseBorderImageWidth(RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
204    bool parseBorderImageOutset(RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
205    bool parseBorderRadius(CSSPropertyID, bool important);
206
207    PassRefPtrWillBeRawPtr<CSSValue> parseAspectRatio();
208
209    PassRefPtrWillBeRawPtr<CSSValue> parseReflect();
210
211    bool parseFlex(CSSParserValueList* args, bool important);
212
213    PassRefPtrWillBeRawPtr<CSSValue> parseObjectPosition();
214
215    // Image generators
216    bool parseCanvas(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
217
218    bool parseDeprecatedGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
219    bool parseDeprecatedLinearGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
220    bool parseDeprecatedRadialGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
221    bool parseLinearGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
222    bool parseRadialGradient(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&, CSSGradientRepeat repeating);
223    bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
224
225    bool parseCrossfade(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
226
227    PassRefPtrWillBeRawPtr<CSSValue> parseImageSet(CSSParserValueList*);
228
229    PassRefPtrWillBeRawPtr<CSSValue> parseWillChange();
230
231    PassRefPtrWillBeRawPtr<CSSValueList> parseFilter();
232    PassRefPtrWillBeRawPtr<CSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, CSSFilterValue::FilterOperationType);
233
234    PassRefPtrWillBeRawPtr<CSSValueList> parseTransformOrigin();
235    PassRefPtrWillBeRawPtr<CSSValueList> parseTransform(CSSPropertyID);
236    PassRefPtrWillBeRawPtr<CSSValue> parseTransformValue(CSSPropertyID, CSSParserValue*);
237
238    bool parseTextEmphasisStyle(bool important);
239
240    PassRefPtrWillBeRawPtr<CSSValue> parseTouchAction();
241
242    void addTextDecorationProperty(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important);
243    bool parseTextDecoration(CSSPropertyID propId, bool important);
244    bool parseTextUnderlinePosition(bool important);
245
246    PassRefPtrWillBeRawPtr<CSSValue> parseTextIndent();
247
248    bool parseLineBoxContain(bool important);
249    bool parseCalculation(CSSParserValue*, ValueRange);
250
251    bool parseFontFeatureTag(CSSValueList*);
252    bool parseFontFeatureSettings(bool important);
253
254    bool parseFontVariantLigatures(bool important);
255
256    bool parseGeneratedImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
257
258    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
259    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
260
261    PassRefPtrWillBeRawPtr<CSSValue> createCSSImageValueWithReferrer(const String& rawValue, const KURL&);
262
263    bool validWidthOrHeight(CSSParserValue*);
264
265    PassRefPtrWillBeRawPtr<CSSBasicShape> parseInsetRoundedCorners(PassRefPtrWillBeRawPtr<CSSBasicShapeInset>, CSSParserValueList*);
266
267    enum SizeParameterType {
268        None,
269        Auto,
270        Length,
271        PageSize,
272        Orientation,
273    };
274
275    bool parsePage(CSSPropertyID propId, bool important);
276    bool parseSize(CSSPropertyID propId, bool important);
277    SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue*, SizeParameterType prevParamType);
278
279    bool parseFontFaceSrcURI(CSSValueList*);
280    bool parseFontFaceSrcLocal(CSSValueList*);
281
282    class ImplicitScope {
283        STACK_ALLOCATED();
284        WTF_MAKE_NONCOPYABLE(ImplicitScope);
285    public:
286        ImplicitScope(CSSPropertyParser* parser)
287            : m_parser(parser)
288        {
289            m_parser->m_implicitShorthand = true;
290        }
291
292        ~ImplicitScope()
293        {
294            m_parser->m_implicitShorthand = false;
295        }
296
297    private:
298        CSSPropertyParser* m_parser;
299    };
300
301    class ShorthandScope {
302        STACK_ALLOCATED();
303    public:
304        ShorthandScope(CSSPropertyParser* parser, CSSPropertyID propId) : m_parser(parser)
305        {
306            if (!(m_parser->m_inParseShorthand++))
307                m_parser->m_currentShorthand = propId;
308        }
309        ~ShorthandScope()
310        {
311            if (!(--m_parser->m_inParseShorthand))
312                m_parser->m_currentShorthand = CSSPropertyInvalid;
313        }
314
315    private:
316        CSSPropertyParser* m_parser;
317    };
318
319    enum ReleaseParsedCalcValueCondition {
320        ReleaseParsedCalcValue,
321        DoNotReleaseParsedCalcValue
322    };
323
324    enum Units {
325        FUnknown = 0x0000,
326        FInteger = 0x0001,
327        FNumber = 0x0002, // Real Numbers
328        FPercent = 0x0004,
329        FLength = 0x0008,
330        FAngle = 0x0010,
331        FTime = 0x0020,
332        FFrequency = 0x0040,
333        FPositiveInteger = 0x0080,
334        FRelative = 0x0100,
335        FResolution = 0x0200,
336        FNonNeg = 0x0400
337    };
338
339    friend inline Units operator|(Units a, Units b)
340    {
341        return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
342    }
343
344    bool validCalculationUnit(CSSParserValue*, Units, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
345
346    bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
347
348    inline bool validUnit(CSSParserValue* value, Units unitflags, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue) { return validUnit(value, unitflags, m_context.mode(), releaseCalc); }
349    bool validUnit(CSSParserValue*, Units, CSSParserMode, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
350
351    bool parseBorderImageQuad(Units, RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
352    int colorIntFromValue(CSSParserValue*);
353    bool isCalculation(CSSParserValue*);
354
355private:
356    // Inputs:
357    CSSParserValueList* m_valueList;
358    const CSSParserContext& m_context;
359    const bool m_inViewport;
360
361    // Outputs:
362    WillBeHeapVector<CSSProperty, 256>& m_parsedProperties;
363    CSSRuleSourceData::Type m_ruleType;
364
365    // Locals during parsing:
366    int m_inParseShorthand;
367    CSSPropertyID m_currentShorthand;
368    bool m_implicitShorthand;
369    RefPtrWillBeMember<CSSCalcValue> m_parsedCalculation;
370
371    // FIXME: There is probably a small set of APIs we could expose for these
372    // classes w/o needing to make them friends.
373    friend class ShadowParseContext;
374    friend class BorderImageParseContext;
375    friend class BorderImageSliceParseContext;
376    friend class BorderImageQuadParseContext;
377    friend class TransformOperationInfo;
378    friend bool parseDeprecatedGradientColorStop(CSSPropertyParser*, CSSParserValue*, CSSGradientColorStop&);
379    friend PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseGradientColorOrKeyword(CSSPropertyParser*, CSSParserValue*);
380};
381
382CSSPropertyID cssPropertyID(const CSSParserString&);
383CSSPropertyID cssPropertyID(const String&);
384CSSValueID cssValueKeywordID(const CSSParserString&);
385
386bool isKeywordPropertyID(CSSPropertyID);
387bool isValidKeywordPropertyAndValue(CSSPropertyID, CSSValueID, const CSSParserContext&);
388
389} // namespace blink
390
391#endif // CSSPropertyParser_h
392