nsCSSParser.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /* parsing of CSS stylesheets, based on a token stream from the CSS scanner */
  6. #ifndef nsCSSParser_h___
  7. #define nsCSSParser_h___
  8. #include "mozilla/Attributes.h"
  9. #include "mozilla/css/Loader.h"
  10. #include "nsCSSPropertyID.h"
  11. #include "nsCSSScanner.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsAutoPtr.h"
  14. #include "nsStringFwd.h"
  15. #include "nsTArrayForwardDeclare.h"
  16. class nsIPrincipal;
  17. class nsIURI;
  18. struct nsCSSSelectorList;
  19. class nsMediaList;
  20. class nsMediaQuery;
  21. class nsCSSKeyframeRule;
  22. class nsCSSValue;
  23. struct nsRuleData;
  24. namespace mozilla {
  25. class CSSStyleSheet;
  26. class CSSVariableValues;
  27. namespace css {
  28. class Rule;
  29. class Declaration;
  30. class StyleRule;
  31. } // namespace css
  32. } // namespace mozilla
  33. // Interface to the css parser.
  34. class MOZ_STACK_CLASS nsCSSParser {
  35. public:
  36. explicit nsCSSParser(mozilla::css::Loader* aLoader = nullptr,
  37. mozilla::CSSStyleSheet* aSheet = nullptr);
  38. ~nsCSSParser();
  39. static void Startup();
  40. static void Shutdown();
  41. private:
  42. nsCSSParser(nsCSSParser const&) = delete;
  43. nsCSSParser& operator=(nsCSSParser const&) = delete;
  44. public:
  45. /**
  46. * Parse aInput into the stylesheet that was previously passed to the
  47. * constructor. Calling this method on an nsCSSParser that had nullptr
  48. * passed in as the style sheet is an error.
  49. *
  50. * @param aInput the data to parse
  51. * @param aSheetURL the URI to use as the sheet URI (for error reporting).
  52. * This must match the URI of the sheet passed to
  53. * the constructor.
  54. * @param aBaseURI the URI to use for relative URI resolution
  55. * @param aSheetPrincipal the principal of the stylesheet. This must match
  56. * the principal of the sheet passed to the
  57. * constructor.
  58. * @param aLineNumber the line number of the first line of the sheet.
  59. * @param aReusableSheets style sheets that can be reused by an @import.
  60. * This can be nullptr.
  61. */
  62. nsresult ParseSheet(const nsAString& aInput,
  63. nsIURI* aSheetURL,
  64. nsIURI* aBaseURI,
  65. nsIPrincipal* aSheetPrincipal,
  66. uint32_t aLineNumber,
  67. mozilla::css::LoaderReusableStyleSheets* aReusableSheets =
  68. nullptr);
  69. // Parse HTML style attribute or its equivalent in other markup
  70. // languages. aBaseURL is the base url to use for relative links in
  71. // the declaration.
  72. already_AddRefed<mozilla::css::Declaration>
  73. ParseStyleAttribute(const nsAString& aAttributeValue,
  74. nsIURI* aDocURL,
  75. nsIURI* aBaseURL,
  76. nsIPrincipal* aNodePrincipal);
  77. // Parse the body of a declaration block. Very similar to
  78. // ParseStyleAttribute, but used under different circumstances.
  79. // The contents of aDeclaration will be erased and replaced with the
  80. // results of parsing; aChanged will be set true if the aDeclaration
  81. // argument was modified.
  82. nsresult ParseDeclarations(const nsAString& aBuffer,
  83. nsIURI* aSheetURL,
  84. nsIURI* aBaseURL,
  85. nsIPrincipal* aSheetPrincipal,
  86. mozilla::css::Declaration* aDeclaration,
  87. bool* aChanged);
  88. nsresult ParseRule(const nsAString& aRule,
  89. nsIURI* aSheetURL,
  90. nsIURI* aBaseURL,
  91. nsIPrincipal* aSheetPrincipal,
  92. mozilla::css::Rule** aResult);
  93. // Parse the value of a single CSS property, and add or replace that
  94. // property in aDeclaration.
  95. //
  96. // SVG "mapped attributes" (which correspond directly to CSS
  97. // properties) are parsed slightly differently from regular CSS; in
  98. // particular, units may be omitted from <length>. The 'aIsSVGMode'
  99. // argument controls this quirk. Note that this *only* applies to
  100. // mapped attributes, not inline styles or full style sheets in SVG.
  101. void ParseProperty(const nsCSSPropertyID aPropID,
  102. const nsAString& aPropValue,
  103. nsIURI* aSheetURL,
  104. nsIURI* aBaseURL,
  105. nsIPrincipal* aSheetPrincipal,
  106. mozilla::css::Declaration* aDeclaration,
  107. bool* aChanged,
  108. bool aIsImportant,
  109. bool aIsSVGMode = false);
  110. // Same as ParseProperty but returns an nsCSSValue in aResult
  111. // rather than storing the property in a Declaration. aPropID
  112. // must be a longhand property.
  113. void ParseLonghandProperty(const nsCSSPropertyID aPropID,
  114. const nsAString& aPropValue,
  115. nsIURI* aSheetURL,
  116. nsIURI* aBaseURL,
  117. nsIPrincipal* aSheetPrincipal,
  118. nsCSSValue& aResult);
  119. // Parse the value of a CSS transform property. Returns
  120. // whether the value was successfully parsed. If
  121. // aDisallowRelativeValues is true then this method will
  122. // only successfully parse if all values are numbers or
  123. // have non-relative dimensions.
  124. bool ParseTransformProperty(const nsAString& aPropValue,
  125. bool aDisallowRelativeValues,
  126. nsCSSValue& aResult);
  127. // The same as ParseProperty but for a variable.
  128. void ParseVariable(const nsAString& aVariableName,
  129. const nsAString& aPropValue,
  130. nsIURI* aSheetURL,
  131. nsIURI* aBaseURL,
  132. nsIPrincipal* aSheetPrincipal,
  133. mozilla::css::Declaration* aDeclaration,
  134. bool* aChanged,
  135. bool aIsImportant);
  136. /**
  137. * Parse aBuffer into a media list |aMediaList|, which must be
  138. * non-null, replacing its current contents. If aHTMLMode is true,
  139. * parse according to HTML rules, with commas as the most important
  140. * delimiter. Otherwise, parse according to CSS rules, with
  141. * parentheses and strings more important than commas. |aURL| and
  142. * |aLineNumber| are used for error reporting.
  143. */
  144. void ParseMediaList(const nsSubstring& aBuffer,
  145. nsIURI* aURL,
  146. uint32_t aLineNumber,
  147. nsMediaList* aMediaList,
  148. bool aHTMLMode);
  149. /*
  150. * Parse aBuffer into a list of media queries and their associated values,
  151. * according to grammar:
  152. * <source-size-list> = <source-size>#?
  153. * <source-size> = <media-condition>? <length>
  154. *
  155. * Note that this grammar is top-level: The function expects to consume the
  156. * entire input buffer.
  157. *
  158. * Output arrays overwritten (not appended) and are cleared in case of parse
  159. * failure.
  160. */
  161. bool ParseSourceSizeList(const nsAString& aBuffer,
  162. nsIURI* aURI, // for error reporting
  163. uint32_t aLineNumber, // for error reporting
  164. InfallibleTArray< nsAutoPtr<nsMediaQuery> >& aQueries,
  165. InfallibleTArray<nsCSSValue>& aValues,
  166. bool aHTMLMode);
  167. /**
  168. * Parse aBuffer into a nsCSSValue |aValue|. Will return false
  169. * if aBuffer is not a valid font family list.
  170. */
  171. bool ParseFontFamilyListString(const nsSubstring& aBuffer,
  172. nsIURI* aURL,
  173. uint32_t aLineNumber,
  174. nsCSSValue& aValue);
  175. /**
  176. * Parse aBuffer into a nsCSSValue |aValue|. Will return false
  177. * if aBuffer is not a valid CSS color specification.
  178. * One can use nsRuleNode::ComputeColor to compute an nscolor from
  179. * the returned nsCSSValue.
  180. */
  181. bool ParseColorString(const nsSubstring& aBuffer,
  182. nsIURI* aURL,
  183. uint32_t aLineNumber,
  184. nsCSSValue& aValue,
  185. bool aSuppressErrors = false);
  186. /**
  187. * Parse aBuffer into a nsCSSValue |aValue|. Will return false
  188. * if aBuffer is not a valid CSS margin specification.
  189. * One can use nsRuleNode::GetRectValue to compute an nsCSSRect from
  190. * the returned nsCSSValue.
  191. */
  192. bool ParseMarginString(const nsSubstring& aBuffer,
  193. nsIURI* aURL,
  194. uint32_t aLineNumber,
  195. nsCSSValue& aValue,
  196. bool aSuppressErrors = false);
  197. /**
  198. * Parse aBuffer into a selector list. On success, caller must
  199. * delete *aSelectorList when done with it.
  200. */
  201. nsresult ParseSelectorString(const nsSubstring& aSelectorString,
  202. nsIURI* aURL,
  203. uint32_t aLineNumber,
  204. nsCSSSelectorList** aSelectorList);
  205. /*
  206. * Parse a keyframe rule (which goes inside an @keyframes rule).
  207. * Return it if the parse was successful.
  208. */
  209. already_AddRefed<nsCSSKeyframeRule>
  210. ParseKeyframeRule(const nsSubstring& aBuffer,
  211. nsIURI* aURL,
  212. uint32_t aLineNumber);
  213. /*
  214. * Parse a selector list for a keyframe rule. Return whether
  215. * the parse succeeded.
  216. */
  217. bool ParseKeyframeSelectorString(const nsSubstring& aSelectorString,
  218. nsIURI* aURL,
  219. uint32_t aLineNumber,
  220. InfallibleTArray<float>& aSelectorList);
  221. /**
  222. * Parse a property and value and return whether the property/value pair
  223. * is supported.
  224. */
  225. bool EvaluateSupportsDeclaration(const nsAString& aProperty,
  226. const nsAString& aValue,
  227. nsIURI* aDocURL,
  228. nsIURI* aBaseURL,
  229. nsIPrincipal* aDocPrincipal);
  230. /**
  231. * Parse an @supports condition and returns the result of evaluating the
  232. * condition.
  233. */
  234. bool EvaluateSupportsCondition(const nsAString& aCondition,
  235. nsIURI* aDocURL,
  236. nsIURI* aBaseURL,
  237. nsIPrincipal* aDocPrincipal);
  238. typedef void (*VariableEnumFunc)(const nsAString&, void*);
  239. /**
  240. * Parses aPropertyValue as a property value and calls aFunc for each
  241. * variable reference that is found. Returns false if there was
  242. * a syntax error in the use of variable references.
  243. */
  244. bool EnumerateVariableReferences(const nsAString& aPropertyValue,
  245. VariableEnumFunc aFunc,
  246. void* aData);
  247. /**
  248. * Parses aPropertyValue as a property value and resolves variable references
  249. * using the values in aVariables.
  250. */
  251. bool ResolveVariableValue(const nsAString& aPropertyValue,
  252. const mozilla::CSSVariableValues* aVariables,
  253. nsString& aResult,
  254. nsCSSTokenSerializationType& aFirstToken,
  255. nsCSSTokenSerializationType& aLastToken);
  256. /**
  257. * Parses a string as a CSS token stream value for particular property,
  258. * resolving any variable references. The parsed property value is stored
  259. * in the specified nsRuleData object. If aShorthandPropertyID has a value
  260. * other than eCSSProperty_UNKNOWN, this is the property that will be parsed;
  261. * otherwise, aPropertyID will be parsed. Either way, only aPropertyID,
  262. * a longhand property, will be copied over to the rule data.
  263. *
  264. * If the property cannot be parsed, it will be treated as if 'initial' or
  265. * 'inherit' were specified, for non-inherited and inherited properties
  266. * respectively.
  267. */
  268. void ParsePropertyWithVariableReferences(
  269. nsCSSPropertyID aPropertyID,
  270. nsCSSPropertyID aShorthandPropertyID,
  271. const nsAString& aValue,
  272. const mozilla::CSSVariableValues* aVariables,
  273. nsRuleData* aRuleData,
  274. nsIURI* aDocURL,
  275. nsIURI* aBaseURL,
  276. nsIPrincipal* aDocPrincipal,
  277. mozilla::CSSStyleSheet* aSheet,
  278. uint32_t aLineNumber,
  279. uint32_t aLineOffset);
  280. bool ParseCounterStyleName(const nsAString& aBuffer,
  281. nsIURI* aURL,
  282. nsAString& aName);
  283. bool ParseCounterDescriptor(nsCSSCounterDesc aDescID,
  284. const nsAString& aBuffer,
  285. nsIURI* aSheetURL,
  286. nsIURI* aBaseURL,
  287. nsIPrincipal* aSheetPrincipal,
  288. nsCSSValue& aValue);
  289. bool ParseFontFaceDescriptor(nsCSSFontDesc aDescID,
  290. const nsAString& aBuffer,
  291. nsIURI* aSheetURL,
  292. nsIURI* aBaseURL,
  293. nsIPrincipal* aSheetPrincipal,
  294. nsCSSValue& aValue);
  295. // Check whether a given value can be applied to a property.
  296. bool IsValueValidForProperty(const nsCSSPropertyID aPropID,
  297. const nsAString& aPropValue);
  298. // Return the default value to be used for -moz-control-character-visibility,
  299. // from preferences (cached by our Startup(), so that both nsStyleText and
  300. // nsRuleNode can have fast access to it).
  301. static uint8_t ControlCharVisibilityDefault();
  302. protected:
  303. // This is a CSSParserImpl*, but if we expose that type name in this
  304. // header, we can't put the type definition (in nsCSSParser.cpp) in
  305. // the anonymous namespace.
  306. void* mImpl;
  307. };
  308. #endif /* nsCSSParser_h___ */