Skip to content

Commit

Permalink
Cleanups to "execute a script block"
Browse files Browse the repository at this point in the history
One normative fix: by saving the script element's node document before
evaluation, this properly changes that document's currentScript back
after evaluation. As previously written, a script that moved during
evaluation would cause its new node document's currentScript to update.

Tests: web-platform-tests/wpt#20775

Editorial cleanups:

* Use a named argument, scriptElement
* Use the saved document variable throughout
* Cleanup source formatting
* Remove <!-- SCRIPT EXEC --> comments
  • Loading branch information
domenic committed Jan 9, 2020
1 parent 060338f commit 938bf7f
Showing 1 changed file with 40 additions and 69 deletions.
109 changes: 40 additions & 69 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -59605,105 +59605,80 @@ o............A....e
loaded, because there's just a binary "are style sheets blocking scripts" state, things aren't
defined in terms of which style sheets are blocking which scripts -->

<p>When the user agent is required to <dfn data-x="execute the script block">execute a script
block</dfn>, it must run the following steps.</p>
<p>To <dfn data-x="execute the script block">execute a script block</dfn> given a
<code>script</code> element <var>scriptElement</var>:</p>

<ol>
<li><p>Let <var>document</var> be <var>scriptElement</var>'s <span>node document</span>.</p></li>

<li>

<p>If the element is flagged as <span>"parser-inserted"</span>, but the element's
<span>node document</span> is not the <code>Document</code> of the parser that created the element,
then return.</p>

</li>

<li>

<p>If <span data-x="concept-script-script">the script's script</span> is null, <span
data-x="concept-event-fire">fire an event</span> named <code data-x="event-error">error</code>
at the element, and return.</p>

</li>

<!-- SCRIPT EXEC -->

<li>
<li><p>If <var>scriptElement</var> is flagged as <span>"parser-inserted"</span>, but
<var>document</var> is not the <code>Document</code> of the parser that created the element, then
return.</p></li>

<p>If the script is <span data-x="concept-script-external">from an external file</span>, or
<span data-x="concept-script-type">the script's type</span> is "<code data-x="">module</code>",
then increment the <span>ignore-destructive-writes counter</span> of the <code>script</code>
element's <span>node document</span>. Let <var>neutralized doc</var> be that
<code>Document</code>.</p>
<li><p>If <span data-x="concept-script-script">the script's script</span> is null for
<var>scriptElement</var>, then <span data-x="concept-event-fire">fire an event</span> named <code
data-x="event-error">error</code> at <var>scriptElement</var>, and return.</p></li>

</li>
<li><p>If <var>scriptElement</var> is <span data-x="concept-script-external">from an external
file</span>, or <span data-x="concept-script-type">the script's type</span> for
<var>scriptElement</var> is "<code data-x="">module</code>", then increment <var>document</var>'s
<span>ignore-destructive-writes counter</span>.</p></li>

<li>

<p>Switch on <span data-x="concept-script-type">the script's type</span>:</p>
<p>Switch on <span data-x="concept-script-type">the script's type</span> for
<var>scriptElement</var>:</p>

<dl class="switch">
<dt>"<code data-x="">classic</code>"</dt>
<dd>
<ol>
<li>
<p>Let <var>old script element</var> be the value to which the <code>script</code>
element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> object was most recently
set.</p>
</li>
<li><p>Let <var>oldCurrentScript</var> be the value to which <var>document</var>'s <code
data-x="dom-document-currentScript">currentScript</code> object was most recently
set.</p></li>

<li>
<p>If the <code>script</code> element's <span>root</span> is <em>not</em> a <span>shadow
root</span>, then set the <code>script</code> element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute to the
<code>script</code> element. Otherwise, set it to null.</p>

<p class="note">This does not use the <span>in a document tree</span> check, as the
<code>script</code> element could have been removed from the document prior to execution,
and in that scenario <code data-x="dom-document-currentScript">currentScript</code> still
needs to point to it.</p>
<p>If <var>scriptElement</var>'s <span>root</span> is <em>not</em> a <span>shadow
root</span>, then set <var>document</var>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute to
<var>scriptElement</var>. Otherwise, set it to null.</p>

<p class="note">This does not use the <span>in a document tree</span> check, as
<var>scriptElement</var> could have been removed from the document prior to execution, and
in that scenario <code data-x="dom-document-currentScript">currentScript</code> still needs
to point to it.</p>
</li>

<li><p><span data-x="run a classic script">Run the classic script</span> given by <span
data-x="concept-script-script">the script's script</span>.</p></li>
data-x="concept-script-script">the script's script</span> for
<var>scriptElement</var>.</p></li>

<li>
<p>Set the <code>script</code> element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute to <var>old script
element</var>.</p>
</li>
<li><p>Set <var>document</var>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute to
<var>oldCurrentScript</var>.</p></li>
</ol>
</dd>

<dt>"<code data-x="">module</code>"</dt>
<dd>
<ol>
<li><p>Assert: The <code>script</code> element's <span>node document</span>'s <code
<li><p>Assert: <var>document</var>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute is null.</p></li>

<li><p><span data-x="run a module script">Run the module script</span> given by <span
data-x="concept-script-script">the script's script</span>.</p></li>
data-x="concept-script-script">the script's script</span> for
<var>scriptElement</var>.</p></li>
</ol>
</dd>
</dl>

</li>

<li>

<p>Decrement the <span>ignore-destructive-writes counter</span> of <var>neutralized doc</var>, if it was incremented in the earlier step.</p>

</li>

<li>

<p>If the script is <span data-x="concept-script-external">from an external file</span>, then
<span data-x="concept-event-fire">fire an event</span> named <code
data-x="event-load">load</code> at the <code>script</code> element.</p>

</li>
<li><p>Decrement the <span>ignore-destructive-writes counter</span> of <var>document</var>, if it
was incremented in the earlier step.</p></li>

<li><p>If <var>scriptElement</var> is <span data-x="concept-script-external">from an external
file</span>, then <span data-x="concept-event-fire">fire an event</span> named <code
data-x="event-load">load</code> at <var>scriptElement</var>.</p></li>
</ol>

</div>
Expand Down Expand Up @@ -88938,7 +88913,6 @@ interface <dfn>ApplicationCache</dfn> : <span>EventTarget</span> {

<div w-nodev>

<!-- SCRIPT EXEC (marks areas related to creation of scripts) -->
<h4 id="scripting-processing-model"><span id="processing-model-7"></span>Processing model</h4>

<h5>Definitions</h5>
Expand Down Expand Up @@ -93794,7 +93768,6 @@ typedef <span>OnBeforeUnloadEventHandlerNonNull</span>? <dfn>OnBeforeUnloadEvent
</div>
</li>

<!-- SCRIPT EXEC, sorta -->
<li><p>Set <var>eventHandler</var>'s <span data-x="event handler value">value</span> to the
result of creating a Web IDL <code>EventHandler</code> callback function object whose object
reference is <var>function</var> and whose <span>callback context</span> is <var>settings
Expand Down Expand Up @@ -101275,8 +101248,6 @@ interface <dfn>SharedWorkerGlobalScope</dfn> : <span>WorkerGlobalScope</span> {
<li><p>Set <var>inside settings</var>'s <span
data-x="concept-environment-execution-ready-flag">execution ready flag</span>.</p></li>

<!-- SCRIPT EXEC -->

<li>
<p>If <var>script</var> is a <span>classic script</span>, then <span data-x="run a classic
script">run the classic script</span> <var>script</var>. Otherwise, it is a <span>module
Expand Down

0 comments on commit 938bf7f

Please sign in to comment.