Skip to content

Commit

Permalink
Make event handlers share a code path with addEventListener()
Browse files Browse the repository at this point in the history
This hook was provided whatwg/dom#596 and event handlers need to use it to fix whatwg/dom#365 and w3c/ServiceWorker#1004 properly. Tests will be added as part of resolving those issues.
  • Loading branch information
annevk authored and Alice Boxhall committed Jan 7, 2019
1 parent 7f89bfa commit 5113402
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -3183,10 +3183,13 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li><dfn data-x-href="https://dom.spec.whatwg.org/#composed-flag">composed flag</dfn></li>
<li><dfn data-x="dom-Event-isTrusted" data-x-href="https://dom.spec.whatwg.org/#dom-event-istrusted"><code>isTrusted</code></dfn> attribute</li>
<li><dfn data-x="dom-Event-initEvent" data-x-href="https://dom.spec.whatwg.org/#dom-event-initevent"><code>initEvent()</code></dfn> method</li>
<li><dfn data-x-href="https://dom.spec.whatwg.org/#add-an-event-listener">add an event listener</dfn></li>
<li><dfn data-x="dom-EventTarget-addEventListener" data-x-href="https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener"><code>addEventListener()</code></dfn> method</li>
<li><dfn data-x="dom-EventListener" data-x-href="https://dom.spec.whatwg.org/#callbackdef-eventlistener"><code>EventListener</code></dfn> callback interface</li>
<li>The <dfn data-x="concept-event-type" data-x-href="https://dom.spec.whatwg.org/#dom-event-type">type</dfn> of an event</li>
<li>The concept of an <dfn data-x="concept-event-listener" data-x-href="https://dom.spec.whatwg.org/#concept-event-listener">event listener</dfn> and the <span data-x="concept-event-listener">event listeners</span> associated with an <code>EventTarget</code></li>
<li>An <dfn data-x-href="https://dom.spec.whatwg.org/#concept-event-listener">event listener</dfn> and its
<dfn data-x="event listener type" data-x-href="https://dom.spec.whatwg.org/#event-listener-type">type</dfn> and
<dfn data-x="event listener callback" data-x-href="https://dom.spec.whatwg.org/#event-listener-callback">callback</dfn></li>

<li>The <dfn data-x="document's character encoding" data-x-href="https://dom.spec.whatwg.org/#concept-document-encoding">encoding</dfn> (herein the <i>character encoding</i>) and
<dfn data-x="concept-document-content-type" data-x-href="https://dom.spec.whatwg.org/#concept-document-content-type">content type</dfn> of a <code>Document</code></li>
Expand Down Expand Up @@ -89690,22 +89693,38 @@ dictionary <dfn>PromiseRejectionEventInit</dfn> : <span>EventInit</span> {

<hr>

<p>When an <span data-x="event handlers">event handler</span> <var>H</var> of an element or object
<var>T</var> implementing the <code>EventTarget</code> interface is first set to a non-null value,
the user agent must append an <span data-x="concept-event-listener">event listener</span> to the
list of <span data-x="concept-event-listener">event listeners</span> associated with <var>T</var>
with <b>type</b> set to the <dfn data-export="">event handler event type</dfn> corresponding to
<var>H</var> and <b>callback</b> set to the result of creating a Web IDL <code
data-x="dom-EventListener">EventListener</code> instance representing a reference to a function of
one argument that executes the steps of <span>the event handler processing algorithm</span>, given
<var>H</var> and its argument. The <code data-x="dom-EventListener">EventListener</code>'s
<span>callback context</span> can be arbitrary; it does not impact the steps of <span>the event
handler processing algorithm</span>. <ref spec=DOM></p>

<p class="note">The <b>callback</b> is emphatically <em>not</em> the <span data-x="event
handlers">event handler</span> itself. Every event handler ends up registering the same
<b>callback</b>, the algorithm defined below, which takes care of invoking the right callback, and
processing the callback's return value.</p>
<p>When an <span data-x="event handlers">event handler</span> <var>H</var> of an
<code>EventTarget</code> object <var>T</var> is first set to a non-null value, then:

<ol>
<li>
<p>Let <var>callback</var> be the result of creating a Web IDL <code
data-x="dom-EventListener">EventListener</code> instance representing a reference to a function
of one argument that executes the steps of <span>the event handler processing algorithm</span>,
given <var>H</var> and its argument.</p>

<p>The <code data-x="dom-EventListener">EventListener</code>'s <span>callback context</span> can
be arbitrary; it does not impact the steps of <span>the event handler processing
algorithm</span>. <ref spec=DOM></p>

<p class="note">The callback is emphatically <em>not</em> the <span data-x="event
handlers">event handler</span> itself. Every event handler ends up registering the same
callback, the algorithm defined below, which takes care of invoking the right callback, and
processing the callback's return value.</p>
</li>

<li>
<p>Let <var>listener</var> be a new <span>event listener</span> whose <span data-x="event
listener type">type</span> is the <dfn data-export="">event handler event type</dfn>
corresponding to <var>H</var> and <span data-x="event listener callback">callback</span> is
<var>callback</var>.</p>

<p class="note">To be clear, an <span>event listener</span> is different from an <code
data-x="dom-EventListener">EventListener</code>.</p>
</li>

<li><p><span>Add an event listener</span> with <var>T</var> and <var>listener</var>.</p></li>
</ol>

<p class="note">This only happens the first time the <span data-x="event handlers">event
handler</span>'s value is set. Since listeners are called in the order they were registered, the
Expand Down Expand Up @@ -120803,6 +120822,7 @@ INSERT INTERFACES HERE
Carlos Gabriel Cardona,
Carlos Perell&oacute; Mar&iacute;n,
Casey Leask,
Cătălin Badea,
C&#x103;t&#x103;lin Mari&#x219;,
Chao Cai,
&#xc724;&#xc11d;&#xcc2c; (Channy Yun),
Expand Down

0 comments on commit 5113402

Please sign in to comment.