1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* CSS parsing utility functions */
8 
9 #ifndef mozilla_ServoCSSParser_h
10 #define mozilla_ServoCSSParser_h
11 
12 #include "mozilla/AlreadyAddRefed.h"
13 #include "mozilla/gfx/Matrix.h"
14 #include "mozilla/ServoTypes.h"
15 #include "nsColor.h"
16 #include "nsCSSPropertyID.h"
17 #include "nsDOMCSSDeclaration.h"
18 #include "nsStringFwd.h"
19 
20 struct nsCSSRect;
21 struct nsTimingFunction;
22 struct RawServoDeclarationBlock;
23 template <class T>
24 class RefPtr;
25 
26 namespace mozilla {
27 
28 class ServoStyleSet;
29 struct URLExtraData;
30 struct StyleFontFamilyList;
31 union StyleComputedFontStyleDescriptor;
32 
33 namespace css {
34 class Loader;
35 }
36 
37 namespace dom {
38 class Document;
39 }
40 
41 class ServoCSSParser {
42  public:
43   using ParsingEnvironment = nsDOMCSSDeclaration::ParsingEnvironment;
44 
45   /**
46    * Returns whether the specified string can be parsed as a valid CSS
47    * <color> value.
48    *
49    * This includes Mozilla-specific keywords such as -moz-default-color.
50    */
51   static bool IsValidCSSColor(const nsACString& aValue);
52 
53   /**
54    * Computes an nscolor from the given CSS <color> value.
55    *
56    * @param aStyleSet The style set whose nsPresContext will be used to
57    *   compute system colors and other special color values.
58    * @param aCurrentColor The color value that currentcolor should compute to.
59    * @param aValue The CSS <color> value.
60    * @param aResultColor The resulting computed color value.
61    * @param aWasCurrentColor Whether aValue was currentcolor. Can be nullptr
62    *   if the caller doesn't care.
63    * @param aLoader The CSS loader for document we're parsing a color for,
64    *   so that parse errors can be reported to the console. If nullptr, errors
65    *   won't be reported to the console.
66    * @return Whether aValue was successfully parsed and aResultColor was set.
67    */
68   static bool ComputeColor(ServoStyleSet* aStyleSet, nscolor aCurrentColor,
69                            const nsACString& aValue, nscolor* aResultColor,
70                            bool* aWasCurrentColor = nullptr,
71                            css::Loader* aLoader = nullptr);
72 
73   /**
74    * Parse a string representing a CSS property value into a
75    * RawServoDeclarationBlock.
76    *
77    * @param aProperty The property to be parsed.
78    * @param aValue The specified value.
79    * @param aParsingEnvironment All the parsing environment data we need.
80    * @param aParsingMode The paring mode we apply.
81    * @return The parsed value as a RawServoDeclarationBlock. We put the value
82    *   in a declaration block since that is how we represent specified values
83    *   in Servo.
84    */
85   static already_AddRefed<RawServoDeclarationBlock> ParseProperty(
86       nsCSSPropertyID aProperty, const nsACString& aValue,
87       const ParsingEnvironment& aParsingEnvironment,
88       ParsingMode aParsingMode = ParsingMode::Default);
89 
90   /**
91    * Parse a animation timing function.
92    *
93    * @param aValue The specified value.
94    * @param aResult The output timing function. (output)
95    * @return Whether the value was successfully parsed.
96    */
97   static bool ParseEasing(const nsACString& aValue, nsTimingFunction& aResult);
98 
99   /**
100    * Parse a specified transform list into a gfx matrix.
101    *
102    * @param aValue The specified value.
103    * @param aContains3DTransform The output flag indicates whether this is any
104    *   3d transform function. (output)
105    * @param aResult The output matrix. (output)
106    * @return Whether the value was successfully parsed.
107    */
108   static bool ParseTransformIntoMatrix(const nsACString& aValue,
109                                        bool& aContains3DTransform,
110                                        gfx::Matrix4x4& aResult);
111 
112   /**
113    * Parse a font shorthand for FontFaceSet matching, so we only care about
114    * FontFamily, FontStyle, FontStretch, and FontWeight.
115    *
116    * @param aValue The specified value.
117    * @param aUrl The parser url extra data.
118    * @param aList The parsed FontFamily list. (output)
119    * @param aStyle The parsed FontStyle. (output)
120    * @param aStretch The parsed FontStretch. (output)
121    * @param aWeight The parsed FontWeight. (output)
122    * @return Whether the value was successfully parsed.
123    */
124   static bool ParseFontShorthandForMatching(
125       const nsACString& aValue, URLExtraData* aUrl, StyleFontFamilyList& aList,
126       StyleComputedFontStyleDescriptor& aStyle, float& aStretch,
127       float& aWeight);
128 
129   /**
130    * Get a URLExtraData from a document.
131    */
132   static already_AddRefed<URLExtraData> GetURLExtraData(dom::Document*);
133 
134   /**
135    * Get a ParsingEnvironment from a document.
136    */
137   static ParsingEnvironment GetParsingEnvironment(dom::Document*);
138 };
139 
140 }  // namespace mozilla
141 
142 #endif  // mozilla_ServoCSSParser_h
143