Skip to content

Commit e728c11

Browse files
authored
Editorial: provide a shorthand for other specifications to create events
This can be useful when you can’t use fire, but need to initialize event attributes in a particular manner or keep the event around for some reason.
1 parent b3571d7 commit e728c11

File tree

2 files changed

+90
-34
lines changed

2 files changed

+90
-34
lines changed

dom.bs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -942,25 +942,54 @@ method must, when invoked, run these steps:
942942

943943
<h3 id=constructing-events>Constructing events</h3>
944944

945-
When a <dfn export for=Event id=concept-event-constructor>constructor</dfn> of the
946-
{{Event}} interface, or of an interface that inherits from the {{Event}} interface, is
947-
invoked, these steps must be run:
945+
<p>When a <dfn export for=Event id=concept-event-constructor>constructor</dfn> of the {{Event}}
946+
interface, or of an interface that inherits from the {{Event}} interface, is invoked, these steps
947+
must be run, given the arguments <var>type</var> and <var>eventInitDict</var>:
948948

949949
<ol>
950-
<li><p>Create an <a>event</a> that uses the interface the constructor was invoked upon.
950+
<li><p>Create a new object <var>event</var> using this interface.
951951

952-
<li><p>Set its <a>initialized flag</a>.
952+
<li><p>Set <var>event</var>'s <a>initialized flag</a>.
953953

954-
<li><p>Initialize the {{Event/type}} attribute to the <var>type</var> argument.
954+
<li><p>Initialize <var>event</var>'s {{Event/type}} attribute to <var>type</var>.
955955

956-
<li><p>If there is an <var>eventInitDict</var> argument, then for each <a>dictionary member</a>
957-
present, find the attribute on <a>event</a> whose <a spec=webidl>identifier</a> matches the key of
958-
the <a>dictionary member</a> and then set the attribute to the value of that
956+
<li><p>For each <a>dictionary member</a> present in <var>eventInitDict</var>, find the attribute on
957+
<var>event</var> whose <a spec=webidl>identifier</a> matches the key of the
958+
<a>dictionary member</a> and then set the attribute to the value of that <a>dictionary member</a>.
959+
960+
<li><p>Return <var>event</var>.
961+
</ol>
962+
963+
<p>To
964+
<dfn export id=concept-event-create lt="creating an event|create an event">create an event</dfn>
965+
using <var>eventInterface</var>, which must be either {{Event}} or an interface that inherits from
966+
it, run these steps:</p>
967+
968+
<ol>
969+
<li><p>Create a new object <var>event</var> using <var>eventInterface</var>.
970+
971+
<li><p>Set <var>event</var>'s <a>initialized flag</a>.
972+
973+
<li><p>Let <var>defaultEventInitDict</var> be the result of
974+
<a lt="converted to an IDL value">converting</a> the JavaScript value undefined to the dictionary
975+
type accepted by <var>eventInterface</var>'s constructor. (This dictionary type will either be
976+
{{EventInit}} or a dictionary that inherits from it.)
977+
978+
<li>For each <a>dictionary member</a> present in <var>defaultEventInitDict</var>, find the
979+
attribute on <var>event</var> whose <a spec=webidl>identifier</a> matches the key of the
980+
<a>dictionary member</a> and then set the attribute to the default value of that
959981
<a>dictionary member</a>.
960982

961-
<li><p>Return the <a>event</a>.
983+
<li><p>Initialize <var>event</var>'s {{Event/isTrusted}} attribute to true.
984+
985+
<li><p>Return <var>event</var>.
962986
</ol>
963987

988+
<p class=note><a>Create an event</a> is meant to be used by other specifications which need to
989+
separately <a lt="create an event">create</a> and <a>dispatch</a> events, instead of simply
990+
<a lt="fire an event">firing</a> them. It ensures the event's attributes are initialized to the
991+
correct defaults.</p>
992+
964993

965994
<h3 id=defining-event-interfaces>Defining event interfaces</h3>
966995

@@ -1447,10 +1476,10 @@ initialized, and a <var>legacy target override flag</var>, run these steps:
14471476
<li><p>If <var>eventConstructor</var> is not given, then let <var>eventConstructor</var> be
14481477
{{Event}}.
14491478

1450-
<li><p>Let <var>event</var> be the result of <a for=Event lt=constructor>invoking</a> the initial
1451-
value of <var>eventConstructor</var> with the argument <var>e</var>.
1479+
<li><p>Let <var>event</var> be the result of <a>creating an event</a> given
1480+
<var>eventConstructor</var>.
14521481

