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"
15 #include "nsStringFwd.h"
16 #include "nsTArrayForwardDeclare.h"
18 class nsCSSStyleSheet
;
21 struct nsCSSSelectorList
;
23 class nsCSSKeyframeRule
;
35 // Interface to the css parser.
37 class MOZ_STACK_CLASS nsCSSParser
{
39 nsCSSParser(mozilla::css::Loader
* aLoader
= nullptr,
40 nsCSSStyleSheet
* aSheet
= nullptr);
43 static void Shutdown();
46 nsCSSParser(nsCSSParser
const&) MOZ_DELETE
;
47 nsCSSParser
& operator=(nsCSSParser
const&) MOZ_DELETE
;
50 // Set a style sheet for the parser to fill in. The style sheet must
51 // implement the nsCSSStyleSheet interface. Null can be passed in to clear
52 // out an existing stylesheet reference.
53 nsresult
SetStyleSheet(nsCSSStyleSheet
* aSheet
);
55 // Set whether or not to emulate Nav quirks
56 nsresult
SetQuirkMode(bool aQuirkMode
);
58 // Set loader to use for child sheets
59 nsresult
SetChildLoader(mozilla::css::Loader
* aChildLoader
);
62 * Parse aInput into the stylesheet that was previously set by calling
63 * SetStyleSheet. Calling this method without calling SetStyleSheet first is
66 * @param aInput the data to parse
67 * @param aSheetURL the URI to use as the sheet URI (for error reporting).
68 * This must match the URI of the sheet passed to
70 * @param aBaseURI the URI to use for relative URI resolution
71 * @param aSheetPrincipal the principal of the stylesheet. This must match
72 * the principal of the sheet passed to SetStyleSheet.
73 * @param aLineNumber the line number of the first line of the sheet.
74 * @param aAllowUnsafeRules see aEnableUnsafeRules in
75 * mozilla::css::Loader::LoadSheetSync
77 nsresult
ParseSheet(const nsAString
& aInput
,
80 nsIPrincipal
* aSheetPrincipal
,
82 bool aAllowUnsafeRules
);
84 // Parse HTML style attribute or its equivalent in other markup
85 // languages. aBaseURL is the base url to use for relative links in
87 nsresult
ParseStyleAttribute(const nsAString
& aAttributeValue
,
90 nsIPrincipal
* aNodePrincipal
,
91 mozilla::css::StyleRule
** aResult
);
93 // Parse the body of a declaration block. Very similar to
94 // ParseStyleAttribute, but used under different circumstances.
95 // The contents of aDeclaration will be erased and replaced with the
96 // results of parsing; aChanged will be set true if the aDeclaration
97 // argument was modified.
98 nsresult
ParseDeclarations(const nsAString
& aBuffer
,
101 nsIPrincipal
* aSheetPrincipal
,
102 mozilla::css::Declaration
* aDeclaration
,
105 nsresult
ParseRule(const nsAString
& aRule
,
108 nsIPrincipal
* aSheetPrincipal
,
109 mozilla::css::Rule
** aResult
);
111 // Parse the value of a single CSS property, and add or replace that
112 // property in aDeclaration.
114 // SVG "mapped attributes" (which correspond directly to CSS
115 // properties) are parsed slightly differently from regular CSS; in
116 // particular, units may be omitted from <length>. The 'aIsSVGMode'
117 // argument controls this quirk. Note that this *only* applies to
118 // mapped attributes, not inline styles or full style sheets in SVG.
119 nsresult
ParseProperty(const nsCSSProperty aPropID
,
120 const nsAString
& aPropValue
,
123 nsIPrincipal
* aSheetPrincipal
,
124 mozilla::css::Declaration
* aDeclaration
,
127 bool aIsSVGMode
= false);
130 * Parse aBuffer into a media list |aMediaList|, which must be
131 * non-null, replacing its current contents. If aHTMLMode is true,
132 * parse according to HTML rules, with commas as the most important
133 * delimiter. Otherwise, parse according to CSS rules, with
134 * parentheses and strings more important than commas. |aURL| and
135 * |aLineNumber| are used for error reporting.
137 nsresult
ParseMediaList(const nsSubstring
& aBuffer
,
139 uint32_t aLineNumber
,
140 nsMediaList
* aMediaList
,
144 * Parse aBuffer into a nsCSSValue |aValue|. Will return false
145 * if aBuffer is not a valid CSS color specification.
146 * One can use nsRuleNode::ComputeColor to compute an nscolor from
147 * the returned nsCSSValue.
149 bool ParseColorString(const nsSubstring
& aBuffer
,
151 uint32_t aLineNumber
,
155 * Parse aBuffer into a selector list. On success, caller must
156 * delete *aSelectorList when done with it.
158 nsresult
ParseSelectorString(const nsSubstring
& aSelectorString
,
160 uint32_t aLineNumber
,
161 nsCSSSelectorList
** aSelectorList
);
164 * Parse a keyframe rule (which goes inside an @keyframes rule).
165 * Return it if the parse was successful.
167 already_AddRefed
<nsCSSKeyframeRule
>
168 ParseKeyframeRule(const nsSubstring
& aBuffer
,
170 uint32_t aLineNumber
);
173 * Parse a selector list for a keyframe rule. Return whether
174 * the parse succeeded.
176 bool ParseKeyframeSelectorString(const nsSubstring
& aSelectorString
,
178 uint32_t aLineNumber
,
179 InfallibleTArray
<float>& aSelectorList
);
182 * Parse a property and value and return whether the property/value pair
185 bool EvaluateSupportsDeclaration(const nsAString
& aProperty
,
186 const nsAString
& aValue
,
189 nsIPrincipal
* aDocPrincipal
);
192 * Parse an @supports condition and returns the result of evaluating the
195 bool EvaluateSupportsCondition(const nsAString
& aCondition
,
198 nsIPrincipal
* aDocPrincipal
);
201 // This is a CSSParserImpl*, but if we expose that type name in this
202 // header, we can't put the type definition (in nsCSSParser.cpp) in
203 // the anonymous namespace.
207 #endif /* nsCSSParser_h___ */