Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / style / CSSMozDocumentRule.cpp
blobb59a66d85640b933f52cc33f0bdac43895eb4500
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 "js/RegExpFlags.h"
11 #include "mozilla/dom/BrowsingContext.h"
12 #include "mozilla/ServoBindings.h"
13 #include "nsContentUtils.h"
14 #include "nsHTMLDocument.h"
16 namespace mozilla::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 // Using JS::RegExpFlag::Unicode to allow patterns containing for example
69 // [^/].
70 return nsContentUtils::IsPatternMatching(
71 NS_ConvertUTF8toUTF16(aDocURISpec),
72 NS_ConvertUTF8toUTF16(aPattern), aDoc,
73 /* aHasMultiple = */ false, JS::RegExpFlag::Unicode)
74 .valueOr(false);
76 case DocumentMatchingFunction::PlainTextDocument:
77 return aDoc->IsHTMLOrXHTML() && aDoc->AsHTMLDocument()->IsPlainText();
78 case DocumentMatchingFunction::UnobservableDocument: {
79 const BrowsingContext* bc = aDoc->GetBrowsingContext();
80 return bc && bc->IsTop() && !bc->HasOpener();
83 MOZ_ASSERT_UNREACHABLE("Unknown matching function");
84 return false;
87 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr<StyleDocumentRule> aRawRule,
88 StyleSheet* aSheet,
89 css::Rule* aParentRule, uint32_t aLine,
90 uint32_t aColumn)
91 : css::ConditionRule(aSheet, aParentRule, aLine, aColumn),
92 mRawRule(std::move(aRawRule)) {}
94 NS_IMPL_ADDREF_INHERITED(CSSMozDocumentRule, css::ConditionRule)
95 NS_IMPL_RELEASE_INHERITED(CSSMozDocumentRule, css::ConditionRule)
97 // QueryInterface implementation for MozDocumentRule
98 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMozDocumentRule)
99 NS_INTERFACE_MAP_END_INHERITING(css::ConditionRule)
101 #ifdef DEBUG
102 /* virtual */
103 void CSSMozDocumentRule::List(FILE* out, int32_t aIndent) const {
104 nsAutoCString str;
105 for (int32_t i = 0; i < aIndent; i++) {
106 str.AppendLiteral(" ");
108 Servo_DocumentRule_Debug(mRawRule, &str);
109 fprintf_stderr(out, "%s\n", str.get());
111 #endif
113 void CSSMozDocumentRule::SetRawAfterClone(RefPtr<StyleDocumentRule> aRaw) {
114 mRawRule = std::move(aRaw);
115 css::ConditionRule::DidSetRawAfterClone();
118 already_AddRefed<StyleLockedCssRules>
119 CSSMozDocumentRule::GetOrCreateRawRules() {
120 return Servo_DocumentRule_GetRules(mRawRule).Consume();
123 StyleCssRuleType CSSMozDocumentRule::Type() const {
124 return StyleCssRuleType::Document;
127 void CSSMozDocumentRule::GetConditionText(nsACString& aConditionText) {
128 Servo_DocumentRule_GetConditionText(mRawRule, &aConditionText);
131 /* virtual */
132 void CSSMozDocumentRule::GetCssText(nsACString& aCssText) const {
133 Servo_DocumentRule_GetCssText(mRawRule, &aCssText);
136 /* virtual */
137 size_t CSSMozDocumentRule::SizeOfIncludingThis(
138 MallocSizeOf aMallocSizeOf) const {
139 // TODO Implement this!
140 return aMallocSizeOf(this);
143 } // namespace mozilla::dom