Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / parser / CSSPropertyParser.h
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/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"
41
42 namespace WebCore {
43
44 // FIXME: Many of these may not be used.
45 class CSSArrayFunctionValue;
46 class CSSBorderImageSliceValue;
47 class CSSPrimitiveValue;
48 class CSSSelectorList;
49 class CSSValue;
50 class CSSValueList;
51 class CSSBasicShape;
52 class CSSBasicShapeInset;
53 class CSSGridLineNamesValue;
54 class Document;
55 class Element;
56 class ImmutableStylePropertySet;
57 class StyleKeyframe;
58 class StylePropertyShorthand;
59 class StyleKeyframe;
60 class UseCounter;
61
62 // Inputs: PropertyID, isImportant bool, CSSParserValueList.
63 // Outputs: Vector of CSSProperties
64
65 class CSSPropertyParser {
66     STACK_ALLOCATED();
67 public:
68     CSSPropertyParser(OwnPtr<CSSParserValueList>&,
69         const CSSParserContext&, bool inViewport, bool savedImportant,
70         WillBeHeapVector<CSSProperty, 256>&, CSSRuleSourceData::Type);
71     ~CSSPropertyParser();
72
73     // FIXME: Should this be on a separate ColorParser object?
74     template<typename StringType>
75     static bool fastParseColor(RGBA32&, const StringType&, bool strict);
76
77     bool parseValue(CSSPropertyID, bool important);
78
79     static bool isSystemColor(int id);
80
81 private:
82     bool inShorthand() const { return m_inParseShorthand; }
83     bool inQuirksMode() const { return isQuirksModeBehavior(m_context.mode()); }
84
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);
88
89     KURL completeURL(const String& url) const;
90
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);
96
97     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseValidPrimitive(CSSValueID ident, CSSParserValue*);
98
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);
103
104     PassRefPtrWillBeRawPtr<CSSValue> parseAttr(CSSParserValueList* args);
105
106     PassRefPtrWillBeRawPtr<CSSValue> parseBackgroundColor();
107
108     bool parseFillImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
109
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>);
120
121     void parseFillRepeat(RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
122     PassRefPtrWillBeRawPtr<CSSValue> parseFillSize(CSSPropertyID, bool &allowComma);
123
124     bool parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtrWillBeRawPtr<CSSValue>&, RefPtrWillBeRawPtr<CSSValue>&);
125     bool parseFillShorthand(CSSPropertyID, const CSSPropertyID* properties, int numProperties, bool important);
126
127     void addFillValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval);
128
129     void addAnimationValue(RefPtrWillBeRawPtr<CSSValue>& lval, PassRefPtrWillBeRawPtr<CSSValue> rval);
130
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();
140
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);
147
148     PassRefPtrWillBeRawPtr<CSSValue> parseColumnWidth();
149     PassRefPtrWillBeRawPtr<CSSValue> parseColumnCount();
150     bool parseColumnsShorthand(bool important);
151
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);
167
168     bool parseClipShape(CSSPropertyID, bool important);
169
170     bool parseItemPositionOverflowPosition(CSSPropertyID, bool important);
171
172     PassRefPtrWillBeRawPtr<CSSValue> parseShapeProperty(CSSPropertyID propId);
173     PassRefPtrWillBeRawPtr<CSSValue> parseBasicShapeAndOrBox();
174     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseBasicShape();
175     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> parseShapeRadius(CSSParserValue*);
176
177     PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeCircle(CSSParserValueList* args);
178     PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeEllipse(CSSParserValueList* args);
179     PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapePolygon(CSSParserValueList* args);
180     PassRefPtrWillBeRawPtr<CSSBasicShape> parseBasicShapeInset(CSSParserValueList* args);
181
182     bool parseFont(bool important);
183     PassRefPtrWillBeRawPtr<CSSValueList> parseFontFamily();
184
185     bool parseCounter(CSSPropertyID, int defaultValue, bool important);
186     PassRefPtrWillBeRawPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
187
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&);
192
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();
199
200     bool parseSVGValue(CSSPropertyID propId, bool important);
201     PassRefPtrWillBeRawPtr<CSSValue> parseSVGPaint();
202     PassRefPtrWillBeRawPtr<CSSValue> parseSVGStrokeDasharray();
203
204     PassRefPtrWillBeRawPtr<CSSValue> parsePaintOrder() const;
205
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);
215
216     bool parseAspectRatio(bool important);
217
218     bool parseReflect(CSSPropertyID, bool important);
219
220     bool parseFlex(CSSParserValueList* args, bool important);
221
222     bool parseObjectPosition(bool important);
223
224     // Image generators
225     bool parseCanvas(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
226
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);
233
234     bool parseCrossfade(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
235
236     PassRefPtrWillBeRawPtr<CSSValue> parseImageSet(CSSParserValueList*);
237
238     bool parseWillChange(bool important);
239
240     PassRefPtrWillBeRawPtr<CSSValueList> parseFilter();
241     PassRefPtrWillBeRawPtr<CSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, CSSFilterValue::FilterOperationType);
242
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>&);
248
249     bool parseTextEmphasisStyle(bool important);
250
251     bool parseTouchAction(bool important);
252
253     void addTextDecorationProperty(CSSPropertyID, PassRefPtrWillBeRawPtr<CSSValue>, bool important);
254     bool parseTextDecoration(CSSPropertyID propId, bool important);
255     bool parseTextUnderlinePosition(bool important);
256
257     PassRefPtrWillBeRawPtr<CSSValue> parseTextIndent();
258
259     bool parseLineBoxContain(bool important);
260     bool parseCalculation(CSSParserValue*, ValueRange);
261
262     bool parseFontFeatureTag(CSSValueList*);
263     bool parseFontFeatureSettings(bool important);
264
265     bool parseFontVariantLigatures(bool important);
266
267     bool parseGeneratedImage(CSSParserValueList*, RefPtrWillBeRawPtr<CSSValue>&);
268
269     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
270     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
271
272     PassRefPtrWillBeRawPtr<CSSValue> createCSSImageValueWithReferrer(const String& rawValue, const KURL&);
273
274     bool validWidthOrHeight(CSSParserValue*);
275
276     PassRefPtrWillBeRawPtr<CSSBasicShape> parseInsetRoundedCorners(PassRefPtrWillBeRawPtr<CSSBasicShapeInset>, CSSParserValueList*);
277
278     enum SizeParameterType {
279         None,
280         Auto,
281         Length,
282         PageSize,
283         Orientation,
284     };
285
286     bool parsePage(CSSPropertyID propId, bool important);
287     bool parseSize(CSSPropertyID propId, bool important);
288     SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue*, SizeParameterType prevParamType);
289
290     bool parseFontFaceSrcURI(CSSValueList*);
291     bool parseFontFaceSrcLocal(CSSValueList*);
292
293     enum PropertyType {
294         PropertyExplicit,
295         PropertyImplicit
296     };
297
298     class ImplicitScope {
299         STACK_ALLOCATED();
300         WTF_MAKE_NONCOPYABLE(ImplicitScope);
301     public:
302         ImplicitScope(CSSPropertyParser* parser, PropertyType propertyType)
303             : m_parser(parser)
304         {
305             m_parser->m_implicitShorthand = propertyType == CSSPropertyParser::PropertyImplicit;
306         }
307
308         ~ImplicitScope()
309         {
310             m_parser->m_implicitShorthand = false;
311         }
312
313     private:
314         CSSPropertyParser* m_parser;
315     };
316
317     class ShorthandScope {
318         STACK_ALLOCATED();
319     public:
320         ShorthandScope(CSSPropertyParser* parser, CSSPropertyID propId) : m_parser(parser)
321         {
322             if (!(m_parser->m_inParseShorthand++))
323                 m_parser->m_currentShorthand = propId;
324         }
325         ~ShorthandScope()
326         {
327             if (!(--m_parser->m_inParseShorthand))
328                 m_parser->m_currentShorthand = CSSPropertyInvalid;
329         }
330
331     private:
332         CSSPropertyParser* m_parser;
333     };
334
335     enum ReleaseParsedCalcValueCondition {
336         ReleaseParsedCalcValue,
337         DoNotReleaseParsedCalcValue
338     };
339
340     enum Units {
341         FUnknown = 0x0000,
342         FInteger = 0x0001,
343         FNumber = 0x0002, // Real Numbers
344         FPercent = 0x0004,
345         FLength = 0x0008,
346         FAngle = 0x0010,
347         FTime = 0x0020,
348         FFrequency = 0x0040,
349         FPositiveInteger = 0x0080,
350         FRelative = 0x0100,
351         FResolution = 0x0200,
352         FNonNeg = 0x0400
353     };
354
355     friend inline Units operator|(Units a, Units b)
356     {
357         return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
358     }
359
360     bool validCalculationUnit(CSSParserValue*, Units, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
361
362     bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
363
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);
366
367     bool parseBorderImageQuad(Units, RefPtrWillBeRawPtr<CSSPrimitiveValue>&);
368     int colorIntFromValue(CSSParserValue*);
369     double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
370     bool isCalculation(CSSParserValue*);
371
372 private:
373     // Inputs:
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!
379
380     // Outputs:
381     WillBeHeapVector<CSSProperty, 256>& m_parsedProperties;
382     CSSRuleSourceData::Type m_ruleType;
383
384     // Locals during parsing:
385     int m_inParseShorthand;
386     CSSPropertyID m_currentShorthand;
387     bool m_implicitShorthand;
388     RefPtrWillBeMember<CSSCalcValue> m_parsedCalculation;
389
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*);
399 };
400
401 CSSPropertyID cssPropertyID(const CSSParserString&);
402 CSSPropertyID cssPropertyID(const String&);
403 CSSValueID cssValueKeywordID(const CSSParserString&);
404
405 bool isKeywordPropertyID(CSSPropertyID);
406 bool isValidKeywordPropertyAndValue(CSSPropertyID, CSSValueID, const CSSParserContext&);
407
408 } // namespace WebCore
409
410 #endif // CSSPropertyParser_h