Skip to content

Commit

Permalink
Don't update document.currentScript for module scripts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
domenic authored and annevk committed Apr 15, 2016
1 parent e0863d9 commit 6918282
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,7 @@ a.setAttribute('href', 'http://example.com/'); // change the content attribute d
<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>
<li><dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#dom-node-textcontent"><code>textContent</code></dfn> attribute</li>

<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree">tree</dfn> concept</li>
<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>
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-order">tree order</dfn> concept</li>
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-root">root</dfn> concept</li>
<li>The <dfn data-noexport="" data-x-href="https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor">inclusive ancestor</dfn> concept</li>
Expand Down Expand Up @@ -8536,7 +8536,7 @@ partial /*sealed*/ interface <dfn>Document</dfn> {
[SameObject] readonly attribute <span>HTMLCollection</span> <span data-x="dom-document-forms">forms</span>;
[SameObject] readonly attribute <span>HTMLCollection</span> <span data-x="dom-document-scripts">scripts</span>;
NodeList <span data-x="dom-document-getElementsByName">getElementsByName</span>(DOMString elementName);
readonly attribute <span>HTMLOrSVGScriptElement</span>? <span data-x="dom-document-currentScript">currentScript</span>;
readonly attribute <span>HTMLOrSVGScriptElement</span>? <span data-x="dom-document-currentScript">currentScript</span>; // classic scripts in a document only

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

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

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

</dd>

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

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

</div>

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

<div w-nodev>

<hr>
Expand Down Expand Up @@ -59012,7 +59021,8 @@ o............A....e

<li>

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

Expand All @@ -59035,22 +59045,7 @@ o............A....e
<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
initialised.</p>

</li>

<li>

<p>Initialise the <code>script</code> element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> object to the <code>script</code>
element.</p>

</li>

<li>

<p>Let <var>settings object</var> be the <span>environment settings object</span> of the
<code>script</code> element's <span>node document</span>'s <code>Window</code> object.</p>
set.</p>

</li>

Expand All @@ -59061,23 +59056,34 @@ o............A....e
<dl class="switch">
<dt>"<code data-x="">classic</code>"</dt>
<dd>
<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>
<ol>
<li><p>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.</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>
</ol>
</dd>

<dt>"<code data-x="">module</code>"</dt>
<dd>
<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>
<ol>
<li><p>Set the <code>script</code> element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> attribute to 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>
</ol>
</dd>
</dl>

</li>

<li>

<p>Initialise the <code>script</code> element's <span>node document</span>'s <code
data-x="dom-document-currentScript">currentScript</code> object to <var>old script
<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>
Expand All @@ -59090,7 +59096,8 @@ o............A....e

<li>

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

Expand Down

0 comments on commit 6918282

Please sign in to comment.