1453-
<li><p>Initialize <var>event</var>'s {{Event/isTrusted}} attribute to true.
1482+
<li><p>Initialize <var>event</var>'s {{Event/type}} attribute to <var>e</var>.
14541483

14551484
<li>
14561485
<p>Initialize any other IDL attributes of <var>event</var> as described in the invocation of this
@@ -5199,15 +5228,17 @@ invoked, must run these steps:
51995228
<li><p>If <var>constructor</var> is null, then <a>throw</a> a {{NotSupportedError}}.
52005229

52015230
<li>
5202-
<p>If the initial value of <var>constructor</var> is undefined, then <a>throw</a> a
5203-
{{NotSupportedError}}.
5231+
<p>If the interface indicated by <var>constructor</var> is not exposed on the <a>relevant global
5232+
object</a> of the <a>context object</a>, then <a>throw</a> a {{NotSupportedError}}.
52045233

52055234
<p class=note>Typically user agents disable support for touch events in some configurations, in
5206-
which case the initial value of {{TouchEvent}} is undefined.
5235+
which case this clause would be triggered for the interface {{TouchEvent}}.
5236+
5237+
<li><p>Let <var>event</var> be the result of <a>creating an event</a> given <var>constructor</var>.
52075238

5208-
<li><p>Let <var>event</var> be the result of <a for=Event lt=constructor>invoking</a> the initial
5209-
value of <var>constructor</var> with the empty string as argument.
5210-
<!-- "initial value" as in before script could get to it -->
5239+
<li><p>Initialize <var>event</var>'s {{Event/type}} attribute to the empty string.
5240+
5241+
<li><p>Initialize <var>event</var>'s {{Event/isTrusted}} attribute to false.
52115242

52125243
<li><p>Unset <var>event</var>'s <a>initialized flag</a>.
52135244

