Rule.h (4730B)
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/. */ 6 7 /* base class for all rule types in a CSS style sheet */ 8 9 #ifndef mozilla_css_Rule_h___ 10 #define mozilla_css_Rule_h___ 11 12 #include "mozilla/MemoryReporting.h" 13 #include "mozilla/StyleSheet.h" 14 #include "mozilla/WeakPtr.h" 15 #include "mozilla/dom/CSSRuleBinding.h" 16 #include "mozilla/dom/DocumentOrShadowRoot.h" 17 #include "nsISupports.h" 18 #include "nsWrapperCache.h" 19 20 template <class T> 21 struct already_AddRefed; 22 23 namespace mozilla { 24 25 enum class StyleCssRuleType : uint8_t; 26 27 namespace css { 28 class GroupRule; 29 30 class Rule : public nsISupports, public nsWrapperCache, public SupportsWeakPtr { 31 protected: 32 Rule(StyleSheet* aSheet, Rule* aParentRule, uint32_t aLineNumber, 33 uint32_t aColumnNumber) 34 : mSheet(aSheet), 35 mParentRule(aParentRule), 36 mLineNumber(aLineNumber), 37 mColumnNumber(aColumnNumber) { 38 #ifdef DEBUG 39 AssertParentRuleType(); 40 #endif 41 } 42 43 #ifdef DEBUG 44 void AssertParentRuleType(); 45 #endif 46 47 Rule(const Rule& aCopy) 48 : mSheet(aCopy.mSheet), 49 mParentRule(aCopy.mParentRule), 50 mLineNumber(aCopy.mLineNumber), 51 mColumnNumber(aCopy.mColumnNumber) {} 52 53 virtual ~Rule() = default; 54 55 public: 56 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 57 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(Rule) 58 // Return true if this rule is known to be a cycle collection leaf, in the 59 // sense that it doesn't have any outgoing owning edges. 60 virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE; 61 62 virtual bool IsGroupRule() const { return false; } 63 64 #ifdef DEBUG 65 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0; 66 #endif 67 68 StyleSheet* GetStyleSheet() const { return mSheet; } 69 70 // Clear the mSheet pointer on this rule and descendants. 71 virtual void DropSheetReference(); 72 73 // Clear the mParentRule pointer on this rule. 74 void DropParentRuleReference() { mParentRule = nullptr; } 75 76 void DropReferences() { 77 DropSheetReference(); 78 DropParentRuleReference(); 79 } 80 81 uint32_t GetLineNumber() const { return mLineNumber; } 82 uint32_t GetColumnNumber() const { return mColumnNumber; } 83 84 // Whether this a rule in a read only style sheet. 85 bool IsReadOnly() const; 86 87 // Whether this rule is an @import rule that hasn't loaded yet (and thus 88 // doesn't affect the style of the page). 89 bool IsIncompleteImportRule() const; 90 91 // This is pure virtual because all of Rule's data members are non-owning and 92 // thus measured elsewhere. 93 virtual size_t SizeOfIncludingThis(MallocSizeOf) const MOZ_MUST_OVERRIDE = 0; 94 95 virtual StyleCssRuleType Type() const = 0; 96 97 // WebIDL interface 98 uint16_t TypeForBindings() const { 99 auto type = uint16_t(Type()); 100 // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15 101 // we return 0. 102 return type > 15 ? 0 : type; 103 } 104 virtual void GetCssText(nsACString& aCssText) const = 0; 105 void SetCssText(const nsACString& aCssText); 106 Rule* GetParentRule() const; 107 StyleSheet* GetParentStyleSheet() const { return GetStyleSheet(); } 108 nsINode* GetAssociatedDocumentOrShadowRoot() const { 109 if (!mSheet) { 110 return nullptr; 111 } 112 auto* associated = mSheet->GetAssociatedDocumentOrShadowRoot(); 113 return associated ? &associated->AsNode() : nullptr; 114 } 115 nsISupports* GetParentObject() const { return mSheet; } 116 117 struct ContainingRuleState { 118 uint32_t mContainingTypes = 0; 119 Maybe<StyleCssRuleType> mParseRelativeType; 120 121 static ContainingRuleState From(Rule* aRule) { 122 return aRule ? aRule->GetContainingRuleStateForParsing() 123 : ContainingRuleState(); 124 } 125 }; 126 ContainingRuleState GetContainingRuleStateForParsing() const; 127 128 protected: 129 // True if we're known-live for cycle collection purposes. 130 bool IsKnownLive() const; 131 132 // Hook subclasses can use to properly unlink the nsWrapperCache of 133 // their declarations. 134 void UnlinkDeclarationWrapper(nsWrapperCache& aDecl); 135 136 // mSheet should only ever be null when we create a synthetic CSSFontFaceRule 137 // for an InspectorFontFace. 138 // 139 // mSheet and mParentRule will be cleared when they are detached from the 140 // parent object, either because the rule is removed or the parent is 141 // destroyed. 142 StyleSheet* MOZ_NON_OWNING_REF mSheet; 143 Rule* MOZ_NON_OWNING_REF mParentRule; 144 145 // Keep the same type so that MSVC packs them. 146 uint32_t mLineNumber; 147 uint32_t mColumnNumber; 148 }; 149 150 } // namespace css 151 } // namespace mozilla 152 153 #endif /* mozilla_css_Rule_h___ */