autumn-web 0.9.0

An opinionated, convention-over-configuration web framework for Rust
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
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
//! HTML to Markdown, for `Accept: text/markdown` negotiation.
//!
//! The converter is small and has no dependencies. It reads Autumn page HTML.
//! It writes `CommonMark` with GFM tables. It keeps the content and removes
//! navigation and controls:
//!
//! - It converts `<main>` when the page has one, else `<body>`.
//! - It drops `script`, `style`, `nav`, `form` controls, `svg`, `template`,
//!   and elements marked `hidden` or `aria-hidden="true"`.
//! - It puts `<title>` and `<meta name="description">` in YAML front matter.
//!
//! It never panics and never recurses deeper than [`MAX_DEPTH`].

use std::fmt::Write as _;

/// Maximum element depth the converter descends into. Deeper content is
/// written as plain text.
pub const MAX_DEPTH: usize = 256;

/// Estimate the token count of `markdown` (about four bytes per token).
///
/// Sent as `x-markdown-tokens`.
#[must_use]
pub const fn estimate_tokens(markdown: &str) -> usize {
    markdown.len().div_ceil(4)
}

/// Convert an HTML document to Markdown.
#[must_use]
pub fn html_to_markdown(html: &str) -> String {
    html_to_markdown_at(html, None)
}

/// Convert the HTML page served at `path` (the request path and query) to
/// Markdown. A relative `<base href>` resolves against `path`, as a browser
/// resolves it against the page URL.
#[must_use]
pub fn html_to_markdown_at(html: &str, path: Option<&str>) -> String {
    // As the HTML parser does first: CRLF and a lone CR are LF.
    let normalized;
    let html = if html.contains('\r') {
        normalized = html.replace("\r\n", "\n").replace('\r', "\n");
        normalized.as_str()
    } else {
        html
    };
    let mut doc = parse(html);
    if let Some((base, scheme_relative)) = doc.document_base(path) {
        doc.base = Some(base);
        doc.base_scheme_relative = scheme_relative;
    }
    let mut out = String::new();
    let title = doc
        .find_metadata(|id| doc.name(id) == Some("title"))
        .map(|t| doc.text_of(t));
    let description = doc.meta_description();
    let title = title.map(|t| collapse_ws(&t)).filter(|t| !t.is_empty());
    let description = description
        .map(|d| collapse_ws(&d))
        .filter(|d| !d.is_empty());
    if title.is_some() || description.is_some() {
        out.push_str("---\n");
        if let Some(t) = &title {
            out.push_str("title: ");
            out.push_str(&super::documents::yaml_quote(t));
            out.push('\n');
        }
        if let Some(d) = &description {
            out.push_str("description: ");
            out.push_str(&super::documents::yaml_quote(d));
            out.push('\n');
        }
        out.push_str("---\n\n");
    }
    // Without a `<main>`, the whole document: a browser moves content
    // before `<body>` or after `</body>` into the body.
    let root = doc.find_visible("main").unwrap_or(ROOT);
    let mut w = Writer::default();
    w.children(&doc, root, 0);
    let body = w.finish();
    if body.is_empty() {
        // Front matter alone is still a document; drop the separator blank.
        if out.ends_with("---\n\n") {
            out.pop();
        }
    } else {
        out.push_str(&body);
    }
    out
}

// ── Tree ────────────────────────────────────────────────────────────────────

const ROOT: usize = 0;

enum Kind {
    Element {
        name: String,
        attrs: Vec<(String, String)>,
    },
    Text(String),
}

struct Node {
    kind: Kind,
    children: Vec<usize>,
}

/// An arena tree. Nodes refer to children by index, so dropping a deep tree
/// never recurses.
struct Doc {
    nodes: Vec<Node>,
    /// The document's `<base href>`, as an `http` or `https` URL. One the
    /// page's origin resolves sits on [`PAGE_ORIGIN`].
    base: Option<url::Url>,
    /// `true` when the base was scheme-relative (`//cdn.example/`): the
    /// page's scheme is not known, so links keep that form.
    base_scheme_relative: bool,
}

/// Stand-in origin for the page: the real one is not known here. A URL
/// resolved onto it is written root-relative. `.invalid` is reserved, so no
/// real link names it.
const PAGE_ORIGIN: &str = "http://autumn.invalid";

impl Doc {
    /// The first `<base>` with an `href`, as a browser picks it, and
    /// whether it was scheme-relative. A path-relative one resolves against
    /// the page `path`, and is not used without it. A scheme other than
    /// `http` or `https` is not used.
    fn document_base(&self, path: Option<&str>) -> Option<(url::Url, bool)> {
        let mut stack = vec![ROOT];
        while let Some(id) = stack.pop() {
            match self.name(id) {
                // Template content is not part of the document.
                Some("template") => continue,
                Some("base") => {
                    if let Some(href) = self.attr(id, "href") {
                        let href = href.trim_matches(|c: char| c <= ' ');
                        let url = match url::Url::parse(href) {
                            Ok(url) => url,
                            Err(url::ParseError::RelativeUrlWithoutBase) => {
                                // `/x` and `//host/x` need only the origin.
                                let page = path.or_else(|| href.starts_with('/').then_some("/"))?;
                                url::Url::parse(PAGE_ORIGIN)
                                    .ok()?
                                    .join(page)
                                    .ok()?
                                    .join(href)
                                    .ok()?
                            }
                            Err(_) => return None,
                        };
                        return matches!(url.scheme(), "http" | "https")
                            .then(|| (url, href.starts_with("//")));
                    }
                }
                _ => {}
            }
            stack.extend(self.nodes[id].children.iter().rev());
        }
        None
    }

    /// `raw` resolved against the document base, as a browser follows it.
    /// An absolute URL, or any URL without a base, is unchanged.
    fn resolve<'a>(&self, raw: &'a str) -> std::borrow::Cow<'a, str> {
        let Some(base) = &self.base else {
            return raw.into();
        };
        let cleaned: String = raw
            .trim_matches(|c: char| c <= ' ')
            .chars()
            .filter(|c| !matches!(c, '\t' | '\n' | '\r'))
            .collect();
        let Err(url::ParseError::RelativeUrlWithoutBase) = url::Url::parse(&cleaned) else {
            return raw.into();
        };
        let Ok(url) = base.join(&cleaned) else {
            return raw.into();
        };
        if url.scheme() == "http" && url.host_str() == PAGE_ORIGIN.strip_prefix("http://") {
            return url[url::Position::BeforePath..].to_owned().into();
        }
        let url = String::from(url);
        // A scheme-relative base keeps its form: the page's scheme is not
        // known, and the stand-in resolves it as `http`.
        if self.base_scheme_relative
            && let Some(rest) = url.strip_prefix("http:")
        {
            return rest.to_owned().into();
        }
        url.into()
    }

    fn name(&self, id: usize) -> Option<&str> {
        match &self.nodes[id].kind {
            Kind::Element { name, .. } => Some(name),
            Kind::Text(_) => None,
        }
    }

    fn attr(&self, id: usize, key: &str) -> Option<&str> {
        match &self.nodes[id].kind {
            Kind::Element { attrs, .. } => attrs
                .iter()
                .find(|(k, _)| k == key)
                .map(|(_, v)| v.as_str()),
            Kind::Text(_) => None,
        }
    }

    /// First element that `want` accepts, in document order, outside hidden
    /// elements and the dropped ones other than `<head>` and `<title>`
    /// (`<template>`, `<svg>`, `<iframe>`, ...): a title in those is not the
    /// page's.
    fn find_metadata(&self, want: impl Fn(usize) -> bool) -> Option<usize> {
        let mut stack = vec![ROOT];
        while let Some(id) = stack.pop() {
            if let Some(n) = self.name(id) {
                let dropped = DROP.contains(&n) && !matches!(n, "head" | "title");
                if id != ROOT && (is_hidden(self, id) || dropped) {
                    continue;
                }
                if want(id) {
                    return Some(id);
                }
            }
            stack.extend(self.nodes[id].children.iter().rev());
        }
        None
    }

    /// First element named `name` that is not hidden and not inside a
    /// hidden or dropped element (such as `<template>`).
    fn find_visible(&self, name: &str) -> Option<usize> {
        let mut stack = vec![ROOT];
        while let Some(id) = stack.pop() {
            if let Some(n) = self.name(id) {
                if id != ROOT && (is_hidden(self, id) || DROP.contains(&n) || n == "template") {
                    continue;
                }
                if n == name {
                    return Some(id);
                }
            }
            stack.extend(self.nodes[id].children.iter().rev());
        }
        None
    }

    fn meta_description(&self) -> Option<String> {
        self.find_metadata(|id| {
            self.name(id) == Some("meta")
                && self
                    .attr(id, "name")
                    .is_some_and(|n| n.eq_ignore_ascii_case("description"))
        })
        .and_then(|id| self.attr(id, "content").map(str::to_owned))
    }

    /// The text below `id`, without markup, and without the descendants the
    /// writer skips: hidden ones and dropped ones (`<script>`, ...).
    fn text_of(&self, id: usize) -> String {
        let mut out = String::new();
        let mut stack = vec![id];
        while let Some(n) = stack.pop() {
            match &self.nodes[n].kind {
                Kind::Text(t) => out.push_str(t),
                Kind::Element { name, .. } => {
                    if n != id && (is_hidden(self, n) || DROP.contains(&name.as_str())) {
                        continue;
                    }
                    stack.extend(self.nodes[n].children.iter().rev());
                }
            }
        }
        out
    }
}

const VOID: &[&str] = &[
    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source",
    "track", "wbr",
];

/// Elements whose content is raw text up to the matching end tag.
const RAW_TEXT: &[&str] = &[
    "script",
    "style",
    "textarea",
    "title",
    "xmp",
    "noscript",
    "iframe",
    "noembed",
    "noframes",
    "plaintext",
];

/// Start tags that close an open element of the listed names first.
fn implied_close(tag: &str) -> &'static [&'static str] {
    match tag {
        "li" => &["li"],
        "dt" | "dd" => &["dt", "dd"],
        "tr" => &["tr", "td", "th"],
        "td" | "th" => &["td", "th"],
        "option" => &["option"],
        "thead" | "tbody" | "tfoot" => &["thead", "tbody", "tfoot", "tr", "td", "th"],
        _ => &[],
    }
}

/// The start tags that close an open `<p>`, as in the HTML parser. A `<li>`,
/// `<dt>` or `<dd>` closes its own kind first, then any `<p>` left open.
fn closes_paragraph(tag: &str) -> bool {
    matches!(
        tag,
        "address"
            | "article"
            | "aside"
            | "blockquote"
            | "center"
            | "details"
            | "dialog"
            | "dir"
            | "div"
            | "dl"
            | "fieldset"
            | "figcaption"
            | "figure"
            | "footer"
            | "form"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "header"
            | "hgroup"
            | "hr"
            | "listing"
            | "main"
            | "menu"
            | "nav"
            | "ol"
            | "p"
            | "plaintext"
            | "pre"
            | "search"
            | "section"
            | "summary"
            | "table"
            | "ul"
            | "xmp"
            | "li"
            | "dd"
            | "dt"
    )
}

/// Elements a document head can hold.
const HEAD_CONTENT: &[&str] = &[
    "base", "link", "meta", "noscript", "script", "style", "template", "title",
];

/// Elements that stop the search for an implied close (a `<li>` inside a
/// nested `<ul>` must not close the outer `<li>`).
const SCOPE: &[&str] = &[
    "ul",
    "ol",
    "table",
    "dl",
    "select",
    "blockquote",
    "template",
];

fn is_heading(name: &str) -> bool {
    matches!(name, "h1" | "h2" | "h3" | "h4" | "h5" | "h6")
}

/// Elements whose content an end tag for an outer element cannot leave: a
/// browser ignores a stray `</div>` inside a `<select>` or a `<template>`.
const WALL: &[&str] = &["select", "template"];

