tsv_css 0.8.0

CSS formatter and parser compatible with Svelte's parseCss — part of tsv
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
// Value normalization utilities: semantic value formatting for the CSS printer.
//
// Internal AST stores semantic data + spans. When formatting we usually format
// semantically (normalize spacing, apply prettier rules); raw-source extraction
// uses `span.extract(source)` directly at the callsite when needed.

mod colors;
mod numbers;
mod splitting;

pub(crate) use colors::format_color_from_source;
pub(crate) use numbers::normalize_dimension_from_source;
pub(crate) use splitting::{
    collapse_whitespace_runs, has_closing_comma, normalize_css_whitespace, normalize_is_noop_in,
    split_args_by_comma, split_by_space_preserving_parens,
};

use std::borrow::Cow;

use super::boundary_ws::boundary_run_spelling;
use crate::color::is_hex_color_body;
use crate::escapes::escape_span_at;
use numbers::{canonical_unit, is_known_css_unit, normalize_css_number};
use tsv_lang::printing::format_string_literal;

/// Which of prettier's two value readers a stretch of raw CSS text goes through — the axis
/// every rule in [`normalize_value_text`] is keyed on.
///
/// The axis is the READER, not the construct: `parser-postcss.js` hands a **declaration
/// value** (line 195) and an `@supports` / `isModuleRuleName` **prelude** to the same
/// `parseValue`, and only `@media` (with `@custom-media`) to `parseMediaQuery`. tsv's own
/// at-rule routing (`parser/atrules/mod.rs`) tests the same pair, so a `@custom-media`
/// prelude reaches this function on the media path like a `@media` one. The two readers
/// tokenize the same text differently and print through different arms, so one authoring
/// can have two canonical forms, and a rule keyed on the wrong one is wrong on three
/// counts at once: the unit gate, the hex fold, and which runs absorb a following number.
///
/// `@container` is on neither list — prettier keeps its params raw — so it never reaches
/// this function and its prelude stays verbatim.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ValueReader {
    /// A declaration value, plus an `@supports` / `@import` prelude: `parseValue`
    /// (postcss-values-parser) → `value-*` nodes, printed as
    /// `printCssNumber(value) + printUnit(unit)` / `value-word`.
    ///
    /// A comment-free declaration value takes these rules through the value AST instead
    /// (each node's own printer); this arm is what a **comment-bearing** one takes, since
    /// CSS value comments live outside the AST and that value is re-emitted from source
    /// (`printer/declarations.rs`).
    Value,
    /// `@media` and `@custom-media`: `parseMediaQuery` → a node split (`media-type`, `media-feature`,
    /// `media-colon`, `media-value`, `media-keyword`, `media-url`, `media-unknown`), of which
    /// `media-type` and `media-value` print through `adjustNumbers` (`print/misc.js`), a regex
    /// pass over the raw text.
    ///
    /// **`media-feature` — name position — takes no number pass at all**, only
    /// `maybeToLowerCase(adjustStrings(value.replaceAll(/ +/g, " ")))`. Name position is
    /// everything before the expression's splitting `:`, and the **whole** interior when
    /// there is none, so the boolean `(0.50)` and range `(WIDTH >= 1.50)` /
    /// `(1.50 < width < 2.50)` forms are all name. [`feature_name_end`] draws that line for
    /// both passes — the number one here and the case fold in
    /// `lowercase_media_feature_names`.
    MediaQuery,
}

/// Normalize CSS numbers, hex colors, unit case and string quotes within a raw value text —
/// an at-rule prelude, or a declaration value the printer re-emits from source — mirroring
/// whichever of prettier's two value readers `path` names. `/* */` comments are copied
/// verbatim, and quoted strings get prettier's quote normalization (prefer single, swapping
/// only to minimize escaping), which is `adjustStrings` on both paths.
///
/// **Where the number pass runs at all** is the media path's first question, because
/// `parseMediaQuery` is a node split rather than one text and `adjustNumbers` prints only
/// `media-type` and `media-value`. A **`media-feature`** — everything up to a feature
/// expression's splitting `:`, and its whole interior when there is none — takes
/// `maybeToLowerCase(adjustStrings(…))` and no number pass, so `@media (1.50: 2.50)` is
/// `(1.50: 2.5)`: the name's number kept, the value's normalized. [`feature_name_end`] draws
/// that boundary, asked here at each top-level `(` and again by
/// [`lowercase_media_feature_names`] for the case fold — one line, two passes. The value
/// path has no such split: `parseValue` reads its whole text as one value.
///
/// **Numbers.** A number that is its own token normalizes to canonical form (`.50` → `0.5`)
/// on both paths; what follows it is where they part. [`ValueReader::Value`] prints a
/// `value-number` node as `printCssNumber(value) + printUnit(unit)` with **no gate on the
/// unit** — whatever trails the number is the unit and rides through unchanged, so `1.50abc`
/// is `1.5abc`. [`ValueReader::MediaQuery`] runs `adjustNumbers`' `(WORD_PART)?(NUMBER)(UNIT)?`
/// regex, which returns the **whole match verbatim** unless the unit it captured is empty,
/// the `<an+b>` `n`, or a unit CSS defines — so `1.50abc` stays. Unit *casing* parts with it:
/// the value path asks [`canonical_unit`] (`printUnit` of the author's spelling), the media
/// path lowercases the unit **before** the gate and prints `printUnit` of *that*, so the
/// `<an+b>` `n` folds there and not on the value path (`2N` → `2n` under `@media`, `2N` under
/// `@supports`) — [`prelude_unit`].
///
/// **A number abutting the run before it** is that run's own tail, not a token of its own:
/// the canonical form of a `.`-leading number carries a leading `0`, and appending it to an
/// ident code point hands the `0` to the run (`x1` + `.50` → `x10` + `.5`, two different
/// tokens). Such a pair is copied verbatim, unit included. *Which* runs count is the second
/// place the paths part. On the value path a word is one token through to its end, so an
/// ident, a `#`-token and a number's own unit all absorb. ⚠️ "Through to its end" is not a
/// character class, and this module's is too narrow twice over: postcss-values-parser's
/// `splitWord` **glues consecutive `word` tokens**, so punctuation that ends one word and
/// starts the next still leaves them one node (`x!1.50`, `x;1.50`, `x|1.50` all absorb); and
/// the tokenizer's word-end set depends on the word's **first** character (`wordEndRe` for a
/// non-digit start, `numEndRe` — which adds `-` and a bare `/` — for a digit start), so
/// `x-1.50` is one word where `9-1.50` is two, and `x/1.50` is one where `9/1.50` is two.
/// No character class can hold that second axis. On the media path only a
/// `WORD_PART` does — `[$@]?[_a-z\u{80}-\u{FFFF}][\w\u{80}-\u{FFFF}-]*`, so a run whose tail
/// after a `$`/`@` is digits is no word part and the regex reads those digits as the head of
/// the number (`$1.50` → `$1.5`), while a number's unit and a `#` absorb nothing at all
/// (`1a` + `.50` → `1a0.5`, `#1.50` → `#1.5`).
///
/// **`#`-prefixed tokens.** A `#` followed by an ident code point or a valid escape is a
/// `<hash-token>` whose value is the ident sequence after it (CSS Syntax 3 §4.3.1); anything
/// else — `#.50` — leaves a bare `<delim-token>`. On [`ValueReader::Value`] a hex **color**
/// (`#` + exactly 3, 4, 6, or 8 ASCII hex digits) is lowercased (`#FFF` → `#fff`), matching
/// prettier's `value-word` arm; any other hash token — an off-length run (`#ABCDE`), a
/// non-hex token, or one inside a `selector(...)` group (a case-sensitive ID selector) — is
/// copied verbatim. On [`ValueReader::MediaQuery`] the `#` is not a token head at all:
/// `adjustNumbers` is a regex over raw text with no hash arm, so the `#` is copied as the
/// delimiter it is and the run after it takes the ordinary ident/number arms.
///
/// An unquoted `url(...)` is a `<url-token>` — opaque per CSS Syntax 3 §4.3.6, so its whole
/// content is copied verbatim (a path like `url(sprite1.50.png)` is never number/unit-normalized).
/// A quoted `url("…")` is a function with a `<string>` arg and takes normal quote normalization.
///
/// That opacity is a **value-reader** rule — `parse-value.js` special-cases the `url` func,
/// while `adjustNumbers` is a regex with no url concept and normalizes straight through one.
/// So on the media path the arm is dropped in **`media-value` position only**
/// (`@media (a: url(1.50))` → `url(1.5)`): a `url()` in *name* position is inside a
/// `media-feature`, which takes no number pass, and one at depth 0 is a `media-url` node,
/// which `adjustNumbers` never prints either — both keep their content
/// (`@media (url(1.50): a)`, `@custom-media --a url(1.50)`). The `#` arm below is the mirror
/// image: it *is* restricted to [`ValueReader::Value`], since `adjustNumbers` has no hash
/// concept at all.
pub(crate) fn normalize_value_text(input: &str, path: ValueReader) -> String {
    let bytes = input.as_bytes();
    let mut out = String::with_capacity(input.len());
    let mut i = 0;
    // `url(...)`/`selector(...)` context tracking, consulted only for `#`-tokens:
    // `preserve_from` is `Some(depth)` while inside the outermost such group, so a
    // `#`-token there is preserved rather than lowercased as a hex color.
    let mut paren_depth = 0usize;
    let mut preserve_from: Option<usize> = None;
    // Whether the identifier just emitted was `url`/`selector`, so a `(` that
    // immediately follows opens a preserve group (a CSS function token requires the
    // `(` to abut the name — any char between, incl. whitespace, breaks it).
    let mut prev_ident_preserve_fn = false;
    // Whether the run just emitted ends in a character that would absorb a digit appended
    // to it, so a number abutting it here is that run's own tail rather than a value of
    // its own — see the number arm.
    let mut prev_run_absorbs_digit = false;
    // The media reader's NODE SPLIT, for the number pass: the offset where the feature
    // expression currently open stops being `media-feature` NAME text — its splitting
    // colon, or its own `)` when it has none. Set at each **top-level** `(` on the media
    // path and never cleared, which needs no reset: it is always inside (or at the end of)
    // the group that set it, so `i < media_name_until` is false everywhere after that
    // group closes and at every position on the value path, where it stays 0.
    let mut media_name_until = 0usize;

    while i < bytes.len() {
        let b = bytes[i];
        let after_preserve_ident = prev_ident_preserve_fn;
        prev_ident_preserve_fn = false;
        let after_absorbing_run = prev_run_absorbs_digit;
        prev_run_absorbs_digit = false;

        // An escape is opaque — its payload is content, never structure (§4.3.7, and
        // `crate::escapes::escape_len` for the extent, terminator included). Copied whole
        // and ahead of every arm below, because each of them would otherwise read the
        // payload byte as the thing it is looking for:
        //
        // - a `\"` / `\'` opened the string arm, which then ran to the *next* real quote
        //   and re-quoted everything between (`(a: x\"y "z")` → `(a: x\'y 'z")`) — the
        //   escape's payload rewritten and the string delimiters moved, output tsv's own
        //   parser then rejects (`Unterminated string`);
        // - a `\#` opened the `#`-token arm, so an escaped hash *inside an ident* was
        //   lowercased as a hex colour (`x\#FFF` → `x\#fff`, a different ident);
        // - a `\/` opened the comment arm, and a hex escape's digits were read as a
        //   number.
        //
        // ⚠️ It does NOT widen the identifier run below, which deliberately stops at an
        // escape. Prettier's prelude passes read an escape's payload as ordinary text too,
        // so stopping here is what keeps the two agreeing: `\41 2.50px` is emitted as
        // `\41 2.5px` by both, where reading the ident sequence's real extent (`A2`, then
        // `.50px`) would hand the number arm a word part to glue against and produce
        // `\41 20.5px` — the ident `A20`. The escape-spelled function name that costs is
        // the `url` arm below, where prettier is blind in the same direction.
        if let Some(end) = escape_span_at(input, i) {
            out.push_str(&input[i..end]);
            i = end;
            continue;
        }

        // Normalize quoted-string quotes (handling backslash escapes). A properly
        // closed string runs through prettier's quote chooser; an unterminated run
        // (malformed input) is copied verbatim.
        if b == b'"' || b == b'\'' {
            let start = i;
            // The extent is `crate::lexer::string_end`'s, not a loop of this scanner's own
            // — see `skip_string` below, which asks the same question for `trivia_span_at`.
            // Either failure arm means the run never closed, and a malformed run is copied
            // verbatim rather than re-quoted.
            let closed = crate::lexer::string_end(bytes, start).ok();
            i = closed.unwrap_or(input.len());
            let literal = &input[start..i];
            if closed.is_some() {
                let content = &literal[1..literal.len() - 1];
                out.push_str(&format_string_literal(content, b as char));
            } else {
                out.push_str(literal);
            }
            continue;
        }

        // Copy block comments verbatim, taking the extent from `crate::comments` — the
        // one definition, shared with the media-feature scanner below and the value
        // scanners.
        if crate::comments::is_comment_start(bytes, i) {
            let start = i;
            i = crate::comments::comment_end(input.as_bytes(), i);
            out.push_str(&input[start..i]);
            continue;
        }

        let Some(ch) = input[i..].chars().next() else {
            break;
        };

        // Copy identifiers verbatim — including any digits they contain, so a
        // number attached to a word (`foo2`, `min-width`) is never normalized.
        if is_ident_start(ch) {
            let start = i;
            i += ch.len_utf8();
            while let Some(c) = input[i..].chars().next() {
                if is_ident_continue(c) {
                    i += c.len_utf8();
                } else {
                    break;
                }
            }
            // How much of the run this path emits, and whether a number abutting it merges
            // into it — the two readers' answers stated side by side in `ident_run_split`.
            // The media path can end the run short of what the loop above read.
            let (emit_len, absorbs) = ident_run_split(&input[start..i], path);
            i = start + emit_len;
            let ident = &input[start..i];
            out.push_str(ident);

            // An unquoted `url(...)` is a `<url-token>` — opaque per CSS Syntax 3
            // §4.3.6, so its content (a path, maybe with number/unit-looking runs like
            // `sprite1.50.png`) is copied verbatim, never number/unit-normalized. A
            // quoted `url("…")` is instead a function with a `<string>` arg; it flows
            // through the normal string branch (quote normalization), so leave it.
            //
            // ⚠️ The name is matched on the run's own bytes, so an **escape-spelled** one
            // (`u\72 l(`, the same `<url-token>`) is missed and its content normalizes.
            // That is **parity, not a gap**: prettier's prelude passes match the name
            // literally too, so `u\72 l(1.50)` is `1.5` and `u\72 l(#FFF)` is `#fff` on
            // both sides. Teaching this arm the ident sequence's real extent would make
            // tsv the only one preserving them. The parser's own recognition, which does
            // decode the escape (`printer::values::function_name_is`), answers a different
            // question — an `@import` prelude the lexer tokenized, not this raw text.
            //
            // The opacity is the VALUE reader's rule, so the media path keeps it everywhere
            // its number pass does not run — name position and depth 0 — and drops it in
            // `media-value` position, where `adjustNumbers` walks the node's raw text with
            // no url concept.
            let media_value_text = path == ValueReader::MediaQuery
                && media_node_at(paren_depth, i, media_name_until) == MediaNode::Value;
            if ident.eq_ignore_ascii_case("url")
                && bytes.get(i) == Some(&b'(')
                && !url_arg_is_quoted(input, i)
                && !media_value_text
            {
                out.push_str(consume_paren_group(input, &mut i));
                continue;
            }

            // A `selector(...)` that immediately follows opens a hex-preserve group:
            // an ID selector's `#` is case-sensitive (numbers inside stay on the normal
            // path — the documented `selector()` over-normalization).
            prev_ident_preserve_fn = ident.eq_ignore_ascii_case("selector");
            prev_run_absorbs_digit = absorbs;
            continue;
        }

        // A `#`-prefixed token, on the value path only. `#` + an ident sequence is a
        // `<hash-token>` (CSS Syntax 3 §4.3.1), and postcss-values-parser reads that whole
        // word as one `value-word`: a hex color (`#` + 3/4/6/8 hex digits) lowercases,
        // matching prettier, and everything else — an off-length run (`#ABCDE`), a non-hex
        // token, an exponent-looking one (`#1e2`), or a `#` inside a `selector(...)` group
        // (a case-sensitive ID) — is copied verbatim. An unquoted `url(...)` never reaches
        // here (opaque, consumed above).
        //
        // ⚠️ The media path has **no hash arm at all** — `adjustNumbers` is a regex over raw
        // text and knows nothing of hash tokens, so the `#` falls through to the delimiter
        // arm below and the run after it takes the ordinary ident/number arms
        // (`#1.50` → `#1.5`, `#abc1.50` verbatim behind its word part). Giving the media
        // path this arm is what produced `#10.5`: the hash token ended at `#1`, the `.50`
        // then normalized on its own, and its canonical leading `0` joined the token before
        // it — a hash whose value changed from `1` to `10`, matching neither oracle.
        if b == b'#' && path == ValueReader::Value {
            let start = i;
            i += 1;
            let body_start = i;
            // Two phases, because the two questions have different answers. **Whether** a
            // token starts here is CSS Syntax 3 §4.3.1's: `#` heads a `<hash-token>` only
            // when an ident code point follows, and otherwise is a bare `<delim-token>`
            // whose neighbour is a token of its own (`#.50` is `#` then `.50` → `#0.5`).
            // **How far** it then reaches is postcss-values-parser's, which is wider — see
            // `is_hash_word_char`.
            if let Some(c) = input[i..].chars().next()
                && is_css_ident_code_point(c)
            {
                i += c.len_utf8();
                while let Some(c) = input[i..].chars().next() {
                    if is_hash_word_char(c) {
                        i += c.len_utf8();
                    } else {
                        break;
                    }
                }
            }
            let body = &input[body_start..i];
            // ⚠️ The fold is asked of the **whole** word, not of a hex-shaped prefix of it.
            // A narrower body reads `#FFF.5` as the colour `#FFF` followed by `.5` and folds
            // it to `#fff.5` — recasing a token that is no colour at all, which is exactly
            // what "any other hash token is copied verbatim" exists to prevent.
            if preserve_from.is_none() && is_hex_color_body(body) {
                out.push('#');
                for c in body.chars() {
                    out.push(c.to_ascii_lowercase());
                }
            } else {
                out.push_str(&input[start..i]);
            }
            // A hash token absorbs an abutting number the way an ident run does — the word
            // runs on through it (`#1` + `.50` stays `#1.50`). A `#` that heads no token
            // (`#.50`, a bare `<delim-token>`: the next code point is no ident code point)
            // absorbs nothing, so the number after it normalizes on this path too.
            prev_run_absorbs_digit = !body.is_empty();
            continue;
        }

        // A number (not attached to an identifier — those are consumed above).
        let num_len = crate::number::number_part_len(&input[i..]);
        if num_len > 0 {
            let num_start = i;
            let num = &input[i..i + num_len];
            i += num_len;
            // Trailing unit. ⚠️ The two readers disagree and only one is implemented here:
            // `[a-z]+` is `adjustNumbers`' `STANDARD_UNIT_REGEX`, i.e. the **media** rule, where
            // `%` and operators do end the unit. The value reader's unit is
            // `word.replace(NUMBER_RE, '')` — the whole rest of the glued word — so `50%.5` is
            // the number `50` with unit `%.5` and `.50#FFF` the number `.50` with unit `#FFF`,
            // both of which prettier prints verbatim. `split_number_and_unit` (numbers.rs) is
            // the value rule, already correct, and serves the declaration-value path.
            // TODO: take the value rule from that sibling on `ValueReader::Value`.
            let unit_start = i;
            while i < bytes.len() && bytes[i].is_ascii_alphabetic() {
                i += 1;
            }
            let unit = &input[unit_start..i];
            // Inside a `selector(...)` group the digits are selector syntax — an
            // `<an+b>` term, an attribute value — not a `<number>` with a canonical
            // serialization, so they are copied verbatim like the `#`-token above.
            let normalizes = preserve_from.is_none()
                && match path {
                    // `[printCssNumber(node.value), printUnit(node.unit)]` — the value node
                    // carries whatever trailed the number as its unit and no gate reads it,
                    // so the number normalizes and the unit rides through (`1.50abc` →
                    // `1.5abc`). `printUnit` is `canonical_unit` below.
                    ValueReader::Value => true,
                    // `adjustNumbers` returns the whole `(WORD_PART)?(NUMBER)(UNIT)?` match
                    // verbatim unless the captured unit is empty, the `<an+b>` `n`, or one
                    // CSS defines — so `1abc` is left alone. And it runs on a `media-value`
                    // or a `media-type`, never on the `media-feature` NAME this number may
                    // sit in.
                    ValueReader::MediaQuery => {
                        media_node_at(paren_depth, num_start, media_name_until) != MediaNode::Name
                            && (unit.is_empty()
                                || unit.eq_ignore_ascii_case("n")
                                || is_known_css_unit(unit))
                    }
                };
            let normalized = normalizes
                .then(|| normalize_css_number(num))
                // A number abutting the run just emitted must not begin with a character
                // that run would absorb: the canonical form of a `.`-leading number carries
                // a leading `0`, so `x1` + `.50` came back as `x10` + `.5` — two different
                // tokens. The pair is copied through instead, which is what prettier's own
                // scan does (`print/misc.js`: a `(WORD_PART)?(NUMBER)(UNIT)?` match is
                // returned verbatim when the word part matched). A signed number is never at
                // risk, since the sign it keeps merges with nothing — `x+1.50` still
                // normalizes.
                .filter(|n| !(after_absorbing_run && n.starts_with(is_css_ident_code_point)));
            if let Some(normalized) = normalized {
                out.push_str(&normalized);
                out.push_str(&prelude_unit(unit, path));
            } else {
                out.push_str(num);
                out.push_str(unit);
            }
            // On the value path the number and its unit are one word, so a number abutting
            // *them* is that word's tail too (`1a` + `.50`, `1.5` + `.50`). Asked of what was
            // just emitted, which answers for either branch and lets the unit speak when
            // there is one. The media path's regex re-splits at the next number instead, so
            // a unit absorbs nothing there (`1a` + `.50` → `1a0.5`).
            prev_run_absorbs_digit =
                path == ValueReader::Value && out.ends_with(is_css_ident_code_point);
            continue;
        }

        // Track `selector(...)` nesting for the `#`-token rule above (an unquoted
        // `url(...)` is consumed opaquely in the ident branch and never reaches here).
        // A `(` right after a `selector` ident opens the outermost hex-preserve group;
        // its matching `)` closes it.
        match ch {
            '(' => {
                // A **top-level** `(` opens a media-feature expression, whose interior
                // `parseMediaFeature` cuts into a `media-feature` name and a `media-value`
                // at its splitting colon. Only the value half reaches `adjustNumbers`, so
                // the boundary is taken here, once per expression, and read by the number
                // and `url()` arms above. A nested `(` opens no expression of its own —
                // the split is per top-level group, which is why the colon
                // `feature_name_end` finds may sit at any depth inside one. Nor does a
                // `<function-token>`'s own paren: `@media calc(1.50)` is a `media-type`,
                // which `adjustNumbers` prints in full (`calc(1.5)`).
                if path == ValueReader::MediaQuery
                    && paren_depth == 0
                    && opens_feature_expression(bytes, i)
                {
                    let (group_end, _) = scan_paren_group(input, i);
                    media_name_until = i + feature_name_end(&input[i..group_end]);
                }
                paren_depth += 1;
                if after_preserve_ident && preserve_from.is_none() {
                    preserve_from = Some(paren_depth);
                }
            }
            ')' => {
                if preserve_from == Some(paren_depth) {
                    preserve_from = None;
                }
                paren_depth = paren_depth.saturating_sub(1);
            }
            _ => {}
        }
        out.push(ch);
        i += ch.len_utf8();
    }

    out
}

/// Which node of `parseMediaQuery`'s split byte `i` sits in — the one question both of
/// [`normalize_value_text`]'s media-path rules are keyed on.
#[derive(Clone, Copy, PartialEq, Eq)]
enum MediaNode {
    /// Query level — outside every paren: a `media-type`, `media-keyword` or `media-url`.
    /// Of these only a `media-type` takes `adjustNumbers`, and it is also the only one that
    /// can carry a bare number, so reporting them alike costs nothing; a `url()` here stays
    /// opaque because the url arm asks for [`MediaNode::Value`] and gets this.
    Type,
    /// A `media-feature`: name position, `maybeToLowerCase(adjustStrings(…))` and **no**
    /// number pass.
    Name,
    /// A `media-value`: `adjustNumbers` over the node's raw text, `url()` and all.
    ///
    /// Also what a media TYPE's own function call reports (`@media calc(url(1.50))`), the
    /// one place the three variants do not name the oracle's node kinds one-to-one. It is
    /// behaviourally exact: `adjustNumbers` walks a `media-type`'s raw text exactly as it
    /// walks a `media-value`'s, url and all — and prettier agrees, printing `calc(url(1.5))`.
    Value,
}

