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"
16 #include "nsAutoPtr.h"
17 #include "nsStringFwd.h"
18 #include "nsTArrayForwardDeclare.h"
22 struct nsCSSSelectorList
;
25 class nsCSSKeyframeRule
;
31 class CSSVariableValues
;
40 // Interface to the css parser.
42 class MOZ_STACK_CLASS nsCSSParser
{
44 explicit nsCSSParser(mozilla::css::Loader
* aLoader
= nullptr,
45 mozilla::CSSStyleSheet
* aSheet
= nullptr);
48 static void Shutdown();
51 nsCSSParser(nsCSSParser
const&) MOZ_DELETE
;
52 nsCSSParser
& operator=(nsCSSParser
const&) MOZ_DELETE
;
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
);
67 * Parse aInput into the stylesheet that was previously set by calling
68 * SetStyleSheet. Calling this method without calling SetStyleSheet first is
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
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
,
85 nsIPrincipal
* aSheetPrincipal
,
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
92 nsresult
ParseStyleAttribute(const nsAString
& aAttributeValue
,
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
,
106 nsIPrincipal
* aSheetPrincipal
,
107 mozilla::css::Declaration
* aDeclaration
,
110 nsresult
ParseRule(const nsAString
& aRule
,
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
,
128 nsIPrincipal
* aSheetPrincipal
,
129 mozilla::css::Declaration
* aDeclaration
,
132 bool aIsSVGMode
= false);
134 // The same as ParseProperty but for a variable.
135 nsresult
ParseVariable(const nsAString
& aVariableName
,
136 const nsAString
& aPropValue
,
139 nsIPrincipal
* aSheetPrincipal
,
140 mozilla::css::Declaration
* aDeclaration
,
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
,
153 uint32_t aLineNumber
,
154 nsMediaList
* aMediaList
,
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
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
,
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
,
182 uint32_t aLineNumber
,
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
,
193 uint32_t aLineNumber
,
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
,
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
,
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
,
221 uint32_t aLineNumber
,
222 InfallibleTArray
<float>& aSelectorList
);
225 * Parse a property and value and return whether the property/value pair
228 bool EvaluateSupportsDeclaration(const nsAString
& aProperty
,
229 const nsAString
& aValue
,
232 nsIPrincipal
* aDocPrincipal
);
235 * Parse an @supports condition and returns the result of evaluating the
238 bool EvaluateSupportsCondition(const nsAString
& aCondition
,
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
,
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
,
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
276 void ParsePropertyWithVariableReferences(
277 nsCSSProperty aPropertyID
,
278 nsCSSProperty aShorthandPropertyID
,
279 const nsAString
& aValue
,
280 const mozilla::CSSVariableValues
* aVariables
,
281 nsRuleData
* aRuleData
,
284 nsIPrincipal
* aDocPrincipal
,
285 mozilla::CSSStyleSheet
* aSheet
,
286 uint32_t aLineNumber
,
287 uint32_t aLineOffset
);
289 bool ParseCounterStyleName(const nsAString
& aBuffer
,
293 bool ParseCounterDescriptor(nsCSSCounterDesc aDescID
,
294 const nsAString
& aBuffer
,
297 nsIPrincipal
* aSheetPrincipal
,
300 // Check whether a given value can be applied to a property.
301 bool IsValueValidForProperty(const nsCSSProperty aPropID
,
302 const nsAString
& aPropValue
);
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.
311 #endif /* nsCSSParser_h___ */