Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / layout / style / CSSStyleRule.cpp
blobd389afca469eaafbd72291b5aa89cf06ecf9fbc9
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 #include "mozilla/dom/CSSStyleRule.h"
9 #include "mozilla/CSSEnabledState.h"
10 #include "mozilla/DeclarationBlock.h"
11 #include "mozilla/PseudoStyleType.h"
12 #include "mozilla/ServoBindings.h"
13 #include "mozilla/dom/CSSStyleRuleBinding.h"
14 #include "nsCSSPseudoElements.h"
16 #include "mozAutoDocUpdate.h"
17 #include "nsISupports.h"
19 namespace mozilla::dom {
21 // -- CSSStyleRuleDeclaration ---------------------------------------
23 CSSStyleRuleDeclaration::CSSStyleRuleDeclaration(
24 already_AddRefed<RawServoDeclarationBlock> aDecls)
25 : mDecls(new DeclarationBlock(std::move(aDecls))) {
26 mDecls->SetOwningRule(Rule());
29 CSSStyleRuleDeclaration::~CSSStyleRuleDeclaration() {
30 mDecls->SetOwningRule(nullptr);
33 // QueryInterface implementation for CSSStyleRuleDeclaration
34 NS_INTERFACE_MAP_BEGIN(CSSStyleRuleDeclaration)
35 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
36 // We forward the cycle collection interfaces to Rule(), which is
37 // never null (in fact, we're part of that object!)
38 if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
39 aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
40 return Rule()->QueryInterface(aIID, aInstancePtr);
42 NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
44 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSStyleRuleDeclaration, Rule())
45 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSStyleRuleDeclaration, Rule())
47 /* nsDOMCSSDeclaration implementation */
49 css::Rule* CSSStyleRuleDeclaration::GetParentRule() { return Rule(); }
51 nsINode* CSSStyleRuleDeclaration::GetAssociatedNode() const {
52 return Rule()->GetAssociatedDocumentOrShadowRoot();
55 nsISupports* CSSStyleRuleDeclaration::GetParentObject() const {
56 return Rule()->GetParentObject();
59 DeclarationBlock* CSSStyleRuleDeclaration::GetOrCreateCSSDeclaration(
60 Operation aOperation, DeclarationBlock** aCreated) {
61 if (aOperation != Operation::Read) {
62 if (StyleSheet* sheet = Rule()->GetStyleSheet()) {
63 sheet->WillDirty();
66 return mDecls;
69 void CSSStyleRule::SetRawAfterClone(RefPtr<RawServoStyleRule> aRaw) {
70 mRawRule = std::move(aRaw);
71 mDecls.SetRawAfterClone(Servo_StyleRule_GetStyle(mRawRule).Consume());
74 void CSSStyleRuleDeclaration::SetRawAfterClone(
75 RefPtr<RawServoDeclarationBlock> aRaw) {
76 RefPtr<DeclarationBlock> block = new DeclarationBlock(aRaw.forget());
77 mDecls->SetOwningRule(nullptr);
78 mDecls = std::move(block);
79 mDecls->SetOwningRule(Rule());
82 nsresult CSSStyleRuleDeclaration::SetCSSDeclaration(
83 DeclarationBlock* aDecl, MutationClosureData* aClosureData) {
84 CSSStyleRule* rule = Rule();
86 if (StyleSheet* sheet = rule->GetStyleSheet()) {
87 if (aDecl != mDecls) {
88 mDecls->SetOwningRule(nullptr);
89 RefPtr<DeclarationBlock> decls = aDecl;
90 Servo_StyleRule_SetStyle(rule->Raw(), decls->Raw());
91 mDecls = std::move(decls);
92 mDecls->SetOwningRule(rule);
94 sheet->RuleChanged(rule, StyleRuleChangeKind::StyleRuleDeclarations);
96 return NS_OK;
99 Document* CSSStyleRuleDeclaration::DocToUpdate() { return nullptr; }
101 nsDOMCSSDeclaration::ParsingEnvironment
102 CSSStyleRuleDeclaration::GetParsingEnvironment(
103 nsIPrincipal* aSubjectPrincipal) const {
104 return GetParsingEnvironmentForRule(Rule(), StyleCssRuleType::Style);
107 // -- CSSStyleRule --------------------------------------------------
109 CSSStyleRule::CSSStyleRule(already_AddRefed<RawServoStyleRule> aRawRule,
110 StyleSheet* aSheet, css::Rule* aParentRule,
111 uint32_t aLine, uint32_t aColumn)
112 : BindingStyleRule(aSheet, aParentRule, aLine, aColumn),
113 mRawRule(aRawRule),
114 mDecls(Servo_StyleRule_GetStyle(mRawRule).Consume()) {}
116 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(CSSStyleRule, css::Rule)
118 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSStyleRule)
120 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSStyleRule, css::Rule)
121 // Keep this in sync with IsCCLeaf.
123 // Trace the wrapper for our declaration. This just expands out
124 // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
125 // directly because the wrapper is on the declaration, not on us.
126 tmp->mDecls.TraceWrapper(aCallbacks, aClosure);
127 NS_IMPL_CYCLE_COLLECTION_TRACE_END
129 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSStyleRule)
130 // Keep this in sync with IsCCLeaf.
132 // Unlink the wrapper for our declaration.
134 // Note that this has to happen before unlinking css::Rule.
135 tmp->UnlinkDeclarationWrapper(tmp->mDecls);
136 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
137 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)
139 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSStyleRule, css::Rule)
140 // Keep this in sync with IsCCLeaf.
141 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
143 bool CSSStyleRule::IsCCLeaf() const {
144 if (!Rule::IsCCLeaf()) {
145 return false;
148 return !mDecls.PreservingWrapper();
151 size_t CSSStyleRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
152 size_t n = aMallocSizeOf(this);
154 // Measurement of the following members may be added later if DMD finds it
155 // is worthwhile:
156 // - mRawRule
157 // - mDecls
159 return n;
162 #ifdef DEBUG
163 void CSSStyleRule::List(FILE* out, int32_t aIndent) const {
164 nsAutoCString str;
165 for (int32_t i = 0; i < aIndent; i++) {
166 str.AppendLiteral(" ");
168 Servo_StyleRule_Debug(mRawRule, &str);
169 fprintf_stderr(out, "%s\n", str.get());
171 #endif
173 /* CSSRule implementation */
175 StyleCssRuleType CSSStyleRule::Type() const { return StyleCssRuleType::Style; }
177 void CSSStyleRule::GetCssText(nsACString& aCssText) const {
178 Servo_StyleRule_GetCssText(mRawRule, &aCssText);
181 nsICSSDeclaration* CSSStyleRule::Style() { return &mDecls; }
183 /* CSSStyleRule implementation */
185 void CSSStyleRule::GetSelectorText(nsACString& aSelectorText) {
186 Servo_StyleRule_GetSelectorText(mRawRule, &aSelectorText);
189 void CSSStyleRule::SetSelectorText(const nsACString& aSelectorText) {
190 if (IsReadOnly()) {
191 return;
194 if (StyleSheet* sheet = GetStyleSheet()) {
195 sheet->WillDirty();
197 // TODO(emilio): May actually be more efficient to handle this as rule
198 // removal + addition, from the point of view of invalidation...
199 const RawServoStyleSheetContents* contents = sheet->RawContents();
200 if (Servo_StyleRule_SetSelectorText(contents, mRawRule, &aSelectorText)) {
201 sheet->RuleChanged(this, StyleRuleChangeKind::Generic);
206 uint32_t CSSStyleRule::GetSelectorCount() {
207 uint32_t aCount;
208 Servo_StyleRule_GetSelectorCount(mRawRule, &aCount);
209 return aCount;
212 nsresult CSSStyleRule::GetSelectorText(uint32_t aSelectorIndex,
213 nsACString& aText) {
214 Servo_StyleRule_GetSelectorTextAtIndex(mRawRule, aSelectorIndex, &aText);
215 return NS_OK;
218 nsresult CSSStyleRule::GetSpecificity(uint32_t aSelectorIndex,
219 uint64_t* aSpecificity) {
220 Servo_StyleRule_GetSpecificityAtIndex(mRawRule, aSelectorIndex, aSpecificity);
221 return NS_OK;
224 nsresult CSSStyleRule::SelectorMatchesElement(Element* aElement,
225 uint32_t aSelectorIndex,
226 const nsAString& aPseudo,
227 bool aRelevantLinkVisited,
228 bool* aMatches) {
229 Maybe<PseudoStyleType> pseudoType = nsCSSPseudoElements::GetPseudoType(
230 aPseudo, CSSEnabledState::IgnoreEnabledState);
231 if (!pseudoType) {
232 *aMatches = false;
233 return NS_OK;
236 *aMatches = Servo_StyleRule_SelectorMatchesElement(
237 mRawRule, aElement, aSelectorIndex, *pseudoType, aRelevantLinkVisited);
238 return NS_OK;
241 NotNull<DeclarationBlock*> CSSStyleRule::GetDeclarationBlock() const {
242 return WrapNotNull(mDecls.mDecls);
245 } // namespace mozilla::dom