Bug 1833466 - Implement CSSMarginRule and the corresponding DOM API. r=webidl,firefox...
[gecko.git] / layout / style / Rule.cpp
blobdeaa46576caa91bff8c7a85dd06726104b7f9569
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 /* base class for all rule types in a CSS style sheet */
9 #include "Rule.h"
11 #include "mozilla/css/GroupRule.h"
12 #include "mozilla/dom/CSSImportRule.h"
13 #include "mozilla/dom/DocumentOrShadowRoot.h"
14 #include "nsCCUncollectableMarker.h"
15 #include "mozilla/dom/Document.h"
16 #include "mozilla/HoldDropJSObjects.h"
17 #include "nsWrapperCacheInlines.h"
18 #include "mozilla/ServoBindings.h"
20 using namespace mozilla;
21 using namespace mozilla::dom;
23 namespace mozilla::css {
25 NS_IMPL_CYCLE_COLLECTING_ADDREF(Rule)
26 NS_IMPL_CYCLE_COLLECTING_RELEASE(Rule)
28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Rule)
29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
30 NS_INTERFACE_MAP_ENTRY(nsISupports)
31 NS_INTERFACE_MAP_END
33 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Rule)
35 bool Rule::IsCCLeaf() const { return !PreservingWrapper(); }
37 bool Rule::IsKnownLive() const {
38 if (HasKnownLiveWrapper()) {
39 return true;
42 StyleSheet* sheet = GetStyleSheet();
43 if (!sheet) {
44 return false;
47 Document* doc = sheet->GetKeptAliveByDocument();
48 return doc &&
49 nsCCUncollectableMarker::InGeneration(doc->GetMarkedCCGeneration());
52 void Rule::UnlinkDeclarationWrapper(nsWrapperCache& aDecl) {
53 // We have to be a bit careful here. We have two separate nsWrapperCache
54 // instances, aDecl and this, that both correspond to the same CC participant:
55 // this. If we just used ReleaseWrapper() on one of them, that would
56 // unpreserve that one wrapper, then trace us with a tracer that clears JS
57 // things, and we would clear the wrapper on the cache that has not
58 // unpreserved the wrapper yet. That would violate the invariant that the
59 // cache keeps caching the wrapper until the wrapper dies.
61 // So we reimplement a modified version of nsWrapperCache::ReleaseWrapper here
62 // that unpreserves both wrappers before doing any clearing.
63 bool needDrop = PreservingWrapper() || aDecl.PreservingWrapper();
64 SetPreservingWrapper(false);
65 aDecl.SetPreservingWrapper(false);
66 if (needDrop) {
67 DropJSObjects(this);
71 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(Rule)
72 return tmp->IsCCLeaf() || tmp->IsKnownLive();
73 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
75 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(Rule)
76 // Please see documentation for nsCycleCollectionParticipant::CanSkip* for why
77 // we need to check HasNothingToTrace here but not in the other two CanSkip
78 // methods.
79 return tmp->IsCCLeaf() || (tmp->IsKnownLive() && tmp->HasNothingToTrace(tmp));
80 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
82 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(Rule)
83 return tmp->IsCCLeaf() || tmp->IsKnownLive();
84 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
86 /* virtual */
87 void Rule::DropSheetReference() { mSheet = nullptr; }
89 void Rule::SetCssText(const nsACString& aCssText) {
90 // We used to throw for some rule types, but not all. Specifically, we did
91 // not throw for StyleRule. Let's just always not throw.
94 Rule* Rule::GetParentRule() const { return mParentRule; }
96 #ifdef DEBUG
97 void Rule::AssertParentRuleType() {
98 // Would be nice to check that this->Type() is StyleCssRuleType::Keyframe
99 // when mParentRule->Tye() is StyleCssRuleType::Keyframes, but we can't call
100 // this->Type() here since it's virtual.
101 // Same for StyleCssRuleType::Margin and StyleCssRuleType::Page.
102 if (mParentRule) {
103 auto type = mParentRule->Type();
104 MOZ_ASSERT(type == StyleCssRuleType::Media ||
105 type == StyleCssRuleType::Style ||
106 type == StyleCssRuleType::Document ||
107 type == StyleCssRuleType::Supports ||
108 type == StyleCssRuleType::Keyframes ||
109 type == StyleCssRuleType::LayerBlock ||
110 type == StyleCssRuleType::Container ||
111 type == StyleCssRuleType::Scope ||
112 type == StyleCssRuleType::StartingStyle ||
113 type == StyleCssRuleType::Page);
116 #endif
118 bool Rule::IsReadOnly() const {
119 MOZ_ASSERT(!mSheet || !mParentRule ||
120 mSheet->IsReadOnly() == mParentRule->IsReadOnly(),
121 "a parent rule should be read only iff the owning sheet is "
122 "read only");
123 return mSheet && mSheet->IsReadOnly();
126 bool Rule::IsIncompleteImportRule() const {
127 if (Type() != StyleCssRuleType::Import) {
128 return false;
130 auto* sheet = static_cast<const dom::CSSImportRule*>(this)->GetStyleSheet();
131 return !sheet || !sheet->IsComplete();
134 } // namespace mozilla::css