Bug 1730256 [wpt PR 30555] - Move getWindowSegments to visualViewport.segments, a...
[gecko.git] / layout / style / Rule.h
blob4bc84121d55f917826b65c757d913307e20ef0c7
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 struct nsRuleData;
20 template <class T>
21 struct already_AddRefed;
22 class nsHTMLCSSStyleSheet;
24 namespace mozilla {
26 enum class StyleCssRuleType : uint8_t;
28 namespace css {
29 class GroupRule;
31 class Rule : public nsISupports, public nsWrapperCache {
32 protected:
33 Rule(StyleSheet* aSheet, Rule* aParentRule, uint32_t aLineNumber,
34 uint32_t aColumnNumber)
35 : mSheet(aSheet),
36 mParentRule(aParentRule),
37 mLineNumber(aLineNumber),
38 mColumnNumber(aColumnNumber) {
39 #ifdef DEBUG
40 AssertParentRuleType();
41 #endif
44 #ifdef DEBUG
45 void AssertParentRuleType();
46 #endif
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;
56 public:
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;
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___ */