ServoCSSParser.h (7622B)
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 "NonCustomCSSPropertyId.h" 13 #include "mozilla/AlreadyAddRefed.h" 14 #include "mozilla/gfx/Matrix.h" 15 #include "nsColor.h" 16 #include "nsDOMCSSDeclaration.h" 17 #include "nsStringFwd.h" 18 19 struct nsCSSRect; 20 template <class T> 21 class RefPtr; 22 23 namespace mozilla { 24 25 struct CSSPropertyId; 26 class ServoStyleSet; 27 struct URLExtraData; 28 struct StyleAbsoluteColor; 29 struct StyleFontFamilyList; 30 struct StyleFontStretch; 31 struct StyleFontWeight; 32 struct StyleFontStyle; 33 struct StyleLockedDeclarationBlock; 34 struct StyleParsingMode; 35 struct StylePerDocumentStyleData; 36 union StyleComputedFontStyleDescriptor; 37 enum class StyleColorSpace : uint8_t; 38 39 template <typename Integer, typename Number, typename LinearStops> 40 struct StyleTimingFunction; 41 struct StylePiecewiseLinearFunction; 42 using StyleComputedTimingFunction = 43 StyleTimingFunction<int32_t, float, StylePiecewiseLinearFunction>; 44 45 namespace css { 46 class Loader; 47 } 48 49 namespace dom { 50 class Document; 51 } 52 53 class ServoCSSParser { 54 public: 55 using ParsingEnvironment = nsDOMCSSDeclaration::ParsingEnvironment; 56 57 /** 58 * Returns whether the specified string can be parsed as a valid CSS 59 * <color> value. 60 * 61 * This includes Mozilla-specific keywords such as -moz-default-color. 62 */ 63 static bool IsValidCSSColor(const nsACString& aValue); 64 65 /** 66 * Computes an nscolor from the given CSS <color> value. 67 * 68 * @param aStyleData The style data to compute system colors and other special 69 * color values. 70 * @param aCurrentColor The color value that currentcolor should compute to. 71 * @param aValue The CSS <color> value. 72 * @param aResultColor The resulting computed color value. 73 * @param aWasCurrentColor Whether aValue was currentcolor. Can be nullptr 74 * if the caller doesn't care. 75 * @param aLoader The CSS loader for document we're parsing a color for, 76 * so that parse errors can be reported to the console. If nullptr, errors 77 * won't be reported to the console. 78 * @return Whether aValue was successfully parsed and aResultColor was set. 79 */ 80 static bool ComputeColor(const StylePerDocumentStyleData* aStyleData, 81 nscolor aCurrentColor, const nsACString& aValue, 82 nscolor* aResultColor, 83 bool* aWasCurrentColor = nullptr, 84 css::Loader* aLoader = nullptr); 85 86 /** 87 * Computes a StyleAbsoluteColor from the given CSS <color> value, following 88 * the HTML spec: 89 * https://html.spec.whatwg.org/#update-a-color-well-control-color 90 * 91 * @param aStyleData The style data to compute system colors and other special 92 * color values. 93 * @param aValue The CSS <color> value. 94 * @param aToColorSpace The color space to convert the color into. 95 * @return The resulting computed color value. For invalid color value, 96 * Nothing() will be returned. 97 */ 98 static Maybe<StyleAbsoluteColor> ComputeColorWellControlColor( 99 const StylePerDocumentStyleData* aStyleData, const nsACString& aValue, 100 StyleColorSpace aToColorSpace); 101 102 /** 103 * Takes a CSS <color> and convert it to another color space. 104 * 105 * @param aStyleSet The style set whose nsPresContext will be used to 106 * compute system colors and other special color values. 107 * @param aFromColor The CSS <color> we use to convert from. 108 * @param aToColorSpace The CSS <color-space> to convert the color into. 109 * @param aResultColor The resulting converted color value. 110 * @param aResultAdjusted Whether the color was adjusted to fit into the SRGB 111 color space. 112 * @param aLoader The CSS loader for document we're parsing a color for, 113 * so that parse errors can be reported to the console. If nullptr, errors 114 * won't be reported to the console. 115 * @return Whether aFromColor and aToColorSpace was successfully parsed and 116 * aResultColor and aResultAdjusted was set. 117 */ 118 static bool ColorTo(const nsACString& aFromColor, 119 const nsACString& aToColorSpace, nsACString* aResultColor, 120 nsTArray<float>* aResultComponents, bool* aResultAdjusted, 121 css::Loader* aLoader = nullptr); 122 123 /** 124 * Parse a string representing a CSS property value into a 125 * StyleLockedDeclarationBlock. 126 * 127 * @param aProperty The property to be parsed. 128 * @param aValue The specified value. 129 * @param aParsingEnvironment All the parsing environment data we need. 130 * @param aParsingMode The parsing mode we apply. 131 * @return The parsed value as a StyleLockedDeclarationBlock. We put the value 132 * in a declaration block since that is how we represent specified values 133 * in Servo. 134 */ 135 static already_AddRefed<StyleLockedDeclarationBlock> ParseProperty( 136 NonCustomCSSPropertyId aProperty, const nsACString& aValue, 137 const ParsingEnvironment& aParsingEnvironment, 138 const StyleParsingMode& aParsingMode); 139 static already_AddRefed<StyleLockedDeclarationBlock> ParseProperty( 140 const CSSPropertyId& aProperty, const nsACString& aValue, 141 const ParsingEnvironment& aParsingEnvironment, 142 const StyleParsingMode& aParsingMode); 143 144 /** 145 * Parse a animation timing function. 146 * 147 * @param aValue The specified value. 148 * @param aResult The output timing function. (output) 149 * @return Whether the value was successfully parsed. 150 */ 151 static bool ParseEasing(const nsACString& aValue, 152 StyleComputedTimingFunction& aResult); 153 154 /** 155 * Parse a specified transform list into a gfx matrix. 156 * 157 * @param aValue The specified value. 158 * @param aContains3DTransform The output flag indicates whether this is any 159 * 3d transform function. (output) 160 * @param aResult The output matrix. (output) 161 * @return Whether the value was successfully parsed. 162 */ 163 static bool ParseTransformIntoMatrix(const nsACString& aValue, 164 bool& aContains3DTransform, 165 gfx::Matrix4x4& aResult); 166 167 /** 168 * Parse a font shorthand for FontFaceSet matching, so we only care about 169 * FontFamily, FontStyle, FontStretch, and FontWeight. 170 * 171 * @param aValue The specified value. 172 * @param aUrl The parser url extra data. 173 * @param aList The parsed FontFamily list. (output) 174 * @param aStyle The parsed FontStyle. (output) 175 * @param aStretch The parsed FontStretch. (output) 176 * @param aWeight The parsed FontWeight. (output) 177 * @param aSize If non-null, returns the parsed font size. (output) 178 * @param aSmallCaps If non-null, whether small-caps was specified (output) 179 * @return Whether the value was successfully parsed. 180 */ 181 static bool ParseFontShorthandForMatching( 182 const nsACString& aValue, URLExtraData* aUrl, StyleFontFamilyList& aList, 183 StyleFontStyle& aStyle, StyleFontStretch& aStretch, 184 StyleFontWeight& aWeight, float* aSize = nullptr, 185 bool* aSmallCaps = nullptr); 186 187 /** 188 * Get a URLExtraData from a document. 189 */ 190 static already_AddRefed<URLExtraData> GetURLExtraData(dom::Document*); 191 192 /** 193 * Get a ParsingEnvironment from a document. 194 */ 195 static ParsingEnvironment GetParsingEnvironment(dom::Document*); 196 }; 197 198 } // namespace mozilla 199 200 #endif // mozilla_ServoCSSParser_h