#[allow(clippy::too_many_lines)] // one tokenizer loop; splitting it hides the state
fn parse(html: &str) -> Doc {
    let mut doc = Doc {
        base: None,
        base_scheme_relative: false,
        nodes: vec![Node {
            kind: Kind::Element {
                name: "#root".to_owned(),
                attrs: Vec::new(),
            },
            children: Vec::new(),
        }],
    };
    let mut stack: Vec<usize> = vec![ROOT];
    // The start tags past `MAX_DEPTH`. The parser does not keep them. An end
    // tag closes the last dropped tag it names; one naming only a kept
    // element closes that element and every dropped tag inside it.
    let mut dropped = Dropped::default();
    let bytes = html.as_bytes();
    let mut i = 0;

    let push = |doc: &mut Doc, stack: &[usize], kind: Kind| -> usize {
        let id = doc.nodes.len();
        doc.nodes.push(Node {
            kind,
            children: Vec::new(),
        });
        let parent = *stack.last().unwrap_or(&ROOT);
        doc.nodes[parent].children.push(id);
        id
    };

    while i < bytes.len() {
        if bytes[i] != b'<' {
            let end = memchr(b'<', &bytes[i..]).map_or(bytes.len(), |n| i + n);
            let text = decode_entities(&html[i..end], false);
            // Past the depth limit, text joins the last kept element, unless
            // a dropped tag around it hides its content.
            if !text.is_empty() && !dropped.hides() {
                push(&mut doc, &stack, Kind::Text(text));
            }
            i = end;
            continue;
        }
        let rest = &html[i..];
        if let Some(body) = rest.strip_prefix("<!--") {
            i = comment_end(body).map_or(bytes.len(), |n| i + 4 + n);
            continue;
        }
        if rest.starts_with("<!") || rest.starts_with("<?") {
            i = rest.find('>').map_or(bytes.len(), |n| i + n + 1);
            continue;
        }
        let mut tag = match read_tag(rest) {
            TagRead::Tag(tag) => tag,
            TagRead::Text => {
                // A lone `<` is text.
                push(&mut doc, &stack, Kind::Text("<".to_owned()));
                i += 1;
                continue;
            }
            // An unterminated tag runs to the end, as in a browser.
            TagRead::Eof => break,
        };
        i += tag.len;
        // A browser reads `</br>` as `<br>`.
        if tag.end && tag.name == "br" {
            tag.end = false;
            tag.attrs.clear();
        }
        if tag.end {
            // `</select>` and `</template>` close their own wall; any other
            // end tag stops at the innermost one.
            let walled = !WALL.contains(&tag.name.as_str());
            if !dropped.is_empty() {
                let wall = dropped.wall().filter(|_| walled);
                match (dropped.last(&tag.name), wall) {
                    (Some(pos), Some(wall)) if pos < wall => {}
                    (Some(pos), _) => dropped.truncate(pos),
                    (None, Some(_)) => {}
                    (None, None) => close_kept(&doc, &mut stack, &mut dropped, &tag.name, walled),
                }
                continue;
            }
            close_kept(&doc, &mut stack, &mut dropped, &tag.name, walled);
            continue;
        }

        // In a `<select>`, an `<input>`, `<keygen>`, `<textarea>` or another
        // `<select>` closes it, as in a browser; a nested `<select>` is then
        // dropped.
        if matches!(
            tag.name.as_str(),
            "input" | "keygen" | "textarea" | "select"
        ) {
            let in_dropped = dropped
                .wall()
                .filter(|&w| dropped.last("select") == Some(w));
            let closed = if let Some(wall) = in_dropped {
                dropped.truncate(wall);
                true
            } else if dropped.wall().is_none()
                && let Some(pos) = stack
                    .iter()
                    .rposition(|&id| matches!(doc.name(id), Some("select" | "template")))
                && doc.name(stack[pos]) == Some("select")
            {
                stack.truncate(pos);
                dropped.clear();
                true
            } else {
                false
            };
            if closed && tag.name == "select" {
                continue;
            }
        }

        // An omitted `</head>`: a tag that cannot be in the head (`<body>`,
        // `<main>`, ...) closes it, as in a browser. Inside a `<template>`
        // the head is not the insertion point, so the template keeps it.
        if !HEAD_CONTENT.contains(&tag.name.as_str())
            && dropped.wall().is_none()
            && let Some(pos) = stack
                .iter()
                .rposition(|&id| matches!(doc.name(id), Some("head" | "template")))
            && doc.name(stack[pos]) == Some("head")
        {
            stack.truncate(pos);
            dropped.clear();
        }

        // Implied end tags. The search runs from the innermost open tag
        // out, through the dropped tags first, so closing a kept element
        // also closes every dropped tag inside it.
        let paragraph: &[&str] = if closes_paragraph(&tag.name) {
            &["p"]
        } else {
            &[]
        };
        for closes in [implied_close(&tag.name), paragraph] {
            if closes.is_empty() || dropped.implied_close(closes) {
                continue;
            }
            for pos in (1..stack.len()).rev() {
                let Some(open) = doc.name(stack[pos]) else {
                    break;
                };
                if closes.contains(&open) {
                    stack.truncate(pos);
                    dropped.clear();
                    break;
                }
                if SCOPE.contains(&open) {
                    break;
                }
            }
        }

        // A `<button>` or an `<a>` closes one that is still open, as in a
        // browser: they never nest. The search stops at a table cell or a
        // template, which a browser's scope does too.
        if matches!(tag.name.as_str(), "button" | "a") {
            if let Some(pos) = dropped.last(&tag.name) {
                dropped.truncate(pos);
            } else if let Some(pos) = stack
                .iter()
                .rposition(|&id| {
                    doc.name(id).is_some_and(|n| {
                        n == tag.name || matches!(n, "td" | "th" | "caption" | "table" | "template")
                    })
                })
                .filter(|&pos| pos > 0 && doc.name(stack[pos]) == Some(tag.name.as_str()))
            {
                stack.truncate(pos);
                dropped.clear();
            }
        }

        // A table part closes what the table holds that is not a table part
        // (a `<p>` a browser moved out of the table), as the browser's
        // "clear the stack back to a table context" does: the row stays in
        // the table, not in the paragraph.
        if matches!(
            tag.name.as_str(),
            "tr" | "tbody" | "thead" | "tfoot" | "caption" | "colgroup"
        ) && let Some(pos) = stack.iter().rposition(|&id| {
            doc.name(id).is_some_and(|n| {
                matches!(
                    n,
                    "table" | "tbody" | "thead" | "tfoot" | "tr" | "td" | "th" | "template"
                )
            })
        }) && matches!(
            doc.name(stack[pos]),
            Some("table" | "tbody" | "thead" | "tfoot")
        ) && pos + 1 < stack.len()
        {
            stack.truncate(pos + 1);
            dropped.clear();
        }

        // A heading closes a heading left open right before it, as in a
        // browser: `<h1>One<h2>Two` is two headings.
        if is_heading(&tag.name) {
            if let Some(top) = dropped.names.last() {
                if is_heading(top) {
                    dropped.truncate(dropped.names.len() - 1);
                }
            } else if stack.len() > 1
                && let Some(&top) = stack.last()
                && doc.name(top).is_some_and(is_heading)
            {
                stack.pop();
            }
        }

        // Browsers ignore `/>` on an HTML element: `<template/>` stays open
        // to `</template>`, and `<script/>` runs to `</script>`. Only a foreign
        // root (`<svg/>`, `<math/>`) closes itself.
        let raw = RAW_TEXT.contains(&tag.name.as_str());
        let is_void = VOID.contains(&tag.name.as_str())
            || (tag.self_closing && matches!(tag.name.as_str(), "svg" | "math"));
        if !is_void && (stack.len() > MAX_DEPTH || !dropped.is_empty()) {
            if raw {
                // Skip the raw text: its content is never markup.
                let body_end = raw_text_end(&html[i..], &tag.name).map_or(bytes.len(), |n| i + n);
                i = body_end + end_tag_len(&html[body_end..]);
            } else {
                let hides = DROP.contains(&tag.name.as_str())
                    || (tag.name == "dialog" && !tag.attrs.iter().any(|(k, _)| k == "open"))
                    || tag.attrs.iter().any(|(k, v)| {
                        k == "hidden" || (k == "aria-hidden" && v.eq_ignore_ascii_case("true"))
                    });
                dropped.push(tag.name, hides);
            }
            continue;
        }
        if dropped.hides() {
            // A void element (an `<img>`) inside a hidden dropped tag.
            continue;
        }
        let name = tag.name.clone();
        let id = push(
            &mut doc,
            &stack,
            Kind::Element {
                name: tag.name,
                attrs: tag.attrs,
            },
        );
        if raw {
            let body_end = raw_text_end(&html[i..], &name).map_or(bytes.len(), |n| i + n);
            let content = &html[i..body_end];
            if !content.is_empty() {
                let text = if name == "title" || name == "textarea" {
                    decode_entities(content, false)
                } else {
                    content.to_owned()
                };
                let tid = doc.nodes.len();
                doc.nodes.push(Node {
                    kind: Kind::Text(text),
                    children: Vec::new(),
                });
                doc.nodes[id].children.push(tid);
            }
            i = body_end + end_tag_len(&html[body_end..]);
        } else if !is_void {
            stack.push(id);
        }
    }
    doc
}

struct Tag {
    name: String,
    attrs: Vec<(String, String)>,
    end: bool,
    self_closing: bool,
    /// Bytes consumed, including `<` and `>`.
    len: usize,
}

/// Result of [`read_tag`].
enum TagRead {
    Tag(Tag),
    /// Not a tag: the `<` is text. The scan stopped at or before the next
    /// `<`, so the parse stays linear.
    Text,
    /// The tag does not end before the input ends.
    Eof,
}

/// Read one tag at the start of `s` (which starts with `<`).
fn read_tag(s: &str) -> TagRead {
    let b = s.as_bytes();
    let mut i = 1;
    let end = b.get(i) == Some(&b'/');
    if end {
        i += 1;
    }
    let name_start = i;
    while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'-' || b[i] == b':') {
        i += 1;
    }
    if i == name_start || !b[name_start].is_ascii_alphabetic() {
        return TagRead::Text;
    }
    let name = s[name_start..i].to_ascii_lowercase();
    let mut attrs = Vec::new();
    let mut self_closing = false;
    loop {
        while i < b.len() && b[i].is_ascii_whitespace() {
            i += 1;
        }
        match b.get(i) {
            None => return TagRead::Eof,
            Some(b'<') => return TagRead::Text,
            Some(b'>') => {
                i += 1;
                break;
            }
            Some(b'/') => {
                self_closing = b.get(i + 1) == Some(&b'>');
                i += 1;
                continue;
            }
            Some(_) => {}
        }
        let key_start = i;
        while i < b.len()
            && !b[i].is_ascii_whitespace()
            && !matches!(b[i], b'=' | b'>' | b'/' | b'<')
        {
            i += 1;
        }
        let key = s[key_start..i].to_ascii_lowercase();
        while i < b.len() && b[i].is_ascii_whitespace() {
            i += 1;
        }
        let mut value = String::new();
        if b.get(i) == Some(&b'=') {
            i += 1;
            while i < b.len() && b[i].is_ascii_whitespace() {
                i += 1;
            }
            match b.get(i) {
                Some(&q @ (b'"' | b'\'')) => {
                    let Some(close) = memchr(q, &b[i + 1..]) else {
                        return TagRead::Eof;
                    };
                    value = decode_entities(&s[i + 1..i + 1 + close], true);
                    i += close + 2;
                }
                Some(_) => {
                    let start = i;
                    while i < b.len() && !b[i].is_ascii_whitespace() && !matches!(b[i], b'>' | b'<')
                    {
                        i += 1;
                    }
                    value = decode_entities(&s[start..i], true);
                }
                None => return TagRead::Eof,
            }
        }
        if !key.is_empty() {
            attrs.push((key, value));
        }
    }
    TagRead::Tag(Tag {
        name,
        attrs,
        end,
        self_closing,
        len: i,
    })
}

fn memchr(needle: u8, hay: &[u8]) -> Option<usize> {
    hay.iter().position(|&c| c == needle)
}