dom.html

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -713,21 +713,40 @@ <h3 class="heading settled" data-level="3.3" id="interface-customevent"><span cl
713713
<li>Set <a data-link-type="dfn" href="#context-object">context object</a>’s <code class="idl"><a data-link-type="idl" href="#dom-customevent-detail">detail</a></code> attribute to <var>detail</var>.
714714
</ol>
715715
<h3 class="heading settled" data-level="3.4" id="constructing-events"><span class="secno">3.4. </span><span class="content">Constructing events</span><a class="self-link" href="#constructing-events"></a></h3>
716-
<p>When a <dfn data-dfn-for="Event" data-dfn-type="dfn" data-export="" id="concept-event-constructor">constructor<a class="self-link" href="#concept-event-constructor"></a></dfn> of the <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> interface, or of an interface that inherits from the <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> interface, is
717-
invoked, these steps must be run:</p>
716+
<p>When a <dfn data-dfn-for="Event" data-dfn-type="dfn" data-export="" id="concept-event-constructor">constructor<a class="self-link" href="#concept-event-constructor"></a></dfn> of the <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> interface, or of an interface that inherits from the <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> interface, is invoked, these steps
717+
must be run, given the arguments <var>type</var> and <var>eventInitDict</var>: </p>
718718
<ol>
719719
<li>
720-
<p>Create an <a data-link-type="dfn" href="#concept-event">event</a> that uses the interface the constructor was invoked upon. </p>
720+
<p>Create a new object <var>event</var> using this interface. </p>
721721
<li>
722-
<p>Set its <a data-link-type="dfn" href="#initialized-flag">initialized flag</a>. </p>
722+
<p>Set <var>event</var>’s <a data-link-type="dfn" href="#initialized-flag">initialized flag</a>. </p>
723723
<li>
724-
<p>Initialize the <code class="idl"><a data-link-type="idl" href="#dom-event-type">type</a></code> attribute to the <var>type</var> argument. </p>
724+
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-type">type</a></code> attribute to <var>type</var>. </p>
725725
<li>
726-
<p>If there is an <var>eventInitDict</var> argument, then for each <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> present, find the attribute on <a data-link-type="dfn" href="#concept-event">event</a> whose <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-identifier">identifier</a> matches the key of
727-
the <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> and then set the attribute to the value of that <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a>. </p>
726+
<p>For each <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> present in <var>eventInitDict</var>, find the attribute on <var>event</var> whose <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-identifier">identifier</a> matches the key of the <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> and then set the attribute to the value of that <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a>. </p>
728727
<li>
729-
<p>Return the <a data-link-type="dfn" href="#concept-event">event</a>. </p>
728+
<p>Return <var>event</var>. </p>
729+
</ol>
730+
<p>To <dfn data-dfn-type="dfn" data-export="" data-lt="creating an event|create an event" id="concept-event-create">create an event<a class="self-link" href="#concept-event-create"></a></dfn> using <var>eventInterface</var>, which must be either <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> or an interface that inherits from
731+
it, run these steps:</p>
732+
<ol>
733+
<li>
734+
<p>Create a new object <var>event</var> using <var>eventInterface</var>. </p>
735+
<li>
736+
<p>Set <var>event</var>’s <a data-link-type="dfn" href="#initialized-flag">initialized flag</a>. </p>
737+
<li>
738+
<p>Let <var>defaultEventInitDict</var> be the result of <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-convert-ecmascript-to-idl-value">converting</a> the JavaScript value undefined to the dictionary
739+
type accepted by <var>eventInterface</var>’s constructor. (This dictionary type will either be <code class="idl"><a data-link-type="idl" href="#dictdef-eventinit">EventInit</a></code> or a dictionary that inherits from it.) </p>
740+
<li>For each <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> present in <var>defaultEventInitDict</var>, find the
741+
attribute on <var>event</var> whose <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-identifier">identifier</a> matches the key of the <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a> and then set the attribute to the default value of that <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary member</a>.
742+
<li>
743+
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-istrusted">isTrusted</a></code> attribute to true. </p>
744+
<li>
745+
<p>Return <var>event</var>. </p>
730746
</ol>
747+
<p class="note" role="note"><a data-link-type="dfn" href="#concept-event-create">Create an event</a> is meant to be used by other specifications which need to
748+
separately <a data-link-type="dfn" href="#concept-event-create">create</a> and <a data-link-type="dfn" href="#concept-event-dispatch">dispatch</a> events, instead of simply <a data-link-type="dfn" href="#concept-event-fire">firing</a> them. It ensures the event’s attributes are initialized to the
749+
correct defaults.</p>
731750
<h3 class="heading settled" data-level="3.5" id="defining-event-interfaces"><span class="secno">3.5. </span><span class="content">Defining event interfaces</span><a class="self-link" href="#defining-event-interfaces"></a></h3>
732751
<p>In general, when defining a new interface that inherits from <code class="idl"><a data-link-type="idl" href="#event">Event</a></code> please always ask
733752
feedback from the <a href="https://whatwg.org/">WHATWG</a> or the <a href="https://www.w3.org/2008/webapps/">W3C WebApps WG</a> community.</p>
@@ -1070,10 +1089,9 @@ <h3 class="heading settled" data-level="3.9" id="firing-events"><span class="sec
10701089
<li>
10711090
<p>If <var>eventConstructor</var> is not given, then let <var>eventConstructor</var> be <code class="idl"><a data-link-type="idl" href="#event">Event</a></code>. </p>
10721091
<li>
1073-
<p>Let <var>event</var> be the result of <a data-link-type="dfn" href="#concept-event-constructor">invoking</a> the initial
1074-
value of <var>eventConstructor</var> with the argument <var>e</var>. </p>
1092+
<p>Let <var>event</var> be the result of <a data-link-type="dfn" href="#concept-event-create">creating an event</a> given <var>eventConstructor</var>. </p>
10751093
<li>
1076-
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-istrusted">isTrusted</a></code> attribute to true. </p>
1094+
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-type">type</a></code> attribute to <var>e</var>. </p>
10771095
<li>
10781096
<p>Initialize any other IDL attributes of <var>event</var> as described in the invocation of this
10791097
algorithm. </p>
@@ -3264,12 +3282,16 @@ <h3 class="heading settled" data-level="4.5" id="interface-document"><span class
32643282
<li>
32653283
<p>If <var>constructor</var> is null, then <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">throw</a> a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#notsupportederror">NotSupportedError</a></code>. </p>
32663284
<li>
3267-
<p>If the initial value of <var>constructor</var> is undefined, then <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">throw</a> a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#notsupportederror">NotSupportedError</a></code>. </p>
3285+
<p>If the interface indicated by <var>constructor</var> is not exposed on the <a data-link-type="dfn" href="https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-global">relevant global
3286+
object</a> of the <a data-link-type="dfn" href="#context-object">context object</a>, then <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">throw</a> a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#notsupportederror">NotSupportedError</a></code>. </p>
32683287
<p class="note" role="note">Typically user agents disable support for touch events in some configurations, in
3269-
which case the initial value of <code class="idl"><a data-link-type="idl" href="https://www.w3.org/TR/touch-events/#touchevent-interface">TouchEvent</a></code> is undefined. </p>
3288+
which case this clause would be triggered for the interface <code class="idl"><a data-link-type="idl" href="https://www.w3.org/TR/touch-events/#touchevent-interface">TouchEvent</a></code>. </p>
32703289
<li>
3271-
<p>Let <var>event</var> be the result of <a data-link-type="dfn" href="#concept-event-constructor">invoking</a> the initial
3272-
value of <var>constructor</var> with the empty string as argument. </p>
3290+
<p>Let <var>event</var> be the result of <a data-link-type="dfn" href="#concept-event-create">creating an event</a> given <var>constructor</var>. </p>
3291+
<li>
3292+
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-type">type</a></code> attribute to the empty string. </p>
3293+
<li>
3294+
<p>Initialize <var>event</var>’s <code class="idl"><a data-link-type="idl" href="#dom-event-istrusted">isTrusted</a></code> attribute to false. </p>
32733295
<li>
32743296
<p>Unset <var>event</var>’s <a data-link-type="dfn" href="#initialized-flag">initialized flag</a>. </p>
32753297
<li>
@@ -5811,6 +5833,7 @@ <h3 class="no-num no-ref heading settled" id="index-defined-here"><span class="c
58115833
<li><a href="#converted-to-ascii-uppercase">converted to ASCII uppercase</a><span>, in §2.2</span>
58125834
<li><a href="#converting-nodes-into-a-node">converting nodes into a node</a><span>, in §4.2.6</span>
58135835
<li><a href="#concept-create-element">create an element</a><span>, in §4.9</span>
5836+
<li><a href="#concept-event-create">create an event</a><span>, in §3.4</span>
58145837
<li><a href="#dom-document-createattribute">createAttribute(localName)</a><span>, in §4.5</span>
58155838
<li><a href="#dom-document-createattributens">createAttributeNS(namespace, qualifiedName)</a><span>, in §4.5</span>
58165839
<li><a href="#dom-document-createcdatasection">createCDATASection(data)</a><span>, in §4.5</span>
@@ -5837,6 +5860,7 @@ <h3 class="no-num no-ref heading settled" id="index-defined-here"><span class="c
58375860
<li><a href="#dom-document-createtreewalker">createTreeWalker(root, whatToShow)</a><span>, in §4.5</span>
58385861
<li><a href="#dom-document-createtreewalker">createTreeWalker(root, whatToShow, filter)</a><span>, in §4.5</span>
58395862
<li><a href="#concept-create-element">creating an element</a><span>, in §4.9</span>
5863+
<li><a href="#concept-event-create">creating an event</a><span>, in §3.4</span>
58405864
<li><a href="#dom-treewalker-currentnode">currentNode</a><span>, in §6.2</span>
58415865
<li><a href="#dom-event-currenttarget">currentTarget</a><span>, in §3.2</span>
58425866
<li><a href="#concept-element-custom">custom</a><span>, in §4.9</span>
@@ -6665,6 +6689,7 @@ <h3 class="no-num no-ref heading settled" id="index-defined-elsewhere"><span cla
66656689
<li><a href="https://heycam.github.io/webidl/#wrongdocumenterror">WrongDocumentError</a>
66666690
<li><a href="https://heycam.github.io/webidl/#call-a-user-objects-operation">call a user object's operation</a>
66676691
<li><a href="https://heycam.github.io/webidl/#dfn-callback-this-value">callback this value</a>
6692+
<li><a href="https://heycam.github.io/webidl/#dfn-convert-ecmascript-to-idl-value">converted to an idl value</a>
66686693
<li><a href="https://heycam.github.io/webidl/#dfn-dictionary-member">dictionary members</a>
66696694
<li><a href="https://heycam.github.io/webidl/#dfn-identifier">identifier</a>
66706695
<li><a href="https://heycam.github.io/webidl/#invoke-a-callback-function">invoke</a>

0 commit comments

Comments
 (0)