/// Read [`MediaNode`] off the two pieces of state the scan carries: its paren depth, and
/// `media_name_until` — the boundary [`normalize_value_text`] took from [`feature_name_end`]
/// at the enclosing expression's own `(`.
///
/// Only meaningful on [`ValueReader::MediaQuery`]; the value path has no split, leaves
/// `media_name_until` at 0, and its callers gate on the path before asking.
fn media_node_at(paren_depth: usize, i: usize, media_name_until: usize) -> MediaNode {
    if i < media_name_until {
        MediaNode::Name
    } else if paren_depth >= 1 {
        MediaNode::Value
    } else {
        MediaNode::Type
    }
}

/// `printUnit` of the unit `path`'s reader captured — the two readers capture it
/// differently and it shows in its **case**.
///
/// The value reader hands `printUnit` the author's own spelling, so an unknown unit comes
/// back unchanged (`2N` stays `2N`, `printUnit` finding no `n` in its map). `adjustNumbers`
/// lowercases the captured unit *before* the gate that admits the `<an+b>` `n` and prints
/// `printUnit` of that, so the media path folds it (`2N` → `2n`). Every *known* unit agrees
/// either way — [`canonical_unit`] lowercases it — and an unknown one never reaches here on
/// the media path at all, its gate having refused the whole match.
fn prelude_unit(unit: &str, path: ValueReader) -> Cow<'_, str> {
    match path {
        // `canonical_unit` lowercases a known unit (`PX`→`px`) and leaves the
        // `n`/empty/unknown cases untouched (none is a known unit).
        ValueReader::Value => canonical_unit(unit),
        ValueReader::MediaQuery if unit.bytes().any(|b| b.is_ascii_uppercase()) => {
            Cow::Owned(unit.to_ascii_lowercase())
        }
        ValueReader::MediaQuery => Cow::Borrowed(unit),
    }
}

/// Whether the `url(` opening at `open` (the `(` byte index) is immediately
/// followed — after optional whitespace — by a quote, i.e. `url("…")`/`url('…')` (a
/// function with a `<string>` arg) rather than an unquoted `<url-token>`.
fn url_arg_is_quoted(s: &str, open: usize) -> bool {
    let bytes = s.as_bytes();
    let mut j = open + 1;
    while j < bytes.len() && bytes[j].is_ascii_whitespace() {
        j += 1;
    }
    matches!(bytes.get(j), Some(b'"') | Some(b'\''))
}

/// Consume the balanced parenthesized group starting at the `(` at `*i`, advancing
/// `*i` past the matching `)` (or to end of input if unbalanced), and return the
/// consumed slice — used to copy an opaque `url(<url-token>)` verbatim.
///
/// **Escapes step whole.** §4.3.6 "Consume a url token" consumes an escape as url content, so
/// the token ends at the first *unescaped* `)` — a `\)` closes nothing. Counting it does not
/// merely end the copy early: everything past it rejoins the normalizing path and the URL's
/// own bytes are rewritten (`url(x\)1.50)` → `url(x\)1.5)`, `url(x\)y#FFF)` → `url(x\)y#fff)`),
/// which is the §4.3.6 opacity this arm exists to enforce, inverted.
///
/// ⚠️ A block comment is deliberately **not** stepped over, for the reason the value
/// scanners' own `matching_close_paren` gives at length (`parser/value/scan.rs`): this is a
/// *bounding* scan over url content, where `/*` opens nothing (§4.3.6), so reading one as a
/// comment would lose the closing paren of `url(foo/*bar)`. Escapes are the opposite case —
/// §4.3.6 names them explicitly.
fn consume_paren_group<'s>(s: &'s str, i: &mut usize) -> &'s str {
    let bytes = s.as_bytes();
    let start = *i;
    let mut depth = 0usize;
    while *i < s.len() {
        let b = bytes[*i];
        if let Some(end) = escape_span_at(s, *i) {
            *i = end;
            continue;
        }
        *i += 1;
        if b == b'(' {
            depth += 1;
        } else if b == b')' {
            depth -= 1;
            if depth == 0 {
                break;
            }
        }
    }
    &s[start..*i]
}

/// Can `ch` begin a CSS identifier? (letter, `_`, `$`, `@`, or non-ASCII)
fn is_ident_start(ch: char) -> bool {
    ch.is_alphabetic() || ch == '_' || ch == '$' || ch == '@' || !ch.is_ascii()
}

/// Can `ch` continue a CSS identifier? (`is_ident_start` plus digits and `-`)
fn is_ident_continue(ch: char) -> bool {
    is_ident_start(ch) || ch.is_ascii_digit() || ch == '-'
}

/// Is `ch` an **ident code point** as CSS Syntax 3 defines one — a letter, a digit, `-`,
/// `_`, or a non-ASCII code point at the **lexer's** threshold?
///
/// The strict counterpart of [`is_ident_continue`], which additionally admits `$` and `@`
/// so a preprocessor-flavoured prelude reads as one run rather than shattering. Those two
/// are `<delim-token>`s to CSS proper, so they end a token where a real ident code point
/// would extend one — the difference that decides whether appending a digit merges.
///
/// The non-ASCII half is `lexer::is_non_ascii_identifier_codepoint`, the crate's single
/// source for that threshold, rather than a bare `!ch.is_ascii()`: this predicate answers
/// what the *lexer* would join, and the lexer stops at the C1 controls (U+0080–U+009F).
fn is_css_ident_code_point(ch: char) -> bool {
    ch.is_ascii_alphanumeric()
        || ch == '-'
        || ch == '_'
        || crate::lexer::is_non_ascii_identifier_codepoint(ch)
}

/// Can `ch` continue the word a `<hash-token>` heads on the value path — the run whose whole
/// text the hex-colour fold is asked of?
///
/// [`is_css_ident_code_point`] decides whether the token starts at all (§4.3.1) and would
/// also end it, but how far it *reaches* has a different oracle: postcss-values-parser reads
/// a `#` word further than the spec's ident sequence, and the gap is exactly where the fold
/// went wrong. `#FFF.5` is `#FFF` + `.5` to the spec, so a spec-width body found the colour
/// `#FFF` inside a token that is none and recased it to `#fff.5`; prettier keeps the word
/// whole and preserves it.
///
/// ⚠️ **This class is a sample, not the rule, and it is too narrow.** The real oracle is the
/// postcss-values-parser tokenizer plus `splitWord`'s gluing of consecutive `word` tokens: the
/// word ends only where a **non-`word` token** intervenes — whitespace, `,`, `:`, `{`, `}`,
/// an operator (`+` / `-` / `*` / `/`), an atword, a string, a comment — or where a `(` makes
/// the run a function rather than a word. Everything else glues, so `#FFF!5`, `#FFF;5`,
/// `#FFF|5`, `#FFF~5`, `#FFF>5`, `#FFF[5`, `#FFF]5`, `#FFF&5`, `#FFF^5`, `#FFF?5`, `#FFF=5`,
/// `#FFF\5` and `#FFF<5` are each one word to prettier and none of them a colour — where this
/// class ends the word and tsv folds. The two error directions are not symmetric — a class too
/// wide only declines a fold prettier makes, a class too narrow **recases a token** — and
/// every one of those misses is on the recasing side.
///
/// The characters the class does hold (`.`, `/`, non-ASCII, and every ident code point) are
/// right; what it lacks is the ~20 punctuation characters above and the tokenizer's
/// digit-initial axis.
///
/// TODO: take the extent from the oracle's tokenizer rather than from this class. The
/// **AST** path has since done exactly that for the function-name question —
/// `parser::value::is_value_word_code_point` states the word class as the oracle's exclusion
/// set, measured code point by code point — so the transcription this TODO wants already
/// exists to copy from, and the remaining work is this path's own: a widening here changes
/// at-rule preludes and comment-bearing values, the recasing direction, so it owes its own
/// fixtures rather than a shared predicate. Note the two are not the same class even once
/// both are right: the AST path refuses `{` / `}` and admits `[` / `]`, which is the
/// oracle's asymmetry at a *function name*, while the list above is about how far a
/// `<hash-token>`'s word reaches.
fn is_hash_word_char(ch: char) -> bool {
    is_css_ident_code_point(ch) || ch == '.' || ch == '/'
}

/// Can `ch` begin `adjustNumbers`' `WORD_PART` — `[$@]?` **`[_a-z\u{80}-\u{FFFF}]`**
/// `[\w\u{80}-\u{FFFF}-]*`, the media path's notion of "a word precedes this number"?
///
/// [`is_ident_start`] without `$`/`@`: the regex allows those only as the optional single
/// character *before* the start class, never as the start itself.
fn is_media_word_part_start(ch: char) -> bool {
    is_ident_start(ch) && ch != '$' && ch != '@'
}

/// Split the ident run `run` the way `adjustNumbers`' regex reads it, for a run that a
/// number abuts.
///
/// Returns `(emit_len, absorbs)` — how much of `run` is the run proper, the rest being where
/// prettier's `NUMBER` begins, and whether a `WORD_PART` reaches the run's end (which is what
/// makes the regex return the abutting number's match verbatim).
///
/// `$`/`@` are in this module's ident class and in neither of the regex's two classes, so
/// they can only ever be `WORD_PART`'s optional leading character. Everything after the
/// **last** of them is continuation-class by construction — this run holds nothing else — so
/// a word part reaches the run's end exactly when that tail holds one character the start
/// class accepts. When none does, the tail is digits and `-`, and the regex's `NUMBER`
/// begins at the first digit in it: `a$1` + `.50` is the run `a$` then the number `1.50`
/// (→ `a$1.5`), and `$` + `.50` is the run `$` then `.50` (→ `$0.5`).
fn media_word_part_split(run: &str) -> (usize, bool) {
    let tail_start = run
        .char_indices()
        .filter(|&(_, c)| c == '$' || c == '@')
        .map(|(i, c)| i + c.len_utf8())
        .next_back()
        .unwrap_or(0);
    let tail = &run[tail_start..];
    if tail.chars().any(is_media_word_part_start) {
        return (run.len(), true);
    }
    let emit_len = tail
        .char_indices()
        .find(|&(_, c)| c.is_ascii_digit())
        .map_or(run.len(), |(i, _)| tail_start + i);
    (emit_len, false)
}

/// How `path`'s reader takes the ident run `run`: how much of it is the run proper — the
/// rest being where prettier's own `NUMBER` begins — and whether a number abutting it merges
/// into it.
///
/// On the value path postcss-values-parser reads a word through to its end, so the whole run
/// is emitted and the only question is whether its last code point is a CSS **ident** code
/// point (§"ident code point": letter, digit, `-`, `_`, non-ASCII). `$` and `@`, which this
/// module's deliberately permissive ident class also admits so preprocessor-flavoured
/// preludes stay readable, are `<delim-token>`s that absorb nothing — `$` + `.50` may safely
/// become `$0.5`.
///
/// On the media path the question is `adjustNumbers`' `WORD_PART` instead, which can end the
/// run short of what this module read — see [`media_word_part_split`].
fn ident_run_split(run: &str, path: ValueReader) -> (usize, bool) {
    match path {
        ValueReader::Value => (run.len(), run.ends_with(is_css_ident_code_point)),
        ValueReader::MediaQuery => media_word_part_split(run),
    }
}

/// How far does the ident **sequence** starting at `start` reach — [`is_ident_continue`]'s
/// class with escapes stepped whole?
///
/// CSS Syntax 3 §"Consume an ident sequence" appends an escape's code point to the name and
/// keeps going, so an escape both begins and continues a sequence: `a\:b` is the single
/// ident `a:b`, `--A\42 C` the single ident `--AB`. A scanner that stops at the `\` reads
/// one name as two fragments, and any rule keyed on the *name* — the `--` prefix that makes
/// a custom-media name case-sensitive, the name→value `:` — then reads the wrong one.
///
/// Returns `start` when nothing there can extend a sequence (a lone `\` that starts no
/// escape — trailing, or before a newline, §4.3.4), so a caller that advances by the result
/// must handle that case itself. Whether the first code point may *start* an ident is the
/// caller's question; this only measures the reach.
fn ident_sequence_end(s: &str, start: usize) -> usize {
    let bytes = s.as_bytes();
    let mut i = start;
    while i < s.len() {
        if bytes[i] == b'\\' {
            // A `\` that starts no escape extends nothing, so the run ends at it.
            let Some(end) = escape_span_at(s, i) else {
                break;
            };
            i = end;
            continue;
        }
        // `i` is a char boundary: the loop advances only by a whole escape or a whole char.
        let Some(ch) = s[i..].chars().next() else {
            break;
        };
        if !is_ident_continue(ch) {
            break;
        }
        i += ch.len_utf8();
    }
    i
}

