Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : /* base class for all rule types in a CSS style sheet */
7 :
8 : #ifndef mozilla_css_Rule_h___
9 : #define mozilla_css_Rule_h___
10 :
11 : #include "mozilla/StyleSheet.h"
12 : #include "mozilla/MemoryReporting.h"
13 : #include "nsISupports.h"
14 : #include "nsIDOMCSSRule.h"
15 : #include "nsWrapperCache.h"
16 :
17 : class nsIDocument;
18 : struct nsRuleData;
19 : template<class T> struct already_AddRefed;
20 : class nsHTMLCSSStyleSheet;
21 :
22 : namespace mozilla {
23 : namespace css {
24 : class GroupRule;
25 :
26 : class Rule : public nsIDOMCSSRule
27 : , public nsWrapperCache
28 : {
29 : protected:
30 3297 : Rule(uint32_t aLineNumber, uint32_t aColumnNumber)
31 3297 : : mSheet(nullptr),
32 : mParentRule(nullptr),
33 : mLineNumber(aLineNumber),
34 3297 : mColumnNumber(aColumnNumber)
35 : {
36 3297 : }
37 :
38 0 : Rule(const Rule& aCopy)
39 0 : : mSheet(aCopy.mSheet),
40 0 : mParentRule(aCopy.mParentRule),
41 0 : mLineNumber(aCopy.mLineNumber),
42 0 : mColumnNumber(aCopy.mColumnNumber)
43 : {
44 0 : }
45 :
46 16 : virtual ~Rule() {}
47 :
48 : public:
49 :
50 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51 10433 : NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(Rule)
52 : // Return true if this rule is known to be a cycle collection leaf, in the
53 : // sense that it doesn't have any outgoing owning edges.
54 : virtual bool IsCCLeaf() const MOZ_MUST_OVERRIDE;
55 :
56 : // nsIDOMCSSRule interface
57 : NS_DECL_NSIDOMCSSRULE
58 :
59 : #ifdef DEBUG
60 : virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
61 : #endif
62 :
63 : // The constants in this list must maintain the following invariants:
64 : // If a rule of type N must appear before a rule of type M in stylesheets
65 : // then N < M
66 : // Note that CSSStyleSheet::RebuildChildList assumes that no other kinds of
67 : // rules can come between two rules of type IMPORT_RULE.
68 : enum {
69 : UNKNOWN_RULE = 0,
70 : CHARSET_RULE,
71 : IMPORT_RULE,
72 : NAMESPACE_RULE,
73 : STYLE_RULE,
74 : MEDIA_RULE,
75 : FONT_FACE_RULE,
76 : PAGE_RULE,
77 : KEYFRAME_RULE,
78 : KEYFRAMES_RULE,
79 : DOCUMENT_RULE,
80 : SUPPORTS_RULE,
81 : FONT_FEATURE_VALUES_RULE,
82 : COUNTER_STYLE_RULE
83 : };
84 :
85 : virtual int32_t GetType() const = 0;
86 :
87 844 : StyleSheet* GetStyleSheet() const { return mSheet; }
88 :
89 : // Return the document the rule lives in, if any
90 254 : nsIDocument* GetDocument() const
91 : {
92 254 : StyleSheet* sheet = GetStyleSheet();
93 254 : return sheet ? sheet->GetAssociatedDocument() : nullptr;
94 : }
95 :
96 : virtual void SetStyleSheet(StyleSheet* aSheet);
97 :
98 207 : void SetParentRule(GroupRule* aRule) {
99 : // We don't reference count this up reference. The group rule
100 : // will tell us when it's going away or when we're detached from
101 : // it.
102 207 : mParentRule = aRule;
103 207 : }
104 :
105 0 : uint32_t GetLineNumber() const { return mLineNumber; }
106 0 : uint32_t GetColumnNumber() const { return mColumnNumber; }
107 :
108 : /**
109 : * Clones |this|. Never returns nullptr.
110 : */
111 : virtual already_AddRefed<Rule> Clone() const = 0;
112 :
113 : // This is pure virtual because all of Rule's data members are non-owning and
114 : // thus measured elsewhere.
115 : virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
116 : const MOZ_MUST_OVERRIDE = 0;
117 :
118 : // WebIDL interface, aka helpers for nsIDOMCSSRule implementation.
119 : virtual uint16_t Type() const = 0;
120 : virtual void GetCssTextImpl(nsAString& aCssText) const = 0;
121 0 : void GetCssText(nsAString& aCssText) const { GetCssTextImpl(aCssText); }
122 : // XPCOM SetCssText is OK, since it never throws.
123 : Rule* GetParentRule() const;
124 0 : StyleSheet* GetParentStyleSheet() const { return GetStyleSheet(); }
125 0 : nsIDocument* GetParentObject() const { return GetDocument(); }
126 :
127 : protected:
128 : // True if we're known-live for cycle collection purposes.
129 : bool IsKnownLive() const;
130 :
131 : // This is sometimes null (e.g., for style attributes).
132 : StyleSheet* mSheet;
133 : // When the parent GroupRule is destroyed, it will call SetParentRule(nullptr)
134 : // on this object. (Through SetParentRuleReference);
135 : GroupRule* MOZ_NON_OWNING_REF mParentRule;
136 :
137 : // Keep the same type so that MSVC packs them.
138 : uint32_t mLineNumber;
139 : uint32_t mColumnNumber;
140 : };
141 :
142 : } // namespace css
143 : } // namespace mozilla
144 :
145 : #endif /* mozilla_css_Rule_h___ */
|