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.
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 "CSSGradientValue.h"
27 #include "CSSParserValues.h"
28 #include "CSSPropertySourceData.h"
29 #include "CSSSelectorList.h"
31 #include "MediaQuery.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/Vector.h>
35 #include <wtf/text/AtomicString.h>
39 class CSSMutableStyleDeclaration;
40 class CSSPrimitiveValue;
41 class CSSPrimitiveValueCache;
55 class WebKitCSSKeyframeRule;
56 class WebKitCSSKeyframesRule;
60 CSSParser(bool strictParsing = true);
63 void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRangeMap* ruleRangeMap = 0);
64 PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
65 PassRefPtr<CSSRule> parseKeyframeRule(CSSStyleSheet*, const String&);
66 static bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important, bool strict);
67 static bool parseColor(RGBA32& color, const String&, bool strict = false);
68 static bool parseSystemColor(RGBA32& color, const String&, Document*);
69 bool parseColor(CSSMutableStyleDeclaration*, const String&);
70 bool parseDeclaration(CSSMutableStyleDeclaration*, const String&, RefPtr<CSSStyleSourceData>* styleSourceData = 0);
71 bool parseMediaQuery(MediaList*, const String&);
73 Document* document() const;
75 CSSPrimitiveValueCache* primitiveValueCache() const { return m_primitiveValueCache.get(); }
77 void addProperty(int propId, PassRefPtr<CSSValue>, bool important);
78 void rollbackLastProperties(int num);
79 bool hasProperties() const { return m_numParsedProperties > 0; }
81 bool parseValue(int propId, bool important);
82 bool parseShorthand(int propId, const int* properties, int numProperties, bool important);
83 bool parse4Values(int propId, const int* properties, bool important);
84 bool parseContent(int propId, bool important);
85 bool parseQuotes(int propId, bool important);
87 PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
89 PassRefPtr<CSSValue> parseBackgroundColor();
91 bool parseFillImage(RefPtr<CSSValue>&);
93 enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
94 PassRefPtr<CSSValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag);
95 PassRefPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
96 PassRefPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
97 void parseFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
99 void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
100 PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
102 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
103 bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important);
105 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
107 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
109 PassRefPtr<CSSValue> parseAnimationDelay();
110 PassRefPtr<CSSValue> parseAnimationDirection();
111 PassRefPtr<CSSValue> parseAnimationDuration();
112 PassRefPtr<CSSValue> parseAnimationFillMode();
113 PassRefPtr<CSSValue> parseAnimationIterationCount();
114 PassRefPtr<CSSValue> parseAnimationName();
115 PassRefPtr<CSSValue> parseAnimationPlayState();
116 PassRefPtr<CSSValue> parseAnimationProperty();
117 PassRefPtr<CSSValue> parseAnimationTimingFunction();
119 bool parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
120 bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
121 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
122 bool parseTransitionShorthand(bool important);
123 bool parseAnimationShorthand(bool important);
125 bool parseDashboardRegions(int propId, bool important);
127 bool parseShape(int propId, bool important);
129 bool parseFont(bool important);
130 PassRefPtr<CSSValueList> parseFontFamily();
132 bool parseCounter(int propId, int defaultValue, bool important);
133 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
135 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
136 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
137 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
138 bool parseColorFromValue(CSSParserValue*, RGBA32&);
139 void parseSelector(const String&, Document* doc, CSSSelectorList&);
141 static bool parseColor(const String&, RGBA32& rgb, bool strict);
143 bool parseFontStyle(bool important);
144 bool parseFontVariant(bool important);
145 bool parseFontWeight(bool important);
146 bool parseFontFaceSrc();
147 bool parseFontFaceUnicodeRange();
150 bool parseSVGValue(int propId, bool important);
151 PassRefPtr<CSSValue> parseSVGPaint();
152 PassRefPtr<CSSValue> parseSVGColor();
153 PassRefPtr<CSSValue> parseSVGStrokeDasharray();
157 PassRefPtr<CSSValue> parseWCSSInputProperty();
160 // CSS3 Parsing Routines (for properties specific to CSS3)
161 bool parseShadow(int propId, bool important);
162 bool parseBorderImage(int propId, bool important, RefPtr<CSSValue>&);
163 bool parseBorderRadius(int propId, bool important);
165 bool parseReflect(int propId, bool important);
168 bool parseCanvas(RefPtr<CSSValue>&);
170 bool parseDeprecatedGradient(RefPtr<CSSValue>&);
171 bool parseLinearGradient(RefPtr<CSSValue>&, CSSGradientRepeat repeating);
172 bool parseRadialGradient(RefPtr<CSSValue>&, CSSGradientRepeat repeating);
173 bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
175 PassRefPtr<CSSValueList> parseTransform();
176 bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
177 bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
179 bool parseTextEmphasisStyle(bool important);
181 bool parseLineBoxContain(bool important);
185 CSSParserSelector* createFloatingSelector();
186 PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
188 Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
189 PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
191 CSSParserValueList* createFloatingValueList();
192 CSSParserValueList* sinkFloatingValueList(CSSParserValueList*);
194 CSSParserFunction* createFloatingFunction();
195 CSSParserFunction* sinkFloatingFunction(CSSParserFunction*);
197 CSSParserValue& sinkFloatingValue(CSSParserValue&);
199 MediaList* createMediaList();
200 CSSRule* createCharsetRule(const CSSParserString&);
201 CSSRule* createImportRule(const CSSParserString&, MediaList*);
202 WebKitCSSKeyframeRule* createKeyframeRule(CSSParserValueList*);
203 WebKitCSSKeyframesRule* createKeyframesRule();
204 CSSRule* createMediaRule(MediaList*, CSSRuleList*);
205 CSSRuleList* createRuleList();
206 CSSRule* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
207 CSSRule* createFontFaceRule();
208 CSSRule* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
209 CSSRule* createMarginAtRule(CSSSelector::MarginBoxType marginBox);
210 void startDeclarationsForMarginBox();
211 void endDeclarationsForMarginBox();
213 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
214 PassOwnPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
215 Vector<OwnPtr<MediaQueryExp> >* createFloatingMediaQueryExpList();
216 PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > sinkFloatingMediaQueryExpList(Vector<OwnPtr<MediaQueryExp> >*);
217 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
218 MediaQuery* createFloatingMediaQuery(PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
219 PassOwnPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
221 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
222 void updateSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*);
224 void invalidBlockHit();
226 Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
228 void updateLastSelectorLineAndPosition();
230 void clearProperties();
235 CSSStyleSheet* m_styleSheet;
236 RefPtr<CSSRule> m_rule;
237 RefPtr<CSSRule> m_keyframe;
238 OwnPtr<MediaQuery> m_mediaQuery;
239 CSSParserValueList* m_valueList;
240 CSSProperty** m_parsedProperties;
241 CSSSelectorList* m_selectorListForParseSelector;
242 RefPtr<CSSPrimitiveValueCache> m_primitiveValueCache;
243 unsigned m_numParsedProperties;
244 unsigned m_maxParsedProperties;
245 unsigned m_numParsedPropertiesBeforeMarginBox;
247 int m_inParseShorthand;
248 int m_currentShorthand;
249 bool m_implicitShorthand;
251 bool m_hasFontFaceOnlyValues;
252 bool m_hadSyntacticallyValidCSSRule;
254 AtomicString m_defaultNamespace;
256 // tokenizer methods and data
257 bool m_inStyleRuleOrDeclaration;
258 SourceRange m_selectorListRange;
259 SourceRange m_ruleBodyRange;
260 SourceRange m_propertyRange;
261 StyleRuleRangeMap* m_ruleRangeMap;
262 RefPtr<CSSRuleSourceData> m_currentRuleData;
263 void markSelectorListStart();
264 void markSelectorListEnd();
265 void markRuleBodyStart();
266 void markRuleBodyEnd();
267 void markPropertyStart();
268 void markPropertyEnd(bool isImportantFound, bool isPropertyParsed);
269 void resetSelectorListMarks() { m_selectorListRange.start = m_selectorListRange.end = 0; }
270 void resetRuleBodyMarks() { m_ruleBodyRange.start = m_ruleBodyRange.end = 0; }
271 void resetPropertyMarks() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; }
272 int lex(void* yylval);
273 int token() { return yyTok; }
274 UChar* text(int* length);
279 void setStyleSheet(CSSStyleSheet*);
281 void recheckAtKeyword(const UChar* str, int len);
283 void setupParser(const char* prefix, const String&, const char* suffix);
285 bool inShorthand() const { return m_inParseShorthand; }
287 void checkForOrphanedUnits();
289 void deleteFontFaceOnlyValues();
291 bool isGeneratedImageValue(CSSParserValue*) const;
292 bool parseGeneratedImage(RefPtr<CSSValue>&);
294 bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important);
296 enum SizeParameterType {
304 bool parsePage(int propId, bool important);
305 bool parseSize(int propId, bool important);
306 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
312 int yy_last_accepting_state;
313 UChar* yy_last_accepting_cpos;
318 int m_lastSelectorLineNumber;
320 bool m_allowImportRules;
321 bool m_allowNamespaceDeclarations;
323 Vector<RefPtr<StyleBase> > m_parsedStyleObjects;
324 Vector<RefPtr<CSSRuleList> > m_parsedRuleLists;
325 HashSet<CSSParserSelector*> m_floatingSelectors;
326 HashSet<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
327 HashSet<CSSParserValueList*> m_floatingValueLists;
328 HashSet<CSSParserFunction*> m_floatingFunctions;
330 OwnPtr<MediaQuery> m_floatingMediaQuery;
331 OwnPtr<MediaQueryExp> m_floatingMediaQueryExp;
332 OwnPtr<Vector<OwnPtr<MediaQueryExp> > > m_floatingMediaQueryExpList;
334 Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
336 // defines units allowed for a certain property, used in parseUnit
340 FNumber = 0x0002, // Real Numbers
350 friend inline Units operator|(Units a, Units b)
352 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
355 static bool validUnit(CSSParserValue*, Units, bool strict);
357 friend class TransformOperationInfo;
360 int cssPropertyID(const CSSParserString&);
361 int cssPropertyID(const String&);
362 int cssValueKeywordID(const CSSParserString&);
364 class ShorthandScope {
365 WTF_MAKE_FAST_ALLOCATED;
367 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
369 if (!(m_parser->m_inParseShorthand++))
370 m_parser->m_currentShorthand = propId;
374 if (!(--m_parser->m_inParseShorthand))
375 m_parser->m_currentShorthand = 0;
382 String quoteCSSString(const String&);
383 String quoteCSSStringIfNeeded(const String&);
384 String quoteCSSURLIfNeeded(const String&);
386 bool isValidNthToken(const CSSParserString&);
387 } // namespace WebCore
389 #endif // CSSParser_h