/// Lowercase the **feature name** in an `@media`/`@import` media-query string,
/// matching prettier — which lowercases the `media-feature` name (`MIN-WIDTH` →
/// `min-width`) but preserves media types (`SCREEN`), the `and`/`or`/`not`/`only`
/// keywords, and feature *values* (`(orientation: LANDSCAPE)` keeps `LANDSCAPE`).
/// Run **after** [`normalize_value_text`] (numbers/units/strings already
/// canonicalized); this only adjusts identifier case.
///
/// Scope: only a **simple** parenthesized feature expression has its name lowercased —
/// a `(...)` group whose only nested `(`, if any, opens a **function call** in the
/// value (`(min-width: calc(…))`, `(width: min(…))`). A grouped/complex condition — a
/// nested `(` that opens a sub-condition (`(not (hover))`, `((a) and (b))`) — is left
/// verbatim, matching prettier's media-query parser, which treats those as
/// `media-unknown`. A `(` glued to an identifier is neither: it is a
/// `<function-token>`'s own paren, and a media TYPE holding one
/// (`@media CALC(A)`) is not a feature name and does not fold at all — see
/// [`opens_feature_expression`], the one test all three passes read. (One small
/// divergence:
/// prettier's parser partially lowercases the *first* feature in `((A) and (B))`; tsv
/// preserves the whole grouped condition for consistency — see
/// `media_grouped_feature_case_prettier_divergence`.)
///
/// Within a simple expression the feature name is everything in **name position** — before
/// the `:` (plain feature), or the whole interior where there's no `:` at all (boolean
/// `(hover)` and range `(width >= 600px)` / `(600px <= width)`, whose values are numeric);
/// [`feature_name_end`] draws that line. The value after a `:` is preserved, and so is a
/// name that is case-*sensitive* rather than a keyword — a custom media `--*` above all
/// (see [`feature_name_preserves_case`]).
pub(crate) fn lowercase_media_feature_names(query: &str) -> Cow<'_, str> {
    let bytes = query.as_bytes();
    // Cheap bail: nothing to lowercase without an uppercase ASCII letter.
    if !bytes.iter().any(u8::is_ascii_uppercase) {
        return Cow::Borrowed(query);
    }

    let mut out = String::with_capacity(query.len());
    let mut i = 0;
    while i < query.len() {
        // Comments/strings copy through verbatim (never lowercase their contents).
        if let Some(end) = trivia_span_at(query, i) {
            out.push_str(&query[i..end]);
            i = end;
            continue;
        }
        // So does an escape, whose payload is ident content: a `\(` at depth 0 is part of a
        // media *type*, not the `(` that opens a feature expression, and reading it as one
        // handed the rest of the query to `scan_paren_group` as a single opaque group —
        // `@media a\(b and (MIN-WIDTH: 1px)` kept its uppercase feature name.
        if let Some(end) = escape_span_at(query, i) {
            out.push_str(&query[i..end]);
            i = end;
            continue;
        }
        match bytes[i] {
            b'(' if opens_feature_expression(bytes, i) => {
                // A top-level `(` opens a media-feature-expression. A group that
                // contains a nested *sub-condition* `(` is grouped/complex — copy it
                // verbatim (prettier's parser treats it as `media-unknown`); a nested
                // function-call `(` in the value (`calc(`) does not count.
                let (end, has_nested) = scan_paren_group(query, i);
                if has_nested {
                    out.push_str(&query[i..end]);
                } else {
                    lowercase_simple_feature_expr(&query[i..end], &mut out);
                }
                i = end;
            }
            _ => {
                // Depth-0 content (media types, `and`/`or`/`not`/`only`, and a media
                // type's own function call): verbatim.
                let ch = query[i..].chars().next().unwrap_or('\0');
                out.push(ch);
                i += ch.len_utf8();
            }
        }
    }
    Cow::Owned(out)
}

/// Index just past a `'…'`/`"…"` string starting at `start`, or the end of input for an
/// unterminated one (either failure arm — a missing close quote or a trailing `\`).
///
/// The string twin of [`crate::comments::comment_end`]; the two are what
/// [`trivia_span_at`] is built from. Defers to [`crate::lexer::string_end`], the crate's
/// single statement of the string token's extent — a *printer* asking where a string ends
/// must get the same answer the lexer did, or a scanner reading a prelude disagrees with
/// the parse it is printing.
fn skip_string(s: &str, start: usize) -> usize {
    crate::lexer::string_end(s.as_bytes(), start).unwrap_or(s.len())
}

/// If a `/* … */` comment or a `'…'`/`"…"` string starts at byte `i`, return the index
/// just past it (so `s[i..end]` is the whole trivia token); otherwise `None`. The
/// single source of truth for "what is skippable trivia" shared by the media-feature
/// scanners below — each copies it through or skips it, but they must all agree on its
/// extent and on what opens it.
///
/// ⚠️ An **escape** is not trivia and is deliberately not here: it is *content*, and the two
/// media-feature scanners want it for opposite reasons — [`lowercase_media_feature_names`]
/// steps one whole to keep it out of the structure it scans for, while
/// [`lowercase_simple_feature_expr`] runs the ident sequence *through* it
/// ([`ident_sequence_end`]). A shared "skip this span" arm would give the second one the
/// first one's answer and split the name at the escape again.
fn trivia_span_at(s: &str, i: usize) -> Option<usize> {
    let bytes = s.as_bytes();
    match bytes[i] {
        b'/' if crate::comments::is_comment_start(bytes, i) => {
            Some(crate::comments::comment_end(bytes, i))
        }
        b'"' | b'\'' => Some(skip_string(s, i)),
        _ => None,
    }
}

/// Does the `(` at `i` open a **media-feature expression**, rather than being a
/// `<function-token>`'s own paren?
///
/// CSS Syntax 3 §"Consume an ident-like token": an ident sequence *immediately* followed by
/// `(` is one `<function-token>`, so a `(` glued to an ident byte is a call and everything
/// else — a `(` at the start, after whitespace, after another `(`, after a connector — opens
/// a `( <media-condition> )` per the Media Queries 4 grammar.
///
/// The single spelling of that question, read at three points: the nested-`(` classification
/// in [`scan_paren_group`], and the top-level `(` in each of the two passes over a media
/// prelude ([`normalize_value_text`]'s number pass and
/// [`lowercase_media_feature_names`]'s case fold). Both passes are keyed on it for the same
/// reason — `@media calc(1.50)` is a `media-type`, which takes `adjustNumbers` and no
/// lowercase, where an expression's name takes the lowercase and no numbers. Two spellings
/// of it is how the two passes came to disagree about where a name even starts.
fn opens_feature_expression(bytes: &[u8], i: usize) -> bool {
    // The byte before, if any, must not be one that can end an ident: ASCII alphanumeric,
    // `-`, `_`, or any non-ASCII byte (the tail of a multi-byte ident char).
    let ends_ident = |b: u8| b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b >= 0x80;
    i == 0 || !ends_ident(bytes[i - 1])
}

/// Scan a parenthesized group starting at the `(` at `open`. Returns
/// `(index_just_past_the_matching_close_paren, contains_a_nested_sub_condition)`,
/// skipping comments/strings. A nested `(` that is a function call in the value (`calc(`)
/// does **not** set the flag — see [`opens_feature_expression`]; only a `(` opening a
/// sub-condition does. For an unbalanced group, returns end-of-input.
fn scan_paren_group(s: &str, open: usize) -> (usize, bool) {
    let bytes = s.as_bytes();
    let mut i = open + 1;
    let mut depth = 1usize;
    let mut has_nested = false;
    while i < s.len() {
        // A `(`/`)` inside a comment or string doesn't change paren depth.
        if let Some(end) = trivia_span_at(s, i) {
            i = end;
            continue;
        }
        // Nor does one inside an escape: `a\(b` is the single ident `a(b` (§"Consume an ident
        // sequence"), not a nested sub-condition, so the group stays a *simple* feature
        // expression and its name still lowercases.
        if let Some(end) = escape_span_at(s, i) {
            i = end;
            continue;
        }
        match bytes[i] {
            b'(' => {
                // A `(` immediately after an identifier byte is a function call inside
                // a feature value (`calc(`, `min(`, `env(`), not a grouped
                // sub-condition — it must NOT make the feature opaque, since prettier
                // still lowercases the feature name (`(MIN-WIDTH: calc(…))` →
                // `(min-width: calc(…))`). Only a `(` that opens a real sub-condition
                // (preceded by `(`, whitespace, or a connector keyword) marks the group
                // as grouped/complex.
                if opens_feature_expression(bytes, i) {
                    has_nested = true;
                }
                depth += 1;
                i += 1;
            }
            b')' => {
                depth -= 1;
                i += 1;
                if depth == 0 {
                    return (i, has_nested);
                }
            }
            _ => i += 1,
        }
    }
    (s.len(), has_nested)
}

/// Where the feature **name** ends inside `group` (a whole `(…)` slice): the byte index of
/// the name→value `:`, or `group.len()` for a boolean or range feature, which has none.
///
/// The **first** colon at any depth inside the group, which is `parseMediaFeature`'s own
/// rule: it splits in `normal` mode and its parens do not protect it, so
/// `(calc(1:1.50): a)` splits *inside* the call — prettier prints `(calc(1: 1.5): a)`, the
/// `1.50` past the split being value text. The parser's node split reads it the same way
/// (`parse_raw_prelude_content`'s `feature_colon_seen`, set by the first `Colon` at
/// `paren_depth >= 1`), so printer and parser draw one line.
///
/// Trivia and escapes are stepped, so the `:` found is one that really separates a name from
/// a value — not one inside a comment or string, and not an escaped one (`(A\:B: 1px)` is a
/// name carrying a `:`, per [`ident_sequence_end`]). ⚠️ That is deliberately **narrower than
/// prettier**, whose split is a raw scan with no notion of either; the divergence it leaves
/// is sanctioned in `docs/conformance_prettier_css.md` §CSS: At-Rules.
fn feature_name_end(group: &str) -> usize {
    debug_assert!(
        group.starts_with('('),
        "a feature expression is its whole parenthesized group"
    );
    let bytes = group.as_bytes();
    let mut i = 0;
    // `group` opens with its own `(`, so the name starts at depth 1 — and a colon deeper
    // than that still splits, so the depth is only here to keep the group's own `(` from
    // reading as one.
    let mut depth = 0usize;
    while i < group.len() {
        if let Some(end) = trivia_span_at(group, i) {
            i = end;
            continue;
        }
        if let Some(end) = escape_span_at(group, i) {
            i = end;
            continue;
        }
        match bytes[i] {
            b'(' => depth += 1,
            b')' => depth = depth.saturating_sub(1),
            b':' if depth >= 1 => return i,
            _ => {}
        }
        i += 1;
    }
    group.len()
}

/// Does the feature name spelled by `name` keep its case?
///
/// Prettier asks this of the **whole** media-feature node (`maybeToLowerCase` in
/// `src/language-css/utilities/index.js`), and asking it of anything smaller is what let a
/// case-sensitive name half-fold: tsv used to test each identifier run for the `--` prefix,
/// so `(--A B: 1px)` preserved `--A` and lowercased `B`.
///
/// - `--` opens an `<extension-name>` (css-extensions-1), an author-defined name with no
///   canonical casing to fold to — unlike a plain feature name, which is a pre-defined
///   keyword and so ASCII case-insensitive (css-values-4 §"Pre-defined Keywords").
/// - `$`, `@`, `#` and a leading `%` are prettier's preprocessor accommodations (`$var`,
///   `@var`, `#{…}`, `%placeholder`). tsv's parser is permissive enough to reach them, and
///   folding the case of a name it does not understand can only lose information.
/// - A name holding a balanced call is likewise not a keyword.
///
/// Prettier's remaining `:--` arm is unreachable here: the name region ends at the first
/// separator `:`, so a name cannot begin with one.
fn feature_name_preserves_case(name: &str) -> bool {
    name.starts_with("--")
        || name.starts_with('%')
        || name.contains(['$', '@', '#'])
        || (name.contains('(') && name.contains(')'))
}

