1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /* DOM object holding utility CSS functions */
7 
8 #include "CSS.h"
9 
10 #include "mozilla/dom/BindingDeclarations.h"
11 #include "mozilla/ServoBindings.h"
12 #include "nsCSSParser.h"
13 #include "nsGlobalWindow.h"
14 #include "nsIDocument.h"
15 #include "nsIURI.h"
16 #include "nsStyleUtil.h"
17 #include "xpcpublic.h"
18 
19 namespace mozilla {
20 namespace dom {
21 
22 struct SupportsParsingInfo
23 {
24   nsIURI* mDocURI;
25   nsIURI* mBaseURI;
26   nsIPrincipal* mPrincipal;
27   StyleBackendType mStyleBackendType;
28 };
29 
30 static nsresult
GetParsingInfo(const GlobalObject & aGlobal,SupportsParsingInfo & aInfo)31 GetParsingInfo(const GlobalObject& aGlobal,
32                SupportsParsingInfo& aInfo)
33 {
34   nsGlobalWindow* win = xpc::WindowOrNull(aGlobal.Get());
35   if (!win) {
36     return NS_ERROR_FAILURE;
37   }
38 
39   nsCOMPtr<nsIDocument> doc = win->GetDoc();
40   if (!doc) {
41     return NS_ERROR_FAILURE;
42   }
43 
44   aInfo.mDocURI = nsCOMPtr<nsIURI>(doc->GetDocumentURI()).get();
45   aInfo.mBaseURI = nsCOMPtr<nsIURI>(doc->GetBaseURI()).get();
46   aInfo.mPrincipal = win->GetPrincipal();
47   aInfo.mStyleBackendType = doc->GetStyleBackendType();
48   return NS_OK;
49 }
50 
51 /* static */ bool
Supports(const GlobalObject & aGlobal,const nsAString & aProperty,const nsAString & aValue,ErrorResult & aRv)52 CSS::Supports(const GlobalObject& aGlobal,
53               const nsAString& aProperty,
54               const nsAString& aValue,
55               ErrorResult& aRv)
56 {
57   SupportsParsingInfo info;
58 
59   nsresult rv = GetParsingInfo(aGlobal, info);
60   if (NS_FAILED(rv)) {
61     aRv.Throw(rv);
62     return false;
63   }
64 
65   if (info.mStyleBackendType == StyleBackendType::Servo) {
66     NS_ConvertUTF16toUTF8 property(aProperty);
67     NS_ConvertUTF16toUTF8 value(aValue);
68     return Servo_CSSSupports(&property, &value);
69   }
70 
71   nsCSSParser parser;
72   return parser.EvaluateSupportsDeclaration(aProperty, aValue, info.mDocURI,
73                                             info.mBaseURI, info.mPrincipal);
74 }
75 
76 /* static */ bool
Supports(const GlobalObject & aGlobal,const nsAString & aCondition,ErrorResult & aRv)77 CSS::Supports(const GlobalObject& aGlobal,
78               const nsAString& aCondition,
79               ErrorResult& aRv)
80 {
81   SupportsParsingInfo info;
82 
83   nsresult rv = GetParsingInfo(aGlobal, info);
84   if (NS_FAILED(rv)) {
85     aRv.Throw(rv);
86     return false;
87   }
88 
89   if (info.mStyleBackendType == StyleBackendType::Servo) {
90     MOZ_CRASH("stylo: CSS.supports() with arguments is not yet implemented");
91   }
92 
93   nsCSSParser parser;
94   return parser.EvaluateSupportsCondition(aCondition, info.mDocURI,
95                                           info.mBaseURI, info.mPrincipal);
96 }
97 
98 /* static */ void
Escape(const GlobalObject & aGlobal,const nsAString & aIdent,nsAString & aReturn)99 CSS::Escape(const GlobalObject& aGlobal,
100             const nsAString& aIdent,
101             nsAString& aReturn)
102 {
103   nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn);
104 }
105 
106 } // namespace dom
107 } // namespace mozilla
108