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 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 #ifndef CSSParser_h 23 #define CSSParser_h 24 25 #include "AtomicString.h" 26 #include "Color.h" 27 #include "CSSParserValues.h" 28 #include "CSSSelectorList.h" 29 #include "MediaQuery.h" 30 #include <wtf/HashSet.h> 31 #include <wtf/Vector.h> 32 33 namespace WebCore { 34 35 class CSSMutableStyleDeclaration; 36 class CSSPrimitiveValue; 37 class CSSProperty; 38 class CSSRule; 39 class CSSRuleList; 40 class CSSSelector; 41 class CSSStyleSheet; 42 class CSSValue; 43 class CSSValueList; 44 class CSSVariablesDeclaration; 45 class Document; 46 class MediaList; 47 class MediaQueryExp; 48 class StyleBase; 49 class StyleList; 50 class WebKitCSSKeyframeRule; 51 class WebKitCSSKeyframesRule; 52 53 class CSSParser { 54 public: 55 CSSParser(bool strictParsing = true); 56 ~CSSParser(); 57 58 void parseSheet(CSSStyleSheet*, const String&); 59 PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&); 60 PassRefPtr<CSSRule> parseKeyframeRule(CSSStyleSheet*, const String&); 61 bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important); 62 static bool parseColor(RGBA32& color, const String&, bool strict = false); 63 bool parseColor(CSSMutableStyleDeclaration*, const String&); 64 bool parseDeclaration(CSSMutableStyleDeclaration*, const String&); 65 bool parseMediaQuery(MediaList*, const String&); 66 67 Document* document() const; 68 69 void addProperty(int propId, PassRefPtr<CSSValue>, bool important); 70 void rollbackLastProperties(int num); hasProperties()71 bool hasProperties() const { return m_numParsedProperties > 0; } 72 73 bool parseValue(int propId, bool important); 74 bool parseShorthand(int propId, const int* properties, int numProperties, bool important); 75 bool parse4Values(int propId, const int* properties, bool important); 76 bool parseContent(int propId, bool important); 77 78 PassRefPtr<CSSValue> parseBackgroundColor(); 79 80 bool parseFillImage(RefPtr<CSSValue>&); 81 PassRefPtr<CSSValue> parseFillPositionXY(bool& xFound, bool& yFound); 82 void parseFillPosition(RefPtr<CSSValue>&, RefPtr<CSSValue>&); 83 PassRefPtr<CSSValue> parseFillSize(); 84 85 bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&); 86 bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important); 87 88 void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval); 89 90 void addAnimationValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval); 91 92 PassRefPtr<CSSValue> parseAnimationDelay(); 93 PassRefPtr<CSSValue> parseAnimationDirection(); 94 PassRefPtr<CSSValue> parseAnimationDuration(); 95 PassRefPtr<CSSValue> parseAnimationIterationCount(); 96 PassRefPtr<CSSValue> parseAnimationName(); 97 PassRefPtr<CSSValue> parseAnimationPlayState(); 98 PassRefPtr<CSSValue> parseAnimationProperty(); 99 PassRefPtr<CSSValue> parseAnimationTimingFunction(); 100 101 bool parseTimingFunctionValue(CSSParserValueList*& args, double& result); 102 bool parseAnimationProperty(int propId, RefPtr<CSSValue>&); 103 bool parseTransitionShorthand(bool important); 104 bool parseAnimationShorthand(bool important); 105 106 bool parseDashboardRegions(int propId, bool important); 107 108 bool parseShape(int propId, bool important); 109 110 bool parseFont(bool important); 111 PassRefPtr<CSSValueList> parseFontFamily(); 112 113 bool parseCounter(int propId, int defaultValue, bool important); 114 PassRefPtr<CSSValue> parseCounterContent(CSSParserValueList* args, bool counters); 115 116 bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha); 117 bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha); 118 PassRefPtr<CSSPrimitiveValue> parseColor(CSSParserValue* = 0); 119 bool parseColorFromValue(CSSParserValue*, RGBA32&, bool = false); 120 void parseSelector(const String&, Document* doc, CSSSelectorList&); 121 122 static bool parseColor(const String&, RGBA32& rgb, bool strict); 123 124 bool parseFontStyle(bool important); 125 bool parseFontVariant(bool important); 126 bool parseFontWeight(bool important); 127 bool parseFontFaceSrc(); 128 bool parseFontFaceUnicodeRange(); 129 130 #if ENABLE(SVG) 131 bool parseSVGValue(int propId, bool important); 132 PassRefPtr<CSSValue> parseSVGPaint(); 133 PassRefPtr<CSSValue> parseSVGColor(); 134 PassRefPtr<CSSValue> parseSVGStrokeDasharray(); 135 #endif 136 137 // CSS3 Parsing Routines (for properties specific to CSS3) 138 bool parseShadow(int propId, bool important); 139 bool parseBorderImage(int propId, bool important, RefPtr<CSSValue>&); 140 141 bool parseReflect(int propId, bool important); 142 143 // Image generators 144 bool parseCanvas(RefPtr<CSSValue>&); 145 bool parseGradient(RefPtr<CSSValue>&); 146 147 PassRefPtr<CSSValueList> parseTransform(); 148 bool parseTransformOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&); 149 150 bool parseVariable(CSSVariablesDeclaration*, const String& variableName, const String& variableValue); 151 void parsePropertyWithResolvedVariables(int propId, bool important, CSSMutableStyleDeclaration*, CSSParserValueList*); 152 153 int yyparse(); 154 155 CSSSelector* createFloatingSelector(); 156 CSSSelector* sinkFloatingSelector(CSSSelector*); 157 158 CSSParserValueList* createFloatingValueList(); 159 CSSParserValueList* sinkFloatingValueList(CSSParserValueList*); 160 161 CSSParserFunction* createFloatingFunction(); 162 CSSParserFunction* sinkFloatingFunction(CSSParserFunction*); 163 164 CSSParserValue& sinkFloatingValue(CSSParserValue&); 165 166 MediaList* createMediaList(); 167 CSSRule* createCharsetRule(const CSSParserString&); 168 CSSRule* createImportRule(const CSSParserString&, MediaList*); 169 WebKitCSSKeyframeRule* createKeyframeRule(CSSParserValueList*); 170 WebKitCSSKeyframesRule* createKeyframesRule(); 171 CSSRule* createMediaRule(MediaList*, CSSRuleList*); 172 CSSRuleList* createRuleList(); 173 CSSRule* createStyleRule(Vector<CSSSelector*>* selectors); 174 CSSRule* createFontFaceRule(); 175 CSSRule* createVariablesRule(MediaList*, bool variablesKeyword); 176 177 MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*); 178 MediaQueryExp* sinkFloatingMediaQueryExp(MediaQueryExp*); 179 Vector<MediaQueryExp*>* createFloatingMediaQueryExpList(); 180 Vector<MediaQueryExp*>* sinkFloatingMediaQueryExpList(Vector<MediaQueryExp*>*); 181 MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, Vector<MediaQueryExp*>*); 182 MediaQuery* createFloatingMediaQuery(Vector<MediaQueryExp*>*); 183 MediaQuery* sinkFloatingMediaQuery(MediaQuery*); 184 185 bool addVariable(const CSSParserString&, CSSParserValueList*); 186 bool addVariableDeclarationBlock(const CSSParserString&); 187 bool checkForVariables(CSSParserValueList*); 188 void addUnresolvedProperty(int propId, bool important); 189 reusableSelectorVector()190 Vector<CSSSelector*>* reusableSelectorVector() { return &m_reusableSelectorVector; } 191 192 public: 193 bool m_strict; 194 bool m_important; 195 int m_id; 196 CSSStyleSheet* m_styleSheet; 197 RefPtr<CSSRule> m_rule; 198 RefPtr<CSSRule> m_keyframe; 199 MediaQuery* m_mediaQuery; 200 CSSParserValueList* m_valueList; 201 CSSProperty** m_parsedProperties; 202 CSSSelectorList* m_selectorListForParseSelector; 203 int m_numParsedProperties; 204 int m_maxParsedProperties; 205 206 int m_inParseShorthand; 207 int m_currentShorthand; 208 bool m_implicitShorthand; 209 210 bool m_hasFontFaceOnlyValues; 211 212 Vector<String> m_variableNames; 213 Vector<RefPtr<CSSValue> > m_variableValues; 214 215 AtomicString m_defaultNamespace; 216 217 // tokenizer methods and data 218 public: 219 int lex(void* yylval); token()220 int token() { return yyTok; } 221 UChar* text(int* length); 222 int lex(); 223 224 private: 225 void clearProperties(); 226 227 void setupParser(const char* prefix, const String&, const char* suffix); 228 inShorthand()229 bool inShorthand() const { return m_inParseShorthand; } 230 231 void checkForOrphanedUnits(); 232 233 void clearVariables(); 234 235 void deleteFontFaceOnlyValues(); 236 237 UChar* m_data; 238 UChar* yytext; 239 UChar* yy_c_buf_p; 240 UChar yy_hold_char; 241 int yy_last_accepting_state; 242 UChar* yy_last_accepting_cpos; 243 int yyleng; 244 int yyTok; 245 int yy_start; 246 247 Vector<RefPtr<StyleBase> > m_parsedStyleObjects; 248 Vector<RefPtr<CSSRuleList> > m_parsedRuleLists; 249 HashSet<CSSSelector*> m_floatingSelectors; 250 HashSet<CSSParserValueList*> m_floatingValueLists; 251 HashSet<CSSParserFunction*> m_floatingFunctions; 252 253 MediaQuery* m_floatingMediaQuery; 254 MediaQueryExp* m_floatingMediaQueryExp; 255 Vector<MediaQueryExp*>* m_floatingMediaQueryExpList; 256 257 Vector<CSSSelector*> m_reusableSelectorVector; 258 259 // defines units allowed for a certain property, used in parseUnit 260 enum Units { 261 FUnknown = 0x0000, 262 FInteger = 0x0001, 263 FNumber = 0x0002, // Real Numbers 264 FPercent = 0x0004, 265 FLength = 0x0008, 266 FAngle = 0x0010, 267 FTime = 0x0020, 268 FFrequency = 0x0040, 269 FRelative = 0x0100, 270 FNonNeg = 0x0200 271 }; 272 273 friend inline Units operator|(Units a, Units b) 274 { 275 return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); 276 } 277 278 static bool validUnit(CSSParserValue*, Units, bool strict); 279 280 friend class TransformOperationInfo; 281 }; 282 283 int cssPropertyID(const CSSParserString&); 284 int cssPropertyID(const String&); 285 int cssValueKeywordID(const CSSParserString&); 286 287 class ShorthandScope { 288 public: ShorthandScope(CSSParser * parser,int propId)289 ShorthandScope(CSSParser* parser, int propId) : m_parser(parser) 290 { 291 if (!(m_parser->m_inParseShorthand++)) 292 m_parser->m_currentShorthand = propId; 293 } ~ShorthandScope()294 ~ShorthandScope() 295 { 296 if (!(--m_parser->m_inParseShorthand)) 297 m_parser->m_currentShorthand = 0; 298 } 299 300 private: 301 CSSParser* m_parser; 302 }; 303 304 } // namespace WebCore 305 306 #endif // CSSParser_h 307