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 "nsGenericHTMLElement.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/dom/ScriptElement.h"
13 
14 namespace mozilla {
15 namespace dom {
16 
17 class HTMLScriptElement final : public nsGenericHTMLElement,
18                                 public ScriptElement {
19  public:
20   using Element::GetText;
21 
22   HTMLScriptElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
23                     FromParser aFromParser);
24 
25   // nsISupports
26   NS_DECL_ISUPPORTS_INHERITED
27 
28   void GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError) override;
29   virtual void SetInnerHTML(const nsAString& aInnerHTML,
30                             nsIPrincipal* aSubjectPrincipal,
31                             mozilla::ErrorResult& aError) override;
32 
33   // nsIScriptElement
34   virtual bool GetScriptType(nsAString& type) override;
35   virtual void GetScriptText(nsAString& text) override;
36   virtual void GetScriptCharset(nsAString& charset) override;
37   virtual void FreezeExecutionAttrs(Document* aOwnerDoc) override;
38   virtual CORSMode GetCORSMode() const override;
39   virtual mozilla::dom::ReferrerPolicy GetReferrerPolicy() override;
40 
41   // nsIContent
42   virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
43   virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
44                               const nsAString& aValue,
45                               nsIPrincipal* aMaybeScriptedPrincipal,
46                               nsAttrValue& aResult) override;
47 
48   virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
49 
50   // Element
51   virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
52                                 const nsAttrValue* aValue,
53                                 const nsAttrValue* aOldValue,
54                                 nsIPrincipal* aMaybeScriptedPrincipal,
55                                 bool aNotify) override;
56 
57   // WebIDL
58   void GetText(nsAString& aValue, ErrorResult& aRv);
59 
60   void SetText(const nsAString& aValue, ErrorResult& aRv);
61 
GetCharset(nsAString & aCharset)62   void GetCharset(nsAString& aCharset) {
63     GetHTMLAttr(nsGkAtoms::charset, aCharset);
64   }
SetCharset(const nsAString & aCharset,ErrorResult & aRv)65   void SetCharset(const nsAString& aCharset, ErrorResult& aRv) {
66     SetHTMLAttr(nsGkAtoms::charset, aCharset, aRv);
67   }
68 
Defer()69   bool Defer() { return GetBoolAttr(nsGkAtoms::defer); }
SetDefer(bool aDefer,ErrorResult & aRv)70   void SetDefer(bool aDefer, ErrorResult& aRv) {
71     SetHTMLBoolAttr(nsGkAtoms::defer, aDefer, aRv);
72   }
73 
GetSrc(nsAString & aSrc)74   void GetSrc(nsAString& aSrc) { GetURIAttr(nsGkAtoms::src, nullptr, aSrc); }
SetSrc(const nsAString & aSrc,nsIPrincipal * aTriggeringPrincipal,ErrorResult & aRv)75   void SetSrc(const nsAString& aSrc, nsIPrincipal* aTriggeringPrincipal,
76               ErrorResult& aRv) {
77     SetHTMLAttr(nsGkAtoms::src, aSrc, aTriggeringPrincipal, aRv);
78   }
79 
GetType(nsAString & aType)80   void GetType(nsAString& aType) { GetHTMLAttr(nsGkAtoms::type, aType); }
SetType(const nsAString & aType,ErrorResult & aRv)81   void SetType(const nsAString& aType, ErrorResult& aRv) {
82     SetHTMLAttr(nsGkAtoms::type, aType, aRv);
83   }
84 
GetHtmlFor(nsAString & aHtmlFor)85   void GetHtmlFor(nsAString& aHtmlFor) {
86     GetHTMLAttr(nsGkAtoms::_for, aHtmlFor);
87   }
SetHtmlFor(const nsAString & aHtmlFor,ErrorResult & aRv)88   void SetHtmlFor(const nsAString& aHtmlFor, ErrorResult& aRv) {
89     SetHTMLAttr(nsGkAtoms::_for, aHtmlFor, aRv);
90   }
91 
GetEvent(nsAString & aEvent)92   void GetEvent(nsAString& aEvent) { GetHTMLAttr(nsGkAtoms::event, aEvent); }
SetEvent(const nsAString & aEvent,ErrorResult & aRv)93   void SetEvent(const nsAString& aEvent, ErrorResult& aRv) {
94     SetHTMLAttr(nsGkAtoms::event, aEvent, aRv);
95   }
96 
Async()97   bool Async() { return mForceAsync || GetBoolAttr(nsGkAtoms::async); }
98 
SetAsync(bool aValue,ErrorResult & aRv)99   void SetAsync(bool aValue, ErrorResult& aRv) {
100     mForceAsync = false;
101     SetHTMLBoolAttr(nsGkAtoms::async, aValue, aRv);
102   }
103 
NoModule()104   bool NoModule() { return GetBoolAttr(nsGkAtoms::nomodule); }
105 
SetNoModule(bool aValue,ErrorResult & aRv)106   void SetNoModule(bool aValue, ErrorResult& aRv) {
107     SetHTMLBoolAttr(nsGkAtoms::nomodule, aValue, aRv);
108   }
109 
GetCrossOrigin(nsAString & aResult)110   void GetCrossOrigin(nsAString& aResult) {
111     // Null for both missing and invalid defaults is ok, since we
112     // always parse to an enum value, so we don't need an invalid
113     // default, and we _want_ the missing default to be null.
114     GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
115   }
SetCrossOrigin(const nsAString & aCrossOrigin,ErrorResult & aError)116   void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError) {
117     SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
118   }
GetIntegrity(nsAString & aIntegrity)119   void GetIntegrity(nsAString& aIntegrity) {
120     GetHTMLAttr(nsGkAtoms::integrity, aIntegrity);
121   }
SetIntegrity(const nsAString & aIntegrity,ErrorResult & aRv)122   void SetIntegrity(const nsAString& aIntegrity, ErrorResult& aRv) {
123     SetHTMLAttr(nsGkAtoms::integrity, aIntegrity, aRv);
124   }
SetReferrerPolicy(const nsAString & aReferrerPolicy,ErrorResult & aError)125   void SetReferrerPolicy(const nsAString& aReferrerPolicy,
126                          ErrorResult& aError) {
127     SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrerPolicy, aError);
128   }
GetReferrerPolicy(nsAString & aReferrerPolicy)129   void GetReferrerPolicy(nsAString& aReferrerPolicy) {
130     GetEnumAttr(nsGkAtoms::referrerpolicy, "", aReferrerPolicy);
131   }
132 
133  protected:
134   virtual ~HTMLScriptElement();
135 
GetAsyncState()136   virtual bool GetAsyncState() override { return Async(); }
137 
138   virtual JSObject* WrapNode(JSContext* aCx,
139                              JS::Handle<JSObject*> aGivenProto) override;
140 
141   // ScriptElement
142   virtual bool HasScriptContent() override;
143 };
144 
145 }  // namespace dom
146 }  // namespace mozilla
147 
148 #endif  // mozilla_dom_HTMLScriptElement_h
149