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/. */
6 
7 /* base class for DOM objects for element.style and cssStyleRule.style */
8 
9 #include "nsDOMCSSDeclaration.h"
10 
11 #include "mozilla/DeclarationBlock.h"
12 #include "mozilla/ProfilerLabels.h"
13 #include "mozilla/StyleSheetInlines.h"
14 #include "mozilla/css/Rule.h"
15 #include "mozilla/dom/CSS2PropertiesBinding.h"
16 #include "mozilla/dom/MutationEventBinding.h"
17 #include "nsCSSProps.h"
18 #include "nsCOMPtr.h"
19 #include "mozAutoDocUpdate.h"
20 #include "mozilla/dom/BindingUtils.h"
21 #include "nsQueryObject.h"
22 #include "mozilla/layers/ScrollLinkedEffectDetector.h"
23 
24 using namespace mozilla;
25 using namespace mozilla::dom;
26 
27 nsDOMCSSDeclaration::~nsDOMCSSDeclaration() = default;
28 
29 /* virtual */
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)30 JSObject* nsDOMCSSDeclaration::WrapObject(JSContext* aCx,
31                                           JS::Handle<JSObject*> aGivenProto) {
32   return CSS2Properties_Binding::Wrap(aCx, this, aGivenProto);
33 }
34 
NS_IMPL_QUERY_INTERFACE(nsDOMCSSDeclaration,nsICSSDeclaration)35 NS_IMPL_QUERY_INTERFACE(nsDOMCSSDeclaration, nsICSSDeclaration)
36 
37 nsresult nsDOMCSSDeclaration::GetPropertyValue(const nsCSSPropertyID aPropID,
38                                                nsACString& aValue) {
39   MOZ_ASSERT(aPropID != eCSSProperty_UNKNOWN,
40              "Should never pass eCSSProperty_UNKNOWN around");
41   MOZ_ASSERT(aValue.IsEmpty());
42 
43   if (DeclarationBlock* decl =
44           GetOrCreateCSSDeclaration(Operation::Read, nullptr)) {
45     decl->GetPropertyValueByID(aPropID, aValue);
46   }
47   return NS_OK;
48 }
49 
SetPropertyValue(const nsCSSPropertyID aPropID,const nsACString & aValue,nsIPrincipal * aSubjectPrincipal,ErrorResult & aRv)50 void nsDOMCSSDeclaration::SetPropertyValue(const nsCSSPropertyID aPropID,
51                                            const nsACString& aValue,
52                                            nsIPrincipal* aSubjectPrincipal,
53                                            ErrorResult& aRv) {
54   if (IsReadOnly()) {
55     return;
56   }
57 
58   switch (aPropID) {
59     case eCSSProperty_background_position:
60     case eCSSProperty_background_position_x:
61     case eCSSProperty_background_position_y:
62     case eCSSProperty_transform:
63     case eCSSProperty_translate:
64     case eCSSProperty_rotate:
65     case eCSSProperty_scale:
66     case eCSSProperty_top:
67     case eCSSProperty_left:
68     case eCSSProperty_bottom:
69     case eCSSProperty_right:
70     case eCSSProperty_margin:
71     case eCSSProperty_margin_top:
72     case eCSSProperty_margin_left:
73     case eCSSProperty_margin_bottom:
74     case eCSSProperty_margin_right:
75     case eCSSProperty_margin_inline_start:
76     case eCSSProperty_margin_inline_end:
77     case eCSSProperty_margin_block_start:
78     case eCSSProperty_margin_block_end:
79       mozilla::layers::ScrollLinkedEffectDetector::PositioningPropertyMutated();
80       break;
81     default:
82       break;
83   }
84 
85   if (aValue.IsEmpty()) {
86     // If the new value of the property is an empty string we remove the
87     // property.
88     return RemovePropertyInternal(aPropID, aRv);
89   }
90 
91   aRv = ParsePropertyValue(aPropID, aValue, false, aSubjectPrincipal);
92 }
93 
GetCssText(nsACString & aCssText)94 void nsDOMCSSDeclaration::GetCssText(nsACString& aCssText) {
95   MOZ_ASSERT(aCssText.IsEmpty());
96 
97   if (auto* decl = GetOrCreateCSSDeclaration(Operation::Read, nullptr)) {
98     decl->ToString(aCssText);
99   }
100 }
101 
SetCssText(const nsACString & aCssText,nsIPrincipal * aSubjectPrincipal,ErrorResult & aRv)102 void nsDOMCSSDeclaration::SetCssText(const nsACString& aCssText,
103                                      nsIPrincipal* aSubjectPrincipal,
104                                      ErrorResult& aRv) {
105   if (IsReadOnly()) {
106     return;
107   }
108 
109   // We don't need to *do* anything with the old declaration, but we need
110   // to ensure that it exists, or else SetCSSDeclaration may crash.
111   RefPtr<DeclarationBlock> created;
112   DeclarationBlock* olddecl =
113       GetOrCreateCSSDeclaration(Operation::Modify, getter_AddRefs(created));
114   if (!olddecl) {
115     aRv.Throw(NS_ERROR_NOT_AVAILABLE);
116     return;
117   }
118 
119   // For nsDOMCSSAttributeDeclaration, SetCSSDeclaration will lead to
120   // Attribute setting code, which leads in turn to BeginUpdate.  We
121   // need to start the update now so that the old rule doesn't get used
122   // between when we mutate the declaration and when we set the new
123   // rule (see stack in bug 209575).
124   mozAutoDocUpdate autoUpdate(DocToUpdate(), true);
125   DeclarationBlockMutationClosure closure = {};
126   MutationClosureData closureData;
127   GetPropertyChangeClosure(&closure, &closureData);
128 
129   ParsingEnvironment servoEnv = GetParsingEnvironment(aSubjectPrincipal);
130   if (!servoEnv.mUrlExtraData) {
131     aRv.Throw(NS_ERROR_NOT_AVAILABLE);
132     return;
133   }
134 
135   // Need to special case closure calling here, since parsing css text
136   // doesn't modify any existing declaration and that is why the callback isn't
137   // called implicitly.
138   if (closureData.mClosure) {
139     closureData.mClosure(&closureData);
140   }
141 
142   RefPtr<DeclarationBlock> newdecl = DeclarationBlock::FromCssText(
143       aCssText, servoEnv.mUrlExtraData, servoEnv.mCompatMode, servoEnv.mLoader,
144       servoEnv.mRuleType);
145 
146   aRv = SetCSSDeclaration(newdecl, &closureData);
147 }
148 
Length()149 uint32_t nsDOMCSSDeclaration::Length() {
150   DeclarationBlock* decl = GetOrCreateCSSDeclaration(Operation::Read, nullptr);
151 
152   if (decl) {
153     return decl->Count();
154   }
155 
156   return 0;
157 }
158 
IndexedGetter(uint32_t aIndex,bool & aFound,nsACString & aPropName)159 void nsDOMCSSDeclaration::IndexedGetter(uint32_t aIndex, bool& aFound,
160                                         nsACString& aPropName) {
161   DeclarationBlock* decl = GetOrCreateCSSDeclaration(Operation::Read, nullptr);
162   aFound = decl && decl->GetNthProperty(aIndex, aPropName);
163 }
164 
165 NS_IMETHODIMP
GetPropertyValue(const nsACString & aPropertyName,nsACString & aReturn)166 nsDOMCSSDeclaration::GetPropertyValue(const nsACString& aPropertyName,
167                                       nsACString& aReturn) {
168   MOZ_ASSERT(aReturn.IsEmpty());
169   if (auto* decl = GetOrCreateCSSDeclaration(Operation::Read, nullptr)) {
170     decl->GetPropertyValue(aPropertyName, aReturn);
171   }
172   return NS_OK;
173 }
174 
GetPropertyPriority(const nsACString & aPropertyName,nsACString & aPriority)175 void nsDOMCSSDeclaration::GetPropertyPriority(const nsACString& aPropertyName,
176                                               nsACString& aPriority) {
177   MOZ_ASSERT(aPriority.IsEmpty());
178   DeclarationBlock* decl = GetOrCreateCSSDeclaration(Operation::Read, nullptr);
179   if (decl && decl->GetPropertyIsImportant(aPropertyName)) {
180     aPriority.AssignLiteral("important");
181   }
182 }
183 
SetProperty(const nsACString & aPropertyName,const nsACString & aValue,const nsACString & aPriority,nsIPrincipal * aSubjectPrincipal,ErrorResult & aRv)184 void nsDOMCSSDeclaration::SetProperty(const nsACString& aPropertyName,
185                                       const nsACString& aValue,
186                                       const nsACString& aPriority,
187                                       nsIPrincipal* aSubjectPrincipal,
188                                       ErrorResult& aRv) {
189   if (IsReadOnly()) {
190     return;
191   }
192 
193   if (aValue.IsEmpty()) {
194     // If the new value of the property is an empty string we remove the
195     // property.
196     // XXX this ignores the priority string, should it?
197     return RemovePropertyInternal(aPropertyName, aRv);
198   }
199 
200   // In the common (and fast) cases we can use the property id
201   nsCSSPropertyID propID = nsCSSProps::LookupProperty(aPropertyName);
202   if (propID == eCSSProperty_UNKNOWN) {
203     return;
204   }
205 
206   bool important;
207   if (aPriority.IsEmpty()) {
208     important = false;
209   } else if (aPriority.LowerCaseEqualsASCII("important")) {
210     important = true;
211   } else {
212     // XXX silent failure?
213     return;
214   }
215 
216   if (propID == eCSSPropertyExtra_variable) {
217     aRv = ParseCustomPropertyValue(aPropertyName, aValue, important,
218                                    aSubjectPrincipal);
219     return;
220   }
221   aRv = ParsePropertyValue(propID, aValue, important, aSubjectPrincipal);
222 }
223 
RemoveProperty(const nsACString & aPropertyName,nsACString & aReturn,ErrorResult & aRv)224 void nsDOMCSSDeclaration::RemoveProperty(const nsACString& aPropertyName,
225                                          nsACString& aReturn,
226                                          ErrorResult& aRv) {
227   if (IsReadOnly()) {
228     return;
229   }
230 
231   nsresult rv = GetPropertyValue(aPropertyName, aReturn);
232   if (NS_WARN_IF(NS_FAILED(rv))) {
233     aRv.Throw(rv);
234     return;
235   }
236   RemovePropertyInternal(aPropertyName, aRv);
237 }
238 
239 /* static */ nsDOMCSSDeclaration::ParsingEnvironment
GetParsingEnvironmentForRule(const css::Rule * aRule,uint16_t aRuleType)240 nsDOMCSSDeclaration::GetParsingEnvironmentForRule(const css::Rule* aRule,
241                                                   uint16_t aRuleType) {
242   if (!aRule) {
243     return {};
244   }
245 
246   MOZ_ASSERT(aRule->Type() == aRuleType);
247 
248   StyleSheet* sheet = aRule->GetStyleSheet();
249   if (!sheet) {
250     return {};
251   }
252 
253   if (Document* document = sheet->GetAssociatedDocument()) {
254     return {
255         sheet->URLData(),
256         document->GetCompatibilityMode(),
257         document->CSSLoader(),
258         aRuleType,
259     };
260   }
261 
262   return {
263       sheet->URLData(),
264       eCompatibility_FullStandards,
265       nullptr,
266       aRuleType,
267   };
268 }
269 
270 template <typename Func>
ModifyDeclaration(nsIPrincipal * aSubjectPrincipal,MutationClosureData * aClosureData,Func aFunc)271 nsresult nsDOMCSSDeclaration::ModifyDeclaration(
272     nsIPrincipal* aSubjectPrincipal, MutationClosureData* aClosureData,
273     Func aFunc) {
274   RefPtr<DeclarationBlock> created;
275   DeclarationBlock* olddecl =
276       GetOrCreateCSSDeclaration(Operation::Modify, getter_AddRefs(created));
277   if (!olddecl) {
278     return NS_ERROR_NOT_AVAILABLE;
279   }
280 
281   // For nsDOMCSSAttributeDeclaration, SetCSSDeclaration will lead to
282   // Attribute setting code, which leads in turn to BeginUpdate.  We
283   // need to start the update now so that the old rule doesn't get used
284   // between when we mutate the declaration and when we set the new
285   // rule (see stack in bug 209575).
286   mozAutoDocUpdate autoUpdate(DocToUpdate(), true);
287   RefPtr<DeclarationBlock> decl = olddecl->EnsureMutable();
288 
289   bool changed;
290   ParsingEnvironment servoEnv = GetParsingEnvironment(aSubjectPrincipal);
291   if (!servoEnv.mUrlExtraData) {
292     return NS_ERROR_NOT_AVAILABLE;
293   }
294 
295   changed = aFunc(decl, servoEnv);
296 
297   if (!changed) {
298     // Parsing failed -- but we don't throw an exception for that.
299     return NS_OK;
300   }
301 
302   return SetCSSDeclaration(decl, aClosureData);
303 }
304 
ParsePropertyValue(const nsCSSPropertyID aPropID,const nsACString & aPropValue,bool aIsImportant,nsIPrincipal * aSubjectPrincipal)305 nsresult nsDOMCSSDeclaration::ParsePropertyValue(
306     const nsCSSPropertyID aPropID, const nsACString& aPropValue,
307     bool aIsImportant, nsIPrincipal* aSubjectPrincipal) {
308   AUTO_PROFILER_LABEL_CATEGORY_PAIR(LAYOUT_CSSParsing);
309 
310   if (IsReadOnly()) {
311     return NS_OK;
312   }
313 
314   DeclarationBlockMutationClosure closure = {};
315   MutationClosureData closureData;
316   GetPropertyChangeClosure(&closure, &closureData);
317 
318   return ModifyDeclaration(
319       aSubjectPrincipal, &closureData,
320       [&](DeclarationBlock* decl, ParsingEnvironment& env) {
321         return Servo_DeclarationBlock_SetPropertyById(
322             decl->Raw(), aPropID, &aPropValue, aIsImportant, env.mUrlExtraData,
323             ParsingMode::Default, env.mCompatMode, env.mLoader, env.mRuleType,
324             closure);
325       });
326 }
327 
ParseCustomPropertyValue(const nsACString & aPropertyName,const nsACString & aPropValue,bool aIsImportant,nsIPrincipal * aSubjectPrincipal)328 nsresult nsDOMCSSDeclaration::ParseCustomPropertyValue(
329     const nsACString& aPropertyName, const nsACString& aPropValue,
330     bool aIsImportant, nsIPrincipal* aSubjectPrincipal) {
331   MOZ_ASSERT(nsCSSProps::IsCustomPropertyName(aPropertyName));
332 
333   if (IsReadOnly()) {
334     return NS_OK;
335   }
336 
337   DeclarationBlockMutationClosure closure = {};
338   MutationClosureData closureData;
339   GetPropertyChangeClosure(&closure, &closureData);
340 
341   return ModifyDeclaration(
342       aSubjectPrincipal, &closureData,
343       [&](DeclarationBlock* decl, ParsingEnvironment& env) {
344         return Servo_DeclarationBlock_SetProperty(
345             decl->Raw(), &aPropertyName, &aPropValue, aIsImportant,
346             env.mUrlExtraData, ParsingMode::Default, env.mCompatMode,
347             env.mLoader, env.mRuleType, closure);
348       });
349 }
350 
RemovePropertyInternal(nsCSSPropertyID aPropID,ErrorResult & aRv)351 void nsDOMCSSDeclaration::RemovePropertyInternal(nsCSSPropertyID aPropID,
352                                                  ErrorResult& aRv) {
353   DeclarationBlock* olddecl =
354       GetOrCreateCSSDeclaration(Operation::RemoveProperty, nullptr);
355   if (IsReadOnly()) {
356     return;
357   }
358 
359   if (!olddecl) {
360     return;  // no decl, so nothing to remove
361   }
362 
363   // For nsDOMCSSAttributeDeclaration, SetCSSDeclaration will lead to
364   // Attribute setting code, which leads in turn to BeginUpdate.  We
365   // need to start the update now so that the old rule doesn't get used
366   // between when we mutate the declaration and when we set the new
367   // rule (see stack in bug 209575).
368   mozAutoDocUpdate autoUpdate(DocToUpdate(), true);
369 
370   DeclarationBlockMutationClosure closure = {};
371   MutationClosureData closureData;
372   GetPropertyChangeClosure(&closure, &closureData);
373 
374   RefPtr<DeclarationBlock> decl = olddecl->EnsureMutable();
375   if (!decl->RemovePropertyByID(aPropID, closure)) {
376     return;
377   }
378   aRv = SetCSSDeclaration(decl, &closureData);
379 }
380 
RemovePropertyInternal(const nsACString & aPropertyName,ErrorResult & aRv)381 void nsDOMCSSDeclaration::RemovePropertyInternal(
382     const nsACString& aPropertyName, ErrorResult& aRv) {
383   if (IsReadOnly()) {
384     return;
385   }
386 
387   DeclarationBlock* olddecl =
388       GetOrCreateCSSDeclaration(Operation::RemoveProperty, nullptr);
389   if (!olddecl) {
390     return;  // no decl, so nothing to remove
391   }
392 
393   // For nsDOMCSSAttributeDeclaration, SetCSSDeclaration will lead to
394   // Attribute setting code, which leads in turn to BeginUpdate.  We
395   // need to start the update now so that the old rule doesn't get used
396   // between when we mutate the declaration and when we set the new
397   // rule (see stack in bug 209575).
398   mozAutoDocUpdate autoUpdate(DocToUpdate(), true);
399 
400   DeclarationBlockMutationClosure closure = {};
401   MutationClosureData closureData;
402   GetPropertyChangeClosure(&closure, &closureData);
403 
404   RefPtr<DeclarationBlock> decl = olddecl->EnsureMutable();
405   if (!decl->RemoveProperty(aPropertyName, closure)) {
406     return;
407   }
408   aRv = SetCSSDeclaration(decl, &closureData);
409 }
410