Closed Bug 1620505 Opened 6 years ago Closed 4 years ago

document.currentScript returns null in microtasks

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

RESOLVED FIXED
90 Branch
Tracking Status
firefox90 --- fixed

People

(Reporter: justice360, Assigned: saschanaz)

Details

(Keywords: dev-doc-needed)

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36

Steps to reproduce:

Run the following JavaScript in the root scope of a script element:

console.log(document.currentScript)

Promise.resolve().then(() => {
console.log(document.currentScript)
})

Actual results:

Outputs:

<script ...>
null

Expected results:

In Chrome/Safari/...

<script ...>
<script ...>

Per HTML spec:

(document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.

I'd say it's a little ambiguous about whether for micro tasks it should return null. Plus this section is labelled as "non-normative". But it's indeed unexpected that browsers handle this differently.

Sorry for the lousy formatting, I didn't expect Markdown works here. Here's the formatted version:

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36

Steps to reproduce:

Run the following JavaScript in the root scope of a script element:

console.log(document.currentScript)

Promise.resolve().then(() => {
console.log(document.currentScript)
})

Actual results:

Outputs:

<script ...>
null

Expected results:

In Chrome/Safari/... it outputs:

<script ...>
<script ...>

Per HTML spec:

(document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.

I'd say it's a little ambiguous about whether for micro tasks it should return null. Plus this section is labelled as "non-normative". But it's indeed unexpected that browsers handle this differently.

Hi,
Thanks for your contribution!
I don't have the technical knowledge to confirm this issue but I'll add product and component so the team can make some research on it. Hopefully, someone with a more deep understanding can help.

Regards,
Jerónimo.

Component: Untriaged → JavaScript Engine
Flags: needinfo?(sdetar)
Product: Firefox → Core

Oh, interesting situation. Thanks for filing this.

I think our behavior makes the most sense here, but I will bounce it over to the DOM team just in case anyone there knows why this is left non-normative in the HTML standard.

Component: JavaScript Engine → DOM: Core & HTML
Flags: needinfo?(sdetar)

That's not where the normative definition is.

  1. We start at https://html.spec.whatwg.org/#execute-the-script-block.
  2. Step 5 takes care of executing a classic script (those without type=module) and setting currentScript accordingly.
  3. Between setting currentScript and unsetting it, it invokes https://html.spec.whatwg.org/#run-a-classic-script.
  4. Close to the end, that invokes https://html.spec.whatwg.org/#clean-up-after-running-script.
  5. As its third and final step, that runs microtasks if the JS stack is empty, which it is per OP's example.

Ergo, Firefox has a bug.

Status: UNCONFIRMED → NEW
Ever confirmed: true
Flags: needinfo?(annevk)
Priority: -- → P3
Summary: document.currentScript returns null in micro tasks → document.currentScript returns null in microtasks
Version: 73 Branch → Trunk

Thanks for the explanation Anne :)

Hi,
Just curious to know about the status of this bug.

I too would like to know where you are at with this bug!

Assignee: nobody → krosylight
Pushed by krosylight@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/014cd94a623c Clear currentScript after running microtasks r=edgar
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/28868 for changes under testing/web-platform/tests
Keywords: dev-doc-needed
Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → 90 Branch
Upstream PR merged by moz-wptsync-bot
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: