forked from WICG/webcomponents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1838 lines (1605 loc) · 87.2 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 src='//resources.whatwg.org/dfn.js' defer class='remove'></script>
<script class='remove'>
function resolveBacklink() {
// For dfn.js
if (window.initDfn) {
initDfn();
}
}
var respecConfig = {
specStatus: "ED",
shortName: "shadow-dom",
useExperimentalStyles: true,
editors: [{ name: "Dimitri Glazkov", url: "mailto:dglazkov@chromium.org", company: "Google, Inc.", w3cid: 40456 },
{ name: "Hayato Ito", url: "mailto:hayato@google.com", company: "Google, Inc.", w3cid: 49814 }],
wg: "Web Platform Working Group",
wgURI: "http://www.w3.org/WebPlatform/WG/",
wgPublicList: "public-webapps",
wgPatentURI: "http://www.w3.org/2004/01/pp-impl/83482/status",
license: "w3c-software-doc",
edDraftURI: "http://w3c.github.io/webcomponents/spec/shadow/",
otherLinks: [
{
key: "Revision history",
href: "https://github.com/w3c/webcomponents/commits/gh-pages/spec/shadow/"
},
{
key: "Bugs filed",
href: "https://github.com/w3c/webcomponents/labels/shadow-dom"
}
],
preProcess: [resolveAutolink],
postProcess: [resolveBacklink],
localBiblio: {
"EDITING": {
title: "HTML Editing APIs",
href: "https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html",
authors: ["Aryeh Gregor"]
}
}
};
</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>Composition</h2>
<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 <dfn data-lt="hosts">host</dfn> zero or one associated <a data-lt="node tree">node trees</a>, called a <dfn>shadow tree</dfn>.</p>
<p>A <dfn>shadow host</dfn> is an element that <a>hosts</a> one <a data-lt="node tree">shadow tree</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>
<p>A <dfn>shadow root</dfn> is the <a>root</a> <a>node</a> of a shadow tree.</p>
<p>
A node <var>A</var> is called a <dfn>composed child</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>composed descendant</dfn> of a node <var>B</var>,
if either <var>A</var> is a <a>composed child</a> of <var>B</var>
or <var>A</var> is a <a>composed child</a> of a node <var>C</var> that is a <a>composed descendant</a> of <var>B</var>.
</p>
<p>
An <dfn>inclusive composed descendant</dfn> is a node or one of its <a data-lt="composed descendant">composed descendants</a>.
</p>
<p>
A node <var>A</var> is called a <dfn>composed parent</dfn> of a node <var>B</var>
if and only if <var>B</var> is a <a>composed child</a> of <var>A</var>.
</p>
<p>
A node <var>A</var> is called a <dfn>composed ancestor</dfn> of a node <var>B</var>
if and only if <var>B</var> is a <a>composed descendant</a> of <var>A</var>.
</p>
<p>
An <dfn>inclusive composed ancestor</dfn> is a node or one of its <a data-lt="composed ancestor">composed ancestors</a>.
</p>
<p>
When a node is an <a>inclusive composed descendant</a> of the <a>root element of a <code>Document</code> object</a>, it is <dfn>in a composed document</dfn>.
</p>
</section>
<section>
<h3>Composed trees</h3>
<p>A <dfn>composed tree of node trees</dfn> is a <a>tree</a> of <a data-lt="node tree">node trees</a>.</p>
<p class="note">
The purpose of introducing a composed tree of node 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 data-lt="tree">a set of relationships</a> between <a data-lt="node">nodes</a>,
a <a>composed tree of node trees</a> is similarly defined as a set of relationships between node trees:</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 B is a shadow tree and the shadow host which hosts <var>B</var> participates in <var>A</var>.
</li>
<li>
A <a>node tree</a> <var>A</var> is called a <dfn>preceding sibling tree</dfn> of a <a>node tree</a> <var>B</var> if all of the following conditions are satisfied:
<ol>
<li><var>A</var> and <var>B</var> share the same <a>parent tree</a>.</li>
<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>
</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 data-lt="tree">trees</a>.</li>
</ul>
<p>
A <a>node tree</a> <var>A</var> is called an <dfn>unclosed tree</dfn> of a <a>node tree</a> <var>B</var> if and only if one, at least, of the following conditions is satisfied:
</p>
<ul>
<li><var>A</var> is an <a>inclusive ancestor tree</a> of <var>B</var>.</li>
<li>The <a>encapsulation mode</a> of <var>A</var> is <a>open</a> and <var>A</var> is a <a>child tree</a> of <var>B</var>.</li>
<li><var>A</var> is an <a>unclosed tree</a> of a <a>node tree</a> <var>C</var> that is an <a>unclosed tree</a> of <var>B</var>.</li>
</ul>
<p>
A node <var>A</var> is called an <dfn>unclosed node</dfn> of a node <var>B</var> if and only if the <a>node tree</a> that <var>A</var> <a>participates</a> in
is an <a>unclosed tree</a> of the <a>node tree</a> that <var>B</var> <a>participates</a> in.
</p>
<p>
A <dfn>composed document</dfn> is a <a>composed tree of node trees</a> whose <a>root tree</a> is a <a>document tree</a>.
</p>
<p><a><code>Window</code></a> object <a data-lt="named access on the window object">named properties</a> [[!HTML]] <strong>must</strong> access the <a data-lt="node">nodes</a> in the <a>document tree</a>.</p>
<section class="informative">
<h3>Example composed tree of node trees</h3>
<figure>
<object data="../../assets/images/composed-tree.svg" width="650" height="823"></object>
<figcaption>
A composed tree of node trees.
</figcaption>
</figure>
<p>
In the figure, there are six node trees, named <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code> and <code>F</code>.
The shadow trees, <code>B</code>, <code>C</code> and <code>D</code>, are hosted by elements which participate in the document tree <code>A</code>.
The shadow trees, <code>E</code> and <code>F</code>, are hosted by elements which participates in the shadow tree <code>D</code>.
The following set of relationships holds in the figure:
</p>
<ul>
<li>The ordered list of <code>A</code>'s <a data-lt="child tree">child trees</a> is [<code>B</code>, <code>C</code>, <code>D</code>].</li>
<li>The ordered list of <code>B</code>'s <a data-lt="child tree">child trees</a> is [].</li>
<li>The ordered list of <code>C</code>'s <a data-lt="child tree">child trees</a> is [].</li>
<li>The ordered list of <code>D</code>'s <a data-lt="child tree">child trees</a> is [<code>E</code>, <code>F</code>].</li>
<li>The ordered list of <code>E</code>'s <a data-lt="child tree">child trees</a> is [].</li>
<li>The ordered list of <code>F</code>'s <a data-lt="child tree">child trees</a> is [].</li>
<li>The document tree, <code>A</code>, is the <a>root tree</a> of the <a>composed tree of node trees</a>.</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 composed tree of node trees.
For example, <a data-lt="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 data-lt="selectors">Selectors</a> matching.
For example, a <a data-lt="descendant combinators">descendant combinator</a> never descends into a node in a <a data-lt="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>
<ul class="note">
<li>
A <a>document tree</a> is always the <a>root tree</a> of a <a>composed tree of node trees</a>.
</li>
<li>
A <a>shadow tree</a> is never the <a>root tree</a> of a <a>composed tree of node trees</a>. It always has a <a>parent tree</a>.
</li>
<li>
A <a>root tree</a> of a <a>composed tree of node trees</a> is not always a <a>document tree</a>.
For example, a <a>node tree</a> whose <a>root</a> is <code>DocumentFragment</code>, but which is not a <a>shadow root</a>, is the <a>root tree</a> of a <a>composed tree of node trees</a>.
</li>
<li>
The algorithms described in the following sections are applied even if the <a>root tree</a> of a <a>composed tree of node trees</a> is not a <a>document tree</a>.
In other words, Shadow DOM doesn't care what the <a>root tree</a> is for any particular feature to work or not work.
</li>
</ul>
</section>
</section>
<section>
<h3>Flat trees</h3>
<p>A <dfn>flat tree</dfn> is a <a>node tree</a> which is constructed out of <a data-lt="node">nodes</a> from multiple <a data-lt="node tree">node trees</a> in a <a>composed tree of node trees</a>.
The exact algorithm of constructing a flat tree is specified later.</p>
<figure>
<object data="../../assets/images/flat-tree.svg" width="654" height="606"></object>
<figcaption>A flat tree</figcaption>
</figure>
<p>A <dfn>document flat tree</dfn> is a <a>flat tree</a> whose root node is a document</p>
<p>A node is <dfn>in a document flat tree</dfn> if it participates in a <a>document flat tree</a>.</p>
<p>
Unless an element is <a>in a document flat tree</a>, the element <strong>must not</strong> create any CSS <a>box</a>.
</p>
<p>
In resolving CSS <a>inheritance</a>, an element <strong>must</strong> inherit from the parent node in the <a>flat tree</a>, if applicable.
</p>
<p>
User agents <strong>must</strong> use the <a>document flat tree</a> in the <a>visual formatting model</a>, instead of the <a>document tree</a>.
</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.
Specifically, it defines the following selectors related to Shadow DOM:
</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 piercing descendant combinator</strong>)</li>
<li><code>::content</code> pseudo-element</li>
<li><code>:host</code> pseudo-class and <code>:host()</code> functional pseudo-class</li>
<li><code>:host-context()</code> functional pseudo-class</li>
</ul>
</div>
</section>
</section>
<section>
<h2>Distributions</h2>
<section>
<h3>Slots</h3>
<p>
The <a>slot element</a> represents an <em>instruction element</em>, called <dfn>slot</dfn>.
When constructing a <a>flat tree</a>, a <a>slot</a> does not participate in the flat tree. Instead, the <a>slot</a> cherry-picks its contents from other places in the composed tree of node trees.
The exact algorithm of constructing a <a>flat tree</a> is specified later.
</p>
<p class="note">
A slot usually cherry-picks its contents from the shadow host.
</p>
<p>
A node can be <dfn>assigned</dfn> to a slot, called an <dfn>assigned slot</dfn>. The exact algorithm of determining the <a>assigned slot</a> for a node is specified later.
</p>
<p>
A node is called <dfn>slot assignable</dfn> if it is either <code>Text</code> or <code>Element</code>.
</p>
<p>A <dfn>distribution</dfn> is the mechanism that determines which <a data-lt="node">nodes</a> appear at each <a>slot</a>. The exact algorithm of a <a>distribution</a> is specified later.</p>
<figure>
<object data="../../assets/images/distribution.svg" width="663" height="598"></object>
<figcaption>A distribution</figcaption>
</figure>
<p>
A <dfn>slot name</dfn> is the name of a <a>slot</a>.
</p>
<p>
A <dfn>default slot</dfn> is the first <a>slot</a> element, in tree order, in a <a>node tree</a>, whose <a>slot name</a> is the empty string or missing.
</p>
</section>
<section>
<h2>Slotting Algorithm</h2>
<p>
The <dfn>slotting algorithm</dfn> <strong>must</strong> be used to determine the <a>assigned slot</a> of each node and <strong>must</strong> be <a data-lt="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>(nullable) <var>SLOT</var>, a <var>slot</var> to which <var>NODE</var> is assigned.</dd>
</dl>
<ol>
<li>
If <var>NODE</var> is <a>slot assignable</a> and the parent node of <var>NODE</var> is a <a>shadow host</a>:
<ol>
<li>Let <var>TREE</var> be the <a>shadow tree</a> that the parent node of <var>NODE</var> <a>hosts</a></li>
<li>Let <var>NAME</var> be the value of the <a href="#widl-Element-slot">slot attribute</a> of <var>NODE</var></li>
<li>
If <var>NAME</var> is the empty string or missing:
<ol>
<li>Let <var>SLOT</var> be a <a>default slot</a> for <var>TREE</var> if it exists, Otherwise, null</li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>Let <var>SLOT</var> be the first <a>slot</a> element, in tree order, in <var>TREE</var>, whose <a>slot name</a> is <var>NAME</var>, and null if there is no such <a>slot</a> element otherwise.</li>
</ol>
</li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>Let <var>SLOT</var> be null</li>
</ol>
</li>
</ol>
</div>
</section>
<section>
<h2>Assigned Nodes Algorithm</h2>
<p>
The <dfn>get assigned nodes algorithm</dfn> <strong>must</strong> be used to determine the <dfn>assigned nodes</dfn> of a <a>slot</a> and <strong>must</strong> be <a data-lt="processing equivalence">equivalent</a> to processing the following steps:
</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>SLOT</var>, a <a>slot</a></dd>
<dt>Output</dt>
<dd><var>ASSIGNED-NODES</var>, an ordered list of nodes</dd>
</dl>
<ol>
<li>
If <var>SLOT</var> participates in a shadow tree:
<ol>
<li>Let <var>HOST</var> be a <a>shadow host</a> which <a>hosts</a> the <a>shadow tree</a></li>
<li>
For each child node <var>NODE</var> of <var>HOST</var>:
<ol>
<li>
If <var>NODE</var> is <a data-lt="assigned slot">assigned</a> to <var>SLOT</var>:
<ol>
<li>Append <var>NODE</var> to <var>ASSIGNED-NODES</var></li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
</section>
<section>
<h2>Distributed Nodes Algorithm</h2>
<p>
The <dfn>get distributed nodes algorithm</dfn> <strong>must</strong> be used to determine the <dfn>distributed nodes</dfn> of a <a>slot</a> and <strong>must</strong> be <a data-lt="processing equivalence">equivalent</a> to processing the following steps:
</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>SLOT</var>, a <a>slot</a></dd>
<dt>Output</dt>
<dd><var>DISTRIBUTED-NODES</var>, an ordered list of nodes.</dd>
</dl>
<ol>
<li>
If the <a>assigned nodes</a> of <var>SLOT</var> is empty:
<ol>
<li>Let <var>ASSIGNED-OR-FALLBACK-CONTENT</var> be the child nodes of <var>SLOT</var></li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>Let <var>ASSIGNED-OR-FALLBACK-CONTENT</var> be the <a>assigned nodes</a> of <var>SLOT</var></li>
</ol>
</li>
<li>
For each node <var>NODE</var> of <var>ASSIGNED-OR-FALLBACK-CONTENT</var>:
<ol>
<li>
If <var>NODE</var> is a <a>slot</a>:
<ol>
<li>Let <var>SUB-LIST</var> be the result of (recursively) running the <a>get distributed nodes algorithm</a> with <var>NODE</var> as input</li>
<li>Append all nodes in <var>SUB-LIST</var> to <var>DISTRIBUTED-NODES</var></li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>Append <var>NODE</var> to <var>DISTRIBUTED-NODES</var> if <var>NODE</var> is <a>slot assignable</a></li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
</section>
</section>
<section>
<h2>Flattening</h2>
<section>
<h3>Flattening Algorithm</h3>
<p>The <dfn>flat tree children calculation algorithm</dfn> <strong>must</strong> be used to determine the child nodes of a node in the <a>flat tree</a> and <strong>must</strong> be <a data-lt="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 flat tree</dd>
<dt>Output</dt>
<dd><var>CHILDREN</var>, the child nodes of <var>NODE</var> in the <a>flat 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 child nodes of the <a>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 nodes of <var>NODE</var></li>
</ol>
</li>
<li>
For each <a>node</a>, <var>CHILD</var>, in <var>CHILD-POOL</var>:
<ol>
<li>
If <var>CHILD</var> is a <a>slot</a>:
<ol>
<li>If <var>CHILD</var> is not assigned to any <a>slot</a>:
<ol>
<li>Append all nodes of the <a>distributed nodes</a> of <var>CHILD</var> to <var>CHILDREN</var></li>
</ol>
</li>
</ol>
</li>
<li>Otherwise:
<ol>
<li>Append <var>CHILD</var> to <var>CHILDREN</var> if <var>CHILD</var> is <a>slot assignable</a></li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
<p>For a given <a>composed tree of node trees</a> <var>COMPOSED-TREE</var>, the <a>flat tree</a> constructed from <var>COMPOSED-TREE</var> <strong>must</strong> be <a data-lt="processing equivalence">equivalent</a> to the following tree:</p>
<ul>
<li>The <a>root</a> <a>node</a> of the <a>flat tree</a> is the <a>root</a> <a>node</a> of the <a>root tree</a> of <var>COMPOSED-TREE</var>.</li>
<li>For a given <a>node</a> which <a>participates</a> in the <a>flat tree</a>, the child <a data-lt="node">nodes</a> of the <a>node</a> is the result of the <a>flat tree children calculation algorithm</a> with the <a>node</a> as input.
</li>
</ul>
</section>
<section class="informative">
<h3>Flattening Example</h3>
<p>
Suppose that we have the following composed tree of node trees:
</p>
<figure>
<object data="../../assets/images/example-composed-tree.svg" width="581" height="616"></object>
<figcaption>An example composed tree of node trees. A node whose color is red represents a <a>slot</a>. Unlike the previous figures, a box surrounding a node tree is omitted here. </figcaption>
</figure>
<p>
This <a>composed tree of node trees</a> is composed of the following 3 <a data-lt="node tree">node trees</a>, one <a>document tree</a> and two <a data-lt="shadow tree">shadow trees</a>:
</p>
<table>
<thead>
<tr>
<th>node tree</th>
<th>Root node is: </th>
<th>Hosted by:</th>
<th>Composed of: (in tree order)</th>
</tr>
</thead>
<tbody>
<tr>
<td>document tree 1</td>
<td>A</td>
<td>-</td>
<td>A, B, C, D, E, F, G, H, I</td>
</tr>
<tr>
<td>shadow tree 1</td>
<td>J</td>
<td>C</td>
<td>J, K, L, M, N, O, P, Q</td>
</tr>
<tr>
<td>shadow tree 2</td>
<td>R</td>
<td>N</td>
<td>R, S, T</td>
</tr>
</tbody>
</table>
<p>
Suppose that an <a>assigned slot</a> of each node, if it exists, is:
</p>
<ul>
<li>D: => L (You can read this as <strong>"D is assigned to L</strong>")</li>
<li>F: => L</li>
<li>H: => O</li>
<li>O: => T</li>
<li>P: => T</li>
</ul>
<figure>
<object data="../../assets/images/example-slotting.svg" width="581" height="616"></object>
<figcaption>A result of slotting.</figcaption>
</figure>
<p>
Then, the <a>assigned nodes</a> and the <a>distributed nodes</a> of each slot will be:
</p>
<table>
<thead>
<tr>
<th>node tree</th>
<th>Root node is: </th>
<th>Hosted by:</th>
<th>Composed of: (in tree order)</th>
<th><a>Assigned slot</a> of each node</th>
<th><a>Assigned nodes</a> of each slot</th>
<th><a>Distributed nodes</a> of each slot</th>
</tr>
</thead>
<tbody>
<tr>
<td>document tree 1</td>
<td>A</td>
<td>-</td>
<td>A, B, C, D, E, F, G, H, I</td>
<td>
<ul>
<li>D: => L</li>
<li>F: => L</li>
<li>H: => O</li>
</ul>
</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>shadow tree 1</td>
<td>J</td>
<td>C</td>
<td>J, K, L, M, N, O, P, Q</td>
<td>
<ul>
<li>O: => T</li>
<li>P: => T</li>
</ul>
</td>
<td>
<ul>
<li>L: [D, F]</li>
<li>M: []</li>
<li>O: [H]</li>
</ul>
</td>
<td>
<ul>
<li>L: [D, F]</li>
<li>M: []</li>
<li>O: [H]</li>
</ul>
</td>
</tr>
<tr>
<td>shadow tree 2</td>
<td>R</td>
<td>N</td>
<td>R, S, T</td>
<td>-</td>
<td>
<ul>
<li>T: [O, P]</li>
</ul>
</td>
<td>
<ul>
<li>T: [H, P]
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<ul class="note">
<li>More than one nodes can be assigned to the same slot. e.g. D: => L, F: => L</li>
<li>A slot can be assigned to an other slot, e.g. O: => T</li>
<li>
The <a>distributed nodes</a> of T are [H, P]. That aren't [O, P].
A slot is never a member of distributed nodes of an other slot.
If a slot <var>A</var> is assigned to an other slot <var>B</var>, the distributed nodes for <var>A</var> are appended to the distributed nodes for <var>B</var>,
instead of <var>A</var> itself, as per the <a>get distributed nodes algorithm</a>.
</li>
</ul>
<p>
The document flat tree will be:
</p>
<figure>
<object data="../../assets/images/example-flat-tree.svg" width="256" height="526"></object>
<figcaption>Document flat tree.</figcaption>
</figure>
<ul class="note">
<li>A slot never participates in a <a>document flat tree</a>. Neither a <a>shadow root</a> does.</li>
<li>Node G doesn't participate in the <a>document flat tree</a>. G is a child of a shadow host, but it's not assigned to any slot.</li>
<li>In an extreme case, even if a document tree has more than 1,000 nodes, you can make the page *blank* by attaching a shadow root to the <code>body</code> element because a document flat tree is used in rendering.</li>
</ul>
</section>
</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 class="note">
This section assumes that an event target is a <code>Node</code> object.
This section is not applied for an event whose event target is not a <code>Node</code> object, such as IndexedDB, XHR and so on.
See <a href="https://github.com/w3c/webcomponents/issues/61">Issue #61</a> for details.
Eventually, DOM Standard should clarify this.
</p>
<section>
<h3>Event Paths</h3>
<section>
<h3>scoped flag</h3>
<p>
A trusted event's <a><code>scoped flag</code></a> <strong>must</strong> be initialized to true
if the event is one of the following events: <code>abort</code>, <code>error</code>, <code>select</code>, <code>change</code>, <code>load</code>, <code>loadedmetadata</code>, <code>reset</code>,
<code>resize</code>, <code>scroll</code> and <code>selectstart</code>.
</p>
</section>
<section>
<h3>relatedTargetScoped flag</h3>
<p>
A trusted event's <a><code>relatedTargetScoped flag</code></a> <strong>must</strong> be initialized to true
if the event has a <code>relatedTarget</code> property [[!DOM-Level-3-Events]] whose value is non-null.
</p>
</section>
<section>
<h3>get the parent</h3>
<p>
The <a>get the parent</a> algorithm associated with <code>Node</code> <strong>must</strong> be <a data-lt="processing equivalence">equivalent</a> to processing the following steps:
</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>NODE</var>, a context object</dd>
<dd><var>TARGET</var>, an eventPath's last item</dd>
<dd><var>EVENT</var>, an event</dd>
<dt>Output</dt>
<dd><var>PARENT</var>, the parent</dd>
</dl>
<ol>
<li>
If all of the following conditions are satisfied, let <var>PARENT</var> be null:
<ol>
<li><var>NODE</var> is a <a>shadow root</a></li>
<li><var>NODE</var> is the <a>root</a> node of <var>TARGET</var></li>
<li><var>EVENT</var>'s <a>scoped flag</a> is true</li>
</ol>
</li>
<li>
Otherwise, if all of the following conditions are satisfied, let <var>PARENT</var> be null:
<ol>
<li>
<var>EVENT</var>'s <a>relatedTargetScoped flag</a> is true
</li>
<li>
<var>EVENT</var> has a <code>relatedTarget</code> property whose value is non-null
</li>
<li>
The <a>relative target</a> and the <a>relative related target</a> are the same, where:
<ul>
<li>
The <a>relative target</a> is the result of <a>retargeting algorithm</a> with <var>NODE</var> and <var>TARGET</var> as input
</li>
<li>
The <a>relative related target</a> is the result of <a>retargeting algorithm</a> with <var>NODE</var> and <var>EVENT</var>'s <code>relatedTarget</code> as input
</li>
</ul>
</li>
</ol>
<li>
Otherwise, if <var>NODE</var> is assigned to a <a>slot</a> <var>SLOT</var>:
<ol>
<li>Let <var>PARENT</var> be <var>SLOT</var></li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>Let <var>PARENT</var> be the <a>composed parent</a> of <var>NODE</var></li>
</ol>
</li>
</li>
</ol>
</div>
<p>
The definitions of <a>retargeting algorithm</a> is specified later.
</p>
<div class="note">
<p>
The motivation of the <a>relatedTargetScoped flag</a> is to avoid the appearance of spurious events, such as <code>mouseover</code> or <code>mouseout</code> events, firing at the node in ancestor trees
in cases where both <code>relatedTarget</code> and <code>target</code> are part of the same <a>shadow tree</a>,
</p>
</div>
</section>
<section class="informative">
<h3>Event Paths Example</h3>
<p>
Let's re-use the same composed tree of node trees used in the flattening example section. Suppose that an event is dispatched on node <code>I</code>. The event path will be:
</p>
<p>
<code>[I, H, O, T, S, R, N, J, C, A]</code> (For the purpose of the explanation, <a>Window</a> is not shown here. The actual event path contains <a>Window</a>.)
</p>
<p>
It's worth pointing out that if we exclude all nodes which don't participate in the flat tree from the event path,
the result would be equivalent to the inclusive ancestors of the node <code>I</code> in the flat tree.
</p>
<figure>
<object data="../../assets/images/event-path-flat-tree.svg" width="580" height="560"></object>
<figcaption>
The relationship between the <a>flat tree</a> and an event path.
If we exclude O, T, R and J, that don't participate in the flat tree, from the event path,
the result would be equivalent to the inclusive ancestors of the node, <code>I</code>, in the <a>flat tree</a>.
</figcaption>
</figure>
<p>
Note that the <a>get the parent</a> algorithm associated with <code>Node</code> 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 always 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>
<figure>
<object data="../../assets/images/event-path-component-trees.svg" width="766" height="386">></object>
<figcaption>
The relationship between an event path and node trees. In the figure, a number shown in a right-side of each node represents a zero-based position of each node in the event path.
A parent node always has a larger number than that of its child node in each node tree.
</figcaption>
</figure>
<p>A <em>local</em> event path for each node tree would be seen as:
</p>
<table>
<thead>
<tr>
<th>node tree</th>
<th><em>Local</em> Event Path</th>
</tr>
</thead>
<tbody>
<tr>
<td>document tree 1</td>
<td>[I, H, C, A]</td>
</tr>
<tr>
<td>shadow tree 1</td>
<td>[O, N, J]</td>
</tr>
<tr>
<td>shadow tree 2</td>
<td>[T, S, R]</td>
</tr>
</tbody>
</table>
<p>
That means, if your concern is only one <a>node tree</a>, you can forget all other node trees.
The event path would be seen as if the event happened only on the node tree you are focusing on.
This is an important aspect in a sense that hosting a shadow tree doesn't have any effect to the <em>local</em> event path
as long as the event is not stopped somewhere in the <a data-lt="descendant tree">descendant trees</a>.
</p>
<p>
If you are a web author and your concern is only a document tree, this might be a good news because an event listener that is registered somewhere on the document tree
would <em>continue to work</em> even when you attach a shadow root to an element in the document tree to <em>enhance</em> the element.
At the same time, an author of a shadow tree also can receive an event which will happen on a node in the document tree, if the node, or its ancestor, is assigned to a slot in the shadow tree.
</p>
</section>
</section>
<section>
<h3>Event Retargeting</h3>
<section>
<h3>Event <code>target</code> Retargeting</h3>
<p>The value of the <code>Event</code> object's <a><code>target</code></a> attribute <strong>must</strong> be the result of the <a>retargeting algorithm</a> with the event's <code>currentTarget</code> and original <code>target</code> value, before adjusted, as input. The result is called a <dfn>relative target</dfn>.
</p>
<p>The <dfn>retargeting algorithm</dfn> <strong>must</strong> be <a data-lt="processing equivalence">equivalent</a> to processing the following steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>BASE</var>, a base node for which a target node should be adjusted</dd>
<dd><var>TARGET</var>, a target node which should be adjusted</dd>
<dt>Output</dt>
<dd><var>RELATIVE-TARGET</var>, a <a>relative target</a>, the result of adjusting <var>TARGET</var> for <var>BASE</var></dd>
</dl>
<ol>
<li>Let <var>BASE-TREE</var> be the <a>node tree</a> which <var>BASE</var> <a>participates</a> in</li>
<li>Let <var>TARGET-TREE</var> be the <a>node tree</a> which <var>TARGET</var> <a>participates</a> in</li>
<li>If <var>BASE-TREE</var> and <var>TARGET-TREE</var> participate in the same <a>composed tree of node trees</a>:
<ol>
<li>Let <var>COMMON-ANCESTOR-TREE</var> be the lowest common <a>inclusive ancestor tree</a> of <var>BASE-TREE</var> and <var>TARGET-TREE</var></li>
</ol>
</li>
<li>Otherwise:
<ol>
<li>Let <var>COMMON-ANCESTOR-TREE</var> be the <a>root tree</a> of <var>TARGET-TREE</var></li>
</ol>
</li>
<li>
For each <a>node</a>, <var>ANCETOR</var>, in an <a>inclusive composed ancestor</a> nodes of <var>TARGET</var>, from descendants to ancestors:
<ol>
<li>
If <var>ANCESTOR</var> participates in <var>COMMON-ANCESTOR-TREE</var>:
<ol>
<li>Let <var>RELATIVE-TARGET</var> be <var>ANCESTOR</var></li>
<li>Stop this algorithm</li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
<p>
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.
The event target <a>retargeting</a> process <strong>must</strong> occur prior to dispatch of an event.
In other words, any DOM mutation occurred in an event listener does not have any affect on the result of retargeting process.
</p>
<p class="note">
The motivation of <a>retargeting</a> is to maintain an encapsulation in the cases where event path is across multiple node trees.
The event's <code>target</code> might not be an <a>unclosed node</a> at some of nodes in the event path without <a>retargeting</a>.
A <a>relative target</a> is a <a>unclosed node</a> that most accurately represents the target of a dispatched event at each node in the event path.
</p>
</section>
<section>
<h3>Event <code>relatedTarget</code> Retargeting</h3>
<p>Some events have a <a><code>relatedTarget</code></a> property, which holds a <a>node</a> that's not the event's target, but is related to the event.</p>
<p>
The value of the <code>Event</code> object's <a><code>relatedTarget</code></a> attribute <strong>must</strong> be the result of the <a>retargeting algorithm</a> with the event's <code>currentTarget</code> and <code>relatedTarget</code> as input.
The result is called a <dfn>relative related target</dfn>.
</p>
<p>
The event relatedTarget retargeting process <strong>must</strong> occur prior to dispatch of an event.
</p>
</section>
<section>
<h3>Touch Events Retargeting </h3>
<p>The <a><code>Touch</code></a> <a data-lt="Touch target"><code>target</code></a> [[!TOUCH-EVENTS]] attribute <strong>must</strong> be adjusted in the same way as an event with a <a><code>relatedTarget</code></a>. Each <a><code>Touch</code></a> <a data-lt="Touch target"><code>target</code></a> in the <a><code>TouchList</code></a> returned from <a><code>TouchEvent</code></a> <a data-lt="touches"><code>touches()</code></a>, <a data-lt="changedTouches"><code>changedTouches()</code></a> and <a data-lt="targetTouches"><code>targetTouches()</code></a> must be the result of the <a>retargeting algorithm</a> with a current target and <a><code>Touch</code></a> <a data-lt="Touch target"><code>target</code></a> as input.</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 data-lt="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 data-lt="element">elements</a>. Also, we will invent a fictional <code>shadow-root</code> <a>element</a> to demarcate the shadow boundaries and represent <a data-lt="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>