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 /* base class for DOM objects for element.style and cssStyleRule.style */
9 #ifndef nsDOMCSSDeclaration_h___
10 #define nsDOMCSSDeclaration_h___
12 #include "nsICSSDeclaration.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/URLExtraData.h"
17 #include "nsAttrValue.h"
19 #include "nsCompatibility.h"
26 enum class StyleCssRuleType
: uint8_t;
27 class DeclarationBlock
;
28 struct DeclarationBlockMutationClosure
;
38 struct MutationClosureData
{
39 MutationClosureData() = default;
41 mozilla::dom::Element
* mElement
= nullptr;
42 Maybe
<nsAttrValue
> mOldValue
;
44 bool mWasCalled
= false;
45 bool mShouldBeCalled
= false;
48 } // namespace mozilla
50 class nsDOMCSSDeclaration
: public nsICSSDeclaration
{
52 // Only implement QueryInterface; subclasses have the responsibility
53 // of implementing AddRef/Release.
54 NS_IMETHOD
QueryInterface(REFNSIID aIID
, void** aInstancePtr
) override
;
56 // Declare addref and release so they can be called on us, but don't
57 // implement them. Our subclasses must handle their own
59 NS_IMETHOD_(MozExternalRefCountType
) AddRef() override
= 0;
60 NS_IMETHOD_(MozExternalRefCountType
) Release() override
= 0;
63 * Method analogous to CSSStyleDeclaration::GetPropertyValue,
64 * which obeys all the same restrictions.
66 virtual void GetPropertyValue(const nsCSSPropertyID aPropID
,
70 * Method analogous to CSSStyleDeclaration::SetProperty. This
71 * method does NOT allow setting a priority (the priority will
72 * always be set to default priority).
74 virtual void SetPropertyValue(const nsCSSPropertyID aPropID
,
75 const nsACString
& aValue
,
76 nsIPrincipal
* aSubjectPrincipal
,
77 mozilla::ErrorResult
& aRv
);
79 // Require subclasses to implement |GetParentRule|.
80 // NS_DECL_NSIDOMCSSSTYLEDECLARATION
81 void GetCssText(nsACString
& aCssText
) override
;
82 void SetCssText(const nsACString
& aCssText
, nsIPrincipal
* aSubjectPrincipal
,
83 mozilla::ErrorResult
& aRv
) override
;
84 void GetPropertyValue(const nsACString
& propertyName
,
85 nsACString
& _retval
) override
;
86 void RemoveProperty(const nsACString
& propertyName
, nsACString
& _retval
,
87 mozilla::ErrorResult
& aRv
) override
;
88 void GetPropertyPriority(const nsACString
& propertyName
,
89 nsACString
& aPriority
) override
;
90 void SetProperty(const nsACString
& propertyName
, const nsACString
& value
,
91 const nsACString
& priority
, nsIPrincipal
* aSubjectPrincipal
,
92 mozilla::ErrorResult
& aRv
) override
;
93 uint32_t Length() override
;
95 // WebIDL interface for CSS2Properties
96 virtual void IndexedGetter(uint32_t aIndex
, bool& aFound
,
97 nsACString
& aPropName
) override
;
99 JSObject
* WrapObject(JSContext
*, JS::Handle
<JSObject
*> aGivenProto
) override
;
101 // Information needed to parse a declaration for Servo side.
102 // Put this in public so other Servo parsing functions can reuse this.
103 struct MOZ_STACK_CLASS ParsingEnvironment
{
104 RefPtr
<mozilla::URLExtraData
> mUrlExtraData
;
105 nsCompatibility mCompatMode
= eCompatibility_FullStandards
;
106 mozilla::css::Loader
* mLoader
= nullptr;
107 mozilla::StyleCssRuleType mRuleType
{1 /* Style */};
111 // The reason for calling GetOrCreateCSSDeclaration.
112 enum class Operation
{
113 // We are calling GetOrCreateCSSDeclaration so that we can read from it.
114 // Does not allocate a new declaration if we don't have one yet; returns
115 // nullptr in this case.
118 // We are calling GetOrCreateCSSDeclaration so that we can set a property on
119 // it or re-parse the whole declaration. Allocates a new declaration if we
120 // don't have one yet. A nullptr return value indicates an error allocating
124 // We are calling GetOrCreateCSSDeclaration so that we can remove a property
125 // from it. Does not allocate a new declaration if we don't have one yet;
126 // returns nullptr in this case.
130 // If aOperation is Modify, aCreated must be non-null and the call may set it
131 // to point to the newly created object.
132 virtual mozilla::DeclarationBlock
* GetOrCreateCSSDeclaration(
133 Operation aOperation
, mozilla::DeclarationBlock
** aCreated
) = 0;
135 virtual nsresult
SetCSSDeclaration(
136 mozilla::DeclarationBlock
* aDecl
,
137 mozilla::MutationClosureData
* aClosureData
) = 0;
138 // Document that we must call BeginUpdate/EndUpdate on around the
139 // calls to SetCSSDeclaration and the style rule mutation that leads
141 virtual mozilla::dom::Document
* DocToUpdate() { return nullptr; }
143 // mUrlExtraData returns URL data for parsing url values in
144 // CSS. Returns nullptr on failure. If mUrlExtraData is nullptr,
145 // mCompatMode may not be set to anything meaningful.
146 // If aSubjectPrincipal is passed, it should be the subject principal of the
147 // scripted caller that initiated the parser.
148 virtual ParsingEnvironment
GetParsingEnvironment(
149 nsIPrincipal
* aSubjectPrincipal
= nullptr) const = 0;
151 // An implementation for GetParsingEnvironment for callers wrapping a
154 // The RuleType argument is just to avoid a virtual call, since all callers
155 // know it statically. Should be equal to aRule->Type().
156 static ParsingEnvironment
GetParsingEnvironmentForRule(
157 const mozilla::css::Rule
* aRule
, mozilla::StyleCssRuleType
);
159 nsresult
ParsePropertyValue(const nsCSSPropertyID aPropID
,
160 const nsACString
& aPropValue
, bool aIsImportant
,
161 nsIPrincipal
* aSubjectPrincipal
);
163 nsresult
ParseCustomPropertyValue(const nsACString
& aPropertyName
,
164 const nsACString
& aPropValue
,
166 nsIPrincipal
* aSubjectPrincipal
);
168 void RemovePropertyInternal(nsCSSPropertyID aPropID
,
169 mozilla::ErrorResult
& aRv
);
170 void RemovePropertyInternal(const nsACString
& aPropert
,
171 mozilla::ErrorResult
& aRv
);
173 virtual void GetPropertyChangeClosure(
174 mozilla::DeclarationBlockMutationClosure
* aClosure
,
175 mozilla::MutationClosureData
* aClosureData
) {}
178 virtual ~nsDOMCSSDeclaration();
181 template <typename ServoFunc
>
182 inline nsresult
ModifyDeclaration(nsIPrincipal
* aSubjectPrincipal
,
183 mozilla::MutationClosureData
* aClosureData
,
184 ServoFunc aServoFunc
);
187 #endif // nsDOMCSSDeclaration_h___