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 "CSSSelector.h"
31 #include "MediaQuery.h"
32 #if ENABLE(CSS_FILTERS)
33 #include "WebKitCSSFilterValue.h"
35 #include <wtf/HashMap.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/Vector.h>
38 #include <wtf/text/AtomicString.h>
42 class CSSBorderImageSliceValue;
43 class CSSMutableStyleDeclaration;
44 class CSSPrimitiveValue;
45 class CSSPrimitiveValueCache;
49 class CSSSelectorList;
56 class WebKitCSSKeyframeRule;
57 class WebKitCSSKeyframesRule;
62 CSSParser(bool strictParsing = true);
65 void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRangeMap* ruleRangeMap = 0);
66 PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
67 PassRefPtr<WebKitCSSKeyframeRule> parseKeyframeRule(CSSStyleSheet*, const String&);
68 static bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important, bool strict);
69 static bool parseColor(RGBA32& color, const String&, bool strict = false);
70 static bool parseSystemColor(RGBA32& color, const String&, Document*);
71 PassRefPtr<CSSPrimitiveValue> parseValidPrimitive(int propId, CSSParserValue*);
72 bool parseColor(CSSMutableStyleDeclaration*, const String&);
73 bool parseDeclaration(CSSMutableStyleDeclaration*, const String&, RefPtr<CSSStyleSourceData>* = 0, CSSStyleSheet* contextStyleSheet = 0);
74 bool parseMediaQuery(MediaList*, const String&);
76 Document* findDocument() const;
78 CSSPrimitiveValueCache* primitiveValueCache() const { return m_primitiveValueCache.get(); }
80 void addProperty(int propId, PassRefPtr<CSSValue>, bool important, bool implicit = false);
81 void rollbackLastProperties(int num);
82 bool hasProperties() const { return m_numParsedProperties > 0; }
84 bool parseValue(int propId, bool important);
85 bool parseShorthand(int propId, const int* properties, int numProperties, bool important);
86 bool parse4Values(int propId, const int* properties, bool important);
87 bool parseContent(int propId, bool important);
88 bool parseQuotes(int propId, bool important);
90 PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
92 PassRefPtr<CSSValue> parseBackgroundColor();
94 bool parseFillImage(CSSParserValueList*, RefPtr<CSSValue>&);
96 enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
97 PassRefPtr<CSSValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag);
98 PassRefPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
99 PassRefPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
100 void parseFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
102 void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
103 PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
105 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
106 bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important);
108 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
110 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
112 PassRefPtr<CSSValue> parseAnimationDelay();
113 PassRefPtr<CSSValue> parseAnimationDirection();
114 PassRefPtr<CSSValue> parseAnimationDuration();
115 PassRefPtr<CSSValue> parseAnimationFillMode();
116 PassRefPtr<CSSValue> parseAnimationIterationCount();
117 PassRefPtr<CSSValue> parseAnimationName();
118 PassRefPtr<CSSValue> parseAnimationPlayState();
119 PassRefPtr<CSSValue> parseAnimationProperty();
120 PassRefPtr<CSSValue> parseAnimationTimingFunction();
122 bool parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
123 bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
124 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
125 bool parseTransitionShorthand(bool important);
126 bool parseAnimationShorthand(bool important);
128 #if ENABLE(CSS_GRID_LAYOUT)
129 bool parseGridTrackList(int propId, bool important);
132 bool parseDashboardRegions(int propId, bool important);
134 bool parseShape(int propId, bool important);
136 bool parseWrapShape(bool shapeInside, bool important);
137 PassRefPtr<CSSWrapShape> parseWrapShapeRect(CSSParserValueList* args);
138 PassRefPtr<CSSWrapShape> parseWrapShapeCircle(CSSParserValueList* args);
139 PassRefPtr<CSSWrapShape> parseWrapShapeEllipse(CSSParserValueList* args);
140 PassRefPtr<CSSWrapShape> parseWrapShapePolygon(CSSParserValueList* args);
142 bool parseFont(bool important);
143 PassRefPtr<CSSValueList> parseFontFamily();
145 bool parseCounter(int propId, int defaultValue, bool important);
146 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
148 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
149 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
150 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
151 bool parseColorFromValue(CSSParserValue*, RGBA32&);
152 void parseSelector(const String&, Document* doc, CSSSelectorList&);
154 static bool parseColor(const String&, RGBA32& rgb, bool strict);
156 bool parseFontStyle(bool important);
157 bool parseFontVariant(bool important);
158 bool parseFontWeight(bool important);
159 bool parseFontFaceSrc();
160 bool parseFontFaceUnicodeRange();
163 bool parseSVGValue(int propId, bool important);
164 PassRefPtr<CSSValue> parseSVGPaint();
165 PassRefPtr<CSSValue> parseSVGColor();
166 PassRefPtr<CSSValue> parseSVGStrokeDasharray();
169 // CSS3 Parsing Routines (for properties specific to CSS3)
170 PassRefPtr<CSSValueList> parseShadow(CSSParserValueList*, int propId);
171 bool parseBorderImage(int propId, RefPtr<CSSValue>&);
172 bool parseBorderImageRepeat(RefPtr<CSSValue>&);
173 bool parseBorderImageSlice(int propId, RefPtr<CSSBorderImageSliceValue>&);
174 bool parseBorderImageWidth(RefPtr<CSSPrimitiveValue>&);
175 bool parseBorderImageOutset(RefPtr<CSSPrimitiveValue>&);
176 bool parseBorderRadius(int propId, bool important);
178 bool parseAspectRatio(bool important);
180 bool parseReflect(int propId, bool important);
182 bool parseFlex(int propId, bool important);
185 bool parseCanvas(CSSParserValueList*, RefPtr<CSSValue>&);
187 bool parseDeprecatedGradient(CSSParserValueList*, RefPtr<CSSValue>&);
188 bool parseLinearGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
189 bool parseRadialGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
190 bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
192 bool parseCrossfade(CSSParserValueList*, RefPtr<CSSValue>&);
194 #if ENABLE(CSS_FILTERS)
195 bool isValidFilterArgument(CSSParserValue* argument, WebKitCSSFilterValue::FilterOperationType&, unsigned argumentCount);
196 PassRefPtr<CSSValueList> parseFilter();
197 #if ENABLE(CSS_SHADERS)
198 PassRefPtr<WebKitCSSFilterValue> parseCustomFilter(CSSParserValue*);
202 PassRefPtr<CSSValueList> parseTransform();
203 bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
204 bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
206 bool parseTextEmphasisStyle(bool important);
208 bool parseLineBoxContain(bool important);
210 bool parseFontFeatureTag(CSSValueList*);
211 bool parseFontFeatureSettings(bool important);
213 bool parseFlowThread(int propId, bool important);
214 bool parseRegionThread(int propId, bool important);
218 CSSParserSelector* createFloatingSelector();
219 PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
221 Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
222 PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
224 CSSParserValueList* createFloatingValueList();
225 CSSParserValueList* sinkFloatingValueList(CSSParserValueList*);
227 CSSParserFunction* createFloatingFunction();
228 CSSParserFunction* sinkFloatingFunction(CSSParserFunction*);
230 CSSParserValue& sinkFloatingValue(CSSParserValue&);
232 MediaList* createMediaList();
233 CSSRule* createCharsetRule(const CSSParserString&);
234 CSSRule* createImportRule(const CSSParserString&, MediaList*);
235 WebKitCSSKeyframeRule* createKeyframeRule(CSSParserValueList*);
236 WebKitCSSKeyframesRule* createKeyframesRule();
237 CSSRule* createMediaRule(MediaList*, CSSRuleList*);
238 CSSRuleList* createRuleList();
239 CSSRule* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
240 CSSRule* createFontFaceRule();
241 CSSRule* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
242 CSSRule* createRegionStylingRule(Vector<OwnPtr<CSSParserSelector> >* regionSelector, CSSRuleList* rules);
243 CSSRule* createMarginAtRule(CSSSelector::MarginBoxType marginBox);
244 void startDeclarationsForMarginBox();
245 void endDeclarationsForMarginBox();
247 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
248 PassOwnPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
249 Vector<OwnPtr<MediaQueryExp> >* createFloatingMediaQueryExpList();
250 PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > sinkFloatingMediaQueryExpList(Vector<OwnPtr<MediaQueryExp> >*);
251 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
252 MediaQuery* createFloatingMediaQuery(PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
253 PassOwnPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
255 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
256 void updateSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*);
257 CSSParserSelector* updateSpecifiers(CSSParserSelector*, CSSParserSelector*);
259 void invalidBlockHit();
261 Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
263 void setReusableRegionSelectorVector(Vector<OwnPtr<CSSParserSelector> >* selectors);
264 Vector<OwnPtr<CSSParserSelector> >* reusableRegionSelectorVector() { return &m_reusableRegionSelectorVector; }
266 void updateLastSelectorLineAndPosition();
267 void updateLastMediaLine(MediaList*);
269 void clearProperties();
274 CSSStyleSheet* m_styleSheet;
275 RefPtr<CSSRule> m_rule;
276 RefPtr<WebKitCSSKeyframeRule> m_keyframe;
277 OwnPtr<MediaQuery> m_mediaQuery;
278 CSSParserValueList* m_valueList;
279 CSSProperty** m_parsedProperties;
280 CSSSelectorList* m_selectorListForParseSelector;
282 RefPtr<CSSPrimitiveValueCache> m_primitiveValueCache;
283 unsigned m_numParsedProperties;
284 unsigned m_maxParsedProperties;
285 unsigned m_numParsedPropertiesBeforeMarginBox;
287 int m_inParseShorthand;
288 int m_currentShorthand;
289 bool m_implicitShorthand;
291 bool m_hasFontFaceOnlyValues;
292 bool m_hadSyntacticallyValidCSSRule;
294 AtomicString m_defaultNamespace;
296 // tokenizer methods and data
297 bool m_inStyleRuleOrDeclaration;
298 SourceRange m_selectorListRange;
299 SourceRange m_ruleBodyRange;
300 SourceRange m_propertyRange;
301 StyleRuleRangeMap* m_ruleRangeMap;
302 RefPtr<CSSRuleSourceData> m_currentRuleData;
303 void markSelectorListStart();
304 void markSelectorListEnd();
305 void markRuleBodyStart();
306 void markRuleBodyEnd();
307 void markPropertyStart();
308 void markPropertyEnd(bool isImportantFound, bool isPropertyParsed);
309 void resetSelectorListMarks() { m_selectorListRange.start = m_selectorListRange.end = 0; }
310 void resetRuleBodyMarks() { m_ruleBodyRange.start = m_ruleBodyRange.end = 0; }
311 void resetPropertyMarks() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; }
312 int lex(void* yylval);
313 int token() { return yyTok; }
314 UChar* text(int* length);
319 void setStyleSheet(CSSStyleSheet*);
321 void recheckAtKeyword(const UChar* str, int len);
323 void setupParser(const char* prefix, const String&, const char* suffix);
325 bool inShorthand() const { return m_inParseShorthand; }
327 void checkForOrphanedUnits();
329 void deleteFontFaceOnlyValues();
331 bool isGeneratedImageValue(CSSParserValue*) const;
332 bool parseGeneratedImage(CSSParserValueList*, RefPtr<CSSValue>&);
334 bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important);
336 enum SizeParameterType {
344 bool parsePage(int propId, bool important);
345 bool parseSize(int propId, bool important);
346 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
352 int yy_last_accepting_state;
353 UChar* yy_last_accepting_cpos;
358 int m_lastSelectorLineNumber;
360 bool m_allowImportRules;
361 bool m_allowNamespaceDeclarations;
363 Vector<RefPtr<CSSRule> > m_parsedRules;
364 Vector<RefPtr<MediaList> > m_parsedMediaLists;
365 Vector<RefPtr<CSSRuleList> > m_parsedRuleLists;
366 HashSet<CSSParserSelector*> m_floatingSelectors;
367 HashSet<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
368 HashSet<CSSParserValueList*> m_floatingValueLists;
369 HashSet<CSSParserFunction*> m_floatingFunctions;
371 OwnPtr<MediaQuery> m_floatingMediaQuery;
372 OwnPtr<MediaQueryExp> m_floatingMediaQueryExp;
373 OwnPtr<Vector<OwnPtr<MediaQueryExp> > > m_floatingMediaQueryExpList;
375 Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
376 Vector<OwnPtr<CSSParserSelector> > m_reusableRegionSelectorVector;
378 // defines units allowed for a certain property, used in parseUnit
382 FNumber = 0x0002, // Real Numbers
392 friend inline Units operator|(Units a, Units b)
394 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
397 static bool validUnit(CSSParserValue*, Units, bool strict);
399 bool parseBorderImageQuad(Units, RefPtr<CSSPrimitiveValue>&);
401 PassRefPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
402 PassRefPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
404 friend class TransformOperationInfo;
405 #if ENABLE(CSS_FILTERS)
406 friend class FilterOperationInfo;
410 int cssPropertyID(const CSSParserString&);
411 int cssPropertyID(const String&);
412 int cssValueKeywordID(const CSSParserString&);
414 class ShorthandScope {
415 WTF_MAKE_FAST_ALLOCATED;
417 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
419 if (!(m_parser->m_inParseShorthand++))
420 m_parser->m_currentShorthand = propId;
424 if (!(--m_parser->m_inParseShorthand))
425 m_parser->m_currentShorthand = 0;
432 String quoteCSSString(const String&);
433 String quoteCSSStringIfNeeded(const String&);
434 String quoteCSSURLIfNeeded(const String&);
436 bool isValidNthToken(const CSSParserString&);
437 } // namespace WebCore
439 #endif // CSSParser_h