/// Emit a simple media-feature expression `(…)` (no nested sub-condition), lowercasing the
/// feature name. See [`lowercase_media_feature_names`] for the name-position rule.
fn lowercase_simple_feature_expr(group: &str, out: &mut String) {
    let name_end = feature_name_end(group);
    // Everything after `group`'s own `(`, up to the separator, is the name prettier tests.
    if feature_name_preserves_case(crate::escapes::trim_css(&group[1..name_end])) {
        out.push_str(group);
        return;
    }

    // The name lowercases; the value after the separator is preserved, so only the region
    // below `name_end` is walked for identifiers and the rest is copied through.
    let mut i = 0;
    while i < name_end {
        // Comments/strings copy through verbatim (their contents are never lowercased).
        // Neither can straddle `name_end`: `feature_name_end` steps trivia whole, so the
        // separator it found is never inside one.
        if let Some(end) = trivia_span_at(group, i) {
            out.push_str(&group[i..end]);
            i = end;
            continue;
        }
        let ch = group[i..].chars().next().unwrap_or('\0');
        // An identifier: the ident class, a leading `-` (custom media, vendor prefixes),
        // or a leading escape — an escape begins an ident sequence exactly as an ident
        // code point does, and `ident_sequence_end` runs through the ones inside it too.
        let end = if is_ident_start(ch) || ch == '-' || ch == '\\' {
            ident_sequence_end(group, i)
        } else {
            i
        };
        if end > i {
            out.push_str(&lowercase_feature_name(&group[i..end]));
            i = end;
        } else {
            // Not an ident start, or a lone `\` that starts no escape.
            out.push(ch);
            i += ch.len_utf8();
        }
    }
    debug_assert_eq!(i, name_end, "the name walk overshot the separator");
    out.push_str(&group[name_end..]);
}

/// Lowercase one identifier of a media-feature name. Whether the name is case-*sensitive*
/// is [`feature_name_preserves_case`]'s question, asked once of the whole name before any
/// of this runs; an already-lowercase identifier borrows unchanged.
fn lowercase_feature_name(name: &str) -> Cow<'_, str> {
    if name.bytes().any(|b| b.is_ascii_uppercase()) {
        Cow::Owned(name.to_ascii_lowercase())
    } else {
        Cow::Borrowed(name)
    }
}

/// Extract and normalize property name from declaration source
///
/// Handles property names with comments, preserving raw escapes (Svelte quirk).
/// Normalizes spacing around comments for readability.
///
/// # Arguments
/// * `decl_source` - Full declaration source (e.g., `color /* test */: red;`)
/// * `colon_pos` - Byte offset of the `property : value` colon within `decl_source`
///   (the parser's `CssDeclaration::colon_offset`, minus the declaration span start —
///   no re-scan; a property comment may itself hold a `:` that a naive scan would
///   mis-match, which the parser-recorded colon already skips)
/// * `has_block_comment` - The parser-recorded `CssDeclaration::has_block_comment`. False
///   proves the declaration holds no `/* … */` at all, so the comment path is skipped
///   without scanning the property text (the free O(1) negative gate
///   `has_value_comments_in_decl` uses).
///
/// # Returns
/// * Normalized property name (e.g., `color /* test */`)
///
/// The common case (a bare property name with no comment and no significant
/// trailing whitespace) returns a `Cow::Borrowed` sub-slice of `decl_source` — no
/// per-declaration allocation. Only the comment-bearing and hex-escape-terminator
/// forms reconstruct an owned `String`.
///
/// # Example
/// ```ignore
/// // colon_pos = the parser's recorded colon offset within decl_source
/// let source = "color/* comment */:red;";
/// assert_eq!(extract_property_name(source, 18, true), "color /* comment */");
///
/// // Every gap comment is preserved, joined single-spaced.
/// let source = "color/* a *//* b */:red;";
/// assert_eq!(extract_property_name(source, 19, true), "color /* a */ /* b */");
///
/// let source = "margin: 10px;";
/// assert_eq!(extract_property_name(source, 6, false), "margin");
/// ```
///
/// # Svelte Quirk
/// Property names preserve raw escapes without decoding.
/// Example: `\00e9motion` stays as `\00e9motion`, not `émotion`
///
/// # Formatter Divergence
/// We add spaces around comments for readability:
/// - Input: `color/* comment */:red;`
/// - Output: `color /* comment */` (normalized spacing)
/// - Prettier: `color/* comment */` (no space before comment)
///
/// # Boundary whitespace
/// The gap is an `allow_whitespace()` juncture to `parseCss` (`read_declaration` ends the
/// name at the first JS-`\s` code point), so a boundary run in it — `color<NBSP>: red`,
/// `top <NBSP>: 0`, `left<NBSP> /* c */ : 0` — is the gap's, not the name's, and this head
/// is what carries it back out: each gap's run is spelled where it stood, with the ASCII
/// whitespace beside it kept as one space (`boundary_run_spelling`). The trim that takes
/// the run off the name and the spelling that restores it read one class, so the run is
/// emitted exactly once.
pub(crate) fn extract_property_name(
    decl_source: &str,
    colon_pos: usize,
    has_block_comment: bool,
) -> Cow<'_, str> {
    let property_part = &decl_source[..colon_pos];

    // `has_block_comment` is recorded at parse time and is false iff the declaration
    // carries no `/* … */` anywhere — so the property→colon gap holds none either and the
    // substring scan below is provably fruitless. A literal `/*` cannot occur in a
    // property name except as a comment start (an escaped one is written `\2f\2a`), so
    // the gate can only skip work, never change the branch taken.
    if has_block_comment && let Some(comment_start) = property_part.find("/*") {
        let before_comment = trim_property_part(&property_part[..comment_start]);
        // The declaration's span starts at the property token, so the trim can only have
        // shortened the part's END — which makes the trimmed length the start of the gap
        // ahead of the first comment.
        debug_assert!(property_part.starts_with(before_comment));
        let mut out = String::from(before_comment);
        let mut gap_start = before_comment.len();

        // Collect EVERY comment in the property→colon gap, not just the first: a
        // declaration may carry two or more (`color /* a */ /* b */ :`), and this
        // gap is the only place a CSS declaration comment survives formatting (the
        // parser drops value comments), so emitting only the first is silent
        // content loss. Each comment is preceded by its gap's boundary run — spelled with
        // the author's ASCII presence, `boundary_run_spelling` — then a single space; the
        // gap after the last comment, ahead of the colon, takes the same spelling.
        let mut comment_pos = comment_start;
        loop {
            // `comment_pos` is at a `/*`.
            // `comment_end_checked`, not `comment_end`: a malformed comment abandons the
            // whole reconstruction rather than being taken as reaching end-of-input.
            let rest = &property_part[comment_pos..];
            let Some(comment_len) = crate::comments::comment_end_checked(rest.as_bytes(), 0) else {
                // Malformed comment (no closing `*/`) - just trim the whole part.
                return Cow::Borrowed(trim_property_part(property_part));
            };
            out.push_str(&boundary_run_spelling(
                &property_part[gap_start..comment_pos],
            ));
            out.push(' ');
            out.push_str(&rest[..comment_len]);
            gap_start = comment_pos + comment_len;
            match rest[comment_len..].find("/*") {
                Some(next) => comment_pos = gap_start + next,
                None => break,
            }
        }
        out.push_str(&boundary_run_spelling(&property_part[gap_start..]));
        Cow::Owned(out)
    } else {
        // No comment: trim insignificant whitespace, but a property name ending in a
        // hex escape (`\41`) consumes one following whitespace as the escape's
        // terminator. That whitespace is part of the identifier token, so prettier
        // keeps it before the `:` (`\41 : red`); any extra whitespace is still trimmed
        // (`color : red` → `color: red`).
        let bare = trim_property_part(property_part);
        // An untouched part — every real declaration — settles here without a second scan.
        if bare.len() == property_part.len() {
            return Cow::Borrowed(bare);
        }
        // The gap's own boundary run rides on the trimmed name, spelled with the author's
        // ASCII presence (`color<NBSP>`, `top <NBSP>`): the trim's class is the run's class,
        // so a part the trim shortened is the one place a run can be. The declaration's span
        // starts at the property token, so the trim can only have shortened the part's END —
        // what it took off is the tail, and a shortened part ends in the class by construction.
        debug_assert!(property_part.starts_with(bare));
        let run = boundary_run_spelling(&property_part[bare.len()..]);
        if !run.is_empty() {
            Cow::Owned(format!("{bare}{run}"))
        } else if crate::escapes::ends_with_live_hex_escape(bare) {
            Cow::Owned(format!("{bare} "))
        } else {
            Cow::Borrowed(bare)
        }
    }
}

/// [`str::trim`] over the class the printer's boundary claim scans back across.
///
/// ⚠️ **Not `str::trim`**, whose class is Unicode `White_Space` — which excludes `U+FEFF`,
/// while JS `\s` (and so `parseCss`'s own `.trim()` of the raw property text) includes it.
/// The gap this trims is also the gap `Printer::preserved_boundary_ws` re-emits from, and the
/// two have to agree on where the name ends or the character belongs to both: `color<ZWNBSP>:`
/// kept the run inside the name AND restored it beside it, doubling the run on every pass
/// (`1 → 2 → 4 → …`) — a non-idempotency no fixture can carry and no ratchet was watching.
/// `is_boundary_whitespace` is the class that scan uses, so asking it here is what makes the
/// two halves one answer.
///
/// The searchers run only for a part whose first or last byte could begin or end a run
/// (`byte_may_be_boundary_whitespace`); a real property name is `color`, an ASCII name with
/// nothing to trim at either end, and settles in four compares. The `debug_assert` keeps the
/// gate's claim under the fixture suite rather than in prose.
#[inline]
fn trim_property_part(property_part: &str) -> &str {
    let bytes = property_part.as_bytes();
    if let (Some(&first), Some(&last)) = (bytes.first(), bytes.last())
        && !crate::whitespace::byte_may_be_boundary_whitespace(first)
        && !crate::whitespace::byte_may_be_boundary_whitespace(last)
    {
        debug_assert_eq!(
            trim_property_part_wide(property_part).len(),
            property_part.len(),
            "an ASCII non-whitespace byte at each end proves there is nothing to trim"
        );
        return property_part;
    }
    trim_property_part_wide(property_part)
}

/// The searcher half of [`trim_property_part`] — one outlined copy.
///
/// A name ending in a **literal** escape owns the code point after its `\` (CSS Syntax 3
/// §4.3.4 / §4.3.7), so `a\ :` names the property `a ` and its space is the escape's payload,
/// not padding: the trim hands that one code point back rather than strand the backslash
/// onto the colon (`a\:`, which escapes the colon and no longer parses). A hex escape's
/// terminator is a different rule — the caller's `ends_with_live_hex_escape`.
#[inline(never)]
fn trim_property_part_wide(property_part: &str) -> &str {
    let trimmed = property_part.trim_matches(crate::whitespace::is_boundary_whitespace);
    if trimmed.len() == property_part.len()
        || !tsv_lang::printing::ends_with_open_escape(trimmed.as_bytes())
    {
        return trimmed;
    }
    // The part starts at the property token, so the trim shortened only its end and the
    // payload is the first code point past `trimmed`. A newline cannot be one (`\` + newline
    // is no escape), so the lexer never put one in the name.
    debug_assert!(property_part.starts_with(trimmed));
    let end = trimmed.len();
    match property_part[end..].chars().next() {
        Some(c) if !matches!(c, '\n' | '\r' | '\x0c') => &property_part[..end + c.len_utf8()],
        _ => trimmed,
    }
}

/// Canonicalize a property name's case: standard CSS property names are ASCII
/// case-insensitive (CSS Syntax 3), and prettier lowercases them (`COLOR`→`color`,
/// `Background-Color`→`background-color`, vendor `-WEBKIT-…`→`-webkit-…`). Returns
/// the input unchanged for the case-sensitive / non-standard / ambiguous forms:
/// - **custom properties** (`--*`) — case-sensitive per CSS Variables 1;
/// - **non-standard property starts** — any name not beginning with an ASCII letter
///   or a vendor-prefix `-`; these aren't CSS property names, so their case is left
///   untouched;
/// - names carrying a **comment** (`color /* c */`) or a **`\` escape** — left
///   verbatim so the lowercasing never touches comment text or an escape's hex
///   digits (both rare in a property position).
///
/// Takes the already-extracted name (see [`extract_property_name`]); only the ASCII
/// letters of a bare identifier are lowercased.
pub(crate) fn lowercase_property_name(name: Cow<'_, str>) -> Cow<'_, str> {
    // Cheapest discriminator first: an all-lowercase name (the overwhelming common
    // case) has nothing to do, so bail before the substring scans below — a word at a
    // time, since every name takes this walk and nearly none has anything to find.
    if !tsv_lang::swar::has_ascii_uppercase(name.as_bytes()) {
        return name;
    }
    // A standard property starts with an ASCII letter or a single vendor-prefix `-`
    // (`--` is a custom property, handled below).
    let standard_start = name
        .as_bytes()
        .first()
        .is_some_and(|&b| b.is_ascii_alphabetic() || b == b'-');
    if !standard_start || name.starts_with("--") || name.contains("/*") || name.contains('\\') {
        return name;
    }
    Cow::Owned(name.to_ascii_lowercase())
}

/// Lowercase an at-rule name (`@MEDIA` → `@media`, `@Font-Face` → `@font-face`),
/// matching prettier — which lowercases **all** at-rule names, including
/// vendor-prefixed ones (`@-WEBKIT-KEYFRAMES` → `@-webkit-keyframes`). An escaped
/// (`\`) name is left verbatim (lowercasing would corrupt an escape's hex digits);
/// an already-lowercase name borrows unchanged. The `@` is written separately by the
/// printer, so `name` is just the keyword.
///
/// Takes the name's **source** slice (`CssAtrule.name_span`), not the decoded
/// `CssAtrule.name`: an escaped name still carries its `\` here, so the escape-guard
/// below is what preserves it (the decoded form would hold a raw control char instead).
pub(crate) fn lowercase_at_rule_name(name: &str) -> Cow<'_, str> {
    if !tsv_lang::swar::has_ascii_uppercase(name.as_bytes()) || name.contains('\\') {
        return Cow::Borrowed(name);
    }
    Cow::Owned(name.to_ascii_lowercase())
}

