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;
53 class CSSSelectorList;
61 class StylePropertySet;
62 class StylePropertyShorthand;
65 class WebKitCSSKeyframesRule;
69 CSSParser(CSSParserMode = CSSStrictMode);
73 void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRangeMap* ruleRangeMap = 0);
74 PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
75 PassRefPtr<StyleKeyframe> parseKeyframeRule(CSSStyleSheet*, const String&);
76 static bool parseValue(StylePropertySet*, int propId, const String&, bool important, CSSParserMode, CSSStyleSheet* contextStyleSheet);
77 static bool parseColor(RGBA32& color, const String&, bool strict = false);
78 static bool parseSystemColor(RGBA32& color, const String&, Document*);
79 static PassRefPtr<CSSValueList> parseFontFaceValue(const AtomicString&, CSSStyleSheet* contextStyleSheet);
80 PassRefPtr<CSSPrimitiveValue> parseValidPrimitive(int propId, CSSParserValue*);
81 bool parseDeclaration(StylePropertySet*, const String&, RefPtr<CSSStyleSourceData>*, CSSStyleSheet* contextStyleSheet);
82 PassOwnPtr<MediaQuery> parseMediaQuery(const String&);
84 Document* findDocument() const;
86 CSSValuePool* cssValuePool() const { return m_cssValuePool.get(); }
88 void addProperty(int propId, PassRefPtr<CSSValue>, bool important, bool implicit = false);
89 void rollbackLastProperties(int num);
90 bool hasProperties() const { return !m_parsedProperties.isEmpty(); }
92 bool parseValue(int propId, bool important);
93 bool parseShorthand(int, const StylePropertyShorthand&, bool);
94 bool parse4Values(int propId, const CSSPropertyID* properties, bool important);
95 bool parseContent(int propId, bool important);
96 bool parseQuotes(int propId, bool important);
98 PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
100 PassRefPtr<CSSValue> parseBackgroundColor();
102 bool parseFillImage(CSSParserValueList*, RefPtr<CSSValue>&);
104 enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 };
105 PassRefPtr<CSSValue> parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag);
106 PassRefPtr<CSSValue> parseFillPositionX(CSSParserValueList*);
107 PassRefPtr<CSSValue> parseFillPositionY(CSSParserValueList*);
108 void parseFillPosition(CSSParserValueList*, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
110 void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
111 PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
113 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
114 bool parseFillShorthand(int propId, const CSSPropertyID* properties, int numProperties, bool important);
116 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
118 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
120 PassRefPtr<CSSValue> parseAnimationDelay();
121 PassRefPtr<CSSValue> parseAnimationDirection();
122 PassRefPtr<CSSValue> parseAnimationDuration();
123 PassRefPtr<CSSValue> parseAnimationFillMode();
124 PassRefPtr<CSSValue> parseAnimationIterationCount();
125 PassRefPtr<CSSValue> parseAnimationName();
126 PassRefPtr<CSSValue> parseAnimationPlayState();
127 PassRefPtr<CSSValue> parseAnimationProperty();
128 PassRefPtr<CSSValue> parseAnimationTimingFunction();
130 bool parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
131 bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result);
132 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
133 bool parseTransitionShorthand(bool important);
134 bool parseAnimationShorthand(bool important);
136 #if ENABLE(CSS_GRID_LAYOUT)
137 bool parseGridTrackList(int propId, bool important);
140 bool parseDashboardRegions(int propId, bool important);
142 bool parseClipShape(int propId, bool important);
144 bool parseExclusionShape(bool shapeInside, bool important);
145 PassRefPtr<CSSWrapShape> parseExclusionShapeRect(CSSParserValueList* args);
146 PassRefPtr<CSSWrapShape> parseExclusionShapeCircle(CSSParserValueList* args);
147 PassRefPtr<CSSWrapShape> parseExclusionShapeEllipse(CSSParserValueList* args);
148 PassRefPtr<CSSWrapShape> parseExclusionShapePolygon(CSSParserValueList* args);
150 bool parseFont(bool important);
151 PassRefPtr<CSSValueList> parseFontFamily();
153 bool parseCounter(int propId, int defaultValue, bool important);
154 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
156 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
157 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
158 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
159 bool parseColorFromValue(CSSParserValue*, RGBA32&);
160 void parseSelector(const String&, Document* doc, CSSSelectorList&);
162 static bool fastParseColor(RGBA32&, const String&, bool strict);
164 bool parseFontVariant(bool important);
165 bool parseFontWeight(bool important);
166 bool parseFontFaceSrc();
167 bool parseFontFaceUnicodeRange();
170 bool parseSVGValue(int propId, bool important);
171 PassRefPtr<CSSValue> parseSVGPaint();
172 PassRefPtr<CSSValue> parseSVGColor();
173 PassRefPtr<CSSValue> parseSVGStrokeDasharray();
176 // CSS3 Parsing Routines (for properties specific to CSS3)
177 PassRefPtr<CSSValueList> parseShadow(CSSParserValueList*, int propId);
178 bool parseBorderImage(int propId, RefPtr<CSSValue>&, bool important = false);
179 bool parseBorderImageRepeat(RefPtr<CSSValue>&);
180 bool parseBorderImageSlice(int propId, RefPtr<CSSBorderImageSliceValue>&);
181 bool parseBorderImageWidth(RefPtr<CSSPrimitiveValue>&);
182 bool parseBorderImageOutset(RefPtr<CSSPrimitiveValue>&);
183 bool parseBorderRadius(int propId, bool important);
185 bool parseAspectRatio(bool important);
187 bool parseReflect(int propId, bool important);
189 bool parseFlex(int propId, bool important);
192 bool parseCanvas(CSSParserValueList*, RefPtr<CSSValue>&);
194 bool parseDeprecatedGradient(CSSParserValueList*, RefPtr<CSSValue>&);
195 bool parseLinearGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
196 bool parseRadialGradient(CSSParserValueList*, RefPtr<CSSValue>&, CSSGradientRepeat repeating);
197 bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma);
199 bool parseCrossfade(CSSParserValueList*, RefPtr<CSSValue>&);
201 #if ENABLE(CSS_IMAGE_SET)
202 PassRefPtr<CSSValue> parseImageSet(CSSParserValueList*);
205 #if ENABLE(CSS_FILTERS)
206 PassRefPtr<CSSValueList> parseFilter();
207 PassRefPtr<WebKitCSSFilterValue> parseBuiltinFilterArguments(CSSParserValueList*, WebKitCSSFilterValue::FilterOperationType);
208 #if ENABLE(CSS_SHADERS)
209 PassRefPtr<WebKitCSSFilterValue> parseCustomFilter(CSSParserValue*);
213 PassRefPtr<CSSValueList> parseTransform();
214 bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
215 bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
217 bool parseTextEmphasisStyle(bool important);
219 bool parseLineBoxContain(bool important);
220 bool parseCalculation(CSSParserValue*, CalculationPermittedValueRange);
222 bool parseFontFeatureTag(CSSValueList*);
223 bool parseFontFeatureSettings(bool important);
225 bool cssRegionsEnabled() const;
226 bool parseFlowThread(const String& flowName, Document*);
227 bool parseFlowThread(int propId, bool important);
228 bool parseRegionThread(int propId, bool important);
230 bool parseFontVariantLigatures(bool important);
234 CSSParserSelector* createFloatingSelector();
235 PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
237 Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
238 PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
240 CSSParserValueList* createFloatingValueList();
241 PassOwnPtr<CSSParserValueList> sinkFloatingValueList(CSSParserValueList*);
243 CSSParserFunction* createFloatingFunction();
244 PassOwnPtr<CSSParserFunction> sinkFloatingFunction(CSSParserFunction*);
246 CSSParserValue& sinkFloatingValue(CSSParserValue&);
248 MediaQuerySet* createMediaQuerySet();
249 CSSRule* createImportRule(const CSSParserString&, MediaQuerySet*);
250 StyleKeyframe* createKeyframe(CSSParserValueList*);
251 WebKitCSSKeyframesRule* createKeyframesRule();
253 typedef Vector<RefPtr<CSSRule> > RuleList;
254 CSSRule* createMediaRule(MediaQuerySet*, RuleList*);
255 RuleList* createRuleList();
256 CSSRule* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
257 CSSRule* createFontFaceRule();
258 CSSRule* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
259 CSSRule* createRegionRule(Vector<OwnPtr<CSSParserSelector> >* regionSelector, RuleList* rules);
260 CSSRule* createMarginAtRule(CSSSelector::MarginBoxType marginBox);
261 void startDeclarationsForMarginBox();
262 void endDeclarationsForMarginBox();
264 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
265 PassOwnPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
266 Vector<OwnPtr<MediaQueryExp> >* createFloatingMediaQueryExpList();
267 PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > > sinkFloatingMediaQueryExpList(Vector<OwnPtr<MediaQueryExp> >*);
268 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
269 MediaQuery* createFloatingMediaQuery(PassOwnPtr<Vector<OwnPtr<MediaQueryExp> > >);
270 PassOwnPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
272 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
273 void updateSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*);
274 CSSParserSelector* updateSpecifiers(CSSParserSelector*, CSSParserSelector*);
276 void invalidBlockHit();
278 Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
280 void setReusableRegionSelectorVector(Vector<OwnPtr<CSSParserSelector> >* selectors);
281 Vector<OwnPtr<CSSParserSelector> >* reusableRegionSelectorVector() { return &m_reusableRegionSelectorVector; }
283 void updateLastSelectorLineAndPosition();
284 void updateLastMediaLine(MediaQuerySet*);
286 void clearProperties();
288 CSSParserMode m_cssParserMode;
291 CSSStyleSheet* m_styleSheet;
292 RefPtr<CSSRule> m_rule;
293 RefPtr<StyleKeyframe> m_keyframe;
294 OwnPtr<MediaQuery> m_mediaQuery;
295 OwnPtr<CSSParserValueList> m_valueList;
296 Vector<CSSProperty, 256> m_parsedProperties;
297 CSSSelectorList* m_selectorListForParseSelector;
299 RefPtr<CSSValuePool> m_cssValuePool;
300 unsigned m_numParsedPropertiesBeforeMarginBox;
302 int m_inParseShorthand;
303 int m_currentShorthand;
304 bool m_implicitShorthand;
306 bool m_hasFontFaceOnlyValues;
307 bool m_hadSyntacticallyValidCSSRule;
309 AtomicString m_defaultNamespace;
311 // tokenizer methods and data
312 bool m_inStyleRuleOrDeclaration;
313 SourceRange m_selectorListRange;
314 SourceRange m_ruleBodyRange;
315 SourceRange m_propertyRange;
316 StyleRuleRangeMap* m_ruleRangeMap;
317 RefPtr<CSSRuleSourceData> m_currentRuleData;
318 void markSelectorListStart();
319 void markSelectorListEnd();
320 void markRuleBodyStart();
321 void markRuleBodyEnd();
322 void markPropertyStart();
323 void markPropertyEnd(bool isImportantFound, bool isPropertyParsed);
324 void resetSelectorListMarks() { m_selectorListRange.start = m_selectorListRange.end = 0; }
325 void resetRuleBodyMarks() { m_ruleBodyRange.start = m_ruleBodyRange.end = 0; }
326 void resetPropertyMarks() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; }
327 int lex(void* yylval);
328 int token() { return m_token; }
330 PassRefPtr<CSSPrimitiveValue> createPrimitiveNumericValue(CSSParserValue*);
331 PassRefPtr<CSSPrimitiveValue> createPrimitiveStringValue(CSSParserValue*);
334 inline bool isIdentifierStart();
336 static inline UChar* checkAndSkipString(UChar*, UChar);
338 void parseEscape(UChar*&);
339 inline void parseIdentifier(UChar*&, bool&);
340 inline void parseString(UChar*&, UChar);
341 inline void parseURI(UChar*&, UChar*&);
342 inline bool parseUnicodeRange();
343 bool parseNthChild();
344 bool parseNthChildExtra();
345 inline void detectFunctionTypeToken(int);
346 inline void detectMediaQueryToken(int);
347 inline void detectNumberToken(UChar*, int);
348 inline void detectDashToken(int);
349 inline void detectAtToken(int, bool);
351 void setStyleSheet(CSSStyleSheet*);
352 void ensureCSSValuePool();
354 inline bool inStrictMode() const { return m_cssParserMode == CSSStrictMode || m_cssParserMode == SVGAttributeMode; }
355 inline bool inQuirksMode() const { return m_cssParserMode == CSSQuirksMode; }
357 void recheckAtKeyword(const UChar* str, int len);
359 void setupParser(const char* prefix, const String&, const char* suffix);
361 bool inShorthand() const { return m_inParseShorthand; }
363 void checkForOrphanedUnits();
365 void deleteFontFaceOnlyValues();
367 bool isGeneratedImageValue(CSSParserValue*) const;
368 bool parseGeneratedImage(CSSParserValueList*, RefPtr<CSSValue>&);
370 bool parseValue(StylePropertySet*, int propId, const String&, bool important, CSSStyleSheet* contextStyleSheet);
372 enum SizeParameterType {
380 bool parsePage(int propId, bool important);
381 bool parseSize(int propId, bool important);
382 SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
384 bool parseFontFaceSrcURI(CSSValueList*);
385 bool parseFontFaceSrcLocal(CSSValueList*);
387 bool parseColor(const String&);
395 ParsingMode m_parsingMode;
396 OwnArrayPtr<UChar> m_dataStart;
397 UChar* m_currentCharacter;
401 int m_lastSelectorLineNumber;
403 bool m_allowImportRules;
404 bool m_allowNamespaceDeclarations;
406 Vector<RefPtr<CSSRule> > m_parsedRules;
407 Vector<RefPtr<StyleKeyframe> > m_parsedKeyframes;
408 Vector<RefPtr<MediaQuerySet> > m_parsedMediaQuerySets;
409 Vector<OwnPtr<RuleList> > m_parsedRuleLists;
410 HashSet<CSSParserSelector*> m_floatingSelectors;
411 HashSet<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
412 HashSet<CSSParserValueList*> m_floatingValueLists;
413 HashSet<CSSParserFunction*> m_floatingFunctions;
415 OwnPtr<MediaQuery> m_floatingMediaQuery;
416 OwnPtr<MediaQueryExp> m_floatingMediaQueryExp;
417 OwnPtr<Vector<OwnPtr<MediaQueryExp> > > m_floatingMediaQueryExpList;
419 Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
420 Vector<OwnPtr<CSSParserSelector> > m_reusableRegionSelectorVector;
422 RefPtr<CSSCalcValue> m_parsedCalculation;
424 // defines units allowed for a certain property, used in parseUnit
428 FNumber = 0x0002, // Real Numbers
438 friend inline Units operator|(Units a, Units b)
440 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
443 bool validCalculationUnit(CSSParserValue*, Units);
445 bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode);
447 inline bool validUnit(CSSParserValue* value, Units unitflags) { return validUnit(value, unitflags, m_cssParserMode); }
448 bool validUnit(CSSParserValue*, Units, CSSParserMode);
450 bool parseBorderImageQuad(Units, RefPtr<CSSPrimitiveValue>&);
451 int colorIntFromValue(CSSParserValue*);
453 enum ReleaseParsedCalcValueCondition {
454 ReleaseParsedCalcValue,
455 DoNotReleaseParsedCalcValue
457 double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue);
458 bool isCalculation(CSSParserValue*);
460 friend class TransformOperationInfo;
461 #if ENABLE(CSS_FILTERS)
462 friend class FilterOperationInfo;
466 int cssPropertyID(const CSSParserString&);
467 int cssPropertyID(const String&);
468 int cssValueKeywordID(const CSSParserString&);
470 void cssPropertyNameIOSAliasing(const char* propertyName, const char*& propertyNameAlias, unsigned& newLength);
473 class ShorthandScope {
474 WTF_MAKE_FAST_ALLOCATED;
476 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
478 if (!(m_parser->m_inParseShorthand++))
479 m_parser->m_currentShorthand = propId;
483 if (!(--m_parser->m_inParseShorthand))
484 m_parser->m_currentShorthand = 0;
491 String quoteCSSString(const String&);
492 String quoteCSSStringIfNeeded(const String&);
493 String quoteCSSURLIfNeeded(const String&);
495 bool isValidNthToken(const CSSParserString&);
496 } // namespace WebCore
498 #endif // CSSParser_h