/// Bytes from the comment body `body` (after `<!--`) through the comment's
/// end, as a browser reads it: `<!-->` and `<!--->` are empty comments, and
/// `--!>` ends one as `-->` does. `None` runs the comment to the end.
fn comment_end(body: &str) -> Option<usize> {
    if body.starts_with('>') {
        return Some(1);
    }
    if body.starts_with("->") {
        return Some(2);
    }
    let mut from = 0;
    while let Some(n) = body[from..].find("--") {
        let at = from + n;
        let after = &body[at + 2..];
        if after.starts_with('>') {
            return Some(at + 3);
        }
        if after.starts_with("!>") {
            return Some(at + 4);
        }
        from = at + 1;
    }
    None
}

/// Length of the end tag at the start of `s`. A quoted attribute value can
/// hold a `>` (`</script x=">">`), so the tag is read, not searched for.
fn end_tag_len(s: &str) -> usize {
    match read_tag(s) {
        TagRead::Tag(tag) => tag.len,
        TagRead::Eof => s.len(),
        TagRead::Text => s.find('>').map_or(s.len(), |n| n + 1),
    }
}

/// Offset of the end tag that closes the raw-text element `name`. As in the
/// HTML tokenizer, `</name` counts only when the tag name ends there, so
/// `</scripture>` inside a script is still script text. Nothing closes
/// `<plaintext>`: its text runs to the end of the document.
fn raw_text_end(hay: &str, name: &str) -> Option<usize> {
    if name == "plaintext" {
        return None;
    }
    let close = format!("</{name}");
    let mut from = 0;
    while let Some(n) = find_ascii_ci(&hay[from..], &close) {
        let at = from + n;
        match hay.as_bytes().get(at + close.len()) {
            None | Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r' | b'\x0c') => return Some(at),
            Some(_) => from = at + close.len(),
        }
    }
    None
}

/// Find `needle` (ASCII) in `hay`, ignoring ASCII case.
fn find_ascii_ci(hay: &str, needle: &str) -> Option<usize> {
    let h = hay.as_bytes();
    let n = needle.as_bytes();
    if n.len() > h.len() {
        return None;
    }
    (0..=h.len() - n.len()).find(|&i| h[i..i + n.len()].eq_ignore_ascii_case(n))
}

/// Decode HTML character references. Unknown names stay as written.
fn decode_entities(s: &str, in_attribute: bool) -> String {
    if !s.contains('&') {
        return s.to_owned();
    }
    let mut out = String::with_capacity(s.len());
    let mut rest = s;
    while let Some(amp) = rest.find('&') {
        out.push_str(&rest[..amp]);
        rest = &rest[amp + 1..];
        let used = decode_reference(rest, in_attribute, &mut out);
        if used == 0 {
            out.push('&');
        }
        rest = &rest[used..];
    }
    out.push_str(rest);
    out
}

/// Decode the character reference at the start of `s` (just after the `&`)
/// onto `out`, as the HTML tokenizer does, and return how many bytes of `s`
/// it used; `0` when there is none, and the `&` stays text.
fn decode_reference(s: &str, in_attribute: bool, out: &mut String) -> usize {
    let b = s.as_bytes();
    if b.first() == Some(&b'#') {
        let hex = matches!(b.get(1), Some(b'x' | b'X'));
        let start = if hex { 2 } else { 1 };
        let digits = b[start..]
            .iter()
            .take_while(|c| {
                if hex {
                    c.is_ascii_hexdigit()
                } else {
                    c.is_ascii_digit()
                }
            })
            .count();
        if digits == 0 {
            return 0;
        }
        let radix = if hex { 16 } else { 10 };
        let code = u32::from_str_radix(&s[start..start + digits], radix).unwrap_or(u32::MAX);
        out.push(numeric_reference(code));
        let end = start + digits;
        return end + usize::from(b.get(end) == Some(&b';'));
    }
    let name = b.iter().take_while(|c| c.is_ascii_alphanumeric()).count();
    if name == 0 {
        return 0;
    }
    let named = super::entities::NAMED;
    if b.get(name) == Some(&b';')
        && let Ok(i) = named.binary_search_by(|(key, _)| (*key).cmp(&s[..name]))
    {
        out.push_str(named[i].1);
        return name + 1;
    }
    // The longest legacy name, read without its `;`. In an attribute, one
    // that runs on into a letter, a digit or `=` is left alone, so a query
    // string (`?a=1&copy=2`) keeps its text.
    let legacy = super::entities::LEGACY;
    for len in (1..=name.min(super::entities::LEGACY_MAX)).rev() {
        if let Ok(i) = legacy.binary_search_by(|(key, _)| (*key).cmp(&s[..len])) {
            let next = b.get(len);
            if in_attribute && next.is_some_and(|c| c.is_ascii_alphanumeric() || *c == b'=') {
                return 0;
            }
            out.push_str(legacy[i].1);
            return len;
        }
    }
    0
}

/// The character a numeric reference stands for. As in a browser, the C1
/// range reads as Windows-1252, and NUL, a surrogate or a value past
/// U+10FFFF is U+FFFD.
fn numeric_reference(code: u32) -> char {
    const WINDOWS_1252: [u32; 32] = [
        0x20AC, 0x81, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0x160, 0x2039,
        0x152, 0x8D, 0x17D, 0x8F, 0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
        0x2DC, 0x2122, 0x161, 0x203A, 0x153, 0x9D, 0x17E, 0x178,
    ];
    let code = match code {
        0x80..=0x9F => WINDOWS_1252[(code - 0x80) as usize],
        other => other,
    };
    char::from_u32(code)
        .filter(|&c| c != '\0')
        .unwrap_or('\u{fffd}')
}

fn collapse_ws(s: &str) -> String {
    s.split_ascii_whitespace().collect::<Vec<_>>().join(" ")
}

// ── Writer ──────────────────────────────────────────────────────────────────

/// An end tag no dropped tag answers: close the last kept element it
/// names, and every dropped tag inside it. Unless the tag is `walled`
/// (`</select>`, `</template>`), it cannot close past the innermost open
/// `<select>` or `<template>`, as in a browser.
fn close_kept(doc: &Doc, stack: &mut Vec<usize>, dropped: &mut Dropped, name: &str, walled: bool) {
    let floor = if walled {
        stack
            .iter()
            .rposition(|&id| doc.name(id).is_some_and(|n| WALL.contains(&n)))
            .unwrap_or(0)
    } else {
        0
    };
    if let Some(pos) = stack.iter().rposition(|&id| doc.name(id) == Some(name))
        && pos > floor
    {
        dropped.clear();
        stack.truncate(pos);
    }
}

/// Open start tags past `MAX_DEPTH`. An index by name finds the match of
/// an end tag in constant time, and each tag leaves the stack once, so a
/// page of unmatched end tags still parses in linear time.
#[derive(Default)]
struct Dropped {
    names: Vec<String>,
    /// Per open tag: whether it hides its content (hidden, or a `DROP`
    /// element), as the writer would at a shallower depth.
    hides: Vec<bool>,
    /// How many open tags hide their content.
    hiding: usize,
    at: std::collections::HashMap<String, Vec<usize>>,
    /// Where the open `WALL` tags sit, innermost last.
    walls: Vec<usize>,
    /// Where the open `SCOPE` tags sit, innermost last.
    scopes: Vec<usize>,
}

impl Dropped {
    const fn is_empty(&self) -> bool {
        self.names.is_empty()
    }

    fn push(&mut self, name: String, hides: bool) {
        let pos = self.names.len();
        if WALL.contains(&name.as_str()) {
            self.walls.push(pos);
        }
        if SCOPE.contains(&name.as_str()) {
            self.scopes.push(pos);
        }
        self.at.entry(name.clone()).or_default().push(pos);
        self.names.push(name);
        self.hides.push(hides);
        self.hiding += usize::from(hides);
    }

    /// `true` inside a dropped tag that hides its content.
    const fn hides(&self) -> bool {
        self.hiding > 0
    }

    /// Where the last open dropped `name` sits, if one is open.
    fn last(&self, name: &str) -> Option<usize> {
        self.at.get(name).and_then(|v| v.last()).copied()
    }

    /// Where the last open dropped `select` or `template` sits: end tags
    /// for elements opened before it cannot reach past it.
    fn wall(&self) -> Option<usize> {
        self.walls.last().copied()
    }

    /// Apply a start tag's implied close (`closes`) to the dropped tags.
    /// `true` when the search ended here, on a closed tag or a `SCOPE`;
    /// `false` when it runs on into the kept elements.
    fn implied_close(&mut self, closes: &[&str]) -> bool {
        let found = closes.iter().filter_map(|name| self.last(name)).max();
        let scope = self.scopes.last().copied();
        match (found, scope) {
            (Some(pos), scope) if scope.is_none_or(|scope| pos >= scope) => {
                self.truncate(pos);
                true
            }
            (_, scope) => scope.is_some(),
        }
    }

    /// Close every dropped tag: a kept ancestor's end tag closed them all.
    fn clear(&mut self) {
        self.names.clear();
        self.hides.clear();
        self.hiding = 0;
        self.at.clear();
        self.walls.clear();
        self.scopes.clear();
    }

    /// Close the dropped tag at `pos` and every tag opened after it.
    fn truncate(&mut self, pos: usize) {
        while self.names.len() > pos {
            let top = self.names.len() - 1;
            if self.walls.last() == Some(&top) {
                self.walls.pop();
            }
            if self.scopes.last() == Some(&top) {
                self.scopes.pop();
            }
            if self.hides.pop() == Some(true) {
                self.hiding -= 1;
            }
            if let Some(n) = self.names.pop()
                && let Some(v) = self.at.get_mut(&n)
            {
                v.pop();
            }
        }
    }
}

/// Elements dropped with their content.
const DROP: &[&str] = &[
    "head", "script", "style", "noscript", "template", "svg", "math", "iframe", "object", "canvas",
    "nav", "button", "input", "select", "textarea", "option", "title", "noembed", "noframes",
];

const BLOCK: &[&str] = &[
    "p",
    "div",
    "section",
    "article",
    "header",
    "footer",
    "main",
    "aside",
    "figure",
    "figcaption",
    "form",
    "fieldset",
    "address",
    "details",
    "summary",
    "dl",
    "dt",
    "dd",
    "#root",
    "center",
    "hgroup",
    "search",
];

/// Builds Markdown as a list of blocks. Each block is finished text; blocks
/// join with one blank line.
#[derive(Default)]
struct Writer {
    blocks: Vec<String>,
    /// Inline text of the block being built.
    line: String,
}

impl Writer {
    fn finish(mut self) -> String {
        self.flush();
        if self.blocks.is_empty() {
            return String::new();
        }
        let mut out = self.blocks.join("\n\n");
        out.push('\n');
        out
    }

    /// End the current paragraph.
    fn flush(&mut self) {
        let text = tidy_inline(&self.line);
        self.line.clear();
        if !text.is_empty() {
            // A `<br>` starts a new Markdown line, which can start a block
            // too, so each line is escaped, not only the first. Its leading
            // spaces go: a browser collapses them, and an indented `#` is
            // still a heading.
            let lines: Vec<String> = text
                .split('\n')
                .map(|line| escape_line_start(line.trim_start_matches(' ').to_owned()))
                .collect();
            self.blocks.push(lines.join("\n"));
        }
    }

    fn children(&mut self, doc: &Doc, id: usize, depth: usize) {
        for &child in &doc.nodes[id].children {
            self.node(doc, child, depth + 1);
        }
    }

