Bug 1698786: part 2) Change some compile-time dependent `printf`s to `MOZ_LOG` in...
[gecko.git] / layout / style / CSSMozDocumentRule.cpp
blob145fe706ab70fa48a0c5b407d08b6f9f69a378e6
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"
15 namespace mozilla {
16 namespace dom {
18 using namespace mozilla::css;
20 /* virtual */
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"));
39 switch (kind) {
40 case Document::MediaDocumentKind::NotMedia:
41 return false;
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");
50 return false;
52 case DocumentMatchingFunction::URL:
53 return aDocURISpec == aPattern;
54 case DocumentMatchingFunction::URLPrefix:
55 return StringBeginsWith(aDocURISpec, aPattern);
56 case DocumentMatchingFunction::Domain: {
57 nsAutoCString host;
58 if (aDocURI) {
59 aDocURI->GetHost(host);
61 int32_t lenDiff = host.Length() - aPattern.Length();
62 if (lenDiff == 0) {
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)
71 .valueOr(false);
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");
81 return false;
84 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr<RawServoMozDocumentRule> aRawRule,
85 StyleSheet* aSheet,
86 css::Rule* aParentRule, uint32_t aLine,
87 uint32_t aColumn)
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)
99 #ifdef DEBUG
100 /* virtual */
101 void CSSMozDocumentRule::List(FILE* out, int32_t aIndent) const {
102 nsAutoCString str;
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());
109 #endif
111 void CSSMozDocumentRule::GetConditionText(nsACString& aConditionText) {
112 Servo_MozDocumentRule_GetConditionText(mRawRule, &aConditionText);
115 void CSSMozDocumentRule::SetConditionText(const nsACString& aConditionText,
116 ErrorResult& aRv) {
117 if (IsReadOnly()) {
118 return;
121 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
124 /* virtual */
125 void CSSMozDocumentRule::GetCssText(nsACString& aCssText) const {
126 Servo_MozDocumentRule_GetCssText(mRawRule, &aCssText);
129 /* virtual */
130 size_t CSSMozDocumentRule::SizeOfIncludingThis(
131 MallocSizeOf aMallocSizeOf) const {
132 // TODO Implement this!
133 return aMallocSizeOf(this);
136 } // namespace dom
137 } // namespace mozilla