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
;
28 class Rule
: public nsISupports
, public nsWrapperCache
{
30 Rule(StyleSheet
* aSheet
, Rule
* aParentRule
, uint32_t aLineNumber
,
31 uint32_t aColumnNumber
)
33 mParentRule(aParentRule
),
34 mLineNumber(aLineNumber
),
35 mColumnNumber(aColumnNumber
) {
37 // Would be nice to check that this->Type() is KEYFRAME_RULE when
38 // mParentRule->Tye() is KEYFRAMES_RULE, but we can't call
39 // this->Type() here since it's virtual.
41 int16_t type
= mParentRule
->Type();
42 MOZ_ASSERT(type
== dom::CSSRule_Binding::MEDIA_RULE
||
43 type
== dom::CSSRule_Binding::DOCUMENT_RULE
||
44 type
== dom::CSSRule_Binding::SUPPORTS_RULE
||
45 type
== dom::CSSRule_Binding::KEYFRAMES_RULE
);
50 Rule(const Rule
& aCopy
)
51 : mSheet(aCopy
.mSheet
),
52 mParentRule(aCopy
.mParentRule
),
53 mLineNumber(aCopy
.mLineNumber
),
54 mColumnNumber(aCopy
.mColumnNumber
) {}
56 virtual ~Rule() = default;
59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
60 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(Rule
)
61 // Return true if this rule is known to be a cycle collection leaf, in the
62 // sense that it doesn't have any outgoing owning edges.
63 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE
;
66 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const = 0;
69 StyleSheet
* GetStyleSheet() const { return mSheet
; }
71 // Clear the mSheet pointer on this rule and descendants.
72 virtual void DropSheetReference();
74 // Clear the mParentRule pointer on this rule.
75 void DropParentRuleReference() { mParentRule
= nullptr; }
77 void DropReferences() {
79 DropParentRuleReference();
82 uint32_t GetLineNumber() const { return mLineNumber
; }
83 uint32_t GetColumnNumber() const { return mColumnNumber
; }
85 // Whether this a rule in a read only style sheet.
86 bool IsReadOnly() const;
88 // Whether this rule is an @import rule that hasn't loaded yet (and thus
89 // doesn't affect the style of the page).
90 bool IsIncompleteImportRule() const;
92 // This is pure virtual because all of Rule's data members are non-owning and
93 // thus measured elsewhere.
94 virtual size_t SizeOfIncludingThis(MallocSizeOf
) const MOZ_MUST_OVERRIDE
= 0;
97 virtual uint16_t Type() const = 0;
98 virtual void GetCssText(nsAString
& aCssText
) const = 0;
99 void SetCssText(const nsAString
& aCssText
);
100 Rule
* GetParentRule() const;
101 StyleSheet
* GetParentStyleSheet() const { return GetStyleSheet(); }
102 nsINode
* GetParentObject() const {
106 auto* associated
= mSheet
->GetAssociatedDocumentOrShadowRoot();
107 return associated
? &associated
->AsNode() : nullptr;
111 // True if we're known-live for cycle collection purposes.
112 bool IsKnownLive() const;
114 // Hook subclasses can use to properly unlink the nsWrapperCache of
115 // their declarations.
116 void UnlinkDeclarationWrapper(nsWrapperCache
& aDecl
);
118 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule
119 // for an InspectorFontFace.
121 // mSheet and mParentRule will be cleared when they are detached from the
122 // parent object, either because the rule is removed or the parent is
124 StyleSheet
* MOZ_NON_OWNING_REF mSheet
;
125 Rule
* MOZ_NON_OWNING_REF mParentRule
;
127 // Keep the same type so that MSVC packs them.
128 uint32_t mLineNumber
;
129 uint32_t mColumnNumber
;
133 } // namespace mozilla
135 #endif /* mozilla_css_Rule_h___ */