|
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 "nsCSSParser.h" |
|
12 #include "nsGlobalWindow.h" |
|
13 #include "nsIDocument.h" |
|
14 #include "nsIURI.h" |
|
15 #include "nsStyleUtil.h" |
|
16 |
|
17 namespace mozilla { |
|
18 namespace dom { |
|
19 |
|
20 struct SupportsParsingInfo |
|
21 { |
|
22 nsIURI* mDocURI; |
|
23 nsIURI* mBaseURI; |
|
24 nsIPrincipal* mPrincipal; |
|
25 }; |
|
26 |
|
27 static nsresult |
|
28 GetParsingInfo(nsISupports* aGlobal, |
|
29 SupportsParsingInfo& aInfo) |
|
30 { |
|
31 if (!aGlobal) { |
|
32 return NS_ERROR_FAILURE; |
|
33 } |
|
34 |
|
35 nsGlobalWindow* win = nsGlobalWindow::FromSupports(aGlobal); |
|
36 nsCOMPtr<nsIDocument> doc = win->GetDoc(); |
|
37 if (!doc) { |
|
38 return NS_ERROR_FAILURE; |
|
39 } |
|
40 |
|
41 aInfo.mDocURI = nsCOMPtr<nsIURI>(doc->GetDocumentURI()); |
|
42 aInfo.mBaseURI = nsCOMPtr<nsIURI>(doc->GetBaseURI()); |
|
43 aInfo.mPrincipal = win->GetPrincipal(); |
|
44 return NS_OK; |
|
45 } |
|
46 |
|
47 /* static */ bool |
|
48 CSS::Supports(const GlobalObject& aGlobal, |
|
49 const nsAString& aProperty, |
|
50 const nsAString& aValue, |
|
51 ErrorResult& aRv) |
|
52 { |
|
53 nsCSSParser parser; |
|
54 SupportsParsingInfo info; |
|
55 |
|
56 nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info); |
|
57 if (NS_FAILED(rv)) { |
|
58 aRv.Throw(rv); |
|
59 return false; |
|
60 } |
|
61 |
|
62 return parser.EvaluateSupportsDeclaration(aProperty, aValue, info.mDocURI, |
|
63 info.mBaseURI, info.mPrincipal); |
|
64 } |
|
65 |
|
66 /* static */ bool |
|
67 CSS::Supports(const GlobalObject& aGlobal, |
|
68 const nsAString& aCondition, |
|
69 ErrorResult& aRv) |
|
70 { |
|
71 nsCSSParser parser; |
|
72 SupportsParsingInfo info; |
|
73 |
|
74 nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info); |
|
75 if (NS_FAILED(rv)) { |
|
76 aRv.Throw(rv); |
|
77 return false; |
|
78 } |
|
79 |
|
80 return parser.EvaluateSupportsCondition(aCondition, info.mDocURI, |
|
81 info.mBaseURI, info.mPrincipal); |
|
82 } |
|
83 |
|
84 /* static */ void |
|
85 CSS::Escape(const GlobalObject& aGlobal, |
|
86 const nsAString& aIdent, |
|
87 nsAString& aReturn, |
|
88 ErrorResult& aRv) |
|
89 { |
|
90 bool success = nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn); |
|
91 |
|
92 if (!success) { |
|
93 aRv.Throw(NS_ERROR_DOM_INVALID_CHARACTER_ERR); |
|
94 } |
|
95 } |
|
96 |
|
97 } // dom |
|
98 } // mozilla |