@@ -9,7 +9,13 @@ ED: https://drafts.csswg.org/css-nesting/
9
9
Editor : Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
10
10
Editor : Adam Argyle, Google, https://nerdy.dev, w3cid 112669
11
11
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.
12
- Link Defaults : css-color-4 (property) color
12
+ </pre>
13
+
14
+ <pre class=link-defaults>
15
+ spec:css-color-4; type:property; text:color
16
+ spec:cssom-1; type:dfn;
17
+ text:child css rules
18
+ text:specified order
13
19
</pre>
14
20
15
21
<h2 id="intro">
@@ -28,17 +34,6 @@ Module Interactions</h3>
28
34
This module introduces new parser rules that extend the [[!CSS21]] parser model.
29
35
This module introduces selectors that extend the [[SELECTORS4]] module.
30
36
31
- <pre class=idl>
32
- [Exposed=Window]
33
- interface CSSNestingRule : CSSRule {
34
- attribute CSSOMString selectorText;
35
- [SameObject] readonly attribute CSSRuleList cssRules;
36
- [SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
37
- unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
38
- undefined deleteRule(unsigned long index);
39
- };
40
- </pre>
41
-
42
37
<h3 id="values">
43
38
Values</h3>
44
39
@@ -256,8 +251,8 @@ Direct Nesting {#direct}
256
251
257
252
// and
258
253
259
- .foo {
260
- color: blue;
254
+ .foo {
255
+ color: blue;
261
256
padding: 2ch;
262
257
}
263
258
*/
@@ -328,7 +323,7 @@ Direct Nesting {#direct}
328
323
color: red;
329
324
&&.bar { color: blue; }
330
325
}
331
- /* Invalid because the second & is no longer
326
+ /* Invalid because the second & is no longer
332
327
at the start of a compound selector */
333
328
334
329
.foo {
@@ -449,9 +444,9 @@ Nesting Conditional Rules {#conditionals}
449
444
A style rule can have any number of <a>conditional rules</a> inside of it, of any type,
450
445
intermixed with any number of declarations, in any order.
451
446
452
- The presence of a nested conditional engages the same logic as if ''@nest'' was present.
453
- The nested conditional rules must contain a <a>nesting selector</a> and follow the same
454
- rules as outlined in <a href="#direct">direct nesting</a> .
447
+ The presence of a nested conditional engages the same logic as if ''@nest'' was present.
448
+ The nested conditional rules must contain a <a>nesting selector</a> and follow the same
449
+ rules as outlined in <a href="#direct">direct nesting</a> .
455
450
456
451
<div class="example">
457
452
For example, the following conditional nestings are valid:
@@ -461,17 +456,17 @@ Nesting Conditional Rules {#conditionals}
461
456
display: grid;
462
457
463
458
@media (orientation: landscape) {
464
- & {
465
- grid-auto-flow: column;
459
+ & {
460
+ grid-auto-flow: column;
466
461
}
467
462
}
468
463
}
469
464
/* equivalent to
470
465
.foo { display: grid; }
471
466
472
467
@media (orientation: landscape) {
473
- .foo {
474
- grid-auto-flow: column;
468
+ .foo {
469
+ grid-auto-flow: column;
475
470
}
476
471
}
477
472
*/
@@ -480,8 +475,8 @@ Nesting Conditional Rules {#conditionals}
480
475
display: grid;
481
476
482
477
@media (orientation: landscape) {
483
- & {
484
- grid-auto-flow: column;
478
+ & {
479
+ grid-auto-flow: column;
485
480
}
486
481
487
482
@media (min-inline-size > 1024px) {
@@ -496,13 +491,13 @@ Nesting Conditional Rules {#conditionals}
496
491
.foo { display: grid; }
497
492
498
493
@media (orientation: landscape) {
499
- .foo {
500
- grid-auto-flow: column;
494
+ .foo {
495
+ grid-auto-flow: column;
501
496
}
502
497
}
503
498
504
499
@media (orientation: landscape) and (min-inline-size > 1024px) {
505
- .foo {
500
+ .foo {
506
501
max-inline-size: 1024px;
507
502
}
508
503
}
@@ -513,7 +508,7 @@ Nesting Conditional Rules {#conditionals}
513
508
514
509
@media (min-width: 480px) {
515
510
& > .bar,
516
- & > .baz {
511
+ & > .baz {
517
512
color: blue;
518
513
}
519
514
}
@@ -522,7 +517,7 @@ Nesting Conditional Rules {#conditionals}
522
517
.foo { color: red; }
523
518
524
519
@media (min-width: 480px) {
525
- .foo > :is(.bar, .baz) {
520
+ .foo > :is(.bar, .baz) {
526
521
color: blue;
527
522
}
528
523
}
@@ -543,7 +538,7 @@ Nesting Conditional Rules {#conditionals}
543
538
544
539
.foo {
545
540
color: red;
546
-
541
+
547
542
@media (min-width: 480px) {
548
543
& h1, h2 { color: blue; }
549
544
}
@@ -553,12 +548,12 @@ Nesting Conditional Rules {#conditionals}
553
548
554
549
.foo {
555
550
color: red;
556
-
551
+
557
552
@nest @media (min-width: 480px) {
558
553
& { color: blue; }
559
554
}
560
555
}
561
- /* Invalid because @nest expects a selector prelude,
556
+ /* Invalid because @nest expects a selector prelude,
562
557
instead a conditional rule was provided */
563
558
</pre>
564
559
</div>
@@ -571,15 +566,15 @@ Mixing Nesting Rules and Declarations {#mixing}
571
566
intermixed with any number of declarations,
572
567
in any order.
573
568
574
- The relative ordering of <a>nested style rules</a> , <a>nested conditional rules</a> and
569
+ The relative ordering of <a>nested style rules</a> , <a>nested conditional rules</a> and
575
570
other declarations <strong> is</strong> important;
576
571
it's possible for a given style rule and a <a>nested style rule</a> within it to match the same element,
577
572
and if the specificity of the two rules is otherwise equivalent,
578
573
the relative order in the stylesheet of the applicable declarations
579
574
determines which declaration "wins" the <a>cascade</a> .
580
575
581
576
<div class="example">
582
- For example, consider the following where the specificity is the same and the
577
+ For example, consider the following where the specificity is the same and the
583
578
cascade is used for value resolution:
584
579
585
580
<pre class="lang-css">
@@ -626,9 +621,9 @@ Mixing Nesting Rules and Declarations {#mixing}
626
621
<!-- div text color is blue -->
627
622
</pre>
628
623
629
- ''article :is(.blue, #gotcha)'' now has a specificity score of (1,1,1).
630
- Nesting uses '':is()'' which assumes the specificity of it's highest selector.
631
-
624
+ ''article :is(.blue, #gotcha)'' now has a specificity score of (1,1,1).
625
+ Nesting uses '':is()'' which assumes the specificity of it's highest selector.
626
+
632
627
633
628
To work around it, the following could be written instead:
634
629
@@ -657,10 +652,92 @@ Mixing Nesting Rules and Declarations {#mixing}
657
652
</div>
658
653
659
654
660
- CSS Object Model Modifications {#cssom}
661
- =======================================
655
+ <!--
656
+ ██████ ██████ ██████ ███████ ██ ██
657
+ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███
658
+ ██ ██ ██ ██ ██ ████ ████
659
+ ██ ██████ ██████ ██ ██ ██ ███ ██
660
+ ██ ██ ██ ██ ██ ██ ██
661
+ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
662
+ ██████ ██████ ██████ ███████ ██ ██
663
+ -->
662
664
663
- <div class=issue>
664
- 1. Add an interface for the @nest rule.
665
- 2. Tie into the general work needed to let rules be nested into style rules.
666
- </div>
665
+ CSSOM {#cssom}
666
+ ==============
667
+
668
+ Modifications to {{CSSStyleRule}} {#cssom-style}
669
+ ---------------------------------------------
670
+
671
+ CSS style rules gain the ability to have nested rules:
672
+
673
+ <pre class=idl>
674
+ partial interface CSSStyleRule {
675
+ [SameObject] readonly attribute CSSRuleList cssRules;
676
+ unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
677
+ undefined deleteRule(unsigned long index);
678
+ };
679
+ </pre>
680
+
681
+ The <dfn attribute for=CSSStyleRule>cssRules</dfn> attribute
682
+ must return a {{CSSRuleList}} object for the [=CSSRule/child CSS rules=] .
683
+
684
+ The <dfn method for=CSSStyleRule>insertRule(<var>rule</var>, <var>index</var>)</dfn> method
685
+ must return the result of
686
+ invoking [=insert a CSS rule=] <var> rule</var>
687
+ into the [=CSSRule/child CSS rules=] at <var> index</var> .
688
+
689
+ The <dfn method for=CSSStyleRule>deleteRule(<var>index</var>)</dfn> method
690
+ must [=remove a CSS rule=] from the [=CSSRule/child CSS rules=] at <var> index</var> .
691
+
692
+
693
+ The {{CSSNestingRule}} Interface {#cssom-nesting}
694
+ -------------------------------------------------
695
+
696
+ The {{CSSNestingRule}} interfaces represents a ''@nest'' rule:
697
+
698
+ <pre class=idl>
699
+ [Exposed=Window]
700
+ interface CSSNestingRule : CSSRule {
701
+ attribute CSSOMString selectorText;
702
+ [SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
703
+ [SameObject] readonly attribute CSSRuleList cssRules;
704
+ unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
705
+ undefined deleteRule(unsigned long index);
706
+ };
707
+ </pre>
708
+
709
+ The <dfn attribute for=CSSNestingRule>selectorText</dfn> attribute,
710
+ on getting,
711
+ must return the result of [=serialize a group of selectors|serializing=]
712
+ the associated [=selector list=] .
713
+ On setting the {{CSSStyleRule/selectorText}} attribute these steps must be run:
714
+
715
+ 1. Run the <a>parse a group of selectors</a> algorithm on the given value.
716
+ 2. If the algorithm returns a non-null value replace the associated [=selector list=] with the returned value.
717
+ 3. Otherwise, if the algorithm returns a null value, do nothing.
718
+
719
+ The <dfn attribute for=CSSNestingRule>style</dfn> attribute
720
+ must return a {{CSSStyleDeclaration}} object for the style rule,
721
+ with the following properties:
722
+
723
+ <dl>
724
+ : [=CSSStyleDeclaration/computed flag=]
725
+ :: Unset.
726
+ : [=CSSStyleDeclaration/declarations=]
727
+ :: The declared declarations in the rule, in <a>specified order</a> .
728
+ : [=CSSStyleDeclaration/parent CSS rule=]
729
+ :: The <a>context object</a> .
730
+ : [=CSSStyleDeclaration/owner node=]
731
+ :: Null.
732
+ </dl>
733
+
734
+ The <dfn attribute for=CSSNestingRule>cssRules</dfn> attribute
735
+ must return a {{CSSRuleList}} object for the [=CSSRule/child CSS rules=] .
736
+
737
+ The <dfn method for=CSSNestingRule>insertRule(<var>rule</var>, <var>index</var>)</dfn> method
738
+ must return the result of
739
+ invoking [=insert a CSS rule=] <var> rule</var>
740
+ into the [=CSSRule/child CSS rules=] at <var> index</var> .
741
+
742
+ The <dfn method for=CSSNestingRule>deleteRule(<var>index</var>)</dfn> method
743
+ must [=remove a CSS rule=] from the [=CSSRule/child CSS rules=] at <var> index</var> .
0 commit comments