Skip to content

Commit 6918282

Browse files
domenicannevk
authored andcommitted
Don't update document.currentScript for module scripts
Make module scripts modify less global script state. They set document.currentScript to null, and no longer fire beforescriptexecute and afterscriptexecute events. Closes #997. See #1013 for a future alternative to document.currentScript, and #943 for more discussion of the events.
1 parent e0863d9 commit 6918282

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

source

+39-32
Original file line numberDiff line numberDiff line change
@@ -3113,7 +3113,7 @@ a.setAttribute('href', 'http://example.com/'); // change the content attribute d
31133113
<li><dfn data-noexport="" data-x="dom-Element-id" data-x-href="https://dom.spec.whatwg.org/#dom-element-id"><code>id</code></dfn> attribute</li>
31143114
<li><dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#dom-node-textcontent"><code>textContent</code></dfn> attribute</li>
31153115

3116-
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree">tree</dfn> concept</li>
3116+
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree">tree</dfn> and <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-shadow-tree">shadow tree</dfn> concepts</li>
31173117
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-order">tree order</dfn> concept</li>
31183118
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-root">root</dfn> concept</li>
31193119
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor">inclusive ancestor</dfn> concept</li>
@@ -8536,7 +8536,7 @@ partial /*sealed*/ interface <dfn>Document</dfn> {
85368536
[SameObject] readonly attribute <span>HTMLCollection</span> <span data-x="dom-document-forms">forms</span>;
85378537
[SameObject] readonly attribute <span>HTMLCollection</span> <span data-x="dom-document-scripts">scripts</span>;
85388538
NodeList <span data-x="dom-document-getElementsByName">getElementsByName</span>(DOMString elementName);
8539-
readonly attribute <span>HTMLOrSVGScriptElement</span>? <span data-x="dom-document-currentScript">currentScript</span>;
8539+
readonly attribute <span>HTMLOrSVGScriptElement</span>? <span data-x="dom-document-currentScript">currentScript</span>; // classic scripts in a document only
85408540

85418541
// <span>dynamic markup insertion</span>
85428542
<span>Document</span> <span data-x="dom-document-open">open</span>(optional DOMString type = "text/html", optional DOMString replace = "");
@@ -9053,12 +9053,14 @@ partial /*sealed*/ interface <dfn>Document</dfn> {
90539053
<dd>
90549054

90559055
<p>Returns the <code>script</code> element, or the <span>SVG <code>script</code></span> element,
9056-
that is currently executing. In the case of reentrant script execution, returns the one that
9057-
most recently started executing amongst those that have not yet finished executing.</p>
9056+
that is currently executing, as long as the element represents a <span>classic script</span>.
9057+
In the case of reentrant script execution, returns the one that most recently started executing
9058+
amongst those that have not yet finished executing.</p>
90589059

90599060
<p>Returns null if the <code>Document</code> is not currently executing a <code>script</code>
90609061
or <span>SVG <code>script</code></span> element (e.g., because the running script is an event
9061-
handler, or a timeout).</p>
9062+
handler, or a timeout), or if the currently executing <code>script</code> or <span>SVG
9063+
<code>script</code></span> element represents a <span>module script</span>.</p>
90629064

90639065
</dd>
90649066

@@ -9067,12 +9069,19 @@ partial /*sealed*/ interface <dfn>Document</dfn> {
90679069
<div w-nodev>
90689070

90699071
<p>The <dfn><code data-x="dom-document-currentScript">currentScript</code></dfn> attribute, on
9070-
getting, must return the value to which it was most recently initialised. When the
9071-
<code>Document</code> is created, the <code
9072-
data-x="dom-document-currentScript">currentScript</code> must be initialised to null.</p>
9072+
getting, must return the value to which it was most recently set. When the <code>Document</code>
9073+
is created, the <code data-x="dom-document-currentScript">currentScript</code> must be
9074+
initialised to null.</p>
90739075

90749076
</div>
90759077

9078+
<p class="note">This API has fallen out of favor in the implementor and standards community, as
9079+
it globally exposes <code>script</code> or <span>SVG <code>script</code></span> elements. As such,
9080+
it is not available in newer contexts, such as when running <span data-x="module script">module
9081+
scripts</span> or when running scripts in a <span>shadow tree</span>. We are looking into creating
9082+
a new solution for identifying the running script in such contexts, which does not make it
9083+
globally available: see issue <a href="https://github.com/whatwg/html/issues/1013">#1013</a>.</p>
9084+
90769085
<div w-nodev>
90779086

90789087
<hr>
@@ -59012,7 +59021,8 @@ o............A....e
5901259021

5901359022
<li>
5901459023

59015-
<p><span>Fire a simple event</span> named <code
59024+
<p>If <span data-x="concept-script-type">the script's type</span> is "<code
59025+
data-x="">classic</code>", <span>fire a simple event</span> named <code
5901659026
data-x="event-beforescriptexecute">beforescriptexecute</code> that bubbles and is cancelable
5901759027
at the <code>script</code> element.</p>
5901859028

@@ -59035,22 +59045,7 @@ o............A....e
5903559045
<p>Let <var>old script element</var> be the value to which the <code>script</code>
5903659046
element's <span>node document</span>'s <code
5903759047
data-x="dom-document-currentScript">currentScript</code> object was most recently
59038-
initialised.</p>
59039-
59040-
</li>
59041-
59042-
<li>
59043-
59044-
<p>Initialise the <code>script</code> element's <span>node document</span>'s <code
59045-
data-x="dom-document-currentScript">currentScript</code> object to the <code>script</code>
59046-
element.</p>
59047-
59048-
</li>
59049-
59050-
<li>
59051-
59052-
<p>Let <var>settings object</var> be the <span>environment settings object</span> of the
59053-
<code>script</code> element's <span>node document</span>'s <code>Window</code> object.</p>
59048+
set.</p>
5905459049

5905559050
</li>
5905659051

@@ -59061,23 +59056,34 @@ o............A....e
5906159056
<dl class="switch">
5906259057
<dt>"<code data-x="">classic</code>"</dt>
5906359058
<dd>
59064-
<p><span data-x="run a classic script">Run the classic script</span> given by <span
59065-
data-x="concept-script-script">the script's script</span>.</p>
59059+
<ol>
59060+
<li><p>Set the <code>script</code> element's <span>node document</span>'s <code
59061+
data-x="dom-document-currentScript">currentScript</code> attribute to the
59062+
<code>script</code> element.</p></li>
59063+
59064+
<li><p><span data-x="run a classic script">Run the classic script</span> given by <span
59065+
data-x="concept-script-script">the script's script</span>.</p></li>
59066+
</ol>
5906659067
</dd>
5906759068

5906859069
<dt>"<code data-x="">module</code>"</dt>
5906959070
<dd>
59070-
<p><span data-x="run a module script">Run the module script</span> given by <span
59071-
data-x="concept-script-script">the script's script</span>.</p>
59071+
<ol>
59072+
<li><p>Set the <code>script</code> element's <span>node document</span>'s <code
59073+
data-x="dom-document-currentScript">currentScript</code> attribute to null.</p></li>
59074+
59075+
<li><p><span data-x="run a module script">Run the module script</span> given by <span
59076+
data-x="concept-script-script">the script's script</span>.</p></li>
59077+
</ol>
5907259078
</dd>
5907359079
</dl>
5907459080

5907559081
</li>
5907659082

5907759083
<li>
5907859084

59079-
<p>Initialise the <code>script</code> element's <span>node document</span>'s <code
59080-
data-x="dom-document-currentScript">currentScript</code> object to <var>old script
59085+
<p>Set the <code>script</code> element's <span>node document</span>'s <code
59086+
data-x="dom-document-currentScript">currentScript</code> attribute to <var>old script
5908159087
element</var>.</p>
5908259088

5908359089
</li>
@@ -59090,7 +59096,8 @@ o............A....e
5909059096

5909159097
<li>
5909259098

59093-
<p><span>Fire a simple event</span> named <code
59099+
<p>If <span data-x="concept-script-type">the script's type</span> is "<code
59100+
data-x="">classic</code>", <span>fire a simple event</span> named <code
5909459101
data-x="event-afterscriptexecute">afterscriptexecute</code> that bubbles (but is not
5909559102
cancelable) at the <code>script</code> element.</p>
5909659103

0 commit comments

Comments
 (0)