/src/mozilla-central/dom/html/HTMLScriptElement.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "nsGkAtoms.h" |
8 | | #include "nsStyleConsts.h" |
9 | | #include "nsIDocument.h" |
10 | | #include "nsIURI.h" |
11 | | #include "nsNetUtil.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "nsUnicharUtils.h" // for nsCaseInsensitiveStringComparator() |
14 | | #include "nsIScriptContext.h" |
15 | | #include "nsIScriptGlobalObject.h" |
16 | | #include "nsIXPConnect.h" |
17 | | #include "nsServiceManagerUtils.h" |
18 | | #include "nsError.h" |
19 | | #include "nsIArray.h" |
20 | | #include "nsTArray.h" |
21 | | #include "nsDOMJSUtils.h" |
22 | | #include "nsIScriptError.h" |
23 | | #include "nsISupportsImpl.h" |
24 | | #include "mozilla/dom/HTMLScriptElement.h" |
25 | | #include "mozilla/dom/HTMLScriptElementBinding.h" |
26 | | |
27 | | NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Script) |
28 | | |
29 | | namespace mozilla { |
30 | | namespace dom { |
31 | | |
32 | | JSObject* |
33 | | HTMLScriptElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) |
34 | 0 | { |
35 | 0 | return HTMLScriptElement_Binding::Wrap(aCx, this, aGivenProto); |
36 | 0 | } |
37 | | |
38 | | HTMLScriptElement::HTMLScriptElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, |
39 | | FromParser aFromParser) |
40 | | : nsGenericHTMLElement(std::move(aNodeInfo)) |
41 | | , ScriptElement(aFromParser) |
42 | 0 | { |
43 | 0 | AddMutationObserver(this); |
44 | 0 | } |
45 | | |
46 | | HTMLScriptElement::~HTMLScriptElement() |
47 | 0 | { |
48 | 0 | } |
49 | | |
50 | | NS_IMPL_ISUPPORTS_INHERITED(HTMLScriptElement, nsGenericHTMLElement, |
51 | | nsIScriptLoaderObserver, |
52 | | nsIScriptElement, |
53 | | nsIMutationObserver) |
54 | | |
55 | | nsresult |
56 | | HTMLScriptElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
57 | | nsIContent* aBindingParent) |
58 | 0 | { |
59 | 0 | nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent, |
60 | 0 | aBindingParent); |
61 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
62 | 0 |
|
63 | 0 | if (GetComposedDoc()) { |
64 | 0 | MaybeProcessScript(); |
65 | 0 | } |
66 | 0 |
|
67 | 0 | return NS_OK; |
68 | 0 | } |
69 | | |
70 | | bool |
71 | | HTMLScriptElement::ParseAttribute(int32_t aNamespaceID, |
72 | | nsAtom* aAttribute, |
73 | | const nsAString& aValue, |
74 | | nsIPrincipal* aMaybeScriptedPrincipal, |
75 | | nsAttrValue& aResult) |
76 | 0 | { |
77 | 0 | if (aNamespaceID == kNameSpaceID_None) { |
78 | 0 | if (aAttribute == nsGkAtoms::crossorigin) { |
79 | 0 | ParseCORSValue(aValue, aResult); |
80 | 0 | return true; |
81 | 0 | } |
82 | 0 | |
83 | 0 | if (aAttribute == nsGkAtoms::integrity) { |
84 | 0 | aResult.ParseStringOrAtom(aValue); |
85 | 0 | return true; |
86 | 0 | } |
87 | 0 | } |
88 | 0 | |
89 | 0 | return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, |
90 | 0 | aMaybeScriptedPrincipal, aResult); |
91 | 0 | } |
92 | | |
93 | | nsresult |
94 | | HTMLScriptElement::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const |
95 | 0 | { |
96 | 0 | *aResult = nullptr; |
97 | 0 |
|
98 | 0 | HTMLScriptElement* it = new HTMLScriptElement(do_AddRef(aNodeInfo), |
99 | 0 | NOT_FROM_PARSER); |
100 | 0 |
|
101 | 0 | nsCOMPtr<nsINode> kungFuDeathGrip = it; |
102 | 0 | nsresult rv = const_cast<HTMLScriptElement*>(this)->CopyInnerTo(it); |
103 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
104 | 0 |
|
105 | 0 | // The clone should be marked evaluated if we are. |
106 | 0 | it->mAlreadyStarted = mAlreadyStarted; |
107 | 0 | it->mLineNumber = mLineNumber; |
108 | 0 | it->mMalformed = mMalformed; |
109 | 0 |
|
110 | 0 | kungFuDeathGrip.swap(*aResult); |
111 | 0 |
|
112 | 0 | return NS_OK; |
113 | 0 | } |
114 | | |
115 | | nsresult |
116 | | HTMLScriptElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName, |
117 | | const nsAttrValue* aValue, |
118 | | const nsAttrValue* aOldValue, |
119 | | nsIPrincipal* aMaybeScriptedPrincipal, |
120 | | bool aNotify) |
121 | 0 | { |
122 | 0 | if (nsGkAtoms::async == aName && kNameSpaceID_None == aNamespaceID) { |
123 | 0 | mForceAsync = false; |
124 | 0 | } |
125 | 0 | if (nsGkAtoms::src == aName && kNameSpaceID_None == aNamespaceID) { |
126 | 0 | mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal( |
127 | 0 | this, aValue ? aValue->GetStringValue() : EmptyString(), |
128 | 0 | aMaybeScriptedPrincipal); |
129 | 0 | } |
130 | 0 | return nsGenericHTMLElement::AfterSetAttr(aNamespaceID, aName, |
131 | 0 | aValue, aOldValue, |
132 | 0 | aMaybeScriptedPrincipal, |
133 | 0 | aNotify); |
134 | 0 | } |
135 | | |
136 | | void |
137 | | HTMLScriptElement::GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError) |
138 | 0 | { |
139 | 0 | if (!nsContentUtils::GetNodeTextContent(this, false, aInnerHTML, fallible)) { |
140 | 0 | aError.ReportOOM(); |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | void |
145 | | HTMLScriptElement::SetInnerHTML(const nsAString& aInnerHTML, |
146 | | nsIPrincipal* aScriptedPrincipal, |
147 | | ErrorResult& aError) |
148 | 0 | { |
149 | 0 | aError = nsContentUtils::SetNodeTextContent(this, aInnerHTML, true); |
150 | 0 | } |
151 | | |
152 | | void |
153 | | HTMLScriptElement::GetText(nsAString& aValue, ErrorResult& aRv) |
154 | 0 | { |
155 | 0 | if (!nsContentUtils::GetNodeTextContent(this, false, aValue, fallible)) { |
156 | 0 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | void |
161 | | HTMLScriptElement::SetText(const nsAString& aValue, ErrorResult& aRv) |
162 | 0 | { |
163 | 0 | aRv = nsContentUtils::SetNodeTextContent(this, aValue, true); |
164 | 0 | } |
165 | | |
166 | | // variation of this code in nsSVGScriptElement - check if changes |
167 | | // need to be transfered when modifying |
168 | | |
169 | | bool |
170 | | HTMLScriptElement::GetScriptType(nsAString& aType) |
171 | 0 | { |
172 | 0 | nsAutoString type; |
173 | 0 | if (!GetAttr(kNameSpaceID_None, nsGkAtoms::type, type)) { |
174 | 0 | return false; |
175 | 0 | } |
176 | 0 | |
177 | 0 | // ASCII whitespace https://infra.spec.whatwg.org/#ascii-whitespace: |
178 | 0 | // U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE. |
179 | 0 | static const char kASCIIWhitespace[] = "\t\n\f\r "; |
180 | 0 | type.Trim(kASCIIWhitespace); |
181 | 0 |
|
182 | 0 | aType.Assign(type); |
183 | 0 | return true; |
184 | 0 | } |
185 | | |
186 | | void |
187 | | HTMLScriptElement::GetScriptText(nsAString& text) |
188 | 0 | { |
189 | 0 | GetText(text, IgnoreErrors()); |
190 | 0 | } |
191 | | |
192 | | void |
193 | | HTMLScriptElement::GetScriptCharset(nsAString& charset) |
194 | 0 | { |
195 | 0 | GetCharset(charset); |
196 | 0 | } |
197 | | |
198 | | void |
199 | | HTMLScriptElement::FreezeExecutionAttrs(nsIDocument* aOwnerDoc) |
200 | 0 | { |
201 | 0 | if (mFrozen) { |
202 | 0 | return; |
203 | 0 | } |
204 | 0 | |
205 | 0 | MOZ_ASSERT(!mIsModule && !mAsync && !mDefer && !mExternal); |
206 | 0 |
|
207 | 0 | // Determine whether this is a classic script or a module script. |
208 | 0 | nsAutoString type; |
209 | 0 | GetScriptType(type); |
210 | 0 | mIsModule = aOwnerDoc->ModuleScriptsEnabled() && |
211 | 0 | !type.IsEmpty() && type.LowerCaseEqualsASCII("module"); |
212 | 0 |
|
213 | 0 | // variation of this code in nsSVGScriptElement - check if changes |
214 | 0 | // need to be transfered when modifying. Note that we don't use GetSrc here |
215 | 0 | // because it will return the base URL when the attr value is "". |
216 | 0 | nsAutoString src; |
217 | 0 | if (GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) { |
218 | 0 | // Empty src should be treated as invalid URL. |
219 | 0 | if (!src.IsEmpty()) { |
220 | 0 | nsCOMPtr<nsIURI> baseURI = GetBaseURI(); |
221 | 0 | nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(mUri), |
222 | 0 | src, OwnerDoc(), baseURI); |
223 | 0 |
|
224 | 0 | if (!mUri) { |
225 | 0 | const char16_t* params[] = { u"src", src.get() }; |
226 | 0 |
|
227 | 0 | nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, |
228 | 0 | NS_LITERAL_CSTRING("HTML"), OwnerDoc(), |
229 | 0 | nsContentUtils::eDOM_PROPERTIES, "ScriptSourceInvalidUri", |
230 | 0 | params, ArrayLength(params), nullptr, |
231 | 0 | EmptyString(), GetScriptLineNumber(), GetScriptColumnNumber()); |
232 | 0 | } |
233 | 0 | } else { |
234 | 0 | const char16_t* params[] = { u"src" }; |
235 | 0 |
|
236 | 0 | nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, |
237 | 0 | NS_LITERAL_CSTRING("HTML"), OwnerDoc(), |
238 | 0 | nsContentUtils::eDOM_PROPERTIES, "ScriptSourceEmpty", |
239 | 0 | params, ArrayLength(params), nullptr, |
240 | 0 | EmptyString(), GetScriptLineNumber(), GetScriptColumnNumber()); |
241 | 0 | } |
242 | 0 |
|
243 | 0 | // At this point mUri will be null for invalid URLs. |
244 | 0 | mExternal = true; |
245 | 0 | } |
246 | 0 |
|
247 | 0 | bool async = (mExternal || mIsModule) && Async(); |
248 | 0 | bool defer = mExternal && Defer(); |
249 | 0 |
|
250 | 0 | mDefer = !async && defer; |
251 | 0 | mAsync = async; |
252 | 0 |
|
253 | 0 | mFrozen = true; |
254 | 0 | } |
255 | | |
256 | | CORSMode |
257 | | HTMLScriptElement::GetCORSMode() const |
258 | 0 | { |
259 | 0 | return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin)); |
260 | 0 | } |
261 | | |
262 | | bool |
263 | | HTMLScriptElement::HasScriptContent() |
264 | 0 | { |
265 | 0 | return (mFrozen ? mExternal : HasAttr(kNameSpaceID_None, nsGkAtoms::src)) || |
266 | 0 | nsContentUtils::HasNonEmptyTextContent(this); |
267 | 0 | } |
268 | | |
269 | | } // namespace dom |
270 | | } // namespace mozilla |