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"
16 // -- CSSPageRuleDeclaration ---------------------------------------
18 CSSPageRuleDeclaration::CSSPageRuleDeclaration(
19 already_AddRefed
<RawServoDeclarationBlock
> aDecls
)
20 : mDecls(new DeclarationBlock(std::move(aDecls
))) {}
22 CSSPageRuleDeclaration::~CSSPageRuleDeclaration() {
23 mDecls
->SetOwningRule(nullptr);
26 // QueryInterface implementation for CSSPageRuleDeclaration
27 NS_INTERFACE_MAP_BEGIN(CSSPageRuleDeclaration
)
28 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
29 // We forward the cycle collection interfaces to Rule(), which is
30 // never null (in fact, we're part of that object!)
31 if (aIID
.Equals(NS_GET_IID(nsCycleCollectionISupports
)) ||
32 aIID
.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant
))) {
33 return Rule()->QueryInterface(aIID
, aInstancePtr
);
35 NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration
)
37 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSPageRuleDeclaration
, Rule())
38 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSPageRuleDeclaration
, Rule())
40 /* nsDOMCSSDeclaration implementation */
42 css::Rule
* CSSPageRuleDeclaration::GetParentRule() { return Rule(); }
44 nsINode
* CSSPageRuleDeclaration::GetParentObject() {
45 return Rule()->GetParentObject();
48 DeclarationBlock
* CSSPageRuleDeclaration::GetOrCreateCSSDeclaration(
49 Operation aOperation
, DeclarationBlock
** aCreated
) {
53 nsresult
CSSPageRuleDeclaration::SetCSSDeclaration(
54 DeclarationBlock
* aDecl
, MutationClosureData
* aClosureData
) {
55 MOZ_ASSERT(aDecl
, "must be non-null");
56 CSSPageRule
* rule
= Rule();
58 if (rule
->IsReadOnly()) {
62 if (aDecl
!= mDecls
) {
63 mDecls
->SetOwningRule(nullptr);
64 RefPtr
<DeclarationBlock
> decls
= aDecl
;
65 Servo_PageRule_SetStyle(rule
->Raw(), decls
->Raw());
66 mDecls
= decls
.forget();
67 mDecls
->SetOwningRule(rule
);
73 nsDOMCSSDeclaration::ParsingEnvironment
74 CSSPageRuleDeclaration::GetParsingEnvironment(
75 nsIPrincipal
* aSubjectPrincipal
) const {
76 return GetParsingEnvironmentForRule(Rule());
79 // -- CSSPageRule --------------------------------------------------
81 CSSPageRule::CSSPageRule(RefPtr
<RawServoPageRule
> aRawRule
, StyleSheet
* aSheet
,
82 css::Rule
* aParentRule
, uint32_t aLine
,
84 : css::Rule(aSheet
, aParentRule
, aLine
, aColumn
),
85 mRawRule(std::move(aRawRule
)),
86 mDecls(Servo_PageRule_GetStyle(mRawRule
).Consume()) {}
88 NS_IMPL_ADDREF_INHERITED(CSSPageRule
, css::Rule
)
89 NS_IMPL_RELEASE_INHERITED(CSSPageRule
, css::Rule
)
91 // QueryInterface implementation for PageRule
92 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSPageRule
)
93 NS_INTERFACE_MAP_END_INHERITING(css::Rule
)
95 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSPageRule
)
97 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSPageRule
, css::Rule
)
98 // Keep this in sync with IsCCLeaf.
100 // Trace the wrapper for our declaration. This just expands out
101 // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
102 // directly because the wrapper is on the declaration, not on us.
103 tmp
->mDecls
.TraceWrapper(aCallbacks
, aClosure
);
104 NS_IMPL_CYCLE_COLLECTION_TRACE_END
106 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSPageRule
)
107 // Keep this in sync with IsCCLeaf.
109 // Unlink the wrapper for our declaration.
111 // Note that this has to happen before unlinking css::Rule.
112 tmp
->UnlinkDeclarationWrapper(tmp
->mDecls
);
113 tmp
->mDecls
.mDecls
->SetOwningRule(nullptr);
114 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule
)
116 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSPageRule
, css::Rule
)
117 // Keep this in sync with IsCCLeaf.
118 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
120 bool CSSPageRule::IsCCLeaf() const {
121 if (!Rule::IsCCLeaf()) {
125 return !mDecls
.PreservingWrapper();
128 size_t CSSPageRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
129 // TODO Implement this!
130 return aMallocSizeOf(this);
134 void CSSPageRule::List(FILE* out
, int32_t aIndent
) const {
136 for (int32_t i
= 0; i
< aIndent
; i
++) {
137 str
.AppendLiteral(" ");
139 Servo_PageRule_Debug(mRawRule
, &str
);
140 fprintf_stderr(out
, "%s\n", str
.get());
144 /* CSSRule implementation */
146 void CSSPageRule::GetCssText(nsAString
& aCssText
) const {
147 Servo_PageRule_GetCssText(mRawRule
, &aCssText
);
150 /* CSSPageRule implementation */
152 nsICSSDeclaration
* CSSPageRule::Style() { return &mDecls
; }
154 JSObject
* CSSPageRule::WrapObject(JSContext
* aCx
,
155 JS::Handle
<JSObject
*> aGivenProto
) {
156 return CSSPageRule_Binding::Wrap(aCx
, this, aGivenProto
);
160 } // namespace mozilla