2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "AtomicString.h"
27 #include "CSSParserValues.h"
28 #include "CSSSelectorList.h"
29 #include "MediaQuery.h"
30 #include <wtf/HashSet.h>
31 #include <wtf/Vector.h>
35 class CSSMutableStyleDeclaration
;
36 class CSSPrimitiveValue
;
44 class CSSVariablesDeclaration
;
50 class WebKitCSSKeyframeRule
;
51 class WebKitCSSKeyframesRule
;
55 CSSParser(bool strictParsing
= true);
58 void parseSheet(CSSStyleSheet
*, const String
&);
59 PassRefPtr
<CSSRule
> parseRule(CSSStyleSheet
*, const String
&);
60 PassRefPtr
<CSSRule
> parseKeyframeRule(CSSStyleSheet
*, const String
&);
61 bool parseValue(CSSMutableStyleDeclaration
*, int propId
, const String
&, bool important
);
62 static bool parseColor(RGBA32
& color
, const String
&, bool strict
= false);
63 bool parseColor(CSSMutableStyleDeclaration
*, const String
&);
64 bool parseDeclaration(CSSMutableStyleDeclaration
*, const String
&);
65 bool parseMediaQuery(MediaList
*, const String
&);
67 Document
* document() const;
69 void addProperty(int propId
, PassRefPtr
<CSSValue
>, bool important
);
70 void rollbackLastProperties(int num
);
71 bool hasProperties() const { return m_numParsedProperties
> 0; }
73 bool parseValue(int propId
, bool important
);
74 bool parseShorthand(int propId
, const int* properties
, int numProperties
, bool important
);
75 bool parse4Values(int propId
, const int* properties
, bool important
);
76 bool parseContent(int propId
, bool important
);
78 PassRefPtr
<CSSValue
> parseBackgroundColor();
80 bool parseFillImage(RefPtr
<CSSValue
>&);
81 PassRefPtr
<CSSValue
> parseFillPositionXY(bool& xFound
, bool& yFound
);
82 void parseFillPosition(RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&);
83 PassRefPtr
<CSSValue
> parseFillSize();
85 bool parseFillProperty(int propId
, int& propId1
, int& propId2
, RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&);
86 bool parseFillShorthand(int propId
, const int* properties
, int numProperties
, bool important
);
88 void addFillValue(RefPtr
<CSSValue
>& lval
, PassRefPtr
<CSSValue
> rval
);
90 void addAnimationValue(RefPtr
<CSSValue
>& lval
, PassRefPtr
<CSSValue
> rval
);
92 PassRefPtr
<CSSValue
> parseAnimationDelay();
93 PassRefPtr
<CSSValue
> parseAnimationDirection();
94 PassRefPtr
<CSSValue
> parseAnimationDuration();
95 PassRefPtr
<CSSValue
> parseAnimationIterationCount();
96 PassRefPtr
<CSSValue
> parseAnimationName();
97 PassRefPtr
<CSSValue
> parseAnimationPlayState();
98 PassRefPtr
<CSSValue
> parseAnimationProperty();
99 PassRefPtr
<CSSValue
> parseAnimationTimingFunction();
101 void parseTransformOriginShorthand(RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&);
102 bool parseTimingFunctionValue(CSSParserValueList
*& args
, double& result
);
103 bool parseAnimationProperty(int propId
, RefPtr
<CSSValue
>&);
104 bool parseTransitionShorthand(bool important
);
105 bool parseAnimationShorthand(bool important
);
107 bool parseDashboardRegions(int propId
, bool important
);
109 bool parseShape(int propId
, bool important
);
111 bool parseFont(bool important
);
112 PassRefPtr
<CSSValueList
> parseFontFamily();
114 bool parseCounter(int propId
, int defaultValue
, bool important
);
115 PassRefPtr
<CSSValue
> parseCounterContent(CSSParserValueList
* args
, bool counters
);
117 bool parseColorParameters(CSSParserValue
*, int* colorValues
, bool parseAlpha
);
118 bool parseHSLParameters(CSSParserValue
*, double* colorValues
, bool parseAlpha
);
119 PassRefPtr
<CSSPrimitiveValue
> parseColor(CSSParserValue
* = 0);
120 bool parseColorFromValue(CSSParserValue
*, RGBA32
&, bool = false);
121 void parseSelector(const String
&, Document
* doc
, CSSSelectorList
&);
123 static bool parseColor(const String
&, RGBA32
& rgb
, bool strict
);
125 bool parseFontStyle(bool important
);
126 bool parseFontVariant(bool important
);
127 bool parseFontWeight(bool important
);
128 bool parseFontFaceSrc();
129 bool parseFontFaceUnicodeRange();
132 bool parseSVGValue(int propId
, bool important
);
133 PassRefPtr
<CSSValue
> parseSVGPaint();
134 PassRefPtr
<CSSValue
> parseSVGColor();
135 PassRefPtr
<CSSValue
> parseSVGStrokeDasharray();
138 // CSS3 Parsing Routines (for properties specific to CSS3)
139 bool parseShadow(int propId
, bool important
);
140 bool parseBorderImage(int propId
, bool important
, RefPtr
<CSSValue
>&);
142 bool parseReflect(int propId
, bool important
);
145 bool parseCanvas(RefPtr
<CSSValue
>&);
146 bool parseGradient(RefPtr
<CSSValue
>&);
148 PassRefPtr
<CSSValueList
> parseTransform();
149 bool parseTransformOrigin(int propId
, int& propId1
, int& propId2
, int& propId3
, RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&);
150 bool parsePerspectiveOrigin(int propId
, int& propId1
, int& propId
, RefPtr
<CSSValue
>&, RefPtr
<CSSValue
>&);
151 bool parseVariable(CSSVariablesDeclaration
*, const String
& variableName
, const String
& variableValue
);
152 void parsePropertyWithResolvedVariables(int propId
, bool important
, CSSMutableStyleDeclaration
*, CSSParserValueList
*);
156 CSSSelector
* createFloatingSelector();
157 CSSSelector
* sinkFloatingSelector(CSSSelector
*);
159 CSSParserValueList
* createFloatingValueList();
160 CSSParserValueList
* sinkFloatingValueList(CSSParserValueList
*);
162 CSSParserFunction
* createFloatingFunction();
163 CSSParserFunction
* sinkFloatingFunction(CSSParserFunction
*);
165 CSSParserValue
& sinkFloatingValue(CSSParserValue
&);
167 MediaList
* createMediaList();
168 CSSRule
* createCharsetRule(const CSSParserString
&);
169 CSSRule
* createImportRule(const CSSParserString
&, MediaList
*);
170 WebKitCSSKeyframeRule
* createKeyframeRule(CSSParserValueList
*);
171 WebKitCSSKeyframesRule
* createKeyframesRule();
172 CSSRule
* createMediaRule(MediaList
*, CSSRuleList
*);
173 CSSRuleList
* createRuleList();
174 CSSRule
* createStyleRule(Vector
<CSSSelector
*>* selectors
);
175 CSSRule
* createFontFaceRule();
176 CSSRule
* createVariablesRule(MediaList
*, bool variablesKeyword
);
178 MediaQueryExp
* createFloatingMediaQueryExp(const AtomicString
&, CSSParserValueList
*);
179 MediaQueryExp
* sinkFloatingMediaQueryExp(MediaQueryExp
*);
180 Vector
<MediaQueryExp
*>* createFloatingMediaQueryExpList();
181 Vector
<MediaQueryExp
*>* sinkFloatingMediaQueryExpList(Vector
<MediaQueryExp
*>*);
182 MediaQuery
* createFloatingMediaQuery(MediaQuery::Restrictor
, const String
&, Vector
<MediaQueryExp
*>*);
183 MediaQuery
* createFloatingMediaQuery(Vector
<MediaQueryExp
*>*);
184 MediaQuery
* sinkFloatingMediaQuery(MediaQuery
*);
186 bool addVariable(const CSSParserString
&, CSSParserValueList
*);
187 bool addVariableDeclarationBlock(const CSSParserString
&);
188 bool checkForVariables(CSSParserValueList
*);
189 void addUnresolvedProperty(int propId
, bool important
);
191 Vector
<CSSSelector
*>* reusableSelectorVector() { return &m_reusableSelectorVector
; }
197 CSSStyleSheet
* m_styleSheet
;
198 RefPtr
<CSSRule
> m_rule
;
199 RefPtr
<CSSRule
> m_keyframe
;
200 MediaQuery
* m_mediaQuery
;
201 CSSParserValueList
* m_valueList
;
202 CSSProperty
** m_parsedProperties
;
203 CSSSelectorList
* m_selectorListForParseSelector
;
204 unsigned m_numParsedProperties
;
205 unsigned m_maxParsedProperties
;
207 int m_inParseShorthand
;
208 int m_currentShorthand
;
209 bool m_implicitShorthand
;
211 bool m_hasFontFaceOnlyValues
;
213 Vector
<String
> m_variableNames
;
214 Vector
<RefPtr
<CSSValue
> > m_variableValues
;
216 AtomicString m_defaultNamespace
;
218 // tokenizer methods and data
220 int lex(void* yylval
);
221 int token() { return yyTok
; }
222 UChar
* text(int* length
);
226 void clearProperties();
228 void setupParser(const char* prefix
, const String
&, const char* suffix
);
230 bool inShorthand() const { return m_inParseShorthand
; }
232 void checkForOrphanedUnits();
234 void clearVariables();
236 void deleteFontFaceOnlyValues();
242 int yy_last_accepting_state
;
243 UChar
* yy_last_accepting_cpos
;
248 Vector
<RefPtr
<StyleBase
> > m_parsedStyleObjects
;
249 Vector
<RefPtr
<CSSRuleList
> > m_parsedRuleLists
;
250 HashSet
<CSSSelector
*> m_floatingSelectors
;
251 HashSet
<CSSParserValueList
*> m_floatingValueLists
;
252 HashSet
<CSSParserFunction
*> m_floatingFunctions
;
254 MediaQuery
* m_floatingMediaQuery
;
255 MediaQueryExp
* m_floatingMediaQueryExp
;
256 Vector
<MediaQueryExp
*>* m_floatingMediaQueryExpList
;
258 Vector
<CSSSelector
*> m_reusableSelectorVector
;
260 // defines units allowed for a certain property, used in parseUnit
264 FNumber
= 0x0002, // Real Numbers
274 friend inline Units
operator|(Units a
, Units b
)
276 return static_cast<Units
>(static_cast<unsigned>(a
) | static_cast<unsigned>(b
));
279 static bool validUnit(CSSParserValue
*, Units
, bool strict
);
281 friend class TransformOperationInfo
;
284 int cssPropertyID(const CSSParserString
&);
285 int cssPropertyID(const String
&);
286 int cssValueKeywordID(const CSSParserString
&);
288 class ShorthandScope
{
290 ShorthandScope(CSSParser
* parser
, int propId
) : m_parser(parser
)
292 if (!(m_parser
->m_inParseShorthand
++))
293 m_parser
->m_currentShorthand
= propId
;
297 if (!(--m_parser
->m_inParseShorthand
))
298 m_parser
->m_currentShorthand
= 0;
305 } // namespace WebCore
307 #endif // CSSParser_h