From 825aa106dd3bb1c52b3a813c89e5c429b5f580d3 Mon Sep 17 00:00:00 2001 From: Adam Vandolder Date: Wed, 5 Oct 2022 15:16:28 +0000 Subject: [PATCH] Bug 1784187 - Process scripts even if they contain no text content. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D156086 --- dom/base/test/mochitest.ini | 1 + dom/base/test/test_bug1784187.html | 38 ++++++++++++++++++++++++++++++ dom/script/ScriptElement.cpp | 15 +++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 dom/base/test/test_bug1784187.html diff --git a/dom/base/test/mochitest.ini b/dom/base/test/mochitest.ini index 161d3a8533cf..57b9a123ad09 100644 --- a/dom/base/test/mochitest.ini +++ b/dom/base/test/mochitest.ini @@ -269,6 +269,7 @@ skip-if = (os == "android" || headless) # See [test_bug1739957.html] support-files = bug1739957.sjs +[test_bug1784187.html] [test_bug5141.html] [test_bug28293.html] [test_bug28293.xhtml] diff --git a/dom/base/test/test_bug1784187.html b/dom/base/test/test_bug1784187.html new file mode 100644 index 000000000000..6f9f782e3e39 --- /dev/null +++ b/dom/base/test/test_bug1784187.html @@ -0,0 +1,38 @@ + + + + + Test for Bug 1784187 + + + + + + +Mozilla Bug 1784187 +

+ + + + diff --git a/dom/script/ScriptElement.cpp b/dom/script/ScriptElement.cpp index 99790ca3e515..bebd9aae307f 100644 --- a/dom/script/ScriptElement.cpp +++ b/dom/script/ScriptElement.cpp @@ -7,10 +7,12 @@ #include "ScriptElement.h" #include "ScriptLoader.h" #include "mozilla/BasicEvents.h" +#include "mozilla/CycleCollectedJSContext.h" #include "mozilla/EventDispatcher.h" #include "mozilla/dom/Document.h" #include "mozilla/dom/Element.h" #include "nsContentUtils.h" +#include "nsThreadUtils.h" #include "nsPresContext.h" #include "nsIParser.h" #include "nsGkAtoms.h" @@ -93,7 +95,18 @@ bool ScriptElement::MaybeProcessScript() { "You forgot to add self as observer"); if (mAlreadyStarted || !mDoneAddingChildren || !cont->GetComposedDoc() || - mMalformed || !HasScriptContent()) { + mMalformed) { + return false; + } + + if (!HasScriptContent()) { + // In the case of an empty, non-external classic script, there is nothing + // to process. However, we must perform a microtask checkpoint afterwards, + // as per https://html.spec.whatwg.org/#clean-up-after-running-script + if (mKind == JS::loader::ScriptKind::eClassic && !mExternal) { + nsContentUtils::AddScriptRunner(NS_NewRunnableFunction( + "ScriptElement::MaybeProcessScript", []() { nsAutoMicroTask mt; })); + } return false; }