Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / svg / SVGScriptElement.cpp
blob22fb23998094e2ae361c7c417a60a847b15f1462
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 #include "mozilla/dom/SVGScriptElement.h"
9 #include "mozilla/dom/FetchPriority.h"
10 #include "nsGkAtoms.h"
11 #include "nsNetUtil.h"
12 #include "nsContentUtils.h"
13 #include "mozilla/dom/SVGScriptElementBinding.h"
14 #include "nsIScriptError.h"
16 NS_IMPL_NS_NEW_SVG_ELEMENT_CHECK_PARSER(Script)
18 using JS::loader::ScriptKind;
20 namespace mozilla::dom {
22 JSObject* SVGScriptElement::WrapNode(JSContext* aCx,
23 JS::Handle<JSObject*> aGivenProto) {
24 return SVGScriptElement_Binding::Wrap(aCx, this, aGivenProto);
27 SVGElement::StringInfo SVGScriptElement::sStringInfo[2] = {
28 {nsGkAtoms::href, kNameSpaceID_None, false},
29 {nsGkAtoms::href, kNameSpaceID_XLink, false}};
31 //----------------------------------------------------------------------
32 // nsISupports methods
34 NS_IMPL_ISUPPORTS_INHERITED(SVGScriptElement, SVGScriptElementBase,
35 nsIScriptLoaderObserver, nsIScriptElement,
36 nsIMutationObserver)
38 //----------------------------------------------------------------------
39 // Implementation
41 SVGScriptElement::SVGScriptElement(
42 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
43 FromParser aFromParser)
44 : SVGScriptElementBase(std::move(aNodeInfo)), ScriptElement(aFromParser) {
45 AddMutationObserver(this);
48 //----------------------------------------------------------------------
49 // nsINode methods
51 nsresult SVGScriptElement::Clone(dom::NodeInfo* aNodeInfo,
52 nsINode** aResult) const {
53 *aResult = nullptr;
55 SVGScriptElement* it = new (aNodeInfo->NodeInfoManager())
56 SVGScriptElement(do_AddRef(aNodeInfo), NOT_FROM_PARSER);
58 nsCOMPtr<nsINode> kungFuDeathGrip = it;
59 nsresult rv1 = it->Init();
60 nsresult rv2 = const_cast<SVGScriptElement*>(this)->CopyInnerTo(it);
61 NS_ENSURE_SUCCESS(rv1, rv1);
62 NS_ENSURE_SUCCESS(rv2, rv2);
64 // The clone should be marked evaluated if we are.
65 it->mAlreadyStarted = mAlreadyStarted;
66 it->mLineNumber = mLineNumber;
67 it->mMalformed = mMalformed;
69 kungFuDeathGrip.swap(*aResult);
71 return NS_OK;
74 //----------------------------------------------------------------------
75 void SVGScriptElement::GetType(nsAString& aType) {
76 GetAttr(nsGkAtoms::type, aType);
79 void SVGScriptElement::SetType(const nsAString& aType, ErrorResult& rv) {
80 rv = SetAttr(kNameSpaceID_None, nsGkAtoms::type, aType, true);
83 void SVGScriptElement::GetCrossOrigin(nsAString& aCrossOrigin) {
84 // Null for both missing and invalid defaults is ok, since we
85 // always parse to an enum value, so we don't need an invalid
86 // default, and we _want_ the missing default to be null.
87 GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aCrossOrigin);
90 void SVGScriptElement::SetCrossOrigin(const nsAString& aCrossOrigin,
91 ErrorResult& aError) {
92 SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
95 already_AddRefed<DOMSVGAnimatedString> SVGScriptElement::Href() {
96 return mStringAttributes[HREF].IsExplicitlySet()
97 ? mStringAttributes[HREF].ToDOMAnimatedString(this)
98 : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
101 //----------------------------------------------------------------------
102 // nsIScriptElement methods
104 void SVGScriptElement::GetScriptText(nsAString& text) const {
105 nsContentUtils::GetNodeTextContent(this, false, text);
108 void SVGScriptElement::GetScriptCharset(nsAString& charset) {
109 charset.Truncate();
112 void SVGScriptElement::FreezeExecutionAttrs(const Document* aOwnerDoc) {
113 if (mFrozen) {
114 return;
117 // Determine whether this is a(n) classic/module/importmap script.
118 DetermineKindFromType(aOwnerDoc);
120 if (mStringAttributes[HREF].IsExplicitlySet() ||
121 mStringAttributes[XLINK_HREF].IsExplicitlySet()) {
122 // variation of this code in nsHTMLScriptElement - check if changes
123 // need to be transferred when modifying
124 bool isHref = false;
125 nsAutoString src;
126 if (mStringAttributes[HREF].IsExplicitlySet()) {
127 mStringAttributes[HREF].GetAnimValue(src, this);
128 isHref = true;
129 } else {
130 mStringAttributes[XLINK_HREF].GetAnimValue(src, this);
133 // Empty src should be treated as invalid URL.
134 if (!src.IsEmpty()) {
135 NS_NewURI(getter_AddRefs(mUri), src, nullptr, GetBaseURI());
137 if (!mUri) {
138 AutoTArray<nsString, 2> params = {
139 isHref ? u"href"_ns : u"xlink:href"_ns, src};
141 nsContentUtils::ReportToConsole(
142 nsIScriptError::warningFlag, "SVG"_ns, OwnerDoc(),
143 nsContentUtils::eDOM_PROPERTIES, "ScriptSourceInvalidUri", params,
144 nullptr, u""_ns, GetScriptLineNumber(),
145 GetScriptColumnNumber().oneOriginValue());
147 } else {
148 AutoTArray<nsString, 1> params = {isHref ? u"href"_ns : u"xlink:href"_ns};
150 nsContentUtils::ReportToConsole(
151 nsIScriptError::warningFlag, "SVG"_ns, OwnerDoc(),
152 nsContentUtils::eDOM_PROPERTIES, "ScriptSourceEmpty", params, nullptr,
153 u""_ns, GetScriptLineNumber(),
154 GetScriptColumnNumber().oneOriginValue());
157 // At this point mUri will be null for invalid URLs.
158 mExternal = true;
161 bool async = (mExternal || mKind == ScriptKind::eModule) && Async();
162 bool defer = mExternal && Defer();
164 mDefer = !async && defer;
165 mAsync = async;
167 mFrozen = true;
170 //----------------------------------------------------------------------
171 // ScriptElement methods
173 bool SVGScriptElement::HasScriptContent() {
174 return (mFrozen ? mExternal
175 : mStringAttributes[HREF].IsExplicitlySet() ||
176 mStringAttributes[XLINK_HREF].IsExplicitlySet()) ||
177 nsContentUtils::HasNonEmptyTextContent(this);
180 //----------------------------------------------------------------------
181 // SVGElement methods
183 SVGElement::StringAttributesInfo SVGScriptElement::GetStringInfo() {
184 return StringAttributesInfo(mStringAttributes, sStringInfo,
185 ArrayLength(sStringInfo));
188 //----------------------------------------------------------------------
189 // nsIContent methods
191 nsresult SVGScriptElement::BindToTree(BindContext& aContext, nsINode& aParent) {
192 nsresult rv = SVGScriptElementBase::BindToTree(aContext, aParent);
193 NS_ENSURE_SUCCESS(rv, rv);
195 if (IsInComposedDoc()) {
196 MaybeProcessScript();
199 return NS_OK;
202 bool SVGScriptElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
203 const nsAString& aValue,
204 nsIPrincipal* aMaybeScriptedPrincipal,
205 nsAttrValue& aResult) {
206 if (aNamespaceID == kNameSpaceID_None &&
207 aAttribute == nsGkAtoms::crossorigin) {
208 ParseCORSValue(aValue, aResult);
209 return true;
212 return SVGScriptElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
213 aMaybeScriptedPrincipal, aResult);
216 CORSMode SVGScriptElement::GetCORSMode() const {
217 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
220 FetchPriority SVGScriptElement::GetFetchPriority() const {
221 // <https://github.com/w3c/svgwg/issues/916>.
222 return FetchPriority::Auto;
225 } // namespace mozilla::dom