nsDOMCSSDeclaration.h (7768B)
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 #ifndef nsDOMCSSDeclaration_h___ 10 #define nsDOMCSSDeclaration_h___ 11 12 #include "mozilla/Attributes.h" 13 #include "mozilla/Maybe.h" 14 #include "mozilla/URLExtraData.h" 15 #include "nsAttrValue.h" 16 #include "nsCOMPtr.h" 17 #include "nsCompatibility.h" 18 #include "nsICSSDeclaration.h" 19 // The following include provides nsCSSProps::PropertyIDLName(), used by 20 // generated CSS2PropertiesBinding.cpp 21 // TODO: Ideally it would only be included from there. 22 #include "nsCSSProps.h" 23 24 class nsIPrincipal; 25 struct JSContext; 26 class JSObject; 27 enum class AttrModType : uint8_t; // Defined in nsIMutationObserver.h 28 29 namespace mozilla { 30 enum class StyleCssRuleType : uint8_t; 31 class DeclarationBlock; 32 struct DeclarationBlockMutationClosure; 33 namespace css { 34 class Loader; 35 class Rule; 36 } // namespace css 37 namespace dom { 38 class Document; 39 class Element; 40 } // namespace dom 41 42 struct MutationClosureData { 43 MutationClosureData() = default; 44 45 mozilla::dom::Element* mElement = nullptr; 46 Maybe<nsAttrValue> mOldValue; 47 AttrModType mModType{0}; // NOTE: The initial value is invalid value. 48 bool mWasCalled = false; 49 bool mShouldBeCalled = false; 50 }; 51 52 } // namespace mozilla 53 54 class nsDOMCSSDeclaration : public nsICSSDeclaration { 55 public: 56 // Only implement QueryInterface; subclasses have the responsibility 57 // of implementing AddRef/Release. 58 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; 59 60 // Declare addref and release so they can be called on us, but don't 61 // implement them. Our subclasses must handle their own 62 // refcounting. 63 NS_IMETHOD_(MozExternalRefCountType) AddRef() override = 0; 64 NS_IMETHOD_(MozExternalRefCountType) Release() override = 0; 65 66 /** 67 * Method analogous to CSSStyleDeclaration::GetPropertyValue, 68 * which obeys all the same restrictions. 69 */ 70 virtual void GetPropertyValue(const NonCustomCSSPropertyId aPropId, 71 nsACString& aValue); 72 73 /** 74 * Method analogous to CSSStyleDeclaration::SetProperty. This 75 * method does NOT allow setting a priority (the priority will 76 * always be set to default priority). 77 */ 78 virtual void SetPropertyValue(const NonCustomCSSPropertyId aPropId, 79 const nsACString& aValue, 80 nsIPrincipal* aSubjectPrincipal, 81 mozilla::ErrorResult& aRv); 82 83 // Require subclasses to implement |GetParentRule|. 84 // NS_DECL_NSIDOMCSSSTYLEDECLARATION 85 void GetCssText(nsACString& aCssText) override; 86 void SetCssText(const nsACString& aCssText, nsIPrincipal* aSubjectPrincipal, 87 mozilla::ErrorResult& aRv) override; 88 void GetPropertyValue(const nsACString& propertyName, 89 nsACString& _retval) override; 90 bool HasLonghandProperty(const nsACString& propertyName) override; 91 void RemoveProperty(const nsACString& propertyName, nsACString& _retval, 92 mozilla::ErrorResult& aRv) override; 93 void GetPropertyPriority(const nsACString& propertyName, 94 nsACString& aPriority) override; 95 void SetProperty(const nsACString& propertyName, const nsACString& value, 96 const nsACString& priority, nsIPrincipal* aSubjectPrincipal, 97 mozilla::ErrorResult& aRv) override; 98 using nsICSSDeclaration::SetProperty; 99 uint32_t Length() override; 100 101 // WebIDL interface for CSSStyleProperties 102 virtual void IndexedGetter(uint32_t aIndex, bool& aFound, 103 nsACString& aPropName) override; 104 105 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 106 107 // Information needed to parse a declaration for Servo side. 108 // Put this in public so other Servo parsing functions can reuse this. 109 struct MOZ_STACK_CLASS ParsingEnvironment { 110 RefPtr<mozilla::URLExtraData> mUrlExtraData; 111 nsCompatibility mCompatMode = eCompatibility_FullStandards; 112 mozilla::css::Loader* mLoader = nullptr; 113 mozilla::StyleCssRuleType mRuleType{1 /* Style */}; 114 }; 115 116 protected: 117 // The reason for calling GetOrCreateCSSDeclaration. 118 enum class Operation { 119 // We are calling GetOrCreateCSSDeclaration so that we can read from it. 120 // Does not allocate a new declaration if we don't have one yet; returns 121 // nullptr in this case. 122 Read, 123 124 // We are calling GetOrCreateCSSDeclaration so that we can set a property on 125 // it or re-parse the whole declaration. Allocates a new declaration if we 126 // don't have one yet. A nullptr return value indicates an error allocating 127 // the declaration. 128 Modify, 129 130 // We are calling GetOrCreateCSSDeclaration so that we can remove a property 131 // from it. Does not allocate a new declaration if we don't have one yet; 132 // returns nullptr in this case. 133 RemoveProperty, 134 }; 135 136 // If aOperation is Modify, aCreated must be non-null and the call may set it 137 // to point to the newly created object. 138 virtual mozilla::DeclarationBlock* GetOrCreateCSSDeclaration( 139 Operation aOperation, mozilla::DeclarationBlock** aCreated) = 0; 140 141 virtual nsresult SetCSSDeclaration( 142 mozilla::DeclarationBlock* aDecl, 143 mozilla::MutationClosureData* aClosureData) = 0; 144 // Document that we must call BeginUpdate/EndUpdate on around the 145 // calls to SetCSSDeclaration and the style rule mutation that leads 146 // to it. 147 virtual mozilla::dom::Document* DocToUpdate() { return nullptr; } 148 149 // mUrlExtraData returns URL data for parsing url values in 150 // CSS. Returns nullptr on failure. If mUrlExtraData is nullptr, 151 // mCompatMode may not be set to anything meaningful. 152 // If aSubjectPrincipal is passed, it should be the subject principal of the 153 // scripted caller that initiated the parser. 154 virtual ParsingEnvironment GetParsingEnvironment( 155 nsIPrincipal* aSubjectPrincipal = nullptr) const = 0; 156 157 // An implementation for GetParsingEnvironment for callers wrapping a 158 // css::Rule. 159 // 160 // The RuleType argument is just to avoid a virtual call, since all callers 161 // know it statically. Should be equal to aRule->Type(). 162 static ParsingEnvironment GetParsingEnvironmentForRule( 163 const mozilla::css::Rule* aRule, mozilla::StyleCssRuleType); 164 165 nsresult ParsePropertyValue(const NonCustomCSSPropertyId aPropId, 166 const nsACString& aPropValue, bool aIsImportant, 167 nsIPrincipal* aSubjectPrincipal); 168 169 nsresult ParseCustomPropertyValue(const nsACString& aPropertyName, 170 const nsACString& aPropValue, 171 bool aIsImportant, 172 nsIPrincipal* aSubjectPrincipal); 173 174 void RemovePropertyInternal(NonCustomCSSPropertyId aPropId, 175 mozilla::ErrorResult& aRv); 176 void RemovePropertyInternal(const nsACString& aPropert, 177 mozilla::ErrorResult& aRv); 178 179 virtual void GetPropertyChangeClosure( 180 mozilla::DeclarationBlockMutationClosure* aClosure, 181 mozilla::MutationClosureData* aClosureData) {} 182 183 protected: 184 virtual ~nsDOMCSSDeclaration(); 185 186 private: 187 template <typename ServoFunc> 188 inline nsresult ModifyDeclaration(nsIPrincipal* aSubjectPrincipal, 189 mozilla::MutationClosureData* aClosureData, 190 ServoFunc aServoFunc); 191 }; 192 193 #endif // nsDOMCSSDeclaration_h___