Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / style / CSSPageRule.cpp
blob04d064b362b527d0e256cfc98c0067afa8455506
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/CSSPageRule.h"
8 #include "mozilla/dom/CSSPageRuleBinding.h"
10 #include "mozilla/DeclarationBlock.h"
11 #include "mozilla/ServoBindings.h"
13 namespace mozilla::dom {
15 // -- CSSPageRuleDeclaration ---------------------------------------
17 CSSPageRuleDeclaration::CSSPageRuleDeclaration(
18 already_AddRefed<StyleLockedDeclarationBlock> aDecls)
19 : mDecls(new DeclarationBlock(std::move(aDecls))) {
20 mDecls->SetOwningRule(Rule());
23 CSSPageRuleDeclaration::~CSSPageRuleDeclaration() {
24 mDecls->SetOwningRule(nullptr);
27 // QueryInterface implementation for CSSPageRuleDeclaration
28 NS_INTERFACE_MAP_BEGIN(CSSPageRuleDeclaration)
29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
30 // We forward the cycle collection interfaces to Rule(), which is
31 // never null (in fact, we're part of that object!)
32 if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
33 aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
34 return Rule()->QueryInterface(aIID, aInstancePtr);
36 NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
38 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
39 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
41 /* nsDOMCSSDeclaration implementation */
43 css::Rule* CSSPageRuleDeclaration::GetParentRule() { return Rule(); }
45 nsINode* CSSPageRuleDeclaration::GetAssociatedNode() const {
46 return Rule()->GetAssociatedDocumentOrShadowRoot();
49 nsISupports* CSSPageRuleDeclaration::GetParentObject() const {
50 return Rule()->GetParentObject();
53 DeclarationBlock* CSSPageRuleDeclaration::GetOrCreateCSSDeclaration(
54 Operation aOperation, DeclarationBlock** aCreated) {
55 if (aOperation != Operation::Read) {
56 if (StyleSheet* sheet = Rule()->GetStyleSheet()) {
57 sheet->WillDirty();
60 return mDecls;
63 void CSSPageRuleDeclaration::SetRawAfterClone(
64 RefPtr<StyleLockedDeclarationBlock> aDeclarationBlock) {
65 mDecls->SetOwningRule(nullptr);
66 mDecls = new DeclarationBlock(aDeclarationBlock.forget());
67 mDecls->SetOwningRule(Rule());
70 nsresult CSSPageRuleDeclaration::SetCSSDeclaration(
71 DeclarationBlock* aDecl, MutationClosureData* aClosureData) {
72 MOZ_ASSERT(aDecl, "must be non-null");
73 CSSPageRule* rule = Rule();
75 if (aDecl != mDecls) {
76 mDecls->SetOwningRule(nullptr);
77 RefPtr<DeclarationBlock> decls = aDecl;
78 Servo_PageRule_SetStyle(rule->Raw(), decls->Raw());
79 mDecls = std::move(decls);
80 mDecls->SetOwningRule(rule);
83 return NS_OK;
86 nsDOMCSSDeclaration::ParsingEnvironment
87 CSSPageRuleDeclaration::GetParsingEnvironment(
88 nsIPrincipal* aSubjectPrincipal) const {
89 return GetParsingEnvironmentForRule(Rule(), StyleCssRuleType::Page);
92 // -- CSSPageRule --------------------------------------------------
94 CSSPageRule::CSSPageRule(RefPtr<StyleLockedPageRule> aRawRule,
95 StyleSheet* aSheet, css::Rule* aParentRule,
96 uint32_t aLine, uint32_t aColumn)
97 : css::Rule(aSheet, aParentRule, aLine, aColumn),
98 mRawRule(std::move(aRawRule)),
99 mDecls(Servo_PageRule_GetStyle(mRawRule).Consume()) {}
101 NS_IMPL_ADDREF_INHERITED(CSSPageRule, css::Rule)
102 NS_IMPL_RELEASE_INHERITED(CSSPageRule, css::Rule)
104 // QueryInterface implementation for PageRule
105 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSPageRule)
106 NS_INTERFACE_MAP_END_INHERITING(css::Rule)
108 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSPageRule)
110 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSPageRule, css::Rule)
111 // Keep this in sync with IsCCLeaf.
113 // Trace the wrapper for our declaration. This just expands out
114 // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
115 // directly because the wrapper is on the declaration, not on us.
116 tmp->mDecls.TraceWrapper(aCallbacks, aClosure);
117 NS_IMPL_CYCLE_COLLECTION_TRACE_END
119 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSPageRule)
120 // Keep this in sync with IsCCLeaf.
122 // Unlink the wrapper for our declaration.
124 // Note that this has to happen before unlinking css::Rule.
125 tmp->UnlinkDeclarationWrapper(tmp->mDecls);
126 tmp->mDecls.mDecls->SetOwningRule(nullptr);
127 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)
129 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSPageRule, css::Rule)
130 // Keep this in sync with IsCCLeaf.
131 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
133 bool CSSPageRule::IsCCLeaf() const {
134 if (!Rule::IsCCLeaf()) {
135 return false;
138 return !mDecls.PreservingWrapper();
141 void CSSPageRule::SetRawAfterClone(RefPtr<StyleLockedPageRule> aRaw) {
142 mRawRule = std::move(aRaw);
143 mDecls.SetRawAfterClone(Servo_PageRule_GetStyle(mRawRule.get()).Consume());
146 StyleCssRuleType CSSPageRule::Type() const { return StyleCssRuleType::Page; }
148 size_t CSSPageRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
149 // TODO Implement this!
150 return aMallocSizeOf(this);
153 #ifdef DEBUG
154 void CSSPageRule::List(FILE* out, int32_t aIndent) const {
155 nsAutoCString str;
156 for (int32_t i = 0; i < aIndent; i++) {
157 str.AppendLiteral(" ");
159 Servo_PageRule_Debug(mRawRule, &str);
160 fprintf_stderr(out, "%s\n", str.get());
162 #endif
164 /* CSSRule implementation */
166 void CSSPageRule::GetCssText(nsACString& aCssText) const {
167 Servo_PageRule_GetCssText(mRawRule, &aCssText);
170 /* CSSPageRule implementation */
172 void CSSPageRule::GetSelectorText(nsACString& aSelectorText) const {
173 Servo_PageRule_GetSelectorText(mRawRule.get(), &aSelectorText);
176 void CSSPageRule::SetSelectorText(const nsACString& aSelectorText) {
177 if (IsReadOnly()) {
178 return;
181 if (StyleSheet* const sheet = GetStyleSheet()) {
182 sheet->WillDirty();
183 const StyleStylesheetContents* const contents = sheet->RawContents();
184 if (Servo_PageRule_SetSelectorText(contents, mRawRule.get(),
185 &aSelectorText)) {
186 sheet->RuleChanged(this, StyleRuleChangeKind::Generic);
191 nsICSSDeclaration* CSSPageRule::Style() { return &mDecls; }
193 JSObject* CSSPageRule::WrapObject(JSContext* aCx,
194 JS::Handle<JSObject*> aGivenProto) {
195 return CSSPageRule_Binding::Wrap(aCx, this, aGivenProto);
198 } // namespace mozilla::dom