<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>28920</bug_id>
          
          <creation_ts>2015-07-06 21:34:37 +0000</creation_ts>
          <short_desc>If the stack of script settings objects is empty, perform a microtask checkpoint right before executing async or defer script</short_desc>
          <delta_ts>2017-07-21 09:35:23 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>HTML</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WORKSFORME</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>Unsorted</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Zibi Braniecki">zbraniecki</reporter>
          <assigned_to name="Ian &apos;Hixie&apos; Hickson">ian</assigned_to>
          <cc>annevk</cc>
    
    <cc>bugs</cc>
    
    <cc>d</cc>
    
    <cc>ehsan</cc>
    
    <cc>mike</cc>
    
    <cc>Ms2ger</cc>
          
          <qa_contact>contributor</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>121834</commentid>
    <comment_count>0</comment_count>
    <who name="Zibi Braniecki">zbraniecki</who>
    <bug_when>2015-07-06 21:34:37 +0000</bug_when>
    <thetext>While regular &lt;script&gt;&apos;s are executed in a separate task which guarantees that MutationObserver callbacks are executed before them, current spec does not specify any special case for async or deferred scripts.

When working on proof of concept shims for future WebAPI&apos;s, it is useful to be able to inject synchronous script that sets MutationObserver whose callback alters Elements injected into DOM.

Without this change, there&apos;s no transparent way to inject such shims, since scripts have to additionally be wrapped in DOMContentLoaded callback because only that guarantees that the MO callbacks are executed.

Currently in Safari and Chrome MutationObserver callbacks are called before executing defer scritps, while in Gecko the behavior is not deterministic.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123274</commentid>
    <comment_count>1</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2015-09-22 15:03:39 +0000</bug_when>
    <thetext>&lt;script async&gt; is executed from a networking task per https://html.spec.whatwg.org/#set-of-scripts-that-will-execute-as-soon-as-possible.

&lt;script defer&gt; is run from the task that is queued from spinning the event loop, as far as can tell.

So I&apos;m not sure what the problem is here.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123278</commentid>
    <comment_count>2</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2015-09-22 18:23:49 +0000</bug_when>
    <thetext>Where is the task for defer queued?

The related gecko bug is https://bugzilla.mozilla.org/show_bug.cgi?id=1180927
which is only about defer, don&apos;t recall if async has any issues.


&quot;Once the user agent stops parsing the document, the user agent must run the following steps:
1.0 ...
2.0 Pop all the nodes off the stack of open elements. 
3.0 If the list of scripts that will execute when the document has finished parsing is not empty, run these substeps:
  3.1. Spin the event loop until the first script in the list of scripts that will execute when the document has finished parsing has its &quot;ready to be parser-executed&quot; flag set and the parser&apos;s Document has no style sheet that is blocking scripts. (so we may or may not spin event loop)
  3.2. Execute the first script in the list of scripts that will execute when the document has finished parsing. (So microtask end point doesn&apos;t happen between defer scripts)
  3.3. ...
  3.4. ... (loop [3.0])


Without async/defer, we do explicitly &quot;If the stack of script settings objects is empty, perform a microtask checkpoint.&quot;, but defer scripts are executed later.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123286</commentid>
    <comment_count>3</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2015-09-23 06:39:52 +0000</bug_when>
    <thetext>The first clause of step 15 of https://html.spec.whatwg.org/#prepare-a-script is about &lt;script defer&gt;. That introduces the term &quot;list of scripts that will execute when the document has finished parsing&quot;. That term is referenced from https://html.spec.whatwg.org/#stop-parsing as you concluded as well.

Step 3.1 there invokes &quot;spin the event loop&quot;. Step 9 of https://html.spec.whatwg.org/#spin-the-event-loop queues a task. That task is used to to run step 3.2 of the algorithm that invoked &quot;spin the event loop&quot;, which ends up executing the script.

Now, per the event loop processing model, perform a microtask checkpoint is run in step 7, after running a task. So I think that all adds up.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123288</commentid>
    <comment_count>4</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2015-09-23 09:51:02 +0000</bug_when>
    <thetext>3.1 spins event loop _only_ if &quot;ready to be parser-executed&quot; isn&apos;t set, or that is at least the way I interpret it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123289</commentid>
    <comment_count>5</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2015-09-23 09:52:35 +0000</bug_when>
    <thetext>(In reply to Olli Pettay from comment #4)
&gt; 3.1 spins event loop _only_ if &quot;ready to be parser-executed&quot; isn&apos;t set, or
&gt; that is at least the way I interpret it.

In fact, as far as I see, that is the only way to interpret it, since there might not be anything in the event loop waiting to be processed, if deferred scripts have been loaded already.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>123290</commentid>
    <comment_count>6</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2015-09-23 09:56:33 +0000</bug_when>
    <thetext>I don&apos;t think so, &quot;spin the event loop&quot; takes a goal as argument. So even if the goal is already met, you&apos;d still have to run the steps.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>128731</commentid>
    <comment_count>7</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2017-07-21 09:35:23 +0000</bug_when>
    <thetext>If this is still unclear for some reason, I suggest raising this again at https://github.com/whatwg/html/issues/new. Thanks!</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>