Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / parser / BisonCSSParser.h
1 /*
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.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifndef BisonCSSParser_h
24 #define BisonCSSParser_h
25
26 #include "core/CSSPropertyNames.h"
27 #include "core/CSSValueKeywords.h"
28 #include "core/css/CSSCalculationValue.h"
29 #include "core/css/CSSFilterValue.h"
30 #include "core/css/CSSGradientValue.h"
31 #include "core/css/CSSProperty.h"
32 #include "core/css/CSSPropertySourceData.h"
33 #include "core/css/CSSSelector.h"
34 #include "core/css/MediaQuery.h"
35 #include "core/css/StylePropertySet.h"
36 #include "core/css/parser/CSSParserMode.h"
37 #include "core/css/parser/CSSParserObserver.h"
38 #include "core/css/parser/CSSParserValues.h"
39 #include "core/css/parser/CSSPropertyParser.h"
40 #include "core/css/parser/CSSTokenizer.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"
47
48 namespace blink {
49
50 class CSSSelectorList;
51 class CSSValue;
52 class CSSValueList;
53 class Document;
54 class Element;
55 class ImmutableStylePropertySet;
56 class MediaQueryExp;
57 class MediaQuerySet;
58 class MutableStylePropertySet;
59 class StyleColor;
60 class StyleKeyframe;
61 class StyleRuleBase;
62 class StyleRuleKeyframes;
63 class StyleKeyframe;
64 class StyleSheetContents;
65
66 // FIXME: This class is shared with CSSTokenizer so should we rename it to CSSSourceLocation?
67 struct CSSParserLocation {
68     unsigned offset;
69     unsigned lineNumber;
70     CSSParserString token;
71 };
72
73 class BisonCSSParser {
74     STACK_ALLOCATED();
75     friend inline int cssyylex(void*, BisonCSSParser*);
76 public:
77     explicit BisonCSSParser(const CSSParserContext&);
78     ~BisonCSSParser();
79
80     void rollbackLastProperties(int num);
81     void setCurrentProperty(CSSPropertyID);
82
83     void parseSheet(StyleSheetContents*, const String&, const TextPosition& startPosition = TextPosition::minimumPosition(), CSSParserObserver* = 0, bool = false);
84     PassRefPtrWillBeRawPtr<StyleRuleBase> parseRule(StyleSheetContents*, const String&);
85     PassRefPtrWillBeRawPtr<StyleKeyframe> parseKeyframeRule(StyleSheetContents*, const String&);
86     bool parseSupportsCondition(const String&);
87     static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, const CSSParserContext&);
88     static bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, CSSParserMode, StyleSheetContents*);
89     static bool parseColor(RGBA32& color, const String&, bool strict = false);
90     static StyleColor colorFromRGBColorString(const String&);
91     static bool parseSystemColor(RGBA32& color, const String&);
92     bool parseDeclaration(MutableStylePropertySet*, const String&, CSSParserObserver*, StyleSheetContents* contextStyleSheet);
93     static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> parseInlineStyleDeclaration(const String&, Element*);
94     PassOwnPtr<Vector<double> > parseKeyframeKeyList(const String&);
95     bool parseAttributeMatchType(CSSSelector::AttributeMatchType&, const String&);
96
97     bool parseValue(CSSPropertyID, bool important);
98     void parseSelector(const String&, CSSSelectorList&);
99
100     CSSParserSelector* createFloatingSelector();
101     CSSParserSelector* createFloatingSelectorWithTagName(const QualifiedName&);
102     PassOwnPtr<CSSParserSelector> sinkFloatingSelector(CSSParserSelector*);
103
104     Vector<OwnPtr<CSSParserSelector> >* createFloatingSelectorVector();
105     PassOwnPtr<Vector<OwnPtr<CSSParserSelector> > > sinkFloatingSelectorVector(Vector<OwnPtr<CSSParserSelector> >*);
106
107     CSSParserValueList* createFloatingValueList();
108     PassOwnPtr<CSSParserValueList> sinkFloatingValueList(CSSParserValueList*);
109
110     CSSParserFunction* createFloatingFunction();
111     CSSParserFunction* createFloatingFunction(const CSSParserString& name, PassOwnPtr<CSSParserValueList> args);
112     PassOwnPtr<CSSParserFunction> sinkFloatingFunction(CSSParserFunction*);
113
114     CSSParserValue& sinkFloatingValue(CSSParserValue&);
115
116     MediaQuerySet* createMediaQuerySet();
117     StyleRuleBase* createImportRule(const CSSParserString&, MediaQuerySet*);
118     StyleKeyframe* createKeyframe(CSSParserValueList*);
119     StyleRuleKeyframes* createKeyframesRule(const String&, PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > >, bool isPrefixed);
120
121     typedef WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > RuleList;
122     StyleRuleBase* createMediaRule(MediaQuerySet*, RuleList*);
123     RuleList* createRuleList();
124     RuleList* appendRule(RuleList*, StyleRuleBase*);
125     StyleRuleBase* createStyleRule(Vector<OwnPtr<CSSParserSelector> >* selectors);
126     StyleRuleBase* createFontFaceRule();
127     StyleRuleBase* createPageRule(PassOwnPtr<CSSParserSelector> pageSelector);
128     StyleRuleBase* createMarginAtRule(CSSSelector::MarginBoxType);
129     StyleRuleBase* createSupportsRule(bool conditionIsSupported, RuleList*);
130     void markSupportsRuleHeaderStart();
131     void markSupportsRuleHeaderEnd();
132     PassRefPtrWillBeRawPtr<CSSRuleSourceData> popSupportsRuleData();
133     StyleRuleBase* createHostRule(RuleList* rules);
134
135     void startDeclarationsForMarginBox();
136     void endDeclarationsForMarginBox();
137
138     MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
139     PassOwnPtrWillBeRawPtr<MediaQueryExp> sinkFloatingMediaQueryExp(MediaQueryExp*);
140     WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> >* createFloatingMediaQueryExpList();
141     PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > > sinkFloatingMediaQueryExpList(WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> >*);
142     MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const AtomicString&, PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > >);
143     MediaQuery* createFloatingMediaQuery(PassOwnPtrWillBeRawPtr<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > >);
144     MediaQuery* createFloatingNotAllQuery();
145     PassOwnPtrWillBeRawPtr<MediaQuery> sinkFloatingMediaQuery(MediaQuery*);
146
147     WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> >* createFloatingKeyframeVector();
148     PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > > sinkFloatingKeyframeVector(WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> >*);
149
150     void addNamespace(const AtomicString& prefix, const AtomicString& uri);
151     QualifiedName determineNameInNamespace(const AtomicString& prefix, const AtomicString& localName);
152
153     CSSParserSelector* rewriteSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*, bool isNamespacePlaceholder = false);
154     CSSParserSelector* rewriteSpecifiersWithElementNameForCustomPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
155     CSSParserSelector* rewriteSpecifiersWithElementNameForContentPseudoElement(const QualifiedName& tag, const AtomicString& elementName, CSSParserSelector* specifiers, bool tagIsForNamespaceRule);
156     CSSParserSelector* rewriteSpecifiersWithNamespaceIfNeeded(CSSParserSelector*);
157     CSSParserSelector* rewriteSpecifiers(CSSParserSelector*, CSSParserSelector*);
158     CSSParserSelector* rewriteSpecifiersForShadowDistributed(CSSParserSelector* specifiers, CSSParserSelector* distributedPseudoElementSelector);
159
160     void invalidBlockHit();
161
162     Vector<OwnPtr<CSSParserSelector> >* reusableSelectorVector() { return &m_reusableSelectorVector; }
163
164     void clearProperties();
165
166     PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet();
167
168     CSSParserContext m_context;
169
170     bool m_important;
171     CSSPropertyID m_id;
172     RawPtrWillBeMember<StyleSheetContents> m_styleSheet;
173     RefPtrWillBeMember<StyleRuleBase> m_rule;
174     RefPtrWillBeMember<StyleKeyframe> m_keyframe;
175     OwnPtr<CSSParserValueList> m_valueList;
176     bool m_supportsCondition;
177
178     WillBeHeapVector<CSSProperty, 256> m_parsedProperties;
179     CSSSelectorList* m_selectorListForParseSelector;
180
181     unsigned m_numParsedPropertiesBeforeMarginBox;
182
183     bool m_hadSyntacticallyValidCSSRule;
184     bool m_logErrors;
185     bool m_ignoreErrors;
186
187     AtomicString m_defaultNamespace;
188
189     // tokenizer methods and data
190     CSSParserObserver* m_observer;
191
192     // Local functions which just call into CSSParserObserver if non-null.
193     void startRule();
194     void endRule(bool valid);
195     void startRuleHeader(CSSRuleSourceData::Type);
196     void endRuleHeader();
197     void startSelector();
198     void endSelector();
199     void startRuleBody();
200     void startProperty();
201     void endProperty(bool isImportantFound, bool isPropertyParsed, CSSParserError = NoCSSError);
202     void startEndUnknownRule();
203
204     void endInvalidRuleHeader();
205     void reportError(const CSSParserLocation&, CSSParserError = GeneralCSSError);
206     void resumeErrorLogging() { m_ignoreErrors = false; }
207     void setLocationLabel(const CSSParserLocation& location) { m_locationLabel = location; }
208     const CSSParserLocation& lastLocationLabel() const { return m_locationLabel; }
209
210     void tokenToLowerCase(CSSParserString& token);
211
212     void markViewportRuleBodyStart() { m_inViewport = true; }
213     void markViewportRuleBodyEnd() { m_inViewport = false; }
214     StyleRuleBase* createViewportRule();
215
216     CSSParserLocation currentLocation() { return m_tokenizer.currentLocation(); }
217
218 private:
219     class StyleDeclarationScope {
220         STACK_ALLOCATED();
221         WTF_MAKE_NONCOPYABLE(StyleDeclarationScope);
222     public:
223         StyleDeclarationScope(BisonCSSParser* parser, const StylePropertySet* declaration)
224             : m_parser(parser)
225             , m_mode(declaration->cssParserMode())
226         {
227             if (isCSSViewportParsingEnabledForMode(m_mode)) {
228                 ASSERT(!m_parser->inViewport());
229                 m_parser->markViewportRuleBodyStart();
230             }
231         }
232
233         ~StyleDeclarationScope()
234         {
235             if (isCSSViewportParsingEnabledForMode(m_mode))
236                 m_parser->markViewportRuleBodyEnd();
237         }
238
239     private:
240         BisonCSSParser* m_parser;
241         CSSParserMode m_mode;
242     };
243
244     inline void ensureLineEndings();
245
246     void setStyleSheet(StyleSheetContents* styleSheet) { m_styleSheet = styleSheet; }
247
248     bool inViewport() const { return m_inViewport; }
249
250     void recheckAtKeyword(const UChar* str, int len);
251
252     template<unsigned prefixLength, unsigned suffixLength>
253     inline void setupParser(const char (&prefix)[prefixLength], const String& string, const char (&suffix)[suffixLength])
254     {
255         setupParser(prefix, prefixLength - 1, string, suffix, suffixLength - 1);
256     }
257     void setupParser(const char* prefix, unsigned prefixLength, const String&, const char* suffix, unsigned suffixLength);
258
259     bool parseValue(MutableStylePropertySet*, CSSPropertyID, const String&, bool important, StyleSheetContents* contextStyleSheet);
260     PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> parseDeclaration(const String&, StyleSheetContents* contextStyleSheet);
261
262     bool parseColor(const String&);
263
264     const String* m_source;
265     TextPosition m_startPosition;
266     CSSRuleSourceData::Type m_ruleHeaderType;
267     unsigned m_ruleHeaderStartOffset;
268     int m_ruleHeaderStartLineNumber;
269     OwnPtr<Vector<unsigned> > m_lineEndings;
270
271     bool m_ruleHasHeader;
272
273     bool m_allowImportRules;
274     bool m_allowNamespaceDeclarations;
275
276     bool m_inViewport;
277
278     CSSParserLocation m_locationLabel;
279
280     WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> > m_parsedRules;
281     WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > m_parsedKeyframes;
282     WillBeHeapVector<RefPtrWillBeMember<MediaQuerySet> > m_parsedMediaQuerySets;
283     WillBeHeapVector<OwnPtrWillBeMember<RuleList> > m_parsedRuleLists;
284     Vector<CSSParserSelector*> m_floatingSelectors;
285     Vector<Vector<OwnPtr<CSSParserSelector> >*> m_floatingSelectorVectors;
286     Vector<CSSParserValueList*> m_floatingValueLists;
287     Vector<CSSParserFunction*> m_floatingFunctions;
288
289     OwnPtrWillBeMember<MediaQuery> m_floatingMediaQuery;
290     OwnPtrWillBeMember<MediaQueryExp> m_floatingMediaQueryExp;
291     OwnPtrWillBeMember<WillBeHeapVector<OwnPtrWillBeMember<MediaQueryExp> > > m_floatingMediaQueryExpList;
292
293     OwnPtrWillBeMember<WillBeHeapVector<RefPtrWillBeMember<StyleKeyframe> > > m_floatingKeyframeVector;
294
295     Vector<OwnPtr<CSSParserSelector> > m_reusableSelectorVector;
296
297     OwnPtrWillBeMember<RuleSourceDataList> m_supportsRuleDataStack;
298
299     bool isLoggingErrors();
300     void logError(const String& message, const CSSParserLocation&);
301
302     CSSTokenizer m_tokenizer;
303
304     friend class TransformOperationInfo;
305     friend class FilterOperationInfo;
306 };
307
308 inline int cssyylex(void* yylval, BisonCSSParser* parser)
309 {
310     return parser->m_tokenizer.lex(yylval);
311 }
312
313 bool isValidNthToken(const CSSParserString&);
314
315 } // namespace blink
316
317 #endif // BisonCSSParser_h