2 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsIScriptElement.h"
10 #include "js/loader/ScriptKind.h"
11 #include "mozilla/dom/Document.h"
12 #include "mozilla/dom/ReferrerPolicyBinding.h"
13 #include "nsIParser.h"
14 #include "nsIWeakReference.h"
16 using JS::loader::ScriptKind
;
18 bool nsIScriptElement::IsClassicNonAsyncDefer() {
19 return mKind
== ScriptKind::eClassic
&& !mAsync
&& !mDefer
;
22 void nsIScriptElement::SetCreatorParser(nsIParser
* aParser
) {
23 mCreatorParser
= do_GetWeakReference(aParser
);
26 void nsIScriptElement::UnblockParser() {
27 if (!IsClassicNonAsyncDefer()) {
28 MOZ_ASSERT_UNREACHABLE(
29 "Tried to unblock parser for a script type that cannot block "
33 nsCOMPtr
<nsIParser
> parser
= do_QueryReferent(mCreatorParser
);
35 parser
->UnblockParser();
39 void nsIScriptElement::ContinueParserAsync() {
40 if (!IsClassicNonAsyncDefer()) {
41 MOZ_ASSERT_UNREACHABLE(
42 "Tried to continue after a script type that cannot block the parser.");
45 nsCOMPtr
<nsIParser
> parser
= do_QueryReferent(mCreatorParser
);
47 parser
->ContinueInterruptedParsingAsync();
51 void nsIScriptElement::BeginEvaluating() {
52 if (!IsClassicNonAsyncDefer()) {
55 nsCOMPtr
<nsIParser
> parser
= do_QueryReferent(mCreatorParser
);
57 parser
->IncrementScriptNestingLevel();
61 void nsIScriptElement::EndEvaluating() {
62 if (!IsClassicNonAsyncDefer()) {
65 nsCOMPtr
<nsIParser
> parser
= do_QueryReferent(mCreatorParser
);
67 parser
->DecrementScriptNestingLevel();
71 already_AddRefed
<nsIParser
> nsIScriptElement::GetCreatorParser() {
72 nsCOMPtr
<nsIParser
> parser
= do_QueryReferent(mCreatorParser
);
73 return parser
.forget();
76 mozilla::dom::ReferrerPolicy
nsIScriptElement::GetReferrerPolicy() {
77 return mozilla::dom::ReferrerPolicy::_empty
;
80 void nsIScriptElement::DetermineKindFromType(
81 const mozilla::dom::Document
* aOwnerDoc
) {
82 MOZ_ASSERT((mKind
!= ScriptKind::eModule
) &&
83 (mKind
!= ScriptKind::eImportMap
) && !mAsync
&& !mDefer
&&
89 if (!type
.IsEmpty()) {
90 if (type
.LowerCaseEqualsASCII("module")) {
91 mKind
= ScriptKind::eModule
;
94 // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
95 // Step 11. Otherwise, if the script block's type string is an ASCII
96 // case-insensitive match for the string "importmap", then set el's type to
98 if (type
.LowerCaseEqualsASCII("importmap")) {
99 mKind
= ScriptKind::eImportMap
;