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 #include "mozilla/dom/CSSMozDocumentRule.h"
8 #include "mozilla/dom/CSSMozDocumentRuleBinding.h"
10 #include "mozilla/dom/BrowsingContext.h"
11 #include "mozilla/ServoBindings.h"
12 #include "nsContentUtils.h"
13 #include "nsHTMLDocument.h"
18 using namespace mozilla::css
;
21 JSObject
* CSSMozDocumentRule::WrapObject(JSContext
* aCx
,
22 JS::Handle
<JSObject
*> aGivenProto
) {
23 return CSSMozDocumentRule_Binding::Wrap(aCx
, this, aGivenProto
);
26 bool CSSMozDocumentRule::Match(const Document
* aDoc
, nsIURI
* aDocURI
,
27 const nsACString
& aDocURISpec
,
28 const nsACString
& aPattern
,
29 DocumentMatchingFunction aMatchingFunction
) {
30 switch (aMatchingFunction
) {
31 case DocumentMatchingFunction::MediaDocument
: {
32 auto kind
= aDoc
->MediaDocumentKind();
33 if (aPattern
.EqualsLiteral("all")) {
34 return kind
!= Document::MediaDocumentKind::NotMedia
;
36 MOZ_ASSERT(aPattern
.EqualsLiteral("plugin") ||
37 aPattern
.EqualsLiteral("image") ||
38 aPattern
.EqualsLiteral("video"));
40 case Document::MediaDocumentKind::NotMedia
:
42 case Document::MediaDocumentKind::Plugin
:
43 return aPattern
.EqualsLiteral("plugin");
44 case Document::MediaDocumentKind::Image
:
45 return aPattern
.EqualsLiteral("image");
46 case Document::MediaDocumentKind::Video
:
47 return aPattern
.EqualsLiteral("video");
49 MOZ_ASSERT_UNREACHABLE("Unknown media document kind");
52 case DocumentMatchingFunction::URL
:
53 return aDocURISpec
== aPattern
;
54 case DocumentMatchingFunction::URLPrefix
:
55 return StringBeginsWith(aDocURISpec
, aPattern
);
56 case DocumentMatchingFunction::Domain
: {
59 aDocURI
->GetHost(host
);
61 int32_t lenDiff
= host
.Length() - aPattern
.Length();
63 return host
== aPattern
;
65 return StringEndsWith(host
, aPattern
) && host
.CharAt(lenDiff
- 1) == '.';
67 case DocumentMatchingFunction::RegExp
: {
68 NS_ConvertUTF8toUTF16
spec(aDocURISpec
);
69 NS_ConvertUTF8toUTF16
regex(aPattern
);
70 return nsContentUtils::IsPatternMatching(spec
, regex
, aDoc
)
73 case DocumentMatchingFunction::PlainTextDocument
:
74 return aDoc
->IsHTMLOrXHTML() && aDoc
->AsHTMLDocument()->IsPlainText();
75 case DocumentMatchingFunction::UnobservableDocument
: {
76 const BrowsingContext
* bc
= aDoc
->GetBrowsingContext();
77 return bc
&& bc
->IsTop() && !bc
->HasOpener();
80 MOZ_ASSERT_UNREACHABLE("Unknown matching function");
84 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr
<RawServoMozDocumentRule
> aRawRule
,
86 css::Rule
* aParentRule
, uint32_t aLine
,
88 : css::ConditionRule(Servo_MozDocumentRule_GetRules(aRawRule
).Consume(),
89 aSheet
, aParentRule
, aLine
, aColumn
),
90 mRawRule(std::move(aRawRule
)) {}
92 NS_IMPL_ADDREF_INHERITED(CSSMozDocumentRule
, css::ConditionRule
)
93 NS_IMPL_RELEASE_INHERITED(CSSMozDocumentRule
, css::ConditionRule
)
95 // QueryInterface implementation for MozDocumentRule
96 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMozDocumentRule
)
97 NS_INTERFACE_MAP_END_INHERITING(css::ConditionRule
)
101 void CSSMozDocumentRule::List(FILE* out
, int32_t aIndent
) const {
103 for (int32_t i
= 0; i
< aIndent
; i
++) {
104 str
.AppendLiteral(" ");
106 Servo_MozDocumentRule_Debug(mRawRule
, &str
);
107 fprintf_stderr(out
, "%s\n", str
.get());
111 void CSSMozDocumentRule::GetConditionText(nsACString
& aConditionText
) {
112 Servo_MozDocumentRule_GetConditionText(mRawRule
, &aConditionText
);
115 void CSSMozDocumentRule::SetConditionText(const nsACString
& aConditionText
,
121 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
125 void CSSMozDocumentRule::GetCssText(nsACString
& aCssText
) const {
126 Servo_MozDocumentRule_GetCssText(mRawRule
, &aCssText
);
130 size_t CSSMozDocumentRule::SizeOfIncludingThis(
131 MallocSizeOf aMallocSizeOf
) const {
132 // TODO Implement this!
133 return aMallocSizeOf(this);
137 } // namespace mozilla