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