/// Extract and format string value from declaration source
///
/// Preserves escape sequences by working with original source text.
///
/// # Arguments
/// * `decl_source` - Full declaration source (e.g., `content: "test\n";`)
/// * `colon_pos` - Byte offset of the declaration colon within `decl_source`
///   (the parser's recorded `colon_offset`, so no re-scan)
/// * `quote` - Quote character to use (' or ")
///
/// # Returns
/// * `Some(formatted_string)` if extraction successful
/// * `None` if the value isn't a well-formed quoted string (len < 2)
///
/// # Example
/// ```ignore
/// let source = "content: 'hello\\nworld';";
/// assert_eq!(extract_string_value(source, 7, '\''), Some("'hello\\nworld'".to_string()));
/// ```
pub(crate) fn extract_string_value(
    decl_source: &str,
    colon_pos: usize,
    quote: char,
) -> Option<String> {
    let value_part = decl_source[colon_pos + 1..].trim();
    // String should be quoted
    if value_part.len() >= 2 {
        let raw_content = &value_part[1..value_part.len() - 1];
        let formatted = format_string_literal(raw_content, quote);
        return Some(formatted);
    }

    None
}

/// Extract and normalize a declaration's post-colon value text, comments included.
///
/// The slicing half of [`normalize_value_with_comments`], which owns the rules: this only
/// says where a declaration's value text starts.
///
/// # Arguments
/// * `decl_source` - Full declaration source (e.g., `margin: 10px /* test */ 20px;`)
/// * `colon_pos` - Byte offset of the declaration colon within `decl_source`
///   (the parser's recorded `colon_offset`, so no re-scan)
///
/// # Returns
/// * the normalized post-colon value text (e.g., `10px /* test */ 20px`)
///
/// # Example
/// ```ignore
/// let source = "margin: 10px  /* test */  20px;";
/// assert_eq!(extract_value_with_comments(source, 6), "10px /* test */ 20px");
/// ```
pub(crate) fn extract_value_with_comments(decl_source: &str, colon_pos: usize) -> String {
    normalize_value_with_comments(&decl_source[colon_pos + 1..])
}

