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/. */
7 /* CSS parsing utility functions */
9 #ifndef mozilla_ServoCSSParser_h
10 #define mozilla_ServoCSSParser_h
12 #include "mozilla/AlreadyAddRefed.h"
13 #include "mozilla/gfx/Matrix.h"
15 #include "nsCSSPropertyID.h"
16 #include "nsDOMCSSDeclaration.h"
17 #include "nsStringFwd.h"
25 struct AnimatedPropertyID
;
28 struct StyleFontFamilyList
;
29 struct StyleFontStretch
;
30 struct StyleFontWeight
;
31 struct StyleFontStyle
;
32 struct StyleLockedDeclarationBlock
;
33 struct StyleParsingMode
;
34 union StyleComputedFontStyleDescriptor
;
36 template <typename Integer
, typename Number
, typename LinearStops
>
37 struct StyleTimingFunction
;
38 struct StylePiecewiseLinearFunction
;
39 using StyleComputedTimingFunction
=
40 StyleTimingFunction
<int32_t, float, StylePiecewiseLinearFunction
>;
50 class ServoCSSParser
{
52 using ParsingEnvironment
= nsDOMCSSDeclaration::ParsingEnvironment
;
55 * Returns whether the specified string can be parsed as a valid CSS
58 * This includes Mozilla-specific keywords such as -moz-default-color.
60 static bool IsValidCSSColor(const nsACString
& aValue
);
63 * Computes an nscolor from the given CSS <color> value.
65 * @param aStyleSet The style set whose nsPresContext will be used to
66 * compute system colors and other special color values.
67 * @param aCurrentColor The color value that currentcolor should compute to.
68 * @param aValue The CSS <color> value.
69 * @param aResultColor The resulting computed color value.
70 * @param aWasCurrentColor Whether aValue was currentcolor. Can be nullptr
71 * if the caller doesn't care.
72 * @param aLoader The CSS loader for document we're parsing a color for,
73 * so that parse errors can be reported to the console. If nullptr, errors
74 * won't be reported to the console.
75 * @return Whether aValue was successfully parsed and aResultColor was set.
77 static bool ComputeColor(ServoStyleSet
* aStyleSet
, nscolor aCurrentColor
,
78 const nsACString
& aValue
, nscolor
* aResultColor
,
79 bool* aWasCurrentColor
= nullptr,
80 css::Loader
* aLoader
= nullptr);
83 * Takes a CSS <color> and convert it to another color space.
85 * @param aStyleSet The style set whose nsPresContext will be used to
86 * compute system colors and other special color values.
87 * @param aFromColor The CSS <color> we use to convert from.
88 * @param aToColorSpace The CSS <color-space> to convert the color into.
89 * @param aResultColor The resulting converted color value.
90 * @param aResultAdjusted Whether the color was adjusted to fit into the SRGB
92 * @param aLoader The CSS loader for document we're parsing a color for,
93 * so that parse errors can be reported to the console. If nullptr, errors
94 * won't be reported to the console.
95 * @return Whether aFromColor and aToColorSpace was successfully parsed and
96 * aResultColor and aResultAdjusted was set.
98 static bool ColorTo(const nsACString
& aFromColor
,
99 const nsACString
& aToColorSpace
, nsACString
* aResultColor
,
100 nsTArray
<float>* aResultComponents
, bool* aResultAdjusted
,
101 css::Loader
* aLoader
= nullptr);
104 * Parse a string representing a CSS property value into a
105 * StyleLockedDeclarationBlock.
107 * @param aProperty The property to be parsed.
108 * @param aValue The specified value.
109 * @param aParsingEnvironment All the parsing environment data we need.
110 * @param aParsingMode The parsing mode we apply.
111 * @return The parsed value as a StyleLockedDeclarationBlock. We put the value
112 * in a declaration block since that is how we represent specified values
115 static already_AddRefed
<StyleLockedDeclarationBlock
> ParseProperty(
116 nsCSSPropertyID aProperty
, const nsACString
& aValue
,
117 const ParsingEnvironment
& aParsingEnvironment
,
118 const StyleParsingMode
& aParsingMode
);
119 static already_AddRefed
<StyleLockedDeclarationBlock
> ParseProperty(
120 const AnimatedPropertyID
& aProperty
, const nsACString
& aValue
,
121 const ParsingEnvironment
& aParsingEnvironment
,
122 const StyleParsingMode
& aParsingMode
);
125 * Parse a animation timing function.
127 * @param aValue The specified value.
128 * @param aResult The output timing function. (output)
129 * @return Whether the value was successfully parsed.
131 static bool ParseEasing(const nsACString
& aValue
,
132 StyleComputedTimingFunction
& aResult
);
135 * Parse a specified transform list into a gfx matrix.
137 * @param aValue The specified value.
138 * @param aContains3DTransform The output flag indicates whether this is any
139 * 3d transform function. (output)
140 * @param aResult The output matrix. (output)
141 * @return Whether the value was successfully parsed.
143 static bool ParseTransformIntoMatrix(const nsACString
& aValue
,
144 bool& aContains3DTransform
,
145 gfx::Matrix4x4
& aResult
);
148 * Parse a font shorthand for FontFaceSet matching, so we only care about
149 * FontFamily, FontStyle, FontStretch, and FontWeight.
151 * @param aValue The specified value.
152 * @param aUrl The parser url extra data.
153 * @param aList The parsed FontFamily list. (output)
154 * @param aStyle The parsed FontStyle. (output)
155 * @param aStretch The parsed FontStretch. (output)
156 * @param aWeight The parsed FontWeight. (output)
157 * @param aSize If non-null, returns the parsed font size. (output)
158 * @param aSmallCaps If non-null, whether small-caps was specified (output)
159 * @return Whether the value was successfully parsed.
161 static bool ParseFontShorthandForMatching(
162 const nsACString
& aValue
, URLExtraData
* aUrl
, StyleFontFamilyList
& aList
,
163 StyleFontStyle
& aStyle
, StyleFontStretch
& aStretch
,
164 StyleFontWeight
& aWeight
, float* aSize
= nullptr,
165 bool* aSmallCaps
= nullptr);
168 * Get a URLExtraData from a document.
170 static already_AddRefed
<URLExtraData
> GetURLExtraData(dom::Document
*);
173 * Get a ParsingEnvironment from a document.
175 static ParsingEnvironment
GetParsingEnvironment(dom::Document
*);
178 } // namespace mozilla
180 #endif // mozilla_ServoCSSParser_h