Bug 1635702 [wpt PR 23413] - Move some internal scroll anchoring tests to wpt, a...
[gecko.git] / layout / style / CSSMozDocumentRule.cpp
blobc506b7a06b8dc9935208e4bfe0dabd785dff4210
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"
12 namespace mozilla {
13 namespace dom {
15 using namespace mozilla::css;
17 /* virtual */
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"));
36 switch (kind) {
37 case Document::MediaDocumentKind::NotMedia:
38 return false;
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");
47 return false;
49 case DocumentMatchingFunction::URL:
50 return aDocURISpec == aPattern;
51 case DocumentMatchingFunction::URLPrefix:
52 return StringBeginsWith(aDocURISpec, aPattern);
53 case DocumentMatchingFunction::Domain: {
54 nsAutoCString host;
55 if (aDocURI) {
56 aDocURI->GetHost(host);
58 int32_t lenDiff = host.Length() - aPattern.Length();
59 if (lenDiff == 0) {
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)
68 .valueOr(false);
71 MOZ_ASSERT_UNREACHABLE("Unknown matching function");
72 return false;
75 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr<RawServoMozDocumentRule> aRawRule,
76 StyleSheet* aSheet,
77 css::Rule* aParentRule, uint32_t aLine,
78 uint32_t aColumn)
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)
90 #ifdef DEBUG
91 /* virtual */
92 void CSSMozDocumentRule::List(FILE* out, int32_t aIndent) const {
93 nsAutoCString str;
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());
100 #endif
102 void CSSMozDocumentRule::GetConditionText(nsAString& aConditionText) {
103 Servo_MozDocumentRule_GetConditionText(mRawRule, &aConditionText);
106 void CSSMozDocumentRule::SetConditionText(const nsAString& aConditionText,
107 ErrorResult& aRv) {
108 if (IsReadOnly()) {
109 return;
112 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
115 /* virtual */
116 void CSSMozDocumentRule::GetCssText(nsAString& aCssText) const {
117 Servo_MozDocumentRule_GetCssText(mRawRule, &aCssText);
120 /* virtual */
121 size_t CSSMozDocumentRule::SizeOfIncludingThis(
122 MallocSizeOf aMallocSizeOf) const {
123 // TODO Implement this!
124 return aMallocSizeOf(this);
127 } // namespace dom
128 } // namespace mozilla