HTMLScriptElement.h (7380B)
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 #ifndef mozilla_dom_HTMLScriptElement_h 8 #define mozilla_dom_HTMLScriptElement_h 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/dom/FetchPriority.h" 12 #include "mozilla/dom/ScriptElement.h" 13 #include "nsGenericHTMLElement.h" 14 #include "nsStringFwd.h" 15 16 class nsDOMTokenList; 17 18 namespace mozilla::dom { 19 20 class OwningTrustedScriptOrNullIsEmptyString; 21 class OwningTrustedScriptOrString; 22 class OwningTrustedScriptURLOrUSVString; 23 class TrustedScriptOrNullIsEmptyString; 24 class TrustedScriptOrString; 25 class TrustedScriptURLOrUSVString; 26 27 class HTMLScriptElement final : public nsGenericHTMLElement, 28 public ScriptElement { 29 public: 30 using Element::GetCharacterDataBuffer; 31 32 HTMLScriptElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, 33 FromParser aFromParser); 34 35 // nsISupports 36 NS_DECL_ISUPPORTS_INHERITED 37 38 // CC 39 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLScriptElement, 40 nsGenericHTMLElement) 41 42 void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError) override; 43 44 void SetInnerHTMLTrusted(const nsAString& aInnerHTML, 45 nsIPrincipal* aSubjectPrincipal, 46 mozilla::ErrorResult& aError) override; 47 48 public: 49 // nsIScriptElement 50 virtual void GetScriptText(nsAString& text) const override; 51 virtual void GetScriptCharset(nsAString& charset) override; 52 virtual void FreezeExecutionAttrs(const Document* aOwnerDoc) override; 53 virtual CORSMode GetCORSMode() const override; 54 virtual FetchPriority GetFetchPriority() const override; 55 virtual mozilla::dom::ReferrerPolicy GetReferrerPolicy() override; 56 57 // nsIContent 58 virtual nsresult BindToTree(BindContext&, nsINode& aParent) override; 59 virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, 60 const nsAString& aValue, 61 nsIPrincipal* aMaybeScriptedPrincipal, 62 nsAttrValue& aResult) override; 63 64 virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override; 65 66 // Element 67 virtual void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName, 68 const nsAttrValue* aValue, 69 const nsAttrValue* aOldValue, 70 nsIPrincipal* aMaybeScriptedPrincipal, 71 bool aNotify) override; 72 73 // WebIDL 74 void GetText(nsAString& aValue, ErrorResult& aRv) const; 75 76 // @param aValue will always be of type `String`. 77 MOZ_CAN_RUN_SCRIPT void GetText(OwningTrustedScriptOrString& aValue, 78 ErrorResult& aRv) const; 79 80 MOZ_CAN_RUN_SCRIPT void SetText(const TrustedScriptOrString& aValue, 81 nsIPrincipal* aSubjectPrincipal, 82 ErrorResult& aRv); 83 84 // @param aValue will always be of type `NullIsEmptyString`. 85 MOZ_CAN_RUN_SCRIPT void GetInnerText( 86 OwningTrustedScriptOrNullIsEmptyString& aValue, ErrorResult& aError); 87 88 MOZ_CAN_RUN_SCRIPT void SetInnerText( 89 const TrustedScriptOrNullIsEmptyString& aValue, 90 nsIPrincipal* aSubjectPrincipal, ErrorResult& aError); 91 92 // @param aTextContent will always be of type `String`. 93 MOZ_CAN_RUN_SCRIPT void GetTrustedScriptOrStringTextContent( 94 Nullable<OwningTrustedScriptOrString>& aTextContent, 95 mozilla::OOMReporter& aError); 96 97 MOZ_CAN_RUN_SCRIPT void SetTrustedScriptOrStringTextContent( 98 const Nullable<TrustedScriptOrString>& aTextContent, 99 nsIPrincipal* aSubjectPrincipal, mozilla::ErrorResult& aError); 100 101 void GetCharset(nsAString& aCharset) { 102 GetHTMLAttr(nsGkAtoms::charset, aCharset); 103 } 104 void SetCharset(const nsAString& aCharset, ErrorResult& aRv) { 105 SetHTMLAttr(nsGkAtoms::charset, aCharset, aRv); 106 } 107 108 bool Defer() { return GetBoolAttr(nsGkAtoms::defer); } 109 void SetDefer(bool aDefer, ErrorResult& aRv) { 110 SetHTMLBoolAttr(nsGkAtoms::defer, aDefer, aRv); 111 } 112 113 // @param aSrc will always be of type `String`. 114 void GetSrc(OwningTrustedScriptURLOrUSVString& aSrc); 115 116 MOZ_CAN_RUN_SCRIPT void SetSrc(const TrustedScriptURLOrUSVString& aSrc, 117 nsIPrincipal* aSubjectPrincipal, 118 ErrorResult& aRv); 119 120 void GetType(nsAString& aType) { GetHTMLAttr(nsGkAtoms::type, aType); } 121 void SetType(const nsAString& aType, ErrorResult& aRv) { 122 SetHTMLAttr(nsGkAtoms::type, aType, aRv); 123 } 124 125 void GetHtmlFor(nsAString& aHtmlFor) { 126 GetHTMLAttr(nsGkAtoms::_for, aHtmlFor); 127 } 128 void SetHtmlFor(const nsAString& aHtmlFor, ErrorResult& aRv) { 129 SetHTMLAttr(nsGkAtoms::_for, aHtmlFor, aRv); 130 } 131 132 void GetEvent(nsAString& aEvent) { GetHTMLAttr(nsGkAtoms::event, aEvent); } 133 void SetEvent(const nsAString& aEvent, ErrorResult& aRv) { 134 SetHTMLAttr(nsGkAtoms::event, aEvent, aRv); 135 } 136 137 bool Async() { return mForceAsync || GetBoolAttr(nsGkAtoms::async); } 138 139 void SetAsync(bool aValue, ErrorResult& aRv) { 140 mForceAsync = false; 141 SetHTMLBoolAttr(nsGkAtoms::async, aValue, aRv); 142 } 143 144 bool NoModule() { return GetBoolAttr(nsGkAtoms::nomodule); } 145 146 void SetNoModule(bool aValue, ErrorResult& aRv) { 147 SetHTMLBoolAttr(nsGkAtoms::nomodule, aValue, aRv); 148 } 149 150 void GetCrossOrigin(nsAString& aResult) { 151 // Null for both missing and invalid defaults is ok, since we 152 // always parse to an enum value, so we don't need an invalid 153 // default, and we _want_ the missing default to be null. 154 GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult); 155 } 156 void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError) { 157 SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError); 158 } 159 void GetIntegrity(nsAString& aIntegrity) { 160 GetHTMLAttr(nsGkAtoms::integrity, aIntegrity); 161 } 162 void SetIntegrity(const nsAString& aIntegrity, ErrorResult& aRv) { 163 SetHTMLAttr(nsGkAtoms::integrity, aIntegrity, aRv); 164 } 165 void SetReferrerPolicy(const nsAString& aReferrerPolicy, 166 ErrorResult& aError) { 167 SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrerPolicy, aError); 168 } 169 void GetReferrerPolicy(nsAString& aReferrerPolicy) { 170 GetEnumAttr(nsGkAtoms::referrerpolicy, "", aReferrerPolicy); 171 } 172 173 nsDOMTokenList* Blocking(); 174 bool IsPotentiallyRenderBlocking() override; 175 176 // Required for the webidl-binding because `GetFetchPriority` is overloaded. 177 using nsGenericHTMLElement::GetFetchPriority; 178 179 [[nodiscard]] static bool Supports(const GlobalObject& aGlobal, 180 const nsAString& aType); 181 182 protected: 183 virtual ~HTMLScriptElement(); 184 185 virtual bool GetAsyncState() override { return Async(); } 186 187 virtual JSObject* WrapNode(JSContext* aCx, 188 JS::Handle<JSObject*> aGivenProto) override; 189 190 // nsIScriptElement 191 nsIContent* GetAsContent() override { return this; } 192 193 // ScriptElement 194 virtual bool HasExternalScriptContent() override; 195 196 RefPtr<nsDOMTokenList> mBlocking; 197 }; 198 199 } // namespace mozilla::dom 200 201 #endif // mozilla_dom_HTMLScriptElement_h