Bumping manifests a=b2g-bump
[gecko.git] / layout / style / nsCSSParser.h
blob9ae2f6eb0743730d3920bedca1c6130dabe51a35
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* parsing of CSS stylesheets, based on a token stream from the CSS scanner */
8 #ifndef nsCSSParser_h___
9 #define nsCSSParser_h___
11 #include "mozilla/Attributes.h"
13 #include "nsCSSProperty.h"
14 #include "nsCSSScanner.h"
15 #include "nsCOMPtr.h"
16 #include "nsAutoPtr.h"
17 #include "nsStringFwd.h"
18 #include "nsTArrayForwardDeclare.h"
20 class nsIPrincipal;
21 class nsIURI;
22 struct nsCSSSelectorList;
23 class nsMediaList;
24 class nsMediaQuery;
25 class nsCSSKeyframeRule;
26 class nsCSSValue;
27 struct nsRuleData;
29 namespace mozilla {
30 class CSSStyleSheet;
31 class CSSVariableValues;
32 namespace css {
33 class Rule;
34 class Declaration;
35 class Loader;
36 class StyleRule;
40 // Interface to the css parser.
42 class MOZ_STACK_CLASS nsCSSParser {
43 public:
44 explicit nsCSSParser(mozilla::css::Loader* aLoader = nullptr,
45 mozilla::CSSStyleSheet* aSheet = nullptr);
46 ~nsCSSParser();
48 static void Shutdown();
50 private:
51 nsCSSParser(nsCSSParser const&) MOZ_DELETE;
52 nsCSSParser& operator=(nsCSSParser const&) MOZ_DELETE;
54 public:
55 // Set a style sheet for the parser to fill in. The style sheet must
56 // implement the CSSStyleSheet interface. Null can be passed in to clear
57 // out an existing stylesheet reference.
58 nsresult SetStyleSheet(mozilla::CSSStyleSheet* aSheet);
60 // Set whether or not to emulate Nav quirks
61 nsresult SetQuirkMode(bool aQuirkMode);
63 // Set loader to use for child sheets
64 nsresult SetChildLoader(mozilla::css::Loader* aChildLoader);
66 /**
67 * Parse aInput into the stylesheet that was previously set by calling
68 * SetStyleSheet. Calling this method without calling SetStyleSheet first is
69 * an error.
71 * @param aInput the data to parse
72 * @param aSheetURL the URI to use as the sheet URI (for error reporting).
73 * This must match the URI of the sheet passed to
74 * SetStyleSheet.
75 * @param aBaseURI the URI to use for relative URI resolution
76 * @param aSheetPrincipal the principal of the stylesheet. This must match
77 * the principal of the sheet passed to SetStyleSheet.
78 * @param aLineNumber the line number of the first line of the sheet.
79 * @param aAllowUnsafeRules see aEnableUnsafeRules in
80 * mozilla::css::Loader::LoadSheetSync
82 nsresult ParseSheet(const nsAString& aInput,
83 nsIURI* aSheetURL,
84 nsIURI* aBaseURI,
85 nsIPrincipal* aSheetPrincipal,
86 uint32_t aLineNumber,
87 bool aAllowUnsafeRules);
89 // Parse HTML style attribute or its equivalent in other markup
90 // languages. aBaseURL is the base url to use for relative links in
91 // the declaration.
92 nsresult ParseStyleAttribute(const nsAString& aAttributeValue,
93 nsIURI* aDocURL,
94 nsIURI* aBaseURL,
95 nsIPrincipal* aNodePrincipal,
96 mozilla::css::StyleRule** aResult);
98 // Parse the body of a declaration block. Very similar to
99 // ParseStyleAttribute, but used under different circumstances.
100 // The contents of aDeclaration will be erased and replaced with the
101 // results of parsing; aChanged will be set true if the aDeclaration
102 // argument was modified.
103 nsresult ParseDeclarations(const nsAString& aBuffer,
104 nsIURI* aSheetURL,
105 nsIURI* aBaseURL,
106 nsIPrincipal* aSheetPrincipal,
107 mozilla::css::Declaration* aDeclaration,
108 bool* aChanged);
110 nsresult ParseRule(const nsAString& aRule,
111 nsIURI* aSheetURL,
112 nsIURI* aBaseURL,
113 nsIPrincipal* aSheetPrincipal,
114 mozilla::css::Rule** aResult);
116 // Parse the value of a single CSS property, and add or replace that
117 // property in aDeclaration.
119 // SVG "mapped attributes" (which correspond directly to CSS
120 // properties) are parsed slightly differently from regular CSS; in
121 // particular, units may be omitted from <length>. The 'aIsSVGMode'
122 // argument controls this quirk. Note that this *only* applies to
123 // mapped attributes, not inline styles or full style sheets in SVG.
124 nsresult ParseProperty(const nsCSSProperty aPropID,
125 const nsAString& aPropValue,
126 nsIURI* aSheetURL,
127 nsIURI* aBaseURL,
128 nsIPrincipal* aSheetPrincipal,
129 mozilla::css::Declaration* aDeclaration,
130 bool* aChanged,
131 bool aIsImportant,
132 bool aIsSVGMode = false);
134 // The same as ParseProperty but for a variable.
135 nsresult ParseVariable(const nsAString& aVariableName,
136 const nsAString& aPropValue,
137 nsIURI* aSheetURL,
138 nsIURI* aBaseURL,
139 nsIPrincipal* aSheetPrincipal,
140 mozilla::css::Declaration* aDeclaration,
141 bool* aChanged,
142 bool aIsImportant);
144 * Parse aBuffer into a media list |aMediaList|, which must be
145 * non-null, replacing its current contents. If aHTMLMode is true,
146 * parse according to HTML rules, with commas as the most important
147 * delimiter. Otherwise, parse according to CSS rules, with
148 * parentheses and strings more important than commas. |aURL| and
149 * |aLineNumber| are used for error reporting.
151 void ParseMediaList(const nsSubstring& aBuffer,
152 nsIURI* aURL,
153 uint32_t aLineNumber,
154 nsMediaList* aMediaList,
155 bool aHTMLMode);
158 * Parse aBuffer into a list of media queries and their associated values,
159 * according to grammar:
160 * <source-size-list> = <source-size>#?
161 * <source-size> = <media-condition>? <length>
163 * Note that this grammar is top-level: The function expects to consume the
164 * entire input buffer.
166 * Output arrays overwritten (not appended) and are cleared in case of parse
167 * failure.
169 bool ParseSourceSizeList(const nsAString& aBuffer,
170 nsIURI* aURI, // for error reporting
171 uint32_t aLineNumber, // for error reporting
172 InfallibleTArray< nsAutoPtr<nsMediaQuery> >& aQueries,
173 InfallibleTArray<nsCSSValue>& aValues,
174 bool aHTMLMode);
177 * Parse aBuffer into a nsCSSValue |aValue|. Will return false
178 * if aBuffer is not a valid font family list.
180 bool ParseFontFamilyListString(const nsSubstring& aBuffer,
181 nsIURI* aURL,
182 uint32_t aLineNumber,
183 nsCSSValue& aValue);
186 * Parse aBuffer into a nsCSSValue |aValue|. Will return false
187 * if aBuffer is not a valid CSS color specification.
188 * One can use nsRuleNode::ComputeColor to compute an nscolor from
189 * the returned nsCSSValue.
191 bool ParseColorString(const nsSubstring& aBuffer,
192 nsIURI* aURL,
193 uint32_t aLineNumber,
194 nsCSSValue& aValue,
195 bool aSuppressErrors = false);
198 * Parse aBuffer into a selector list. On success, caller must
199 * delete *aSelectorList when done with it.
201 nsresult ParseSelectorString(const nsSubstring& aSelectorString,
202 nsIURI* aURL,
203 uint32_t aLineNumber,
204 nsCSSSelectorList** aSelectorList);
207 * Parse a keyframe rule (which goes inside an @keyframes rule).
208 * Return it if the parse was successful.
210 already_AddRefed<nsCSSKeyframeRule>
211 ParseKeyframeRule(const nsSubstring& aBuffer,
212 nsIURI* aURL,
213 uint32_t aLineNumber);
216 * Parse a selector list for a keyframe rule. Return whether
217 * the parse succeeded.
219 bool ParseKeyframeSelectorString(const nsSubstring& aSelectorString,
220 nsIURI* aURL,
221 uint32_t aLineNumber,
222 InfallibleTArray<float>& aSelectorList);
225 * Parse a property and value and return whether the property/value pair
226 * is supported.
228 bool EvaluateSupportsDeclaration(const nsAString& aProperty,
229 const nsAString& aValue,
230 nsIURI* aDocURL,
231 nsIURI* aBaseURL,
232 nsIPrincipal* aDocPrincipal);
235 * Parse an @supports condition and returns the result of evaluating the
236 * condition.
238 bool EvaluateSupportsCondition(const nsAString& aCondition,
239 nsIURI* aDocURL,
240 nsIURI* aBaseURL,
241 nsIPrincipal* aDocPrincipal);
243 typedef void (*VariableEnumFunc)(const nsAString&, void*);
246 * Parses aPropertyValue as a property value and calls aFunc for each
247 * variable reference that is found. Returns false if there was
248 * a syntax error in the use of variable references.
250 bool EnumerateVariableReferences(const nsAString& aPropertyValue,
251 VariableEnumFunc aFunc,
252 void* aData);
255 * Parses aPropertyValue as a property value and resolves variable references
256 * using the values in aVariables.
258 bool ResolveVariableValue(const nsAString& aPropertyValue,
259 const mozilla::CSSVariableValues* aVariables,
260 nsString& aResult,
261 nsCSSTokenSerializationType& aFirstToken,
262 nsCSSTokenSerializationType& aLastToken);
265 * Parses a string as a CSS token stream value for particular property,
266 * resolving any variable references. The parsed property value is stored
267 * in the specified nsRuleData object. If aShorthandPropertyID has a value
268 * other than eCSSProperty_UNKNOWN, this is the property that will be parsed;
269 * otherwise, aPropertyID will be parsed. Either way, only aPropertyID,
270 * a longhand property, will be copied over to the rule data.
272 * If the property cannot be parsed, it will be treated as if 'initial' or
273 * 'inherit' were specified, for non-inherited and inherited properties
274 * respectively.
276 void ParsePropertyWithVariableReferences(
277 nsCSSProperty aPropertyID,
278 nsCSSProperty aShorthandPropertyID,
279 const nsAString& aValue,
280 const mozilla::CSSVariableValues* aVariables,
281 nsRuleData* aRuleData,
282 nsIURI* aDocURL,
283 nsIURI* aBaseURL,
284 nsIPrincipal* aDocPrincipal,
285 mozilla::CSSStyleSheet* aSheet,
286 uint32_t aLineNumber,
287 uint32_t aLineOffset);
289 bool ParseCounterStyleName(const nsAString& aBuffer,
290 nsIURI* aURL,
291 nsAString& aName);
293 bool ParseCounterDescriptor(nsCSSCounterDesc aDescID,
294 const nsAString& aBuffer,
295 nsIURI* aSheetURL,
296 nsIURI* aBaseURL,
297 nsIPrincipal* aSheetPrincipal,
298 nsCSSValue& aValue);
300 // Check whether a given value can be applied to a property.
301 bool IsValueValidForProperty(const nsCSSProperty aPropID,
302 const nsAString& aPropValue);
304 protected:
305 // This is a CSSParserImpl*, but if we expose that type name in this
306 // header, we can't put the type definition (in nsCSSParser.cpp) in
307 // the anonymous namespace.
308 void* mImpl;
311 #endif /* nsCSSParser_h___ */