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 #ifndef mozilla_CSSStyleRule_h
8 #define mozilla_CSSStyleRule_h
9 
10 #include "mozilla/BindingStyleRule.h"
11 #include "mozilla/ServoBindingTypes.h"
12 #include "mozilla/WeakPtr.h"
13 
14 #include "nsDOMCSSDeclaration.h"
15 
16 namespace mozilla {
17 
18 class DeclarationBlock;
19 
20 namespace dom {
21 class DocGroup;
22 class CSSStyleRule;
23 
24 class CSSStyleRuleDeclaration final : public nsDOMCSSDeclaration {
25  public:
26   NS_DECL_ISUPPORTS_INHERITED
27 
28   css::Rule* GetParentRule() final;
29   nsINode* GetAssociatedNode() const final;
30   nsISupports* GetParentObject() const final;
31 
32  protected:
33   mozilla::DeclarationBlock* GetOrCreateCSSDeclaration(
34       Operation aOperation, mozilla::DeclarationBlock** aCreated) final;
35   nsresult SetCSSDeclaration(DeclarationBlock* aDecl,
36                              MutationClosureData* aClosureData) final;
37   Document* DocToUpdate() final;
38   ParsingEnvironment GetParsingEnvironment(
39       nsIPrincipal* aSubjectPrincipal) const final;
40 
41  private:
42   // For accessing the constructor.
43   friend class CSSStyleRule;
44 
45   explicit CSSStyleRuleDeclaration(
46       already_AddRefed<RawServoDeclarationBlock> aDecls);
47   ~CSSStyleRuleDeclaration();
48 
49   inline CSSStyleRule* Rule();
50   inline const CSSStyleRule* Rule() const;
51 
52   RefPtr<DeclarationBlock> mDecls;
53 };
54 
55 class CSSStyleRule final : public BindingStyleRule, public SupportsWeakPtr {
56  public:
57   CSSStyleRule(already_AddRefed<RawServoStyleRule> aRawRule, StyleSheet* aSheet,
58                css::Rule* aParentRule, uint32_t aLine, uint32_t aColumn);
59 
60   NS_DECL_ISUPPORTS_INHERITED
61   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(CSSStyleRule,
62                                                          css::Rule)
63   bool IsCCLeaf() const final MOZ_MUST_OVERRIDE;
64 
65   uint32_t GetSelectorCount() override;
66   nsresult GetSelectorText(uint32_t aSelectorIndex, nsACString& aText) override;
67   nsresult GetSpecificity(uint32_t aSelectorIndex,
68                           uint64_t* aSpecificity) override;
69   nsresult SelectorMatchesElement(dom::Element* aElement,
70                                   uint32_t aSelectorIndex,
71                                   const nsAString& aPseudo,
72                                   bool aRelevantLinkVisited,
73                                   bool* aMatches) override;
74   NotNull<DeclarationBlock*> GetDeclarationBlock() const override;
75 
76   // WebIDL interface
Type()77   uint16_t Type() const final { return dom::CSSRule_Binding::STYLE_RULE; }
78   void GetCssText(nsACString& aCssText) const final;
79   void GetSelectorText(nsACString& aSelectorText) final;
80   void SetSelectorText(const nsACString& aSelectorText) final;
81   nsICSSDeclaration* Style() final;
82 
Raw()83   RawServoStyleRule* Raw() const { return mRawRule; }
84 
85   // Methods of mozilla::css::Rule
86   size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const final;
87 #ifdef DEBUG
88   void List(FILE* out = stdout, int32_t aIndent = 0) const final;
89 #endif
90 
91  private:
92   ~CSSStyleRule() = default;
93 
94   // For computing the offset of mDecls.
95   friend class CSSStyleRuleDeclaration;
96 
97   RefPtr<RawServoStyleRule> mRawRule;
98   CSSStyleRuleDeclaration mDecls;
99 };
100 
Rule()101 CSSStyleRule* CSSStyleRuleDeclaration::Rule() {
102   return reinterpret_cast<CSSStyleRule*>(reinterpret_cast<uint8_t*>(this) -
103                                          offsetof(CSSStyleRule, mDecls));
104 }
105 
Rule()106 const CSSStyleRule* CSSStyleRuleDeclaration::Rule() const {
107   return reinterpret_cast<const CSSStyleRule*>(
108       reinterpret_cast<const uint8_t*>(this) - offsetof(CSSStyleRule, mDecls));
109 }
110 
111 }  // namespace dom
112 }  // namespace mozilla
113 
114 #endif  // mozilla_CSSStyleRule_h
115