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"
9 #include "mozilla/ServoBindings.h"
10 #include "nsContentUtils.h"
15 using namespace mozilla::css
;
18 JSObject
* CSSMozDocumentRule::WrapObject(JSContext
* aCx
,
19 JS::Handle
<JSObject
*> aGivenProto
) {
20 return CSSMozDocumentRule_Binding::Wrap(aCx
, this, aGivenProto
);
23 bool CSSMozDocumentRule::Match(const Document
* aDoc
, nsIURI
* aDocURI
,
24 const nsACString
& aDocURISpec
,
25 const nsACString
& aPattern
,
26 DocumentMatchingFunction aMatchingFunction
) {
27 switch (aMatchingFunction
) {
28 case DocumentMatchingFunction::MediaDocument
: {
29 auto kind
= aDoc
->MediaDocumentKind();
30 if (aPattern
.EqualsLiteral("all")) {
31 return kind
!= Document::MediaDocumentKind::NotMedia
;
33 MOZ_ASSERT(aPattern
.EqualsLiteral("plugin") ||
34 aPattern
.EqualsLiteral("image") ||
35 aPattern
.EqualsLiteral("video"));
37 case Document::MediaDocumentKind::NotMedia
:
39 case Document::MediaDocumentKind::Plugin
:
40 return aPattern
.EqualsLiteral("plugin");
41 case Document::MediaDocumentKind::Image
:
42 return aPattern
.EqualsLiteral("image");
43 case Document::MediaDocumentKind::Video
:
44 return aPattern
.EqualsLiteral("video");
46 MOZ_ASSERT_UNREACHABLE("Unknown media document kind");
49 case DocumentMatchingFunction::URL
:
50 return aDocURISpec
== aPattern
;
51 case DocumentMatchingFunction::URLPrefix
:
52 return StringBeginsWith(aDocURISpec
, aPattern
);
53 case DocumentMatchingFunction::Domain
: {
56 aDocURI
->GetHost(host
);
58 int32_t lenDiff
= host
.Length() - aPattern
.Length();
60 return host
== aPattern
;
62 return StringEndsWith(host
, aPattern
) && host
.CharAt(lenDiff
- 1) == '.';
64 case DocumentMatchingFunction::RegExp
: {
65 NS_ConvertUTF8toUTF16
spec(aDocURISpec
);
66 NS_ConvertUTF8toUTF16
regex(aPattern
);
67 return nsContentUtils::IsPatternMatching(spec
, regex
, aDoc
)
71 MOZ_ASSERT_UNREACHABLE("Unknown matching function");
75 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr
<RawServoMozDocumentRule
> aRawRule
,
77 css::Rule
* aParentRule
, uint32_t aLine
,
79 : css::ConditionRule(Servo_MozDocumentRule_GetRules(aRawRule
).Consume(),
80 aSheet
, aParentRule
, aLine
, aColumn
),
81 mRawRule(std::move(aRawRule
)) {}
83 NS_IMPL_ADDREF_INHERITED(CSSMozDocumentRule
, css::ConditionRule
)
84 NS_IMPL_RELEASE_INHERITED(CSSMozDocumentRule
, css::ConditionRule
)
86 // QueryInterface implementation for MozDocumentRule
87 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMozDocumentRule
)
88 NS_INTERFACE_MAP_END_INHERITING(css::ConditionRule
)
92 void CSSMozDocumentRule::List(FILE* out
, int32_t aIndent
) const {
94 for (int32_t i
= 0; i
< aIndent
; i
++) {
95 str
.AppendLiteral(" ");
97 Servo_MozDocumentRule_Debug(mRawRule
, &str
);
98 fprintf_stderr(out
, "%s\n", str
.get());
102 void CSSMozDocumentRule::GetConditionText(nsAString
& aConditionText
) {
103 Servo_MozDocumentRule_GetConditionText(mRawRule
, &aConditionText
);
106 void CSSMozDocumentRule::SetConditionText(const nsAString
& aConditionText
,
112 aRv
.Throw(NS_ERROR_NOT_IMPLEMENTED
);
116 void CSSMozDocumentRule::GetCssText(nsAString
& aCssText
) const {
117 Servo_MozDocumentRule_GetCssText(mRawRule
, &aCssText
);
121 size_t CSSMozDocumentRule::SizeOfIncludingThis(
122 MallocSizeOf aMallocSizeOf
) const {
123 // TODO Implement this!
124 return aMallocSizeOf(this);
128 } // namespace mozilla