Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* parsing of CSS stylesheets, based on a token stream from the CSS scanner */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef nsCSSParser_h___ |
michael@0 | 9 | #define nsCSSParser_h___ |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/Attributes.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsCSSProperty.h" |
michael@0 | 14 | #include "nsCSSScanner.h" |
michael@0 | 15 | #include "nsCOMPtr.h" |
michael@0 | 16 | #include "nsStringFwd.h" |
michael@0 | 17 | #include "nsTArrayForwardDeclare.h" |
michael@0 | 18 | |
michael@0 | 19 | class nsCSSStyleSheet; |
michael@0 | 20 | class nsIPrincipal; |
michael@0 | 21 | class nsIURI; |
michael@0 | 22 | struct nsCSSSelectorList; |
michael@0 | 23 | class nsMediaList; |
michael@0 | 24 | class nsCSSKeyframeRule; |
michael@0 | 25 | class nsCSSValue; |
michael@0 | 26 | class nsRuleData; |
michael@0 | 27 | |
michael@0 | 28 | namespace mozilla { |
michael@0 | 29 | class CSSVariableValues; |
michael@0 | 30 | namespace css { |
michael@0 | 31 | class Rule; |
michael@0 | 32 | class Declaration; |
michael@0 | 33 | class Loader; |
michael@0 | 34 | class StyleRule; |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | // Interface to the css parser. |
michael@0 | 39 | |
michael@0 | 40 | class MOZ_STACK_CLASS nsCSSParser { |
michael@0 | 41 | public: |
michael@0 | 42 | nsCSSParser(mozilla::css::Loader* aLoader = nullptr, |
michael@0 | 43 | nsCSSStyleSheet* aSheet = nullptr); |
michael@0 | 44 | ~nsCSSParser(); |
michael@0 | 45 | |
michael@0 | 46 | static void Shutdown(); |
michael@0 | 47 | |
michael@0 | 48 | private: |
michael@0 | 49 | nsCSSParser(nsCSSParser const&) MOZ_DELETE; |
michael@0 | 50 | nsCSSParser& operator=(nsCSSParser const&) MOZ_DELETE; |
michael@0 | 51 | |
michael@0 | 52 | public: |
michael@0 | 53 | // Set a style sheet for the parser to fill in. The style sheet must |
michael@0 | 54 | // implement the nsCSSStyleSheet interface. Null can be passed in to clear |
michael@0 | 55 | // out an existing stylesheet reference. |
michael@0 | 56 | nsresult SetStyleSheet(nsCSSStyleSheet* aSheet); |
michael@0 | 57 | |
michael@0 | 58 | // Set whether or not to emulate Nav quirks |
michael@0 | 59 | nsresult SetQuirkMode(bool aQuirkMode); |
michael@0 | 60 | |
michael@0 | 61 | // Set loader to use for child sheets |
michael@0 | 62 | nsresult SetChildLoader(mozilla::css::Loader* aChildLoader); |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Parse aInput into the stylesheet that was previously set by calling |
michael@0 | 66 | * SetStyleSheet. Calling this method without calling SetStyleSheet first is |
michael@0 | 67 | * an error. |
michael@0 | 68 | * |
michael@0 | 69 | * @param aInput the data to parse |
michael@0 | 70 | * @param aSheetURL the URI to use as the sheet URI (for error reporting). |
michael@0 | 71 | * This must match the URI of the sheet passed to |
michael@0 | 72 | * SetStyleSheet. |
michael@0 | 73 | * @param aBaseURI the URI to use for relative URI resolution |
michael@0 | 74 | * @param aSheetPrincipal the principal of the stylesheet. This must match |
michael@0 | 75 | * the principal of the sheet passed to SetStyleSheet. |
michael@0 | 76 | * @param aLineNumber the line number of the first line of the sheet. |
michael@0 | 77 | * @param aAllowUnsafeRules see aEnableUnsafeRules in |
michael@0 | 78 | * mozilla::css::Loader::LoadSheetSync |
michael@0 | 79 | */ |
michael@0 | 80 | nsresult ParseSheet(const nsAString& aInput, |
michael@0 | 81 | nsIURI* aSheetURL, |
michael@0 | 82 | nsIURI* aBaseURI, |
michael@0 | 83 | nsIPrincipal* aSheetPrincipal, |
michael@0 | 84 | uint32_t aLineNumber, |
michael@0 | 85 | bool aAllowUnsafeRules); |
michael@0 | 86 | |
michael@0 | 87 | // Parse HTML style attribute or its equivalent in other markup |
michael@0 | 88 | // languages. aBaseURL is the base url to use for relative links in |
michael@0 | 89 | // the declaration. |
michael@0 | 90 | nsresult ParseStyleAttribute(const nsAString& aAttributeValue, |
michael@0 | 91 | nsIURI* aDocURL, |
michael@0 | 92 | nsIURI* aBaseURL, |
michael@0 | 93 | nsIPrincipal* aNodePrincipal, |
michael@0 | 94 | mozilla::css::StyleRule** aResult); |
michael@0 | 95 | |
michael@0 | 96 | // Parse the body of a declaration block. Very similar to |
michael@0 | 97 | // ParseStyleAttribute, but used under different circumstances. |
michael@0 | 98 | // The contents of aDeclaration will be erased and replaced with the |
michael@0 | 99 | // results of parsing; aChanged will be set true if the aDeclaration |
michael@0 | 100 | // argument was modified. |
michael@0 | 101 | nsresult ParseDeclarations(const nsAString& aBuffer, |
michael@0 | 102 | nsIURI* aSheetURL, |
michael@0 | 103 | nsIURI* aBaseURL, |
michael@0 | 104 | nsIPrincipal* aSheetPrincipal, |
michael@0 | 105 | mozilla::css::Declaration* aDeclaration, |
michael@0 | 106 | bool* aChanged); |
michael@0 | 107 | |
michael@0 | 108 | nsresult ParseRule(const nsAString& aRule, |
michael@0 | 109 | nsIURI* aSheetURL, |
michael@0 | 110 | nsIURI* aBaseURL, |
michael@0 | 111 | nsIPrincipal* aSheetPrincipal, |
michael@0 | 112 | mozilla::css::Rule** aResult); |
michael@0 | 113 | |
michael@0 | 114 | // Parse the value of a single CSS property, and add or replace that |
michael@0 | 115 | // property in aDeclaration. |
michael@0 | 116 | // |
michael@0 | 117 | // SVG "mapped attributes" (which correspond directly to CSS |
michael@0 | 118 | // properties) are parsed slightly differently from regular CSS; in |
michael@0 | 119 | // particular, units may be omitted from <length>. The 'aIsSVGMode' |
michael@0 | 120 | // argument controls this quirk. Note that this *only* applies to |
michael@0 | 121 | // mapped attributes, not inline styles or full style sheets in SVG. |
michael@0 | 122 | nsresult ParseProperty(const nsCSSProperty aPropID, |
michael@0 | 123 | const nsAString& aPropValue, |
michael@0 | 124 | nsIURI* aSheetURL, |
michael@0 | 125 | nsIURI* aBaseURL, |
michael@0 | 126 | nsIPrincipal* aSheetPrincipal, |
michael@0 | 127 | mozilla::css::Declaration* aDeclaration, |
michael@0 | 128 | bool* aChanged, |
michael@0 | 129 | bool aIsImportant, |
michael@0 | 130 | bool aIsSVGMode = false); |
michael@0 | 131 | |
michael@0 | 132 | // The same as ParseProperty but for a variable. |
michael@0 | 133 | nsresult ParseVariable(const nsAString& aVariableName, |
michael@0 | 134 | const nsAString& aPropValue, |
michael@0 | 135 | nsIURI* aSheetURL, |
michael@0 | 136 | nsIURI* aBaseURL, |
michael@0 | 137 | nsIPrincipal* aSheetPrincipal, |
michael@0 | 138 | mozilla::css::Declaration* aDeclaration, |
michael@0 | 139 | bool* aChanged, |
michael@0 | 140 | bool aIsImportant); |
michael@0 | 141 | /** |
michael@0 | 142 | * Parse aBuffer into a media list |aMediaList|, which must be |
michael@0 | 143 | * non-null, replacing its current contents. If aHTMLMode is true, |
michael@0 | 144 | * parse according to HTML rules, with commas as the most important |
michael@0 | 145 | * delimiter. Otherwise, parse according to CSS rules, with |
michael@0 | 146 | * parentheses and strings more important than commas. |aURL| and |
michael@0 | 147 | * |aLineNumber| are used for error reporting. |
michael@0 | 148 | */ |
michael@0 | 149 | void ParseMediaList(const nsSubstring& aBuffer, |
michael@0 | 150 | nsIURI* aURL, |
michael@0 | 151 | uint32_t aLineNumber, |
michael@0 | 152 | nsMediaList* aMediaList, |
michael@0 | 153 | bool aHTMLMode); |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Parse aBuffer into a nsCSSValue |aValue|. Will return false |
michael@0 | 157 | * if aBuffer is not a valid CSS color specification. |
michael@0 | 158 | * One can use nsRuleNode::ComputeColor to compute an nscolor from |
michael@0 | 159 | * the returned nsCSSValue. |
michael@0 | 160 | */ |
michael@0 | 161 | bool ParseColorString(const nsSubstring& aBuffer, |
michael@0 | 162 | nsIURI* aURL, |
michael@0 | 163 | uint32_t aLineNumber, |
michael@0 | 164 | nsCSSValue& aValue); |
michael@0 | 165 | |
michael@0 | 166 | /** |
michael@0 | 167 | * Parse aBuffer into a selector list. On success, caller must |
michael@0 | 168 | * delete *aSelectorList when done with it. |
michael@0 | 169 | */ |
michael@0 | 170 | nsresult ParseSelectorString(const nsSubstring& aSelectorString, |
michael@0 | 171 | nsIURI* aURL, |
michael@0 | 172 | uint32_t aLineNumber, |
michael@0 | 173 | nsCSSSelectorList** aSelectorList); |
michael@0 | 174 | |
michael@0 | 175 | /* |
michael@0 | 176 | * Parse a keyframe rule (which goes inside an @keyframes rule). |
michael@0 | 177 | * Return it if the parse was successful. |
michael@0 | 178 | */ |
michael@0 | 179 | already_AddRefed<nsCSSKeyframeRule> |
michael@0 | 180 | ParseKeyframeRule(const nsSubstring& aBuffer, |
michael@0 | 181 | nsIURI* aURL, |
michael@0 | 182 | uint32_t aLineNumber); |
michael@0 | 183 | |
michael@0 | 184 | /* |
michael@0 | 185 | * Parse a selector list for a keyframe rule. Return whether |
michael@0 | 186 | * the parse succeeded. |
michael@0 | 187 | */ |
michael@0 | 188 | bool ParseKeyframeSelectorString(const nsSubstring& aSelectorString, |
michael@0 | 189 | nsIURI* aURL, |
michael@0 | 190 | uint32_t aLineNumber, |
michael@0 | 191 | InfallibleTArray<float>& aSelectorList); |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * Parse a property and value and return whether the property/value pair |
michael@0 | 195 | * is supported. |
michael@0 | 196 | */ |
michael@0 | 197 | bool EvaluateSupportsDeclaration(const nsAString& aProperty, |
michael@0 | 198 | const nsAString& aValue, |
michael@0 | 199 | nsIURI* aDocURL, |
michael@0 | 200 | nsIURI* aBaseURL, |
michael@0 | 201 | nsIPrincipal* aDocPrincipal); |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * Parse an @supports condition and returns the result of evaluating the |
michael@0 | 205 | * condition. |
michael@0 | 206 | */ |
michael@0 | 207 | bool EvaluateSupportsCondition(const nsAString& aCondition, |
michael@0 | 208 | nsIURI* aDocURL, |
michael@0 | 209 | nsIURI* aBaseURL, |
michael@0 | 210 | nsIPrincipal* aDocPrincipal); |
michael@0 | 211 | |
michael@0 | 212 | typedef void (*VariableEnumFunc)(const nsAString&, void*); |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * Parses aPropertyValue as a property value and calls aFunc for each |
michael@0 | 216 | * variable reference that is found. Returns false if there was |
michael@0 | 217 | * a syntax error in the use of variable references. |
michael@0 | 218 | */ |
michael@0 | 219 | bool EnumerateVariableReferences(const nsAString& aPropertyValue, |
michael@0 | 220 | VariableEnumFunc aFunc, |
michael@0 | 221 | void* aData); |
michael@0 | 222 | |
michael@0 | 223 | /** |
michael@0 | 224 | * Parses aPropertyValue as a property value and resolves variable references |
michael@0 | 225 | * using the values in aVariables. |
michael@0 | 226 | */ |
michael@0 | 227 | bool ResolveVariableValue(const nsAString& aPropertyValue, |
michael@0 | 228 | const mozilla::CSSVariableValues* aVariables, |
michael@0 | 229 | nsString& aResult, |
michael@0 | 230 | nsCSSTokenSerializationType& aFirstToken, |
michael@0 | 231 | nsCSSTokenSerializationType& aLastToken); |
michael@0 | 232 | |
michael@0 | 233 | /** |
michael@0 | 234 | * Parses a string as a CSS token stream value for particular property, |
michael@0 | 235 | * resolving any variable references. The parsed property value is stored |
michael@0 | 236 | * in the specified nsRuleData object. If aShorthandPropertyID has a value |
michael@0 | 237 | * other than eCSSProperty_UNKNOWN, this is the property that will be parsed; |
michael@0 | 238 | * otherwise, aPropertyID will be parsed. Either way, only aPropertyID, |
michael@0 | 239 | * a longhand property, will be copied over to the rule data. |
michael@0 | 240 | * |
michael@0 | 241 | * If the property cannot be parsed, it will be treated as if 'initial' or |
michael@0 | 242 | * 'inherit' were specified, for non-inherited and inherited properties |
michael@0 | 243 | * respectively. |
michael@0 | 244 | */ |
michael@0 | 245 | void ParsePropertyWithVariableReferences( |
michael@0 | 246 | nsCSSProperty aPropertyID, |
michael@0 | 247 | nsCSSProperty aShorthandPropertyID, |
michael@0 | 248 | const nsAString& aValue, |
michael@0 | 249 | const mozilla::CSSVariableValues* aVariables, |
michael@0 | 250 | nsRuleData* aRuleData, |
michael@0 | 251 | nsIURI* aDocURL, |
michael@0 | 252 | nsIURI* aBaseURL, |
michael@0 | 253 | nsIPrincipal* aDocPrincipal, |
michael@0 | 254 | nsCSSStyleSheet* aSheet, |
michael@0 | 255 | uint32_t aLineNumber, |
michael@0 | 256 | uint32_t aLineOffset); |
michael@0 | 257 | |
michael@0 | 258 | protected: |
michael@0 | 259 | // This is a CSSParserImpl*, but if we expose that type name in this |
michael@0 | 260 | // header, we can't put the type definition (in nsCSSParser.cpp) in |
michael@0 | 261 | // the anonymous namespace. |
michael@0 | 262 | void* mImpl; |
michael@0 | 263 | }; |
michael@0 | 264 | |
michael@0 | 265 | #endif /* nsCSSParser_h___ */ |