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 #include "ServoCSSParser.h"
10 
11 #include "mozilla/ServoBindings.h"
12 #include "mozilla/ServoStyleSet.h"
13 #include "mozilla/dom/Document.h"
14 
15 using namespace mozilla;
16 using namespace mozilla::dom;
17 
18 /* static */
IsValidCSSColor(const nsACString & aValue)19 bool ServoCSSParser::IsValidCSSColor(const nsACString& aValue) {
20   return Servo_IsValidCSSColor(&aValue);
21 }
22 
23 /* static */
ComputeColor(ServoStyleSet * aStyleSet,nscolor aCurrentColor,const nsACString & aValue,nscolor * aResultColor,bool * aWasCurrentColor,css::Loader * aLoader)24 bool ServoCSSParser::ComputeColor(ServoStyleSet* aStyleSet,
25                                   nscolor aCurrentColor,
26                                   const nsACString& aValue,
27                                   nscolor* aResultColor, bool* aWasCurrentColor,
28                                   css::Loader* aLoader) {
29   return Servo_ComputeColor(aStyleSet ? aStyleSet->RawSet() : nullptr,
30                             aCurrentColor, &aValue, aResultColor,
31                             aWasCurrentColor, aLoader);
32 }
33 
34 /* static */
ParseProperty(nsCSSPropertyID aProperty,const nsAString & aValue,const ParsingEnvironment & aParsingEnvironment,ParsingMode aParsingMode)35 already_AddRefed<RawServoDeclarationBlock> ServoCSSParser::ParseProperty(
36     nsCSSPropertyID aProperty, const nsAString& aValue,
37     const ParsingEnvironment& aParsingEnvironment, ParsingMode aParsingMode) {
38   NS_ConvertUTF16toUTF8 value(aValue);
39   return Servo_ParseProperty(
40              aProperty, &value, aParsingEnvironment.mUrlExtraData, aParsingMode,
41              aParsingEnvironment.mCompatMode, aParsingEnvironment.mLoader)
42       .Consume();
43 }
44 
45 /* static */
ParseEasing(const nsAString & aValue,URLExtraData * aUrl,nsTimingFunction & aResult)46 bool ServoCSSParser::ParseEasing(const nsAString& aValue, URLExtraData* aUrl,
47                                  nsTimingFunction& aResult) {
48   return Servo_ParseEasing(&aValue, aUrl, &aResult);
49 }
50 
51 /* static */
ParseTransformIntoMatrix(const nsACString & aValue,bool & aContains3DTransform,gfx::Matrix4x4 & aResult)52 bool ServoCSSParser::ParseTransformIntoMatrix(const nsACString& aValue,
53                                               bool& aContains3DTransform,
54                                               gfx::Matrix4x4& aResult) {
55   return Servo_ParseTransformIntoMatrix(&aValue, &aContains3DTransform,
56                                         &aResult.components);
57 }
58 
59 /* static */
ParseFontShorthandForMatching(const nsAString & aValue,URLExtraData * aUrl,RefPtr<SharedFontList> & aList,StyleComputedFontStyleDescriptor & aStyle,float & aStretch,float & aWeight)60 bool ServoCSSParser::ParseFontShorthandForMatching(
61     const nsAString& aValue, URLExtraData* aUrl, RefPtr<SharedFontList>& aList,
62     StyleComputedFontStyleDescriptor& aStyle, float& aStretch, float& aWeight) {
63   return Servo_ParseFontShorthandForMatching(&aValue, aUrl, &aList, &aStyle,
64                                              &aStretch, &aWeight);
65 }
66 
67 /* static */
GetURLExtraData(Document * aDocument)68 already_AddRefed<URLExtraData> ServoCSSParser::GetURLExtraData(
69     Document* aDocument) {
70   MOZ_ASSERT(aDocument);
71 
72   nsCOMPtr<nsIReferrerInfo> referrerInfo =
73       ReferrerInfo::CreateForInternalCSSResources(aDocument);
74 
75   // FIXME this is using the wrong base uri (bug 1343919)
76   RefPtr<URLExtraData> url = new URLExtraData(
77       aDocument->GetDocumentURI(), referrerInfo, aDocument->NodePrincipal());
78   return url.forget();
79 }
80 
81 /* static */ ServoCSSParser::ParsingEnvironment
GetParsingEnvironment(Document * aDocument)82 ServoCSSParser::GetParsingEnvironment(Document* aDocument) {
83   return ParsingEnvironment(GetURLExtraData(aDocument),
84                             aDocument->GetCompatibilityMode(),
85                             aDocument->CSSLoader());
86 }
87