forked from whatwg/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.bs
10202 lines (7865 loc) · 382 KB
/
dom.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: DOM Standard
H1: DOM
Shortname: dom
Status: LS
Group: WHATWG
No Editor: true
!Participate: <a href=https://github.com/whatwg/dom>GitHub whatwg/dom</a> (<a href=https://github.com/whatwg/dom/issues/new>new issue</a>, <a href=https://github.com/whatwg/dom/issues>open issues</a>, <a href=https://www.w3.org/Bugs/Public/buglist.cgi?component=DOM&product=WebAppsWG&resolution=--->legacy open bugs</a>)
!Participate: <a href=https://wiki.whatwg.org/wiki/IRC>IRC: #whatwg on Freenode</a>
!Commits: <a href=https://github.com/whatwg/dom/commits>GitHub whatwg/dom/commits</a>
!Commits: [SNAPSHOT-LINK]
!Commits: <a href=https://twitter.com/thedomstandard>@thedomstandard</a>
!Tests: <a href=https://github.com/w3c/web-platform-tests/tree/master/dom>web-platform-tests dom/</a> (<a href=https://github.com/w3c/web-platform-tests/labels/dom>ongoing work</a>)
!Translation (non-normative): <span title=Japanese><a href=https://triple-underscore.github.io/DOM4-ja.html lang=ja hreflang=ja rel=alternate>日本語</a></span>
Logo: https://resources.whatwg.org/logo-dom.svg
Abstract: DOM defines a platform-neutral model for events, aborting activities, and node trees.
Ignored Terms: EmptyString, Array, Document
Boilerplate: omit feedback-header, omit conformance
Indent: 1
</pre>
<!--
Bikeshed problems:
"Document" isn't properly defining itself, and the force switch isn't working.
-->
<script src=https://resources.whatwg.org/file-issue.js async></script>
<script src=https://resources.whatwg.org/commit-snapshot-shortcut-key.js async></script>
<script src=https://resources.whatwg.org/dfn.js defer></script>
<pre class=anchors>
urlPrefix: https://www.w3.org/TR/xml/#NT-
type: type
text: Name; url: Name
text: Char; url: Char
text: PubidChar; url: PubidChar
urlPrefix: https://www.w3.org/TR/xml-names/#NT-
type: type
text: QName; url: QName
url: https://w3c.github.io/DOM-Parsing/#dfn-createcontextualfragment-fragment
type: method; text: createContextualFragment(); for: Range
type: interface
url: https://w3c.github.io/touch-events/#idl-def-touchevent
text: TouchEvent
url: https://w3c.github.io/deviceorientation/spec-source-orientation.html#devicemotion
text: DeviceMotionEvent
text: DeviceOrientationEvent
url: https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15
text: WebGLContextEvent
urlPrefix: https://w3c.github.io/ServiceWorker/#; spec: SERVICE-WORKERS
type: dfn; urlPrefix: dfn-
text: service worker
text: script resource; for: service worker
text: has ever been evaluated flag; for: service worker
urlPrefix: https://tc39.github.io/ecma262/#; spec: ECMASCRIPT
text: Construct; url: sec-construct; type: abstract-op
text: Realm; url: realm; type: dfn
urlPrefix: https://w3c.github.io/hr-time/#; spec: HR-TIME
type:typedef; urlPrefix: dom-; text: DOMHighResTimeStamp
type:dfn; text: time origin
type:dfn; text: clock resolution
</pre>
<pre class=link-defaults>
spec:html; type:element
text: title
text: script
spec:infra; type:dfn; text:list
</pre>
<h2 id=goals class=no-num>Goals</h2>
This specification standardizes the DOM. It does so as follows:
<ol>
<li>
<p>By consolidating <cite>DOM Level 3 Core</cite> [[DOM-Level-3-Core]],
<cite>Element Traversal</cite> [[ELEMENTTRAVERSAL]],
<cite>Selectors API Level 2</cite> [[SELECTORS-API2]], the
"DOM Event Architecture" and "Basic Event Interfaces" chapters of
<cite>DOM Level 3 Events</cite> [[uievents-20031107]] (specific types of events do not
belong in the DOM Standard), and <cite>DOM Level 2 Traversal and Range</cite>
[[DOM-Level-2-Traversal-Range]], and:
<ul class=brief>
<li>Aligning them with the JavaScript ecosystem where possible.
<li>Aligning them with existing implementations.
<li>Simplifying them as much as possible.
</ul>
<li><p>By moving features from the HTML Standard [[!HTML]] that make more sense to be
specified as part of the DOM Standard.
<li>
<p>By defining a replacement for the "Mutation Events" and
"Mutation Name Event Types" chapters of <cite>DOM Level 3 Events</cite>
[[uievents-20031107]] as the old model
was problematic.
<p class="note no-backref">The old model is expected to be removed from implementations
in due course.
<li><p>By defining new features that simplify common DOM operations.
</ol>
<h2 id=infrastructure oldids=terminology,dependencies>Infrastructure</h2>
<p>This specification depends on the Infra Standard. [[!INFRA]]
<p>Some of the terms used in this specification are defined in <cite>Encoding</cite>,
<cite>Selectors</cite>, <cite>Web IDL</cite>, <cite>XML</cite>, and <cite>Namespaces in XML</cite>.
[[!ENCODING]]
[[!SELECTORS4]]
[[!WEBIDL]]
[[!XML]]
[[!XML-NAMES]]
<p>The term <dfn export>context object</dfn> means the object on which the algorithm,
attribute getter, attribute setter, or method being discussed was called. When the
<a>context object</a> is unambiguous, the term can be omitted.
<p>When extensions are needed, the DOM Standard can be updated accordingly, or a new standard
can be written that hooks into the provided extensibility hooks for
<dfn export lt="other applicable specifications">applicable specifications</dfn>.
<!-- https://www.w3.org/mid/[email protected] -->
<h3 id=trees>Trees</h3> <!-- Sorry reddit, this is not /r/trees -->
<p>A <dfn export id=concept-tree>tree</dfn> is a finite hierarchical tree structure. In
<dfn export id=concept-tree-order>tree order</dfn> is preorder, depth-first traversal of a
<a>tree</a>.
<!-- https://en.wikipedia.org/wiki/Depth-first_search -->
<p>An object that
<dfn export for=tree id=concept-tree-participate lt="participate|participate in a tree|participates in a tree">participates</dfn>
in a <a>tree</a> has a <dfn export for=tree id=concept-tree-parent>parent</dfn>, which is either
null or an object, and has
<dfn export for=tree id=concept-tree-child lt="child|children">children</dfn>, which is an
<a>ordered set</a> of objects. An object <var>A</var> whose <a for=tree>parent</a> is object
<var>B</var> is a <a for=tree>child</a> of <var>B</var>.
<p>The <dfn export for=tree id=concept-tree-root>root</dfn> of an object is itself, if its
<a for=tree>parent</a> is null, or else it is the <a for=tree>root</a> of its
<a for=tree>parent</a>. The <a for=tree>root</a> of a <a>tree</a> is any object
<a for=tree>participating</a> in that <a>tree</a> whose <a for=tree>parent</a> is null.
An object <var>A</var> is called a
<dfn export for=tree id=concept-tree-descendant>descendant</dfn> of an object
<var>B</var>, if either <var>A</var> is a
<a for=tree>child</a> of <var>B</var> or
<var>A</var> is a <a for=tree>child</a> of an
object <var ignore>C</var> that is a
<a>descendant</a> of <var>B</var>.
An
<dfn export for=tree id=concept-tree-inclusive-descendant>inclusive descendant</dfn> is
an object or one of its
<a>descendants</a>.
An object <var>A</var> is called an
<dfn export for=tree id=concept-tree-ancestor>ancestor</dfn> of an object
<var>B</var> if and only if <var>B</var> is a
<a>descendant</a> of
<var>A</var>.
An <dfn export for=tree id=concept-tree-inclusive-ancestor>inclusive ancestor</dfn> is
an object or one of its <a>ancestors</a>.
An object <var>A</var> is called a
<dfn export for=tree id=concept-tree-sibling>sibling</dfn> of an object
<var>B</var>, if and only if <var>B</var> and <var>A</var>
share the same non-null <a for=tree>parent</a>.
An <dfn export for=tree id=concept-tree-inclusive-sibling>inclusive sibling</dfn> is an
object or one of its <a for=tree>siblings</a>.
An object <var>A</var> is
<dfn export for=tree id=concept-tree-preceding>preceding</dfn> an object
<var>B</var> if <var>A</var> and <var>B</var> are in the
same <a>tree</a> and <var>A</var> comes
before <var>B</var> in
<a>tree order</a>.
An object <var>A</var> is
<dfn export for=tree id=concept-tree-following>following</dfn> an object
<var>B</var> if <var>A</var> and <var>B</var> are in the
same <a>tree</a> and <var>A</var> comes
after <var>B</var> in
<a>tree order</a>.
The <dfn export for=tree id=concept-tree-first-child>first child</dfn> of an object is its
first <a for=tree>child</a> or null if it has no <a>children</a>.
The <dfn export for=tree id=concept-tree-last-child>last child</dfn> of an object is its
last <a for=tree>child</a> or null if it has no <a>children</a>.
The <dfn export for=tree id=concept-tree-previous-sibling>previous sibling</dfn> of an
object is its first <a>preceding</a>
<a for=tree>sibling</a> or null if it has no
<a>preceding</a>
<a for=tree>sibling</a>.
The <dfn export for=tree id=concept-tree-next-sibling>next sibling</dfn> of an
object is its first <a>following</a>
<a for=tree>sibling</a> or null if it has no
<a>following</a>
<a for=tree>sibling</a>.
<p>The <dfn export for=tree id=concept-tree-index>index</dfn> of an object is its number of
<a>preceding</a> <a for=tree>siblings</a>, or 0 if it has none.
<h3 id="ordered-sets">Ordered sets</h3>
<p>The <dfn export id=concept-ordered-set-parser>ordered set parser</dfn> takes a string
<var>input</var> and then runs these steps:
<ol>
<li><p>Let <var>inputTokens</var> be the result of
<a lt="split on ASCII whitespace">splitting <var>input</var> on ASCII whitespace</a>.
<li><p>Let <var>tokens</var> be a new <a>ordered set</a>.
<li><p><a for=list>For each</a> <var>token</var> in <var>inputTokens</var>, <a for=set>append</a>
<var>token</var> to <var>tokens</var>.
<li>Return <var>tokens</var>.
</ol>
<p>The <dfn export id=concept-ordered-set-serializer>ordered set serializer</dfn> takes a
<var>set</var> and returns the concatenation of the strings in <var>set</var>, separated from each
other by U+0020, if <var>set</var> is non-empty, and the empty string otherwise.
<h3 id=selectors>Selectors</h3>
<!--
To <dfn export>match a relative selectors string</dfn> <var>relativeSelectors</var>
against a <var>set</var>, run these steps:
<ol>
<li>Let <var>s</var> be the result of
<a>parse a relative selector</a> from
<var>relativeSelectors</var> against <var>set</var>.
[[!SELECTORS4]]
<li>If <var>s</var> is failure, <a>throw</a> a "{{SyntaxError!!exception}}" {{DOMException}}.
<li>Return the result of <a>evaluate a selector</a> <var>s</var>
using <a>:scope elements</a> <var>set</var>. [[!SELECTORS4]]
</ol>
-->
<p>To <dfn export>scope-match a selectors string</dfn> <var>selectors</var> against a
<var>node</var>, run these steps:
<ol>
<li><p>Let <var>s</var> be the result of <a>parse a selector</a> <var>selectors</var>.
[[!SELECTORS4]]
<li><p>If <var>s</var> is failure, then <a>throw</a> a "{{SyntaxError!!exception}}"
{{DOMException}}.
<li><p>Return the result of <a>evaluate a selector</a> <var>s</var> against <var>node</var>'s
<a for=tree>root</a> using <a>scoping root</a> <var>node</var>. [[!SELECTORS4]].
</ol>
<p class=note>Support for namespaces within selectors is not planned and will not be
added.
<h3 id=namespaces>Namespaces</h3>
<p>To <dfn export>validate</dfn> a <var>qualifiedName</var>, <a>throw</a> an
"{{InvalidCharacterError!!exception}}" {{DOMException}} if <var>qualifiedName</var> does not match
the <code><a type>Name</a></code> or <code><a type>QName</a></code> production.
To <dfn export>validate and extract</dfn> a <var>namespace</var> and <var>qualifiedName</var>,
run these steps:
<ol>
<li>If <var>namespace</var> is the empty string, set it to null.
<li><a>Validate</a> <var>qualifiedName</var>.
<li>Let <var>prefix</var> be null.
<li>Let <var>localName</var> be <var>qualifiedName</var>.
<li>If <var>qualifiedName</var> contains a "<code>:</code>" (U+003E), then split the
string on it and set <var>prefix</var> to the part before and <var>localName</var> to
the part after.
<li>If <var>prefix</var> is non-null and <var>namespace</var> is null, then <a>throw</a> a
"{{NamespaceError!!exception}}" {{DOMException}}.
<li>If <var>prefix</var> is "<code>xml</code>" and <var>namespace</var> is not the
<a>XML namespace</a>, then <a>throw</a> a "{{NamespaceError!!exception}}" {{DOMException}}.
<li>If either <var>qualifiedName</var> or <var>prefix</var> is
"<code>xmlns</code>" and <var>namespace</var> is not the
<a>XMLNS namespace</a>, then <a>throw</a> a
"{{NamespaceError!!exception}}" {{DOMException}}.
<li>If <var>namespace</var> is the <a>XMLNS namespace</a> and neither <var>qualifiedName</var>
nor <var>prefix</var> is "<code>xmlns</code>", then <a>throw</a> a "{{NamespaceError!!exception}}"
{{DOMException}}.
<li>Return <var>namespace</var>, <var>prefix</var>, and <var>localName</var>.
</ol>
<h2 id=events>Events</h2>
<h3 id=introduction-to-dom-events>Introduction to "DOM Events"</h3>
Throughout the web platform <a>events</a> are <a>dispatched</a> to objects to signal an
occurrence, such as network activity or user interaction. These objects implement the
{{EventTarget}} interface and can therefore add <a>event listeners</a> to observe
<a>events</a> by calling {{EventTarget/addEventListener()}}:
<pre class=lang-javascript>
obj.addEventListener("load", imgFetched)
function imgFetched(ev) {
// great success
…
}
</pre>
<a>Event listeners</a> can be removed
by utilizing the
{{EventTarget/removeEventListener()}}
method, passing the same arguments.
<a>Events</a> are objects too and implement the
{{Event}} interface (or a derived interface). In the example above
<var ignore>ev</var> is the <a>event</a>. It is
passed as argument to
<a>event listener</a>'s <b>callback</b>
(typically a JavaScript Function as shown above).
<a>Event listeners</a> key off the
<a>event</a>'s
{{Event/type}} attribute value
("<code>load</code>" in the above example). The
<a>event</a>'s
{{Event/target}} attribute value returns the
object to which the <a>event</a> was
<a>dispatched</a>
(<var ignore>obj</var> above).
<p id="synthetic-events">Now while typically <a>events</a> are <a>dispatched</a> by the user agent
as the result of user interaction or the completion of some task, applications can <a>dispatch</a>
<a>events</a> themselves, commonly known as synthetic events:
<pre class='lang-javascript'>
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) })
// create and dispatch the event
var event = new CustomEvent("cat", {"detail":{"hazcheeseburger":true}})
obj.dispatchEvent(event)
</pre>
Apart from signaling, <a>events</a> are
sometimes also used to let an application control what happens next in an
operation. For instance as part of form submission an
<a>event</a> whose
{{Event/type}} attribute value is
"<code>submit</code>" is
<a>dispatched</a>. If this
<a>event</a>'s
{{Event/preventDefault()}} method is
invoked, form submission will be terminated. Applications who wish to make
use of this functionality through <a>events</a>
<a>dispatched</a> by the application
(synthetic events) can make use of the return value of the
{{EventTarget/dispatchEvent()}} method:
<pre class='lang-javascript'>
if(obj.dispatchEvent(event)) {
// event was not canceled, time for some magic
…
}
</pre>
When an <a>event</a> is
<a>dispatched</a> to an object that
<a>participates</a> in a
<a>tree</a> (e.g. an
<a for="/">element</a>), it can reach
<a>event listeners</a> on that object's
<a>ancestors</a> too. First all object's
<a>ancestor</a>
<a>event listeners</a> whose
<b>capture</b> variable is set to true are invoked, in
<a>tree order</a>. Second, object's own
<a>event listeners</a> are invoked. And
finally, and only if <a>event</a>'s
{{Event/bubbles}} attribute value is true,
object's <a>ancestor</a>
<a>event listeners</a> are invoked again,
but now in reverse <a>tree order</a>.
Lets look at an example of how <a>events</a> work in a <a>tree</a>:
<pre class='lang-markup'>
<!doctype html>
<html>
<head>
<title>Boring example</title>
</head>
<body>
<p>Hello <span id=x>world</span>!</p>
<script>
function test(e) {
debug(e.target, e.currentTarget, e.eventPhase)
}
document.addEventListener("hey", test, {capture: true})
document.body.addEventListener("hey", test)
var ev = new Event("hey", {bubbles:true})
document.getElementById("x").dispatchEvent(ev)
</script>
</body>
</html>
</pre>
The <code>debug</code> function will be invoked twice. Each time the <a>event</a>'s
{{Event/target}} attribute value will be the
<code>span</code> <a for="/">element</a>. The
first time {{Event/currentTarget}} attribute's
value will be the <a>document</a>, the second
time the <code>body</code> <a for="/">element</a>.
{{Event/eventPhase}} attribute's value
switches from {{Event/CAPTURING_PHASE}}
to {{Event/BUBBLING_PHASE}}. If an
<a>event listener</a> was registered for
the <code>span</code> <a for="/">element</a>,
{{Event/eventPhase}} attribute's value
would have been {{Event/AT_TARGET}}.
<h3 id=interface-event>Interface {{Event}}</h3>
<pre class="idl">
[Constructor(DOMString type, optional EventInit eventInitDict),
Exposed=(Window,Worker,AudioWorklet)]
interface Event {
readonly attribute DOMString type;
readonly attribute EventTarget? target;
readonly attribute EventTarget? currentTarget;
sequence<EventTarget> composedPath();
const unsigned short NONE = 0;
const unsigned short CAPTURING_PHASE = 1;
const unsigned short AT_TARGET = 2;
const unsigned short BUBBLING_PHASE = 3;
readonly attribute unsigned short eventPhase;
void stopPropagation();
attribute boolean cancelBubble; // historical alias of .stopPropagation
void stopImmediatePropagation();
readonly attribute boolean bubbles;
readonly attribute boolean cancelable;
void preventDefault();
readonly attribute boolean defaultPrevented;
readonly attribute boolean composed;
[Unforgeable] readonly attribute boolean isTrusted;
readonly attribute DOMHighResTimeStamp timeStamp;
void initEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false); // historical
};
dictionary EventInit {
boolean bubbles = false;
boolean cancelable = false;
boolean composed = false;
};
</pre>
<p>An {{Event}} object is simply named an <dfn export id=concept-event>event</dfn>. It allows for
signaling that something has occurred, e.g., that an image has completed downloading.</p>
<p>An <a>event</a> has an associated <dfn export for=Event>relatedTarget</dfn> (null or an
{{EventTarget}} object). Unless stated otherwise it is null.</p>
<p class="note">Other specifications use <a>relatedTarget</a> to define a <code>relatedTarget</code>
attribute. [[UIEVENTS]]
<p>An <a>event</a> has an associated <dfn export for=Event>path</dfn>. A <a for=Event>path</a> is a
<a for=/>list</a> of <a for=/>structs</a>. Each <a for=/>struct</a> consists of an
<dfn for=Event/path>item</dfn> (an {{EventTarget}} object), <dfn for=Event/path>target</dfn> (null
or an {{EventTarget}} object), a <dfn for=Event/path>relatedTarget</dfn> (null or an
{{EventTarget}} object), <dfn for=Event/path>root-of-closed-tree</dfn> (a boolean), and a
<dfn for=Event/path>slot-in-closed-tree</dfn> (a boolean). A <a for=Event>path</a> is initially the
empty list.</p>
<p><a lt="Other applicable specifications">Specifications</a> may define
<dfn export for=Event>retargeting steps</dfn> for all or some <a>events</a>.
The algorithm is passed <var>event</var>, as indicated in the <a>dispatch</a>
algorithm below.
<dl class=domintro>
<dt><code><var>event</var> = new <a constructor lt="Event()">Event</a>(<var>type</var> [, <var>eventInitDict</var>])</code>
<dd>Returns a new <var>event</var> whose
{{Event/type}} attribute value is set to
<var>type</var>. The optional <var>eventInitDict</var> argument
allows for setting the {{Event/bubbles}} and
{{Event/cancelable}} attributes via object
members of the same name.
<dt><code><var>event</var> . {{Event/type}}</code>
<dd>Returns the type of <var>event</var>, e.g.
"<code>click</code>", "<code>hashchange</code>", or
"<code>submit</code>".
<dt><code><var>event</var> . {{Event/target}}</code>
<dd>Returns the object to which <var>event</var> is <a>dispatched</a>.
<dt><code><var>event</var> . {{Event/currentTarget}}</code>
<dd>Returns the object whose <a>event listener</a>'s <b>callback</b> is currently being
invoked.
<dt><code><var>event</var> . {{Event/composedPath()}}</code>
<dd>Returns the <a for=Event/path>item</a> objects of <var>event</var>'s <a for=Event>path</a>
(objects on which listeners will be invoked), except for any <a>nodes</a> in
<a for=/>shadow trees</a> of which the <a for=/>shadow root</a>'s <a for=ShadowRoot>mode</a> is
"<code>closed</code>" that are not reachable from <var>event</var>'s {{Event/currentTarget}}.
<dt><code><var>event</var> . {{Event/eventPhase}}</code>
<dd>Returns the <a>event</a>'s phase, which is one of {{Event/NONE}},
{{Event/CAPTURING_PHASE}},
{{Event/AT_TARGET}}, and
{{Event/BUBBLING_PHASE}}.
<dt><code><var>event</var> . <a method for=Event lt="stopPropagation()">stopPropagation</a>()</code>
<dd>When <a>dispatched</a> in a <a>tree</a>, invoking this method prevents
<var>event</var> from reaching any objects other than the current object.
<dt><code><var>event</var> . <a method for=Event lt="stopImmediatePropagation()">stopImmediatePropagation</a>()</code>
<dd>Invoking this method prevents <var>event</var> from reaching
any registered <a>event listeners</a> after the current one finishes running and, when
<a>dispatched</a> in a <a>tree</a>, also prevents <var>event</var> from reaching any
other objects.
<dt><code><var>event</var> . {{Event/bubbles}}</code>
<dd>Returns true or false depending on how <var>event</var> was initialized. True if
<var>event</var> goes through its {{Event/target}} attribute value's <a>ancestors</a>
in reverse <a>tree order</a>, and false otherwise.
<dt><code><var>event</var> . {{Event/cancelable}}</code>
<dd>Returns true or false depending on how <var>event</var> was initialized. Its return
value does not always carry meaning, but true can indicate that part of the operation
during which <var>event</var> was <a>dispatched</a>, can be canceled by invoking the
{{Event/preventDefault()}} method.
<dt><code><var>event</var> . <a method for=Event lt="preventDefault()">preventDefault</a>()</code>
<dd>If invoked when the {{Event/cancelable}} attribute value is true, and while executing a
listener for the <var>event</var> with {{AddEventListenerOptions/passive}} set to false, signals to
the operation that caused <var>event</var> to be <a>dispatched</a> that it needs to be canceled.
<dt><code><var>event</var> . {{Event/defaultPrevented}}</code>
<dd>Returns true if {{Event/preventDefault()}} was invoked successfully to indicate cancelation,
and false otherwise.
<dt><code><var>event</var> . {{Event/composed}}</code>
<dd>Returns true or false depending on how <var>event</var> was initialized. True if
<var>event</var> invokes listeners past a {{ShadowRoot}} <a>node</a> that is the
<a for=tree>root</a> of its {{Event/target}} attribute value, and false otherwise.
<dt><code><var>event</var> . {{Event/isTrusted}}</code>
<dd>Returns true if <var>event</var> was
<a>dispatched</a> by the user agent, and
false otherwise.
<dt><code><var>event</var> . {{Event/timeStamp}}</code>
<dd>Returns the <var>event</var>'s timestamp as the number of milliseconds measured relative to
the <a>time origin</a>.
</dl>
The <dfn attribute for=Event><code>type</code></dfn> attribute must
return the value it was initialized to. When an
<a>event</a> is created the attribute must be
initialized to the empty string.
The <dfn attribute for=Event><code>target</code></dfn> and
<dfn attribute for=Event><code>currentTarget</code></dfn>
attributes must return the values they were initialized to. When an
<a>event</a> is created the attributes must be
initialized to null.
<p>The <dfn method for=Event><code>composedPath()</code></dfn> method, when invoked, must run these
steps:
<ol>
<li><p>Let <var>reversedComposedPath</var> be an empty <a for=/>list</a>.
<li><p>Let <var>hiddenSubtreeLevel</var> be 0.
<li><p>Let <var>hasSeenCurrentTarget</var> be false.
<li><p>Let <var>currentTarget</var> be <a>context object</a>'s {{Event/currentTarget}}
attribute value.
<li><p>Let <var>reversedPath</var> be <a>context object</a>'s <a for=Event>path</a>, in reverse
order.
<li>
<p><a for=list>For each</a> <var>struct</var> in <var>reversedPath</var>:
<ol>
<li><p>If <var>struct</var>'s <a for=Event/path>item</a> is <var>currentTarget</var>, then set
<var>hasSeenCurrentTarget</var> to true.
<li><p>Otherwise, if <var>hasSeenCurrentTarget</var> is true and <var>struct</var>'s
<a for=Event/path>root-of-closed-tree</a> is true, then increase <var>hiddenSubtreeLevel</var> by
1.
<li><p>If <var>hiddenSubtreeLevel</var> is 0, then <a for=list>append</a> <var>struct</var>'s
<a for=Event/path>item</a> to <var>reversedComposedPath</var>.
<li><p>If <var>struct</var>'s <a for=Event/path>slot-in-closed-tree</a> is true and
<var>hiddenSubtreeLevel</var> is greater than 0, then decrease <var>hiddenSubtreeLevel</var> by
1.
</ol>
<li><p>Return <var>reversedComposedPath</var>, in reverse order.
</ol>
The <dfn attribute for=Event><code>eventPhase</code></dfn>
attribute must return the value it was initialized to, which must be one of
the following:
<dl>
<dt><dfn const for=Event>NONE</dfn> (numeric value 0)
<dd><a>Events</a> not currently
<a>dispatched</a> are in this phase.
<dt><dfn const for=Event>CAPTURING_PHASE</dfn> (numeric value 1)
<dd>When an <a>event</a> is
<a>dispatched</a> to an object that
<a>participates</a> in a
<a>tree</a> it will be in this phase before it
reaches its {{Event/target}} attribute value.
<dt><dfn const for=Event>AT_TARGET</dfn> (numeric value 2)
<dd>When an <a>event</a> is
<a>dispatched</a> it will be in this
phase on its {{Event/target}} attribute value.
<dt><dfn const for=Event>BUBBLING_PHASE</dfn> (numeric value 3)
<dd>When an <a>event</a> is
<a>dispatched</a> to an object that
<a>participates</a> in a
<a>tree</a> it will be in this phase after it
reaches its {{Event/target}} attribute value.
</dl>
Initially the attribute must be initialized to
{{Event/NONE}}.
<hr>
Each <a>event</a> has the following associated
flags that are all initially unset:
<ul>
<li><dfn export for=Event id=stop-propagation-flag>stop propagation flag</dfn>
<li><dfn export for=Event id=stop-immediate-propagation-flag>stop immediate propagation flag</dfn>
<li><dfn export for=Event id=canceled-flag>canceled flag</dfn>
<li><dfn export for=Event id=in-passive-listener-flag>in passive listener flag</dfn>
<li><dfn export for=Event id=composed-flag>composed flag</dfn>
<li><dfn export for=Event id=initialized-flag>initialized flag</dfn>
<li><dfn export for=Event id=dispatch-flag>dispatch flag</dfn>
</ul>
<p>The <dfn method for=Event><code>stopPropagation()</code></dfn> method, when invoked, must set the
<a>context object</a>'s <a>stop propagation flag</a>.</p>
<p>The <dfn attribute for=Event><code>cancelBubble</code></dfn> attribute's getter must return true
if <a>context object</a>'s <a>stop propagation flag</a> is set, and false otherwise.
<p>The {{Event/cancelBubble}} attribute's setter must set <a>context object</a>'s
<a>stop propagation flag</a> if the given value is true, and do nothing otherwise.
<p>The <dfn method for=Event><code>stopImmediatePropagation()</code></dfn> method, when invoked,
must set <a>context object</a>'s <a>stop propagation flag</a> and <a>context object</a>'s
<a>stop immediate propagation flag</a>.</p>
The <dfn attribute for=Event><code>bubbles</code></dfn> and
<dfn attribute for=Event><code>cancelable</code></dfn> attributes
must return the values they were initialized to.
The <dfn method for=Event><code>preventDefault()</code></dfn> method, when invoked, must set the
<a>canceled flag</a> if the {{Event/cancelable}} attribute value is true and the
<a>in passive listener flag</a> is unset.
<p class="note no-backref">This means there are scenarios where invoking {{Event/preventDefault()}}
has no effect. User agents are encouraged to log the precise cause in a developer console, to aid
debugging.
<p>The <dfn attribute for=Event><code>defaultPrevented</code></dfn> attribute's getter must return
true if <a>context object</a>'s <a>canceled flag</a> is set, and false otherwise.</p>
<p>The <dfn attribute for=Event><code>composed</code></dfn> attribute's getter must return true if
<a>context object</a>'s <a>composed flag</a> is set, and false otherwise.</p>
<hr>
<p>The <dfn attribute for=Event><code>isTrusted</code></dfn> attribute
must return the value it was initialized to. When an
<a>event</a> is created the attribute must be
initialized to false.
<p class="note no-backref">{{Event/isTrusted}} is a convenience that indicates whether an
<a>event</a> is <a>dispatched</a> by the user agent (as opposed to using
{{EventTarget/dispatchEvent()}}). The sole legacy exception is {{HTMLElement/click()}}, which causes
the user agent to dispatch an <a>event</a> whose {{Event/isTrusted}} attribute is initialized to
false.
The <dfn attribute for=Event><code>timeStamp</code></dfn> attribute must return the value it was
initialized to.
<p class=warning> User agents are strongly encouraged to set minimum resolution of the
{{Event/timeStamp}} attribute to 5 microseconds following the existing <a>clock resolution</a>
recommendation. [[!HR-TIME]]
<hr>
To <dfn export for=Event id=concept-event-initialize>initialize</dfn> an
<var>event</var>, with <var>type</var>,
<var>bubbles</var>, and <var>cancelable</var>, run these steps:
<ol>
<li>Set the <a>initialized flag</a>.
<li>Unset the <a>stop propagation flag</a>,
<a>stop immediate propagation flag</a>, and
<a>canceled flag</a>.
<li>Set the {{Event/isTrusted}} attribute
to false.
<li>Set the {{Event/target}} attribute to
null.
<li>Set the {{Event/type}} attribute to
<var>type</var>.
<li>Set the {{Event/bubbles}} attribute to
<var>bubbles</var>.
<li>Set the {{Event/cancelable}} attribute
to <var>cancelable</var>.
</ol>
<p>The
<dfn method for=Event><code>initEvent(<var>type</var>, <var>bubbles</var>, <var>cancelable</var>)</code></dfn>
method, when invoked, must run these steps:</p>
<ol>
<li><p>If <a>context object</a>'s <a>dispatch flag</a> is set, then return.
<li><p><a>Initialize</a> <a>context object</a> with <var>type</var>, <var>bubbles</var>, and
<var>cancelable</var>.
</ol>
<p class="note no-backref">As <a>events</a> have constructors {{Event/initEvent()}} is redundant and
incapable of setting {{Event/composed}}. It has to be supported for legacy content.
<h3 id=interface-customevent>Interface {{CustomEvent}}</h3>
<pre class=idl>
[Constructor(DOMString type, optional CustomEventInit eventInitDict),
Exposed=(Window,Worker)]
interface CustomEvent : Event {
readonly attribute any detail;
void initCustomEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional any detail = null);
};
dictionary CustomEventInit : EventInit {
any detail = null;
};
</pre>
<a>Events</a> using the
{{CustomEvent}} interface can be used to carry custom data.
<dl class=domintro>
<dt><code><var>event</var> = new <a constructor lt="CustomEvent()">CustomEvent</a>(<var>type</var> [, <var>eventInitDict</var>])</code>
<dd>Works analogously to the constructor for {{Event}} except
that the optional <var>eventInitDict</var> argument now
allows for setting the {{CustomEvent/detail}} attribute
too.
<dt><code><var>event</var> . {{CustomEvent/detail}}</code>
<dd>Returns any custom data <var>event</var> was created with.
Typically used for synthetic events.
<!-- initCustomEvent is dead -->
</dl>
The <dfn attribute for=CustomEvent><code>detail</code></dfn> attribute
must return the value it was initialized to.
The
<dfn method for=CustomEvent><code>initCustomEvent(<var>type</var>, <var>bubbles</var>, <var>cancelable</var>, <var>detail</var>)</code></dfn>
method must, when invoked, run these steps:
<ol>
<li>If <a>context object</a>'s <a>dispatch flag</a> is set, then return.
<li><a>Initialize</a> the
<a>context object</a> with <var>type</var>, <var>bubbles</var>, and
<var>cancelable</var>.
<li>Set <a>context object</a>'s {{CustomEvent/detail}}
attribute to <var>detail</var>.
</ol>
<h3 id=constructing-events>Constructing events</h3>
<p>When a <dfn export for=Event id=concept-event-constructor>constructor</dfn> of the {{Event}}
interface, or of an interface that inherits from the {{Event}} interface, is invoked, these steps
must be run, given the arguments <var>type</var> and <var>eventInitDict</var>:
<ol>
<li><p>Create a new object <var>event</var> using this interface.
<li><p>Set <var>event</var>'s <a>initialized flag</a>.
<li><p>Initialize <var>event</var>'s {{Event/type}} attribute to <var>type</var>.
<li><p>Initialize <var>event</var>'s {{Event/timeStamp}} attribute to a {{DOMHighResTimeStamp}}
representing the high resolution time from the <a>time origin</a> to the occurrence of the call
to the <var>event</var>'s <a>constructor</a>.
<li><p>For each <a>dictionary member</a> present in <var>eventInitDict</var>, find the attribute on
<var>event</var> whose <a spec=webidl>identifier</a> matches the key of the
<a>dictionary member</a> and then set the attribute to the value of that <a>dictionary member</a>.
<li><p>Return <var>event</var>.
</ol>
<p>To
<dfn export id=concept-event-create lt="creating an event|create an event">create an event</dfn>
using <var>eventInterface</var>, which must be either {{Event}} or an interface that inherits from
it, and optionally given a <a>Realm</a> <var>realm</var>, run these steps:</p>
<ol>
<li>
<p>Create a new object <var>event</var> using <var>eventInterface</var>. If <var>realm</var>
is given, use that Realm; otherwise, use the default behavior defined in Web IDL.
<p class="XXX">As of the time of this writing Web IDL does not yet define any default behavior;
see <a href="https://github.com/heycam/webidl/issues/135">heycam/webidl#135</a>.
<li><p>Set <var>event</var>'s <a>initialized flag</a>.
<li><p>Let <var>defaultEventInitDict</var> be the result of
<a lt="converted to an IDL value">converting</a> the JavaScript value undefined to the dictionary
type accepted by <var>eventInterface</var>'s constructor. (This dictionary type will either be
{{EventInit}} or a dictionary that inherits from it.)
<li>For each <a>dictionary member</a> present in <var>defaultEventInitDict</var>, find the
attribute on <var>event</var> whose <a spec=webidl>identifier</a> matches the key of the
<a>dictionary member</a> and then set the attribute to the default value of that
<a>dictionary member</a>.
<li><p>Set <var>event</var>'s {{Event/timeStamp}} attribute to a {{DOMHighResTimeStamp}}
representing the high resolution time from the <a>time origin</a> to the occurrence that the
event is signaling.
<p class=note>For example, in macOS the occurrence time for input actions are available via the
timestamp property of corresponding <code>NSEvent</code> objects. So in this case, the
{{Event/timeStamp}} value will be equivalent to the <code>NSEvent</code>'s timestamp offset by
<a>time origin</a>, translated in milliseconds, and with its precision adjusted to meet the minimum
<a>clock resolution</a>.
<li><p>Initialize <var>event</var>'s {{Event/isTrusted}} attribute to true.
<li><p>Return <var>event</var>.
</ol>
<p class=note><a>Create an event</a> is meant to be used by other specifications which need to
separately <a lt="create an event">create</a> and <a>dispatch</a> events, instead of simply
<a lt="fire an event">firing</a> them. It ensures the event's attributes are initialized to the
correct defaults.</p>
<h3 id=defining-event-interfaces>Defining event interfaces</h3>
In general, when defining a new interface that inherits from {{Event}} please always ask
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.
The {{CustomEvent}} interface can be used as starting point.
However, do not introduce any <code>init<var ignore>*</var>Event()</code>
methods as they are redundant with constructors. Interfaces that inherit
from the {{Event}} interface that have such a method only have it
for historical reasons.
<h3 id=interface-eventtarget>Interface {{EventTarget}}</h3>
<pre class=idl>
[Constructor,
Exposed=(Window,Worker,AudioWorklet)]
interface EventTarget {
void addEventListener(DOMString type, EventListener? callback, optional (AddEventListenerOptions or boolean) options);
void removeEventListener(DOMString type, EventListener? callback, optional (EventListenerOptions or boolean) options);
boolean dispatchEvent(Event event);
};
callback interface EventListener {
void handleEvent(Event event);
};
dictionary EventListenerOptions {
boolean capture = false;
};
dictionary AddEventListenerOptions : EventListenerOptions {
boolean passive = false;
boolean once = false;
};
</pre>
<p>The {{EventTarget}} object represents the target to which an <a>event</a> is <a>dispatched</a>
when something has occurred.
<p>Each {{EventTarget}} object has an associated list of <a>event listeners</a>.
<p>An <dfn export id=concept-event-listener>event listener</dfn> can be used to observe a specific
<a>event</a>.
<p>An <a>event listener</a> consists of these fields:</p>
<ul class="brief">
<li><b>type</b> (a string)
<li><b>callback</b> (an {{EventListener}})
<li><b>capture</b> (a boolean, initially false)
<li><b>passive</b> (a boolean, initially false)
<li><b>once</b> (a boolean, initially false)
<li><b>removed</b> (a boolean for bookkeeping purposes, initially false)
</ul>
<p class="note no-backref">Although <b>callback</b> is an {{EventListener}}, as can be seen from the
fields above, an <a>event listener</a> is a broader concept.
<p>Each {{EventTarget}} object also has an associated <dfn export>get the parent</dfn> algorithm,
which takes an <a>event</a> <var>event</var>, and returns an {{EventTarget}} object. Unless
specified otherwise it returns null.
<p class="note no-backref"><a>Nodes</a>, <a for=/>shadow roots</a>, and <a>documents</a> override
the <a>get the parent</a> algorithm.
<p>Each {{EventTarget}} object can have an associated
<dfn export for=EventTarget>activation behavior</dfn> algorithm. The
<a for=EventTarget>activation behavior</a> algorithm is passed an <a>event</a>, as indicated in the
<a>dispatch</a> algorithm.</p>
<p class="note no-backref">This exists because user agents perform certain actions for certain
{{EventTarget}} objects, e.g., the <{area}> element, in response to synthetic {{MouseEvent}}
<a>events</a> whose {{Event/type}} attribute is <code>click</code>. Web compatibility prevented it
from being removed and it is now the enshrined way of defining an activation of something. [[!HTML]]
<p>Each {{EventTarget}} object that has <a for=EventTarget>activation behavior</a>, can additionally
have both (not either) a <dfn export for=EventTarget>legacy-pre-activation behavior</dfn> algorithm
and a <dfn export for=EventTarget>legacy-canceled-activation behavior</dfn> algorithm.
<p class="note no-backref">These algorithms only exist for checkbox and radio <{input}> elements and
are not to be used for anything else. [[!HTML]]
<dl class=domintro>
<dt><code><var>target</var> = new <a constructor for=EventTarget lt=EventTarget()>EventTarget</a>();</code>
<dd>
Creates a new {{EventTarget}} object, which can be used by developers to <a>dispatch</a> and listen for
<a>events</a>.
<dt><code><var>target</var> . <a method for=EventTarget lt=addEventListener()>addEventListener</a>(<var>type</var>, <var>callback</var> [, <var>options</var>])</code>
<dd>
Appends an <a>event listener</a> for <a>events</a> whose {{Event/type}} attribute value
is <var>type</var>. The <var>callback</var> argument sets the <b>callback</b> that will
be invoked when the <a>event</a> is <a>dispatched</a>.
The <var>options</var> argument sets listener-specific options. For compatibility this can be just
a boolean, in which case the method behaves exactly as if the value was specified as
<var>options</var>' <code>capture</code> member.
When set to true, <var>options</var>' <code>capture</code> member prevents <b>callback</b> from
being invoked when the <a>event</a>'s {{Event/eventPhase}} attribute value is
{{Event/BUBBLING_PHASE}}. When false (or not present), <b>callback</b> will not be invoked when
<a>event</a>'s {{Event/eventPhase}} attribute value is {{Event/CAPTURING_PHASE}}. Either way,
<b>callback</b> will be invoked if <a>event</a>'s {{Event/eventPhase}} attribute value is
{{Event/AT_TARGET}}.
When set to true, <var>options</var>' <code>passive</code> member indicates that the
<b>callback</b> will not cancel the event by invoking {{Event/preventDefault()}}. This is used to
enable performance optimizations described in [[#observing-event-listeners]].
When set to true, <var>options</var>'s <code>once</code> member indicates that the <b>callback</b>