Bug 1833466 - Implement CSSMarginRule and the corresponding DOM API. r=webidl,firefox...
[gecko.git] / layout / style / CSSMarginRule.cpp
blob64a706666cdcd5549de2dd19760e0475008b07b5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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/CSSMarginRule.h"
8 #include "mozilla/dom/CSSMarginRuleBinding.h"
10 #include "mozilla/DeclarationBlock.h"
11 #include "mozilla/ServoBindings.h"
13 namespace mozilla::dom {
15 // -- CSSMarginRuleDeclaration ---------------------------------------
17 CSSMarginRuleDeclaration::CSSMarginRuleDeclaration(
18 already_AddRefed<StyleLockedDeclarationBlock> aDecls)
19 : mDecls(new DeclarationBlock(std::move(aDecls))) {
20 mDecls->SetOwningRule(Rule());
23 CSSMarginRuleDeclaration::~CSSMarginRuleDeclaration() {
24 mDecls->SetOwningRule(nullptr);
27 // QueryInterface implementation for CSSMarginRuleDeclaration
28 NS_INTERFACE_MAP_BEGIN(CSSMarginRuleDeclaration)
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(CSSMarginRuleDeclaration, Rule())
39 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSMarginRuleDeclaration, Rule())
41 /* nsDOMCSSDeclaration implementation */
42 css::Rule* CSSMarginRuleDeclaration::GetParentRule() { return Rule(); }
44 nsINode* CSSMarginRuleDeclaration::GetAssociatedNode() const {
45 return Rule()->GetAssociatedDocumentOrShadowRoot();
48 nsISupports* CSSMarginRuleDeclaration::GetParentObject() const {
49 return Rule()->GetParentObject();
52 DeclarationBlock* CSSMarginRuleDeclaration::GetOrCreateCSSDeclaration(
53 Operation aOperation, DeclarationBlock** aCreated) {
54 if (aOperation != Operation::Read) {
55 if (StyleSheet* sheet = Rule()->GetStyleSheet()) {
56 sheet->WillDirty();
59 return mDecls;
62 void CSSMarginRuleDeclaration::SetRawAfterClone(
63 RefPtr<StyleLockedDeclarationBlock> aDeclarationBlock) {
64 mDecls->SetOwningRule(nullptr);
65 mDecls = new DeclarationBlock(aDeclarationBlock.forget());
66 mDecls->SetOwningRule(Rule());
69 nsresult CSSMarginRuleDeclaration::SetCSSDeclaration(
70 DeclarationBlock* aDecl, MutationClosureData* aClosureData) {
71 MOZ_ASSERT(aDecl, "must be non-null");
72 CSSMarginRule* rule = Rule();
74 if (aDecl != mDecls) {
75 mDecls->SetOwningRule(nullptr);
76 RefPtr<DeclarationBlock> decls = aDecl;
77 // TODO alaskanemily: bug 1890418 for implementing this and margin-rule
78 // style properties in general.
79 // Servo_MarginRule_SetStyle(rule->Raw(), decls->Raw());
80 mDecls = std::move(decls);
81 mDecls->SetOwningRule(rule);
84 return NS_OK;
87 nsDOMCSSDeclaration::ParsingEnvironment
88 CSSMarginRuleDeclaration::GetParsingEnvironment(
89 nsIPrincipal* aSubjectPrincipal) const {
90 return GetParsingEnvironmentForRule(Rule(), StyleCssRuleType::Margin);
93 // -- CSSMarginRule --------------------------------------------------
95 CSSMarginRule::CSSMarginRule(RefPtr<StyleMarginRule> aRawRule,
96 StyleSheet* aSheet, css::Rule* aParentRule,
97 uint32_t aLine, uint32_t aColumn)
98 : css::Rule(aSheet, aParentRule, aLine, aColumn),
99 mRawRule(std::move(aRawRule)),
100 mDecls(Servo_MarginRule_GetStyle(mRawRule).Consume()) {}
102 NS_IMPL_ADDREF_INHERITED(CSSMarginRule, css::Rule)
103 NS_IMPL_RELEASE_INHERITED(CSSMarginRule, css::Rule)
105 // QueryInterface implementation for MarginRule
106 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMarginRule)
107 NS_INTERFACE_MAP_END_INHERITING(css::Rule)
109 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSMarginRule)
111 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSMarginRule, css::Rule)
112 // Keep this in sync with IsCCLeaf.
114 // Trace the wrapper for our declaration. This just expands out
115 // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
116 // directly because the wrapper is on the declaration, not on us.
117 tmp->mDecls.TraceWrapper(aCallbacks, aClosure);
118 NS_IMPL_CYCLE_COLLECTION_TRACE_END
120 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSMarginRule)
121 // Keep this in sync with IsCCLeaf.
123 // Unlink the wrapper for our declaration.
125 // Note that this has to happen before unlinking css::Rule.
126 tmp->UnlinkDeclarationWrapper(tmp->mDecls);
127 tmp->mDecls.mDecls->SetOwningRule(nullptr);
128 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)
130 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSMarginRule, css::Rule)
131 // Keep this in sync with IsCCLeaf.
132 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
134 bool CSSMarginRule::IsCCLeaf() const {
135 if (!Rule::IsCCLeaf()) {
136 return false;
139 return !mDecls.PreservingWrapper();
142 void CSSMarginRule::SetRawAfterClone(RefPtr<StyleMarginRule> aRaw) {
143 mRawRule = std::move(aRaw);
144 mDecls.SetRawAfterClone(Servo_MarginRule_GetStyle(mRawRule.get()).Consume());
147 // WebIDL interfaces
148 StyleCssRuleType CSSMarginRule::Type() const {
149 return StyleCssRuleType::Margin;
152 // CSSRule implementation
154 void CSSMarginRule::GetCssText(nsACString& aCssText) const {
155 Servo_MarginRule_GetCssText(mRawRule, &aCssText);
158 void CSSMarginRule::GetName(nsACString& aRuleName) const {
159 Servo_MarginRule_GetName(mRawRule, &aRuleName);
162 size_t CSSMarginRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
163 // TODO Implement this!
164 return aMallocSizeOf(this);
167 #ifdef DEBUG
168 void CSSMarginRule::List(FILE* out, int32_t aIndent) const {
169 nsAutoCString str;
170 for (int32_t i = 0; i < aIndent; i++) {
171 str.AppendLiteral(" ");
173 Servo_MarginRule_Debug(mRawRule, &str);
174 fprintf_stderr(out, "%s\n", str.get());
176 #endif
178 JSObject* CSSMarginRule::WrapObject(JSContext* aCx,
179 JS::Handle<JSObject*> aGivenProto) {
180 return CSSMarginRule_Binding::Wrap(aCx, this, aGivenProto);
183 } // namespace mozilla::dom