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 "CSSCalculationValue.h"
27 #include "CSSGradientValue.h"
28 #include "CSSParserMode.h"
29 #include "CSSParserValues.h"
30 #include "CSSProperty.h"
31 #include "CSSPropertyNames.h"
32 #include "CSSPropertySourceData.h"
33 #include "CSSSelector.h"
35 #include "MediaQuery.h"
36 #include <wtf/HashMap.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/OwnArrayPtr.h>
39 #include <wtf/Vector.h>
40 #include <wtf/text/AtomicString.h>
42 #if ENABLE(CSS_FILTERS)
43 #include "WebKitCSSFilterValue.h"
48 class CSSBorderImageSliceValue;
49 class CSSPrimitiveValue;
52 class CSSSelectorList;
61 class StylePropertySet;
62 class StylePropertyShorthand;
64 class StyleRuleKeyframes;
70 CSSParser(CSSParserMode = CSSStrictMode);
74 void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRangeMap* ruleRangeMap = 0);
75 PassRefPtr<StyleRuleBase> parseRule(CSSStyleSheet*, const String&);
76 PassRefPtr<StyleKeyframe> parseKeyframeRule(CSSStyleSheet*, const String&);
77 static bool parseValue(StylePropertySet*, int propId, const String&, bool important, CSSParserMode, CSSStyleSheet* contextStyleSheet);
78 static bool parseColor(RGBA32& color, const String&, bool strict = false);
79 static bool parseSystemColor(RGBA32& color, const String&, Document*);
80 static PassRefPtr<CSSValueList> parseFontFaceValue(const AtomicString&, CSSStyleSheet* contextStyleSheet);
81 PassRefPtr<CSSPrimitiveValue> parseValidPrimitive(int propId, CSSParserValue*);
82 bool parseDeclaration(StylePropertySet*, const String&, RefPtr<CSSStyleSourceData>*, CSSStyleSheet* contextStyleSheet);
83 PassOwnPtr<MediaQuery> parseMediaQuery(const String&);
85 Document* findDocument() const;
87 CSSValuePool* cssValuePool() const { return m_cssValuePool.get(); }
89 void addProperty(int propId, PassRefPtr<CSSValue>, bool important, bool implicit = false);
90 void rollbackLastProperties(int num);
91 bool hasProperties() const { return !m_parsedProperties.isEmpty(); }
93 bool parseValue(int propId, bool important);
94 bool parseShorthand(int, const StylePropertyShorthand&, bool);
95 bool parse4Values(int propId, const CSSPropertyID* properties, bool important);
96 bool parseContent(int propId, bool important);
97 bool parseQuotes(int propId, bool important);
99 PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
101 PassRefPtr<CSSValue> parseBackgroundColor();
103 bool parseFillImage(CSSParserValueList*, RefPtr<CSSValue>&);
105 enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
106 PassRefPtr<CSSValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag);
107 PassRefPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
108 PassRefPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
109 void parseFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
111 void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
112 PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
114 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
115 bool parseFillShorthand(int propId, const CSSPropertyID* properties, int numProperties, bool important);
117 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
119 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
121 PassRefPtr<CSSValue> parseAnimationDelay();
122 PassRefPtr<CSSValue> parseAnimationDirection();
123 PassRefPtr<CSSValue> parseAnimationDuration();
124 PassRefPtr<CSSValue> parseAnimationFillMode();
125 PassRefPtr<CSSValue> parseAnimationIterationCount();
126 PassRefPtr<CSSValue> parseAnimationName();
127 PassRefPtr<CSSValue> parseAnimationPlayState();
128 PassRefPtr<CSSValue> parseAnimationProperty();
129 PassRefPtr<CSSValue> parseAnimationTimingFunction();
131 bool parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
132 bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
133 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
134 bool parseTransitionShorthand(bool important);
135 bool parseAnimationShorthand(bool important);
137 #if ENABLE(CSS_GRID_LAYOUT)
138 bool parseGridTrackList(int propId, bool important);
141 bool parseDashboardRegions(int propId, bool important);
143 bool parseClipShape(int propId, bool important);
145 bool parseExclusionShape(bool shapeInside, bool important);
146 PassRefPtr<CSSWrapShape> parseExclusionShapeRect(CSSParserValueList* args);
147 PassRefPtr<CSSWrapShape> parseExclusionShapeCircle(CSSParserValueList* args);
148 PassRefPtr<CSSWrapShape> parseExclusionShapeEllipse(CSSParserValueList* args);
149 PassRefPtr<CSSWrapShape> parseExclusionShapePolygon(CSSParserValueList* args);
151 bool parseFont(bool important);
152 PassRefPtr<CSSValueList> parseFontFamily();
154 bool parseCounter(int propId, int defaultValue, bool important);
155 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
157 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
158 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
159 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
160 bool parseColorFromValue(CSSParserValue*, RGBA32&);
161 void parseSelector(const String&, Document* doc, CSSSelectorList&);
163 static bool fastParseColor(RGBA32&, const String&, bool strict);
165 bool parseFontVariant(bool important);
166 bool parseFontWeight(bool important);
167 bool parseFontFaceSrc();
168 bool parseFontFaceUnicodeRange();
171 bool parseSVGValue(int propId, bool important);
172 PassRefPtr<CSSValue> parseSVGPaint();
173 PassRefPtr<CSSValue> parseSVGColor();
174 PassRefPtr<CSSValue> parseSVGStrokeDasharray();
177 // CSS3 Parsing Routines (for properties specific to CSS3)
178 PassRefPtr<CSSValueList> parseShadow(CSSParserValueList*, int propId);
179 bool parseBorderImage(int propId, RefPtr<CSSValue>&, bool important = false);
180 bool parseBorderImageRepeat(RefPtr<CSSValue>&);
181 bool parseBorderImageSlice(int propId, RefPtr<CSSBorderImageSliceValue>&);
182 bool parseBorderImageWidth(RefPtr<CSSPrimitiveValue>&);
183 bool parseBorderImageOutset(RefPtr<CSSPrimitiveValue>&);
184 bool parseBorderRadius(int propId, bool important);
186 bool parseAspectRatio(bool important);
188 bool parseReflect(int propId, bool important);
190 bool parseFlex(int propId, bool important);
193 bool parseCanvas(CSSParserValueList*, RefPtr<CSSValue>&);
195 bool parseDeprecatedGradient(CSSParserValueList*, RefPtr<CSSValue>&);
196 bool parseLinearGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
197 bool parseRadialGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
198 bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
200 bool parseCrossfade(CSSParserValueList*, RefPtr<CSSValue>&);
202 #if ENABLE(CSS_IMAGE_SET)
203 PassRefPtr<CSSValue> parseImageSet(CSSParserValueList*);
206 #if ENABLE(CSS_FILTERS)
207 PassRefPtr<CSSValueList> parseFilter();
208 PassRefPtr<WebKitCSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, WebKitCSSFilterValue::FilterOperationType);
209 #if ENABLE(CSS_SHADERS)
210 PassRefPtr<WebKitCSSFilterValue> parseCustomFilter(CSSParserValue*);
214 PassRefPtr<CSSValueList> parseTransform();
215 bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
216 bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
218 bool parseTextEmphasisStyle(bool important);
220 bool parseLineBoxContain(bool important);
221 bool parseCalculation(CSSParserValue*, CalculationPermittedValueRange);
223 bool parseFontFeatureTag(CSSValueList*);
224 bool parseFontFeatureSettings(bool important);
226 bool cssRegionsEnabled() const;
227 bool parseFlowThread(const String& flowName, Document*);
228 bool parseFlowThread(int propId, bool important);
229 bool parseRegionThread(int propId, bool important);
231 bool parseFontVariantLigatures(bool important);
235 CSSParserSelector* createFloatingSelector();
236 PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
238 Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
239 PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
241 CSSParserValueList* createFloatingValueList();
242 PassOwnPtr<CSSParserValueList> sinkFloatingValueList(CSSParserValueList*);
244 CSSParserFunction* createFloatingFunction();
245 PassOwnPtr<CSSParserFunction> sinkFloatingFunction(CSSParserFunction*);
247 CSSParserValue& sinkFloatingValue(CSSParserValue&);
249 MediaQuerySet* createMediaQuerySet();
250 StyleRuleBase* createImportRule(const CSSParserString&, MediaQuerySet*);
251 StyleKeyframe* createKeyframe(CSSParserValueList*);
252 StyleRuleKeyframes* createKeyframesRule();
254 typedef Vector<RefPtr<StyleRuleBase> > RuleList;
255 StyleRuleBase* createMediaRule(MediaQuerySet*, RuleList*);
256 RuleList* createRuleList();
257 StyleRuleBase* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
258 StyleRuleBase* createFontFaceRule();
259 StyleRuleBase* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
260 StyleRuleBase* createRegionRule(Vector<OwnPtr<CSSParserSelector> >* regionSelector, RuleList* rules);
261 StyleRuleBase* createMarginAtRule(CSSSelector::MarginBoxType);
262 void startDeclarationsForMarginBox();
263 void endDeclarationsForMarginBox();
265 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
266 PassOwnPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
267 Vector<OwnPtr<MediaQueryExp> >* createFloatingMediaQueryExpList();
268 PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > sinkFloatingMediaQueryExpList(Vector<OwnPtr<MediaQueryExp> >*);
269 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
270 MediaQuery* createFloatingMediaQuery(PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
271 PassOwnPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
273 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
274 void updateSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*);
275 CSSParserSelector* updateSpecifiers(CSSParserSelector*, CSSParserSelector*);
277 void invalidBlockHit();
279 Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
281 void setReusableRegionSelectorVector(Vector<OwnPtr<CSSParserSelector> >* selectors);
282 Vector<OwnPtr<CSSParserSelector> >* reusableRegionSelectorVector() { return &m_reusableRegionSelectorVector; }
284 void updateLastSelectorLineAndPosition();
285 void updateLastMediaLine(MediaQuerySet*);
287 void clearProperties();
289 CSSParserMode m_cssParserMode;
292 CSSStyleSheet* m_styleSheet;
293 RefPtr<StyleRuleBase> m_rule;
294 RefPtr<StyleKeyframe> m_keyframe;
295 OwnPtr<MediaQuery> m_mediaQuery;
296 OwnPtr<CSSParserValueList> m_valueList;
297 Vector<CSSProperty, 256> m_parsedProperties;
298 CSSSelectorList* m_selectorListForParseSelector;
300 RefPtr<CSSValuePool> m_cssValuePool;
301 unsigned m_numParsedPropertiesBeforeMarginBox;
303 int m_inParseShorthand;
304 int m_currentShorthand;
305 bool m_implicitShorthand;
307 bool m_hasFontFaceOnlyValues;
308 bool m_hadSyntacticallyValidCSSRule;
310 AtomicString m_defaultNamespace;
312 // tokenizer methods and data
313 bool m_inStyleRuleOrDeclaration;
314 SourceRange m_selectorListRange;
315 SourceRange m_ruleBodyRange;
316 SourceRange m_propertyRange;
317 StyleRuleRangeMap* m_ruleRangeMap;
318 RefPtr<CSSRuleSourceData> m_currentRuleData;
319 void markSelectorListStart();
320 void markSelectorListEnd();
321 void markRuleBodyStart();
322 void markRuleBodyEnd();
323 void markPropertyStart();
324 void markPropertyEnd(bool isImportantFound, bool isPropertyParsed);
325 void resetSelectorListMarks() { m_selectorListRange.start = m_selectorListRange.end = 0; }
326 void resetRuleBodyMarks() { m_ruleBodyRange.start = m_ruleBodyRange.end = 0; }
327 void resetPropertyMarks() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; }
328 int lex(void* yylval);
329 int token() { return m_token; }
331 PassRefPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
332 PassRefPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
335 inline bool isIdentifierStart();
337 static inline UChar* checkAndSkipString(UChar*, UChar);
339 void parseEscape(UChar*&);
340 inline void parseIdentifier(UChar*&, bool&);
341 inline void parseString(UChar*&, UChar);
342 inline void parseURI(UChar*&, UChar*&);
343 inline bool parseUnicodeRange();
344 bool parseNthChild();
345 bool parseNthChildExtra();
346 inline void detectFunctionTypeToken(int);
347 inline void detectMediaQueryToken(int);
348 inline void detectNumberToken(UChar*, int);
349 inline void detectDashToken(int);
350 inline void detectAtToken(int, bool);
352 void setStyleSheet(CSSStyleSheet*);
353 void ensureCSSValuePool();
355 inline bool inStrictMode() const { return m_cssParserMode == CSSStrictMode || m_cssParserMode == SVGAttributeMode; }
356 inline bool inQuirksMode() const { return m_cssParserMode == CSSQuirksMode; }
358 void recheckAtKeyword(const UChar* str, int len);
360 void setupParser(const char* prefix, const String&, const char* suffix);
362 bool inShorthand() const { return m_inParseShorthand; }
364 void checkForOrphanedUnits();
366 void deleteFontFaceOnlyValues();
368 bool isGeneratedImageValue(CSSParserValue*) const;
369 bool parseGeneratedImage(CSSParserValueList*, RefPtr<CSSValue>&);
371 bool parseValue(StylePropertySet*, int propId, const String&, bool important, CSSStyleSheet* contextStyleSheet);
373 enum SizeParameterType {
381 bool parsePage(int propId, bool important);
382 bool parseSize(int propId, bool important);
383 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
385 bool parseFontFaceSrcURI(CSSValueList*);
386 bool parseFontFaceSrcLocal(CSSValueList*);
388 bool parseColor(const String&);
396 ParsingMode m_parsingMode;
397 OwnArrayPtr<UChar> m_dataStart;
398 UChar* m_currentCharacter;
402 int m_lastSelectorLineNumber;
404 bool m_allowImportRules;
405 bool m_allowNamespaceDeclarations;
407 Vector<RefPtr<StyleRuleBase> > m_parsedRules;
408 Vector<RefPtr<StyleKeyframe> > m_parsedKeyframes;
409 Vector<RefPtr<MediaQuerySet> > m_parsedMediaQuerySets;
410 Vector<OwnPtr<RuleList> > m_parsedRuleLists;
411 HashSet<CSSParserSelector*> m_floatingSelectors;
412 HashSet<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
413 HashSet<CSSParserValueList*> m_floatingValueLists;
414 HashSet<CSSParserFunction*> m_floatingFunctions;
416 OwnPtr<MediaQuery> m_floatingMediaQuery;
417 OwnPtr<MediaQueryExp> m_floatingMediaQueryExp;
418 OwnPtr<Vector<OwnPtr<MediaQueryExp> > > m_floatingMediaQueryExpList;
420 Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
421 Vector<OwnPtr<CSSParserSelector> > m_reusableRegionSelectorVector;
423 RefPtr<CSSCalcValue> m_parsedCalculation;
425 // defines units allowed for a certain property, used in parseUnit
429 FNumber = 0x0002, // Real Numbers
439 friend inline Units operator|(Units a, Units b)
441 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
444 bool validCalculationUnit(CSSParserValue*, Units);
446 bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
448 inline bool validUnit(CSSParserValue* value, Units unitflags) { return validUnit(value, unitflags, m_cssParserMode); }
449 bool validUnit(CSSParserValue*, Units, CSSParserMode);
451 bool parseBorderImageQuad(Units, RefPtr<CSSPrimitiveValue>&);
452 int colorIntFromValue(CSSParserValue*);
454 enum ReleaseParsedCalcValueCondition {
455 ReleaseParsedCalcValue,
456 DoNotReleaseParsedCalcValue
458 double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
459 bool isCalculation(CSSParserValue*);
461 friend class TransformOperationInfo;
462 #if ENABLE(CSS_FILTERS)
463 friend class FilterOperationInfo;
467 int cssPropertyID(const CSSParserString&);
468 int cssPropertyID(const String&);
469 int cssValueKeywordID(const CSSParserString&);
471 void cssPropertyNameIOSAliasing(const char* propertyName, const char*& propertyNameAlias, unsigned& newLength);
474 class ShorthandScope {
475 WTF_MAKE_FAST_ALLOCATED;
477 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
479 if (!(m_parser->m_inParseShorthand++))
480 m_parser->m_currentShorthand = propId;
484 if (!(--m_parser->m_inParseShorthand))
485 m_parser->m_currentShorthand = 0;
492 String quoteCSSString(const String&);
493 String quoteCSSStringIfNeeded(const String&);
494 String quoteCSSURLIfNeeded(const String&);
496 bool isValidNthToken(const CSSParserString&);
497 } // namespace WebCore
499 #endif // CSSParser_h