-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathOverview.bs
More file actions
3427 lines (2754 loc) · 127 KB
/
Overview.bs
File metadata and controls
3427 lines (2754 loc) · 127 KB
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
<h1>Selectors Level 4</h1>
<pre class='metadata'>
Group: CSSWG
Shortname: selectors
Level: 4
Status: ED
ED: http://dev.w3.org/csswg/selectors
TR: http://www.w3.org/TR/selectors/
Previous Version: http://www.w3.org/TR/2013/WD-selectors4-20130502/
Previous Version: http://www.w3.org/TR/2012/WD-selectors4-20120823/
Previous Version: http://www.w3.org/TR/2011/WD-selectors4-20110929/
Editor: Elika J. Etemad, Invited Expert, http://fantasai.inkedblade.net/contact
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
Former Editor: Tantek Çelik, http://www.tantek.com
Former Editor: Daniel Glazman
Former Editor: Ian Hickson
Former Editor: Peter Linss
Former Editor: John Williams
Abstract: <b>Selectors</b> are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in a document. Selectors have been optimized for use with HTML and XML, and are designed to be usable in performance-critical code. They are a core component of <abbr title="Cascading Style Sheets">CSS</abbr> (Cascading Style Sheets), which uses Selectors to bind style properties to elements in the document.
Abstract: Selectors Level 4 describes the selectors that already exist in [[!SELECT]], and further introduces new selectors for CSS and other languages that may need them.
At Risk: the column combinator
At Risk: the '':drop()'' pseudo-class
At Risk: the '':read-write'' pseudo-class
Ignored Terms: DocumentFragment, math, h1, shadow tree, box tree, query(), quirks mode
Link Defaults: html5 (element) a, dom-core-ls (interface) document
</pre>
<style>
.tprofile td, th { vertical-align: baseline; padding: 0 0.5em; }
.tprofile th { text-align: right; }
</style>
<h2 id="context">
Introduction</h2>
<em>This section is not normative.</em>
A selector is a boolean predicate
that takes an element in a tree structure
and tests whether the element matches the selector or not.
These expressions may be used for many things:
<ul>
<li>
directly on an element to test whether it matches some criteria,
such as in the <code>element.matches()</code> function defined in [[DOM]]
<li>
applied to an entire tree of elements
to filter it into a set of elements that match the criteria,
such as in the <code>document.queryAll()</code> function defined in [[DOM]]
or the selector of a CSS style rule.
<li>
used "in reverse" to generate markup that would match a given selector,
such as in <a href="http://haml.info/">HAML</a> or <a href="http://en.wikipedia.org/wiki/Zen_Coding">Zen Coding</a>
</ul>
Selectors Levels 1, 2, and 3 are defined as the subsets of selector
functionality defined in the <a href="http://www.w3.org/TR/REC-CSS1">CSS1</a>,
<a href="http://www.w3.org/TR/CSS21/">CSS2.1</a>, and
<a href="http://www.w3.org/TR/css3-selectors/">Selectors Level 3</a>
specifications, respectively. This module defines Selectors Level 4.
<h3 id="placement">Module Interactions</h3>
This module replaces the definitions of
and extends the set of selectors defined for CSS in [[SELECT]] and [[CSS21]].
Pseudo-element selectors,
which define abstract elements in a rendering tree,
are not part of this specification:
their generic syntax is described here,
but, due to their close integration with the rendering model and irrelevance to other uses such as DOM queries,
they will be defined in other modules.
<h2 id="overview">
Selectors Overview</h2>
<em>This section is non-normative, as it merely summarizes the
following sections.</em>
A selector represents a structure. This structure can be used as a
condition (e.g. in a CSS rule) that determines which elements a
selector matches in the document tree, or as a flat description of the
HTML or XML fragment corresponding to that structure.
Selectors may range from simple element names to rich contextual
representations.
The following table summarizes the Selector syntax:
<table class="data">
<col class="pattern">
<col class="meaning">
<col class="section">
<col class="level">
<thead>
<tr>
<th>Pattern
<th>Represents
<th>Section
<th>Level
<tbody>
<tr>
<td><code>*</code>
<td>any element
<td><a href="#universal-selector">Universal selector</a>
<td>2
<tr>
<td><code>E</code>
<td>an element of type E
<td><a href="#type-selectors">Type (tag name) selector</a>
<td>1
<tbody>
<tr>
<td><code>E:not(<var>s1</var>, <var>s2</var>)</code>
<td>an E element that does not match either <a>compound selector</a> <var>s1</var>
or <a>compound selector</a> <var>s2</var>
<td><a href="#negation">Negation pseudo-class</a>
<td>3/4
<tr>
<td><code>E:matches(<var>s1</var>, <var>s2</var>)</code>
<td>an E element that matches <a>compound selector</a> <var>s1</var>
and/or <a>compound selector</a> <var>s2</var>
<td><a href="#matches">Matches-any pseudo-class</a>
<td>4
<tr>
<td><code>E:has(<var>rs1</var>, <var>rs2</var>)</code>
<td>an E element,
if either of the <a>relative selectors</a> <var>rs1</var> or <var>rs2</var>,
when evaluated with E as the <a>:scope elements</a>,
match an element
<td><a href="#relational">Relational pseudo-class</a>
<td>4
<tbody>
<tr>
<td><code>E.warning</code>
<td>an E element belonging to the class <code>warning</code>
(the document language specifies how class is determined).
<td><a href="#class-html">Class selectors</a>
<td>1
<tr>
<td><code>E#myid</code>
<td>an E element with ID equal to <code>myid</code>.
<td><a href="#id-selectors">ID selectors</a>
<td>1
<tr>
<td><code>E[foo]</code>
<td>an E element with a <code>foo</code> attribute
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>2
<tr>
<td><code>E[foo="bar"]</code>
<td>an E element whose <code>foo</code> attribute value is
exactly equal to <code>bar</code>
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>2
<tr>
<td><code>E[foo="bar" i]</code>
<td>an E element whose <code>foo</code> attribute value is
exactly equal to any (ASCII-range) case-permutation of <code>bar</code>
<td><a href="#attribute-case">Attribute selectors: Case-sensitivity</a>
<td>4
<tr>
<td><code>E[foo~="bar"]</code>
<td>an E element whose <code>foo</code> attribute value is
a list of whitespace-separated values, one of which is
exactly equal to <code>bar</code>
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>2
<tr>
<td><code>E[foo^="bar"]</code>
<td>an E element whose <code>foo</code> attribute value
begins exactly with the string "bar"
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>3
<tr>
<td><code>E[foo$="bar"]</code>
<td>an E element whose <code>foo</code> attribute value
ends exactly with the string <code>bar</code>
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>3
<tr>
<td><code>E[foo*="bar"]</code>
<td>an E element whose <code>foo</code> attribute value
contains the substring <code>bar</code>
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>3
<tr>
<td><code>E[foo|="en"]</code>
<td>an E element whose <code>foo</code> attribute value is
a hyphen-separated list of values beginning with <code>en</code>
<td><a href="#attribute-selectors">Attribute selectors</a>
<td>2
<tbody>
<tr>
<td><code>E:dir(ltr)</code>
<td>an element of type E in with left-to-right directionality
(the document language specifies how directionality is determined)
<td><a href="#dir-pseudo">The :dir() pseudo-class</a>
<td>4
<tr>
<td><code>E:lang(zh, *-hant)</code>
<td>an element of type E tagged as being either in Chinese
(any dialect or writing system)
or othewise written with traditional Chinese characters
<td><a href="#lang-pseudo">The :lang() pseudo-class</a>
<td>2/4
<tbody>
<tr>
<td><code>E:any-link</code>
<td>an E element being the source anchor of a hyperlink
<td><a href="#any-link-pseudo">The hyperlink pseudo-class</a>
<td>4
<tr>
<td><code>E:link</code>
<td>an E element being the source anchor of a hyperlink
of which the target is not yet visited
<td><a href="#link">The link history pseudo-classes</a>
<td>1
<tr>
<td><code>E:visited</code>
<td>an E element being the source anchor of a hyperlink
of which the target is already visited
<td><a href="#link">The link history pseudo-classes</a>
<td>1
<tr>
<td><code>E:target</code>
<td>an E element being the target of the referring URL
<td><a href="#target-pseudo">The target pseudo-class</a>
<td>3
<tr>
<td><code>E:scope</code>
<td>an E element being a designated reference element
<td><a href="#scope-pseudo">The scope pseudo-class</a>
<td>4
<tbody>
<tr>
<td><code>E:current</code>
<td>an E element that is currently presented in a time-dimensional canvas
<td><a href="#time-pseudos">Time-dimensional Pseudo-classes</a>
<td>4
<tr>
<td><code>E:current(<var>s</var>)</code>
<td>an E element that is the deepest '':current'' element that
matches selector <var>s</var>
<td><a href="#time-pseudos">Time-dimensional Pseudo-classes</a>
<td>4
<tr>
<td><code>E:past</code>
<td>an E element that is in the past in a time-dimensional canvas
<td><a href="#time-pseudos">Time-dimensional Pseudo-classes</a>
<td>4
<tr>
<td><code>E:future</code>
<td>an E element that is in the future in a time-dimensional canvas
<td><a href="#time-pseudos">Time-dimensional Pseudo-classes</a>
<td>4
<tbody>
<tr>
<td><code>E:active</code>
<td>an E element that is in an activated state
<td><a href="#useraction-pseudos">The user action pseudo-classes</a>
<td>1
<tr>
<td><code>E:hover</code>
<td>an E element that is under the cursor,
or that has a descendant under the cursor
<td><a href="#useraction-pseudos">The user action pseudo-classes</a>
<td>2
<tr>
<td><code>E:focus</code>
<td>an E element that has user input focus
<td><a href="#useraction-pseudos">The user action pseudo-classes</a>
<td>2
<tr>
<td><code>E:active-drop</code>
<td>an E element that will receive the item currently being dragged
<td><a href="#drag-pseudos">The drag-and-drop pseudo-classes</a>
<td>4
<tr>
<td><code>E:valid-drop</code>
<td>an E element that could receive the item currently being dragged
<td><a href="#drag-pseudos">The drag-and-drop pseudo-classes</a>
<td>4
<tr>
<td><code>E:invalid-drop</code>
<td>an E element that cannot receive the item currently being dragged, but could receive some other item
<td><a href="#drag-pseudos">The drag-and-drop pseudo-classes</a>
<td>4
<tbody>
<tr>
<td><code>E:enabled<br>E:disabled</code>
<td>a user interface element E that is enabled or disabled, respectively
<td><a href="#enableddisabled">The :enabled and :disabled pseudo-classes</a>
<td>3
<tr>
<td><code>E:read-only</code><br><code>E:read-write</code>
<td>a user interface element E that
<td><a href="#rw-pseudos">The mutability pseudo-classes</a>
<td>3-UI/4
<tr>
<td><code>E:placeholder-shown</code>
<td>an input control currently showing placeholder text
<td><a href="#rw-pseudos">The placeholder-shown pseudo-class</a>
<td>3-UI/4
<tr>
<td><code>E:default</code>
<td>a user interface element E that
<td><a href="#default-pseudo">The default option pseudo-class :default</a>
<td>3-UI/4
<tr>
<td><code>E:checked</code>
<td>a user interface element E that is checked/selected
(for instance a radio-button or checkbox)
<td><a href="#checked">The selected-option pseudo-class</a>
<td>3
<tr>
<td><code>E:indeterminate</code>
<td>a user interface element E that is in an indeterminate state
(neither checked nor unchecked)
<td><a href="#indeterminate">The indeterminate-value pseudo-class</a>
<td>4
<tr>
<td><code>E:valid</code><br><code>E:invalid</code>
<td>a user-input element E that
<td><a href="#range-pseudos">The validity pseudo-classes</a>
<td>3-UI/4
<tr>
<td><code>E:in-range</code><br><code>E:out-of-range</code>
<td>a user-input element E whose value is in-range/out-of-range
<td><a href="#range-pseudos">The range pseudo-classes</a>
<td>3-UI/4
<tr>
<td><code>E:required</code><br><code>E:optional</code>
<td>a user-input element E that requires/does not require input
<td><a href="#opt-pseudos">The optionality pseudo-classes</a>
<td>3-UI/4
<tr>
<td><code>E:required</code><br><code>E:optional</code>
<td>a user-input element E with incorrect input
<td><a href="#opt-pseudos">The optionality pseudo-classes</a>
<td>4
<tbody>
<tr>
<td><code>E:root</code>
<td>an E element, root of the document
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:empty</code>
<td>an E element that has no children (not even text nodes)
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:blank</code>
<td>an E element that has no content except maybe white space
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>4
<tr>
<td><code>E:nth-child(<var>n</var> [of <var>sel</var>]? )</code>
<td>an E element, the <var>n</var>-th child of its parent matching <var>sel</var>
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:nth-last-child(<var>n</var> [of <var>sel</var>]? )</code>
<td>an E element, the <var>n</var>-th child of its parent matching <var>sel</var>,
counting from the last one
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:first-child</code>
<td>an E element, first child of its parent
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>2
<tr>
<td><code>E:last-child</code>
<td>an E element, last child of its parent
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:only-child</code>
<td>an E element, only child of its parent
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:nth-of-type(<var>n</var>)</code>
<td>an E element, the <var>n</var>-th sibling of its type
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:nth-last-of-type(<var>n</var>)</code>
<td>an E element, the <var>n</var>-th sibling of its type,
counting from the last one
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:first-of-type</code>
<td>an E element, first sibling of its type
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:last-of-type</code>
<td>an E element, last sibling of its type
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tr>
<td><code>E:only-of-type</code>
<td>an E element, only sibling of its type
<td><a href="#structural-pseudos">Structural pseudo-classes</a>
<td>3
<tbody>
<tr>
<td><code>E F</code>
<td>an F element descendant of an E element
<td><a href="#descendant-combinators">Descendant combinator</a>
<td>1
<tr>
<td><code>E > F</code>
<td>an F element child of an E element
<td><a href="#child-combinators">Child combinator</a>
<td>2
<tr>
<td><code>E + F</code>
<td>an F element immediately preceded by an E element
<td><a href="#adjacent-sibling-combinators">Next-sibling combinator</a>
<td>2
<tr>
<td><code>E ~ F</code>
<td>an F element preceded by an E element
<td><a href="#general-sibling-combinators">Following-sibling combinator</a>
<td>3
<tbody>
<tr>
<td><code>F || E</code>
<td>an E element that represents a cell in a grid/table
belonging to a column represented by an element F
<td><a href="#table-pseudos">Grid-Structural pseudo-classes</a>
<td>4
<tr>
<td><code>E:nth-column(<var>n</var>)</code>
<td>an E element that represents a cell belonging to the
<var>n</var>th column in a grid/table
<td><a href="#table-pseudos">Grid-Structural pseudo-classes</a>
<td>4
<tr>
<td><code>E:nth-last-column(<var>n</var>)</code>
<td>an E element that represents a cell belonging to the
<var>n</var>th column in a grid/table, counting from the last one
<td>4
</table>
Note: Some Level 4 selectors (noted above as "3-UI") were introduced in [[CSS3UI]].
<h3 id="profiles">
<a>Fast</a> vs <a>Complete</a> Selector Profiles</h3>
Selectors are used in many different contexts,
with wildly varying performance characteristics.
Some powerful selectors are unfortunately too slow
to realistically include in the more performance-sensitive contexts.
To accommodate this, two profiles of the Selectors spec are defined:
<dl>
<dt><dfn id='fast-profile'>fast</dfn>
<dd>
The <a>fast</a> profile is appropriate for use in any context,
including dynamic browser CSS selector matching.
It includes every selector defined in this document,
except for:
<ul>
<li>The '':has()'' pseudo-class
</ul>
<dt><dfn id='complete-profile'>complete</dfn>
<dd>
The <a>complete</a> profile is appropriate for contexts which aren't extremely performance sensitive.
For example, the <a method for=Element>query()</a> method defined in [[DOM]] should use the ''complete'' profile.
It includes all of the selectors defined in this document.
</dl>
CSS implementations conformant to Selectors Level 4 must use the ''fast'' profile for CSS selection.
<p class='issue'>
The categorization of things into the "fast" or "complete" profiles needs implementor review.
If some things currently not in the fast profile can reasonably be done in CSS Selectors,
we should move them.
<h2 id="syntax">
Selector Syntax and Structure</h2>
<h3 id="structure">
Structure and Terminology</h3>
The term <dfn export>selector</dfn> can refer to a <a>simple selector</a>,
<a>compound selector</a>, <a>complex selector</a>, or <a>selector list</a>.
A <a>selector list</a> is a comma-separated list of <a>selectors</a>;
see <a href="#grouping">Selector Lists</a>.
A <dfn id="complex" export>complex selector</dfn> is a sequence of one or more <a>compound selectors</a>
separated by <a>combinators</a>.
A <dfn id="compound" export>compound selector</dfn>
is a sequence of <a>simple selectors</a>
that are not separated by a <a>combinator</a>.and one or mo
If it contains a <a>type selector</a> or <a>universal selector</a>,
that selector comes first in the sequence.
Only one type selector or universal selector is allowed in the sequence.
A <dfn id="simple" export>simple selector</dfn> represents an aspect of an element to be matched against.
A simple selector is either a
<a>type selector</a>,
<a>universal selector</a>,
<a>attribute selector</a>,
<a>class selector</a>,
<a>ID selector</a>,
or <a>pseudo-class</a>.
A <dfn export>combinator</dfn> represents a particular kind of relationship
between the elements matched by the <a>compound selectors</a> on either side.
Combinators in Selectors level 4 include:
whitespace,
“greater-than sign” (U+003E, <code>></code>),
“plus sign” (U+002B, <code>+</code>),
and “tilde” (U+007E, <code>~</code>).
Issue: Fill in more combinators.
An empty selector,
containing no <a>compound selector</a>,
is an <a href="#invalid">invalid selector</a>.
Note: As whitespace is a valid <a>combinator</a>,
no whitespace is allowed between the <a>simple selectors</a>
in a <a>compound selector</a>.
<h3 id='evaluating-selectors'>
Evaluating a Selector</h3>
This section describes how to <dfn export>evaluate a selector</dfn> against a set of elements.
APIs using this algorithm must provide a selector,
and one or more root elements
indicating the trees that will be searched by the selector.
They may optionally provide:
<ul>
<li>
a <a>scoping method</a> and <a>scoping root</a>.
If not specified, the selector defaults to being unscoped.
<li>
a set of <a>:scope elements</a>,
for resolving the '':scope'' pseudo-class against.
If not specified,
the set defaults to being empty.
If the selector is a <a>relative selector</a>,
the set of <a>:scope elements</a> must not be empty.
Note: Note that if the selector is scoped,
the scoping root is automatically taken as the <a>:scope element</a>,
so it doesn't have to be provided explicitly
unless a different result is desired.
<li>
which <a>pseudo-elements</a> are allowed to show up in the match list, if any.
If not specified, this defaults to allowing all pseudo-elements.
</ul>
A <a>selector</a> is evaluated against some initial list of elements:
the <dfn export>selector match list</dfn>.
The <a>selector match list</a> is initially populated with the root elements
provided to the algorithm,
and all elements reachable from them by traversing any number of child lists.
If the selector is <a>scope-contained</a>,
the <a>selector match list</a> is immediately filtered
to contain only elements that are either the <a>scoping root</a>
or descendants of the <a>scoping root</a>.
The selector is processed from left to right in order,
with <a>simple selectors</a> filtering the <a>selector match list</a>,
and <a>combinators</a> and <a>pseudo-elements</a> changing the <a>selector match list</a> into something new.
If the selector is <a>scope-contained</a>
then after each <a>combinator</a>
the <a>selector match list</a> must be filtered
to contain only elements that are either the <a>scoping root</a>
or descendants of the <a>scoping root</a>.
If the selector is <a>scope-filtered</a>,
then after the selector is finished processing,
the <a>selector match list</a> must be filtered
to contain only elements that are either the <a>scoping root</a>
or descendants of the <a>scoping root</a>.
After the selector is finished matching,
the <a>selector match list</a> must be filtered
to only contain elements and <a>pseudo-elements</a>
allowed by the invoker of this algorithm.
When this process is done, the elements in the <a>selector match list</a>
are the elements said to match the selector.
If the order of elements matter,
they must be sorted in document order,
unless otherwise specified.
<div class='example'>
For example, to evaluate the selector "div > i.name" against a document,
the <a>selector match list</a> is first set to all the elements in the entire document.
Then, the "div" type selector is evaluated, filtering the <a>selector match list</a>
to only contain elements with a tagname of "div".
Then, the ">" child combinator is evaluated, transforming the <a>selector match list</a>
by replacing each element currently in it with the element's children.
Then, the "i" type selector is evaluated, filtering the <a>selector match list</a>
to only contain elements with a tagname of "i".
Finally, the ".name" class selector is evaluated, filter the <a>selector match list</a>
to only contain elements with a class of "name".
</div>
Note: Many implementations of selectors instead evaluate them right to left,
as it's more efficient to do so in many cases.
This, as usual, is completely valid,
as long as it results in the same elements being returned
as the spec's algorithm would.
Issue: The relative position of pseudo-elements in the <a>selector match list</a> is undefined.
There's not yet a context that exposes this information,
but we need to decide on something eventually,
before something <em>is</em> exposed.
<h3 id='data-model'>
Data Model</h3>
Selectors are evaluated against a DOM tree. [[!DOM]]
Within this specification,
this may be referred to as the "document tree".
In addition to the information present in the document tree,
for the purpose of selectors,
elements are assumed to have arbitrary <a>pseudo-class</a> flags
and <a>pseudo-element</a> slots,
as defined by the given <a>pseudo-class</a> and <a>pseudo-element</a>.
<div class='example'>
For example, the first element child of each element
has a flag indicating that it matches the '':first-child'' pseudo-element.
For another example,
all elements have ''::before'' pseudo-elements,
but not only elements with <a>shadow trees</a> have ''::shadow'' pseudo-elements.
</div>
Five aspects of a DOM element are especially relevant for selectors:
<ul>
<li>The element's type (also known as tagname), which is a string.
<li>The element's namespace, which is a string.
<li>An ID, which is a string.
<li>Classes, which are strings.
<li>Attributes, which are pairs of strings consisting of an attribute name and an attribute value.
</ul>
These aspects are referred to as <dfn export title="feature">features</dfn>.
The <a>simple selectors</a> which are keyed off of them
(<a>universal selector</a>, <a>type selectors</a>, <a>ID selectors</a>, <a>class selectors</a>, and <a>attribute selectors</a>)
are called <dfn export title="feature selector">feature selectors</dfn>.
While elements may lack any of the individual <a>features</a>,
some elements are <dfn export>featureless</dfn>.
A <a>featureless</a> element does not match any <a>feature selector</a>,
or any selector that resolves based on <a>features</a>
(such as '':not(div)''),
with the exception of namespace selectors--
a <a>featureless</a> element is treated as having <em>all</em> namespaces.
Thus, only <a>pseudo-classes</a> or <a>pseudo-elements</a> can be used to select them.
Individual <a>featureless</a> elements may define additional restrictions on what kinds of selectors can match them.
<div class='example'>
For example, the <a>host element</a> in a <a>shadow tree</a> is <a>featureless</a>,
and can't be matched by <em>any</em> <a>pseudo-class</a> except for '':host'' and '':host-context()''.)
</div>
<details class='why'>
<summary>Why do featureless elements match all namespaces?</summary>
In a previous version of Selectors,
all <a>compound selectors</a> contained a <a>type selector</a>;
if you didn't supply one explicitly,
a <a>universal selector</a> was implied.
Default namespaces [[CSS3NAMESPACE]] affected the implied universal selector,
so that selectors like '':hover'' actually meant ''defaultNS|*:hover''.
The universal selector is no longer implied,
but default namespaces still exist,
and have to apply to all compound selectors.
The behavior of <
9E70
a>featureless</a> elements,
like the <a>host element</a> in a shadow tree,
thus needs to accommodate that and work appropriately.
In this case, "appropriately" means "ignoring the default namespace".
</details>
When matching a selector against a document which is in <a>quirks mode</a>,
class and ID selectors must be matched <a title="ASCII case-insensitive">ASCII case-insensitively</a>
against the classes and ID of the elements in the document.
<h4 id='dom-mapping'>
Guidance on Mapping Document Languages to DOM</h4>
The DOM document structure is powerful and useful,
but generic enough to model pretty much any langauge that describes tree-based data
(or even graph-based, with a suitable interpretation).
Some languages, like HTML, already have well-defined procedures
for producing a DOM object from a resource.
If a given language does not,
such a procedure must be defined
in order for Selectors to apply to documents in that language.
At minimum,
the document language must define what maps to the DOM concept of an "element".
The primary one-to-many relationship between nodes--
parent/child in tree-based structures,
element/neighbors in graph-based structures--
should be reflected as the child nodes of an element.
Other features of the element should be reflected as <a>features</a> when possible,
mapping to something that serves a similar purpose to the same feature in DOM:
<dl>
<dt>type
<dd>
If the elements in the document language have some notion of "type"
as a basic distinguisher between different groups of elements,
it should be reflected as the "type" <a>feature</a>.
If this "type" can be separated into a "basic" name
and a "namespace" that groups names into higher-level groups,
the latter should be reflected as the "namespace" <a>feature</a>.
Otherwise, the element shouldn't have a "namespace" <a>feature</a>,
and the entire name should be reflected as the "type" <a>feature</a>.
<dt>id
<dd>
If some aspect of the element functions as a unique identifier across the document,
it should be mapped to the "id" <a>feature</a>.
Note: While HTML only allows an element to have a single ID,
this should not be taken as a general restriction.
The important quality of an ID is that each ID should be associated with a single element;
a single element can validly have multiple IDs.
<dt>classes and attributes
<dd>
Aspects of the element that are useful for identifying the element,
but are not generally unique to elements within a document,
should be mapped to the "class" or "attribute" <a>features</a>
depending on if they're something equivalent to a "label" (a string by itself)
or a "property" (a name/value pair)
<dt>pseudo-classes and pseudo-attributes
<dd>
If any elements match any pseudo-classes or have any pseudo-elements,
that must be explicitly defined.
Issue: Some pseudo-classes are *syntactical*,
like '':has()'' and '':matches()'',
and thus should always work.
Need to indicate that somewhere.
Probably the structural pseudos always work
whenever the child list is ordered.
</dl>
<div class='example'>
For example, <a href>JSONSelect</a> is a library that uses selectors
to extract information from JSON documents.
<ul>
<li>
The "elements" of the JSON document
are each array, object, boolean, string, number, or null.
The array and object elements have their contents as children.
<li>
Each element's type is its JS type name:
"array", "object", etc.
<li>
Children of an object
have their key as a class.
<li>
Children of an array match the '':first-child'', '':nth-child()'', etc pseudo-classes.
<li>
The root object matches '':root''.
<li>
It additionally defines '':val()'' and '':contains()'' pseudo-classes,
for matching boolean/number/string elements with a particular value
or which contain a particular substring.
</ul>
This structure is sufficient to allow powerful, compact querying of JSON documents with selectors.
</div>
<h3 id="scoping">
Scoped Selectors</h3>
Some host applications may choose to <dfn export title="scope | scoped selector">scope</dfn> selectors
to a particular subtree or fragment of the document.
The root of the scoping subtree is called the <dfn export>scoping root</dfn>,
and may be either a true element (the <dfn export>scoping element</dfn>)
or a <dfn export title="virtual scoping root">virtual</dfn> one (such as a <a interface>DocumentFragment</a>).
There are two <dfn export title="scoping method | selector scoping method">scoping methods</dfn> for selectors:
<dl>
<dt><dfn export title="scope-contained | scope-contained selector">scope-contained</dfn>
<dd>
With this method of scoping, selectors match as if
the <a>scoping root</a> were the root of the document:
all <a>compound selectors</a> must be represented within the scope.
(The '':root'' pseudo-class, however,
still only matches the actual root of the document.)
<dt><dfn export title="scope-filtered | scope-filtered selector">scope-filtered</dfn>
<dd>
With this method of scoping, a selector matches an element only if the element is within the scope,
even if other components of the selector are outside the scope.
(A <a>scoping element</a> is considered to be in scope.)
</dl>
<div class='example'>
For example,
the <code>element.querySelector()</code> function defined in [[DOM]]
allows the author to define a <a>scope-filtered</a> selector.
On the other hand, the selectors within an [[HTML5]] scoped stylesheet
define <a>scope-contained</a> selectors.
</div>
Note: If there are no <a>:scope elements</a> when a selector is evaluated,
the <a>scoping root</a> acts like a <a>:scope element</a>.
<h3 id="relative">
Relative Selectors</h3>
Certain contexts may accept <dfn title="relative selector | relative | scope-relative" export>relative selectors</dfn>,
which are a shorthand for selectors that represent elements relative to a <a>:scope element</a>
(i.e. an element that matches '':scope'').
In a <a>relative selector</a>,
“:scope ” (the '':scope'' pseudo-class followed by a space)
is implied at the beginning of each <a>complex selector</a>
that does not already contain the '':scope'' pseudo-class.
This allows the selector to begin syntactically with a <a>combinator</a>.
However, it must be <a href="#absolutize">absolutized</a> before matching.
Relative selectors, once absolutized,
can additionally be <a>scope-contained</a> or <a>scope-filtered</a>.
<h4 id='absolutizing'>
Absolutizing a Relative Selector</h4>
To <dfn id='absolutize' export>absolutize a relative selector</dfn>:
If there are no <a>:scope elements</a>
and the selector is <a>scoped</a> to a <a>virtual scoping root</a>:
<ol>
<li>
If the selector starts with a <a>child combinator</a>,
remove the child combinator.
The selector is now absolute,
with the additional constraint that the first compound selector in the selector
only matches elements without a parent.
<li>
Otherwise, if the selector starts with any combinator other than the <a>descendant combinator</a>,
change the selector to '':not(*)''.
<span class='note'>This is the shortest selector that is valid, but guaranteed to match nothing.</span>
<li>
Otherwise, the selector is already absolute.
</ol>
Otherwise:
<ol>
<li>
If the selector starts with a <a>combinator</a> other than the <a>descendant combinator</a>,
prepend '':scope'' as the initial <a>compound selector</a>.
<li>
Otherwise, if the selector does not contain any instance of the '':scope'' pseudo-class
(either at the top-level or as an argument to a functional pseudo-class),
prepend '':scope'' followed by the <a>descendant combinator</a>.
<li>
Otherwise, the selector is already absolute.
</ol>
To <dfn id='absolutize-list' export>absolutize a relative selector list</dfn>,
absolutize each relative selector in the list.
<h3 id="pseudo-classes">
Pseudo-classes</h3>
The <dfn export>pseudo-class</dfn> concept is introduced to permit selection based on
information that lies outside of the document tree
or that can be awkward or impossible to express using the other simple selectors.
The syntax of a <a>pseudo-class</a>
is a ":" (U+003A COLON)
followed by the name of the <a>pseudo-class</a>,
which is either an <a>identifier</a>,
or in the case of a <dfn export>functional pseudo-class</dfn>,
a function potentially containing arguments between parentheses
(similar to CSS functions).
No <a>white space</a> is allowed between the colon and the name of the <a>pseudo-class</a>,
and as usual for CSS syntax,
there are no spaces between the function name and its opening parenthesis.
As usual for CSS syntax,
<a>white space</a> is allowed arond the arguments between the parentheses
of a functional pseudo-class,
unless otherwise specified.
<a>Pseudo-classes</a> are identical to any other type of <a>simple selector</a>:
they're allowed in all <a>compound selectors</a> contained in a selector,
and must follow the <a>type selector</a> or <a>universal selector</a>, if present.
<a>Pseudo-class</a> names are <a>ASCII case-insensitive</a>.
Some <a>pseudo-classes</a> are mutually exclusive
(such that a <a>compound selector</a> containing them, while valid, will never match anything),
while others can apply simultaneously to the same element.
<a>Pseudo-classes</a> may be dynamic,
in the sense that an element can acquire or lose a pseudo-class
while a user interacts with the document.
<h3 id="pseudo-elements">Pseudo-elements</h3>
Similar to how <a>pseudo-classes</a> represent additional information
not directly present in the document tree
(or that is inconvenient to use),
a <dfn export>pseudo-element</dfn> is an <em>element</em>
not directly present in the document tree,
used to create abstractions about the document tree
beyond those specified by the document language.
<div class="example">
For instance, document languages do not offer mechanisms to access
the first letter or first line of an element's content,
but there exist <a>pseudo-elements</a>
(''::first-letter'' and ''::first-line'')
that allow those things to be styled.
<a>Pseudo-elements</a> can also represent content that doesn't exist in the source document at all,
such as the ''::before'' and ''::after'' pseudo-elements
which allow additional content to be inserted before or after the contents of any element.
</div>
The syntax of a <a>pseudo-element</a>
is "::" (two U+003A COLON characters)
followed by the name of the <a>pseudo-element</a> as an <a>identifier</a>.
<a>Pseudo-element</a> names are <a>ASCII case-insensitive</a>.
No <a>white space</a> is allowed between the two colons, or between the colons and the name.
Due to legacy constraints,
user agents must allow ''::before'', ''::after'', ''::first-line'', and ''::first-letter'' pseudo-elements
to be written with a single colon rather than two,
similar to <a>pseudo-class</a> syntax.
Authors must always use the double-colon syntax for these <a>pseudo-elements</a>.
<a>Pseudo-elements</a> do not exist independently;
they belong to another element on the page,
called their <dfn export>originating element</dfn>.
When a <a>pseudo-element</a> is encountered in a selector as part of a <a>compound selector</a>,
the part of the selector to the left of the <a>pseudo-element</a> selects the <a>originating element</a> for the <a>pseudo-element</a>;
the part of the selector to the right, if any, applies to the <a>pseudo-element</a> itself.
If a <a>pseudo-element</a> <em>starts</em> a <a>compound selector</a>,
it's treated as if the selector to its left was the <a>universal selector</a>.
<div class='example'>
For example, in the selector ''div a::before'',
the ''a'' elements matched by the selector are the <a>originating elements</a>
for the ''::before'' pseudo-elements attached to them.
The selector ''::first-line'' is equivalent to ''*::first-line'',
which selects the ''::first-line'' pseudo-element on <em>every</em> element in the document.
</div>