/* * Copyright (C) 2003 Lars Knoll (knoll@kde.org) * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved. * Copyright (C) 2008 Eric Seidel * Copyright (C) 2009 - 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef CSSParser_h #define CSSParser_h #include "CSSCalculationValue.h" #include "CSSGradientValue.h" #include "CSSParserMode.h" #include "CSSParserValues.h" #include "CSSProperty.h" #include "CSSPropertyNames.h" #include "CSSPropertySourceData.h" #include "CSSSelector.h" #include "Color.h" #include "MediaQuery.h" #include #include #include #include #include #if ENABLE(CSS_FILTERS) #include "WebKitCSSFilterValue.h" #endif namespace WebCore { class CSSBorderImageSliceValue; class CSSPrimitiveValue; class CSSProperty; class CSSSelectorList; class CSSValue; class CSSValueList; class CSSBasicShape; class Document; class Element; class MediaQueryExp; class MediaQuerySet; class StyleKeyframe; class StylePropertySet; class StylePropertyShorthand; class StyleRuleBase; class StyleRuleKeyframes; class StyleKeyframe; class StyleSheetContents; class StyledElement; #if ENABLE(CSS_SHADERS) class WebKitCSSArrayFunctionValue; class WebKitCSSMixFunctionValue; #endif class CSSParser { friend inline int cssyylex(void*, CSSParser*); public: CSSParser(const CSSParserContext&); ~CSSParser(); void parseSheet(StyleSheetContents*, const String&, int startLineNumber = 0, RuleSourceDataList* = 0); PassRefPtr parseRule(StyleSheetContents*, const String&); PassRefPtr parseKeyframeRule(StyleSheetContents*, const String&); static bool parseValue(StylePropertySet*, CSSPropertyID, const String&, bool important, CSSParserMode, StyleSheetContents*); static bool parseColor(RGBA32& color, const String&, bool strict = false); static bool parseSystemColor(RGBA32& color, const String&, Document*); static PassRefPtr parseFontFaceValue(const AtomicString&); PassRefPtr parseValidPrimitive(int ident, CSSParserValue*); bool parseDeclaration(StylePropertySet*, const String&, PassRefPtr, StyleSheetContents* contextStyleSheet); static PassRefPtr parseInlineStyleDeclaration(const String&, Element*); PassOwnPtr parseMediaQuery(const String&); void addProperty(CSSPropertyID, PassRefPtr, bool important, bool implicit = false); void rollbackLastProperties(int num); bool hasProperties() const { return !m_parsedProperties->isEmpty(); } bool parseValue(CSSPropertyID, bool important); bool parseShorthand(CSSPropertyID, const StylePropertyShorthand&, bool important); bool parse4Values(CSSPropertyID, const CSSPropertyID* properties, bool important); bool parseContent(CSSPropertyID, bool important); bool parseQuotes(CSSPropertyID, bool important); #if ENABLE(CSS_VARIABLES) static bool parseValue(StylePropertySet*, CSSPropertyID, const String&, bool important, Document*); bool cssVariablesEnabled() const; void storeVariableDeclaration(const CSSParserString&, PassOwnPtr, bool important); #endif PassRefPtr parseAttr(CSSParserValueList* args); PassRefPtr parseBackgroundColor(); bool parseFillImage(CSSParserValueList*, RefPtr&); enum FillPositionFlag { InvalidFillPosition = 0, AmbiguousFillPosition = 1, XFillPosition = 2, YFillPosition = 4 }; PassRefPtr parseFillPositionComponent(CSSParserValueList*, unsigned& cumulativeFlags, FillPositionFlag& individualFlag); PassRefPtr parseFillPositionX(CSSParserValueList*); PassRefPtr parseFillPositionY(CSSParserValueList*); void parseFillPosition(CSSParserValueList*, RefPtr&, RefPtr&); void parseFillRepeat(RefPtr&, RefPtr&); PassRefPtr parseFillSize(CSSPropertyID, bool &allowComma); bool parseFillProperty(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtr&, RefPtr&); bool parseFillShorthand(CSSPropertyID, const CSSPropertyID* properties, int numProperties, bool important); void addFillValue(RefPtr& lval, PassRefPtr rval); void addAnimationValue(RefPtr& lval, PassRefPtr rval); PassRefPtr parseAnimationDelay(); PassRefPtr parseAnimationDirection(); PassRefPtr parseAnimationDuration(); PassRefPtr parseAnimationFillMode(); PassRefPtr parseAnimationIterationCount(); PassRefPtr parseAnimationName(); PassRefPtr parseAnimationPlayState(); PassRefPtr parseAnimationProperty(); PassRefPtr parseAnimationTimingFunction(); bool parseTransformOriginShorthand(RefPtr&, RefPtr&, RefPtr&); bool parseCubicBezierTimingFunctionValue(CSSParserValueList*& args, double& result); bool parseAnimationProperty(CSSPropertyID, RefPtr&); bool parseTransitionShorthand(bool important); bool parseAnimationShorthand(bool important); bool cssGridLayoutEnabled() const; bool parseGridTrackList(CSSPropertyID, bool important); bool parseDashboardRegions(CSSPropertyID, bool important); bool parseClipShape(CSSPropertyID, bool important); bool parseBasicShape(CSSPropertyID, bool important); PassRefPtr parseBasicShapeRectangle(CSSParserValueList* args); PassRefPtr parseBasicShapeCircle(CSSParserValueList* args); PassRefPtr parseBasicShapeEllipse(CSSParserValueList* args); PassRefPtr parseBasicShapePolygon(CSSParserValueList* args); bool parseFont(bool important); PassRefPtr parseFontFamily(); bool parseCounter(CSSPropertyID, int defaultValue, bool important); PassRefPtr parseCounterContent(CSSParserValueList* args, bool counters); bool parseColorParameters(CSSParserValue*, int* colorValues, bool parseAlpha); bool parseHSLParameters(CSSParserValue*, double* colorValues, bool parseAlpha); PassRefPtr parseColor(CSSParserValue* = 0); bool parseColorFromValue(CSSParserValue*, RGBA32&); void parseSelector(const String&, CSSSelectorList&); static bool fastParseColor(RGBA32&, const String&, bool strict); bool parseLineHeight(bool important); bool parseFontSize(bool important); bool parseFontVariant(bool important); bool parseFontWeight(bool important); bool parseFontFaceSrc(); bool parseFontFaceUnicodeRange(); #if ENABLE(SVG) bool parseSVGValue(CSSPropertyID propId, bool important); PassRefPtr parseSVGPaint(); PassRefPtr parseSVGColor(); PassRefPtr parseSVGStrokeDasharray(); #endif // CSS3 Parsing Routines (for properties specific to CSS3) PassRefPtr parseShadow(CSSParserValueList*, CSSPropertyID); bool parseBorderImage(CSSPropertyID, RefPtr&, bool important = false); bool parseBorderImageRepeat(RefPtr&); bool parseBorderImageSlice(CSSPropertyID, RefPtr&); bool parseBorderImageWidth(RefPtr&); bool parseBorderImageOutset(RefPtr&); bool parseBorderRadius(CSSPropertyID, bool important); bool parseAspectRatio(bool important); bool parseReflect(CSSPropertyID, bool important); bool parseFlex(CSSParserValueList* args, bool important); // Image generators bool parseCanvas(CSSParserValueList*, RefPtr&); bool parseDeprecatedGradient(CSSParserValueList*, RefPtr&); bool parseLinearGradient(CSSParserValueList*, RefPtr&, CSSGradientRepeat repeating); bool parseRadialGradient(CSSParserValueList*, RefPtr&, CSSGradientRepeat repeating); bool parseGradientColorStops(CSSParserValueList*, CSSGradientValue*, bool expectComma); bool parseCrossfade(CSSParserValueList*, RefPtr&); #if ENABLE(CSS_IMAGE_RESOLUTION) PassRefPtr parseImageResolution(CSSParserValueList*); #endif #if ENABLE(CSS_IMAGE_SET) PassRefPtr parseImageSet(CSSParserValueList*); #endif #if ENABLE(CSS_FILTERS) PassRefPtr parseFilter(); PassRefPtr parseBuiltinFilterArguments(CSSParserValueList*, WebKitCSSFilterValue::FilterOperationType); #if ENABLE(CSS_SHADERS) PassRefPtr parseCustomFilterArrayFunction(CSSParserValue*); PassRefPtr parseMixFunction(CSSParserValue*); PassRefPtr parseCustomFilter(CSSParserValue*); PassRefPtr parseCustomFilterTransform(CSSParserValueList*); #endif #endif static bool isBlendMode(int ident); static bool isCompositeOperator(int ident); PassRefPtr parseTransform(); PassRefPtr parseTransformValue(CSSParserValue*); bool parseTransformOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, CSSPropertyID& propId3, RefPtr&, RefPtr&, RefPtr&); bool parsePerspectiveOrigin(CSSPropertyID propId, CSSPropertyID& propId1, CSSPropertyID& propId2, RefPtr&, RefPtr&); bool parseTextEmphasisStyle(bool important); void addTextDecorationProperty(CSSPropertyID, PassRefPtr, bool important); bool parseTextDecoration(CSSPropertyID propId, bool important); bool parseLineBoxContain(bool important); bool parseCalculation(CSSParserValue*, CalculationPermittedValueRange); bool parseFontFeatureTag(CSSValueList*); bool parseFontFeatureSettings(bool important); bool cssRegionsEnabled() const; bool parseFlowThread(const String& flowName); bool parseFlowThread(CSSPropertyID, bool important); bool parseRegionThread(CSSPropertyID, bool important); bool parseFontVariantLigatures(bool important); CSSParserSelector* createFloatingSelector(); PassOwnPtr sinkFloatingSelector(CSSParserSelector*); CSSSelectorVector* createFloatingSelectorVector(); PassOwnPtr sinkFloatingSelectorVector(CSSSelectorVector*); CSSParserValueList* createFloatingValueList(); PassOwnPtr sinkFloatingValueList(CSSParserValueList*); CSSParserFunction* createFloatingFunction(); PassOwnPtr sinkFloatingFunction(CSSParserFunction*); CSSParserValue& sinkFloatingValue(CSSParserValue&); MediaQuerySet* createMediaQuerySet(); StyleRuleBase* createImportRule(const CSSParserString&, MediaQuerySet*); StyleKeyframe* createKeyframe(CSSParserValueList*); StyleRuleKeyframes* createKeyframesRule(const String&, PassOwnPtr > >); typedef Vector > RuleList; StyleRuleBase* createMediaRule(MediaQuerySet*, RuleList*); RuleList* createRuleList(); StyleRuleBase* createStyleRule(CSSSelectorVector* selectors); StyleRuleBase* createFontFaceRule(); StyleRuleBase* createPageRule(PassOwnPtr pageSelector); StyleRuleBase* createRegionRule(CSSSelectorVector* regionSelector, RuleList* rules); StyleRuleBase* createMarginAtRule(CSSSelector::MarginBoxType); void startDeclarationsForMarginBox(); void endDeclarationsForMarginBox(); MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, CSSParserValueList*); PassOwnPtr sinkFloatingMediaQueryExp(MediaQueryExp*); Vector >* createFloatingMediaQueryExpList(); PassOwnPtr > > sinkFloatingMediaQueryExpList(Vector >*); MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, PassOwnPtr > >); MediaQuery* createFloatingMediaQuery(PassOwnPtr > >); PassOwnPtr sinkFloatingMediaQuery(MediaQuery*); Vector >* createFloatingKeyframeVector(); PassOwnPtr > > sinkFloatingKeyframeVector(Vector >*); void addNamespace(const AtomicString& prefix, const AtomicString& uri); QualifiedName determineNameInNamespace(const AtomicString& prefix, const AtomicString& localName); void updateSpecifiersWithElementName(const AtomicString& namespacePrefix, const AtomicString& elementName, CSSParserSelector*); CSSParserSelector* updateSpecifiers(CSSParserSelector*, CSSParserSelector*); void invalidBlockHit(); CSSSelectorVector* selectorVector() { return m_selectorVector.get(); } void setReusableRegionSelectorVector(CSSSelectorVector* selectors); CSSSelectorVector* reusableRegionSelectorVector() { return &m_reusableRegionSelectorVector; } void updateLastSelectorLineAndPosition(); void updateLastMediaLine(MediaQuerySet*); void clearProperties(); PassRefPtr createStylePropertySet(); CSSParserContext m_context; bool m_important; CSSPropertyID m_id; StyleSheetContents* m_styleSheet; RefPtr m_rule; RefPtr m_keyframe; OwnPtr m_mediaQuery; OwnPtr m_valueList; typedef Vector ParsedPropertyVector; OwnPtr m_parsedProperties; CSSSelectorList* m_selectorListForParseSelector; unsigned m_numParsedPropertiesBeforeMarginBox; int m_inParseShorthand; CSSPropertyID m_currentShorthand; bool m_implicitShorthand; bool m_hasFontFaceOnlyValues; bool m_hadSyntacticallyValidCSSRule; AtomicString m_defaultNamespace; // tokenizer methods and data size_t m_parsedTextPrefixLength; SourceRange m_propertyRange; OwnPtr m_currentRuleDataStack; RuleSourceDataList* m_ruleSourceDataResult; void fixUnparsedPropertyRanges(CSSRuleSourceData*); void markRuleHeaderStart(CSSRuleSourceData::Type); void markRuleHeaderEnd(); void markRuleBodyStart(); void markRuleBodyEnd(); void markPropertyStart(); void markPropertyEnd(bool isImportantFound, bool isPropertyParsed); void processAndAddNewRuleToSourceTreeIfNeeded(); void addNewRuleToSourceTree(PassRefPtr); PassRefPtr popRuleData(); void resetPropertyRange() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; } bool isExtractingSourceData() const { return !!m_currentRuleDataStack; } inline int lex(void* yylval) { return (this->*m_lexFunc)(yylval); } int token() { return m_token; } PassRefPtr createPrimitiveNumericValue(CSSParserValue*); PassRefPtr createPrimitiveStringValue(CSSParserValue*); static KURL completeURL(const CSSParserContext&, const String& url); private: bool is8BitSource() { return m_is8BitSource; } template int realLex(void* yylval); UChar*& currentCharacter16(); template inline CharacterType*& currentCharacter(); template inline CharacterType* tokenStart(); template inline void setTokenStart(CharacterType*); inline unsigned tokenStartOffset(); inline UChar tokenStartChar(); template inline bool isIdentifierStart(); template static inline CharacterType* checkAndSkipString(CharacterType*, int); template unsigned parseEscape(CharacterType*&); template inline void UnicodeToChars(DestCharacterType*&, unsigned); template inline bool parseIdentifierInternal(SrcCharacterType*&, DestCharacterType*&, bool&); template inline void parseIdentifier(CharacterType*&, CSSParserString&, bool&); template inline bool parseStringInternal(SrcCharacterType*&, DestCharacterType*&, UChar); template inline void parseString(CharacterType*&, CSSParserString& resultString, UChar); template inline bool parseURIInternal(CharacterType*&, CharacterType*&); template inline void parseURI(CSSParserString&); template inline bool parseUnicodeRange(); template bool parseNthChild(); template bool parseNthChildExtra(); template inline void detectFunctionTypeToken(int); template inline void detectMediaQueryToken(int); template inline void detectNumberToken(CharacterType*, int); template inline void detectDashToken(int); template inline void detectAtToken(int, bool); template inline void setRuleHeaderEnd(const CharacterType*); void setStyleSheet(StyleSheetContents*); inline bool inStrictMode() const { return m_context.mode == CSSStrictMode || m_context.mode == SVGAttributeMode; } inline bool inQuirksMode() const { return m_context.mode == CSSQuirksMode; } KURL completeURL(const String& url) const; void recheckAtKeyword(const UChar* str, int len); void setupParser(const char* prefix, const String&, const char* suffix); bool inShorthand() const { return m_inParseShorthand; } bool validWidth(CSSParserValue*); bool validHeight(CSSParserValue*); void checkForOrphanedUnits(); void deleteFontFaceOnlyValues(); bool isGeneratedImageValue(CSSParserValue*) const; bool parseGeneratedImage(CSSParserValueList*, RefPtr&); bool parseValue(StylePropertySet*, CSSPropertyID, const String&, bool important, StyleSheetContents* contextStyleSheet); PassRefPtr parseDeclaration(const String&, StyleSheetContents* contextStyleSheet); enum SizeParameterType { None, Auto, Length, PageSize, Orientation, }; bool parsePage(CSSPropertyID propId, bool important); bool parseSize(CSSPropertyID propId, bool important); SizeParameterType parseSizeParameter(CSSValueList* parsedValues, CSSParserValue* value, SizeParameterType prevParamType); bool parseFontFaceSrcURI(CSSValueList*); bool parseFontFaceSrcLocal(CSSValueList*); bool parseColor(const String&); enum ParsingMode { NormalMode, MediaQueryMode, NthChildMode }; ParsingMode m_parsingMode; bool m_is8BitSource; OwnArrayPtr m_dataStart8; OwnArrayPtr m_dataStart16; LChar* m_currentCharacter8; UChar* m_currentCharacter16; union { LChar* ptr8; UChar* ptr16; } m_tokenStart; unsigned m_length; int m_token; int m_lineNumber; int m_lastSelectorLineNumber; bool m_allowImportRules; bool m_allowNamespaceDeclarations; int (CSSParser::*m_lexFunc)(void*); Vector > m_parsedRules; Vector > m_parsedKeyframes; Vector > m_parsedMediaQuerySets; Vector > m_parsedRuleLists; HashSet m_floatingSelectors; HashSet m_floatingSelectorVectors; HashSet m_floatingValueLists; HashSet m_floatingFunctions; OwnPtr m_floatingMediaQuery; OwnPtr m_floatingMediaQueryExp; OwnPtr > > m_floatingMediaQueryExpList; OwnPtr > > m_floatingKeyframeVector; OwnPtr m_selectorVector; CSSSelectorVector m_reusableRegionSelectorVector; RefPtr m_parsedCalculation; // defines units allowed for a certain property, used in parseUnit enum Units { FUnknown = 0x0000, FInteger = 0x0001, FNumber = 0x0002, // Real Numbers FPercent = 0x0004, FLength = 0x0008, FAngle = 0x0010, FTime = 0x0020, FFrequency = 0x0040, FRelative = 0x0100, #if ENABLE(CSS_IMAGE_RESOLUTION) FResolution= 0x0200, #endif FNonNeg = 0x0400 }; friend inline Units operator|(Units a, Units b) { return static_cast(static_cast(a) | static_cast(b)); } bool validCalculationUnit(CSSParserValue*, Units); bool shouldAcceptUnitLessValues(CSSParserValue*, Units, CSSParserMode); inline bool validUnit(CSSParserValue* value, Units unitflags) { return validUnit(value, unitflags, m_context.mode); } bool validUnit(CSSParserValue*, Units, CSSParserMode); bool parseBorderImageQuad(Units, RefPtr&); int colorIntFromValue(CSSParserValue*); enum ReleaseParsedCalcValueCondition { ReleaseParsedCalcValue, DoNotReleaseParsedCalcValue }; double parsedDouble(CSSParserValue*, ReleaseParsedCalcValueCondition releaseCalc = DoNotReleaseParsedCalcValue); bool isCalculation(CSSParserValue*); friend class TransformOperationInfo; #if ENABLE(CSS_FILTERS) friend class FilterOperationInfo; #endif }; CSSPropertyID cssPropertyID(const CSSParserString&); CSSPropertyID cssPropertyID(const String&); int cssValueKeywordID(const CSSParserString&); #if PLATFORM(IOS) void cssPropertyNameIOSAliasing(const char* propertyName, const char*& propertyNameAlias, unsigned& newLength); #endif class ShorthandScope { WTF_MAKE_FAST_ALLOCATED; public: ShorthandScope(CSSParser* parser, CSSPropertyID propId) : m_parser(parser) { if (!(m_parser->m_inParseShorthand++)) m_parser->m_currentShorthand = propId; } ~ShorthandScope() { if (!(--m_parser->m_inParseShorthand)) m_parser->m_currentShorthand = CSSPropertyInvalid; } private: CSSParser* m_parser; }; String quoteCSSString(const String&); String quoteCSSStringIfNeeded(const String&); String quoteCSSURLIfNeeded(const String&); bool isValidNthToken(const CSSParserString&); template <> inline void CSSParser::setTokenStart(LChar* tokenStart) { m_tokenStart.ptr8 = tokenStart; } template <> inline void CSSParser::setTokenStart(UChar* tokenStart) { m_tokenStart.ptr16 = tokenStart; } inline unsigned CSSParser::tokenStartOffset() { if (is8BitSource()) return m_tokenStart.ptr8 - m_dataStart8.get(); return m_tokenStart.ptr16 - m_dataStart16.get(); } inline UChar CSSParser::tokenStartChar() { if (is8BitSource()) return *m_tokenStart.ptr8; return *m_tokenStart.ptr16; } inline int cssyylex(void* yylval, CSSParser* parser) { return parser->lex(yylval); } } // namespace WebCore #endif // CSSParser_h