    #[allow(clippy::too_many_lines)] // one match arm per element kind
    fn node(&mut self, doc: &Doc, id: usize, depth: usize) {
        let (name, _) = match &doc.nodes[id].kind {
            Kind::Text(t) => {
                self.line.push_str(&escape_inline(t));
                return;
            }
            Kind::Element { name, attrs } => (name.as_str(), attrs),
        };
        if is_hidden(doc, id) || DROP.contains(&name) {
            return;
        }
        if depth > MAX_DEPTH {
            self.line.push_str(&escape_inline(&doc.text_of(id)));
            return;
        }
        match name {
            "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => {
                self.flush();
                let level = usize::from(name.as_bytes()[1] - b'0');
                let text = Self::inline_of(doc, id, depth);
                if !text.is_empty() {
                    self.blocks.push(format!("{} {text}", "#".repeat(level)));
                }
            }
            "br" => self.line.push_str("  \n"),
            "hr" => {
                self.flush();
                self.blocks.push("---".to_owned());
            }
            "strong" | "b" => self.wrap(doc, id, depth, "**"),
            "em" | "i" => self.wrap(doc, id, depth, "*"),
            "del" | "s" | "strike" => self.wrap(doc, id, depth, "~~"),
            "code" | "kbd" | "samp" => {
                let raw = doc.text_of(id);
                let text = collapse_ws(&raw);
                if !text.is_empty() {
                    // Edge spaces go outside the span, as with other markers.
                    let lead = if raw.starts_with(char::is_whitespace) {
                        " "
                    } else {
                        ""
                    };
                    let trail = if raw.ends_with(char::is_whitespace) {
                        " "
                    } else {
                        ""
                    };
                    let fence = "`".repeat(longest_backtick_run(&text) + 1);
                    let pad = if text.starts_with('`') || text.ends_with('`') {
                        " "
                    } else {
                        ""
                    };
                    let _ = write!(self.line, "{lead}{fence}{pad}{text}{pad}{fence}{trail}");
                }
            }
            "a" => {
                let (lead, text, trail) = Self::inline_parts(doc, id, depth);
                self.line.push_str(lead);
                match doc
                    .attr(id, "href")
                    .and_then(|h| safe_url(&doc.resolve(h), true))
                {
                    // A link with no text shows nothing, so it gets no
                    // label made up from its URL.
                    Some(href) if !text.is_empty() => {
                        // A `!` right before the label would make it an image,
                        // unless an odd run of `\` escapes it.
                        if let Some(before) = self.line.strip_suffix('!')
                            && before.bytes().rev().take_while(|&b| b == b'\\').count() % 2 == 0
                        {
                            self.line.pop();
                            self.line.push_str("\\!");
                        }
                        // A destination decodes character references too,
                        // so an `&copy;` the URL holds as text is escaped.
                        let _ = write!(self.line, "[{text}]({})", escape_entity_like(&href));
                    }
                    _ => self.line.push_str(&text),
                }
                self.line.push_str(trail);
            }
            "img" => {
                // With no `src`, a browser loads a `srcset` candidate; the
                // Markdown takes the first.
                let src = doc
                    .attr(id, "src")
                    .filter(|s| !s.trim().is_empty())
                    .or_else(|| doc.attr(id, "srcset").and_then(first_srcset_url));
                if let Some(src) = src.and_then(|s| safe_url(&doc.resolve(s), false)) {
                    let alt = escape_inline(&collapse_ws(doc.attr(id, "alt").unwrap_or("")));
                    let _ = write!(self.line, "![{alt}]({})", escape_entity_like(&src));
                }
            }
            "pre" => {
                self.flush();
                let code = doc.text_of(id);
                // As in a browser, only a newline right after `<pre>` and one
                // right before `</pre>` are not content; spaces are.
                let code = code.strip_prefix('\n').unwrap_or(&code);
                let code = code.strip_suffix('\n').unwrap_or(code);
                let lang = doc.nodes[id]
                    .children
                    .iter()
                    .find(|&&c| doc.name(c) == Some("code"))
                    .and_then(|&c| doc.attr(c, "class"))
                    .and_then(|class| {
                        class
                            .split_ascii_whitespace()
                            .find_map(|c| c.strip_prefix("language-"))
                    })
                    // A language name is a word: a backtick in it would
                    // break the fence.
                    .filter(|lang| {
                        lang.chars().all(|c| {
                            c.is_alphanumeric() || matches!(c, '-' | '_' | '+' | '.' | '#')
                        })
                    })
                    .unwrap_or("");
                let fence = "`".repeat((longest_backtick_run(code) + 1).max(3));
                self.blocks.push(format!("{fence}{lang}\n{code}\n{fence}"));
            }
            "blockquote" => {
                self.flush();
                let inner = Self::sub_block(doc, id, depth);
                if !inner.is_empty() {
                    let quoted: Vec<String> = inner
                        .lines()
                        .map(|l| {
                            if l.is_empty() {
                                ">".to_owned()
                            } else {
                                format!("> {l}")
                            }
                        })
                        .collect();
                    self.blocks.push(quoted.join("\n"));
                }
            }
            "ul" | "ol" | "menu" => {
                self.flush();
                self.blocks.extend(Self::list(doc, id, depth, name == "ol"));
            }
            "table" => {
                self.flush();
                let table = Self::table(doc, id, depth);
                if !table.is_empty() {
                    self.blocks.push(table);
                }
            }
            "li" => {
                // A list item outside a list: treat as a paragraph.
                self.flush();
                self.children(doc, id, depth);
                self.flush();
            }
            "label" | "legend" | "caption" => {
                self.flush();
                self.children(doc, id, depth);
                self.flush();
            }
            _ if BLOCK.contains(&name) => {
                self.flush();
                self.children(doc, id, depth);
                self.flush();
            }
            _ => self.children(doc, id, depth),
        }
    }

    fn wrap(&mut self, doc: &Doc, id: usize, depth: usize, mark: &str) {
        let (lead, text, trail) = Self::inline_parts(doc, id, depth);
        if !text.is_empty() {
            let _ = write!(self.line, "{lead}{mark}{text}{mark}{trail}");
        }
    }

    /// Render the children of `id` as one inline string, on one line.
    fn inline_of(doc: &Doc, id: usize, depth: usize) -> String {
        Self::inline_parts(doc, id, depth).1
    }

    /// [`Self::inline_of`], plus the space that sat at each edge, so a
    /// marker never glues two words together.
    fn inline_parts(doc: &Doc, id: usize, depth: usize) -> (&'static str, String, &'static str) {
        let mut sub = Self::default();
        sub.children(doc, id, depth);
        let edge = |c: Option<char>| {
            if c.is_some_and(char::is_whitespace) {
                " "
            } else {
                ""
            }
        };
        let lead = edge(sub.line.chars().next());
        let trail = edge(sub.line.chars().last());
        sub.flush();
        let text = sub.blocks.join(" ").replace("  \n", " ").replace('\n', " ");
        (lead, text, trail)
    }

    /// Render the children of `id` as finished blocks.
    fn sub_block(doc: &Doc, id: usize, depth: usize) -> String {
        let mut sub = Self::default();
        sub.children(doc, id, depth);
        sub.finish().trim_end().to_owned()
    }

    /// A list as blocks: its items, and what a browser shows between them.
    /// Text or an element that is no `<li>` stays in place, so it splits the
    /// list there; numbering carries on past it.
    fn list(doc: &Doc, id: usize, depth: usize, ordered: bool) -> Vec<String> {
        let mut blocks = Vec::new();
        let mut lines: Vec<String> = Vec::new();
        let mut other = Self::default();
        let end_other = |other: &mut Self, lines: &mut Vec<String>, blocks: &mut Vec<String>| {
            let text = std::mem::take(other).finish();
            let text = text.trim_end();
            if !text.is_empty() {
                if !lines.is_empty() {
                    blocks.push(std::mem::take(lines).join("\n"));
                }
                blocks.push(text.to_owned());
            }
        };
        let numbers = if ordered {
            Self::item_numbers(doc, id)
        } else {
            Vec::new()
        };
        // `<ol type>` picks letters or Roman numerals; the attribute is case
        // sensitive.
        let style = doc
            .attr(id, "type")
            .filter(|t| matches!(*t, "a" | "A" | "i" | "I"));
        // A Markdown list number is 0 to 999999999, in decimal. For any
        // other label, the items are bullets that spell out their labels.
        let as_bullets = style.is_some() || numbers.iter().any(|n| !(0..=999_999_999).contains(n));
        let mut numbers = numbers.into_iter();
        for &item in &doc.nodes[id].children {
            if doc.name(item) != Some("li") {
                other.node(doc, item, depth + 1);
                continue;
            }
            if is_hidden(doc, item) {
                continue;
            }
            end_other(&mut other, &mut lines, &mut blocks);
            let (marker, label) = match numbers.next() {
                Some(n) if as_bullets => ("- ".to_owned(), format!("{}\\. ", list_label(n, style))),
                Some(n) => (format!("{n}. "), String::new()),
                None => ("- ".to_owned(), String::new()),
            };
            let pad = " ".repeat(marker.len());
            // A nested list sits under the item text with no blank line.
            let body = collapse_nested_list_gap(&Self::sub_block(doc, item, depth + 1));
            for (k, l) in body.lines().enumerate() {
                if k == 0 {
                    lines.push(format!("{marker}{label}{l}"));
                } else if l.is_empty() {
                    lines.push(String::new());
                } else {
                    lines.push(format!("{pad}{l}"));
                }
            }
            if body.is_empty() {
                lines.push(format!("{marker}{label}").trim_end().to_owned());
            }
        }
        end_other(&mut other, &mut lines, &mut blocks);
        if !lines.is_empty() {
            blocks.push(lines.join("\n"));
        }
        blocks
    }

    /// The number a browser shows on each visible item of the `<ol>` `id`.
    /// Counters are signed. `<ol reversed>` counts down, from the item count
    /// unless `start` says otherwise; `<li value>` renumbers from that item
    /// on.
    fn item_numbers(doc: &Doc, id: usize) -> Vec<i64> {
        let items: Vec<usize> = doc.nodes[id]
            .children
            .iter()
            .copied()
            .filter(|&c| doc.name(c) == Some("li") && !is_hidden(doc, c))
            .collect();
        let reversed = doc.attr(id, "reversed").is_some();
        let count = i64::try_from(items.len()).unwrap_or(i64::MAX);
        let mut n: i64 = doc
            .attr(id, "start")
            .and_then(|v| v.trim().parse().ok())
            .unwrap_or(if reversed { count } else { 1 });
        items
            .iter()
            .map(|&item| {
                if let Some(v) = doc.attr(item, "value").and_then(|v| v.trim().parse().ok()) {
                    n = v;
                }
                let number = n;
                n = if reversed {
                    n.saturating_sub(1)
                } else {
                    n.saturating_add(1)
                };
                number
            })
            .collect()
    }

    /// The text of each row's cells, in the columns a browser gives them.
    fn place_cells(doc: &Doc, rows_at: &[TableRow], depth: usize) -> Vec<Vec<String>> {
        // GFM has no spans: a place a `rowspan` or `colspan` covers is an
        // empty cell, so later cells keep the columns a browser gives them.
        // Placeholders are bounded by the table's own cells, so a hostile
        // span cannot blow up the output.
        let own: usize = rows_at.iter().map(|row| row.cells.len()).sum();
        let mut budget = own.saturating_mul(8).saturating_add(64);
        // The row index each column stays covered until (exclusive).
        let mut covered_until: Vec<usize> = Vec::new();
        let mut rows = Vec::new();
        for (r, row) in rows_at.iter().enumerate() {
            // A span ends with its row group (`thead`, `tbody`, `tfoot`).
            if r > 0 && rows_at[r - 1].group != row.group {
                covered_until.clear();
            }
            let mut cells = Vec::new();
            let mut col = 0;
            for &c in &row.cells {
                while budget > 0 && covered_until.get(col).is_some_and(|&u| u > r) {
                    cells.push(String::new());
                    budget -= 1;
                    col += 1;
                }
                cells.push(
                    Self::inline_of(doc, c, depth + 1)
                        .replace('|', "\\|")
                        .replace('\n', " "),
                );
                let span = |name: &str| {
                    doc.attr(c, name)
                        .and_then(|v| v.trim().parse::<usize>().ok())
                };
                // `rowspan="0"` runs to the end of its row group.
                let rowspan = match span("rowspan") {
                    Some(0) => usize::MAX,
                    Some(n) => n.min(65_534),
                    None => 1,
                };
                let colspan = span("colspan").unwrap_or(1).clamp(1, 1000);
                for k in 0..colspan {
                    if k > 0 {
                        if budget == 0 {
                            break;
                        }
                        cells.push(String::new());
                        budget -= 1;
                    }
                    if covered_until.len() <= col {
                        covered_until.resize(col + 1, 0);
                    }
                    covered_until[col] = r.saturating_add(rowspan);
                    col += 1;
                }
            }
            if !cells.is_empty() {
                rows.push(cells);
            }
        }
        rows
    }

    fn table(doc: &Doc, id: usize, depth: usize) -> String {
        let mut stack = vec![id];
        // A cell outside any `<tr>` opens a row in a browser, which takes
        // the cells after it up to the next `<tr>` or section.
        let mut rows_at: Vec<TableRow> = Vec::new();
        // Text or an element that is no table part: a browser moves it out,
        // before the table, and shows it.
        let mut foster_ids = Vec::new();
        let end_row = |implicit: &mut Vec<usize>, group: usize, rows_at: &mut Vec<TableRow>| {
            if let Some(&first) = implicit.first() {
                rows_at.push(TableRow {
                    at: first,
                    group,
                    cells: std::mem::take(implicit),
                });
            }
        };
        while let Some(n) = stack.pop() {
            let mut implicit = Vec::new();
            for &c in &doc.nodes[n].children {
                if is_hidden(doc, c) {
                    continue;
                }
                match doc.name(c) {
                    Some("td" | "th") => implicit.push(c),
                    Some("tr") => {
                        end_row(&mut implicit, n, &mut rows_at);
                        // In a row, anything but a cell is moved out too.
                        let (cells, other): (Vec<usize>, Vec<usize>) = doc.nodes[c]
                            .children
                            .iter()
                            .copied()
                            .filter(|&k| !is_hidden(doc, k))
                            .partition(|&k| matches!(doc.name(k), Some("td" | "th")));
                        foster_ids.extend(other);
                        rows_at.push(TableRow {
                            at: c,
                            group: n,
                            cells,
                        });
                    }
                    Some("thead" | "tbody" | "tfoot") => {
                        end_row(&mut implicit, n, &mut rows_at);
                        stack.push(c);
                    }
                    Some("caption" | "colgroup" | "col") => {}
                    _ => foster_ids.push(c),
                }
            }
            end_row(&mut implicit, n, &mut rows_at);
        }
        // Sections are read last-first; rebuild document order.
        rows_at.sort_unstable_by_key(|r| r.at);
        foster_ids.sort_unstable();
        let mut foster = Self::default();
        for c in foster_ids {
            foster.node(doc, c, depth + 1);
        }
        let foster = foster.finish().trim_end().to_owned();
        let rows = Self::place_cells(doc, &rows_at, depth);
        // A visible `<caption>` goes above the table, as its own paragraph.
        let caption = doc.nodes[id]
            .children
            .iter()
            .find(|&&c| doc.name(c) == Some("caption") && !is_hidden(doc, c))
            .map(|&c| escape_line_start(Self::inline_of(doc, c, depth + 1)))
            .filter(|c| !c.is_empty());
        let lead: Vec<String> = [Some(foster), caption]
            .into_iter()
            .flatten()
            .filter(|b| !b.is_empty())
            .collect();
        let Some(width) = rows.iter().map(Vec::len).max() else {
            return lead.join("\n\n");
        };
        let mut out: Vec<String> = lead.into_iter().flat_map(|b| [b, String::new()]).collect();
        for (k, row) in rows.iter().enumerate() {
            // Only the header row sets the column count; GFM fills a short
            // row with empty cells, so the others are not padded.
            if k == 0 {
                let mut cells = row.clone();
                cells.resize(width, String::new());
                out.push(format!("| {} |", cells.join(" | ")));
                out.push(format!("|{}", " --- |".repeat(width)));
            } else {
                out.push(format!("| {} |", row.join(" | ")));
            }
        }
        out.join("\n")
    }
}

