Closed Bug 1784187 Opened 4 months ago Closed 2 months ago

Empty scripts should be executed too

Categories

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

Firefox 105
defect

Tracking

()

RESOLVED FIXED
107 Branch
Tracking Status
firefox107 --- fixed

People

(Reporter: tristan.fraipont, Assigned: avandolder)

Details

Attachments

(2 files)

Attached file test-case.html

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:103.0) Gecko/20100101 Firefox/103.0

Steps to reproduce:

See https://github.com/web-platform-tests/wpt/issues/35393.

Register a MutationObserver on the body looking for added nodes in the body's subtree, which will log the innerHTML of an element with id "parent".
Below in the markup, set this one:

<div id="parent">
  <script></script>
  <!--Further content-->
</div>

Actual results:

When the MutationObserver fires the whole "parent" element has been completely parsed and the innerHTML includes the comment "<!--Further content-->".

Expected results:

Basically, the innerHTML should have contained only "<script></script>".

Reasoning: the empty <script></script> should have triggered the "run a classic script" algorithm, which itself should have called the cleanup after running a script steps, which should have triggered a microtask-checkpoint, ultimately forcing the MutationObserver's pending records to be signaled and handled, with what the parser had until the end tag </script>.
If you want to go the full route, when the end tag </script> is met, we're in https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incdata:prepare-the-script-element. From there in "prepare the script element" source text is set to the empty string, and further down no special handling of the empty string is ever mentioned, so it should get executed normally, just like it happens when we had just a single white space character between the script's tags.

Ps: this is not a regression, I went back to M61 where it also did reproduce.

This seems to be a Gecko bug for MutationObserver.

Olli, are we still actively working on MutationObserver? Is this something that we should fix?

Severity: -- → S2
Flags: needinfo?(smaug)

Doesn't look like a MutationObserver issue, but rather an issue with script handling. We're returning early somewhere because of empty content.
I think https://searchfox.org/mozilla-central/rev/f3616b887b8627d8ad841bb1a11138ed658206c5/dom/script/ScriptElement.cpp#89,96
which calls https://searchfox.org/mozilla-central/rev/f3616b887b8627d8ad841bb1a11138ed658206c5/dom/html/HTMLScriptElement.cpp#243,245

Flags: needinfo?(smaug)

Adam, do you want to give it a try? :)

Flags: needinfo?(avandolder)

I will give it a shot!

Assignee: nobody → avandolder
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Flags: needinfo?(avandolder)
Pushed by avandolder@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/14a8a8ae0f7a
Process scripts even if they contain no text content. r=smaug
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 107 Branch
You need to log in before you can comment on or make changes to this bug.