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.
23 #ifndef BisonCSSParser_h
24 #define BisonCSSParser_h
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "core/css/CSSCalculationValue.h"
29 #include "core/css/CSSFilterValue.h"
30 #include "core/css/CSSGradientValue.h"
31 #include "core/css/CSSParserMode.h"
32 #include "core/css/CSSParserValues.h"
33 #include "core/css/CSSProperty.h"
34 #include "core/css/CSSPropertySourceData.h"
35 #include "core/css/CSSSelector.h"
36 #include "core/css/CSSTokenizer.h"
37 #include "core/css/MediaQuery.h"
38 #include "core/css/StylePropertySet.h"
39 #include "core/css/parser/CSSParserObserver.h"
40 #include "core/css/parser/CSSPropertyParser.h"
41 #include "platform/graphics/Color.h"
42 #include "wtf/HashSet.h"
43 #include "wtf/OwnPtr.h"
44 #include "wtf/Vector.h"
45 #include "wtf/text/AtomicString.h"
46 #include "wtf/text/TextPosition.h"
50 class AnimationParseContext;
51 class CSSArrayFunctionValue;
52 class CSSBorderImageSliceValue;
53 class CSSPrimitiveValue;
54 class CSSSelectorList;
58 class CSSBasicShapeInset;
61 class ImmutableStylePropertySet;
64 class MutableStylePropertySet;
66 class StylePropertyShorthand;
68 class StyleRuleKeyframes;
70 class StyleSheetContents;
73 // FIXME: This class is shared with CSSTokenizer so should we rename it to CSSSourceLocation?
74 struct CSSParserLocation {
77 CSSParserString token;
80 class BisonCSSParser {
82 friend inline int cssyylex(void*, BisonCSSParser*);
84 explicit BisonCSSParser(const CSSParserContext&);
87 void rollbackLastProperties(int num);
88 void setCurrentProperty(CSSPropertyID);
90 void parseSheet(StyleSheetContents*, const String&, const TextPosition& startPosition = TextPosition::minimumPosition(), CSSParserObserver* = 0, bool = false);
91 PassRefPtrWillBeRawPtr<StyleRuleBase> parseRule(StyleSheetContents*, const String&);
92 PassRefPtrWillBeRawPtr<StyleKeyframe> parseKeyframeRule(StyleSheetContents*, const String&);
93 bool parseSupportsCondition(const String&);
94 static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, CSSParserMode, StyleSheetContents*);
95 static bool parseColor(RGBA32& color, const String&, bool strict = false);
96 static bool parseSystemColor(RGBA32& color, const String&);
97 static PassRefPtrWillBeRawPtr<CSSValueList> parseFontFaceValue(const AtomicString&);
98 static PassRefPtrWillBeRawPtr<CSSValue> parseAnimationTimingFunctionValue(const String&);
99 bool parseDeclaration(MutableStylePropertySet*, const String&, CSSParserObserver*, StyleSheetContents* contextStyleSheet);
100 static PassRefPtr<ImmutableStylePropertySet> parseInlineStyleDeclaration(const String&, Element*);
101 PassRefPtrWillBeRawPtr<MediaQuerySet> parseMediaQueryList(const String&);
102 PassOwnPtr<Vector<double> > parseKeyframeKeyList(const String&);
104 static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, const Document&);
106 bool parseValue(CSSPropertyID, bool important);
107 void parseSelector(const String&, CSSSelectorList&);
109 CSSParserSelector* createFloatingSelector();
110 CSSParserSelector* createFloatingSelectorWithTagName(const QualifiedName&);
111 PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
113 Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
114 PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
116 CSSParserValueList* createFloatingValueList();
117 PassOwnPtr<CSSParserValueList> sinkFloatingValueList(CSSParserValueList*);
119 CSSParserFunction* createFloatingFunction();
120 CSSParserFunction* createFloatingFunction(const CSSParserString& name, PassOwnPtr<CSSParserValueList> args);
121 PassOwnPtr<CSSParserFunction> sinkFloatingFunction(CSSParserFunction*);
123 CSSParserValue& sinkFloatingValue(CSSParserValue&);
125 MediaQuerySet* createMediaQuerySet();
126 StyleRuleBase* createImportRule(const CSSParserString&, MediaQuerySet*);
127 StyleKeyframe* createKeyframe(CSSParserValueList*);
128 StyleRuleKeyframes* createKeyframesRule(const String&, PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > >, bool isPrefixed);
130 typedef WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > RuleList;
131 StyleRuleBase* createMediaRule(MediaQuerySet*, RuleList*);
132 RuleList* createRuleList();
133 RuleList* appendRule(RuleList*, StyleRuleBase*);
134 StyleRuleBase* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
135 StyleRuleBase* createFontFaceRule();
136 StyleRuleBase* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
137 StyleRuleBase* createMarginAtRule(CSSSelector::MarginBoxType);
138 StyleRuleBase* createSupportsRule(bool conditionIsSupported, RuleList*);
139 void markSupportsRuleHeaderStart();
140 void markSupportsRuleHeaderEnd();
141 PassRefPtrWillBeRawPtr<CSSRuleSourceData> popSupportsRuleData();
142 StyleRuleBase* createHostRule(RuleList* rules);
144 void startDeclarationsForMarginBox();
145 void endDeclarationsForMarginBox();
147 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
148 PassOwnPtrWillBeRawPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
149 WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> >* createFloatingMediaQueryExpList();
150 PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > > sinkFloatingMediaQueryExpList(WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> >*);
151 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const AtomicString&, PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > >);
152 MediaQuery* createFloatingMediaQuery(PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > >);
153 MediaQuery* createFloatingNotAllQuery();
154 PassOwnPtrWillBeRawPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
156 WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> >* createFloatingKeyframeVector();
157 PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > > sinkFloatingKeyframeVector(WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> >*);
159 void addNamespace(const AtomicString& prefix, const AtomicString& uri);
160 QualifiedName determineNameInNamespace(const AtomicString& prefix, const AtomicString& localName);
162 CSSParserSelector* rewriteSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*, bool isNamespacePlaceholder = false);
163 CSSParserSelector* rewriteSpecifiersWithElementNameForCustomPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
164 CSSParserSelector* rewriteSpecifiersWithElementNameForContentPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
165 CSSParserSelector* rewriteSpecifiersWithNamespaceIfNeeded(CSSParserSelector*);
166 CSSParserSelector* rewriteSpecifiers(CSSParserSelector*, CSSParserSelector*);
167 CSSParserSelector* rewriteSpecifiersForShadowDistributed(CSSParserSelector* specifiers, CSSParserSelector* distributedPseudoElementSelector);
169 void invalidBlockHit();
171 Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
173 void clearProperties();
175 PassRefPtr<ImmutableStylePropertySet> createStylePropertySet();
177 CSSParserContext m_context;
181 RawPtrWillBeMember<StyleSheetContents> m_styleSheet;
182 RefPtrWillBeMember<StyleRuleBase> m_rule;
183 RefPtrWillBeMember<StyleKeyframe> m_keyframe;
184 RefPtrWillBeMember<MediaQuerySet> m_mediaList;
185 OwnPtr<CSSParserValueList> m_valueList;
186 bool m_supportsCondition;
188 WillBeHeapVector<CSSProperty, 256> m_parsedProperties;
189 CSSSelectorList* m_selectorListForParseSelector;
191 unsigned m_numParsedPropertiesBeforeMarginBox;
193 bool m_hasFontFaceOnlyValues;
194 bool m_hadSyntacticallyValidCSSRule;
198 AtomicString m_defaultNamespace;
200 // tokenizer methods and data
201 CSSParserObserver* m_observer;
203 // Local functions which just call into CSSParserObserver if non-null.
205 void endRule(bool valid);
206 void startRuleHeader(CSSRuleSourceData::Type);
207 void endRuleHeader();
208 void startSelector();
210 void startRuleBody();
211 void startProperty();
212 void endProperty(bool isImportantFound, bool isPropertyParsed, CSSParserError = NoCSSError);
213 void startEndUnknownRule();
215 void endInvalidRuleHeader();
216 void reportError(const CSSParserLocation&, CSSParserError = GeneralCSSError);
217 void resumeErrorLogging() { m_ignoreErrors = false; }
218 void setLocationLabel(const CSSParserLocation& location) { m_locationLabel = location; }
219 const CSSParserLocation& lastLocationLabel() const { return m_locationLabel; }
221 void tokenToLowerCase(CSSParserString& token);
223 void markViewportRuleBodyStart() { m_inViewport = true; }
224 void markViewportRuleBodyEnd() { m_inViewport = false; }
225 StyleRuleBase* createViewportRule();
227 CSSParserLocation currentLocation() { return m_tokenizer.currentLocation(); }
230 class StyleDeclarationScope {
232 WTF_MAKE_NONCOPYABLE(StyleDeclarationScope);
234 StyleDeclarationScope(BisonCSSParser* parser, const StylePropertySet* declaration)
236 , m_mode(declaration->cssParserMode())
238 if (isCSSViewportParsingEnabledForMode(m_mode)) {
239 ASSERT(!m_parser->inViewport());
240 m_parser->markViewportRuleBodyStart();
244 ~StyleDeclarationScope()
246 if (isCSSViewportParsingEnabledForMode(m_mode))
247 m_parser->markViewportRuleBodyEnd();
251 BisonCSSParser* m_parser;
252 CSSParserMode m_mode;
255 inline void ensureLineEndings();
257 void setStyleSheet(StyleSheetContents* styleSheet) { m_styleSheet = styleSheet; }
259 bool inViewport() const { return m_inViewport; }
261 void recheckAtKeyword(const UChar* str, int len);
263 template<unsigned prefixLength, unsigned suffixLength>
264 inline void setupParser(const char (&prefix)[prefixLength], const String& string, const char (&suffix)[suffixLength])
266 setupParser(prefix, prefixLength - 1, string, suffix, suffixLength - 1);
268 void setupParser(const char* prefix, unsigned prefixLength, const String&, const char* suffix, unsigned suffixLength);
270 void deleteFontFaceOnlyValues();
272 bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, StyleSheetContents* contextStyleSheet);
273 PassRefPtr<ImmutableStylePropertySet> parseDeclaration(const String&, StyleSheetContents* contextStyleSheet);
275 bool parseColor(const String&);
277 const String* m_source;
278 TextPosition m_startPosition;
279 CSSRuleSourceData::Type m_ruleHeaderType;
280 unsigned m_ruleHeaderStartOffset;
281 int m_ruleHeaderStartLineNumber;
282 OwnPtr<Vector<unsigned> > m_lineEndings;
284 bool m_ruleHasHeader;
286 bool m_allowImportRules;
287 bool m_allowNamespaceDeclarations;
291 CSSParserLocation m_locationLabel;
293 WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > m_parsedRules;
294 WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > m_parsedKeyframes;
295 WillBeHeapVector<RefPtrWillBeMember<MediaQuerySet> > m_parsedMediaQuerySets;
296 WillBeHeapVector<OwnPtrWillBeMember<RuleList> > m_parsedRuleLists;
297 Vector<CSSParserSelector*> m_floatingSelectors;
298 Vector<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
299 Vector<CSSParserValueList*> m_floatingValueLists;
300 Vector<CSSParserFunction*> m_floatingFunctions;
302 OwnPtrWillBeMember<MediaQuery> m_floatingMediaQuery;
303 OwnPtrWillBeMember<MediaQueryExp> m_floatingMediaQueryExp;
304 OwnPtrWillBeMember<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > > m_floatingMediaQueryExpList;
306 OwnPtrWillBeMember<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > > m_floatingKeyframeVector;
308 Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
310 OwnPtrWillBeMember<RuleSourceDataList> m_supportsRuleDataStack;
312 bool isLoggingErrors();
313 void logError(const String& message, const CSSParserLocation&);
315 CSSTokenizer m_tokenizer;
317 friend class TransformOperationInfo;
318 friend class FilterOperationInfo;
321 inline int cssyylex(void* yylval, BisonCSSParser* parser)
323 return parser->m_tokenizer.lex(yylval);
326 bool isValidNthToken(const CSSParserString&);
328 } // namespace WebCore
330 #endif // BisonCSSParser_h