-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathOverview.bs
743 lines (609 loc) · 19.9 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<pre class='metadata'>
Title: CSS Nesting Module
Shortname: css-nesting
Level: 1
Status: ED
Work Status: Exploring
Group: CSSWG
ED: https://drafts.csswg.org/css-nesting/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
Editor: Adam Argyle, Google, https://nerdy.dev, w3cid 112669
Abstract: This module introduces the ability to nest one style rule inside another, with the selector of the child rule relative to the selector of the parent rule. This increases the modularity and maintainability of CSS stylesheets.
</pre>
<pre class=link-defaults>
spec:css-color-4; type:property; text:color
spec:cssom-1; type:dfn;
text:child css rules
text:specified order
</pre>
<h2 id="intro">
Introduction</h2>
<em>This section is not normative.</em>
This module describes support for nesting a style rule within another style rule,
allowing the inner rule's selector to reference the elements matched by the outer rule.
This feature allows related styles to be aggregated into a single structure within the CSS document,
improving readability and maintainability.
<h3 id="placement">
Module Interactions</h3>
This module introduces new parser rules that extend the [[!CSS21]] parser model.
This module introduces selectors that extend the [[SELECTORS4]] module.
<h3 id="values">
Values</h3>
This specification does not define any new properties or values.
<h3 id="motivation">
Motivation</h3>
CSS Rules for even moderately complicated web pages include lots of duplication for the purpose of styling related content.
For example, here is a portion of the CSS markup for one version of the [[CSS3COLOR]] module:
<div class='example'>
<pre class=lang-css>
table.colortable td {
text-align:center;
}
table.colortable td.c {
text-transform:uppercase;
}
table.colortable td:first-child, table.colortable td:first-child+td {
border:1px solid black;
}
table.colortable th {
text-align:center;
background:black;
color:white;
}
</pre>
</div>
Nesting allows the grouping of related style rules, like this:
<div class='example'>
<pre class=lang-css>
table.colortable {
& td {
text-align:center;
&.c { text-transform:uppercase }
&:first-child, &:first-child + td { border:1px solid black }
}
& th {
text-align:center;
background:black;
color:white;
}
}
</pre>
</div>
Besides removing duplication,
the grouping of related rules improves the readability and maintainability of the resulting CSS.
Nesting Selector: the ''&'' selector {#nest-selector}
=====================================================
When using a <a>nested style rule</a>,
one must be able to refer to the elements matched by the parent rule;
that is, after all, <em>the entire point of nesting</em>.
To accomplish that,
this specification defines a new selector,
the <dfn>nesting selector</dfn>,
written as an ASCII ampersand <dfn selector>&</dfn>.
When used in the selector of a <a>nested style rule</a>,
the <a>nesting selector</a> represents the elements matched by the parent rule.
When used in any other context,
it represents nothing.
(That is, it's valid, but matches no elements.)
<div class="note">
The <a>nesting selector</a> can be desugared
by replacing it with the parent style rule's selector,
wrapped in a '':is()'' selector.
For example,
<pre class=lang-css>
a, b {
& c { color: blue; }
}
</pre>
is equivalent to
<pre class=lang-css>
:is(a, b) c { color: blue; }
</pre>
</div>
The <a>specificity</a> of the <a>nesting selector</a>
is equal to the largest specificity among the parent style rule's selector
that match the given element.
<div class="example">
For example, given the following style rules:
<pre class=lang-css>
#a, .b {
& c { color: blue; }
}
</pre>
Then in a DOM structure like
<pre class="lang-html">
<div id=a>
<c>foo</c>
</div>
</pre>
the ''&'' selector has specificity [1,0,0]
because it matches due to the ''#a'' selector,
giving the entire ''color: blue'' rule a specificity of [1,0,1].
</div>
Note: This specificity is intentionally equivalent to that of the desugaring described above.
The <a>nesting selector</a> is allowed anywhere in a <a>compound selector</a>,
even before a <a>type selector</a>,
violating the normal restrictions on ordering within a <a>compound selector</a>.
Note: This is required to allow direct nesting.
Also, the "type selectors must come first" has no <em>intrinsic</em> reason behind it;
it exists because we need to be able to tell simple selectors apart unambiguously
when they're directly appended together in a <a>compound selector</a>,
and it's not clear from ''.foodiv'' that it should mean the same as ''div.foo''.
An ampersand is unambiguously separable from an ident, tho,
so there is no problem with it preceding a type selector,
like ''&div''.
Nesting Style Rules {#nesting}
==============================
Nesting style rules naively inside of other style rules is, unfortunately, problematic--
the syntax of a selector is ambiguous with the syntax of a declaration,
so an implementation requires unbounded lookahead
to tell whether a given bit of text is a declaration or the start of a style rule.
As CSS to date requires only a single token of lookahead in its parsing,
this drawback is generally considered unacceptable among popular implementations of CSS.
To get around this limitation,
this specification defines two methods of <dfn lt="nested style rule|nesting style rule">nesting style rules</dfn> inside of other style rules,
both designed to be immediately unambiguous with the surrounding declarations.
The first, <a>direct nesting</a>,
has a somewhat restricted syntax,
but imposes minimal additional "weight" in the form of disambiguating syntax,
and is suitable for most purposes.
The second, the ''@nest'' rule,
imposes a small syntactic weight to disambiguate it from surrounding declarations,
but has no restrictions on the makeup of the selector.
The two are otherwise equivalent,
and either can be used as desired by the stylesheet author.
Direct Nesting {#direct}
------------------------
A style rule can be <dfn lt="direct nesting|directly nested">directly nested</dfn>
within another style rule if its selector is <a>nest-prefixed</a>.
To be <dfn>nest-prefixed</dfn>,
a <a>nesting selector</a> must be the first <a>simple selector</a>
in the first <a>compound selector</a>
of the selector.
If the selector is a list of selectors,
every <a>complex selector</a> in the list must be <a>nest-prefixed</a>
for the selector as a whole to <a>nest-prefixed</a>.
<div class="example">
For example, the following nestings are valid:
<pre class=lang-css>
.foo {
color: blue;
& > .bar { color: red; }
}
/* equivalent to
.foo { color: blue; }
.foo > .bar { color: red; }
*/
.foo {
color: blue;
&.bar { color: red; }
}
/* equivalent to
.foo { color: blue; }
.foo.bar { color: red; }
*/
.foo, .bar {
color: blue;
& + .baz, &.qux { color: red; }
}
/* equivalent to
.foo, .bar { color: blue; }
:is(.foo, .bar) + .baz,
:is(.foo, .bar).qux { color: red; }
*/
.foo {
color: blue;
& .bar & .baz & .qux { color: red; }
}
/* equivalent to
.foo { color: blue; }
.foo .bar .foo .baz .foo .qux { color: blue; }
*/
.foo {
color: blue;
& { padding: 2ch; }
}
/* equivalent to
.foo { color: blue; }
.foo { padding: 2ch; }
// and
.foo {
color: blue;
padding: 2ch;
}
*/
.error, #404 {
&:not(.foo,.bar) > .baz { color: red; }
}
/* equivalent to
:is(.error, #404):not(.foo,.bar) > .baz { color: red; }
*/
.foo {
&:is(.bar, &.baz) { color: red; }
}
/* equivalent to
.foo:is(.bar, .foo.baz) { color: red; }
*/
figure {
margin: 0;
& > figcaption {
background: hsl(0 0% 0% / 50%);
& > p {
font-size: .9rem;
}
}
}
/* equivalent to
figure { margin: 0; }
figure > figcaption { background: hsl(0 0% 0% / 50%); }
figure > figcaption > p { font-size: .9rem; }
*/
main {
& > section,
& > article {
background: white;
& > header {
font-size: 1.25rem;
}
}
}
/* equivalent to
main > :is(section, article) { background: white; }
main > :is(section, article) > header { font-size: 1.25rem; }
*/
</pre>
But the following are invalid:
<pre class=lang-css>
.foo {
color: red;
.bar { color: blue; }
}
/* Invalid because there's no nesting selector */
.foo {
color: red;
.bar & { color:blue; }
}
/* Invalid because & isn't in the first compound selector */
.foo {
color: red;
&&.bar { color: blue; }
}
/* Invalid because the second & is no longer
at the start of a compound selector */
.foo {
color: red;
&.bar, .baz { color: blue; }
}
/* Invalid because the second selector in the list doesn't
contain a nesting selector. */
</pre>
</div>
Note: The last invalid example is technically not ambiguous,
but it's still invalid because allowing it would be an editing hazard.
Later edits to the stylesheet might remove the first selector in the list,
making the other one the new "first selector",
and making the rule invalid.
Turning an otherwise-innocuous action
(like removing a selector from a list)
into a possible error
makes editing more complicated,
and is author-hostile,
so we disallow it as a possibility.
The Nesting At-Rule: ''@nest'' {#at-nest}
-----------------------------------------
While <a>direct nesting</a> looks nice,
it is somewhat fragile.
Some valid nesting selectors,
like ''.foo &'',
are disallowed,
and editing the selector in certain ways can make the rule invalid unexpectedly.
As well,
some people find the nesting challenging to distinguish visually
from the surrounding declarations.
To aid in all these issues,
this specification defines the ''@nest'' rule,
which imposes fewer restrictions on how to validly nest style rules.
Its syntax is:
<pre class=prod>
<dfn>@nest</dfn> = @nest <<selector>> { <<declaration-list>> }
</pre>
The ''@nest'' rule functions identically to a style rule:
it starts with a selector,
and contains declarations that apply to the elements the selector matches.
The only difference is that the selector used in a ''@nest'' rule
must be <dfn>nest-containing</dfn>,
which means it contains a <a>nesting selector</a> in it somewhere.
A list of selectors is <a>nest-containing</a> if all of its individual <a>complex selectors</a>
are <a>nest-containing</a>.
<div class="example">
For example, the following nestings are valid:
<pre class="lang-css">
.foo {
color: red;
@nest & > .bar {
color: blue;
}
}
/* equivalent to
.foo { color: red; }
.foo > .bar { color: blue; }
*/
.foo {
color: red;
@nest .parent & {
color: blue;
}
}
/* equivalent to
.foo { color: red; }
.parent .foo { color: blue; }
*/
.foo {
color: red;
@nest :not(&) {
color: blue;
}
}
/* equivalent to
.foo { color: red; }
:not(.foo) { color: blue; }
*/
</pre>
But the following are invalid:
<pre class=lang-css>
.foo {
color: red;
@nest .bar {
color: blue;
}
}
/* Invalid because there's no nesting selector */
.foo {
color: red;
@nest & .bar, .baz {
color: blue;
}
}
/* Invalid because not all selectors in the list
contain a nesting selector */
</pre>
</div>
Nesting Conditional Rules {#conditionals}
-----------------------------------------------
A style rule can have any number of <a>conditional rules</a> inside of it, of any type,
intermixed with any number of declarations, in any order.
The presence of a nested conditional engages the same logic as if ''@nest'' was present.
The nested conditional rules must contain a <a>nesting selector</a> and follow the same
rules as outlined in <a href="#direct">direct nesting</a>.
<div class="example">
For example, the following conditional nestings are valid:
<pre class="lang-css">
.foo {
display: grid;
@media (orientation: landscape) {
& {
grid-auto-flow: column;
}
}
}
/* equivalent to
.foo { display: grid; }
@media (orientation: landscape) {
.foo {
grid-auto-flow: column;
}
}
*/
.foo {
display: grid;
@media (orientation: landscape) {
& {
grid-auto-flow: column;
}
@media (min-inline-size > 1024px) {
& {
max-inline-size: 1024px;
}
}
}
}
/* equivalent to
.foo { display: grid; }
@media (orientation: landscape) {
.foo {
grid-auto-flow: column;
}
}
@media (orientation: landscape) and (min-inline-size > 1024px) {
.foo {
max-inline-size: 1024px;
}
}
*/
.foo {
color: red;
@media (min-width: 480px) {
& > .bar,
& > .baz {
color: blue;
}
}
}
/* equivalent to
.foo { color: red; }
@media (min-width: 480px) {
.foo > :is(.bar, .baz) {
color: blue;
}
}
*/
</pre>
But the following are invalid:
<pre class=lang-css>
.foo {
display: grid;
@media (orientation: landscape) {
grid-auto-flow: column;
}
}
/* Invalid because there's no nesting selector */
.foo {
color: red;
@media (min-width: 480px) {
& h1, h2 { color: blue; }
}
}
/* Invalid because not all selectors in the list
contain a nesting selector */
.foo {
color: red;
@nest @media (min-width: 480px) {
& { color: blue; }
}
}
/* Invalid because @nest expects a selector prelude,
instead a conditional rule was provided */
</pre>
</div>
Mixing Nesting Rules and Declarations {#mixing}
-----------------------------------------------
A style rule can have any number of <a>nested style rules</a> inside of it,
of either type,
intermixed with any number of declarations,
in any order.
The relative ordering of <a>nested style rules</a>, <a>nested conditional rules</a> and
other declarations <strong>is</strong> important;
it's possible for a given style rule and a <a>nested style rule</a> within it to match the same element,
and if the specificity of the two rules is otherwise equivalent,
the relative order in the stylesheet of the applicable declarations
determines which declaration "wins" the <a>cascade</a>.
<div class="example">
For example, consider the following where the specificity is the same and the
cascade is used for value resolution:
<pre class="lang-css">
article {
& .blue { color: blue; } /* (0,1,1) */
& .red { color: red; } /* (0,1,1) */
}
/* equivalent to
article .blue { color: blue; }
article .red { color: red; }
*/
</pre>
<pre class="lang-html">
<article>
<div class="red blue"></div>
</article>
<!-- div text color is red -->
</pre>
The specificity of ''article .blue'' and ''article .red'' is (0,1,1).
Now consider this follow up which adds an ID selector:
<pre class="lang-css">
article {
& .blue, & #gotcha { color: blue; } /* (1,1,1) */
& .red { color: red; } /* (0,1,1) */
}
/* equivalent to
article :is(.blue, #gotcha) { color: blue; }
article .red { color: red; }
*/
</pre>
<pre class="lang-html">
<article>
<div class="red blue"></div>
</article>
<!-- div text color is blue -->
</pre>
''article :is(.blue, #gotcha)'' now has a specificity score of (1,1,1).
Nesting uses '':is()'' which assumes the specificity of it's highest selector.
To work around it, the following could be written instead:
<pre class="lang-css">
article {
& .blue { color: blue; } /* (0,1,1) */
& .red { color: red; } /* (0,1,1) */
& #gotcha { color: blue; } /* (1,0,1) */
}
/* equivalent to
article .blue { color: blue; }
article .red { color: red; }
article #gotcha { color: blue; }
*/
</pre>
<pre class="lang-html">
<article>
<div class="red blue"></div>
</article>
<!-- div text color is red -->
</pre>
</div>
<!--
██████ ██████ ██████ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ███ ███
██ ██ ██ ██ ██ ████ ████
██ ██████ ██████ ██ ██ ██ ███ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██████ ██████ ███████ ██ ██
-->
CSSOM {#cssom}
==============
Modifications to {{CSSStyleRule}} {#cssom-style}
---------------------------------------------
CSS style rules gain the ability to have nested rules:
<pre class=idl>
partial interface CSSStyleRule {
[SameObject] readonly attribute CSSRuleList cssRules;
unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
undefined deleteRule(unsigned long index);
};
</pre>
The <dfn attribute for=CSSStyleRule>cssRules</dfn> attribute
must return a {{CSSRuleList}} object for the [=CSSRule/child CSS rules=].
The <dfn method for=CSSStyleRule>insertRule(<var>rule</var>, <var>index</var>)</dfn> method
must return the result of
invoking [=insert a CSS rule=] <var>rule</var>
into the [=CSSRule/child CSS rules=] at <var>index</var>.
The <dfn method for=CSSStyleRule>deleteRule(<var>index</var>)</dfn> method
must [=remove a CSS rule=] from the [=CSSRule/child CSS rules=] at <var>index</var>.
The {{CSSNestingRule}} Interface {#cssom-nesting}
-------------------------------------------------
The {{CSSNestingRule}} interfaces represents a ''@nest'' rule:
<pre class=idl>
[Exposed=Window]
interface CSSNestingRule : CSSRule {
attribute CSSOMString selectorText;
[SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
[SameObject] readonly attribute CSSRuleList cssRules;
unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
undefined deleteRule(unsigned long index);
};
</pre>
The <dfn attribute for=CSSNestingRule>selectorText</dfn> attribute,
on getting,
must return the result of [=serialize a group of selectors|serializing=]
the associated [=selector list=].
On setting the {{CSSStyleRule/selectorText}} attribute these steps must be run:
1. Run the <a>parse a group of selectors</a> algorithm on the given value.
2. If the algorithm returns a non-null value replace the associated [=selector list=] with the returned value.
3. Otherwise, if the algorithm returns a null value, do nothing.
The <dfn attribute for=CSSNestingRule>style</dfn> attribute
must return a {{CSSStyleDeclaration}} object for the style rule,
with the following properties:
<dl>
: [=CSSStyleDeclaration/computed flag=]
:: Unset.
: [=CSSStyleDeclaration/declarations=]
:: The declared declarations in the rule, in <a>specified order</a>.
: [=CSSStyleDeclaration/parent CSS rule=]
:: The <a>context object</a>.
: [=CSSStyleDeclaration/owner node=]
:: Null.
</dl>
The <dfn attribute for=CSSNestingRule>cssRules</dfn> attribute
must return a {{CSSRuleList}} object for the [=CSSRule/child CSS rules=].
The <dfn method for=CSSNestingRule>insertRule(<var>rule</var>, <var>index</var>)</dfn> method
must return the result of
invoking [=insert a CSS rule=] <var>rule</var>
into the [=CSSRule/child CSS rules=] at <var>index</var>.
The <dfn method for=CSSNestingRule>deleteRule(<var>index</var>)</dfn> method
must [=remove a CSS rule=] from the [=CSSRule/child CSS rules=] at <var>index</var>.