Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / style / Rule.h
blob719ccc7b752f7bc7625e9535ec6296074a354c88
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"
19 template <class T>
20 struct already_AddRefed;
22 namespace mozilla {
24 enum class StyleCssRuleType : uint8_t;
26 namespace css {
27 class GroupRule;
29 class Rule : public nsISupports, public nsWrapperCache {
30 protected:
31 Rule(StyleSheet* aSheet, Rule* aParentRule, uint32_t aLineNumber,
32 uint32_t aColumnNumber)
33 : mSheet(aSheet),
34 mParentRule(aParentRule),
35 mLineNumber(aLineNumber),
36 mColumnNumber(aColumnNumber) {
37 #ifdef DEBUG
38 AssertParentRuleType();
39 #endif
42 #ifdef DEBUG
43 void AssertParentRuleType();
44 #endif
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;
54 public:
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; }
63 #ifdef DEBUG
64 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
65 #endif
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() {
76 DropSheetReference();
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;
96 // WebIDL interface
97 uint16_t TypeForBindings() const {
98 auto type = uint16_t(Type());
99 // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15
100 // we return 0.
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 {
108 if (!mSheet) {
109 return nullptr;
111 auto* associated = mSheet->GetAssociatedDocumentOrShadowRoot();
112 return associated ? &associated->AsNode() : nullptr;
114 nsISupports* GetParentObject() const {
115 return mSheet ? mSheet->GetRelevantGlobal() : nullptr;
118 protected:
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
131 // destroyed.
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;
140 } // namespace css
141 } // namespace mozilla
143 #endif /* mozilla_css_Rule_h___ */