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>
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.
26 #include "AtomicString.h"
28 #include "CSSParserValues.h"
29 #include "CSSSelectorList.h"
30 #include "MediaQuery.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/Vector.h>
37 class CSSMutableStyleDeclaration;
38 class CSSPrimitiveValue;
47 class CSSVariablesDeclaration;
53 class WebKitCSSKeyframeRule;
54 class WebKitCSSKeyframesRule;
58 typedef HashMap<CSSStyleRule*, std::pair<unsigned, unsigned> > StyleRuleRanges;
60 CSSParser(bool strictParsing = true);
63 void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRanges* ruleRangeMap = 0);
64 PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
65 PassRefPtr<CSSRule> parseKeyframeRule(CSSStyleSheet*, const String&);
66 bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important);
67 static bool parseColor(RGBA32& color, const String&, bool strict = false);
68 bool parseColor(CSSMutableStyleDeclaration*, const String&);
69 bool parseDeclaration(CSSMutableStyleDeclaration*, const String&);
70 bool parseMediaQuery(MediaList*, const String&);
72 Document* document() const;
74 void addProperty(int propId, PassRefPtr<CSSValue>, bool important);
75 void rollbackLastProperties(int num);
76 bool hasProperties() const { return m_numParsedProperties > 0; }
78 bool parseValue(int propId, bool important);
79 bool parseShorthand(int propId, const int* properties, int numProperties, bool important);
80 bool parse4Values(int propId, const int* properties, bool important);
81 bool parseContent(int propId, bool important);
83 PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
85 PassRefPtr<CSSValue> parseBackgroundColor();
87 bool parseFillImage(RefPtr<CSSValue>&);
88 PassRefPtr<CSSValue> parseFillPositionXY(bool& xFound, bool& yFound);
89 void parseFillPosition(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
90 void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
91 PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
93 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
94 bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important);
96 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
98 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
100 PassRefPtr<CSSValue> parseAnimationDelay();
101 PassRefPtr<CSSValue> parseAnimationDirection();
102 PassRefPtr<CSSValue> parseAnimationDuration();
103 PassRefPtr<CSSValue> parseAnimationFillMode();
104 PassRefPtr<CSSValue> parseAnimationIterationCount();
105 PassRefPtr<CSSValue> parseAnimationName();
106 PassRefPtr<CSSValue> parseAnimationPlayState();
107 PassRefPtr<CSSValue> parseAnimationProperty();
108 PassRefPtr<CSSValue> parseAnimationTimingFunction();
110 void parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
111 bool parseTimingFunctionValue(CSSParserValueList*& args, double& result);
112 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
113 bool parseTransitionShorthand(bool important);
114 bool parseAnimationShorthand(bool important);
116 bool parseDashboardRegions(int propId, bool important);
118 bool parseShape(int propId, bool important);
120 bool parseFont(bool important);
121 PassRefPtr<CSSValueList> parseFontFamily();
123 bool parseCounter(int propId, int defaultValue, bool important);
124 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
126 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
127 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
128 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
129 bool parseColorFromValue(CSSParserValue*, RGBA32&);
130 void parseSelector(const String&, Document* doc, CSSSelectorList&);
132 static bool parseColor(const String&, RGBA32& rgb, bool strict);
134 bool parseFontStyle(bool important);
135 bool parseFontVariant(bool important);
136 bool parseFontWeight(bool important);
137 bool parseFontFaceSrc();
138 bool parseFontFaceUnicodeRange();
141 bool parseSVGValue(int propId, bool important);
142 PassRefPtr<CSSValue> parseSVGPaint();
143 PassRefPtr<CSSValue> parseSVGColor();
144 PassRefPtr<CSSValue> parseSVGStrokeDasharray();
148 PassRefPtr<CSSValue> parseWCSSInputProperty();
151 // CSS3 Parsing Routines (for properties specific to CSS3)
152 bool parseShadow(int propId, bool important);
153 bool parseBorderImage(int propId, bool important, RefPtr<CSSValue>&);
154 bool parseBorderRadius(int propId, bool important);
156 bool parseReflect(int propId, bool important);
159 bool parseCanvas(RefPtr<CSSValue>&);
160 bool parseGradient(RefPtr<CSSValue>&);
162 PassRefPtr<CSSValueList> parseTransform();
163 bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
164 bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
165 bool parseVariable(CSSVariablesDeclaration*, const String& variableName, const String& variableValue);
166 void parsePropertyWithResolvedVariables(int propId, bool important, CSSMutableStyleDeclaration*, CSSParserValueList*);
170 CSSSelector* createFloatingSelector();
171 CSSSelector* sinkFloatingSelector(CSSSelector*);
173 CSSParserValueList* createFloatingValueList();
174 CSSParserValueList* sinkFloatingValueList(CSSParserValueList*);
176 CSSParserFunction* createFloatingFunction();
177 CSSParserFunction* sinkFloatingFunction(CSSParserFunction*);
179 CSSParserValue& sinkFloatingValue(CSSParserValue&);
181 MediaList* createMediaList();
182 CSSRule* createCharsetRule(const CSSParserString&);
183 CSSRule* createImportRule(const CSSParserString&, MediaList*);
184 WebKitCSSKeyframeRule* createKeyframeRule(CSSParserValueList*);
185 WebKitCSSKeyframesRule* createKeyframesRule();
186 CSSRule* createMediaRule(MediaList*, CSSRuleList*);
187 CSSRuleList* createRuleList();
188 CSSRule* createStyleRule(Vector<CSSSelector*>* selectors);
189 CSSRule* createFontFaceRule();
190 CSSRule* createVariablesRule(MediaList*, bool variablesKeyword);
191 CSSRule* createPageRule(CSSSelector* pageSelector);
192 CSSRule* createMarginAtRule(CSSSelector::MarginBoxType marginBox);
193 void startDeclarationsForMarginBox();
194 void endDeclarationsForMarginBox();
196 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
197 MediaQueryExp* sinkFloatingMediaQueryExp(MediaQueryExp*);
198 Vector<MediaQueryExp*>* createFloatingMediaQueryExpList();
199 Vector<MediaQueryExp*>* sinkFloatingMediaQueryExpList(Vector<MediaQueryExp*>*);
200 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, Vector<MediaQueryExp*>*);
201 MediaQuery* createFloatingMediaQuery(Vector<MediaQueryExp*>*);
202 MediaQuery* sinkFloatingMediaQuery(MediaQuery*);
204 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
206 bool addVariable(const CSSParserString&, CSSParserValueList*);
207 bool addVariableDeclarationBlock(const CSSParserString&);
208 bool checkForVariables(CSSParserValueList*);
209 void addUnresolvedProperty(int propId, bool important);
210 void invalidBlockHit();
212 Vector<CSSSelector*>* reusableSelectorVector() { return &m_reusableSelectorVector; }
214 void updateLastSelectorLineAndPosition();
216 void clearProperties();
221 CSSStyleSheet* m_styleSheet;
222 RefPtr<CSSRule> m_rule;
223 RefPtr<CSSRule> m_keyframe;
224 MediaQuery* m_mediaQuery;
225 CSSParserValueList* m_valueList;
226 CSSProperty** m_parsedProperties;
227 CSSSelectorList* m_selectorListForParseSelector;
228 unsigned m_numParsedProperties;
229 unsigned m_maxParsedProperties;
230 unsigned m_numParsedPropertiesBeforeMarginBox;
232 int m_inParseShorthand;
233 int m_currentShorthand;
234 bool m_implicitShorthand;
236 bool m_hasFontFaceOnlyValues;
237 bool m_hadSyntacticallyValidCSSRule;
239 Vector<String> m_variableNames;
240 Vector<RefPtr<CSSValue> > m_variableValues;
242 AtomicString m_defaultNamespace;
244 // tokenizer methods and data
245 unsigned m_ruleBodyStartOffset;
246 unsigned m_ruleBodyEndOffset;
247 StyleRuleRanges* m_ruleRanges;
248 void markRuleBodyStart();
249 void markRuleBodyEnd();
250 void resetRuleBodyMarks() { m_ruleBodyStartOffset = m_ruleBodyEndOffset = 0; }
251 int lex(void* yylval);
252 int token() { return yyTok; }
253 UChar* text(int* length);
258 void recheckAtKeyword(const UChar* str, int len);
260 void setupParser(const char* prefix, const String&, const char* suffix);
262 bool inShorthand() const { return m_inParseShorthand; }
264 void checkForOrphanedUnits();
266 void clearVariables();
268 void deleteFontFaceOnlyValues();
270 enum SizeParameterType {
278 bool parsePage(int propId, bool important);
279 bool parseSize(int propId, bool important);
280 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
286 int yy_last_accepting_state;
287 UChar* yy_last_accepting_cpos;
292 int m_lastSelectorLineNumber;
294 bool m_allowImportRules;
295 bool m_allowVariablesRules;
296 bool m_allowNamespaceDeclarations;
298 Vector<RefPtr<StyleBase> > m_parsedStyleObjects;
299 Vector<RefPtr<CSSRuleList> > m_parsedRuleLists;
300 HashSet<CSSSelector*> m_floatingSelectors;
301 HashSet<CSSParserValueList*> m_floatingValueLists;
302 HashSet<CSSParserFunction*> m_floatingFunctions;
304 MediaQuery* m_floatingMediaQuery;
305 MediaQueryExp* m_floatingMediaQueryExp;
306 Vector<MediaQueryExp*>* m_floatingMediaQueryExpList;
308 Vector<CSSSelector*> m_reusableSelectorVector;
310 // defines units allowed for a certain property, used in parseUnit
314 FNumber = 0x0002, // Real Numbers
324 friend inline Units operator|(Units a, Units b)
326 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
329 static bool validUnit(CSSParserValue*, Units, bool strict);
331 friend class TransformOperationInfo;
334 int cssPropertyID(const CSSParserString&);
335 int cssPropertyID(const String&);
336 int cssValueKeywordID(const CSSParserString&);
338 class ShorthandScope : public FastAllocBase {
340 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
342 if (!(m_parser->m_inParseShorthand++))
343 m_parser->m_currentShorthand = propId;
347 if (!(--m_parser->m_inParseShorthand))
348 m_parser->m_currentShorthand = 0;
355 String quoteCSSString(const String&);
356 String quoteCSSStringIfNeeded(const String&);
357 String quoteCSSURLIfNeeded(const String&);
359 } // namespace WebCore
361 #endif // CSSParser_h