Skip to content
Permalink
Browse files

Make event handlers share a code path with addEventListener()

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 committed Mar 14, 2018
1 parent be2f5df commit 9a2a66cd8f8ddfa4c35d784bcda5c080e82aad38
Showing with 37 additions and 17 deletions.
  1. +37 −17 source
54 source
<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>

<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
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),

0 comments on commit 9a2a66c

Please sign in to comment.
You can’t perform that action at this time.