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"
21 struct already_AddRefed
;
22 class nsHTMLCSSStyleSheet
;
26 enum class StyleCssRuleType
: uint8_t;
31 class Rule
: public nsISupports
, public nsWrapperCache
{
33 Rule(StyleSheet
* aSheet
, Rule
* aParentRule
, uint32_t aLineNumber
,
34 uint32_t aColumnNumber
)
36 mParentRule(aParentRule
),
37 mLineNumber(aLineNumber
),
38 mColumnNumber(aColumnNumber
) {
40 AssertParentRuleType();
45 void AssertParentRuleType();
48 Rule(const Rule
& aCopy
)
49 : mSheet(aCopy
.mSheet
),
50 mParentRule(aCopy
.mParentRule
),
51 mLineNumber(aCopy
.mLineNumber
),
52 mColumnNumber(aCopy
.mColumnNumber
) {}
54 virtual ~Rule() = default;
57 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
58 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(Rule
)
59 // Return true if this rule is known to be a cycle collection leaf, in the
60 // sense that it doesn't have any outgoing owning edges.
61 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE
;
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 {
115 return mSheet
? mSheet
->GetRelevantGlobal() : nullptr;
119 // True if we're known-live for cycle collection purposes.
120 bool IsKnownLive() const;
122 // Hook subclasses can use to properly unlink the nsWrapperCache of
123 // their declarations.
124 void UnlinkDeclarationWrapper(nsWrapperCache
& aDecl
);
126 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
127 // for an InspectorFontFace.
129 // mSheet and mParentRule will be cleared when they are detached from the
130 // parent object, either because the rule is removed or the parent is
132 StyleSheet
* MOZ_NON_OWNING_REF mSheet
;
133 Rule
* MOZ_NON_OWNING_REF mParentRule
;
135 // Keep the same type so that MSVC packs them.
136 uint32_t mLineNumber
;
137 uint32_t mColumnNumber
;
141 } // namespace mozilla
143 #endif /* mozilla_css_Rule_h___ */