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