WebCore/css/CSSParser.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
       
     3  * Copyright (C) 2004, 2005, 2006, 2008 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 CSSParser_h
       
    24 #define CSSParser_h
       
    25 
       
    26 #include "AtomicString.h"
       
    27 #include "Color.h"
       
    28 #include "CSSParserValues.h"
       
    29 #include "CSSSelectorList.h"
       
    30 #include "MediaQuery.h"
       
    31 #include <wtf/HashMap.h>
       
    32 #include <wtf/HashSet.h>
       
    33 #include <wtf/Vector.h>
       
    34 
       
    35 namespace WebCore {
       
    36 
       
    37     class CSSMutableStyleDeclaration;
       
    38     class CSSPrimitiveValue;
       
    39     class CSSProperty;
       
    40     class CSSRule;
       
    41     class CSSRuleList;
       
    42     class CSSSelector;
       
    43     class CSSStyleRule;
       
    44     class CSSStyleSheet;
       
    45     class CSSValue;
       
    46     class CSSValueList;
       
    47     class CSSVariablesDeclaration;
       
    48     class Document;
       
    49     class MediaList;
       
    50     class MediaQueryExp;
       
    51     class StyleBase;
       
    52     class StyleList;
       
    53     class WebKitCSSKeyframeRule;
       
    54     class WebKitCSSKeyframesRule;
       
    55 
       
    56     class CSSParser {
       
    57     public:
       
    58         typedef HashMap<CSSStyleRule*, std::pair<unsigned, unsigned> > StyleRuleRanges;
       
    59 
       
    60         CSSParser(bool strictParsing = true);
       
    61         ~CSSParser();
       
    62 
       
    63         void parseSheet(CSSStyleSheet*, const String&, int startLineNumber = 0, StyleRuleRanges* ruleRangeMap = 0);
       
    64         PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
       
    65         PassRefPtr<CSSRule> parseKeyframeRule(CSSStyleSheet*, const String&);
       
    66         bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important);
       
    67         static bool parseColor(RGBA32& color, const String&, bool strict = false);
       
    68         bool parseColor(CSSMutableStyleDeclaration*, const String&);
       
    69         bool parseDeclaration(CSSMutableStyleDeclaration*, const String&);
       
    70         bool parseMediaQuery(MediaList*, const String&);
       
    71 
       
    72         Document* document() const;
       
    73 
       
    74         void addProperty(int propId, PassRefPtr<CSSValue>, bool important);
       
    75         void rollbackLastProperties(int num);
       
    76         bool hasProperties() const { return m_numParsedProperties > 0; }
       
    77 
       
    78         bool parseValue(int propId, bool important);
       
    79         bool parseShorthand(int propId, const int* properties, int numProperties, bool important);
       
    80         bool parse4Values(int propId, const int* properties, bool important);
       
    81         bool parseContent(int propId, bool important);
       
    82 
       
    83         PassRefPtr<CSSValue> parseAttr(CSSParserValueList* args);
       
    84 
       
    85         PassRefPtr<CSSValue> parseBackgroundColor();
       
    86 
       
    87         bool parseFillImage(RefPtr<CSSValue>&);
       
    88         PassRefPtr<CSSValue> parseFillPositionXY(bool& xFound, bool& yFound);
       
    89         void parseFillPosition(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
    90         void parseFillRepeat(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
    91         PassRefPtr<CSSValue> parseFillSize(int propId, bool &allowComma);
       
    92 
       
    93         bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
    94         bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important);
       
    95 
       
    96         void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
       
    97 
       
    98         void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
       
    99 
       
   100         PassRefPtr<CSSValue> parseAnimationDelay();
       
   101         PassRefPtr<CSSValue> parseAnimationDirection();
       
   102         PassRefPtr<CSSValue> parseAnimationDuration();
       
   103         PassRefPtr<CSSValue> parseAnimationFillMode();
       
   104         PassRefPtr<CSSValue> parseAnimationIterationCount();
       
   105         PassRefPtr<CSSValue> parseAnimationName();
       
   106         PassRefPtr<CSSValue> parseAnimationPlayState();
       
   107         PassRefPtr<CSSValue> parseAnimationProperty();
       
   108         PassRefPtr<CSSValue> parseAnimationTimingFunction();
       
   109 
       
   110         void parseTransformOriginShorthand(RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
   111         bool parseTimingFunctionValue(CSSParserValueList*& args, double& result);
       
   112         bool parseAnimationProperty(int propId, RefPtr<CSSValue>&);
       
   113         bool parseTransitionShorthand(bool important);
       
   114         bool parseAnimationShorthand(bool important);
       
   115 
       
   116         bool parseDashboardRegions(int propId, bool important);
       
   117 
       
   118         bool parseShape(int propId, bool important);
       
   119 
       
   120         bool parseFont(bool important);
       
   121         PassRefPtr<CSSValueList> parseFontFamily();
       
   122 
       
   123         bool parseCounter(int propId, int defaultValue, bool important);
       
   124         PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters);
       
   125 
       
   126         bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha);
       
   127         bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha);
       
   128         PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0);
       
   129         bool parseColorFromValue(CSSParserValue*, RGBA32&);
       
   130         void parseSelector(const String&, Document* doc, CSSSelectorList&);
       
   131 
       
   132         static bool parseColor(const String&, RGBA32& rgb, bool strict);
       
   133 
       
   134         bool parseFontStyle(bool important);
       
   135         bool parseFontVariant(bool important);
       
   136         bool parseFontWeight(bool important);
       
   137         bool parseFontFaceSrc();
       
   138         bool parseFontFaceUnicodeRange();
       
   139 
       
   140 #if ENABLE(SVG)
       
   141         bool parseSVGValue(int propId, bool important);
       
   142         PassRefPtr<CSSValue> parseSVGPaint();
       
   143         PassRefPtr<CSSValue> parseSVGColor();
       
   144         PassRefPtr<CSSValue> parseSVGStrokeDasharray();
       
   145 #endif
       
   146 
       
   147 #if ENABLE(WCSS)
       
   148         PassRefPtr<CSSValue> parseWCSSInputProperty();
       
   149 #endif
       
   150 
       
   151         // CSS3 Parsing Routines (for properties specific to CSS3)
       
   152         bool parseShadow(int propId, bool important);
       
   153         bool parseBorderImage(int propId, bool important, RefPtr<CSSValue>&);
       
   154         bool parseBorderRadius(int propId, bool important);
       
   155 
       
   156         bool parseReflect(int propId, bool important);
       
   157 
       
   158         // Image generators
       
   159         bool parseCanvas(RefPtr<CSSValue>&);
       
   160         bool parseGradient(RefPtr<CSSValue>&);
       
   161 
       
   162         PassRefPtr<CSSValueList> parseTransform();
       
   163         bool parseTransformOrigin(int propId, int& propId1, int& propId2, int& propId3, RefPtr<CSSValue>&, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
   164         bool parsePerspectiveOrigin(int propId, int& propId1, int& propId2,  RefPtr<CSSValue>&, RefPtr<CSSValue>&);
       
   165         bool parseVariable(CSSVariablesDeclaration*, const String& variableName, const String& variableValue);
       
   166         void parsePropertyWithResolvedVariables(int propId, bool important, CSSMutableStyleDeclaration*, CSSParserValueList*);
       
   167 
       
   168         int yyparse();
       
   169 
       
   170         CSSSelector* createFloatingSelector();
       
   171         CSSSelector* sinkFloatingSelector(CSSSelector*);
       
   172 
       
   173         CSSParserValueList* createFloatingValueList();
       
   174         CSSParserValueList* sinkFloatingValueList(CSSParserValueList*);
       
   175 
       
   176         CSSParserFunction* createFloatingFunction();
       
   177         CSSParserFunction* sinkFloatingFunction(CSSParserFunction*);
       
   178 
       
   179         CSSParserValue& sinkFloatingValue(CSSParserValue&);
       
   180 
       
   181         MediaList* createMediaList();
       
   182         CSSRule* createCharsetRule(const CSSParserString&);
       
   183         CSSRule* createImportRule(const CSSParserString&, MediaList*);
       
   184         WebKitCSSKeyframeRule* createKeyframeRule(CSSParserValueList*);
       
   185         WebKitCSSKeyframesRule* createKeyframesRule();
       
   186         CSSRule* createMediaRule(MediaList*, CSSRuleList*);
       
   187         CSSRuleList* createRuleList();
       
   188         CSSRule* createStyleRule(Vector<CSSSelector*>* selectors);
       
   189         CSSRule* createFontFaceRule();
       
   190         CSSRule* createVariablesRule(MediaList*, bool variablesKeyword);
       
   191         CSSRule* createPageRule(CSSSelector* pageSelector);
       
   192         CSSRule* createMarginAtRule(CSSSelector::MarginBoxType marginBox);
       
   193         void startDeclarationsForMarginBox();
       
   194         void endDeclarationsForMarginBox();
       
   195 
       
   196         MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*);
       
   197         MediaQueryExp* sinkFloatingMediaQueryExp(MediaQueryExp*);
       
   198         Vector<MediaQueryExp*>* createFloatingMediaQueryExpList();
       
   199         Vector<MediaQueryExp*>* sinkFloatingMediaQueryExpList(Vector<MediaQueryExp*>*);
       
   200         MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, Vector<MediaQueryExp*>*);
       
   201         MediaQuery* createFloatingMediaQuery(Vector<MediaQueryExp*>*);
       
   202         MediaQuery* sinkFloatingMediaQuery(MediaQuery*);
       
   203 
       
   204         void addNamespace(const AtomicString& prefix, const AtomicString& uri);
       
   205 
       
   206         bool addVariable(const CSSParserString&, CSSParserValueList*);
       
   207         bool addVariableDeclarationBlock(const CSSParserString&);
       
   208         bool checkForVariables(CSSParserValueList*);
       
   209         void addUnresolvedProperty(int propId, bool important);
       
   210         void invalidBlockHit();
       
   211 
       
   212         Vector<CSSSelector*>* reusableSelectorVector() { return &m_reusableSelectorVector; }
       
   213 
       
   214         void updateLastSelectorLineAndPosition();
       
   215 
       
   216         void clearProperties();
       
   217 
       
   218         bool m_strict;
       
   219         bool m_important;
       
   220         int m_id;
       
   221         CSSStyleSheet* m_styleSheet;
       
   222         RefPtr<CSSRule> m_rule;
       
   223         RefPtr<CSSRule> m_keyframe;
       
   224         MediaQuery* m_mediaQuery;
       
   225         CSSParserValueList* m_valueList;
       
   226         CSSProperty** m_parsedProperties;
       
   227         CSSSelectorList* m_selectorListForParseSelector;
       
   228         unsigned m_numParsedProperties;
       
   229         unsigned m_maxParsedProperties;
       
   230         unsigned m_numParsedPropertiesBeforeMarginBox;
       
   231 
       
   232         int m_inParseShorthand;
       
   233         int m_currentShorthand;
       
   234         bool m_implicitShorthand;
       
   235 
       
   236         bool m_hasFontFaceOnlyValues;
       
   237         bool m_hadSyntacticallyValidCSSRule;
       
   238 
       
   239         Vector<String> m_variableNames;
       
   240         Vector<RefPtr<CSSValue> > m_variableValues;
       
   241 
       
   242         AtomicString m_defaultNamespace;
       
   243 
       
   244         // tokenizer methods and data
       
   245         unsigned m_ruleBodyStartOffset;
       
   246         unsigned m_ruleBodyEndOffset;
       
   247         StyleRuleRanges* m_ruleRanges;
       
   248         void markRuleBodyStart();
       
   249         void markRuleBodyEnd();
       
   250         void resetRuleBodyMarks() { m_ruleBodyStartOffset = m_ruleBodyEndOffset = 0; }
       
   251         int lex(void* yylval);
       
   252         int token() { return yyTok; }
       
   253         UChar* text(int* length);
       
   254         void countLines();
       
   255         int lex();
       
   256 
       
   257     private:
       
   258         void recheckAtKeyword(const UChar* str, int len);
       
   259 
       
   260         void setupParser(const char* prefix, const String&, const char* suffix);
       
   261 
       
   262         bool inShorthand() const { return m_inParseShorthand; }
       
   263 
       
   264         void checkForOrphanedUnits();
       
   265 
       
   266         void clearVariables();
       
   267 
       
   268         void deleteFontFaceOnlyValues();
       
   269 
       
   270         enum SizeParameterType {
       
   271             None,
       
   272             Auto,
       
   273             Length,
       
   274             PageSize,
       
   275             Orientation,
       
   276         };
       
   277 
       
   278         bool parsePage(int propId, bool important);
       
   279         bool parseSize(int propId, bool important);
       
   280         SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType);
       
   281 
       
   282         UChar* m_data;
       
   283         UChar* yytext;
       
   284         UChar* yy_c_buf_p;
       
   285         UChar yy_hold_char;
       
   286         int yy_last_accepting_state;
       
   287         UChar* yy_last_accepting_cpos;
       
   288         int yyleng;
       
   289         int yyTok;
       
   290         int yy_start;
       
   291         int m_lineNumber;
       
   292         int m_lastSelectorLineNumber;
       
   293 
       
   294         bool m_allowImportRules;
       
   295         bool m_allowVariablesRules;
       
   296         bool m_allowNamespaceDeclarations;
       
   297 
       
   298         Vector<RefPtr<StyleBase> > m_parsedStyleObjects;
       
   299         Vector<RefPtr<CSSRuleList> > m_parsedRuleLists;
       
   300         HashSet<CSSSelector*> m_floatingSelectors;
       
   301         HashSet<CSSParserValueList*> m_floatingValueLists;
       
   302         HashSet<CSSParserFunction*> m_floatingFunctions;
       
   303 
       
   304         MediaQuery* m_floatingMediaQuery;
       
   305         MediaQueryExp* m_floatingMediaQueryExp;
       
   306         Vector<MediaQueryExp*>* m_floatingMediaQueryExpList;
       
   307 
       
   308         Vector<CSSSelector*> m_reusableSelectorVector;
       
   309 
       
   310         // defines units allowed for a certain property, used in parseUnit
       
   311         enum Units {
       
   312             FUnknown   = 0x0000,
       
   313             FInteger   = 0x0001,
       
   314             FNumber    = 0x0002,  // Real Numbers
       
   315             FPercent   = 0x0004,
       
   316             FLength    = 0x0008,
       
   317             FAngle     = 0x0010,
       
   318             FTime      = 0x0020,
       
   319             FFrequency = 0x0040,
       
   320             FRelative  = 0x0100,
       
   321             FNonNeg    = 0x0200
       
   322         };
       
   323 
       
   324         friend inline Units operator|(Units a, Units b)
       
   325         {
       
   326             return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
       
   327         }
       
   328 
       
   329         static bool validUnit(CSSParserValue*, Units, bool strict);
       
   330 
       
   331         friend class TransformOperationInfo;
       
   332     };
       
   333 
       
   334     int cssPropertyID(const CSSParserString&);
       
   335     int cssPropertyID(const String&);
       
   336     int cssValueKeywordID(const CSSParserString&);
       
   337 
       
   338     class ShorthandScope : public FastAllocBase {
       
   339     public:
       
   340         ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
       
   341         {
       
   342             if (!(m_parser->m_inParseShorthand++))
       
   343                 m_parser->m_currentShorthand = propId;
       
   344         }
       
   345         ~ShorthandScope()
       
   346         {
       
   347             if (!(--m_parser->m_inParseShorthand))
       
   348                 m_parser->m_currentShorthand = 0;
       
   349         }
       
   350 
       
   351     private:
       
   352         CSSParser* m_parser;
       
   353     };
       
   354 
       
   355     String quoteCSSString(const String&);
       
   356     String quoteCSSStringIfNeeded(const String&);
       
   357     String quoteCSSURLIfNeeded(const String&);
       
   358 
       
   359 } // namespace WebCore
       
   360 
       
   361 #endif // CSSParser_h