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 "nsISupports.h"
17 #include "nsWrapperCache.h"
20 struct already_AddRefed
;
24 enum class StyleCssRuleType
: uint8_t;
29 class Rule
: public nsISupports
, public nsWrapperCache
{
31 Rule(StyleSheet
* aSheet
, Rule
* aParentRule
, uint32_t aLineNumber
,
32 uint32_t aColumnNumber
)
34 mParentRule(aParentRule
),
35 mLineNumber(aLineNumber
),
36 mColumnNumber(aColumnNumber
) {
38 AssertParentRuleType();
43 void AssertParentRuleType();
46 Rule(const Rule
& aCopy
)
47 : mSheet(aCopy
.mSheet
),
48 mParentRule(aCopy
.mParentRule
),
49 mLineNumber(aCopy
.mLineNumber
),
50 mColumnNumber(aCopy
.mColumnNumber
) {}
52 virtual ~Rule() = default;
55 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
56 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(Rule
)
57 // Return true if this rule is known to be a cycle collection leaf, in the
58 // sense that it doesn't have any outgoing owning edges.
59 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE
;
61 virtual bool IsGroupRule() const { return false; }
64 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const = 0;
67 StyleSheet
* GetStyleSheet() const { return mSheet
; }
69 // Clear the mSheet pointer on this rule and descendants.
70 virtual void DropSheetReference();
72 // Clear the mParentRule pointer on this rule.
73 void DropParentRuleReference() { mParentRule
= nullptr; }
75 void DropReferences() {
77 DropParentRuleReference();
80 uint32_t GetLineNumber() const { return mLineNumber
; }
81 uint32_t GetColumnNumber() const { return mColumnNumber
; }
83 // Whether this a rule in a read only style sheet.
84 bool IsReadOnly() const;
86 // Whether this rule is an @import rule that hasn't loaded yet (and thus
87 // doesn't affect the style of the page).
88 bool IsIncompleteImportRule() const;
90 // This is pure virtual because all of Rule's data members are non-owning and
91 // thus measured elsewhere.
92 virtual size_t SizeOfIncludingThis(MallocSizeOf
) const MOZ_MUST_OVERRIDE
= 0;
94 virtual StyleCssRuleType
Type() const = 0;
97 uint16_t TypeForBindings() const {
98 auto type
= uint16_t(Type());
99 // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15
101 return type
> 15 ? 0 : type
;
103 virtual void GetCssText(nsACString
& aCssText
) const = 0;
104 void SetCssText(const nsACString
& aCssText
);
105 Rule
* GetParentRule() const;
106 StyleSheet
* GetParentStyleSheet() const { return GetStyleSheet(); }
107 nsINode
* GetAssociatedDocumentOrShadowRoot() const {
111 auto* associated
= mSheet
->GetAssociatedDocumentOrShadowRoot();
112 return associated
? &associated
->AsNode() : nullptr;
114 nsISupports
* GetParentObject() const { return mSheet
; }
117 // True if we're known-live for cycle collection purposes.
118 bool IsKnownLive() const;
120 // Hook subclasses can use to properly unlink the nsWrapperCache of
121 // their declarations.
122 void UnlinkDeclarationWrapper(nsWrapperCache
& aDecl
);
124 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
125 // for an InspectorFontFace.
127 // mSheet and mParentRule will be cleared when they are detached from the
128 // parent object, either because the rule is removed or the parent is
130 StyleSheet
* MOZ_NON_OWNING_REF mSheet
;
131 Rule
* MOZ_NON_OWNING_REF mParentRule
;
133 // Keep the same type so that MSVC packs them.
134 uint32_t mLineNumber
;
135 uint32_t mColumnNumber
;
139 } // namespace mozilla
141 #endif /* mozilla_css_Rule_h___ */