/// Normalize a stretch of declaration-value source the printer re-emits as text.
///
/// CSS value comments live outside the AST, so a comment-bearing value cannot become a doc
/// and the printer copies its text instead (`printer/declarations.rs`). Copying it is not
/// the same as printing it: prettier reads that value with `parseValue` either way
/// (`parser-postcss.js` line 195 — the reader an `@supports` prelude also takes), so its
/// numbers, hex colors, units and quotes normalize exactly as they do without a comment.
///
/// Two passes, because they answer different questions: [`normalize_css_whitespace`] for
/// the gaps the comments sit in, then [`normalize_value_text`] for the tokens between
/// them. This is the one name every declaration path asks both by — the width check
/// included, so a measure and its emit can never pick different spellings.
pub(crate) fn normalize_value_with_comments(value: &str) -> String {
    normalize_value_text(&normalize_css_whitespace(value), ValueReader::Value)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Stands in for the parser-recorded `CssDeclaration::has_block_comment` — true iff
    /// the declaration carries a `/* … */` anywhere. These test inputs hold no string or
    /// `url()` that could disguise a literal `/*`, so the substring check agrees with
    /// what the parser would record.
    fn has_block_comment(src: &str) -> bool {
        src.contains("/*")
    }

    #[test]
    fn test_extract_property_name_borrows_common_case() {
        // The overwhelmingly-common case: a bare property name with no comment and
        // no significant trailing whitespace borrows a sub-slice of the input — no
        // per-declaration allocation.
        // `colon_pos` is the parser's recorded `colon_offset` in production; these
        // inputs carry no colon inside a comment, so the first `:` is that colon.
        let colon = |s: &str| s.find(':').unwrap();
        for src in [
            "margin: 10px",
            "color:red",
            "--custom-prop: var(--x)",
            "grid-template-columns: 1fr 2fr",
        ] {
            let got = extract_property_name(src, colon(src), has_block_comment(src));
            assert!(
                matches!(got, Cow::Borrowed(_)),
                "bare property in {src:?} must borrow, got owned {got:?}"
            );
        }
        assert_eq!(
            extract_property_name("margin: 10px", colon("margin: 10px"), false),
            "margin"
        );
        assert_eq!(
            extract_property_name("color:red", colon("color:red"), false),
            "color"
        );
        assert_eq!(
            extract_property_name(
                "--custom-prop: var(--x)",
                colon("--custom-prop: var(--x)"),
                false
            ),
            "--custom-prop"
        );
    }

    #[test]
    fn test_extract_property_name_trims_but_still_borrows() {
        // Insignificant whitespace around the property name is trimmed; the result
        // is still a borrowed sub-slice (trim returns a sub-slice, no alloc).
        let src = "color : red";
        let got = extract_property_name(src, src.find(':').unwrap(), has_block_comment(src));
        assert_eq!(got, "color");
        assert!(matches!(got, Cow::Borrowed(_)));
    }

    #[test]
    fn test_extract_property_name_comment_owns() {
        // A comment in the property name reconstructs an owned, space-normalized form.
        // The comment holds no `:`, so the real colon is the first `:`.
        let src = "color/* comment */:red";
        let got = extract_property_name(src, src.find(':').unwrap(), has_block_comment(src));
        assert_eq!(got, "color /* comment */");
        assert!(matches!(got, Cow::Owned(_)));
    }

    #[test]
    fn test_extract_property_name_hex_escape_terminator_owns() {
        // A property name ending in a hex escape consumes one trailing whitespace as
        // the escape's terminator — that space is preserved, so the result owns.
        let src = "\\41 : red";
        let got = extract_property_name(src, src.find(':').unwrap(), has_block_comment(src));
        assert_eq!(got, "\\41 ");
        assert!(matches!(got, Cow::Owned(_)));

        // A non-escape trailing space is trimmed (borrowed).
        let src = "color : red";
        let got = extract_property_name(src, src.find(':').unwrap(), has_block_comment(src));
        assert_eq!(got, "color");
        assert!(matches!(got, Cow::Borrowed(_)));
    }

    #[test]
    fn test_lowercase_property_name() {
        let low = |s: &str| lowercase_property_name(Cow::Borrowed(s)).into_owned();
        // Standard properties + vendor prefixes lowercase.
        assert_eq!(low("COLOR"), "color");
        assert_eq!(low("Background-Color"), "background-color");
        assert_eq!(low("-WEBKIT-Box-Shadow"), "-webkit-box-shadow");
        // Already-lowercase borrows unchanged (no allocation).
        assert!(matches!(
            lowercase_property_name(Cow::Borrowed("color")),
            Cow::Borrowed("color")
        ));
        // Case-sensitive / non-standard names are preserved.
        assert_eq!(low("--MyVar"), "--MyVar"); // custom property
        assert_eq!(low("$fontFamily"), "$fontFamily"); // non-letter start → not a CSS property
        assert_eq!(low("#{$Foo}"), "#{$Foo}"); // non-letter start → preserved
        assert_eq!(low("COLOR /* C */"), "COLOR /* C */"); // comment-bearing
        assert_eq!(low("\\43OLOR"), "\\43OLOR"); // escaped
    }

    #[test]
    fn test_lowercase_at_rule_name() {
        let low = |s: &str| lowercase_at_rule_name(s).into_owned();
        assert_eq!(low("MEDIA"), "media");
        assert_eq!(low("Font-Face"), "font-face");
        assert_eq!(low("-WEBKIT-KEYFRAMES"), "-webkit-keyframes");
        assert_eq!(low("INCLUDE"), "include"); // non-standard directive name, still lowercased
        // Already-lowercase borrows unchanged (no allocation).
        assert!(matches!(
            lowercase_at_rule_name("media"),
            Cow::Borrowed("media")
        ));
        // Escaped names preserved (lowercasing would corrupt the escape's hex digits).
        assert_eq!(low("\\4D edia"), "\\4D edia");
    }

    #[test]
    fn test_lowercase_media_feature_names() {
        let f = |s: &str| lowercase_media_feature_names(s).into_owned();
        // Simple feature expressions: name lowercased, value preserved.
        assert_eq!(f("(MIN-WIDTH: 100px)"), "(min-width: 100px)");
        assert_eq!(f("(ORIENTATION: LANDSCAPE)"), "(orientation: LANDSCAPE)");
        assert_eq!(f("(HOVER)"), "(hover)"); // boolean feature
        assert_eq!(f("(WIDTH >= 600px)"), "(width >= 600px)"); // range, name-first
        assert_eq!(f("(600px <= WIDTH)"), "(600px <= width)"); // range, value-first
        // Media type + keyword preserved (only the feature name lowercases).
        assert_eq!(
            f("SCREEN and (MIN-WIDTH: 1px)"),
            "SCREEN and (min-width: 1px)"
        );
        // Custom media is case-sensitive → preserved.
        assert_eq!(f("(--SMALL-VIEWPORT)"), "(--SMALL-VIEWPORT)");
        // A function-valued feature is still simple — the name lowercases even though
        // the value carries nested parens (`calc(`/`min(`).
        assert_eq!(
            f("(MIN-WIDTH: calc(100px + 1px))"),
            "(min-width: calc(100px + 1px))"
        );
        assert_eq!(f("(WIDTH: min(50%, 100px))"), "(width: min(50%, 100px))");
        // Grouped/complex condition (nested sub-condition parens) left verbatim — even
        // when a nested feature itself has a function value.
        assert_eq!(
            f("((MIN-WIDTH: 1px) and (MAX-WIDTH: 2px))"),
            "((MIN-WIDTH: 1px) and (MAX-WIDTH: 2px))"
        );
        assert_eq!(
            f("((MIN-WIDTH: calc(1px)) and (b))"),
            "((MIN-WIDTH: calc(1px)) and (b))"
        );
        assert_eq!(f("(NOT (HOVER))"), "(NOT (HOVER))");
        // No uppercase → borrows unchanged (no allocation).
        assert!(matches!(
            lowercase_media_feature_names("(min-width: 100px)"),
            Cow::Borrowed(_)
        ));
        // A comment in the expression is preserved; the name still lowercases.
        assert_eq!(f("(MIN-WIDTH: /* c */ 1px)"), "(min-width: /* c */ 1px)");
        // A `(` glued to an ident opens a `<function-token>`, not an expression, so a media
        // TYPE carrying a call is not a feature name and nothing in it folds.
        assert_eq!(f("CALC(A)"), "CALC(A)");
        assert_eq!(
            f("CALC(A) and (MIN-WIDTH: 1px)"),
            "CALC(A) and (min-width: 1px)"
        );
        // The colon a call's parens hold is still the expression's splitting one, so a
        // name may run into a call and its remainder is value text.
        assert_eq!(f("(CALC(1:2): A)"), "(calc(1:2): A)");
        assert_eq!(f("(A(B:C))"), "(a(b:C))");
    }

    #[test]
    fn test_normalize_value_text_hex() {
        // The value path (`@supports`, `@import`): a hex color of a valid length lowercases.
        let sup = |s: &str| normalize_value_text(s, ValueReader::Value);
        assert_eq!(sup("(background: #FFF)"), "(background: #fff)"); // 3-digit
        assert_eq!(sup("(a: #ABCD)"), "(a: #abcd)"); // 4-digit RGBA
        assert_eq!(sup("(a: #AABBCC)"), "(a: #aabbcc)"); // 6-digit
        assert_eq!(sup("(a: #AABBCCDD)"), "(a: #aabbccdd)"); // 8-digit RRGGBBAA
        assert_eq!(sup("(a: #1E2)"), "(a: #1e2)"); // exponent-looking but a valid 3-hex color
        // Off-length or non-hex `#`-tokens keep their case (not colors).
        assert_eq!(sup("(a: #ABCDE)"), "(a: #ABCDE)"); // 5 digits
        assert_eq!(sup("(a: #ABCDEFA)"), "(a: #ABCDEFA)"); // 7 digits
        assert_eq!(sup("(a: #GG)"), "(a: #GG)"); // non-hex
        // Inside `url(...)`/`selector(...)`, a `#` is a URL fragment / ID selector →
        // preserved even though it is a valid hex-color shape. Function-name case is
        // irrelevant to the detection (CSS function tokens are case-insensitive).
        assert_eq!(sup("(background: url(#ABC))"), "(background: url(#ABC))");
        assert_eq!(sup("selector(#ABC)"), "selector(#ABC)");
        assert_eq!(sup("URL(#ABC)"), "URL(#ABC)");
        // A hex color OUTSIDE the preserve group still lowercases; the group's is kept.
        assert_eq!(sup("url(#ABC) #FFF"), "url(#ABC) #fff");
        // A non-opaque function (var/color-mix) is a value context → hex lowercases.
        assert_eq!(sup("var(--x, #ABC)"), "var(--x, #abc)");
        // Number normalization is unaffected by the new hex flag.
        assert_eq!(sup("(margin: .5px)"), "(margin: 0.5px)");

        // An unquoted `url(...)` is an opaque `<url-token>`: number/unit-looking
        // content is copied verbatim, never normalized.
        assert_eq!(
            sup("(background: url(sprite1.50.png))"),
            "(background: url(sprite1.50.png))"
        );
        assert_eq!(sup("(width: url(A.5PX))"), "(width: url(A.5PX))");
        assert_eq!(sup("url()"), "url()"); // empty url-token
        // A quoted `url("…")` is a `<string>` arg: quotes normalize, content preserved.
        assert_eq!(
            normalize_value_text("(background: url(\"v2.00.png\"))", ValueReader::Value),
            "(background: url('v2.00.png'))"
        );
        // The media path walks into a `media-value`'s url (see
        // `the_two_value_readers_answer_a_number_differently`) — and finds nothing to do
        // here, the word part `x1` absorbing the number that abuts it either way.
        assert_eq!(
            normalize_value_text("(a: url(x1.50))", ValueReader::MediaQuery),
            "(a: url(x1.50))"
        );

        // The media path (`@media`): the regex has no hash arm, so a `#` is copied as the
        // delimiter it is and its run keeps its case.
        assert_eq!(
            normalize_value_text("(min-width: #FFF)", ValueReader::MediaQuery),
            "(min-width: #FFF)"
        );
        assert_eq!(
            normalize_value_text("(margin: .5px)", ValueReader::MediaQuery),
            "(margin: 0.5px)"
        );
    }

    /// A `\)` inside an unquoted `url()` is url content (§4.3.6), not the token's close,
    /// so the whole token is copied verbatim and the bytes past the escape are neither
    /// number- nor hex-normalized.
    ///
    /// Prettier throws `Unbalanced parenthesis` on the single-condition spelling of every
    /// input below, so the fixture that grades this end to end
    /// (`css/at_rules/supports_url_escaped_paren`) has to reach for a two-condition prelude
    /// that happens to balance postcss's own count. These pin the scan directly, including
    /// the **unbalanced** shapes no prettier oracle exists for at all.
    #[test]
    fn an_escaped_paren_does_not_close_a_url_token() {
        // The two normalizations the tail would otherwise take.
        assert_eq!(
            normalize_value_text(r"(a: url(x\)1.50))", ValueReader::Value),
            r"(a: url(x\)1.50))"
        );
        assert_eq!(
            normalize_value_text(r"(a: url(x\)y#FFF))", ValueReader::Value),
            r"(a: url(x\)y#FFF))"
        );
        // An escaped OPEN paren nests nothing either — the token still ends at the first
        // unescaped `)`, so `1.50` stays inside it.
        assert_eq!(
            normalize_value_text(r"(a: url(x\(1.50))", ValueReader::Value),
            r"(a: url(x\(1.50))"
        );
        // A hex escape carries its whitespace terminator, so the `)` here is the escape's
        // payload and not a close.
        assert_eq!(
            normalize_value_text(r"(a: url(x\29 1.50))", ValueReader::Value),
            r"(a: url(x\29 1.50))"
        );
        // Unbalanced: the group runs to end of input rather than stopping at the escape.
        assert_eq!(
            normalize_value_text(r"(a: url(x\)1.50)", ValueReader::Value),
            r"(a: url(x\)1.50)"
        );
        // A trailing `\` starts no escape, so the `)` before it still closes.
        assert_eq!(
            normalize_value_text(r"(a: url(x1.50)\)", ValueReader::Value),
            r"(a: url(x1.50)\)"
        );
        // Control: no escape, so the token is opaque for the ordinary reason.
        assert_eq!(
            normalize_value_text("(a: url(x1.50))", ValueReader::Value),
            "(a: url(x1.50))"
        );
    }

    /// A `(` or `)` inside an escape is ident content (§"Consume an ident sequence"), so it
    /// neither opens a sub-condition nor moves the group's depth: the feature expression
    /// stays *simple* and its name lowercases.
    ///
    /// Prettier drops the whole prelude on the unbalanced spelling (`@media (a: b\(c)` →
    /// `@media  {`), so only the balanced one is gradable by fixture
    /// (`css/at_rules/media_feature_escaped_paren_value`); both are pinned here.
    #[test]
    fn an_escaped_paren_is_not_a_media_sub_condition() {
        // Balanced raw parens inside escapes — the fixture's shape.
        assert_eq!(
            lowercase_media_feature_names(r"(MIN-WIDTH: a\(b\)c)"),
            r"(min-width: a\(b\)c)"
        );
        // Unbalanced: without the escape arm the group swallowed the rest of the query.
        assert_eq!(
            lowercase_media_feature_names(r"(MIN-WIDTH: a\(b)"),
            r"(min-width: a\(b)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(MIN-WIDTH: a\)b)"),
            r"(min-width: a\)b)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(MIN-WIDTH: a\(b) and (MAX-WIDTH: 1px)"),
            r"(min-width: a\(b) and (max-width: 1px)"
        );
        // A real function call in the value still isn't a sub-condition, and a real
        // sub-condition still is — the escape arm moves neither.
        assert_eq!(
            lowercase_media_feature_names("(MIN-WIDTH: calc(1px))"),
            "(min-width: calc(1px))"
        );
        assert_eq!(
            lowercase_media_feature_names("((MIN-WIDTH: 1px))"),
            "((MIN-WIDTH: 1px))"
        );
        // At depth 0 an escaped `(` is part of a media TYPE, not the `(` that opens a
        // feature expression — reading it as one handed the rest of the query to
        // `scan_paren_group` as one opaque group and the feature name never lowercased.
        assert_eq!(
            lowercase_media_feature_names(r"a\(b and (MIN-WIDTH: 1px)"),
            r"a\(b and (min-width: 1px)"
        );
    }

    /// A media-feature name is one **ident sequence**, and an escape is ident content
    /// (CSS Syntax 3 §"Consume an ident sequence"), so the name runs *through* every escape
    /// it carries — including one spelling the `:` that would otherwise look like the
    /// name→value separator.
    ///
    /// Two rules key on the whole name, and a scanner that stopped at the `\` got both
    /// wrong on the fragment after it: a plain name is a pre-defined keyword and so ASCII
    /// case-insensitive (css-values-4 §"Pre-defined Keywords"), while a custom-media name
    /// is an `<extension-name>` (css-extensions-1) with no canonical casing to fold to, so
    /// only the fragment carrying the `--` was preserved.
    ///
    /// The `\:` spelling has no prettier oracle — prettier reads the escape's payload as
    /// the separator and splits the ident (`(a\: B: 1px)`), a cataloged divergence
    /// (`media_feature_escaped_colon_prettier_divergence`).
    #[test]
    fn an_escaped_colon_does_not_end_a_feature_name() {
        // The whole run is the name, so the whole run lowercases.
        assert_eq!(
            lowercase_media_feature_names(r"(A\:B: 1px)"),
            r"(a\:b: 1px)"
        );
        // Case-sensitive per css-extensions-1 — must survive whole, past the escape.
        assert_eq!(
            lowercase_media_feature_names(r"(--A\:B: 1px)"),
            r"(--A\:B: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(--A\42 C: 1px)"),
            r"(--A\42 C: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(--MIN\-WIDTH: 1px)"),
            r"(--MIN\-WIDTH: 1px)"
        );
        // A boolean feature — no `:` at all, so the whole group is name position.
        assert_eq!(lowercase_media_feature_names(r"(--A\42 C)"), r"(--A\42 C)");
        // An escape elsewhere in a plain name is ordinary content and the run lowercases
        // across it, hex digits included (a hex escape is itself case-insensitive).
        assert_eq!(
            lowercase_media_feature_names(r"(MIN\-WIDTH: 1px)"),
            r"(min\-width: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(A\4B C: 1px)"),
            r"(a\4b c: 1px)"
        );
        // A lone `\` starts no escape (§4.3.4) and so extends nothing: the run ends at it
        // and the scanner still advances.
        assert_eq!(
            lowercase_media_feature_names("(A\\\nB: 1px)"),
            "(a\\\nb: 1px)"
        );
        assert_eq!(lowercase_media_feature_names(r"(A: 1px)\"), r"(a: 1px)\");
        // The value side is preserved whether or not it carries an escape.
        assert_eq!(
            lowercase_media_feature_names(r"(A: LAND\53 CAPE)"),
            r"(a: LAND\53 CAPE)"
        );
    }

    /// Case-sensitivity is a property of the **whole** feature name, which is why
    /// [`feature_name_preserves_case`] is asked once rather than per identifier: a name
    /// spelled as several runs kept only the run that carried the marker, so `(--A B: 1px)`
    /// preserved `--A` and lowercased `B`. Prettier asks the same question of the same
    /// region (`maybeToLowerCase`), so every case here matches it.
    #[test]
    fn case_sensitivity_is_a_property_of_the_whole_feature_name() {
        // An `<extension-name>` marks the whole name, however many runs it spans.
        assert_eq!(
            lowercase_media_feature_names(r"(--A B: 1px)"),
            r"(--A B: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(--A\Z B: 1px)"),
            r"(--A\Z B: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names("(--A 1PX: 1px)"),
            "(--A 1PX: 1px)"
        );
        // Leading whitespace does not hide the marker.
        assert_eq!(
            lowercase_media_feature_names("( --A B : 1px)"),
            "( --A B : 1px)"
        );
        // Preprocessor spellings tsv's permissive parser reaches: folding a name it does
        // not understand can only lose information, and prettier preserves them too.
        assert_eq!(lowercase_media_feature_names("(A$B: 1px)"), "(A$B: 1px)");
        assert_eq!(lowercase_media_feature_names("($A: 1px)"), "($A: 1px)");
        assert_eq!(lowercase_media_feature_names("(A@B: 1px)"), "(A@B: 1px)");
        assert_eq!(lowercase_media_feature_names("(A#B: 1px)"), "(A#B: 1px)");
        assert_eq!(lowercase_media_feature_names("(%A: 1px)"), "(%A: 1px)");
        // A `%` that is not leading is not a marker, so the name still folds.
        assert_eq!(lowercase_media_feature_names("(A%B: 1px)"), "(a%b: 1px)");
        // A `:` in the VALUE does not end the name, so a call there cannot mark it.
        assert_eq!(
            lowercase_media_feature_names("(MIN-WIDTH: calc(1px))"),
            "(min-width: calc(1px))"
        );
        assert_eq!(
            lowercase_media_feature_names("(MIN-WIDTH: url(x:y))"),
            "(min-width: url(x:y))"
        );
        // A `:` inside trivia is not the separator either.
        assert_eq!(
            lowercase_media_feature_names("(/* : */ MIN-WIDTH: 1px)"),
            "(/* : */ min-width: 1px)"
        );
        assert_eq!(
            lowercase_media_feature_names(r"(MIN-WIDTH: ':')"),
            r"(min-width: ':')"
        );
    }

    /// A number abutting the identifier before it is that identifier's own tail: the ident
    /// run stops at the first non-ident code point, so what follows can only be a
    /// `.`-leading number, and its canonical leading zero would be swallowed by the ident.
    /// Prettier states the same rule textually and both formatters copy the pair through.
    #[test]
    fn a_number_abutting_an_identifier_is_copied_verbatim() {
        // `x1` + `.50`, not `x10` + `.5`.
        assert_eq!(
            normalize_value_text("(a: x1.50)", ValueReader::Value),
            "(a: x1.50)"
        );
        // The unit rides along verbatim too — no `canonical_unit` lowercasing.
        assert_eq!(
            normalize_value_text("(a: x.50PX)", ValueReader::Value),
            "(a: x.50PX)"
        );
        // A leading `--` reaches the ident arm at the first letter, so it is covered.
        assert_eq!(
            normalize_value_text("(a: --x.50)", ValueReader::Value),
            "(a: --x.50)"
        );
        // Separated by whitespace, the number is a value of its own and normalizes.
        assert_eq!(
            normalize_value_text("(a: x .50)", ValueReader::Value),
            "(a: x 0.5)"
        );
        // So does one that starts the value.
        assert_eq!(
            normalize_value_text("(a: .50px)", ValueReader::Value),
            "(a: 0.5px)"
        );
        // A signed number keeps its sign, which merges with nothing, so it normalizes even
        // against an identifier. (`x-1` is one ident run, so `x-1.50` is the glued case.)
        assert_eq!(
            normalize_value_text("(a: x+1.50)", ValueReader::Value),
            "(a: x+1.5)"
        );
        assert_eq!(
            normalize_value_text("(a: x-1.50)", ValueReader::Value),
            "(a: x-1.50)"
        );
        // `$` and `@` are `<delim-token>`s, not ident code points, so nothing merges into
        // them even though this module's ident class reads them as run starts.
        assert_eq!(
            normalize_value_text("(a: $.50)", ValueReader::MediaQuery),
            "(a: $0.5)"
        );
        assert_eq!(
            normalize_value_text("(a: @.50)", ValueReader::MediaQuery),
            "(a: @0.5)"
        );
        assert_eq!(
            normalize_value_text("(a: $x.50)", ValueReader::MediaQuery),
            "(a: $x.50)"
        );
        // An escape does not make a word part (prettier's own scan reads no `\` either),
        // so the number after one still normalizes — matching prettier.
        assert_eq!(
            normalize_value_text(r"(a: \41 2.50px)", ValueReader::Value),
            r"(a: \41 2.5px)"
        );
        assert_eq!(
            normalize_value_text(r"(a: x\41 .50)", ValueReader::Value),
            r"(a: x\41 0.5)"
        );
        // The ident arm's own `url(` exit clears the word part: what follows the group is
        // not the ident's tail.
        assert_eq!(
            normalize_value_text("(a: url(x).50)", ValueReader::Value),
            "(a: url(x)0.5)"
        );
    }

    /// The three rules keyed on [`ValueReader`], each measured against the reader prettier
    /// actually uses: the unit gate, the hex fold, and which runs absorb a number. One
    /// authoring, two canonical forms — so every case is asserted on both paths.
    #[test]
    fn the_two_value_readers_answer_a_number_differently() {
        let value = |s: &str| normalize_value_text(s, ValueReader::Value);
        let media = |s: &str| normalize_value_text(s, ValueReader::MediaQuery);

        // The unit gate. `printUnit` has none — whatever trails the number is the unit —
        // where `adjustNumbers` keeps the whole match on a unit CSS doesn't define.
        assert_eq!(value("(a: 1.50abc)"), "(a: 1.5abc)");
        assert_eq!(media("(a: 1.50abc)"), "(a: 1.50abc)");
        // A unit both readers accept normalizes on both, casing included.
        assert_eq!(value("(a: 1.50PX)"), "(a: 1.5px)");
        assert_eq!(media("(a: 1.50PX)"), "(a: 1.5px)");
        // The `<an+b>` `n` is where the unit's CASE parts: `adjustNumbers` lowercases what
        // it captured before its gate and prints that, where `printUnit` is handed the
        // author's own spelling and finds no `n` in its map.
        assert_eq!(value("(a: 2N)"), "(a: 2N)");
        assert_eq!(media("(a: 2N)"), "(a: 2n)");
        assert_eq!(value("(a: 2N + 1)"), "(a: 2N + 1)");
        assert_eq!(media("(a: 2N + 1)"), "(a: 2n + 1)");

        // `<url-token>` opacity is the value reader's rule: `parse-value.js` special-cases
        // the func, where `adjustNumbers` is a regex with no url concept and walks straight
        // through one in `media-value` position.
        assert_eq!(value("(a: url(1.50))"), "(a: url(1.50))");
        assert_eq!(media("(a: url(1.50))"), "(a: url(1.5))");
        assert_eq!(media("(a: url(1.50px) b)"), "(a: url(1.5px) b)");
        // Only there, though — a `url()` in NAME position sits in a `media-feature`, which
        // takes no number pass, and one at depth 0 is a `media-url` node, which
        // `adjustNumbers` never prints either.
        assert_eq!(media("(url(1.50): a)"), "(url(1.50): a)");
        assert_eq!(media("(url(1.50))"), "(url(1.50))");
        assert_eq!(media("a url(1.50)"), "a url(1.50)");
        assert_eq!(media("(a: b) url(1.50)"), "(a: b) url(1.50)");

        // A number's own unit absorbs the number after it on the value path (one word), and
        // absorbs nothing on the media path (the regex re-splits at the second number).
        assert_eq!(value("(a: 1a.50)"), "(a: 1a.50)");
        assert_eq!(media("(a: 1a.50)"), "(a: 1a0.5)");
        // The same with no unit between them — a second `.` ends the first number.
        assert_eq!(value("(a: 1.5.50)"), "(a: 1.5.50)");
        assert_eq!(media("(a: 1.5.50)"), "(a: 1.50.5)");

        // A `<hash-token>` absorbs on the value path (postcss-values-parser reads the word
        // whole); the media path has no hash arm at all, so the `#` is a bare delimiter and
        // the run after it takes the ordinary number arm.
        assert_eq!(value("(a: #1.50)"), "(a: #1.50)");
        assert_eq!(media("(a: #1.50)"), "(a: #1.5)");
        assert_eq!(value("(a: #1.50px)"), "(a: #1.50px)");
        assert_eq!(media("(a: #1.50px)"), "(a: #1.5px)");
        // A `#` followed by no ident code point heads no hash token (§4.3.1 leaves a
        // `<delim-token>`), so the number normalizes on both.
        assert_eq!(value("(a: #.50)"), "(a: #0.5)");
        assert_eq!(media("(a: #.50)"), "(a: #0.5)");
        // A hash token's own ident run still absorbs on the media path, via its word part.
        assert_eq!(value("(a: #abc1.50)"), "(a: #abc1.50)");
        assert_eq!(media("(a: #abc1.50)"), "(a: #abc1.50)");
        // The hex fold is the value reader's alone.
        assert_eq!(value("(a: #FFF)"), "(a: #fff)");
        assert_eq!(media("(a: #FFF)"), "(a: #FFF)");

        // `$`/`@` head no `WORD_PART` (`[$@]?` must be followed by a letter or `_`), so the
        // media reader's number starts inside the run this module read as one ident.
        assert_eq!(value("(a: $1.50)"), "(a: $1.50)");
        assert_eq!(media("(a: $1.50)"), "(a: $1.5)");
        assert_eq!(media("(a: x$1.50)"), "(a: x$1.5)");
        // A word part after the `$` absorbs again, on both.
        assert_eq!(value("(a: $a1.50)"), "(a: $a1.50)");
        assert_eq!(media("(a: $a1.50)"), "(a: $a1.50)");
        // …and the word part may start anywhere after the last `$`/`@`.
        assert_eq!(media("(a: $$a.50)"), "(a: $$a.50)");
        // With no word part and no digits in the tail, the run ends where it did.
        assert_eq!(media("(a: $-.50)"), "(a: $-0.5)");
    }

    /// `adjustNumbers` prints a `media-type` and a `media-value`, never a `media-feature`,
    /// so the media reader's number pass stops at the boundary [`feature_name_end`] draws —
    /// the expression's first colon, or its whole interior when it has none.
    #[test]
    fn the_media_readers_number_pass_skips_feature_name_position() {
        let media = |s: &str| normalize_value_text(s, ValueReader::MediaQuery);

        // The headline: one expression, two halves, two answers.
        assert_eq!(media("(1.50: 2.50)"), "(1.50: 2.5)");
        // No colon, so the whole interior is name — the boolean and both range forms.
        assert_eq!(media("(0.50)"), "(0.50)");
        assert_eq!(media("(width >= 1.50)"), "(width >= 1.50)");
        assert_eq!(media("(1.50 < width < 2.50)"), "(1.50 < width < 2.50)");
        // A call's parens do not protect the colon, so a name may run into one and a value
        // may start inside one.
        assert_eq!(media("(calc(1.50): a)"), "(calc(1.50): a)");
        assert_eq!(media("(calc(1.50))"), "(calc(1.50))");
        assert_eq!(media("(calc(1:1.50): a)"), "(calc(1:1.5): a)");
        // Nor do a grouped condition's: the split is per TOP-LEVEL group, at its first
        // colon wherever that sits, which is what keeps a nested feature's value normalized.
        assert_eq!(media("(not (a: 1.50))"), "(not (a: 1.5))");
        assert_eq!(
            media("((1.50: a) and (b: 1.50))"),
            "((1.50: a) and (b: 1.5))"
        );
        assert_eq!(media("((1.50))"), "((1.50))");
        // Depth 0 is `media-type` position and does take the pass.
        assert_eq!(media("1.50"), "1.5");
        assert_eq!(media("1.50, (a: 1.50)"), "1.5, (a: 1.5)");
        assert_eq!(media("not (1.50: a)"), "not (1.50: a)");
        // A `(` glued to an ident is a `<function-token>`'s, not an expression's, so a
        // media type carrying a call takes the pass through it.
        assert_eq!(media("calc(1.50)"), "calc(1.5)");
        assert_eq!(
            media("calc(1.50) and (calc(1.50): a)"),
            "calc(1.5) and (calc(1.50): a)"
        );
        // Every top-level group carries its own split.
        assert_eq!(media("(a: 1.50) and (2.50: b)"), "(a: 1.5) and (2.50: b)");
        assert_eq!(media("(1.50: a), (b: 2.50)"), "(1.50: a), (b: 2.5)");
        // Trivia is stepped, so a colon inside it splits nothing and the name reaches past
        // it — deliberately narrower than prettier's raw scan (docs/conformance_prettier_css.md
        // §CSS: At-Rules), and the same reading `feature_name_end` gives the case fold.
        assert_eq!(media("(1.50 /* c */: a)"), "(1.50 /* c */: a)");
        assert_eq!(media("(/* : */ 1.50: a)"), "(/* : */ 1.50: a)");
        assert_eq!(media(r"(a\:b: 1.50)"), r"(a\:b: 1.5)");
        // The value path has no such split — `parseValue` reads its whole text as one value.
        assert_eq!(
            normalize_value_text("(1.50: 2.50)", ValueReader::Value),
            "(1.5: 2.5)"
        );
    }

    /// The hex fold is asked of the **whole** hash word, so a colour is a colour only when
    /// the token ends at its digits. A body narrowed to the hex-shaped prefix recased a
    /// token that is no colour (`#FFF.5` → `#fff.5`).
    #[test]
    fn the_hex_fold_reads_the_whole_hash_word() {
        let value = |s: &str| normalize_value_text(s, ValueReader::Value);

        // The word runs on past the digits — no colour here, so nothing is recased.
        assert_eq!(value("(a: #FFF.5)"), "(a: #FFF.5)");
        assert_eq!(value("(a: #FFF/5)"), "(a: #FFF/5)");
        assert_eq!(value("(a: #FFFé)"), "(a: #FFFé)");
        assert_eq!(value("(a: #FFF-x)"), "(a: #FFF-x)");
        assert_eq!(value("(a: #FFF_x)"), "(a: #FFF_x)");
        assert_eq!(value("(a: #ABCD.5)"), "(a: #ABCD.5)");
        // A separator ends the word, so the colour before it folds.
        assert_eq!(value("(a: #FFF 5)"), "(a: #fff 5)");
        assert_eq!(value("(a: #FFF)"), "(a: #fff)");
        // A word of hex-colour length folds however it got there.
        assert_eq!(value("(a: #FFF9)"), "(a: #fff9)");

        // Whether the token starts at all stays §4.3.1's question, and its answer is
        // narrower than the word class: `.` continues a hash word but heads none, so `#.50`
        // is a bare delimiter and the number after it is its own token.
        assert_eq!(value("(a: #.50)"), "(a: #0.5)");
        // `-` IS an ident code point, so it heads one — and the word then absorbs, which is
        // what keeps `#-` a hash token whose value is `-` rather than `-0`.
        assert_eq!(value("(a: #-.50)"), "(a: #-.50)");
    }
}