forked from WICG/webcomponents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1648 lines (1405 loc) · 94.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Shadow DOM</title>
<script src='./autolink-config.js' class='remove'></script>
<script src='../../assets/scripts/autolink.js' class='remove'></script>
<link rel="stylesheet" href="../../assets/styles/respec-complement.css" type="text/css" />
<script src='//www.w3.org/Tools/respec/respec-w3c-common' async class='remove'></script>
<script class='remove'>
var respecConfig = {
specStatus: "ED",
shortName: "shadow-dom",
editors: [{ name: "Dimitri Glazkov", url: "mailto:[email protected]", company: "Google, Inc." },
{ name: "Hayato Ito", url: "mailto:[email protected]", company: "Google, Inc." }],
wg: "W3C Web Applications (WebApps)",
wgURI: "http://www.w3.org/2008/webapps/",
wgPublicList: "public-webapps",
wgPatentURI: "",
edDraftURI: "http://w3c.github.io/webcomponents/spec/shadow/",
otherLinks: [
{
key: "Revision history",
href: "https://github.com/w3c/webcomponents/commits/gh-pages/spec/shadow/"
}
],
preProcess: [resolveAutolink],
localBiblio: {
"EDITING": {
title: "HTML Editing APIs",
href: "https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html",
authors: ["Aryeh Gregor"]
}
}
};
var bugAssistConfig = {
blocked: '14978',
short_desc: '[Shadow]: ',
product: 'WebAppsWG',
component: 'Component Model'
};
</script>
<script src="../../assets/scripts/bug-assist.js" class="remove"></script>
</head>
<body>
<section id='abstract'>
<p>
This specification describes a method of combining multiple DOM trees into one hierarchy and how these trees interact with each other within a document, thus
enabling better composition of the DOM.
</p>
</section>
<section id='sotd'>
</section>
<section>
<h2>Conformance</h2>
<p>All diagrams, examples, notes, are non-normative, as well as sections explicitly marked as non-normative. Everything else in this specification is normative.</p>
<p>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in [[!RFC2119]]. For readability, these words do not appear in all uppercase letters in this specification.</p>
<p>To help with layering and to avoid circular dependencies between various parts of specification, this document consists of three consecutive narratives:</p>
<ol>
<li>setting up the stage for the specification,</li>
<li>explaining of the conceptual model and algorithms behind it, and</li>
<li>expressing this model with DOM interfaces and HTML elements.</li>
</ol>
<p>In a sense, these parts can be viewed as <em>math</em>, which sets up the reasoning environment, <em>physics</em>, which is the theoretical reasoning about the concept, and <em>mechanics</em>, which is the practical application of this reasoning.</p>
<p>Any point, at which a conforming UA must make decisions about the state or reaction to the state of the conceptual model, is captured as <a href="http://en.wikipedia.org/wiki/Algorithm">algorithm</a>. The algorithms are defined in terms of processing equivalence. The <dfn>processing equivalence</dfn> is a constraint imposed on the algorithm implementors, requiring the output of the both UA-implemented and the specified algorithm to be exactly the same for all inputs.</p>
</section>
<section>
<h2>Concepts</h2>
<section class="informative">
<h3>Introduction</h3>
<p>
See the <a href="http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom/">Shadow DOM 101</a> as a non-normative introduction.
</p>
</section>
<section>
<h3>Shadow trees</h3>
<p>A <dfn>document tree</dfn> is a <a>node tree</a> [[!DOM]] whose <a>root</a> <a>node</a> is a <a>document</a>.</p>
<p>Any element can have an <dfn title="shadow roots list">associated ordered list</dfn> of zero or more <a title="node tree">node trees</a>.</p>
<p>An element <dfn>hosts</dfn> a <a>node tree</a> if the <a>node tree</a> is a member of this associated list.</p>
<p>A <dfn>shadow host</dfn> is an element that <a>hosts</a> one or more <a title="node tree">node trees</a>.</p>
<p>A <dfn>shadow tree</dfn> is a <a>node tree</a> <a title="hosts">hosted</a> by a <a>shadow host</a>.</p>
<p>A shadow tree has an associated flag, called an <dfn>encapsulation mode</dfn>, which is either <dfn>open</dfn> or <dfn>closed</dfn>.
<p class="note">
The behavior of a <a>closed</a> shadow tree is not well defined in this spec yet. The status is tracked in <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=27775">W3C bug 27775</a>.
Unless otherwise mentioned, an encapsulation mode of a shadow tree is <a>open</a>.
The default-to-open is still under discussion and it may change. The status is tracked in <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=20144">W3C bug 20144</a>.
</p>
<p>A <dfn>shadow root</dfn> is the <a>root</a> <a>node</a> of a shadow tree.</p>
<p>If more than one <a>shadow tree</a> is <a title="hosts">hosted</a> by the same <a>shadow host</a>, the more recently added <a>shadow tree</a> is called the <dfn>younger shadow tree</dfn> and the less recently added <a>shadow tree</a> is called the <dfn>older shadow tree</dfn>.</p>
<p>If there is no <a>older shadow tree</a> than a given <a>shadow tree</a>,
the <a>shadow tree</a> is called the <dfn>oldest shadow tree</dfn>.</p>
<p>If there is no <a>younger shadow tree</a> than a given <a>shadow tree</a>,
the <a>shadow tree</a> is called the <dfn>youngest shadow tree</dfn>.</p>
<p>The <dfn>older shadow root</dfn> is the root node of the <a>older shadow tree</a>.</p>
<p>The <dfn>younger shadow root</dfn> is the root node of the <a>younger shadow tree</a>.</p>
<p>The <dfn>oldest shadow root</dfn> is the root node of the <a>oldest shadow tree</a>.</p>
<p>The <dfn>youngest shadow root</dfn> is the root node of the <a>youngest shadow tree</a>.</p>
<p>
A node <var>A</var> is called a <dfn>child or hosted shadow root</dfn> of a node <var>B</var>,
if either <var>A</var> is a child of <var>B</var> or A is the <a>root</a> node of the <a>shadow tree</a> that B <a>hosts</a>.
</p>
<p>
A node <var>A</var> is called a <dfn>deep descendant</dfn> of a node <var>B</var>,
if either <var>A</var> is a <a>child or hosted shadow root</a> of <var>B</var>
or <var>A</var> is <a>child or hosted shadow root</a> of a node <var>C</var> that is a <a>deep descendant</a> of <var>B</var>.
</p>
<p>
An <dfn>inclusive deep descendant</dfn> is a node or one of its <a title="deep descendant">deep descendants</a>.
</p>
<p>
When an element is an <a>inclusive deep descendant</a> of a <a>document element</a>, it is <dfn>in a document deeply</dfn>.
</p>
</section>
<section>
<h3>Trees of trees</h3>
<p>A <dfn>tree of trees</dfn> is a <a>tree</a> of <a title="node tree">node trees</a>.</p>
<p class="note">
The purpose of introducing a tree of trees here is to define algorithms easily in the following sections.
This is a kind of a notation techchique to make the this specification simpler.
</p>
<p>Just like a <a>node tree</a> is defined as <a title="tree">a set of relationships</a> between <a title="node">nodes</a>,
a <a>tree of trees</a> is similarly defined as a set of relationships between <a title="node tree">node trees</a>:</p>
<ul>
<li>
A <a>node tree</a> <var>A</var> is called a <dfn>parent tree</dfn> of a <a>node tree</a> <var>B</var> if either of the following conditions is satisfied:
<ul>
<li><var>A</var> and <var>B</var> are next to each other in the same <a title="shadow roots list">associated ordered list</a> and <var>A</var> is the <a>older shadow tree</a> relative to <var>B</var>.</li>
<li><var>B</var> is the <a>oldest shadow tree</a> and <a>shadow host</a> of <var>B</var> participates in <var>A</var>.</li>
</ul>
</li>
<li>If there is more than one <a>node tree</a> that shares the same <a>parent tree</a>, the <a>tree order</a> between them in the <a>tree of trees</a> is defined as follows:
<ol>
<li>Let <var>A</var> and <var>B</var> be <a title="node tree">node trees</a> that share the same <a>parent tree</a>.</li>
<li><var>A</var> comes before <var>B</var> if either of the following conditions is satisfied:
<ul>
<li>The <a>shadow host</a> which <a>hosts</a> <var>A</var> is <a>preceding</a> the <a>shadow host</a> which <a>hosts</a> <var>B</var>.</li>
<li><var>B</var> is not the <a>oldest shadow tree</a>.</li>
</ul>
</li>
</ol>
</li>
<li>Other relationships and terms, such as <dfn>root tree</dfn>, <dfn>child tree</dfn>,
<dfn>descendant tree</dfn>, <dfn>inclusive descendant tree</dfn>,
<dfn>ancestor tree</dfn>, <dfn>inclusive ancestor tree</dfn>,
<dfn>preceding tree</dfn>
are defined in the similar way as defined in <a title="tree">trees</a>.</li>
</ul>
<p>The <a><code>ownerDocument</code></a> property of a <a>node</a> in a shadow tree <strong>must</strong> refers to the <a>document</a> of the <a>shadow host</a> which <a>hosts</a> the shadow tree.</p>
<p><a><code>Window</code></a> object <a title="named access on the window object">named properties</a> [[!HTML]] <strong>must</strong> access the <a title="node">nodes</a> in the <a>document tree</a>.</p>
<section class="informative">
<h3>Example tree of trees</h3>
<figure>
<object data="../../assets/images/tree-of-trees.svg" width="650" height="823"></object>
<figcaption>
A tree of trees.
</figcaption>
</figure>
<p>
In the figure, there are six node trees, named A, B, C, D, E and F.
The node trees, C, D and E, are hosted by the same <a>shadow host</a>, which participates in the node tree A.
The node tree C is the <a>oldest shadow tree</a>. The node tree E is the <a>youngest shadow tree</a>.
The following set of relationships holds in the figure:
</p>
<ul>
<li>The ordered list of A's <a title="child tree">child trees</a> is [B, C].</li>
<li>The ordered list of B's <a title="child tree">child trees</a> is [].</li>
<li>The ordered list of C's <a title="child tree">child trees</a> is [F, D].</li>
<li>The ordered list of D's <a title="child tree">child trees</a> is [E].</li>
<li>The ordered list of E's <a title="child tree">child trees</a> is [].</li>
</ul>
<div class="note">
<p>
As for a relationship between nodes, it's worth mentioning that there is no <a>ancestor</a>/<a>descendant</a> relationships between two nodes if they participate in different node trees.
A shadow root is not a <a>child</a> node of the shadow host. The <a>parent</a> node of a <a>shadow root</a> doesn't exist.
Because of this nature, most of existing APIs are <strong>scoped</strong> and don't affect other node trees, even though they are forming one tree of trees.
For example, <a title="getElementById"><code>document.getElementById(elementId)</code></a> never returns an element in a shadow tree,
even when the element has the given <code>elementId</code>.
</p>
<p>
The same thing also applies to CSS <a title="selectors">Selectors</a> matching.
For example, a <a title="descendant combinators">descendant combinator</a> never descends into a node in a <a title="child tree">child</a> <a>shadow tree</a>
because a <a>shadow root</a> is not a <a>child</a> node of the <a>shadow host</a>.
Unless a special CSS Selector for Shadow DOM, which is mentioned later, is used, a CSS Selector never matches an element in a different node tree.
</p>
</div>
<p class="note">
Because <code>ShadowRoot</code> inherits <code>DocumentFragment</code>, as <a href="#idl-def-ShadowRoot">specified</a> later,
you can use <code>ShadowRoot.getElementByID(elementId)</code> to get a node in the <a>shadow tree</a>.
</p>
</section>
</section>
<section>
<h3>Composed trees</h3>
<p>A <dfn>composed tree</dfn> is a <a>node tree</a> which is constructed out of <a title="node">nodes</a> from multiple <a title="node tree">node trees</a> in a <a>tree of trees</a>.
The exact algorithm of constructing a composed tree is specified later.</p>
<figure>
<object data="../../assets/images/composed-tree.svg" width="654" height="606"></object>
<figcaption>A composed tree</figcaption>
</figure>
<p>
If an element doesn't <a>participate</a> in a <a>composed tree</a> whose <a>root</a> node is a document, the element <strong>must</strong> not appear in the <a>formating structure</a> [[!CSS21]] nor create any CSS <a>box</a>.
This behavior <strong>must</strong> not be overridden by setting the 'display' property.
</p>
<p>
In resolving CSS <a>inheritance</a>, an element <strong>must</strong> inherit from the parent node in the <a>composed tree</a>, if applicable.
</p>
<div class="note">
<p>
The editor's draft of CSS Scoping specification [[css-scoping-1]] defines the selectors which are related to Shadow DOM.
For those who would search this Shadow DOM specifaction with the name of these selectors, mentioning these selectores here:
</p>
<ul>
<li><code>:shadow</code> pseudo element</li>
<li><code>/deep/</code> combinator, which was replaced with a <code>>>></code> combinator (or <strong>shadow piecing descendant combinator</strong>)</li>
<li><code>::content</code> pseudo-element</li>
<li><code>:host-context()</code> functional pseudo-class</li>
</ul>
</div>
</section>
</section>
<section>
<h2>Distributions</h2>
<section>
<h3>Insertion Points</h3>
<p>An <dfn>insertion point</dfn> is a defined location where <a title="node">nodes</a> in a different <a>node tree</a> appear instead of the nodes's original position when constructing a <a>composed tree</a>.</p>
<figure>
<object data="../../assets/images/distributions.svg" width="663" height="598"></object>
<figcaption>A distribution</figcaption>
</figure>
<p>A <dfn>distribution</dfn> is the mechanism that determines which <a title="node">nodes</a> appear at each <a>insertion point</a>. The exact algorithm of a <a>distribution</a> is specified later.</p>
</section>
<section>
<h3>Content Insertion Points</h3>
<p>A <dfn>content insertion point</dfn> is an <a>insertion point</a> to where the child <a title="node">nodes</a> of the <a>shadow host</a> are distributed.
The <a>content element</a> that satisfies all of the following conditions represents a <a>content insertion point</a>:</p>
<ul>
<li>The <a>root</a> <a>node</a> of the <a>content element</a> is a <a>shadow root</a></li>
<li>There is no other <a>content element</a> in the <a title="ancestor">ancestors</a> of the <a>content element</a></li>
<li>There is no <a>shadow element</a> in the <a title="ancestor">ancestors</a> of the <a>content element</a></li>
</ul>
</section>
<section>
<h3>Shadow Insertion Points</h3>
<p>A <dfn>shadow insertion point</dfn> is an <a>insertion point</a> to where the children of the <a>older shadow root</a> are distributed.
The <a>shadow element</a> that satisfies of the following conditions represents a <a>shadow insertion point</a>:</p>
<ul>
<li>The <a>root</a> <a>node</a> of the <a>shadow element</a> is a <a>shadow root</a></li>
<li>There is no other <a>shadow element</a> which is <a>preceding</a> the <a>shadow element</a></li>
<li>There is no other <a>content element</a> in the <a title="ancestor">ancestors</a> of the <a>shadow element</a></li>
</ul>
</section>
<section>
<h3>Distribution Results</h3>
<p>Each <a>tree of trees</a> has the <dfn>distribution result</dfn> which describes the result of distributions.
The <a>distribution result</a> <strong>must</strong> be <a title="processing equivalence">equivalent</a> of the following:</p>
<ol>
<li>Each <a>insertion point</a> has an ordered list, called <dfn>distributed nodes</dfn>, which consists of <a title="node">nodes</a> which are distributed into the <a>insertion point</a>.</li>
<li>Each <a>node</a> that is not an <a>insertion point</a> has an ordered list, called <dfn>destination insertion points</dfn>, which consists of <a title="insertion point">insertion points</a> to where the <a>node</a> is distributed</li>
</ol>
<p>An <a>insertion point</a> <var>A</var> is the <dfn>final destination</dfn> of a <a>node</a> <var>B</var> if <var>A</var> is the last item of the <a>destination insertion points</a> of <var>B</var>.</p>
<p>When a <a>node</a> <var>A</var> is <dfn title="distributes">distributed</dfn> into an <a>insertion point</a> <var>B</var>, the following steps <strong>must</strong> happen:</p>
<ul>
<li>Add <var>A</var> to the <a>distributed nodes</a> of <var>B</var></li>
<li>Add <var>B</var> to the <a>destination insertion points</a> of <var>A</var></li>
</ul>
<div class="note">
<p>One case that deserves special consideration is the situation when an <a>insertion point</a> is a child <a>node</a> of another <a>shadow host</a>. In such situations, the <a title="node">nodes</a> <a title="distributes">distributed</a> into that <a>insertion point</a> appear as if they were child <a title="node">nodes</a> of the <a>shadow host</a> in the context of <a>distribution</a>. Thus, the <a title="node">nodes</a> <a title="distributes">distributed</a> to a <a>shadow tree</a> could have already been <a title="distributes">distributed</a> from its parent tree.</p>
<p>Despite being distributed to more than one insertion point, a node still only appears once in the composed tree at the final destination.</p>
</div>
<figure>
<object data="../../assets/images/re-distributions.svg" width="693" height="822"></object>
<figcaption>A re-distribution.
In the figure, a node <em>child 1</em> is distributed into <em>insertion point 1</em>. Then <em>child1</em> is re-distributed into <em>insertion point 3</em>.
The destination insertion points of <em>child 1</em> is [<em>insertion point 1</em>, <em>insertion point 3</em>] and <em>insertion point 3</em> is the final destination of <em>child 1</em>.
The distributed nodes of <em>insertion point 1</em> and <em>insertion point 3</em> is [<em>child 1</em>] and [<em>child 1</em>, <em>child 3</em>], respectively.
</figcaption>
</figure>
</section>
<section>
<h3>Distribution Algorithms</h3>
<p>The <dfn>distribution algorithm</dfn> <strong>must</strong> be used to determine the <a>distribution result</a> for a <a>tree of trees</a> and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>TREE-OF-TREES</var>, a <a>tree of trees</a></dd>
<dt>Output</dt>
<dd>The <a>distribution result</a> of <var>TREE-OF-TREES</var> is updated</dd>
</dl>
<ol>
<li>Let all <a>distributed nodes</a> and <a>destination insertion points</a> owned by <a title="node">nodes</a> in <var>TREE-OF-TREES</var> be empty</li>
<li>Let <var>ROOT-TREE</var> be the <a>root tree</a> of <var>TREE-OF-TREES</var></li>
<li>Run the <a>distribution resolution algorithm</a> with <var>ROOT-TREE</var> as input</li>
</ol>
</div>
<p>The <dfn>distribution resolution algorithm</dfn> <strong>must</strong> be used to determine the <a>distribution result</a> for a given <a>node tree</a> and its <a title="descendant tree">descendant trees</a>, and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>NODE-TREE</var>, a <a>node tree</a></dd>
<dt>Output</dt>
<dd>The <a>distribution result</a> is updated for the <a title="inclusive descendant tree">inclusive descendant trees</a> of <var>NODE-TREE</var></dd>
</dl>
<ol>
<li>For each <a>shadow host</a>, <var>SHADOW-HOST</var>, which participates in <var>NODE-TREE</var>, in <a>tree order</a>:
<ol>
<li>Let <var>POOL</var> be the result of the <a>pool population algorithm</a> with <var>SHADOW-HOST</var> as input</li>
<li>For each <a>shadow tree</a>, <var>SHADOW-TREE</var>, which <var>SHADOW-HOST</var> <a>hosts</a>, in order from the <a>youngest shadow tree</a> to the <a>oldest shadow tree</a>:
<ol>
<li>Run the <a>pool distribution algorithm</a> with <var>SHADOW-TREE</var> and <var>POOL</var> as input</li>
</ol></li>
<li>For each <a>shadow tree</a>, <var>SHADOW-TREE</var>, that <var>SHADOW-HOST</var> <a>hosts</a>, in order from the <a>oldest shadow tree</a> to the <a>youngest shadow tree</a>:
<ol>
<li>Let <var>SHADOW</var> be the <a>shadow insertion point</a> which participates in <var>SHADOW-TREE</var></li>
<li>If such a <var>SHADOW</var> exits:
<ol>
<li>If <var>SHADOW-TREE</var> is not the <a>oldest shadow tree</a>:
<ol>
<li>Let <var>POOL</var> be the result of the <a>pool population algorithm</a> with the root node of the <a>older shadow tree</a> relative to <var>SHADOW-TREE</var> as input.</li>
</ol></li>
<li>For each <a>node</a>, <var>CHILD</var>, in <var>POOL</var>
<ol>
<li><a title="distributes">Distribute</a> <var>CHILD</var> into <var>SHADOW</var></li>
</ol></li>
</ol></li>
<li>Run the <a>distribution resolution algorithm</a>, recursively, with <var>SHADOW-TREE</var> as input</li>
</ol></li>
</ol></li>
</ol>
</div>
<p class="note">
The algorithm implies that a <a>shadow insertion point</a> can be used in the <a>oldest shadow tree</a>,
where a <a>shadow insertion point</a> behaves in a similar way to a <a>content insertion point</a>.
</p>
<p>The <dfn>pool population algorithm</dfn> <strong>must</strong> be used to populate <a title="node">nodes</a> from the child <a title="node">nodes</a> of a given <a>node</a> and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>NODE</var>, a node</dd>
<dt>Output</dt>
<dd><var>POOL</var>, an ordered list of nodes</dd>
</dl>
<ol>
<li>Let <var>POOL</var> be an empty ordered list.</li>
<li>For each child <a>node</a>, <var>CHILD</var>, of <var>NODE</var>:
<ol>
<li>If <var>CHILD</var> is an <a>insertion point</a>:
<ol>
<li>Add all <a title="node">nodes</a> in the <a>distributed nodes</a> of <var>CHILD</var> to <var>POOL</var></li>
</ol></li>
<li>Otherwise:
<ol>
<li>Add <var>CHILD</var> to <var>POOL</var></li>
</ol></li>
</ol></li>
</ol>
</div>
<p>The <dfn>pool distribution algorithm</dfn> <strong>must</strong> be used to distribute <a title="node">nodes</a> in a pool into the <a title="content insertion point">content insertion points</a> in a <a>shadow tree</a> and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>SHADOW-TREE</var>, a shadow tree</dd>
<dd><var>POOL</var>, an ordered list of nodes</dd>
<dt>Output</dt>
<dd>Nodes in POOL are distributed into the content insertion points in the tree.</dd>
</dl>
<ol>
<li>For each <a>content insertion point</a>, <var>CONTENT</var>, which participates in <var>SHADOW-TREE</var>, in tree order:
<ol>
<li>For each <a>node</a>, <var>NODE</var>, in <var>POOL</var>
<ol>
<li>If <var>NODE</var> satisfies <var>CONTENT</var>'s matching criteria:
<ol>
<li><a title="distributes">Distribute</a> <var>NODE</var> into <var>CONTENT</var></li>
<li>Remove <var>NODE</var> from <var>POOL</var></li>
</ol></li>
</ol></li>
<li>If no <a title="node">nodes</a> are distributed to <var>CONTENT</var>:
<ol>
<li>For each child, <var>CHILD</var>, of <var>CONTENT</var>
<ol>
<li><a title="distributes">Distribute</a> <var>CHILD</var> into <var>CONTENT</var></li>
</ol></li>
</ol></li>
</ol></li>
</ol>
</div>
<p class="note">If no nodes are distributed into a <a>content insertion point</a> <var>CONTENT</var>, the child <a title="node">nodes</a> of <var>CONTENT</var> are distributed into <var>CONTENT</var> as fallback nodes.</p>
<p>If any condition which affects the <a>distribution result</a> changes, the <a>distribution result</a> <strong>must</strong> be updated before any use of the <a>distribution result</a>.</p>
</section>
<section>
<h3>Satisfying Matching Criteria</h3>
<p>The <dfn>matching criteria</dfn> for an <a>insertion point</a> is a set of <a title="compound selector">compound selectors</a> [[!SELECTORS4]]. These <a title="compound selector">compound selectors</a> are restricted to contain only these <a title="simple selector">simple selectors</a>:</p>
<ul>
<li>A <a>type selector</a> or a <a>universal selector</a></li>
<li><a title="class selector">class selector(s)</a></li>
<li>An <a>ID selector</a></li>
<li><a title="attribute selector">attribute selector(s)</a></li>
<li>A <a>negation pseudo-class</a>, <code>:not()</code></li>
</ul>
<p>A <a>node</a> <dfn title="satisfies-matching-criteria">satisfies</dfn> a <a>matching criteria</a> only if:</p>
<ol>
<li>all <a title="compound selector">compound selectors</a> in the set, contain only the <a title="simple selector">simple selectors</a> specified above; and</li>
<li>a <a>node</a> matches at least one <a title="compound selector">compound selectors</a> in the set or the set is empty.</li>
</ol>
</section>
</section>
<section>
<h2>Composition</h2>
<p>The <dfn>composed tree children calculation algorithm</dfn> <strong>must</strong> be used to determine the child <a title="node">nodes</a> of a <a>node</a> in the <a>composed tree</a> and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>NODE</var>, a <a>node</a> which participates in a composed tree</dd>
<dt>Output</dt>
<dd><var>CHILDREN</var>, the child nodes of <var>NODE</var> in the <a>composed tree</a>.</dd>
</dl>
<ol>
<li>Let <var>CHILDREN</var> be an empty ordered list of nodes</li>
<li>If <var>NODE</var> is a <a>shadow host</a>:
<ol>
<li>Let <var>CHILD-POOL</var> be the children of the <a>youngest shadow root</a> which <var>NODE</var> <a>hosts</a>.</li>
</ol></li>
<li>Otherwise:
<ol>
<li>Let <var>CHILD-POOL</var> be the child <a title="node">nodes</a> of NODE</li>
</ol></li>
<li>For each <a>node</a>, <var>CHILD</var>, in <var>CHILD-POOL</var>:
<ol>
<li>If <var>CHILD</var> is an <a>insertion point</a>:
<ol>
<li>For each <a>node</a>, <var>DISTRIBUTED-NODE</var>, in the <a>distributed nodes</a> of the <a>insertion point</a> <var>CHILD</var>:
<ol>
<li>If <var>CHILD</var> is the <a>final destination</a> of <var>DISTRIBUTED-NODE</var>, add <var>DISTRIBUTED-NODE</var> to <var>CHILDREN</var></li>
</ol></li>
</ol></li>
<li>Otherwise:
<ol>
<li>Add <var>CHILD</var> to <var>CHILDREN</var></li>
</ol></li>
</ol></li>
</ol>
</div>
<p>For a given <a>tree of trees</a> <var>TREE-OF-TREES</var>, the <a>composed tree</a> constructed from <var>TREE-OF-TREES</var> <strong>must</strong> be <a title="processing equivalence">equivalent</a> to the following tree:</p>
<ul>
<li>The <a>root</a> <a>node</a> of the <a>composed tree</a> is the <a>root</a> <a>node</a> of the <a>root tree</a> of <var>TREE-OF-TREES</var>.</li>
<li>For a given <a>node</a> which <a>participates</a> in the <a>composed tree</a>, the child <a title="node">nodes</a> of the <a>node</a> is the result of the <a>composed tree children calculation algorithm</a> with the <a>node</a> as input.
</li>
</ul>
</section>
<section>
<h2>Events</h2>
<p>In each algorithm in this section, the <a>Window</a> <strong>must</strong> be considered as if it were the parent node of the <a>Document</a> so that the <a>Window</a> also receives an <a>event</a>.</p>
<p>When an <a>event</a> is <a title="event dispatch">dispatched</a> in a <a>shadow tree</a>, its path either crosses the <a title="shadow tree">shadow trees</a> or is terminated at the <a>shadow root</a>. One exception are the <a title="mutation event">mutation events</a>. The <a title="mutation event">mutation event types</a> <strong>must</strong> never be dispatched in a <a>shadow tree</a>.</p>
<section>
<h3>Events that are Always Stopped</h3>
<p>The <dfn title="events-always-stopped">following events</dfn> <strong>must</strong> always be stopped
right before the <a>shadow host</a> that <a>hosts</a> the target node:</p>
<ul>
<li><code>abort</code></li>
<li><code>error</code></li>
<li><code>select</code></li>
<li><code>change</code></li>
<li><code>load</code></li>
<li><code>reset</code></li>
<li><code>resize</code></li>
<li><code>scroll</code></li>
<li><code>selectstart</code></li>
</ul>
<p><a title="event path calculation algorithm">The exact algorithm</a> is specified later.</p>
<p class="issue">
It's not a good idea to have a fixed list of events. Alternative ideas are being discussed in <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=20247">W3C bug 20247</a>.
</p>
</section>
<section>
<h3>Event Paths</h3>
<p>The <dfn>event path calculation algorithm</dfn> must be used to determine event path and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>NODE</var>, a node</dd>
<dd><var>EVENT</var>, an event</dd>
<dt>Output</dt>
<dd><var>PATH</var>, an event path, a ordered list of an event target</dd>
</dl>
<ol>
<li>Let <var>PATH</var> be the empty ordered list of nodes</li>
<li>Let <var>INSERTION-POINT-STACK</var> be an empty stack of nodes
<li>Let <var>CURRENT</var> be <var>NODE</var></li>
<li>Repeat while <var>CURRENT</var> exists:
<ol>
<li>Add <var>CURRENT</var> to <var>PATH</var></li>
<li>Let <var>INSERTION-POINTS</var> be the <a>destination insertion points</a> of <var>CURRENT</var>
<li>Exclude <a>shadow insertion point</a>s whose containing shadow root has <a>older shadow root</a>s from <var>INSERTION-POINTS</var>
<li>If <var>INSERTION-POINTS</var> is not empty:
<ol>
<li>Push <var>INSERTION-POINTS</var> into <var>INSERTION-POINT-STACK</var> in order of first destination to final destination
<li>Pop <var>INSERTION-POINT-STACK</var> and set <var>CURRENT</var> to be the popped node.</li>
</ol>
<li>Otherwise if <var>CURRENT</var> is a <a>shadow root</a>:
<ol>
<li>Let <var>SHADOW-HOST</var> be the <a>shadow host</a> which <a>hosts</a> <var>CURRENT</var></li>
<li>If <var>INSERTION-POINT-STACK</var> is not empty and
the most recent node in the <var>INSERTION-POINT-STACK</var> is in the same node tree as <var>SHADOW-HOST</var>
<ol>
<li>Pop <var>INSERTION-POINT-STACK</var> and set <var>CURRENT</var> to be the popped node.</li>
</ol>
<li>Otherwise if <var>CURRENT</var> has a <a>younger shadow root</a> and it has a <a>shadow insertion point</a>:
<ol>
<li>Let <var>CURRENT</var> be the <a>shadow insertion point</a></li>
</ol>
<li>Otherwise if <var>SHADOW-HOST</var> <a>hosts</a> the <a>node tree</a> which <var>NODE</var> <a>participates</a> in and <var>EVENT</var> is one of the <a title="events-always-stopped">events which must be stopped</a>:
<ol>
<li>Stop this algorithm</li>
</ol></li>
<li>Otherwise:
<ol>
<li>Let <var>CURRENT</var> be <var>SHADOW-HOST</var></li>
</ol>
</ol></li>
<li>Otherwise:
<ol>
<li>Let <var>CURRENT</var> be the <a>parent</a> <a>node</a> of <var>CURRENT</var></li>
</ol></li>
</ol></li>
</ol>
</div>
</section>
<section class="informative">
<h3>Event Paths Example</h3>
<p>
Suppose we have the following tree of trees:
</p>
<figure>
<object data="../../assets/images/event-path-tree-of-trees.svg" width="301" height="1074"></object>
<figcaption>An example tree of trees. Nodes which are not involved in the example event path, which is explained later, are omitted.</figcaption>
</figure>
<ul>
<li>
<code>A</code> is a <a>document</a>.
</li>
<li>
<code>E</code>, <code>J</code>, <code>N</code>, <code>Q</code>, <code>S</code> and <code>V</code> are <a title="shadow root">shadow roots</a>.
</li>
<li>
<code>I</code>, <code>M</code>, <code>P</code>, <code>R</code> and <code>U</code> are <a title="content insertion point">content insertion points</a>.
</li>
<li>
<code>X</code> is a <a>shadow insertion point</a>.
</li>
</ul>
<p>
This <a>tree of trees</a> has the following seven <a title="node tree">node trees</a>, one <a>document tree</a> and six <a title="shadow tree">shadow trees</a>:
</p>
<ul>
<li>
The <var>document tree 1</var>. Node <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code> participate in that.
</li>
<li>
The <var>shadow tree 2</var> hosted by <code>B</code>. Node <code>E</code>, <code>F</code>, <code>G</code>, <code>H</code> and <code>I</code> participate in that.
</li>
<li>
The <var>shadow tree 3</var> hosted by <code>H</code>. Node <code>J</code>, <code>K</code>, <code>L</code> and <code>M</code> participate in that.
</li>
<li>
The <var>shadow tree 4</var> hosted by <code>K</code>. Node <code>N</code>, <code>O</code> and <code>P</code> participate in that.
</li>
<li>
The <var>shadow tree 5</var> hosted by <code>O</code>. Node <code>Q</code> and <code>R</code> participate in that.
</li>
<li>
The <var>shadow tree 6</var> hosted by <code>F</code>. Node <code>S</code>, <code>T</code> and <code>U</code> participate in that.
</li>
<li>
The <var>shadow tree 7</var> hosted by <code>B</code>. Node <code>V</code>, <code>W</code> and <code>X</code> participate in that.
This <a>shadow tree</a> is younger than the <var>shadow tree 2</var>.
</li>
</ul>
<p>
Let's assume that the <a>distribution result</a> of this <a>tree of trees</a> is:
</p>
<ul>
<li>
The <a>destination insertion points</a> of <code>C</code> are <code>[I, M]</code> (<code>C</code> is re-distributed)
</li>
<li>
The <a>destination insertion points</a> of <code>L</code> are <code>[P, R]</code> (<code>L</code> is re-distributed)
</li>
<li>
The <a>destination insertion points</a> of <code>G</code> are <code>[U]</code>
</li>
<li>
The <a>destination insertion points</a> of <code>F</code> are <code>[X]</code>
</li>
</ul>
<p>
In this case, if an event is dispatched on node <code>D</code>, the event path will be:
</p>
<p>
<code>
[D, C, M, L, R, Q, P, O, N, K, J, I, H, G, U, T, S, F, E, X, W, V, B, A] (<a>Window</a> is omitted)
</code>
</p>
<p>
Note that the <a>event path calculation algorithm</a> is designed to achieve the following goals:
</p>
<ol>
<li>
If there is a node, <var>CHILD</var>, in the event path and <var>CHILD</var> has a parent node, <var>PARENT</var>, in the node tree, the event path also includes <var>PARENT</var>.
<var>PARENT</var> always appears somewhere after <var>CHILD</var> in the event path.
</li>
<li>
Nodes in the event path form a <em>linear ancestor chain</em> in each <a>node tree</a>. There are no <em>branch points</em> in each <a>node tree</a>.
</li>
</ol>
<div class="issue">
Charts below are not updated yet to the new result in <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=23887">bug 23887</a>.
</div>
<figure>
<object data="../../assets/images/event-path-node-trees.svg" width="884" height="473">></object>
<figcaption>
The relationship between an event path and node trees. In the figure, a number shown in a left-side of each node represents a zero-based position of each node in the event path.
A parent node always have a larger number than that of its child node in each node tree.
</figcaption>
</figure>
<p>
That means if we focus on one <a>node tree</a> and forget all other <a title="node tree">node trees</a>,
the event path would be seen as if the event happened only on the <a>node tree</a> which we are focused on.
This is an important aspect in a sense that hosting shadow trees doesn't have any effect to the event path <em>within</em> the <a>node tree</a> the <a>shadow host</a> participate in
as long as the event is not stopped somewhere in the <a title="descendant tree">descendant trees</a>.
</p>
<p>
For example, from the view of the <var>document tree 1</var>, the event path would be seen as <code>[D, C, B, A]</code>.
From the view of the <var>shadow tree 2</var>, the event path would be seen as <code>[I, H, G, F, E]</code>.
The similar things also apply to other <a title="node tree">node trees</a>.
</p>
<p>
It is also worth pointing out that if we exclude all <a title="insertion point">insertion points</a> and <a title="shadow root">shadow roots</a> from an event path,
the result would be equivalent to the inclusive ancestors of the node on which the event is dispatched, in the composed tree.
</p>
<figure>
<object data="../../assets/images/event-path-and-composed-tree.svg" width="217" height="1723"></object>
<figcaption>
The relationship between an event path and the <a>composed tree</a>. The event path used in the example is shown in the left-hand side and the <a>composed tree</a> is shown in the right-hand side.
If we exclude all <a title="insertion point">insertion points</a> and <a title="shadow root">shadow roots</a> from the event path,
the result would be equivalent to the inclusive ancestors of the node, <code>D</code>, in the <a>composed tree</a>.
</figcaption>
</figure>
</section>
<section>
<h3>Event Retargeting</h3>
<p>In the cases where event path is across multiple node trees, the event's information about the target of the event is adjusted in order to maintain <a href="#encapsulation">encapsulation</a>. Event <dfn>retargeting</dfn> is a process of computing relative targets for each ancestor of the <a>node</a> at which the event is dispatched. A <dfn>relative target</dfn> is a <a>node</a> that most accurately represents the target of a dispatched event at a given ancestor while maintaining the encapsulation.</p>
<p>The <dfn>retargeting algorithm</dfn> is used to determine relative targets, and it <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>EVENT-PATH</var>, an event path</dd>
<dd><var>CURRENT-TARGET</var>, a <a>node</a> where the event listener is invoked.</dd>
<dt>Output</dt>
<dd><var>RELATIVE-TARGET</var>, adjusted target</dd>
</dl>
<ol>
<li>Let <var>CURRENT-TARGET-TREE</var> be the <a>node tree</a> which <var>CURRENT-TARGET</var> <a>participates</a> in</li>
<li>Let <var>ORIGINAL-TARGET</var> be the first item in <var>EVENT-PATH</var></li>
<li>Let <var>ORIGINAL-TARGET-TREE</var> be the <a>node tree</a> which <var>ORIGINAL-TARGET</var> <a>participates</a> in</li>
<li>Let <var>RELATIVE-TARGET-TREE</var> be the lowest common <a>inclusive ancestor tree</a> of <var>CURRENT-TARGET-TREE</var> and <var>ORIGINAL-TARGET-TREE</var></li>
<li>Let <var>RELATIVE-TARGET</var> be the first <a>node</a> in <var>EVENT-PATH</var> which satisfies the following condition:
<ol>
<li>The <a>node</a> <a>participates</a> in <var>RELATIVE-TARGET-TREE</var></li>
</ol>
</li>
</ol>
</div>
<p>The retargeting process <strong>must</strong> occur prior to dispatch of an event.</p>
</section>
<section>
<h3>Retargeting <code>relatedTarget</code></h3>
<p>Some events have a <a><code>relatedTarget</code></a> [[!DOM-Level-3-Events]] property, which holds a <a>node</a> that's not the event's target, but is related to the event.</p>
<p>For instance, a <code>mouseover</code> event's <a><code>relatedTarget</code></a> may hold the <a>node</a> from which the mouse has moved to event's <code>target</code>. In the case where <code>relatedTarget</code> is in a <a>shadow tree</a>, the conforming UAs <strong>must</strong> not leak its actual value outside of this tree. In cases where both <code>relatedTarget</code> and <code>target</code> are part of the same <a>shadow tree</a>, the conforming UAs <strong>must</strong> <em>stop</em> events at the shadow root to avoid the appearance of spurious <code>mouseover</code> and <code>mouseout</code> events firing from the same node.</p>
<p>Thus, if an event has a <code>relatedTarget</code>, its value and extent of event dispatch <strong>must</strong> be adjusted. In general:</p>
<ol>
<li>For a given node, the <code>relatedTarget</code> <strong>must</strong> be changed to its ancestor (or self) that is in the same <a>shadow tree</a> as the node</li>
<li>Event listeners <strong>must not</strong> be invoked on a <a>node</a> for which the <code>target</code> and <code>relatedTarget</code> are the same.</li>
</ol>
<p>The <dfn>related target resolution algorithm</dfn> <strong>must</strong> be used to determine the value of the <a><code>relatedTarget</code></a> property and <strong>must</strong> be <a title="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>EVENT</var>, an event</dd>
<dd><var>CURRENT-TARGET</var>, the <a>node</a> on which event listeners would be invoked</dd>
<dd><var>RELATED-TARGET</var>, the related target for the event</dd>
<dt>Output</dt>
<dd><var>ADJUSTED-RELATED-TARGET</var>, the <dfn>adjusted related target</dfn> for <var>CURRENT-TARGET</var></dd>
</dl>
<ol>
<li>Let <var>CURRENT-TARGET-TREE</var> be the <a>node tree</a> which <var>CURRENT-TARGET</var> <a>participates</a> in</li>
<li>Let <var>RELATED-TARGET-TREE</var> be the <a>node tree</a> which <var>RELATED-TARGET</var> <a>participates</a> in</li>
<li>Let <var>RELATED-TARGET-EVENT-PATH</var> be the result of the <a>event path calculation algorithm</a> with <var>RELATED-TARGET</var> and <var>EVENT</var> as input</li>
<li>If <var>CURRENT-TARGET-TREE</var> and <var>RELATED-TARGET-TREE</var> participate in the same <a>tree of trees</a>:
<ol>
<li>Let <var>LOWEST-COMMON-ANCESTOR-TREE</var> be the lowest common <a>inclusive ancestor tree</a> of <var>CURRENT-TARGET-TREE</var> and <var>RELATED-TARGET-TREE</var></li>
</ol>
</li>
<li>Otherwise:
<ol>
<li>Let <var>LOWEST-COMMON-ANCESTOR-TREE</var> be the <a>root tree</a> of <var>RELATED-TARGET-TREE</var></li>
</ol>
</li>
<li>For each <a>inclusive ancestor tree</a>, <var>COMMON-ANCESTOR-TREE</var>, of the <var>LOWEST-COMMON-ANCESTOR-TREE</var>, in ascending order:
<ol>
<li>Let <var>ADJUSTED-RELATED-TARGET</var> be the first <a>node</a> in <var>RELATED-TARGET-EVENT-PATH</var> which satisfies the following condition:
<ol>
<li>The <a>node</a> <a>participates</a> in <var>COMMON-ANCESTOR-TREE</var></li>
</ol>
</li>
<li>If such a <var>ADJUSTED-RELATED-TARGET</var> exists:
<ol>
<li>Stops this algorithm</li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
<p class="note">The result of the related target resolution algorithm is not always null. Unless that, you should file a bug for this specification.</p>
<p>The relatedTarget retargeting process <strong>must</strong> occur prior to dispatch of an event.</p>
</section>
<section>
<h3>Retargeting Touch Events</h3>
<p>The <a><code>Touch</code></a> <a title="Touch target"><code>target</code></a> [[!TOUCH-EVENTS]] attribute must be adjusted in the same way as an event with a <a><code>relatedTarget</code></a>. Each <a><code>Touch</code></a> <a title="Touch target"><code>target</code></a> in the <a><code>TouchList</code></a> returned from <a><code>TouchEvent</code></a> <a title="touches"><code>touches()</code></a>, <a title="changedTouches"><code>changedTouches()</code></a> and <a title="targetTouches"><code>targetTouches()</code></a> must be the result of <a>related target resolution algorithm</a>, given <var>NODE</var> and <a><code>Touch</code></a> <a title="Touch target"><code>target</code></a> as arguments.</p>
</section>
<section>
<h3>Retargeting Focus Events</h3>
<p>The <code>focus</code>, <code>DOMFocusIn</code>, <code>blur</code>, and <code>DOMFocusOut</code> events <strong>must</strong> be treated in the same way as events with a <code>relatedTarget</code>, where the corresponding <a>node</a> that is losing focus as a result of <code>target</code> gaining focus or the <a>node</a> that is gaining focus, and thus causing the blurring of <code>target</code> acts as the related target.</p>
</section>
<section>
<h3>Event Dispatch</h3>
<p>At the time of event dispatch:</p>
<ul>
<li>The <a><code>Event</code></a> <a><code>target</code></a> and <a><code>currentTarget</code></a> attributes <strong>must</strong> return the <a>relative target</a> for the <a>node</a> on which event listeners are <a title="event listener invoke">invoked</a></li>
<li>The <a><code>MouseEvent</code></a> <a><code>relatedTarget</code></a> attribute <strong>must</strong> return the <a>adjusted related target</a></li>
<li>The <a><code>MouseEvent</code></a> <a><code>offsetX</code></a> and <a><code>offsetY</code></a> attributes <strong>must</strong> return the coordinates relative to the origin of the <a>padding edge</a> of the <a>relative target</a></li>
<li>The <a><code>Touch</code></a> <a title="Touch target"><code>target</code></a> attribute <strong>must</strong> return the <a>adjusted related target</a></li>
<li>If the <a><code>relatedTarget</code></a> and <a><code>target</code></a> are the same for a given node, its the event listeners <strong>must not</strong> be invoked. <a><code>TouchEvent</code></a> is not subject to this rule.</li>
<li>When <em>capturing</em>, which entails processing step 6 of the <a title="event dispatch">event dispatch algorithm</a>, the event listeners <strong>must not</strong> be <a title="event listener invoke">invoked</a> on a <a>node</a> <strong>if</strong> it is the same as its <a>relative target</a></li>
<li>When <em>bubbling</em>, which entails processing step 9 of the <a title="event dispatch">event dispatch algorithm</a>, the <a><code>Event</code></a> <a>eventPhase</a> attribute <strong>must</strong> return <a>AT_TARGET</a> <strong>if</strong> the <a>relative target</a> is same as the <a>node</a> on which event listeners are <a title="event listener invoke">invoked</a></li>
<li>If the event's <a><code>bubbles</code></a> attribute value is <strong>false</strong>, run these substeps:
<ol>
<li>Reverse the order of <em>event path</em></li>
<li>Initialize event's <a><code>eventPhase</code></a> attribute to <a><code>AT_TARGET</code></a></li>
<li>For each object in <em>event path</em> where the <a>relative target</a> is same as the object, <a title="event listener invoke">invoke</a> its <a title="event listener">event listeners</a>, with event <em>event</em>, as long as <em>event</em>'s <a>stop propagation flag</a> is unset</li>
</ol></li>
</ul>
<p>Upon completion of the event dispatch, the <a><code>Event</code></a> object's <a><code>target</code></a> and <a><code>currentTarget</code></a> <strong>must</strong> be to the highest ancestor's <a>relative target</a>. Since it is possible for a script to hold on to the <code>Event</code> object past the scope of event dispatch, this step is necessary to avoid revealing the <a title="node">nodes</a> in <a title="shadow tree">shadow trees</a>.</p>
</section>
<section class="informative">
<h3>Event Retargeting Example</h3>
<p>Suppose we have a user interface for a media controller, represented by this tree, composed of both <a>document tree</a> and the <a title="shadow tree">shadow trees</a>. In this example, we will assume that selectors are allowed to cross the shadow boundaries and we will use these selectors to identify the <a title="element">elements</a>. Also, we will invent a fictional <code>shadow-root</code> <a>element</a> to demarcate the shadow boundaries and represent <a title="shadow root">shadow roots</a>:</p>
<pre class="example">
<div id="player">
<span class=shadow-boundary><shadow-root id="player-shadow-root"></span>
<div id="controls">
<button id="play-button">PLAY</button>
<input type="range" id="timeline">
<span class=shadow-boundary><shadow-root id="timeline-shadow-root"></span>
<div id="slider-thumb" id="timeline-slider-thumb"></div>
<span class=shadow-boundary></shadow-root></span>
</input>
<div id="volume-slider-container">
<input type="range" id="volume-slider">
<span class=shadow-boundary><shadow-root id="volume-shadow-root"></span>
<div id="slider-thumb" id="volume-slider-thumb"></div>
<span class=shadow-boundary></shadow-root></span>
</input>
</div>
</div>
<span class=shadow-boundary></shadow-root></span>
</div>
</pre>
<p>Let's have a user position their pointing device over the volume slider's thumb (<code>#volume-slider-thumb</code>), thus triggering a <code>mouseover</code> event on that node. For this event, let's pretend it has no associated <code>relatedTarget</code>.</p>
<p>Per the <a>retargeting algorithm</a>, we should have the following set of ancestors and relative targets:</p>
<table>
<thead>
<tr>
<th>Ancestor</th>
<th>Relative Target</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>#player</code></td>
<td><code><strong>#player</strong></code></td>
</tr>
<tr>
<td><code>#player-shadow-root</code></td>
<td><code>#volume-slider</code></td>
</tr>
<tr>
<td><code>#controls</code></td>
<td><code>#volume-slider</code></td>
</tr>
<tr>
<td><code>#volume-slider-container</code></td>
<td><code>#volume-slider</code></td>
</tr>
<tr>
<td><code>#volume-slider</code></td>
<td><code><strong>#volume-slider</strong></code></td>
</tr>
<tr>
<td><code>#volume-shadow-root</code></td>
<td><code>#volume-slider-thumb</code></td>
</tr>
<tr>
<td><code>#volume-slider-thumb</code></td>
<td><code><strong>#volume-slider-thumb</strong></code></td>
</tr>
</tbody>
</table>
<p>After we dispatch the <code>mouseover</code> event using these newly computed relative targets, the user decides to move their pointing device over the thumb of the timeline
(<code>#timeline-slider-thumb</code>). This triggers both a <code>mouseout</code> event for the volume slider thumb and the <code>mouseover</code> event for the timeline thumb.</p>
<p>Let's see how the <code>relatedTarget</code> value of the volume thumb's <code>mouseout</code> event is affected. For this event, the <code>relatedTarget</code> is the timeline thumb (<code>#timeline-slider-thumb</code>). Per the <a>related target resolution algorithm</a>, we should have the following set of ancestors and adjusted related targets:</p>
<table>
<thead>
<tr>
<th>Ancestor</th>
<th>Relative Target</th>
<th>Adjusted related Target</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>#player</code></td>
<td><code><strong>#player</strong></code></td>
<td><code><strong>#player</strong></code></td>
</tr>
<tr>
<td><code>#player-shadow-root</code></td>
<td><code>#volume-slider</code></td>
<td><code>#timeline</code></td>
</tr>
<tr>
<td><code>#controls</code></td>
<td><code>#volume-slider</code></td>
<td><code>#timeline</code></td>
</tr>
<tr>
<td><code>#volume-slider-container</code></td>
<td><code>#volume-slider</code></td>