forked from whatwg/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.bs
10059 lines (7627 loc) · 397 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'>
Group: WHATWG
H1: DOM
Shortname: dom
Text Macro: TWITTER thedomstandard
Abstract: DOM defines a platform-neutral model for events, aborting activities, and node trees.
Translation: ja https://triple-underscore.github.io/DOM4-ja.html
Ignored Terms: EmptyString, Array, Document
Indent: 1
</pre>
<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://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.
<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 <a for=string>concatenation</a> of <var>set</var> using U+0020 SPACE.
<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>match a selector against a tree</a> with <var>s</var> and
<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 <a for="event listener">callback</a>
(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
<a for="event listener">capture</a> 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? srcElement; // historical
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;
attribute boolean returnValue; // historical
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>A <dfn export>potential event target</dfn> is null or an {{EventTarget}} object.
<p>An <a>event</a> has an associated <dfn export for=Event>target</dfn> (a
<a>potential event target</a>). Unless stated otherwise it is null.
<p>An <a>event</a> has an associated <dfn export for=Event>relatedTarget</dfn> (a
<a>potential event target</a>). Unless stated otherwise it is null.
<p class=note>Other specifications use <a for=Event>relatedTarget</a> to define a
<code>relatedTarget</code> attribute. [[UIEVENTS]]
<p>An <a>event</a> has an associated <dfn export for=Event>touch target list</dfn> (a
<a for=/>list</a> of zero or more <a>potential event targets</a>). Unless stated otherwise it is the
empty list.
<p class=note>The <a for=Event>touch target list</a> is for the exclusive use of defining the
{{TouchEvent}} interface and related interfaces. [[TOUCH-EVENTS]]
<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), an
<dfn for=Event/path>item-in-shadow-tree</dfn> (a boolean), a <dfn for=Event/path>target</dfn> (a
<a>potential event target</a>), a
<dfn id=event-path-relatedtarget for=Event/path>relatedTarget</dfn> (a
<a>potential event target</a>), a <dfn for=Event/path>touch target list</dfn> (a <a for=/>list</a>
of <a>potential event targets</a>), a <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>
<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> (its
<a for=Event>target</a>).
<dt><code><var>event</var> . {{Event/currentTarget}}</code>
<dd>Returns the object whose <a>event listener</a>'s <a for="event listener">callback</a> 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 for=/>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 <a for=Event>target</a>'s <a for=tree>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 <a for=Event>target</a>, 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>
<p>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.
<p>The <dfn attribute for=Event><code>target</code></dfn> attribute's getter, when invoked, must
return the <a>context object</a>'s <a for=Event>target</a>.
<p>The <dfn attribute for=Event><code>srcElement</code></dfn> attribute's getter, when invoked, must
return the <a>context object</a>'s <a for=Event>target</a>.
<p>The <dfn attribute for=Event><code>currentTarget</code></dfn> attribute must return the value it
was initialized to. When an <a>event</a> is created the attribute 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 the <a>context object</a>'s {{Event/currentTarget}}
attribute value.
<li><p>Let <var>reversedPath</var> be the <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>
<p>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 <a for=Event>target</a>.
<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
<a for=Event>target</a>.
<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 <a for=Event>target</a>.
</dl>
<p>Initially the attribute must be initialized to {{Event/NONE}}.
<hr>
<p>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, when invoked,
must return true if the <a>context object</a>'s <a>stop propagation flag</a> is set, and false
otherwise.
<p>The {{Event/cancelBubble}} attribute's setter, when invoked, must set the <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 the <a>context object</a>'s <a>stop propagation flag</a> and the <a>context object</a>'s
<a>stop immediate propagation flag</a>.</p>
<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.
<p>To <dfn>set the canceled flag</dfn>, given an <a>event</a> <var>event</var>, if
<var>event</var>'s {{Event/cancelable}} attribute value is true and <var>event</var>'s
<a>in passive listener flag</a> is unset, then set <var>event</var>'s <a>canceled flag</a>, and do
nothing otherwise.
<p>The <dfn attribute for=Event><code>returnValue</code></dfn> attribute's getter, when invoked,
must return false if <a>context object</a>'s <a>canceled flag</a> is set, and true otherwise.
<p>The {{Event/returnValue}} attribute's setter, when invoked, must <a>set the canceled flag</a>
with the <a>context object</a> if the given value is false, and do nothing otherwise.
<p>The <dfn method for=Event><code>preventDefault()</code></dfn> method, when invoked, must
<a>set the canceled flag</a> with the <a>context object</a>.
<p class="note no-backref">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, when
invoked, must return true if the <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, when invoked, must
return true if the <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.
<p>The <dfn attribute for=Event><code>timeStamp</code></dfn> attribute must return the value it was
initialized to.
<hr>
<p>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><p>Set <var>event</var>'s <a>initialized flag</a>.
<li><p>Unset <var>event</var>'s <a>stop propagation flag</a>,
<a>stop immediate propagation flag</a>, and <a>canceled flag</a>.
<li><p>Set <var>event</var>'s {{Event/isTrusted}} attribute to false.
<li><p>Set <var>event</var>'s <a for=Event>target</a> to null.
<li><p>Set <var>event</var>'s {{Event/type}} attribute to <var>type</var>.
<li><p>Set <var>event</var>'s {{Event/bubbles}} attribute to <var>bubbles</var>.
<li><p>Set <var>event</var>'s {{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 the <a>context object</a>'s <a>dispatch flag</a> is set, then return.
<li><p><a>Initialize</a> the <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-window-extensions>Legacy extensions to the {{Window}} interface</h3>
<pre class=idl>
partial interface Window {
[Replaceable] readonly attribute any event; // historical
};
</pre>
<p>Each {{Window}} object has an associated <dfn for=Window>current event</dfn> (undefined or an
{{Event}} object). Unless stated otherwise it is undefined.
<p>The <dfn attribute for=Window><code>event</code></dfn> attribute's getter, when invoked, must
return the <a>context object</a>'s <a for=Window>current event</a>.
<p class=note>Web developers are strongly encouraged to instead rely on the {{Event}} object passed
to event listeners, as that will result in more portable code. This attribute is not available in
workers or worklets, and is inaccurate for events dispatched in <a>shadow trees</a>.
<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>
<p>The <dfn attribute for=CustomEvent><code>detail</code></dfn> attribute must return the value it
was initialized to.
<p>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><p>If the <a>context object</a>'s <a>dispatch flag</a> is set, then return.
<li><p><a>Initialize</a> the <a>context object</a> with <var>type</var>, <var>bubbles</var>, and
<var>cancelable</var>.
<li><p>Set the <a>context object</a>'s {{CustomEvent/detail}} attribute to <var>detail</var>.
</ol>
<h3 id=constructing-events>Constructing events</h3>
<a lt="Other applicable specifications">Specifications</a> may define
<dfn export id=concept-event-constructor-ext>event constructing steps</dfn> for all or some
<a for=/>events</a>. The algorithm is passed an <var>event</var> as indicated in the
<a>inner event creation steps</a>.
<p class=note>This construct can be used by {{Event}} subclasses that have a more complex structure
than a simple 1:1 mapping between their initializing dictionary members and IDL attributes.
<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>Let <var>event</var> be the result of running the <a>inner event creation steps</a> with
this interface, null, now, and <var>eventInitDict</var>.
<li><p>Initialize <var>event</var>'s {{Event/type}} attribute to <var>type</var>.
<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>If <var>realm</var> is not given, then set it to null.
<li>
<p>Let <var>dictionary</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.)
<p class="XXX">This does not work if members are required; see
<a href="https://github.com/whatwg/dom/issues/600">whatwg/dom#600</a>.
<li>
<p>Let <var>event</var> be the result of running the <a>inner event creation steps</a> with
<var>eventInterface</var>, <var>realm</var>, the time of the occurrence that the event is
signaling, and <var>dictionary</var>.
<p class=example id=example-timestamp-initialization>In macOS the time of the occurrence for input
actions is available via the <code>timestamp</code> property of <code>NSEvent</code> objects.
<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>
<p>The <dfn noexport>inner event creation steps</dfn>, given an <var>interface</var>,
<var>realm</var>, <var>time</var>, and <var>dictionary</var>, are as follows:</p>
<ol>
<li>
<p>Let <var>event</var> be the result of creating a new object using <var>eventInterface</var>. If
<var>realm</var> is non-null, then 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>Initialize <var>event</var>'s {{Event/timeStamp}} attribute to a {{DOMHighResTimeStamp}}
representing the high resolution time from the <a>time origin</a> to <var>time</var>.
<p class=warning>User agents should set a minimum resolution of <var>event</var>'s
{{Event/timeStamp}} attribute to 5 microseconds following the existing <a>clock resolution</a>
recommendation. [[!HR-TIME]]
<li><p><a for=map>For each</a> <var>member</var> → <var>value</var> in <var>dictionary</var>, if
<var>event</var> has an attribute whose <a spec=webidl>identifier</a> is <var>member</var>, then
initialize that attribute to <var>value</var>.
<li><p>Run the <a>event constructing steps</a> with <var>event</var>.
<li><p>Return <var>event</var>.
</ol>
<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>An {{EventTarget}} object represents a target to which an <a>event</a> can be <a>dispatched</a>
when something has occurred.
<p>Each {{EventTarget}} object has an associated <dfn for=EventTarget>event listener list</dfn> (a
<a for=/>list</a> of zero or more <a>event listeners</a>). It is initially the empty list.
<!-- Intentionally not exported. -->
<p>An <dfn export id=concept-event-listener>event listener</dfn> can be used to observe a specific
<a>event</a> and consists of:
<ul class="brief">
<li><dfn for="event listener">type</dfn> (a string)
<li><dfn for="event listener">callback</dfn> (null or an {{EventListener}} object)
<li><dfn for="event listener">capture</dfn> (a boolean, initially false)
<li><dfn for="event listener">passive</dfn> (a boolean, initially false)
<li><dfn for="event listener">once</dfn> (a boolean, initially false)
<li><dfn for="event listener">removed</dfn> (a boolean for bookkeeping purposes, initially false)
</ul>
<p class="note no-backref">Although <a for="event listener">callback</a> is an {{EventListener}}
object, an <a>event listener</a> is a broader concept as can be seen above.
<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 for=/>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]]
<p>An {{EventTarget}} object <var>target</var> is said to <dfn export>have a listener for</dfn>
<var>type</var> if <var>target</var>'s <a>event listener list</a> <a for=list>contains</a> an
<a>event listener</a> whose <a for="event listener">type</a> is <var>type</var>.