/// The label a browser shows for item number `n` of an `<ol type>`:
/// letters (`a`, `A`) from 1 up, Roman numerals (`i`, `I`) from 1 to 3999,
/// and decimal otherwise.
fn list_label(n: i64, style: Option<&str>) -> String {
    let upper = style.is_some_and(|s| s == "A" || s == "I");
    let label = match style {
        Some("a" | "A") if n >= 1 => {
            // Bijective base 26: z, aa, ab, ...
            let mut n = n;
            let mut letters = Vec::new();
            while n > 0 {
                n -= 1;
                letters.push(b'a' + u8::try_from(n % 26).unwrap_or(0));
                n /= 26;
            }
            letters.iter().rev().map(|&b| char::from(b)).collect()
        }
        Some("i" | "I") if (1..=3999).contains(&n) => {
            const ROMAN: [(i64, &str); 13] = [
                (1000, "m"),
                (900, "cm"),
                (500, "d"),
                (400, "cd"),
                (100, "c"),
                (90, "xc"),
                (50, "l"),
                (40, "xl"),
                (10, "x"),
                (9, "ix"),
                (5, "v"),
                (4, "iv"),
                (1, "i"),
            ];
            let mut n = n;
            let mut out = String::new();
            for (value, digits) in ROMAN {
                while n >= value {
                    out.push_str(digits);
                    n -= value;
                }
            }
            out
        }
        _ => return n.to_string(),
    };
    if upper {
        label.to_ascii_uppercase()
    } else {
        label
    }
}

/// A table row: its first node (for document order), the section it sits
/// in, and its cells.
struct TableRow {
    at: usize,
    group: usize,
    cells: Vec<usize>,
}

/// Join a list item's text and its nested list with one newline.
fn collapse_nested_list_gap(body: &str) -> String {
    let mut out = String::with_capacity(body.len());
    let lines: Vec<&str> = body.split('\n').collect();
    let mut k = 0;
    while k < lines.len() {
        let l = lines[k];
        if l.is_empty() && lines.get(k + 1).is_some_and(|n| is_list_line(n)) && k > 0 {
            k += 1;
            continue;
        }
        if k > 0 {
            out.push('\n');
        }
        out.push_str(l);
        k += 1;
    }
    out
}

fn is_list_line(l: &str) -> bool {
    l.starts_with("- ")
        || l.split_once(". ")
            .is_some_and(|(n, _)| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))
}

fn is_hidden(doc: &Doc, id: usize) -> bool {
    doc.attr(id, "hidden").is_some()
        || doc
            .attr(id, "aria-hidden")
            .is_some_and(|v| v.eq_ignore_ascii_case("true"))
        // A `<dialog>` shows only while `open`.
        || (doc.name(id) == Some("dialog") && doc.attr(id, "open").is_none())
}

/// Collapse inline whitespace; keep the hard breaks that `<br>` wrote.
fn tidy_inline(s: &str) -> String {
    s.split("  \n")
        .map(collapse_ws)
        .collect::<Vec<_>>()
        .join("  \n")
        .trim_matches(|c: char| c == ' ' || c == '\n')
        .to_owned()
}

/// Escape Markdown syntax in text. `<` is escaped too, so decoded text
/// never becomes raw HTML in a Markdown renderer.
pub(super) fn escape_inline(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if matches!(c, '\\' | '*' | '_' | '`' | '[' | ']' | '<' | '~') {
            out.push('\\');
        }
        out.push(c);
    }
    escape_entity_like(&out)
}

/// A link or image URL that is safe to write, percent-encoded for a
/// Markdown link. Relative URLs and `http`/`https` pass; `mailto` passes for
/// links. Every other scheme (`javascript:`, `data:`, ...) gives `None`.
fn safe_url(raw: &str, link: bool) -> Option<String> {
    // The URL parser trims C0 controls and spaces, then drops every tab and
    // newline, so `https://exa&#10;mple.com` is `https://example.com`.
    let url: String = raw
        .trim_matches(|c: char| c <= ' ')
        .chars()
        .filter(|c| !matches!(c, '\t' | '\n' | '\r'))
        .collect();
    // An empty `href` links to the page itself; an empty `src` shows nothing.
    if url.is_empty() {
        return link.then(String::new);
    }
    // A character reference the decoder left (`javascript&colon;`) still
    // spells a scheme once a Markdown renderer decodes it, so a reference
    // (`&name;`, `&#…;`) before the first `/`, `?` or `#` is refused. A
    // plain `&`, as in `R&D/guide`, decodes to nothing and stays.
    let head: String = url
        .chars()
        .filter(|c| !c.is_ascii_whitespace() && !c.is_control())
        .take_while(|c| !matches!(c, '/' | '?' | '#'))
        .collect();
    // The `#` of a numeric reference (`&#58;`) is no fragment here, so the
    // reference check reads on to the first `/` or `?`.
    let reference_head: String = url
        .chars()
        .filter(|c| !c.is_ascii_whitespace() && !c.is_control())
        .take_while(|c| !matches!(c, '/' | '?'))
        .collect();
    if has_reference(&reference_head) {
        return None;
    }
    // Whatever comes before a `:` there is a scheme, however long.
    let scheme = head.split_once(':').map(|(s, _)| s.to_ascii_lowercase());
    let special = match scheme.as_deref() {
        None | Some("http" | "https") => true,
        Some("mailto") if link => false,
        Some(_) => return None,
    };
    if special {
        // An http(s) URL, or a relative one on an http(s) page: the URL
        // parser reads `\` as `/` before the query.
        let end = url.find(['?', '#']).unwrap_or(url.len());
        let (path, rest) = url.split_at(end);
        if path.contains('\\') {
            return Some(encode_destination(&format!(
                "{}{rest}",
                path.replace('\\', "/")
            )));
        }
    }
    Some(encode_destination(&url))
}

/// `url` as a Markdown link destination: percent-encoded and with
/// entity-shaped `&` escaped, so it reads back as written. It checks no
/// scheme; use it for URLs Autumn builds itself.
pub(super) fn link_destination(url: &str) -> String {
    escape_entity_like(&encode_destination(url))
}

/// Percent-encode what would end or break a Markdown link destination.
fn encode_destination(url: &str) -> String {
    let mut out = String::with_capacity(url.len());
    for c in url.chars() {
        match c {
            ' ' => out.push_str("%20"),
            '(' => out.push_str("%28"),
            ')' => out.push_str("%29"),
            '<' => out.push_str("%3C"),
            '>' => out.push_str("%3E"),
            // A `\` would escape the closing `)` of the Markdown link.
            '\\' => out.push_str("%5C"),
            c if c.is_control() => {
                let mut buf = [0u8; 4];
                for b in c.encode_utf8(&mut buf).bytes() {
                    let _ = write!(out, "%{b:02X}");
                }
            }
            c => out.push(c),
        }
    }
    out
}

/// `true` when `s` holds a character reference a Markdown renderer would
/// decode: `&`, then letters, digits or `#`, then `;`.
fn has_reference(s: &str) -> bool {
    s.match_indices('&').any(|(i, _)| {
        let rest = &s[i + 1..];
        let len = rest
            .bytes()
            .take_while(|b| b.is_ascii_alphanumeric() || *b == b'#')
            .count();
        len > 0 && rest.as_bytes().get(len) == Some(&b';')
    })
}

/// The URL of the first candidate in a `srcset`: past leading whitespace
/// and commas, up to the next whitespace, without trailing commas.
fn first_srcset_url(srcset: &str) -> Option<&str> {
    let start = srcset.trim_start_matches(|c: char| c.is_ascii_whitespace() || c == ',');
    let end = start
        .find(|c: char| c.is_ascii_whitespace())
        .unwrap_or(start.len());
    Some(start[..end].trim_end_matches(',')).filter(|u| !u.is_empty())
}

/// Escape `&` where it starts text a renderer would read as an entity
/// (`&lt;`, `&#60;`).
fn escape_entity_like(s: &str) -> String {
    if !s.contains('&') {
        return s.to_owned();
    }
    let mut out = String::with_capacity(s.len() + 4);
    for (i, c) in s.char_indices() {
        if c == '&' {
            let rest = &s[i + 1..];
            let name_len = rest
                .bytes()
                .take_while(|b| b.is_ascii_alphanumeric() || *b == b'#')
                .count();
            if name_len > 0 && rest.as_bytes().get(name_len) == Some(&b';') {
                out.push('\\');
            }
        }
        out.push(c);
    }
    out
}

