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"
27 struct StyleFontFamilyList
;
28 struct StyleFontStretch
;
29 struct StyleFontWeight
;
30 struct StyleFontStyle
;
31 struct StyleLockedDeclarationBlock
;
32 struct StyleParsingMode
;
33 union StyleComputedFontStyleDescriptor
;
35 template <typename Integer
, typename Number
, typename LinearStops
>
36 struct StyleTimingFunction
;
37 struct StylePiecewiseLinearFunction
;
38 using StyleComputedTimingFunction
=
39 StyleTimingFunction
<int32_t, float, StylePiecewiseLinearFunction
>;
49 class ServoCSSParser
{
51 using ParsingEnvironment
= nsDOMCSSDeclaration::ParsingEnvironment
;
54 * Returns whether the specified string can be parsed as a valid CSS
57 * This includes Mozilla-specific keywords such as -moz-default-color.
59 static bool IsValidCSSColor(const nsACString
& aValue
);
62 * Computes an nscolor from the given CSS <color> value.
64 * @param aStyleSet The style set whose nsPresContext will be used to
65 * compute system colors and other special color values.
66 * @param aCurrentColor The color value that currentcolor should compute to.
67 * @param aValue The CSS <color> value.
68 * @param aResultColor The resulting computed color value.
69 * @param aWasCurrentColor Whether aValue was currentcolor. Can be nullptr
70 * if the caller doesn't care.
71 * @param aLoader The CSS loader for document we're parsing a color for,
72 * so that parse errors can be reported to the console. If nullptr, errors
73 * won't be reported to the console.
74 * @return Whether aValue was successfully parsed and aResultColor was set.
76 static bool ComputeColor(ServoStyleSet
* aStyleSet
, nscolor aCurrentColor
,
77 const nsACString
& aValue
, nscolor
* aResultColor
,
78 bool* aWasCurrentColor
= nullptr,
79 css::Loader
* aLoader
= nullptr);
82 * Parse a string representing a CSS property value into a
83 * StyleLockedDeclarationBlock.
85 * @param aProperty The property to be parsed.
86 * @param aValue The specified value.
87 * @param aParsingEnvironment All the parsing environment data we need.
88 * @param aParsingMode The parsing mode we apply.
89 * @return The parsed value as a StyleLockedDeclarationBlock. We put the value
90 * in a declaration block since that is how we represent specified values
93 static already_AddRefed
<StyleLockedDeclarationBlock
> ParseProperty(
94 nsCSSPropertyID aProperty
, const nsACString
& aValue
,
95 const ParsingEnvironment
& aParsingEnvironment
,
96 const StyleParsingMode
& aParsingMode
);
99 * Parse a animation timing function.
101 * @param aValue The specified value.
102 * @param aResult The output timing function. (output)
103 * @return Whether the value was successfully parsed.
105 static bool ParseEasing(const nsACString
& aValue
,
106 StyleComputedTimingFunction
& aResult
);
109 * Parse a specified transform list into a gfx matrix.
111 * @param aValue The specified value.
112 * @param aContains3DTransform The output flag indicates whether this is any
113 * 3d transform function. (output)
114 * @param aResult The output matrix. (output)
115 * @return Whether the value was successfully parsed.
117 static bool ParseTransformIntoMatrix(const nsACString
& aValue
,
118 bool& aContains3DTransform
,
119 gfx::Matrix4x4
& aResult
);
122 * Parse a font shorthand for FontFaceSet matching, so we only care about
123 * FontFamily, FontStyle, FontStretch, and FontWeight.
125 * @param aValue The specified value.
126 * @param aUrl The parser url extra data.
127 * @param aList The parsed FontFamily list. (output)
128 * @param aStyle The parsed FontStyle. (output)
129 * @param aStretch The parsed FontStretch. (output)
130 * @param aWeight The parsed FontWeight. (output)
131 * @param aSize If non-null, returns the parsed font size. (output)
132 * @param aSmallCaps If non-null, whether small-caps was specified (output)
133 * @return Whether the value was successfully parsed.
135 static bool ParseFontShorthandForMatching(
136 const nsACString
& aValue
, URLExtraData
* aUrl
, StyleFontFamilyList
& aList
,
137 StyleFontStyle
& aStyle
, StyleFontStretch
& aStretch
,
138 StyleFontWeight
& aWeight
, float* aSize
= nullptr,
139 bool* aSmallCaps
= nullptr);
142 * Get a URLExtraData from a document.
144 static already_AddRefed
<URLExtraData
> GetURLExtraData(dom::Document
*);
147 * Get a ParsingEnvironment from a document.
149 static ParsingEnvironment
GetParsingEnvironment(dom::Document
*);
152 } // namespace mozilla
154 #endif // mozilla_ServoCSSParser_h