|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* DOM object for element.style */ |
|
7 |
|
8 #include "nsDOMCSSAttrDeclaration.h" |
|
9 |
|
10 #include "mozilla/css/Declaration.h" |
|
11 #include "mozilla/css/StyleRule.h" |
|
12 #include "mozilla/dom/Element.h" |
|
13 #include "nsIDocument.h" |
|
14 #include "nsIDOMMutationEvent.h" |
|
15 #include "nsIURI.h" |
|
16 #include "nsNodeUtils.h" |
|
17 #include "nsWrapperCacheInlines.h" |
|
18 #include "nsIFrame.h" |
|
19 #include "ActiveLayerTracker.h" |
|
20 |
|
21 using namespace mozilla; |
|
22 |
|
23 nsDOMCSSAttributeDeclaration::nsDOMCSSAttributeDeclaration(dom::Element* aElement, |
|
24 bool aIsSMILOverride) |
|
25 : mElement(aElement) |
|
26 , mIsSMILOverride(aIsSMILOverride) |
|
27 { |
|
28 MOZ_COUNT_CTOR(nsDOMCSSAttributeDeclaration); |
|
29 |
|
30 NS_ASSERTION(aElement, "Inline style for a NULL element?"); |
|
31 } |
|
32 |
|
33 nsDOMCSSAttributeDeclaration::~nsDOMCSSAttributeDeclaration() |
|
34 { |
|
35 MOZ_COUNT_DTOR(nsDOMCSSAttributeDeclaration); |
|
36 } |
|
37 |
|
38 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(nsDOMCSSAttributeDeclaration, mElement) |
|
39 |
|
40 // mElement holds a strong ref to us, so if it's going to be |
|
41 // skipped, the attribute declaration can't be part of a garbage |
|
42 // cycle. |
|
43 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsDOMCSSAttributeDeclaration) |
|
44 if (tmp->mElement && Element::CanSkip(tmp->mElement, true)) { |
|
45 if (tmp->PreservingWrapper()) { |
|
46 // This marks the wrapper black. |
|
47 tmp->GetWrapper(); |
|
48 } |
|
49 return true; |
|
50 } |
|
51 return tmp->IsBlack(); |
|
52 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END |
|
53 |
|
54 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsDOMCSSAttributeDeclaration) |
|
55 return tmp->IsBlack() || |
|
56 (tmp->mElement && Element::CanSkipInCC(tmp->mElement)); |
|
57 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END |
|
58 |
|
59 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsDOMCSSAttributeDeclaration) |
|
60 return tmp->IsBlack() || |
|
61 (tmp->mElement && Element::CanSkipThis(tmp->mElement)); |
|
62 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END |
|
63 |
|
64 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMCSSAttributeDeclaration) |
|
65 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
66 NS_IMPL_QUERY_TAIL_INHERITING(nsDOMCSSDeclaration) |
|
67 |
|
68 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMCSSAttributeDeclaration) |
|
69 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMCSSAttributeDeclaration) |
|
70 |
|
71 nsresult |
|
72 nsDOMCSSAttributeDeclaration::SetCSSDeclaration(css::Declaration* aDecl) |
|
73 { |
|
74 NS_ASSERTION(mElement, "Must have Element to set the declaration!"); |
|
75 css::StyleRule* oldRule = |
|
76 mIsSMILOverride ? mElement->GetSMILOverrideStyleRule() : |
|
77 mElement->GetInlineStyleRule(); |
|
78 NS_ASSERTION(oldRule, "Element must have rule"); |
|
79 |
|
80 nsRefPtr<css::StyleRule> newRule = |
|
81 oldRule->DeclarationChanged(aDecl, false); |
|
82 if (!newRule) { |
|
83 return NS_ERROR_OUT_OF_MEMORY; |
|
84 } |
|
85 |
|
86 return |
|
87 mIsSMILOverride ? mElement->SetSMILOverrideStyleRule(newRule, true) : |
|
88 mElement->SetInlineStyleRule(newRule, nullptr, true); |
|
89 } |
|
90 |
|
91 nsIDocument* |
|
92 nsDOMCSSAttributeDeclaration::DocToUpdate() |
|
93 { |
|
94 // XXXbz this is a bit of a hack, especially doing it before the |
|
95 // BeginUpdate(), but this is a good chokepoint where we know we |
|
96 // plan to modify the CSSDeclaration, so need to notify |
|
97 // AttributeWillChange if this is inline style. |
|
98 if (!mIsSMILOverride) { |
|
99 nsNodeUtils::AttributeWillChange(mElement, kNameSpaceID_None, |
|
100 nsGkAtoms::style, |
|
101 nsIDOMMutationEvent::MODIFICATION); |
|
102 } |
|
103 |
|
104 // We need OwnerDoc() rather than GetCurrentDoc() because it might |
|
105 // be the BeginUpdate call that inserts mElement into the document. |
|
106 return mElement->OwnerDoc(); |
|
107 } |
|
108 |
|
109 css::Declaration* |
|
110 nsDOMCSSAttributeDeclaration::GetCSSDeclaration(bool aAllocate) |
|
111 { |
|
112 if (!mElement) |
|
113 return nullptr; |
|
114 |
|
115 css::StyleRule* cssRule; |
|
116 if (mIsSMILOverride) |
|
117 cssRule = mElement->GetSMILOverrideStyleRule(); |
|
118 else |
|
119 cssRule = mElement->GetInlineStyleRule(); |
|
120 |
|
121 if (cssRule) { |
|
122 return cssRule->GetDeclaration(); |
|
123 } |
|
124 if (!aAllocate) { |
|
125 return nullptr; |
|
126 } |
|
127 |
|
128 // cannot fail |
|
129 css::Declaration *decl = new css::Declaration(); |
|
130 decl->InitializeEmpty(); |
|
131 nsRefPtr<css::StyleRule> newRule = new css::StyleRule(nullptr, decl); |
|
132 |
|
133 // this *can* fail (inside SetAttrAndNotify, at least). |
|
134 nsresult rv; |
|
135 if (mIsSMILOverride) |
|
136 rv = mElement->SetSMILOverrideStyleRule(newRule, false); |
|
137 else |
|
138 rv = mElement->SetInlineStyleRule(newRule, nullptr, false); |
|
139 |
|
140 if (NS_FAILED(rv)) { |
|
141 return nullptr; // the decl will be destroyed along with the style rule |
|
142 } |
|
143 |
|
144 return decl; |
|
145 } |
|
146 |
|
147 void |
|
148 nsDOMCSSAttributeDeclaration::GetCSSParsingEnvironment(CSSParsingEnvironment& aCSSParseEnv) |
|
149 { |
|
150 NS_ASSERTION(mElement, "Something is severely broken -- there should be an Element here!"); |
|
151 |
|
152 nsIDocument* doc = mElement->OwnerDoc(); |
|
153 aCSSParseEnv.mSheetURI = doc->GetDocumentURI(); |
|
154 aCSSParseEnv.mBaseURI = mElement->GetBaseURI(); |
|
155 aCSSParseEnv.mPrincipal = mElement->NodePrincipal(); |
|
156 aCSSParseEnv.mCSSLoader = doc->CSSLoader(); |
|
157 } |
|
158 |
|
159 NS_IMETHODIMP |
|
160 nsDOMCSSAttributeDeclaration::GetParentRule(nsIDOMCSSRule **aParent) |
|
161 { |
|
162 NS_ENSURE_ARG_POINTER(aParent); |
|
163 |
|
164 *aParent = nullptr; |
|
165 return NS_OK; |
|
166 } |
|
167 |
|
168 /* virtual */ nsINode* |
|
169 nsDOMCSSAttributeDeclaration::GetParentObject() |
|
170 { |
|
171 return mElement; |
|
172 } |
|
173 |
|
174 NS_IMETHODIMP |
|
175 nsDOMCSSAttributeDeclaration::SetPropertyValue(const nsCSSProperty aPropID, |
|
176 const nsAString& aValue) |
|
177 { |
|
178 // Scripted modifications to style.opacity or style.transform |
|
179 // could immediately force us into the animated state if heuristics suggest |
|
180 // this is scripted animation. |
|
181 if (aPropID == eCSSProperty_opacity || aPropID == eCSSProperty_transform || |
|
182 aPropID == eCSSProperty_left || aPropID == eCSSProperty_top || |
|
183 aPropID == eCSSProperty_right || aPropID == eCSSProperty_bottom || |
|
184 aPropID == eCSSProperty_margin_left || aPropID == eCSSProperty_margin_top || |
|
185 aPropID == eCSSProperty_margin_right || aPropID == eCSSProperty_margin_bottom) { |
|
186 nsIFrame* frame = mElement->GetPrimaryFrame(); |
|
187 if (frame) { |
|
188 ActiveLayerTracker::NotifyInlineStyleRuleModified(frame, aPropID); |
|
189 } |
|
190 } |
|
191 return nsDOMCSSDeclaration::SetPropertyValue(aPropID, aValue); |
|
192 } |