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 CSSParser_h
24#define CSSParser_h
25
26#include "CSSPropertyNames.h"
27#include "CSSValueKeywords.h"
28#include "core/css/CSSCalculationValue.h"
29#include "core/css/CSSFilterValue.h"
30#include "core/css/CSSGradientValue.h"
31#include "core/css/CSSParserMode.h"
32#include "core/css/CSSParserValues.h"
33#include "core/css/CSSProperty.h"
34#include "core/css/CSSPropertySourceData.h"
35#include "core/css/CSSSelector.h"
36#include "core/css/CSSTokenizer.h"
37#include "core/css/MediaQuery.h"
38#include "core/css/StylePropertySet.h"
39#include "core/frame/UseCounter.h"
40#include "platform/graphics/Color.h"
41#include "wtf/HashSet.h"
42#include "wtf/OwnPtr.h"
43#include "wtf/Vector.h"
44#include "wtf/text/AtomicString.h"
45#include "wtf/text/TextPosition.h"
46
47namespace WebCore {
48
49class AnimationParseContext;
50class CSSArrayFunctionValue;
51class CSSBorderImageSliceValue;
52class CSSMixFunctionValue;
53class CSSPrimitiveValue;
54class CSSSelectorList;
55class CSSShaderValue;
56class CSSValue;
57class CSSValueList;
58class CSSBasicShape;
59class Document;
60class Element;
61class ImmutableStylePropertySet;
62class MediaQueryExp;
63class MediaQuerySet;
64class MutableStylePropertySet;
65class StyleKeyframe;
66class StylePropertyShorthand;
67class StyleRuleBase;
68class StyleRuleKeyframes;
69class StyleKeyframe;
70class StyleSheetContents;
71
72// FIXME: This class is shared with CSSTokenizer so should we rename it to CSSSourceLocation?
73struct CSSParserLocation {
74    unsigned offset;
75    unsigned lineNumber;
76    CSSParserString token;
77};
78
79class CSSParser {
80    friend inline int cssyylex(void*, CSSParser*);
81
82public:
83    class SourceDataHandler;
84    enum ErrorType {
85        NoError,
86        PropertyDeclarationError,
87        InvalidPropertyValueError,
88        InvalidPropertyError,
89        InvalidSelectorError,
90        InvalidSupportsConditionError,
91        InvalidRuleError,
92        InvalidMediaQueryError,
93        InvalidKeyframeSelectorError,
94        InvalidSelectorPseudoError,
95        UnterminatedCommentError,
96        GeneralError
97    };
98
99    CSSParser(const CSSParserContext&, UseCounter* = 0);
100
101    ~CSSParser();
102
103    void parseSheet(StyleSheetContents*, const String&, const TextPosition& startPosition = TextPosition::minimumPosition(), SourceDataHandler* = 0, bool = false);
104    PassRefPtr<StyleRuleBase> parseRule(StyleSheetContents*, const String&);
105    PassRefPtr<StyleKeyframe> parseKeyframeRule(StyleSheetContents*, const String&);
106    bool parseSupportsCondition(const String&);
107    static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, CSSParserMode, StyleSheetContents*);
108    static bool parseColor(RGBA32& color, const String&, bool strict = false);
109    static bool parseSystemColor(RGBA32& color, const String&, Document*);
110    static PassRefPtr<CSSValueList> parseFontFaceValue(const AtomicString&);
111    PassRefPtr<CSSPrimitiveValue> parseValidPrimitive(CSSValueID ident, CSSParserValue*);
112    bool parseDeclaration(MutableStylePropertySet*, const String&, SourceDataHandler*, StyleSheetContents* contextStyleSheet);
113    static PassRefPtr<ImmutableStylePropertySet> parseInlineStyleDeclaration(const String&, Element*);
114    PassRefPtr<MediaQuerySet> parseMediaQueryList(const String&);
115    PassOwnPtr<Vector<double> > parseKeyframeKeyList(const String&);
116
117    void addPropertyWithPrefixingVariant(CSSPropertyID, PassRefPtr<CSSValue>, bool important, bool implicit = false);
118    void addProperty(CSSPropertyID, PassRefPtr<CSSValue>, bool important, bool implicit = false);
119    void rollbackLastProperties(int num);
120    bool hasProperties() const { return !m_parsedProperties.isEmpty(); }
121    void addExpandedPropertyForValue(CSSPropertyID propId, PassRefPtr<CSSValue>, bool);
122    void setCurrentProperty(CSSPropertyID);
123
124    bool parseValue(CSSPropertyID, bool important);
125    bool parseShorthand(CSSPropertyID, const StylePropertyShorthand&, bool important);
126    bool parse4Values(CSSPropertyID, const CSSPropertyID* properties, bool important);
127    bool parseContent(CSSPropertyID, bool important);
128    bool parseQuotes(CSSPropertyID, bool important);
129
130    static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, const Document&);
131    void storeVariableDeclaration(const CSSParserString&, PassOwnPtr<CSSParserValueList>, bool important);
132
133    PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
134
135    PassRefPtr<CSSValue> parseBackgroundColor();
136
137    bool parseFillImage(CSSParserValueList*, RefPtr<CSSValue>&);
138
139    enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
140    enum FillPositionParsingMode { ResolveValuesAsPercent = 0, ResolveValuesAsKeyword = 1 };
141    PassRefPtr<CSSPrimitiveValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag, FillPositionParsingMode = ResolveValuesAsPercent);
142    PassRefPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
143    PassRefPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
144    void parse2ValuesFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
145    bool isPotentialPositionValue(CSSParserValue*);
146    void parseFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
147    void parse3ValuesFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&, PassRefPtr<CSSPrimitiveValue>, PassRefPtr<CSSPrimitiveValue>);
148    void parse4ValuesFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&, PassRefPtr<CSSPrimitiveValue>, PassRefPtr<CSSPrimitiveValue>);
149
150    void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
151    PassRefPtr<CSSValue> parseFillSize(CSSPropertyID, bool &allowComma);
152
153    bool parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
154    bool parseFillShorthand(CSSPropertyID, const CSSPropertyID* properties, int numProperties, bool important);
155
156    void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
157
158    void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
159
160    PassRefPtr<CSSValue> parseAnimationDelay();
161    PassRefPtr<CSSValue> parseAnimationDirection();
162    PassRefPtr<CSSValue> parseAnimationDuration();
163    PassRefPtr<CSSValue> parseAnimationFillMode();
164    PassRefPtr<CSSValue> parseAnimationIterationCount();
165    PassRefPtr<CSSValue> parseAnimationName();
166    PassRefPtr<CSSValue> parseAnimationPlayState();
167    PassRefPtr<CSSValue> parseAnimationProperty(AnimationParseContext&);
168    PassRefPtr<CSSValue> parseAnimationTimingFunction();
169
170    bool parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
171    bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
172    bool parseAnimationProperty(CSSPropertyID, RefPtr<CSSValue>&, AnimationParseContext&);
173    bool parseTransitionShorthand(CSSPropertyID, bool important);
174    bool parseAnimationShorthand(CSSPropertyID, bool important);
175
176    PassRefPtr<CSSValue> parseColumnWidth();
177    PassRefPtr<CSSValue> parseColumnCount();
178    bool parseColumnsShorthand(bool important);
179
180    PassRefPtr<CSSValue> parseGridPosition();
181    bool parseIntegerOrStringFromGridPosition(RefPtr<CSSPrimitiveValue>& numericValue, RefPtr<CSSPrimitiveValue>& gridLineName);
182    bool parseGridItemPositionShorthand(CSSPropertyID, bool important);
183    bool parseGridAreaShorthand(bool important);
184    bool parseSingleGridAreaLonghand(RefPtr<CSSValue>&);
185    bool parseGridTrackList(CSSPropertyID, bool important);
186    bool parseGridTrackRepeatFunction(CSSValueList&);
187    PassRefPtr<CSSValue> parseGridTrackSize(CSSParserValueList& inputList);
188    PassRefPtr<CSSPrimitiveValue> parseGridBreadth(CSSParserValue*);
189    PassRefPtr<CSSValue> parseGridTemplate();
190    void parseGridLineNames(CSSParserValueList* inputList, CSSValueList&);
191
192    bool parseClipShape(CSSPropertyID, bool important);
193
194    bool parseBasicShape(CSSPropertyID, bool important);
195    PassRefPtr<CSSBasicShape> parseBasicShapeRectangle(CSSParserValueList* args);
196    PassRefPtr<CSSBasicShape> parseBasicShapeCircle(CSSParserValueList* args);
197    PassRefPtr<CSSBasicShape> parseBasicShapeEllipse(CSSParserValueList* args);
198    PassRefPtr<CSSBasicShape> parseBasicShapePolygon(CSSParserValueList* args);
199    PassRefPtr<CSSBasicShape> parseBasicShapeInsetRectangle(CSSParserValueList* args);
200
201    bool parseFont(bool important);
202    PassRefPtr<CSSValueList> parseFontFamily();
203
204    bool parseCounter(CSSPropertyID, int defaultValue, bool important);
205    PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
206
207    bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
208    bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
209    PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
210    bool parseColorFromValue(CSSParserValue*, RGBA32&);
211    void parseSelector(const String&, CSSSelectorList&);
212
213    template<typename StringType>
214    static bool fastParseColor(RGBA32&, const StringType&, bool strict);
215
216    bool parseLineHeight(bool important);
217    bool parseFontSize(bool important);
218    bool parseFontVariant(bool important);
219    bool parseFontWeight(bool important);
220    bool parseFontFaceSrc();
221    bool parseFontFaceUnicodeRange();
222
223    bool parseSVGValue(CSSPropertyID propId, bool important);
224    PassRefPtr<CSSValue> parseSVGPaint();
225    PassRefPtr<CSSValue> parseSVGColor();
226    PassRefPtr<CSSValue> parseSVGStrokeDasharray();
227
228    PassRefPtr<CSSValue> parsePaintOrder() const;
229
230    // CSS3 Parsing Routines (for properties specific to CSS3)
231    PassRefPtr<CSSValueList> parseShadow(CSSParserValueList*, CSSPropertyID);
232    bool parseBorderImageShorthand(CSSPropertyID, bool important);
233    PassRefPtr<CSSValue> parseBorderImage(CSSPropertyID);
234    bool parseBorderImageRepeat(RefPtr<CSSValue>&);
235    bool parseBorderImageSlice(CSSPropertyID, RefPtr<CSSBorderImageSliceValue>&);
236    bool parseBorderImageWidth(RefPtr<CSSPrimitiveValue>&);
237    bool parseBorderImageOutset(RefPtr<CSSPrimitiveValue>&);
238    bool parseBorderRadius(CSSPropertyID, bool important);
239
240    bool parseAspectRatio(bool important);
241
242    bool parseReflect(CSSPropertyID, bool important);
243
244    bool parseFlex(CSSParserValueList* args, bool important);
245
246    bool parseObjectPosition(bool important);
247
248    // Image generators
249    bool parseCanvas(CSSParserValueList*, RefPtr<CSSValue>&);
250
251    bool parseDeprecatedGradient(CSSParserValueList*, RefPtr<CSSValue>&);
252    bool parseDeprecatedLinearGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
253    bool parseDeprecatedRadialGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
254    bool parseLinearGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
255    bool parseRadialGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
256    bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
257
258    bool parseCrossfade(CSSParserValueList*, RefPtr<CSSValue>&);
259
260    PassRefPtr<CSSValue> parseImageSet(CSSParserValueList*);
261
262    PassRefPtr<CSSValueList> parseFilter();
263    PassRefPtr<CSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, CSSFilterValue::FilterOperationType);
264    PassRefPtr<CSSMixFunctionValue> parseMixFunction(CSSParserValue*);
265    PassRefPtr<CSSArrayFunctionValue> parseCustomFilterArrayFunction(CSSParserValue*);
266    PassRefPtr<CSSValueList> parseCustomFilterTransform(CSSParserValueList*);
267    PassRefPtr<CSSValueList> parseCustomFilterParameters(CSSParserValueList*);
268    PassRefPtr<CSSFilterValue> parseCustomFilterFunctionWithAtRuleReferenceSyntax(CSSParserValue*);
269    PassRefPtr<CSSFilterValue> parseCustomFilterFunctionWithInlineSyntax(CSSParserValue*);
270    PassRefPtr<CSSFilterValue> parseCustomFilterFunction(CSSParserValue*);
271    bool parseFilterRuleSrc();
272    PassRefPtr<CSSShaderValue> parseFilterRuleSrcUriAndFormat(CSSParserValueList*);
273
274    static bool isBlendMode(CSSValueID);
275    static bool isCompositeOperator(CSSValueID);
276
277    PassRefPtr<CSSValueList> parseTransform();
278    PassRefPtr<CSSValue> parseTransformValue(CSSParserValue*);
279    bool parseTransformOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, CSSPropertyID& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
280    bool parsePerspectiveOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2,  RefPtr<CSSValue>&, RefPtr<CSSValue>&);
281
282    bool parseTextEmphasisStyle(bool important);
283
284    bool parseTouchAction(bool important);
285
286    void addTextDecorationProperty(CSSPropertyID, PassRefPtr<CSSValue>, bool important);
287    bool parseTextDecoration(CSSPropertyID propId, bool important);
288    bool parseTextUnderlinePosition(bool important);
289
290    PassRefPtr<CSSValue> parseTextIndent();
291
292    bool parseLineBoxContain(bool important);
293    bool parseCalculation(CSSParserValue*, ValueRange);
294
295    bool parseFontFeatureTag(CSSValueList*);
296    bool parseFontFeatureSettings(bool important);
297
298    bool parseFlowThread(const String& flowName);
299    bool parseFlowThread(CSSPropertyID, bool important);
300    bool parseRegionThread(CSSPropertyID, bool important);
301
302    bool parseFontVariantLigatures(bool important);
303
304    bool parseGeneratedImage(CSSParserValueList*, RefPtr<CSSValue>&);
305
306    CSSParserSelector* createFloatingSelector();
307    CSSParserSelector* createFloatingSelectorWithTagName(const QualifiedName&);
308    PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
309
310    Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
311    PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
312
313    CSSParserValueList* createFloatingValueList();
314    PassOwnPtr<CSSParserValueList> sinkFloatingValueList(CSSParserValueList*);
315
316    CSSParserFunction* createFloatingFunction();
317    CSSParserFunction* createFloatingFunction(const CSSParserString& name, PassOwnPtr<CSSParserValueList> args);
318    PassOwnPtr<CSSParserFunction> sinkFloatingFunction(CSSParserFunction*);
319
320    CSSParserValue& sinkFloatingValue(CSSParserValue&);
321
322    MediaQuerySet* createMediaQuerySet();
323    StyleRuleBase* createImportRule(const CSSParserString&, MediaQuerySet*);
324    StyleKeyframe* createKeyframe(CSSParserValueList*);
325    StyleRuleKeyframes* createKeyframesRule(const String&, PassOwnPtr<Vector<RefPtr<StyleKeyframe> > >, bool isPrefixed);
326
327    typedef Vector<RefPtr<StyleRuleBase> > RuleList;
328    StyleRuleBase* createMediaRule(MediaQuerySet*, RuleList*);
329    RuleList* createRuleList();
330    RuleList* appendRule(RuleList*, StyleRuleBase*);
331    StyleRuleBase* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
332    StyleRuleBase* createFontFaceRule();
333    StyleRuleBase* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
334    StyleRuleBase* createRegionRule(Vector<OwnPtr<CSSParserSelector> >* regionSelector, RuleList* rules);
335    StyleRuleBase* createMarginAtRule(CSSSelector::MarginBoxType);
336    StyleRuleBase* createSupportsRule(bool conditionIsSupported, RuleList*);
337    void markSupportsRuleHeaderStart();
338    void markSupportsRuleHeaderEnd();
339    PassRefPtr<CSSRuleSourceData> popSupportsRuleData();
340    StyleRuleBase* createHostRule(RuleList* rules);
341    StyleRuleBase* createFilterRule(const CSSParserString&);
342
343    void startDeclarationsForMarginBox();
344    void endDeclarationsForMarginBox();
345
346    MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
347    PassOwnPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
348    Vector<OwnPtr<MediaQueryExp> >* createFloatingMediaQueryExpList();
349    PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > sinkFloatingMediaQueryExpList(Vector<OwnPtr<MediaQueryExp> >*);
350    MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const AtomicString&, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
351    MediaQuery* createFloatingMediaQuery(PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
352    MediaQuery* createFloatingNotAllQuery();
353    PassOwnPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
354
355    Vector<RefPtr<StyleKeyframe> >* createFloatingKeyframeVector();
356    PassOwnPtr<Vector<RefPtr<StyleKeyframe> > > sinkFloatingKeyframeVector(Vector<RefPtr<StyleKeyframe> >*);
357
358    void addNamespace(const AtomicString& prefix, const AtomicString& uri);
359    QualifiedName determineNameInNamespace(const AtomicString& prefix, const AtomicString& localName);
360
361    CSSParserSelector* rewriteSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*, bool isNamespacePlaceholder = false);
362    CSSParserSelector* rewriteSpecifiersWithElementNameForCustomPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
363    CSSParserSelector* rewriteSpecifiersWithElementNameForContentPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
364    CSSParserSelector* rewriteSpecifiersWithNamespaceIfNeeded(CSSParserSelector*);
365    CSSParserSelector* rewriteSpecifiers(CSSParserSelector*, CSSParserSelector*);
366    CSSParserSelector* rewriteSpecifiersForShadowDistributed(CSSParserSelector* specifiers, CSSParserSelector* distributedPseudoElementSelector);
367
368    void invalidBlockHit();
369
370    Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
371
372    void setReusableRegionSelectorVector(Vector<OwnPtr<CSSParserSelector> >* selectors);
373    Vector<OwnPtr<CSSParserSelector> >* reusableRegionSelectorVector() { return &m_reusableRegionSelectorVector; }
374
375    void clearProperties();
376
377    PassRefPtr<ImmutableStylePropertySet> createStylePropertySet();
378
379    CSSParserContext m_context;
380
381    bool m_important;
382    CSSPropertyID m_id;
383    StyleSheetContents* m_styleSheet;
384    RefPtr<StyleRuleBase> m_rule;
385    RefPtr<StyleKeyframe> m_keyframe;
386    RefPtr<MediaQuerySet> m_mediaList;
387    OwnPtr<CSSParserValueList> m_valueList;
388    bool m_supportsCondition;
389
390    typedef Vector<CSSProperty, 256> ParsedPropertyVector;
391    ParsedPropertyVector m_parsedProperties;
392    CSSSelectorList* m_selectorListForParseSelector;
393
394    unsigned m_numParsedPropertiesBeforeMarginBox;
395
396    int m_inParseShorthand;
397    CSSPropertyID m_currentShorthand;
398    bool m_implicitShorthand;
399
400    bool m_hasFontFaceOnlyValues;
401    bool m_hadSyntacticallyValidCSSRule;
402    bool m_logErrors;
403    bool m_ignoreErrors;
404
405    bool m_inFilterRule;
406
407    AtomicString m_defaultNamespace;
408
409    // tokenizer methods and data
410    SourceDataHandler* m_sourceDataHandler;
411
412    void startRule();
413    void endRule(bool valid);
414    void startRuleHeader(CSSRuleSourceData::Type);
415    void endRuleHeader();
416    void startSelector();
417    void endSelector();
418    void startRuleBody();
419    void startProperty();
420    void endProperty(bool isImportantFound, bool isPropertyParsed, ErrorType = NoError);
421    void startEndUnknownRule();
422
423    void endInvalidRuleHeader();
424    void reportError(const CSSParserLocation&, ErrorType = GeneralError);
425    void resumeErrorLogging() { m_ignoreErrors = false; }
426    void setLocationLabel(const CSSParserLocation& location) { m_locationLabel = location; }
427    const CSSParserLocation& lastLocationLabel() const { return m_locationLabel; }
428
429    void tokenToLowerCase(const CSSParserString& token);
430
431    void markViewportRuleBodyStart() { m_inViewport = true; }
432    void markViewportRuleBodyEnd() { m_inViewport = false; }
433    StyleRuleBase* createViewportRule();
434
435    PassRefPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
436    PassRefPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
437    PassRefPtr<CSSPrimitiveValue> createPrimitiveVariableNameValue(CSSParserValue*);
438
439    static KURL completeURL(const CSSParserContext&, const String& url);
440
441    CSSParserLocation currentLocation() { return m_tokenizer.currentLocation(); }
442
443private:
444    enum PropertyType {
445        PropertyExplicit,
446        PropertyImplicit
447    };
448
449    class ImplicitScope {
450        WTF_MAKE_NONCOPYABLE(ImplicitScope);
451    public:
452        ImplicitScope(WebCore::CSSParser* parser, PropertyType propertyType)
453            : m_parser(parser)
454        {
455            m_parser->m_implicitShorthand = propertyType == CSSParser::PropertyImplicit;
456        }
457
458        ~ImplicitScope()
459        {
460            m_parser->m_implicitShorthand = false;
461        }
462
463    private:
464        WebCore::CSSParser* m_parser;
465    };
466
467    class StyleDeclarationScope {
468        WTF_MAKE_NONCOPYABLE(StyleDeclarationScope);
469    public:
470        StyleDeclarationScope(CSSParser* parser, const StylePropertySet* declaration)
471            : m_parser(parser)
472            , m_mode(declaration->cssParserMode())
473        {
474            if (isCSSViewportParsingEnabledForMode(m_mode)) {
475                ASSERT(!m_parser->inViewport());
476                m_parser->markViewportRuleBodyStart();
477            }
478        }
479
480        ~StyleDeclarationScope()
481        {
482            if (isCSSViewportParsingEnabledForMode(m_mode))
483                m_parser->markViewportRuleBodyEnd();
484        }
485
486    private:
487        CSSParser* m_parser;
488        CSSParserMode m_mode;
489    };
490
491    inline void ensureLineEndings();
492
493    void setStyleSheet(StyleSheetContents* styleSheet) { m_styleSheet = styleSheet; }
494
495    bool inQuirksMode() const { return isQuirksModeBehavior(m_context.mode()); }
496    bool inViewport() const { return m_inViewport; }
497
498    KURL completeURL(const String& url) const;
499
500    void recheckAtKeyword(const UChar* str, int len);
501
502    template<unsigned prefixLength, unsigned suffixLength>
503    inline void setupParser(const char (&prefix)[prefixLength], const String& string, const char (&suffix)[suffixLength])
504    {
505        setupParser(prefix, prefixLength - 1, string, suffix, suffixLength - 1);
506    }
507    void setupParser(const char* prefix, unsigned prefixLength, const String&, const char* suffix, unsigned suffixLength);
508    bool inShorthand() const { return m_inParseShorthand; }
509
510    bool validWidthOrHeight(CSSParserValue*);
511
512    void deleteFontFaceOnlyValues();
513
514    bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, StyleSheetContents* contextStyleSheet);
515    PassRefPtr<ImmutableStylePropertySet> parseDeclaration(const String&, StyleSheetContents* contextStyleSheet);
516
517    enum SizeParameterType {
518        None,
519        Auto,
520        Length,
521        PageSize,
522        Orientation,
523    };
524
525    bool parsePage(CSSPropertyID propId, bool important);
526    bool parseSize(CSSPropertyID propId, bool important);
527    SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
528
529    bool parseFontFaceSrcURI(CSSValueList*);
530    bool parseFontFaceSrcLocal(CSSValueList*);
531
532    bool parseColor(const String&);
533
534    const String* m_source;
535    TextPosition m_startPosition;
536    CSSRuleSourceData::Type m_ruleHeaderType;
537    unsigned m_ruleHeaderStartOffset;
538    int m_ruleHeaderStartLineNumber;
539    OwnPtr<Vector<unsigned> > m_lineEndings;
540
541    bool m_ruleHasHeader;
542
543    bool m_allowImportRules;
544    bool m_allowNamespaceDeclarations;
545
546    bool parseViewportProperty(CSSPropertyID propId, bool important);
547    bool parseViewportShorthand(CSSPropertyID propId, CSSPropertyID first, CSSPropertyID second, bool important);
548
549    bool m_inViewport;
550
551    CSSParserLocation m_locationLabel;
552
553    bool useLegacyBackgroundSizeShorthandBehavior() const;
554
555    Vector<RefPtr<StyleRuleBase> > m_parsedRules;
556    Vector<RefPtr<StyleKeyframe> > m_parsedKeyframes;
557    Vector<RefPtr<MediaQuerySet> > m_parsedMediaQuerySets;
558    Vector<OwnPtr<RuleList> > m_parsedRuleLists;
559    Vector<CSSParserSelector*> m_floatingSelectors;
560    Vector<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
561    Vector<CSSParserValueList*> m_floatingValueLists;
562    Vector<CSSParserFunction*> m_floatingFunctions;
563
564    OwnPtr<MediaQuery> m_floatingMediaQuery;
565    OwnPtr<MediaQueryExp> m_floatingMediaQueryExp;
566    OwnPtr<Vector<OwnPtr<MediaQueryExp> > > m_floatingMediaQueryExpList;
567
568    OwnPtr<Vector<RefPtr<StyleKeyframe> > > m_floatingKeyframeVector;
569
570    Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
571    Vector<OwnPtr<CSSParserSelector> > m_reusableRegionSelectorVector;
572
573    RefPtr<CSSCalcValue> m_parsedCalculation;
574
575    OwnPtr<RuleSourceDataList> m_supportsRuleDataStack;
576
577    // defines units allowed for a certain property, used in parseUnit
578    enum Units {
579        FUnknown = 0x0000,
580        FInteger = 0x0001,
581        FNumber = 0x0002, // Real Numbers
582        FPercent = 0x0004,
583        FLength = 0x0008,
584        FAngle = 0x0010,
585        FTime = 0x0020,
586        FFrequency = 0x0040,
587        FPositiveInteger = 0x0080,
588        FRelative = 0x0100,
589        FResolution = 0x0200,
590        FNonNeg = 0x0400
591    };
592
593    friend inline Units operator|(Units a, Units b)
594    {
595        return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
596    }
597
598    enum ReleaseParsedCalcValueCondition {
599        ReleaseParsedCalcValue,
600        DoNotReleaseParsedCalcValue
601    };
602
603    bool isLoggingErrors();
604    void logError(const String& message, const CSSParserLocation&);
605
606    bool validCalculationUnit(CSSParserValue*, Units, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
607
608    bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
609
610    inline bool validUnit(CSSParserValue* value, Units unitflags, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue) { return validUnit(value, unitflags, m_context.mode(), releaseCalc); }
611    bool validUnit(CSSParserValue*, Units, CSSParserMode, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
612
613    bool parseBorderImageQuad(Units, RefPtr<CSSPrimitiveValue>&);
614    int colorIntFromValue(CSSParserValue*);
615    double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
616    bool isCalculation(CSSParserValue*);
617
618    UseCounter* m_useCounter;
619
620    CSSTokenizer m_tokenizer;
621
622    friend class TransformOperationInfo;
623    friend class FilterOperationInfo;
624};
625
626CSSPropertyID cssPropertyID(const CSSParserString&);
627CSSPropertyID cssPropertyID(const String&);
628CSSValueID cssValueKeywordID(const CSSParserString&);
629
630class ShorthandScope {
631    WTF_MAKE_FAST_ALLOCATED;
632public:
633    ShorthandScope(CSSParser* parser, CSSPropertyID propId) : m_parser(parser)
634    {
635        if (!(m_parser->m_inParseShorthand++))
636            m_parser->m_currentShorthand = propId;
637    }
638    ~ShorthandScope()
639    {
640        if (!(--m_parser->m_inParseShorthand))
641            m_parser->m_currentShorthand = CSSPropertyInvalid;
642    }
643
644private:
645    CSSParser* m_parser;
646};
647
648class CSSParser::SourceDataHandler {
649public:
650    virtual void startRuleHeader(CSSRuleSourceData::Type, unsigned offset) = 0;
651    virtual void endRuleHeader(unsigned offset) = 0;
652    virtual void startSelector(unsigned offset) = 0;
653    virtual void endSelector(unsigned offset) = 0;
654    virtual void startRuleBody(unsigned offset) = 0;
655    virtual void endRuleBody(unsigned offset, bool error) = 0;
656    virtual void startEndUnknownRule() = 0;
657    virtual void startProperty(unsigned offset) = 0;
658    virtual void endProperty(bool isImportant, bool isParsed, unsigned offset, CSSParser::ErrorType) = 0;
659    virtual void startComment(unsigned offset) = 0;
660    virtual void endComment(unsigned offset) = 0;
661};
662
663String quoteCSSString(const String&);
664String quoteCSSStringIfNeeded(const String&);
665String quoteCSSURLIfNeeded(const String&);
666
667bool isValidNthToken(const CSSParserString&);
668
669inline int cssyylex(void* yylval, CSSParser* parser)
670{
671    return parser->m_tokenizer.lex(yylval);
672}
673
674} // namespace WebCore
675
676#endif // CSSParser_h
677