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/StyleSheetInlines.h"
12 #include "mozilla/MemoryReporting.h"
13 #include "nsISupports.h"
14 #include "nsIDOMCSSRule.h"
15 
16 class nsIDocument;
17 struct nsRuleData;
18 template<class T> struct already_AddRefed;
19 class nsHTMLCSSStyleSheet;
20 
21 namespace mozilla {
22 namespace css {
23 class GroupRule;
24 
25 #define DECL_STYLE_RULE_INHERIT_NO_DOMRULE  \
26  /* nothing */
27 
28 #define DECL_STYLE_RULE_INHERIT                            \
29   DECL_STYLE_RULE_INHERIT_NO_DOMRULE                       \
30   virtual nsIDOMCSSRule* GetDOMRule() override;        \
31   virtual nsIDOMCSSRule* GetExistingDOMRule() override;
32 
33 class Rule : public nsISupports {
34 protected:
Rule(uint32_t aLineNumber,uint32_t aColumnNumber)35   Rule(uint32_t aLineNumber, uint32_t aColumnNumber)
36     : mSheet(nullptr),
37       mParentRule(nullptr),
38       mLineNumber(aLineNumber),
39       mColumnNumber(aColumnNumber)
40   {
41   }
42 
Rule(const Rule & aCopy)43   Rule(const Rule& aCopy)
44     : mSheet(aCopy.mSheet),
45       mParentRule(aCopy.mParentRule),
46       mLineNumber(aCopy.mLineNumber),
47       mColumnNumber(aCopy.mColumnNumber)
48   {
49   }
50 
~Rule()51   virtual ~Rule() {}
52 
53 public:
54 
55 #ifdef DEBUG
56   virtual void List(FILE* out = stdout, int32_t aIndent = 0) const = 0;
57 #endif
58 
59   // The constants in this list must maintain the following invariants:
60   //   If a rule of type N must appear before a rule of type M in stylesheets
61   //   then N < M
62   // Note that CSSStyleSheet::RebuildChildList assumes that no other kinds of
63   // rules can come between two rules of type IMPORT_RULE.
64   enum {
65     UNKNOWN_RULE = 0,
66     CHARSET_RULE,
67     IMPORT_RULE,
68     NAMESPACE_RULE,
69     STYLE_RULE,
70     MEDIA_RULE,
71     FONT_FACE_RULE,
72     PAGE_RULE,
73     KEYFRAME_RULE,
74     KEYFRAMES_RULE,
75     DOCUMENT_RULE,
76     SUPPORTS_RULE,
77     FONT_FEATURE_VALUES_RULE,
78     COUNTER_STYLE_RULE
79   };
80 
81   virtual int32_t GetType() const = 0;
82 
GetStyleSheet()83   CSSStyleSheet* GetStyleSheet() const { return mSheet; }
84 
85   // Return the document the rule lives in, if any
GetDocument()86   nsIDocument* GetDocument() const
87   {
88     CSSStyleSheet* sheet = GetStyleSheet();
89     return sheet ? sheet->GetDocument() : nullptr;
90   }
91 
92   virtual void SetStyleSheet(CSSStyleSheet* aSheet);
93 
SetParentRule(GroupRule * aRule)94   void SetParentRule(GroupRule* aRule) {
95     // We don't reference count this up reference. The group rule
96     // will tell us when it's going away or when we're detached from
97     // it.
98     mParentRule = aRule;
99   }
100 
GetLineNumber()101   uint32_t GetLineNumber() const { return mLineNumber; }
GetColumnNumber()102   uint32_t GetColumnNumber() const { return mColumnNumber; }
103 
104   /**
105    * Clones |this|. Never returns nullptr.
106    */
107   virtual already_AddRefed<Rule> Clone() const = 0;
108 
109   // Note that this returns null for inline style rules since they aren't
110   // supposed to have a DOM rule representation (and our code wouldn't work).
111   virtual nsIDOMCSSRule* GetDOMRule() = 0;
112 
113   // Like GetDOMRule(), but won't create one if we don't have one yet
114   virtual nsIDOMCSSRule* GetExistingDOMRule() = 0;
115 
116   // to implement methods on nsIDOMCSSRule
117   nsresult GetParentRule(nsIDOMCSSRule** aParentRule);
118   nsresult GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet);
119   Rule* GetCSSRule();
120 
121   // This is pure virtual because all of Rule's data members are non-owning and
122   // thus measured elsewhere.
123   virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
124     const MOZ_MUST_OVERRIDE = 0;
125 
126 protected:
127   // This is sometimes null (e.g., for style attributes).
128   CSSStyleSheet*    mSheet;
129   // When the parent GroupRule is destroyed, it will call SetParentRule(nullptr)
130   // on this object. (Through SetParentRuleReference);
131   GroupRule* MOZ_NON_OWNING_REF mParentRule;
132 
133   // Keep the same type so that MSVC packs them.
134   uint32_t          mLineNumber;
135   uint32_t          mColumnNumber;
136 };
137 
138 } // namespace css
139 } // namespace mozilla
140 
141 #endif /* mozilla_css_Rule_h___ */
142