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 #ifndef mozilla_css_Rule_h___
10 #define mozilla_css_Rule_h___
12 #include "mozilla/dom/CSSRuleBinding.h"
13 #include "mozilla/dom/DocumentOrShadowRoot.h"
14 #include "mozilla/StyleSheet.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/WeakPtr.h"
17 #include "nsISupports.h"
18 #include "nsWrapperCache.h"
21 struct already_AddRefed
;
25 enum class StyleCssRuleType
: uint8_t;
30 class Rule
: public nsISupports
, public nsWrapperCache
, public SupportsWeakPtr
{
32 Rule(StyleSheet
* aSheet
, Rule
* aParentRule
, uint32_t aLineNumber
,
33 uint32_t aColumnNumber
)
35 mParentRule(aParentRule
),
36 mLineNumber(aLineNumber
),
37 mColumnNumber(aColumnNumber
) {
39 AssertParentRuleType();
44 void AssertParentRuleType();
47 Rule(const Rule
& aCopy
)
48 : mSheet(aCopy
.mSheet
),
49 mParentRule(aCopy
.mParentRule
),
50 mLineNumber(aCopy
.mLineNumber
),
51 mColumnNumber(aCopy
.mColumnNumber
) {}
53 virtual ~Rule() = default;
56 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
57 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(Rule
)
58 // Return true if this rule is known to be a cycle collection leaf, in the
59 // sense that it doesn't have any outgoing owning edges.
60 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE
;
62 virtual bool IsGroupRule() const { return false; }
65 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const = 0;
68 StyleSheet
* GetStyleSheet() const { return mSheet
; }
70 // Clear the mSheet pointer on this rule and descendants.
71 virtual void DropSheetReference();
73 // Clear the mParentRule pointer on this rule.
74 void DropParentRuleReference() { mParentRule
= nullptr; }
76 void DropReferences() {
78 DropParentRuleReference();
81 uint32_t GetLineNumber() const { return mLineNumber
; }
82 uint32_t GetColumnNumber() const { return mColumnNumber
; }
84 // Whether this a rule in a read only style sheet.
85 bool IsReadOnly() const;
87 // Whether this rule is an @import rule that hasn't loaded yet (and thus
88 // doesn't affect the style of the page).
89 bool IsIncompleteImportRule() const;
91 // This is pure virtual because all of Rule's data members are non-owning and
92 // thus measured elsewhere.
93 virtual size_t SizeOfIncludingThis(MallocSizeOf
) const MOZ_MUST_OVERRIDE
= 0;
95 virtual StyleCssRuleType
Type() const = 0;
98 uint16_t TypeForBindings() const {
99 auto type
= uint16_t(Type());
100 // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15
102 return type
> 15 ? 0 : type
;
104 virtual void GetCssText(nsACString
& aCssText
) const = 0;
105 void SetCssText(const nsACString
& aCssText
);
106 Rule
* GetParentRule() const;
107 StyleSheet
* GetParentStyleSheet() const { return GetStyleSheet(); }
108 nsINode
* GetAssociatedDocumentOrShadowRoot() const {
112 auto* associated
= mSheet
->GetAssociatedDocumentOrShadowRoot();
113 return associated
? &associated
->AsNode() : nullptr;
115 nsISupports
* GetParentObject() const { return mSheet
; }
117 struct ContainingRuleState
{
118 uint32_t mContainingTypes
= 0;
119 Maybe
<StyleCssRuleType
> mParseRelativeType
;
121 static ContainingRuleState
From(Rule
* aRule
) {
122 return aRule
? aRule
->GetContainingRuleStateForParsing()
123 : ContainingRuleState();
126 ContainingRuleState
GetContainingRuleStateForParsing() const;
129 // True if we're known-live for cycle collection purposes.
130 bool IsKnownLive() const;
132 // Hook subclasses can use to properly unlink the nsWrapperCache of
133 // their declarations.
134 void UnlinkDeclarationWrapper(nsWrapperCache
& aDecl
);
136 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
137 // for an InspectorFontFace.
139 // mSheet and mParentRule will be cleared when they are detached from the
140 // parent object, either because the rule is removed or the parent is
142 StyleSheet
* MOZ_NON_OWNING_REF mSheet
;
143 Rule
* MOZ_NON_OWNING_REF mParentRule
;
145 // Keep the same type so that MSVC packs them.
146 uint32_t mLineNumber
;
147 uint32_t mColumnNumber
;
151 } // namespace mozilla
153 #endif /* mozilla_css_Rule_h___ */