/// Escape a marker at the start of a paragraph line that Markdown would
/// read as a heading, quote, list, or rule.
fn escape_line_start(text: String) -> String {
    let first = text.chars().next();
    if matches!(first, Some('#' | '>' | '-' | '+' | '=')) {
        return format!("\\{text}");
    }
    let digits = text.bytes().take_while(u8::is_ascii_digit).count();
    if digits > 0
        && matches!(text.as_bytes().get(digits), Some(b'.' | b')'))
        && text
            .as_bytes()
            .get(digits + 1)
            .is_none_or(u8::is_ascii_whitespace)
    {
        return format!("{}\\{}", &text[..digits], &text[digits..]);
    }
    text
}

/// Length of the longest run of backticks in `s`.
fn longest_backtick_run(s: &str) -> usize {
    s.split(|c| c != '`').map(str::len).max().unwrap_or(0)
}

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

    fn md(html: &str) -> String {
        html_to_markdown(html)
    }

    #[test]
    fn headings_and_paragraphs() {
        assert_eq!(
            md("<h1>Title</h1><p>One   two\n three.</p><h2>Sub</h2><p>Four</p>"),
            "# Title\n\nOne two three.\n\n## Sub\n\nFour\n"
        );
    }

    #[test]
    fn front_matter_from_head() {
        let out = md("<!DOCTYPE html><html><head><title>Hi &amp; bye</title>\
             <meta name=\"description\" content=\"A &quot;page&quot;\"></head>\
             <body><p>Body</p></body></html>");
        assert_eq!(
            out,
            "---\ntitle: \"Hi & bye\"\ndescription: \"A \\\"page\\\"\"\n---\n\nBody\n"
        );
    }

    #[test]
    fn main_wins_over_body_chrome() {
        let out = md("<body><header><nav><a href=\"/\">Home</a></nav></header>\
             <main><h1>Post</h1><p>Text</p></main><footer>Foot</footer></body>");
        assert_eq!(out, "# Post\n\nText\n");
    }

    #[test]
    fn scripts_styles_and_hidden_content_are_dropped() {
        let out = md(
            "<p>a</p><script>var x = '<p>no</p>';</script><style>p{}</style>\
             <div hidden>gone</div><span aria-hidden=\"true\">icon</span><p>b</p>",
        );
        assert_eq!(out, "a\n\nb\n");
    }

    #[test]
    fn inline_formatting() {
        assert_eq!(
            md("<p><strong>bold</strong> <em>it</em> <code>a*b</code> \
                <a href=\"/x?a=1&amp;b=2\">link</a> <img src=\"/i.png\" alt=\"pic\"></p>"),
            "**bold** *it* `a*b` [link](/x?a=1&b=2) ![pic](/i.png)\n"
        );
    }

    #[test]
    fn text_with_markdown_syntax_is_escaped() {
        assert_eq!(md("<p>2 * 3 = [six]_</p>"), "2 \\* 3 = \\[six\\]\\_\n");
    }

    #[test]
    fn an_encoded_scheme_separator_is_not_a_safe_link() {
        assert_eq!(
            md("<p><a href=\"javascript&colon;alert(1)\">x</a></p>"),
            "x\n"
        );
        let out = md("<p>a <img src=\"data&colon;x\" alt=\"i\"> b</p>");
        assert!(!out.contains("data"), "{out}");
        // An `&` in the query is fine.
        assert_eq!(
            md("<p><a href=\"/s?a=1&amp;b=2\">s</a></p>"),
            "[s](/s?a=1&b=2)\n"
        );
    }

    #[test]
    fn javascript_links_keep_only_their_text() {
        assert_eq!(md("<p><a href=\"javascript:alert(1)\">x</a></p>"), "x\n");
    }

    #[test]
    fn lists_nest() {
        assert_eq!(
            md("<ul><li>a</li><li>b<ol><li>c</li><li>d</li></ol></li></ul>"),
            "- a\n- b\n  1. c\n  2. d\n"
        );
    }

    #[test]
    fn implied_end_tags_close_list_items_and_paragraphs() {
        assert_eq!(md("<ul><li>a<li>b</ul><p>x<p>y"), "- a\n- b\n\nx\n\ny\n");
    }

    #[test]
    fn preformatted_code_keeps_whitespace_and_language() {
        assert_eq!(
            md("<pre><code class=\"language-rust\">fn main() {\n    1 &lt; 2\n}</code></pre>"),
            "```rust\nfn main() {\n    1 < 2\n}\n```\n"
        );
    }

    #[test]
    fn blockquote_prefixes_every_line() {
        assert_eq!(
            md("<blockquote><p>a</p><p>b</p></blockquote>"),
            "> a\n>\n> b\n"
        );
    }

    #[test]
    fn tables_become_gfm() {
        assert_eq!(
            md("<table><thead><tr><th>A</th><th>B|C</th></tr></thead>\
                <tbody><tr><td>1</td><td>2</td></tr></tbody></table>"),
            "| A | B\\|C |\n| --- | --- |\n| 1 | 2 |\n"
        );
    }

    #[test]
    fn hidden_table_parts_stay_hidden() {
        assert_eq!(
            md("<table><tr><th>A</th><th hidden>secret</th></tr>\
                <tbody aria-hidden=\"true\"><tr><td>gone</td></tr></tbody>\
                <tbody><tr hidden><td>gone</td></tr><tr><td>1</td></tr></tbody></table>"),
            "| A |\n| --- |\n| 1 |\n"
        );
    }

    #[test]
    fn line_breaks_and_rules() {
        assert_eq!(md("<p>a<br>b</p><hr><p>c</p>"), "a  \nb\n\n---\n\nc\n");
    }

    #[test]
    fn entities_decode() {
        assert_eq!(
            md("<p>&lt;&gt;&amp;&#39;&#x41;&nbsp;&copy;&unknown;</p>"),
            "\\<>&'A\u{a0}©\\&unknown;\n"
        );
    }

    #[test]
    fn forms_keep_labels_and_drop_controls() {
        assert_eq!(
            md(
                "<form><label>Name</label><input name=\"n\"><button>Go</button>\
                <select><option>x</option></select><textarea>t</textarea></form>"
            ),
            "Name\n"
        );
    }

    #[test]
    fn unsafe_urls_are_dropped_and_text_cannot_become_html() {
        assert_eq!(
            md("<p><a href=\"java&#9;script:alert(1)\">a</a> \
                <a href=\"data:text/html,x\">b</a> <img src=\"data:x\" alt=\"c\"> \
                <a href=\"mailto:me@example.com\">d</a> \
                <a href=\"/x y(1)\">e</a> &lt;img src=x onerror=1&gt;</p>"),
            "a b [d](mailto:me@example.com) [e](/x%20y%281%29) \\<img src=x onerror=1>\n"
        );
    }

    #[test]
    fn paragraph_markers_and_entity_text_are_escaped() {
        assert_eq!(md("<p># of items</p>"), "\\# of items\n");
        assert_eq!(md("<p>1. Intro</p>"), "1\\. Intro\n");
        assert_eq!(md("<p>&amp;lt;</p>"), "\\&lt;\n");
        assert_eq!(md("<p>AT&amp;T</p>"), "AT&T\n");
    }

    #[test]
    fn inline_edges_keep_their_spaces() {
        assert_eq!(
            md("<p>Hello<strong> world</strong>!</p>"),
            "Hello **world**!\n"
        );
        assert_eq!(
            md("<p>See<a href=\"/x\"> this </a>now</p>"),
            "See [this](/x) now\n"
        );
        assert_eq!(md("<h2>a<br>b</h2>"), "## a b\n");
    }

    #[test]
    fn hidden_and_template_mains_are_skipped() {
        assert_eq!(md("<main hidden>secret</main><main>real</main>"), "real\n");
        assert_eq!(
            md("<template><main>tpl</main></template><main>real</main>"),
            "real\n"
        );
    }

    #[test]
    fn iframe_fallback_is_raw_text() {
        assert_eq!(
            md("<div><iframe></div><main>hidden</main></iframe><main>real</main></div>"),
            "real\n"
        );
    }

    #[test]
    fn an_omitted_head_end_tag_does_not_swallow_the_body() {
        assert_eq!(
            md("<head><title>T</title><body><main>Visible</main>"),
            "---\ntitle: \"T\"\n---\n\nVisible\n"
        );
        assert_eq!(
            md("<html><head><meta name=description content=d><p>Body text"),
            "---\ndescription: \"d\"\n---\n\nBody text\n"
        );
    }

    #[test]
    fn a_template_in_the_head_keeps_its_content() {
        assert_eq!(
            md(
                "<head><template><main>secret</main></template></head><body><main>visible</main></body>"
            ),
            "visible\n"
        );
    }

    #[test]
    fn a_kept_end_tag_closes_the_dropped_tags_inside_it() {
        let deep = "<div>".repeat(250);
        let spans = "<span>".repeat(100);
        let out = md(&format!(
            "{deep}<section hidden>{spans}secret</section><main>visible</main>"
        ));
        assert!(!out.contains("secret"), "{out}");
        assert!(out.contains("visible"), "{out}");
    }

    #[test]
    fn an_input_like_tag_closes_a_select() {
        assert_eq!(md("<select><input><main>Visible</main>"), "Visible\n");
        assert_eq!(md("<select><option>a<select><p>Shown</p>"), "Shown\n");
        let deep = "<div>".repeat(300) + "<select><textarea>t</textarea>Visible";
        assert!(md(&deep).contains("Visible"));
    }

    #[test]
    fn a_stray_end_tag_cannot_leave_a_select() {
        assert_eq!(
            md("<div><select></div><main>hidden</main></select><main>real</main></div>"),
            "real\n"
        );
        assert_eq!(
            md("<div><template></div><main>hidden</main></template><main>real</main></div>"),
            "real\n"
        );
    }

    #[test]
    fn an_implied_close_ends_the_dropped_tags_inside_it() {
        let deep = "<div>".repeat(250);
        let spans = "<span>".repeat(100);
        let out = md(&format!(
            "{deep}<p>first{spans}<span hidden>secret<p>visible</p>after"
        ));
        assert!(!out.contains("secret"), "{out}");
        assert!(out.contains("visible") && out.contains("after"), "{out}");
    }

    #[test]
    fn every_html_block_closes_an_open_paragraph() {
        assert_eq!(
            md("<p hidden>secret<address>Visible</address><p>After"),
            "Visible\n\nAfter\n"
        );
        for block in [
            "details",
            "fieldset",
            "figcaption",
            "hgroup",
            "menu",
            "search",
            "summary",
        ] {
            let out = md(&format!("<p hidden>secret<{block}>Visible</{block}>"));
            assert!(
                !out.contains("secret") && out.contains("Visible"),
                "{block}: {out}"
            );
        }
        // A list item closes the paragraph inside the item before it.
        let out = md("<ul><li><p>one<li>two</ul>");
        assert!(out.contains("- one") && out.contains("- two"), "{out}");
        let out = md("<p hidden>secret<li>shown");
        assert!(!out.contains("secret") && out.contains("shown"), "{out}");
    }

    #[test]
    fn plaintext_runs_to_the_end_of_the_document() {
        let out = md("<p>before</p><nav><plaintext></nav><main>secret</main>");
        assert!(out.contains("before") && !out.contains("secret"), "{out}");
        let deep = "<div>".repeat(300) + "<nav><plaintext></nav><p>secret</p>";
        assert!(!md(&deep).contains("secret"));
    }

    #[test]
    fn a_raw_text_end_tag_ends_at_its_own_close() {
        assert_eq!(
            md("<body><script>x</script x=\"<b>secret</b>\">visible</body>"),
            "visible\n"
        );
        let deep = "<div>".repeat(300) + "<script>x</script x='<b>secret</b>'>ok";
        let out = md(&deep);
        assert!(!out.contains("secret") && out.contains("ok"), "{out}");
    }

    #[test]
    fn a_marker_after_a_line_break_is_escaped() {
        assert_eq!(
            md("<p>Hello<br># not a heading<br>- not a list<br>1. not a list</p>"),
            "Hello  \n\\# not a heading  \n\\- not a list  \n1\\. not a list\n"
        );
        assert_eq!(md("<p>Hello<br> # indented</p>"), "Hello  \n\\# indented\n");
    }

    #[test]
    fn only_an_open_dialog_is_content() {
        let out = md("<main><p>Page</p><dialog open><p>Confirm?</p></dialog>\
             <dialog><p>secret</p></dialog></main>");
        assert!(out.contains("Confirm?") && !out.contains("secret"), "{out}");
        let deep = "<div>".repeat(300) + "<dialog>secret</dialog><dialog open>shown</dialog>";
        let out = md(&deep);
        assert!(out.contains("shown") && !out.contains("secret"), "{out}");
    }

    #[test]
    fn fallback_content_is_not_published() {
        let out = md("<p>shown</p><noembed>secret</noembed><noframes>secret</noframes>");
        assert_eq!(out, "shown\n");
    }

    #[test]
    fn literal_tildes_are_escaped() {
        assert_eq!(md("<p>~~not deleted~~</p>"), "\\~\\~not deleted\\~\\~\n");
        assert_eq!(md("<p>a</p><p>~~~</p><p>b</p>"), "a\n\n\\~\\~\\~\n\nb\n");
        assert_eq!(md("<p><del>gone</del></p>"), "~~gone~~\n");
    }

    #[test]
    fn an_ordered_list_keeps_its_start_number() {
        assert_eq!(
            md("<ol start=\"5\"><li>Step five</li><li>Step six</li></ol>"),
            "5. Step five\n6. Step six\n"
        );
        // A browser shows -2, which no Markdown list number can.
        assert_eq!(md("<ol start=\"-2\"><li>a</li></ol>"), "- -2\\. a\n");
        assert_eq!(md("<ol><li>a</li></ol>"), "1. a\n");
    }

    #[test]
    fn a_reversed_list_counts_down() {
        assert_eq!(
            md("<ol reversed><li>Third</li><li>Second</li><li>First</li></ol>"),
            "3. Third\n2. Second\n1. First\n"
        );
        assert_eq!(
            md("<ol reversed start=\"10\"><li>a</li><li>b</li></ol>"),
            "10. a\n9. b\n"
        );
    }

    #[test]
    fn a_code_language_with_a_backtick_is_dropped() {
        assert_eq!(
            md("<pre><code class=\"language-```\">x</code></pre><p>after</p>"),
            "```\nx\n```\n\nafter\n"
        );
        assert_eq!(
            md("<pre><code class=\"language-c++\">x</code></pre>"),
            "```c++\nx\n```\n"
        );
    }

    #[test]
    fn a_list_item_value_renumbers() {
        assert_eq!(
            md("<ol start=\"5\"><li>Five</li><li value=\"10\">Ten</li><li>Eleven</li></ol>"),
            "5. Five\n10. Ten\n11. Eleven\n"
        );
    }

    #[test]
    fn content_inside_a_table_but_outside_its_cells_is_kept() {
        assert_eq!(md("<table>Visible</table>"), "Visible\n");
        assert_eq!(
            md("<table><tr>Visible<td>42</td></tr></table>"),
            "Visible\n\n| 42 |\n| --- |\n"
        );
        assert_eq!(
            md("<table>\n  <p>Note</p>\n  <tr><td>1</td></tr>\n</table>"),
            "Note\n\n| 1 |\n| --- |\n"
        );
    }

    #[test]
    fn a_table_keeps_its_caption() {
        assert_eq!(
            md("<table><caption>Quarterly totals</caption><tr><td>42</td></tr></table>"),
            "Quarterly totals\n\n| 42 |\n| --- |\n"
        );
        assert_eq!(
            md("<table><caption hidden>gone</caption><tr><td>1</td></tr></table>"),
            "| 1 |\n| --- |\n"
        );
    }

    #[test]
    fn a_heading_closes_an_open_heading() {
        assert_eq!(md("<main><h1>One<h2>Two</h2></main>"), "# One\n\n## Two\n");
    }

    #[test]
    fn every_named_character_reference_decodes() {
        assert_eq!(md("<p>Save &frac12; today</p>"), "Save \u{bd} today\n");
        assert_eq!(
            md("<p>&NotEqualTilde;&Aacute;&zwnj;x</p>"),
            "\u{2242}\u{338}\u{c1}\u{200c}x\n"
        );
        // Not a reference: the source text stays, escaped.
        assert_eq!(md("<p>&zzz;</p>"), "\\&zzz;\n");
    }

    #[test]
    fn inline_code_keeps_the_spaces_around_it() {
        assert_eq!(md("<p>A<code> B </code>C</p>"), "A `B` C\n");
    }

    #[test]
    fn references_decode_as_in_a_browser() {
        // A legacy name needs no `;` in text.
        assert_eq!(md("<p>&copy 2026 &notit;</p>"), "\u{a9} 2026 \u{ac}it;\n");
        // C1 numbers read as Windows-1252; a `;` is optional.
        assert_eq!(
            md("<p>&#128; &#x93;q&#x94; &#169 x</p>"),
            "\u{20ac} \u{201c}q\u{201d} \u{a9} x\n"
        );
        // In an attribute, a legacy name running into `=` stays text.
        assert_eq!(
            md("<p><a href=\"/s?a=1&copy=2\">s</a></p>"),
            "[s](/s?a=1&copy=2)\n"
        );
    }

    #[test]
    fn a_list_item_closes_through_a_div() {
        assert_eq!(md("<ul><li>one<div><li>two</ul>"), "- one\n- two\n");
    }

    #[test]
    fn a_backslash_in_a_link_is_encoded() {
        // In the path it is a `/`, as a browser reads it; after the query
        // starts it stays, encoded so it cannot escape the closing `)`.
        assert_eq!(
            md("<p><a href=\"/docs\\\">Docs</a></p>"),
            "[Docs](/docs/)\n"
        );
        assert_eq!(
            md("<p><a href=\"/docs?q=\\\">Docs</a></p>"),
            "[Docs](/docs?q=%5C)\n"
        );
    }

    #[test]
    fn a_menu_is_a_bullet_list() {
        assert_eq!(
            md("<menu><li>One</li><li>Two</li></menu>"),
            "- One\n- Two\n"
        );
    }

    #[test]
    fn preformatted_text_keeps_its_trailing_spaces() {
        assert_eq!(md("<pre>x </pre>"), "```\nx \n```\n");
        assert_eq!(md("<pre>\nx\n</pre>"), "```\nx\n```\n");
    }

    #[test]
    fn a_button_or_link_closes_an_open_one() {
        assert_eq!(
            md("<button>one<button>two</button><main>Visible</main>"),
            "Visible\n"
        );
        assert_eq!(
            md("<p><a href=\"/one\">one<a href=\"/two\">two</a>end</p>"),
            "[one](/one)[two](/two)end\n"
        );
        // A link in a table cell is not closed by one in the next cell.
        assert!(
            md("<table><tr><td><a href=\"/a\">a</a><td><a href=\"/b\">b</a></table>")
                .contains("[a](/a) | [b](/b)")
        );
    }

    #[test]
    fn comments_close_as_in_a_browser() {
        assert_eq!(md("<main><p>One</p><!--><p>Two</p></main>"), "One\n\nTwo\n");
        assert_eq!(
            md("<main><p>One</p><!---><p>Two</p></main>"),
            "One\n\nTwo\n"
        );
        assert_eq!(
            md("<main><p>One</p><!-- x --!><p>Two</p></main>"),
            "One\n\nTwo\n"
        );
        assert_eq!(
            md("<main><p>One</p><!-- x - -> y --><p>Two</p></main>"),
            "One\n\nTwo\n"
        );
    }

    #[test]
    fn a_bang_before_a_link_stays_text() {
        assert_eq!(md("<p>!<a href=\"/x\">x</a></p>"), "\\![x](/x)\n");
        assert_eq!(md("<p>Wow! <a href=\"/x\">x</a></p>"), "Wow! [x](/x)\n");
    }

    #[test]
    fn crlf_reads_as_lf() {
        assert_eq!(md("<pre>\r\nx\r\ny\r\n</pre>"), "```\nx\ny\n```\n");
        assert_eq!(md("<p>a\rb</p>"), "a b\n");
    }

    #[test]
    fn a_bang_after_an_escaped_backslash_is_escaped() {
        // The text `\` writes as `\\`, which leaves the `!` bare.
        assert_eq!(md("<p>\\!<a href=\"/x\">x</a></p>"), "\\\\\\![x](/x)\n");
    }

    #[test]
    fn front_matter_keeps_control_characters() {
        assert_eq!(
            md("<head><title>A&#8;B</title></head><body>x</body>"),
            "---\ntitle: \"A\\u0008B\"\n---\n\nx\n"
        );
    }

    #[test]
    fn a_long_unknown_scheme_is_refused() {
        assert_eq!(
            md("<a href=\"abcdefghijklmnopq:payload\">Open</a>"),
            "Open\n"
        );
        assert_eq!(md("<a href=\"/a/b:c\">Path</a>"), "[Path](/a/b:c)\n");
    }

    #[test]
    fn text_between_list_items_stays_in_place() {
        assert_eq!(md("<ul>Intro<li>One</li></ul>"), "Intro\n\n- One\n");
        assert_eq!(
            md("<ol>\n<li>a</li>\n<p>note</p>\n<li>b</li>\n</ol>"),
            "1. a\n\nnote\n\n2. b\n"
        );
        // Whitespace between items does not split the list.
        assert_eq!(md("<ul>\n<li>a</li>\n<li>b</li>\n</ul>"), "- a\n- b\n");
    }

    #[test]
    fn cells_outside_a_row_form_one() {
        assert_eq!(
            md("<table><td>A</td><td>B</td></table>"),
            "| A | B |\n| --- | --- |\n"
        );
        assert_eq!(
            md("<table><tbody><td>A<td>B<tr><td>C</td></tr></tbody></table>"),
            "| A | B |\n| --- | --- |\n| C |\n"
        );
    }

    #[test]
    fn content_outside_the_body_joins_it() {
        assert_eq!(md("<body>One</body>Two"), "OneTwo\n");
        assert_eq!(
            md("<html><head><title>T</title></head>Before<body><p>In</p></body></html>After"),
            "---\ntitle: \"T\"\n---\n\nBefore\n\nIn\n\nAfter\n"
        );
    }

    #[test]
    fn a_reference_in_a_url_stays_literal() {
        assert_eq!(
            md("<a href=\"/go?x=&amp;copy;&amp;y=1\">Go</a>"),
            "[Go](/go?x=\\&copy;&y=1)\n"
        );
        assert_eq!(
            md("<img src=\"/i?a=&amp;lt;\" alt=\"i\">"),
            "![i](/i?a=\\&lt;)\n"
        );
    }

    #[test]
    fn list_numbers_are_signed() {
        assert_eq!(
            md("<ol reversed start=\"1\"><li>A</li><li>B</li><li>C</li></ol>"),
            "- 1\\. A\n- 0\\. B\n- -1\\. C\n"
        );
        assert_eq!(
            md("<ol start=\"-2\"><li>A</li><li value=\"7\">B</li></ol>"),
            "- -2\\. A\n- 7\\. B\n"
        );
        assert_eq!(
            md("<ol reversed start=\"2\"><li>A</li><li>B</li></ol>"),
            "2. A\n1. B\n"
        );
        // No overflow at the top of the range.
        assert_eq!(
            md("<ol start=\"9223372036854775807\"><li>A</li><li>B</li></ol>"),
            "- 9223372036854775807\\. A\n- 9223372036854775807\\. B\n"
        );
    }

    #[test]
    fn an_empty_href_is_still_a_link() {
        assert_eq!(md("<a href=\"\">Reload</a>"), "[Reload]()\n");
        assert_eq!(md("<a href=\" \">Reload</a>"), "[Reload]()\n");
        assert_eq!(md("<p>a<a href=\"\"></a>b</p>"), "ab\n");
        assert_eq!(md("<a href=\"/tracking\"> </a><p>Body</p>"), "Body\n");
        assert_eq!(md("<img src=\"\" alt=\"x\">"), "");
    }

    #[test]
    fn links_resolve_against_the_base() {
        let page = |body: &str| {
            md(&format!(
                "<head><base href=\"https://cdn.example/assets/\"></head><body>{body}</body>"
            ))
        };
        assert_eq!(
            page("<a href=\"guide\">Guide</a>"),
            "[Guide](https://cdn.example/assets/guide)\n"
        );
        assert_eq!(
            page("<a href=\"/top\">Top</a> <img src=\"i.png\" alt=\"i\">"),
            "[Top](https://cdn.example/top) ![i](https://cdn.example/assets/i.png)\n"
        );
        assert_eq!(
            page("<a href=\"https://other.example/x\">X</a>"),
            "[X](https://other.example/x)\n"
        );
        assert_eq!(page("<a href=\"javascript:alert(1)\">J</a>"), "J\n");
        // A root-relative base needs only the origin; a path-relative one
        // needs the page path; one in a template is not used.
        assert_eq!(
            md("<base href=\"/sub/\"><a href=\"guide\">Guide</a>"),
            "[Guide](/sub/guide)\n"
        );
        assert_eq!(
            md("<base href=\"../assets/\"><a href=\"guide\">Guide</a>"),
            "[Guide](guide)\n"
        );
        assert_eq!(
            html_to_markdown_at(
                "<base href=\"../assets/\"><a href=\"guide?q=1#top\">Guide</a>",
                Some("/docs/page")
            ),
            "[Guide](/assets/guide?q=1#top)\n"
        );
        assert_eq!(
            md("<base href=\"//cdn.example/x/\"><a href=\"guide\">Guide</a>"),
            "[Guide](//cdn.example/x/guide)\n"
        );
        assert_eq!(
            md("<template><base href=\"https://x.example/\"></template><a href=\"g\">G</a>"),
            "[G](g)\n"
        );
    }

    #[test]
    fn ordered_list_types_keep_their_labels() {
        assert_eq!(
            md("<ol type=\"A\"><li>First</li><li>Second</li></ol>"),
            "- A\\. First\n- B\\. Second\n"
        );
        assert_eq!(
            md("<ol type=\"a\" start=\"26\"><li>z</li><li>aa</li></ol>"),
            "- z\\. z\n- aa\\. aa\n"
        );
        assert_eq!(
            md("<ol type=\"I\" start=\"3\"><li>c</li><li>d</li></ol>"),
            "- III\\. c\n- IV\\. d\n"
        );
        assert_eq!(
            md("<ol type=\"i\" start=\"0\"><li>z</li></ol>"),
            "- 0\\. z\n"
        );
        assert_eq!(md("<ol type=\"1\"><li>a</li></ol>"), "1. a\n");
        assert_eq!(list_label(1994, Some("i")), "mcmxciv");
    }

    #[test]
    fn spanned_cells_keep_their_columns() {
        assert_eq!(
            md("<table><tr><td rowspan=\"2\">X</td><td>Y</td></tr><tr><td>Z</td></tr></table>"),
            "| X | Y |\n| --- | --- |\n|  | Z |\n"
        );
        assert_eq!(
            md(
                "<table><tr><th colspan=\"2\">H</th><th>I</th></tr><tr><td>a</td><td>b</td><td>c</td></tr></table>"
            ),
            "| H |  | I |\n| --- | --- | --- |\n| a | b | c |\n"
        );
        // A span stops at the end of its row group.
        assert_eq!(
            md(
                "<table><tbody><tr><td rowspan=\"0\">X</td><td>Y</td></tr></tbody><tbody><tr><td>Z</td></tr></tbody></table>"
            ),
            "| X | Y |\n| --- | --- |\n| Z |\n"
        );
        // Placeholders are bounded by the table's own cells.
        let out = md(
            "<table><tr><td colspan=\"1000\" rowspan=\"0\">A</td></tr><tr><td>B</td></tr></table>",
        );
        // Two cells: 8 placeholders each and 64 spare, not 1000 columns.
        assert!(out.matches(" |").count() < 2 * (2 * 8 + 64 + 2), "{out}");
    }

    #[test]
    fn a_backslash_in_an_http_url_is_a_slash() {
        assert_eq!(
            md("<a href=\"https://example.com\\docs\">Docs</a>"),
            "[Docs](https://example.com/docs)\n"
        );
        assert_eq!(
            md("<a href=\"/docs\\guide?q=a\\b\">G</a>"),
            "[G](/docs/guide?q=a%5Cb)\n"
        );
        assert_eq!(
            md("<a href=\"mailto:a\\b@example.com\">M</a>"),
            "[M](mailto:a%5Cb@example.com)\n"
        );
    }

    #[test]
    fn an_image_with_only_srcset_keeps_its_first_candidate() {
        assert_eq!(
            md("<img srcset=\"/hero.jpg\" alt=\"Hero\">"),
            "![Hero](/hero.jpg)\n"
        );
        assert_eq!(
            md("<img src=\"\" srcset=\" /a.jpg 1x, /b.jpg 2x\" alt=\"A\">"),
            "![A](/a.jpg)\n"
        );
        assert_eq!(
            md("<img src=\"/s.jpg\" srcset=\"/a.jpg 1x\" alt=\"S\">"),
            "![S](/s.jpg)\n"
        );
        assert_eq!(md("<img srcset=\" , \" alt=\"x\">"), "");
    }

    #[test]
    fn a_plain_ampersand_in_a_relative_path_stays_a_link() {
        assert_eq!(
            md("<a href=\"R&amp;D/guide\">Guide</a>"),
            "[Guide](R&D/guide)\n"
        );
        // A reference that would spell a scheme once decoded is refused.
        assert_eq!(md("<a href=\"javascript&amp;colon;alert(1)\">J</a>"), "J\n");
        assert_eq!(md("<a href=\"javascript&amp;#58;alert(1)\">J</a>"), "J\n");
    }

    #[test]
    fn an_end_br_is_a_line_break() {
        assert_eq!(md("<p>One</br>Two</p>"), "One  \nTwo\n");
    }

    #[test]
    fn a_url_loses_its_tabs_and_newlines() {
        assert_eq!(
            md("<a href=\" https://exa&#10;mple.com/x&#9;y \">Link</a>"),
            "[Link](https://example.com/xy)\n"
        );
    }

    #[test]
    fn a_row_after_moved_out_content_stays_in_the_table() {
        assert_eq!(
            md("<table><p>Note<tr><td>1</td></tr></table>"),
            "Note\n\n| 1 |\n| --- |\n"
        );
    }

    #[test]
    fn code_blocks_skip_hidden_descendants() {
        assert_eq!(
            md("<pre><code><span hidden>secret</span>visible<script>x()</script></code></pre>"),
            "```\nvisible\n```\n"
        );
        assert_eq!(
            md("<p><code>a<span aria-hidden=\"true\">b</span>c</code></p>"),
            "`ac`\n"
        );
    }

    #[test]
    fn hidden_content_past_the_depth_limit_stays_hidden() {
        let deep = "<div>".repeat(300);
        for hidden in [
            "<div hidden>secret<img src=x alt=pic></div>",
            "<div aria-hidden=\"true\">secret</div>",
            "<nav>secret</nav>",
        ] {
            let out = md(&format!("{deep}{hidden}<p>ok</p>"));
            assert!(
                !out.contains("secret") && !out.contains("pic"),
                "{hidden}: {out}"
            );
            assert!(out.contains("ok"), "{hidden}: {out}");
        }
        // Plain content past the limit is still kept.
        assert!(md(&format!("{deep}<span>kept</span>")).contains("kept"));
    }

    #[test]
    fn raw_text_past_the_depth_limit_stays_dropped() {
        let html = "<div>".repeat(300) + "<script>var s='</div>'; secret()</script>ok";
        let out = md(&html);
        assert!(!out.contains("secret"), "{out}");
        assert!(out.contains("ok"), "{out}");
    }

    #[test]
    fn front_matter_skips_hidden_metadata() {
        let out = md("<template><title>secret</title>\
             <meta name=\"description\" content=\"hidden\"></template>\
             <svg><title>icon</title></svg><iframe><title>framed</title></iframe>\
             <object><title>embedded</title></object><title>real</title>\
             <meta name=\"description\" content=\"shown\"><p>x</p>");
        assert_eq!(
            out,
            "---\ntitle: \"real\"\ndescription: \"shown\"\n---\n\nx\n"
        );
    }

    #[test]
    fn a_self_closing_html_tag_stays_open() {
        let out = md("<template/><main>hidden</main></template><main>real</main>");
        assert_eq!(out, "real\n");
        // A foreign root does close itself.
        assert_eq!(md("<p>a<svg/>b</p>"), "ab\n");
    }

    #[test]
    fn a_self_closing_raw_text_tag_still_runs_to_its_end_tag() {
        let out = md("<script/>\"<main>injected</main>\"</script><main>real</main>");
        assert_eq!(out, "real\n");
    }

    #[test]
    fn raw_text_ends_only_at_its_own_tag_name() {
        let out = md("<script>\"</scripture><main>injected</main>\"</script>\
             <main>real</main>");
        assert_eq!(out, "real\n");
        let deep = "<div>".repeat(300) + "<script>'</scripts><p>secret</p>'</SCRIPT >ok";
        let out = md(&deep);
        assert!(!out.contains("secret"), "{out}");
        assert!(out.contains("ok"), "{out}");
    }

    #[test]
    fn fences_outgrow_backticks_in_code() {
        assert_eq!(md("<p><code>a``b</code></p>"), "```a``b```\n");
        assert_eq!(md("<pre>````x</pre>"), "`````\n````x\n`````\n");
    }

    #[test]
    fn unterminated_tags_parse_in_linear_time() {
        let started = std::time::Instant::now();
        let _ = md(&"<a b".repeat(200_000));
        let _ = md(&"<a b=\"".repeat(200_000));
        assert!(started.elapsed() < std::time::Duration::from_secs(5));
    }

    #[test]
    fn unmatched_end_tags_past_the_depth_limit_parse_in_linear_time() {
        // The growth rate, not a wall-clock budget: a debug build on a busy
        // runner is slow, but four times the input must take about four
        // times as long (a quadratic parse takes sixteen). Best of three
        // runs keeps a scheduler hiccup out of it.
        let time = |html: &str| {
            (0..3)
                .map(|_| {
                    let started = std::time::Instant::now();
                    let _ = md(html);
                    started.elapsed()
                })
                .min()
                .unwrap()
        };
        let cases: [fn(usize) -> String; 2] = [
            // Many dropped start tags, then many end tags that match none.
            |n| {
                format!(
                    "{}{}{}",
                    "<div>".repeat(300),
                    "<span>".repeat(n),
                    "</b>".repeat(n)
                )
            },
            // Each `<p>` asks for an implied close across every dropped span.
            |n| {
                format!(
                    "{}{}{}",
                    "<div>".repeat(300),
                    "<span>".repeat(n),
                    "<p></p>".repeat(n)
                )
            },
        ];
        for case in cases {
            let small = time(&case(5_000));
            let large = time(&case(20_000));
            assert!(large < small * 10, "{small:?} -> {large:?}");
        }
    }

    #[test]
    fn deep_nesting_does_not_overflow() {
        let html = format!(
            "{}deep{}",
            "<div>".repeat(100_000),
            "</div>".repeat(100_000)
        );
        assert_eq!(md(&html), "deep\n");
    }

    #[test]
    fn malformed_input_never_panics() {
        for html in [
            "<",
            "</",
            "<p",
            "<a href=",
            "<a href=\"x",
            "&#",
            "&#x;",
            "&#99999999999;",
            "</p></div>",
            "<!--",
            "<!DOCTYPE",
            "<pre>",
            "<table><td>x",
            "<li>orphan",
            "<<<>>>",
            "<p>\u{0}</p>",
            "<p>é</p><b>ü",
        ] {
            let _ = md(html);
        }
    }

    #[test]
    fn empty_document_is_empty() {
        assert_eq!(md(""), "");
        assert_eq!(md("<html><body></body></html>"), "");
    }

    #[test]
    fn token_estimate_rounds_up() {
        assert_eq!(estimate_tokens(""), 0);
        assert_eq!(estimate_tokens("abcde"), 2);
    }
}