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
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
use crate::entities::{Alignment, ListStyle, MarkerType, SemanticRole, TextDirection};
use crate::parser_tools::djot_options::DjotImportOptions;
use crate::parser_tools::list_grouper::default_list_suffix;

/// An inline image recovered by a parser, before it becomes an `ImageAnchor`.
///
/// `src` is whatever the source document pointed at — a relative path, a bare
/// name, a URL. Parsers do not resolve it; that is the embedding application's
/// job, and this crate never touches the filesystem.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedImage {
    pub src: String,
    pub alt: String,
    /// Display size in pixels, `0` when the source did not state one.
    ///
    /// Djot and HTML both carry these as attributes (`{width=800}`,
    /// `<img width=…>`); Markdown has no syntax for them, so a Markdown import
    /// leaves both zero and the caller supplies intrinsic dimensions.
    pub width: i64,
    pub height: i64,
}

/// A parsed inline span with formatting info.
///
/// A span carries *either* text or an image, never both: an image span's `text`
/// is empty and its description lives in [`ParsedImage::alt`], so alt text
/// cannot leak into the block's prose (and therefore cannot be counted as
/// manuscript words or matched by a search).
#[derive(Debug, Clone, Default)]
pub struct ParsedSpan {
    pub text: String,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub strikeout: bool,
    pub code: bool,
    /// Superscript (djot `^x^`). Maps to `CharVerticalAlignment::SuperScript`.
    pub superscript: bool,
    /// Subscript (djot `~x~`). Maps to `CharVerticalAlignment::SubScript`.
    pub subscript: bool,
    pub link_href: Option<String>,
    /// Set when this span is an inline image rather than text.
    pub image: Option<ParsedImage>,
    /// Set when this span is a footnote *reference* rather than text — the
    /// label naming the note, never the number a reader sees.
    pub footnote_ref: Option<String>,
    /// The text keeps its whitespace as it is: it came from an element styled
    /// `white-space: pre`, `pre-wrap` or `break-spaces`, where a tab or two spaces in a
    /// row are the text's own. An HTML reader collapses none of it.
    pub preserve_whitespace: bool,
}

/// A parsed table cell containing inline spans.
#[derive(Debug, Clone)]
pub struct ParsedTableCell {
    pub spans: Vec<ParsedSpan>,
}

/// A parsed table extracted from markdown or HTML.
#[derive(Debug, Clone)]
pub struct ParsedTable {
    /// Number of header rows (typically 1 for markdown tables).
    pub header_rows: usize,
    /// All rows (header + body), each containing cells with their inline spans.
    pub rows: Vec<Vec<ParsedTableCell>>,
    /// Blockquote nesting depth at the point the table appeared
    /// (0 = not inside a blockquote), mirroring `ParsedBlock::blockquote_depth`.
    pub blockquote_depth: u32,
    /// How many of those quotations open at the table, mirroring
    /// `ParsedBlock::blockquote_opens`.
    pub blockquote_opens: u32,
}

impl ParsedTable {
    /// How many columns the table needs to hold every cell it has: as many as its widest
    /// row has cells. A row shorter than that is completed with empty cells by whoever
    /// builds the table.
    ///
    /// The first row does not say it. The rows of a Djot table differ in length when their
    /// source does, and an HTML table's first row is often one heading cell spanning every
    /// column: a table sized from its first row kept the cells of a longer row in the model,
    /// where they showed, while every export writes a row up to the table's width only, so
    /// the first save dropped them.
    pub fn column_count(&self) -> usize {
        self.rows.iter().map(Vec::len).max().unwrap_or(0)
    }
}

/// How many cells a table may reach once its short rows are completed (see
/// [`ParsedTable::column_count`]), however few it has of its own: a table this size loads
/// in a moment.
const SQUARED_CELLS_ALWAYS: usize = 4096;

/// How many times the cells it has of its own a larger table may reach once its short
/// rows are completed.
///
/// A table whose rows differ in length because its source wrote them so, a wide heading
/// row over rows of one cell or a few, grows to about as many times its own cells as it has
/// columns: a dozen or so for a table a writer makes. The tables that made hundreds of
/// thousands of cells out of a few kilobytes grow hundreds of times. At four times, a table
/// of ten columns heading five hundred rows of one cell was read as paragraphs, its grid
/// lost for a table that loads in a few milliseconds.
const SQUARED_CELLS_PER_OWN_CELL: usize = 16;

/// Push `table`, which has `own_cells` cells of its own in its source, onto `elements`: as
/// a table, or as the paragraphs of its cells when completing its short rows would make it
/// many times that.
///
/// Every row of a loaded table is completed to the widest one. A few kilobytes of a table
/// whose first row is wide and whose other rows hold one cell each, or of HTML cells each
/// spanning a thousand columns, made hundreds of thousands of empty cells, and held a load
/// or a paste for seconds. Such a table is read as the paragraphs of its cells that hold
/// anything, in reading order, at the table's quotation depth: every word stays, the grid
/// goes.
fn push_table(elements: &mut Vec<ParsedElement>, table: ParsedTable, own_cells: usize) {
    let squared = table.rows.len().saturating_mul(table.column_count());
    let allowed = SQUARED_CELLS_ALWAYS.max(own_cells.saturating_mul(SQUARED_CELLS_PER_OWN_CELL));
    if squared <= allowed {
        elements.push(ParsedElement::Table(table));
        return;
    }
    elements.extend(
        ParsedElement::flatten_to_blocks(vec![ParsedElement::Table(table)])
            .into_iter()
            .filter(|block| spans_carry_content(&block.spans))
            .map(ParsedElement::Block),
    );
}

/// Record that a quotation opened at the first block or table of `elements` from `first`
/// on: the quotation that began where `elements` stood at `first` and closes now. Nothing
/// when the quotation holds none.
fn mark_quotation_opened(elements: &mut [ParsedElement], first: usize) {
    let Some(rest) = elements.get_mut(first..) else {
        return;
    };
    for element in rest {
        match element {
            ParsedElement::Block(block) => {
                block.blockquote_opens += 1;
                return;
            }
            ParsedElement::Table(table) => {
                table.blockquote_opens += 1;
                return;
            }
            ParsedElement::FootnoteDefinition { .. } => {}
        }
    }
}

/// The cells a table's rows hold, for [`push_table`].
fn cells_in_rows(rows: &[Vec<ParsedTableCell>]) -> usize {
    rows.iter().map(Vec::len).sum()
}

/// A parsed element: either a block or a table.
#[derive(Debug, Clone)]
pub enum ParsedElement {
    Block(ParsedBlock),
    Table(ParsedTable),
    /// A footnote definition: the label its references name, and the blocks
    /// making up its body.
    ///
    /// Separate from `Block` because a definition is not part of the flow it was
    /// written in — it belongs wherever the output format puts notes, which the
    /// importer expresses by giving it a detached frame of its own.
    FootnoteDefinition {
        label: String,
        blocks: Vec<ParsedBlock>,
    },
}

impl ParsedElement {
    /// Extract blocks, flattening tables into one block per cell.
    /// Use when table structure is not needed.
    pub fn flatten_to_blocks(elements: Vec<ParsedElement>) -> Vec<ParsedBlock> {
        let mut blocks = Vec::new();
        for elem in elements {
            match elem {
                ParsedElement::Block(b) => blocks.push(b),
                // A definition is not part of the flow it was written in — it
                // belongs wherever the format puts notes. Flattening it in
                // would splice a note's body into the prose at the point the
                // definition happened to be typed.
                ParsedElement::FootnoteDefinition { .. } => {}
                ParsedElement::Table(t) => {
                    for row in t.rows {
                        for cell in row {
                            blocks.push(ParsedBlock {
                                spans: cell.spans,
                                heading_level: None,
                                list_style: None,
                                list_indent: 0,
                                list_prefix: String::new(),
                                list_suffix: String::new(),
                                list_start: None,
                                marker: None,
                                is_code_block: false,
                                code_language: None,
                                blockquote_depth: t.blockquote_depth,
                                blockquote_opens: 0,
                                line_height: None,
                                non_breakable_lines: None,
                                page_break_before: None,
                                front_matter: None,
                                direction: None,
                                background_color: None,
                                alignment: None,
                                top_margin: None,
                                text_indent: None,
                                semantic_role: None,
                            });
                        }
                    }
                }
            }
        }
        if blocks.is_empty() {
            blocks.push(ParsedBlock {
                spans: vec![ParsedSpan {
                    text: String::new(),
                    ..Default::default()
                }],
                heading_level: None,
                list_style: None,
                list_indent: 0,
                list_prefix: String::new(),
                list_suffix: String::new(),
                list_start: None,
                marker: None,
                is_code_block: false,
                code_language: None,
                blockquote_depth: 0,
                blockquote_opens: 0,
                line_height: None,
                non_breakable_lines: None,
                page_break_before: None,
                front_matter: None,
                direction: None,
                background_color: None,
                alignment: None,
                top_margin: None,
                text_indent: None,
                semantic_role: None,
            });
        }
        blocks
    }
}

/// A parsed block (paragraph, heading, list item, code block)
///
/// `Default` is an unformatted, empty paragraph — the shape a caller wants when
/// it has prose and no structure to give it. Deriving it also means a field
/// added later reaches those callers as "absent" rather than as a compile error
/// they would fix by guessing a value.
#[derive(Debug, Clone, Default)]
pub struct ParsedBlock {
    pub spans: Vec<ParsedSpan>,
    pub heading_level: Option<i64>,
    pub list_style: Option<ListStyle>,
    pub list_indent: u32,
    /// Ordered-list delimiter prefix (e.g. `"("` for djot `(1)` lists; empty
    /// otherwise). Stored on the `List` entity for round-trip fidelity.
    pub list_prefix: String,
    /// Ordered-list delimiter suffix (`"."` for `1.`, `")"` for `1)`/`(1)`;
    /// empty for unordered lists). Markdown reads it from the source, Djot from
    /// its list style, and HTML gives an ordered list
    /// [`default_list_suffix`].
    pub list_suffix: String,
    /// The number the first item of the list this item belongs to wears, when it is not 1
    /// (Djot and Markdown `3.`, HTML `<ol start="3">`). Every item of a list carries it.
    /// Stored on the `List` entity.
    pub list_start: Option<i64>,
    /// Task-list checkbox marker (djot `- [ ]` / `- [x]`). Maps to
    /// `Block.fmt_marker`. `None` for non-task blocks.
    pub marker: Option<MarkerType>,
    pub is_code_block: bool,
    pub code_language: Option<String>,
    pub blockquote_depth: u32,
    /// How many of the quotations the block stands in open at it, the innermost ones:
    /// quotations as deep as the element's before it but not the same ones. `0` when the
    /// block goes on in the quotations the element before it stands in, as deep as it
    /// stands. Two quotations one after the other (Djot and Markdown `> a` and `> b` with a
    /// blank line between, HTML `<blockquote>` twice) were read as one, since the depth
    /// alone does not tell them apart.
    pub blockquote_opens: u32,
    pub line_height: Option<i64>,
    pub non_breakable_lines: Option<bool>,
    /// Start this block on a new page (djot `{page_break_before=true}`). Maps to
    /// `Block.fmt_page_break_before`. `None` when absent.
    pub page_break_before: Option<bool>,
    /// This block opens the document as front matter (djot `{front_matter=true}`). Maps to
    /// `Block.fmt_front_matter`. `None` when absent.
    pub front_matter: Option<bool>,
    pub direction: Option<TextDirection>,
    pub background_color: Option<String>,
    /// Paragraph alignment (djot `{alignment=left|right|center|justify}`). Maps
    /// to `Block.fmt_alignment`. `None` when no alignment attribute is present.
    pub alignment: Option<Alignment>,
    /// This block's own space-above (djot `{top_margin=<int>}`). Maps to
    /// `Block.fmt_top_margin` and overrides the document-wide paragraph
    /// spacing for this block alone. `None` when absent.
    pub top_margin: Option<i64>,
    /// This block's own first-line indent (djot `{text_indent=<int>}`). Maps to
    /// `Block.fmt_text_indent` and overrides the document-wide first-line
    /// indent for this block alone. `None` when absent.
    pub text_indent: Option<i64>,
    /// The enclosing blockquote's semantic role (djot `{semantic_role=epigraph}`),
    /// written on the quote's first block because block attributes are the only channel
    /// djot offers. The importer lifts it onto the `Frame`, where it belongs.
    pub semantic_role: Option<SemanticRole>,
}

impl ParsedBlock {
    /// Returns `true` when this block carries no block-level formatting,
    /// meaning its content is purely inline.
    pub fn is_inline_only(&self) -> bool {
        self.heading_level.is_none()
            && self.list_style.is_none()
            && !self.is_code_block
            && self.blockquote_depth == 0
            && self.line_height.is_none()
            && self.non_breakable_lines.is_none()
            && self.page_break_before.is_none()
            && self.front_matter.is_none()
            && self.direction.is_none()
            && self.background_color.is_none()
            && self.alignment.is_none()
            && self.top_margin.is_none()
            && self.text_indent.is_none()
    }
}

// ─── Markdown parsing ────────────────────────────────────────────────

/// Labels referenced as `[^label]` in `markdown` with no `[^label]: ...`
/// definition anywhere in the document.
///
/// pulldown-cmark's default footnote mode (`Options::ENABLE_FOOTNOTES`, i.e.
/// GitHub's syntax) only emits `Event::FootnoteReference` for a label some
/// `Tag::FootnoteDefinition` actually defines (verified against 0.13) — an
/// undefined reference silently decomposes into three ordinary `Text` events
/// (`"["`, `"^label"`, `"]"`) instead, indistinguishable from a reader having
/// typed literal brackets. `Options::ENABLE_OLD_FOOTNOTES` recognises it, but
/// as `parse_markdown`'s doc comment explains, trades away multi-paragraph
/// definition bodies to get there. Neither flag alone gives both, so
/// `parse_markdown` hands pulldown-cmark a synthetic empty definition for
/// every such label instead — real enough for it to recognise the reference,
/// thrown away again before `elements` is returned.
///
/// Finding "every such label" is two parts: which labels pulldown-cmark's own
/// block scanner (a first pass, at the caller's `options`) already recognises
/// as truly defined, and which `[^…]`-shaped spans exist in the raw text at
/// all. The second part is a plain scan, not a parser — it can over-match
/// (inside a code span, inside a fenced block, a coincidental `[^x]:` in the
/// middle of a sentence that real block parsing would never treat as a
/// definition), but over-matching only ever produces an unused synthetic
/// definition: pulldown-cmark's real, correct parse of the augmented text is
/// what decides whether any span actually becomes a reference, and
/// `parse_markdown` drops every synthesized definition regardless of whether
/// that happened.
fn dangling_footnote_labels(
    markdown: &str,
    options: pulldown_cmark::Options,
) -> std::collections::BTreeSet<String> {
    use pulldown_cmark::{Event, Parser, Tag};

    let mut defined: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    for event in Parser::new_ext(markdown, options) {
        if let Event::Start(Tag::FootnoteDefinition(label)) = event {
            defined.insert(label.to_string());
        }
    }

    let mut referenced: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    let bytes = markdown.as_bytes();
    let mut search_from = 0usize;
    while let Some(rel) = markdown[search_from..].find("[^") {
        let open = search_from + rel;
        let label_start = open + 2;
        let Some(close_rel) = markdown[label_start..].find(']') else {
            break;
        };
        let close = label_start + close_rel;
        let label = &markdown[label_start..close];
        // `]:` immediately after is definition syntax, not a reference —
        // pulldown-cmark's own scan above already supplied the ground truth
        // for which labels are truly defined; this only needs to avoid
        // counting a definition's own label as a "reference" candidate.
        let looks_like_definition = bytes.get(close + 1) == Some(&b':');
        if !label.is_empty() && !looks_like_definition && !label.chars().any(char::is_whitespace) {
            referenced.insert(label.to_string());
        }
        search_from = close + 1;
    }

    referenced.difference(&defined).cloned().collect()
}

/// The delimiter the ordered list that opens at byte `start` of `source` wears: `")"` for
/// `1)`, `"."` for `1.`.
///
/// pulldown-cmark reports a list as `Tag::List(Some(first))` and says nothing of the
/// delimiter, but the range of that event starts at the first item's marker (or at the
/// indentation before it, or after the `> ` of a quotation it stands in), never inside the
/// item's content. So the line is read from `start`: blanks and `>` are skipped, then the
/// digits, and the character after them is the delimiter. Anything else reads as `"."`,
/// the delimiter CommonMark gives an ordered list by default.
fn markdown_ordered_suffix(source: &str, start: usize) -> &'static str {
    let Some(rest) = source.get(start..) else {
        return ".";
    };
    let line = rest.split('\n').next().unwrap_or("");
    let after_blanks = line.trim_start_matches([' ', '\t', '>']);
    let after_digits = after_blanks.trim_start_matches(|c: char| c.is_ascii_digit());
    if after_digits.starts_with(')') {
        ")"
    } else {
        "."
    }
}

/// Read `markdown` into blocks and tables.
///
/// The parser reads nested containers without recursing, whatever their depth. The
/// quotations the text holds are kept within
/// [`MAX_QUOTE_LEVELS`](crate::parser_tools::quote_depth::MAX_QUOTE_LEVELS) (see
/// `quote_depth`): the writers walk one call deeper for each, and a text nested 500
/// quotations deep loaded, then aborted the process at its first save.
pub fn parse_markdown(markdown: &str) -> Vec<ParsedElement> {
    use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};

    // ENABLE_FOOTNOTES is required for pulldown-cmark to parse `[^label]`
    // references and `[^label]: body` definitions at all — without it both
    // fall through as plain link-reference-shaped text. Deliberately NOT
    // `ENABLE_OLD_FOOTNOTES` too: that variant makes a reference with no
    // definition survive (see `dangling_footnote_labels`), but in exchange
    // breaks multi-paragraph definition bodies — a blank line before an
    // indented continuation escapes the definition and becomes a sibling
    // code block, which is exactly the shape this crate's own Markdown
    // exporter writes for a note with more than one paragraph. Getting both
    // is `dangling_footnote_labels`'s job, not an `Options` flag's.
    let options = Options::ENABLE_STRIKETHROUGH
        | Options::ENABLE_TABLES
        | Options::ENABLE_TASKLISTS
        | Options::ENABLE_FOOTNOTES;

    // See `dangling_footnote_labels`: pulldown-cmark's GFM-style footnotes
    // only recognise `[^label]` as a reference when some definition exists
    // for it anywhere in the document, so an undefined one — the normal
    // state for a host that owns note bodies itself — has to be given a
    // throwaway definition before parsing, or it silently becomes the
    // literal text "[^label]" instead of surviving as a reference.
    let dangling = dangling_footnote_labels(markdown, options);
    let augmented_owner;
    let source: &str = if dangling.is_empty() {
        markdown
    } else {
        augmented_owner = dangling
            .iter()
            .fold(markdown.to_string(), |mut acc, label| {
                acc.push_str("\n\n[^");
                acc.push_str(label);
                acc.push_str("]:\n");
                acc
            });
        &augmented_owner
    };
    let parser = Parser::new_ext(source, options).into_offset_iter();

    let mut elements: Vec<ParsedElement> = Vec::new();
    let mut current_spans: Vec<ParsedSpan> = Vec::new();
    let mut current_heading: Option<i64> = None;
    let mut current_list_style: Option<ListStyle> = None;
    let mut current_list_start: Option<i64> = None;
    let mut current_list_suffix = String::new();
    // The task-list checkbox of the item being read, which only its first block wears.
    let mut current_marker: Option<MarkerType> = None;
    let mut is_code_block = false;
    let mut code_language: Option<String> = None;
    let mut blockquote_depth: u32 = 0;
    // Where `elements` stood as each open quotation began (see `mark_quotation_opened`).
    let mut quotations_open: Vec<usize> = Vec::new();
    let mut in_block = false;

    // Formatting state stack
    let mut bold = false;
    let mut italic = false;
    let mut strikeout = false;
    let mut link_href: Option<String> = None;
    // Set between an image's Start and End; its alt text arrives as Text events.
    let mut pending_image: Option<ParsedImage> = None;

    // The label and element index of the footnote definition currently open,
    // if any. Mirrors `parse_djot`'s `footnote_open`: a definition's body is
    // ordinary block content, parsed by the same machinery as everything
    // else, then lifted back out at `End` into its own top-level element so
    // it never becomes part of the flow it was written in. CommonMark
    // footnote definitions cannot nest, so one slot suffices.
    let mut footnote_open: Option<(String, usize)> = None;

    // List style stack for nested lists (also tracks nesting depth), and the start of each
    // of those lists beside it.
    let mut list_stack: Vec<Option<ListStyle>> = Vec::new();
    let mut list_start_stack: Vec<Option<i64>> = Vec::new();
    // The delimiter each of those lists wears (`"."` or `")"`; empty for a bullet list).
    let mut list_suffix_stack: Vec<String> = Vec::new();
    let mut current_list_indent: u32 = 0;

    // Table tracking state
    let mut in_table = false;
    let mut in_table_head = false;
    let mut table_rows: Vec<Vec<ParsedTableCell>> = Vec::new();
    let mut current_row_cells: Vec<ParsedTableCell> = Vec::new();
    let mut current_cell_spans: Vec<ParsedSpan> = Vec::new();
    let mut table_header_rows: usize = 0;

    for (event, range) in parser {
        match event {
            Event::Start(Tag::Paragraph) => {
                in_block = true;
                current_heading = None;
                is_code_block = false;
            }
            Event::End(TagEnd::Paragraph) => {
                if !current_spans.is_empty() || in_block {
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: std::mem::take(&mut current_spans),
                        heading_level: current_heading.take(),
                        list_style: current_list_style.clone(),
                        list_indent: current_list_indent,
                        list_prefix: String::new(),
                        list_suffix: current_list_suffix.clone(),
                        list_start: current_list_start,
                        marker: current_marker.clone(),
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                }
                in_block = false;
                current_list_style = None;
                current_list_start = None;
                current_list_suffix.clear();
                current_marker = None;
            }
            Event::Start(Tag::Heading { level, .. }) => {
                in_block = true;
                current_heading = Some(heading_level_to_i64(level));
                is_code_block = false;
            }
            Event::End(TagEnd::Heading(_)) => {
                elements.push(ParsedElement::Block(ParsedBlock {
                    spans: std::mem::take(&mut current_spans),
                    heading_level: current_heading.take(),
                    list_style: None,
                    list_indent: 0,
                    list_prefix: String::new(),
                    list_suffix: String::new(),
                    list_start: None,
                    marker: None,
                    is_code_block: false,
                    code_language: None,
                    blockquote_depth,
                    blockquote_opens: 0,
                    line_height: None,
                    non_breakable_lines: None,
                    page_break_before: None,
                    front_matter: None,
                    direction: None,
                    background_color: None,
                    alignment: None,
                    top_margin: None,
                    text_indent: None,
                    semantic_role: None,
                }));
                in_block = false;
            }
            Event::Start(Tag::List(ordered)) => {
                let style = if ordered.is_some() {
                    Some(ListStyle::Decimal)
                } else {
                    Some(ListStyle::Disc)
                };
                list_stack.push(style);
                list_start_stack.push(ordered.and_then(list_start_of));
                list_suffix_stack.push(if ordered.is_some() {
                    markdown_ordered_suffix(source, range.start).to_string()
                } else {
                    String::new()
                });
            }
            Event::TaskListMarker(checked) => {
                current_marker = Some(if checked {
                    MarkerType::Checked
                } else {
                    MarkerType::Unchecked
                });
            }
            Event::End(TagEnd::List(_)) => {
                list_stack.pop();
                list_start_stack.pop();
                list_suffix_stack.pop();
            }
            Event::Start(Tag::Item) => {
                // Flush any accumulated spans from the parent item before
                // starting a child item in a tight list
                if !current_spans.is_empty() {
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: std::mem::take(&mut current_spans),
                        heading_level: None,
                        list_style: current_list_style.clone(),
                        list_indent: current_list_indent,
                        list_prefix: String::new(),
                        list_suffix: current_list_suffix.clone(),
                        list_start: current_list_start,
                        marker: current_marker.clone(),
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                }
                in_block = true;
                current_list_style = list_stack.last().cloned().flatten();
                current_list_start = list_start_stack.last().copied().flatten();
                current_list_suffix = list_suffix_stack.last().cloned().unwrap_or_default();
                // Set after the flush above, which wrote the parent item with its own box.
                current_marker = None;
                current_list_indent = if list_stack.is_empty() {
                    0
                } else {
                    (list_stack.len() - 1) as u32
                };
            }
            Event::End(TagEnd::Item) => {
                // The paragraph inside the item will have already been flushed,
                // but if there was no inner paragraph (tight list), flush now.
                if !current_spans.is_empty() {
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: std::mem::take(&mut current_spans),
                        heading_level: None,
                        list_style: current_list_style.clone(),
                        list_indent: current_list_indent,
                        list_prefix: String::new(),
                        list_suffix: current_list_suffix.clone(),
                        list_start: current_list_start,
                        marker: current_marker.clone(),
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                }
                in_block = false;
                current_list_style = None;
                current_list_start = None;
                current_list_suffix.clear();
                current_marker = None;
            }
            Event::Start(Tag::CodeBlock(kind)) => {
                in_block = true;
                is_code_block = true;
                code_language = match &kind {
                    pulldown_cmark::CodeBlockKind::Fenced(lang) if !lang.is_empty() => {
                        Some(lang.to_string())
                    }
                    _ => None,
                };
            }
            Event::End(TagEnd::CodeBlock) => {
                // pulldown-cmark appends a trailing '\n' to code block text — strip it
                if let Some(last) = current_spans.last_mut()
                    && last.text.ends_with('\n')
                {
                    last.text.truncate(last.text.len() - 1);
                }
                elements.push(ParsedElement::Block(ParsedBlock {
                    spans: std::mem::take(&mut current_spans),
                    heading_level: None,
                    list_style: None,
                    list_indent: 0,
                    list_prefix: String::new(),
                    list_suffix: String::new(),
                    list_start: None,
                    marker: None,
                    is_code_block: true,
                    code_language: code_language.take(),
                    blockquote_depth,
                    blockquote_opens: 0,
                    line_height: None,
                    non_breakable_lines: None,
                    page_break_before: None,
                    front_matter: None,
                    direction: None,
                    background_color: None,
                    alignment: None,
                    top_margin: None,
                    text_indent: None,
                    semantic_role: None,
                }));
                in_block = false;
                is_code_block = false;
            }
            // ─── Table events ───────────────────────────────────────
            Event::Start(Tag::Table(_)) => {
                in_table = true;
                in_table_head = false;
                table_rows.clear();
                current_row_cells.clear();
                current_cell_spans.clear();
                table_header_rows = 0;
            }
            Event::End(TagEnd::Table) => {
                let own_cells = cells_in_rows(&table_rows);
                push_table(
                    &mut elements,
                    ParsedTable {
                        header_rows: table_header_rows,
                        rows: std::mem::take(&mut table_rows),
                        blockquote_depth,
                        blockquote_opens: 0,
                    },
                    own_cells,
                );
                in_table = false;
            }
            Event::Start(Tag::TableHead) => {
                in_table_head = true;
                current_row_cells.clear();
            }
            Event::End(TagEnd::TableHead) => {
                // Flush the header row
                table_rows.push(std::mem::take(&mut current_row_cells));
                table_header_rows += 1;
                in_table_head = false;
            }
            Event::Start(Tag::TableRow) => {
                current_row_cells.clear();
            }
            Event::End(TagEnd::TableRow) if !in_table_head => {
                // Body rows only — header row is flushed in End(TableHead)
                table_rows.push(std::mem::take(&mut current_row_cells));
            }
            Event::Start(Tag::TableCell) => {
                current_cell_spans.clear();
            }
            Event::End(TagEnd::TableCell) => {
                current_row_cells.push(ParsedTableCell {
                    spans: std::mem::take(&mut current_cell_spans),
                });
            }
            // ─── Inline formatting ──────────────────────────────────
            Event::Start(Tag::Emphasis) => {
                italic = true;
            }
            Event::End(TagEnd::Emphasis) => {
                italic = false;
            }
            Event::Start(Tag::Strong) => {
                bold = true;
            }
            Event::End(TagEnd::Strong) => {
                bold = false;
            }
            Event::Start(Tag::Strikethrough) => {
                strikeout = true;
            }
            Event::End(TagEnd::Strikethrough) => {
                strikeout = false;
            }
            Event::Start(Tag::Link { dest_url, .. }) => {
                link_href = Some(dest_url.to_string());
            }
            Event::End(TagEnd::Link) => {
                link_href = None;
            }
            // Markdown has no syntax for display size, so width/height stay 0
            // and the caller supplies the image's intrinsic dimensions.
            Event::Start(Tag::Image { dest_url, .. }) => {
                pending_image = Some(ParsedImage {
                    src: dest_url.to_string(),
                    alt: String::new(),
                    width: 0,
                    height: 0,
                });
            }
            Event::End(TagEnd::Image) => {
                if let Some(image) = pending_image.take() {
                    let span = ParsedSpan {
                        text: String::new(),
                        bold,
                        italic,
                        underline: false,
                        strikeout,
                        code: false,
                        superscript: false,
                        subscript: false,
                        link_href: link_href.clone(),
                        image: Some(image),
                        footnote_ref: None,
                        preserve_whitespace: false,
                    };
                    if in_table {
                        current_cell_spans.push(span);
                    } else {
                        if !in_block {
                            in_block = true;
                        }
                        current_spans.push(span);
                    }
                }
            }
            Event::Text(text) => {
                // Inside an image this is its alt text, which pulldown-cmark
                // emits as an ordinary Text event. Without this guard it fell
                // through and landed in the paragraph as prose — the image was
                // dropped and its description silently became manuscript text.
                if let Some(img) = pending_image.as_mut() {
                    img.alt.push_str(&text);
                    continue;
                }
                let span = ParsedSpan {
                    text: text.to_string(),
                    bold,
                    italic,
                    underline: false,
                    strikeout,
                    code: is_code_block,
                    superscript: false,
                    subscript: false,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: None,
                    preserve_whitespace: false,
                };
                if in_table {
                    current_cell_spans.push(span);
                } else {
                    if !in_block {
                        in_block = true;
                    }
                    current_spans.push(span);
                }
            }
            Event::Code(text) => {
                let span = ParsedSpan {
                    text: text.to_string(),
                    bold,
                    italic,
                    underline: false,
                    strikeout,
                    code: true,
                    superscript: false,
                    subscript: false,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: None,
                    preserve_whitespace: false,
                };
                if in_table {
                    current_cell_spans.push(span);
                } else {
                    if !in_block {
                        in_block = true;
                    }
                    current_spans.push(span);
                }
            }
            Event::SoftBreak => {
                let span = ParsedSpan {
                    text: " ".to_string(),
                    bold,
                    italic,
                    underline: false,
                    strikeout,
                    code: false,
                    superscript: false,
                    subscript: false,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: None,
                    preserve_whitespace: false,
                };
                if in_table {
                    current_cell_spans.push(span);
                } else {
                    current_spans.push(span);
                }
            }
            // A hard break (two spaces or a backslash at the end of the line) is a line
            // of the same block, as in the Djot reader. A table cell is one line, and so
            // is an image's description.
            Event::HardBreak if pending_image.is_some() => {
                if let Some(img) = pending_image.as_mut() {
                    img.alt.push(' ');
                }
            }
            Event::HardBreak => {
                let span = ParsedSpan {
                    text: if in_table { " " } else { LINE_SEPARATOR_STR }.to_string(),
                    bold,
                    italic,
                    underline: false,
                    strikeout,
                    code: false,
                    superscript: false,
                    subscript: false,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: None,
                    preserve_whitespace: false,
                };
                if in_table {
                    current_cell_spans.push(span);
                } else {
                    current_spans.push(span);
                }
            }
            Event::Start(Tag::BlockQuote(_)) => {
                blockquote_depth += 1;
                quotations_open.push(elements.len());
            }
            Event::End(TagEnd::BlockQuote(_)) => {
                blockquote_depth = blockquote_depth.saturating_sub(1);
                if let Some(first) = quotations_open.pop() {
                    mark_quotation_opened(&mut elements, first);
                }
            }
            // ── Footnote definitions ──
            //
            // `[^label]: body` opens a block-level container; its body is
            // ordinary paragraphs/lists/etc., pushed onto `elements` by the
            // ordinary machinery above. `End` drains everything pushed since
            // `Start` back out into one `FootnoteDefinition`, exactly as
            // `parse_djot` does for the same `[^label]: body` syntax.
            // Flushing any spans left open by an interrupted block first
            // matches every other container boundary in this parser
            // (List/Item/BlockQuote).
            Event::Start(Tag::FootnoteDefinition(label)) => {
                if !current_spans.is_empty() {
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: std::mem::take(&mut current_spans),
                        heading_level: current_heading.take(),
                        list_style: current_list_style.clone(),
                        list_indent: current_list_indent,
                        list_prefix: String::new(),
                        list_suffix: current_list_suffix.clone(),
                        list_start: current_list_start,
                        marker: current_marker.clone(),
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                }
                footnote_open = Some((label.to_string(), elements.len()));
            }
            Event::End(TagEnd::FootnoteDefinition) => {
                if !current_spans.is_empty() {
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: std::mem::take(&mut current_spans),
                        heading_level: current_heading.take(),
                        list_style: current_list_style.clone(),
                        list_indent: current_list_indent,
                        list_prefix: String::new(),
                        list_suffix: current_list_suffix.clone(),
                        list_start: current_list_start,
                        marker: current_marker.clone(),
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                }
                if let Some((label, start)) = footnote_open.take() {
                    let blocks: Vec<ParsedBlock> = elements
                        .drain(start..)
                        .filter_map(|e| match e {
                            ParsedElement::Block(b) => Some(b),
                            // A table inside a footnote is not representable
                            // as note content, same limitation as djot.
                            _ => None,
                        })
                        .collect();
                    elements.push(ParsedElement::FootnoteDefinition { label, blocks });
                }
            }
            // A footnote reference. pulldown-cmark emits this whether or not
            // a matching `[^label]:` definition exists anywhere in the
            // document — a dangling reference (the normal state for a host
            // that owns note bodies itself) must survive just the same,
            // mirroring `parse_djot`'s `E::FootnoteReference` handling.
            Event::FootnoteReference(label) => {
                let span = ParsedSpan {
                    text: String::new(),
                    bold,
                    italic,
                    underline: false,
                    strikeout,
                    code: false,
                    superscript: false,
                    subscript: false,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: Some(label.to_string()),
                    preserve_whitespace: false,
                };
                if in_table {
                    current_cell_spans.push(span);
                } else {
                    if !in_block {
                        in_block = true;
                    }
                    current_spans.push(span);
                }
            }
            _ => {}
        }
    }

    // Flush any remaining content
    if !current_spans.is_empty() {
        elements.push(ParsedElement::Block(ParsedBlock {
            spans: std::mem::take(&mut current_spans),
            heading_level: current_heading,
            list_style: current_list_style,
            list_indent: current_list_indent,
            list_prefix: String::new(),
            list_suffix: current_list_suffix.clone(),
            list_start: current_list_start,
            marker: current_marker.clone(),
            is_code_block,
            code_language: code_language.take(),
            blockquote_depth,
            blockquote_opens: 0,
            line_height: None,
            non_breakable_lines: None,
            page_break_before: None,
            front_matter: None,
            direction: None,
            background_color: None,
            alignment: None,
            top_margin: None,
            text_indent: None,
            semantic_role: None,
        }));
    }

    // Drop the throwaway definitions `dangling_footnote_labels` asked for —
    // they exist only to make pulldown-cmark recognise the reference as
    // real, never to become a note the document owns. `elements` cannot
    // become empty from this: a label reached `dangling` only via a
    // reference actually present in `markdown`, so its surrounding block
    // survives even with the synthetic definition gone.
    if !dangling.is_empty() {
        elements.retain(
            |e| !matches!(e, ParsedElement::FootnoteDefinition { label, .. } if dangling.contains(label)),
        );
    }

    // If no elements were parsed, create a single empty paragraph
    if elements.is_empty() {
        elements.push(ParsedElement::Block(ParsedBlock {
            spans: vec![ParsedSpan {
                text: String::new(),
                ..Default::default()
            }],
            heading_level: None,
            list_style: None,
            list_indent: 0,
            list_prefix: String::new(),
            list_suffix: String::new(),
            list_start: None,
            marker: None,
            is_code_block: false,
            code_language: None,
            blockquote_depth: 0,
            blockquote_opens: 0,
            line_height: None,
            non_breakable_lines: None,
            page_break_before: None,
            front_matter: None,
            direction: None,
            background_color: None,
            alignment: None,
            top_margin: None,
            text_indent: None,
            semantic_role: None,
        }));
    }

    // Quotations past the ceiling a document holds are flattened into the deepest kept
    // (see `quote_depth`): the writers walk one call deeper for each.
    crate::parser_tools::quote_depth::clamp_quote_depths(&mut elements);
    elements
}

fn heading_level_to_i64(level: pulldown_cmark::HeadingLevel) -> i64 {
    use pulldown_cmark::HeadingLevel;
    match level {
        HeadingLevel::H1 => 1,
        HeadingLevel::H2 => 2,
        HeadingLevel::H3 => 3,
        HeadingLevel::H4 => 4,
        HeadingLevel::H5 => 5,
        HeadingLevel::H6 => 6,
    }
}

// ─── HTML parsing ────────────────────────────────────────────────────

use scraper::Node;

/// Parsed CSS block-level styles from an inline `style` attribute.
#[derive(Debug, Clone, Default)]
struct BlockStyles {
    line_height: Option<i64>,
    non_breakable_lines: Option<bool>,
    page_break_before: Option<bool>,
    direction: Option<TextDirection>,
    background_color: Option<String>,
    /// `white-space` asks for the source's own spacing to be kept verbatim.
    /// Separate from `non_breakable_lines` because the two do not coincide:
    /// `nowrap` forbids wrapping but still collapses runs of spaces, so a
    /// block carrying it must be normalised like any other.
    preserve_whitespace: Option<bool>,
}

/// Parse relevant CSS properties from an inline style string.
/// Handles: line-height, white-space, break-before/page-break-before, direction,
/// background-color.
fn parse_block_styles(style: &str) -> BlockStyles {
    let mut result = BlockStyles::default();
    for part in style.split(';') {
        let part = part.trim();
        if let Some((prop, val)) = part.split_once(':') {
            let prop = prop.trim().to_ascii_lowercase();
            let val = val.trim();
            match prop.as_str() {
                "line-height" => {
                    // Try parsing as a plain number (multiplier)
                    if let Ok(v) = val.parse::<f64>() {
                        result.line_height = Some((v * 1000.0) as i64);
                    }
                }
                "white-space" if val == "pre" || val == "nowrap" || val == "pre-wrap" => {
                    result.non_breakable_lines = Some(true);
                    if val != "nowrap" {
                        result.preserve_whitespace = Some(true);
                    }
                }
                // CSS3 `break-before` and its CSS2 predecessor `page-break-before` mean
                // the same thing; both are read because both are written (browsers still
                // want the legacy spelling, and so do most EPUB engines).
                "break-before" | "page-break-before" => {
                    result.page_break_before = match val.to_ascii_lowercase().as_str() {
                        "page" | "always" | "left" | "right" | "recto" | "verso" => Some(true),
                        "avoid" | "auto" => Some(false),
                        _ => None,
                    };
                }
                "direction" => {
                    if val.eq_ignore_ascii_case("rtl") {
                        result.direction = Some(TextDirection::RightToLeft);
                    } else if val.eq_ignore_ascii_case("ltr") {
                        result.direction = Some(TextDirection::LeftToRight);
                    }
                }
                "background-color" | "background" => {
                    result.background_color = Some(val.to_string());
                }
                _ => {}
            }
        }
    }
    result
}

/// What an inline `style` says of the whitespace in an element's text: `Some(true)` when
/// `white-space` keeps it as it is (`pre`, `pre-wrap`, `break-spaces`), `Some(false)` when
/// it collapses it (`normal`, `nowrap`, `pre-line`), `None` when the style does not say.
/// The last declaration wins, as in CSS, unless an earlier one is `!important` and it is
/// not.
fn style_keeps_whitespace(style: &str) -> Option<bool> {
    let mut kept = None;
    let mut important_kept = false;
    for part in style.split(';') {
        let Some((property, value)) = part.split_once(':') else {
            continue;
        };
        if !property.trim().eq_ignore_ascii_case("white-space") {
            continue;
        }
        let value = value.trim().to_ascii_lowercase();
        let (value, important) = value
            .strip_suffix("important")
            .and_then(|rest| rest.trim_end().strip_suffix('!'))
            .map_or((value.as_str(), false), |rest| (rest.trim_end(), true));
        let says = match value {
            "pre" | "pre-wrap" | "break-spaces" => true,
            "normal" | "nowrap" | "pre-line" => false,
            _ => continue,
        };
        if important || !important_kept {
            kept = Some(says);
            important_kept |= important;
        }
    }
    kept
}

/// The `white-space` declarations of a document's `<style>` blocks that this reader honours:
/// the rules whose every selector is `*` or a bare tag name (`p, li { white-space: pre-wrap; }`
/// is what `QTextDocument::toHtml` writes). Not a CSS engine: a class, an id, an attribute, a
/// pseudo-class, a combinator, and everything inside an at-rule, is skipped.
///
/// Resolved once, when the sheet is read, into the one declaration that wins for each tag
/// the sheet names and the one that wins for `*`: a lookup for an element then builds nothing.
/// The declaration is put in front of the element's inline `style` attribute, so the one
/// precedence function, [`style_keeps_whitespace`], decides between the sheet and an element's
/// own `style` as it does between two declarations of one attribute.
#[derive(Debug, Default)]
struct SheetWhitespace {
    /// What every element gets (`*` rules): the winning keyword and whether it is `!important`.
    universal: Option<(&'static str, bool)>,
    /// What a tag the sheet names gets: the winner among the `*` rules and then the rules
    /// naming it, each in source order, so that a rule naming the tag beats `*` as its
    /// specificity says.
    per_tag: std::collections::HashMap<String, (&'static str, bool)>,
}

/// One rule of a sheet: the tags it names (`*` for every element) and its `white-space`
/// declarations as `(keyword, important)`.
struct WhitespaceRule {
    selectors: Vec<String>,
    declarations: Vec<(&'static str, bool)>,
}

/// The declaration that wins when `next` follows `current` in the cascade: the later one,
/// unless `current` is `!important` and `next` is not.
fn cascade(
    current: Option<(&'static str, bool)>,
    next: (&'static str, bool),
) -> Option<(&'static str, bool)> {
    match current {
        Some((_, true)) if !next.1 => current,
        _ => Some(next),
    }
}

impl SheetWhitespace {
    /// The sheet of `css`, in one pass over its rules: time and memory in proportion to the
    /// rules and their selectors, whatever the sheet holds.
    fn from_css(css: &str) -> Self {
        let rules = parse_whitespace_rules(css);
        let mut sheet = SheetWhitespace::default();
        for rule in rules
            .iter()
            .filter(|r| r.selectors.iter().any(|s| s == "*"))
        {
            for declaration in &rule.declarations {
                sheet.universal = cascade(sheet.universal, *declaration);
            }
        }
        for rule in &rules {
            for tag in rule.selectors.iter().filter(|s| *s != "*") {
                let winner = sheet
                    .per_tag
                    .entry(tag.clone())
                    .or_insert_with(|| sheet.universal.unwrap_or(("", false)));
                let mut current = (!winner.0.is_empty()).then_some(*winner);
                for declaration in &rule.declarations {
                    current = cascade(current, *declaration);
                }
                if let Some(resolved) = current {
                    *winner = resolved;
                }
            }
        }
        sheet.per_tag.retain(|_, (keyword, _)| !keyword.is_empty());
        sheet
    }

    /// The declaration that applies to an element named `tag`, `None` when none does.
    fn declaration_for(&self, tag: &str) -> Option<(&'static str, bool)> {
        self.per_tag.get(tag).copied().or(self.universal)
    }

    fn is_empty(&self) -> bool {
        self.universal.is_none() && self.per_tag.is_empty()
    }

    /// Whether the sheet keeps the whitespace of the document's body, which every element
    /// without a rule of its own inherits. A sheet cannot style the `<html>` and `<body>`
    /// elements of a fragment (the HTML parser drops them), so what they say is the state
    /// the walk starts from; `body` beats `html`, as the inheritance goes.
    fn initial_keeps_whitespace(&self) -> Option<bool> {
        let of = |tag: &str| {
            self.declaration_for(tag).and_then(|(keyword, important)| {
                style_keeps_whitespace(&format!(
                    "white-space: {keyword}{};",
                    if important { " !important" } else { "" }
                ))
            })
        };
        of("body").or_else(|| of("html"))
    }
}

/// The `white-space` rules of `css` that [`SheetWhitespace`] honours.
///
/// One pass that reads the text as bytes and cuts it only at ASCII delimiters, so that
/// nothing in it, an unterminated comment, string or block, a stray `}` or text that is not
/// ASCII, can panic or loop.
fn parse_whitespace_rules(css: &str) -> Vec<WhitespaceRule> {
    // Comments, and the `<!--` and `-->` a stylesheet inside HTML is wrapped in (Word and
    // Outlook do), which browsers ignore as tokens.
    let mut text = String::with_capacity(css.len());
    let mut rest = css;
    while let Some(start) = rest.find("/*") {
        text.push_str(&rest[..start]);
        text.push(' ');
        match rest[start + 2..].find("*/") {
            Some(end) => rest = &rest[start + 2 + end + 2..],
            None => {
                rest = "";
                break;
            }
        }
    }
    text.push_str(rest);
    let text = text.replace("<!--", " ").replace("-->", " ");
    let bytes = text.as_bytes();

    // The first of `stops` at or after `from` outside a string, or the end.
    let scan = |from: usize, stops: &[u8]| -> usize {
        let mut i = from;
        while i < bytes.len() {
            match bytes[i] {
                b'\\' => i += 1,
                quote @ (b'"' | b'\'') => {
                    i += 1;
                    while i < bytes.len() && bytes[i] != quote {
                        if bytes[i] == b'\\' {
                            i += 1;
                        }
                        i += 1;
                    }
                }
                b if stops.contains(&b) => return i,
                _ => {}
            }
            i += 1;
        }
        bytes.len()
    };
    // The `}` closing the block whose `{` is at `open`, counting nested braces, or the end.
    let block_end = |open: usize| -> usize {
        let mut depth = 0usize;
        let mut i = open;
        while i < bytes.len() {
            i = scan(i, b"{}");
            match bytes.get(i) {
                Some(b'{') => depth += 1,
                Some(b'}') => {
                    depth = depth.saturating_sub(1);
                    if depth == 0 {
                        return i;
                    }
                }
                _ => return bytes.len(),
            }
            i += 1;
        }
        bytes.len()
    };

    let mut rules = Vec::new();
    let mut pos = 0usize;
    while pos < bytes.len() {
        let prelude_end = scan(pos, b"{;}");
        let prelude = text[pos..prelude_end].trim();
        match bytes.get(prelude_end) {
            // A statement (`@import url(x);`) or a stray `}`: nothing to keep.
            Some(b';') | Some(b'}') => pos = prelude_end + 1,
            Some(b'{') => {
                let close = block_end(prelude_end);
                if !prelude.starts_with('@') {
                    let body = &text[prelude_end + 1..close];
                    if let Some(rule) = whitespace_rule(prelude, body) {
                        rules.push(rule);
                    }
                }
                pos = close + 1;
            }
            _ => break,
        }
    }
    rules
}

/// The rule `selectors { body }` makes, when every selector is `*` or a bare tag name and
/// the body holds a valid `white-space` declaration.
fn whitespace_rule(selectors: &str, body: &str) -> Option<WhitespaceRule> {
    let selectors: Vec<String> = selectors
        .split(',')
        .map(|s| s.trim().to_ascii_lowercase())
        .collect();
    let bare = |s: &String| {
        s == "*" || (!s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-'))
    };
    if !selectors.iter().all(bare) {
        return None;
    }
    let mut declarations = Vec::new();
    // The declarations of the rule itself: a nested rule (`p { span { white-space: pre } }`)
    // is another rule's, and a block in the body is taken out before it is split.
    let own;
    let body = if body.contains('{') {
        let mut text = String::with_capacity(body.len());
        let mut depth = 0usize;
        for ch in body.chars() {
            match ch {
                '{' => depth += 1,
                '}' if depth > 0 => {
                    depth -= 1;
                    if depth == 0 {
                        // What ended the nested rule ends a declaration too.
                        text.push(';');
                    }
                }
                _ if depth == 0 => text.push(ch),
                _ => {}
            }
        }
        own = text;
        own.as_str()
    } else {
        body
    };
    for part in body.split(';') {
        let Some((property, value)) = part.split_once(':') else {
            continue;
        };
        if !property.trim().eq_ignore_ascii_case("white-space") {
            continue;
        }
        let value = value.trim().to_ascii_lowercase();
        let (value, important) = value
            .strip_suffix("important")
            .and_then(|rest| rest.trim_end().strip_suffix('!'))
            .map_or((value.as_str(), false), |rest| (rest.trim_end(), true));
        // Only the keywords CSS defines; any other value is dropped, as a browser does.
        let keyword = match value {
            "normal" => "normal",
            "nowrap" => "nowrap",
            "pre" => "pre",
            "pre-wrap" => "pre-wrap",
            "pre-line" => "pre-line",
            "break-spaces" => "break-spaces",
            _ => continue,
        };
        declarations.push((keyword, important));
    }
    (!declarations.is_empty()).then_some(WhitespaceRule {
        selectors,
        declarations,
    })
}

/// What the whitespace of an element's text is, from the sheet and the element's own
/// `style`: the sheet's declarations first, so that the inline ones win unless the sheet's
/// is `!important`. `None` when neither says.
fn element_keeps_whitespace(
    el: &scraper::node::Element,
    sheet: Option<&SheetWhitespace>,
) -> Option<bool> {
    let inline = el.attr("style");
    match sheet.and_then(|s| s.declaration_for(el.name())) {
        None => inline.and_then(style_keeps_whitespace),
        Some((keyword, important)) => style_keeps_whitespace(&format!(
            "white-space: {keyword}{};{}",
            if important { " !important" } else { "" },
            inline.unwrap_or("")
        )),
    }
}

/// The CSS of the `<style>` blocks of `fragment` that apply to a screen: no `type` or
/// `text/css`, no `media` or `all` or `screen`.
fn html_sheet(fragment: &scraper::Html) -> Option<SheetWhitespace> {
    let mut css = String::new();
    for node in fragment.tree.nodes() {
        let Node::Element(el) = node.value() else {
            continue;
        };
        if el.name() != "style" {
            continue;
        }
        let type_ok = el
            .attr("type")
            .is_none_or(|t| t.trim().is_empty() || t.trim().eq_ignore_ascii_case("text/css"));
        let media_ok = el.attr("media").is_none_or(|m| {
            let m = m.trim();
            m.is_empty() || m.eq_ignore_ascii_case("all") || m.eq_ignore_ascii_case("screen")
        });
        if !(type_ok && media_ok) {
            continue;
        }
        for child in node.children() {
            if let Node::Text(text) = child.value() {
                css.push_str(text);
                css.push('\n');
            }
        }
    }
    if css.is_empty() {
        return None;
    }
    let sheet = SheetWhitespace::from_css(&css);
    (!sheet.is_empty()).then_some(sheet)
}

/// Whether `name` is an element that sets its content apart as a block, in the sense of
/// "blanks next to it are layout".
fn is_block_level_name(name: &str) -> bool {
    matches!(
        name,
        "address"
            | "article"
            | "aside"
            | "blockquote"
            | "caption"
            | "center"
            | "dd"
            | "details"
            | "dialog"
            | "dir"
            | "div"
            | "dl"
            | "dt"
            | "fieldset"
            | "figcaption"
            | "figure"
            | "footer"
            | "form"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "header"
            | "hgroup"
            | "hr"
            | "li"
            | "main"
            | "menu"
            | "nav"
            | "ol"
            | "p"
            | "pre"
            | "search"
            | "section"
            | "summary"
            | "table"
            | "tbody"
            | "td"
            | "tfoot"
            | "th"
            | "thead"
            | "tr"
            | "ul"
    )
}

/// Whether `name` is an element the reader turns into a block of its own: its content is
/// flushed apart from the text around it. The block-level names beyond these (a `<section>`,
/// a `<dt>`) are recursed into the run they stand in, so their texts join it.
fn is_block_in_the_reader(name: &str) -> bool {
    matches!(
        name,
        "p" | "div"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "li"
            | "pre"
            | "blockquote"
            | "ul"
            | "ol"
            | "table"
    )
}

/// What a blank text node is, where whitespace is kept.
#[derive(Debug, PartialEq, Eq)]
enum BlankText {
    /// Content: kept as it is.
    Content,
    /// The layout between blocks, or at the edge of the element holding them: nothing.
    Layout,
    /// The layout between two block-level elements the reader does not split a run at
    /// (`<dt>a</dt>\n<dd>b</dd>`, two sections): the boundary a browser shows between two
    /// block boxes, which is one line break where whitespace is kept (and one space in a
    /// table cell, which is a single paragraph). The texts of the two stay apart, one line
    /// each, and not joined into a word.
    Boundary,
}

/// Classify a text node of a parent whose whitespace is kept: only blanks, with a block-level
/// element on one side and a block-level element or the edge of the parent on the other, is
/// layout. Blanks between two inline siblings (`<b>a</b> <i>b</i>`) are the space between two
/// words, and blanks alone in an element (`<p> </p>`) are the content a writer put there;
/// both stay. Decided from the two neighbours, so the cost does not depend on how many
/// siblings there are.
fn blank_text_kind(text: &str, node: ego_tree::NodeRef<Node>) -> BlankText {
    if !text.chars().all(is_html_space) {
        return BlankText::Content;
    }
    // The neighbour on a side, skipping comments: `None` at the edge of the parent.
    let neighbour = |next: bool| {
        let mut at = node;
        loop {
            let sibling = if next {
                at.next_sibling()
            } else {
                at.prev_sibling()
            };
            match sibling {
                Some(s) if matches!(s.value(), Node::Comment(_)) => at = s,
                other => return other,
            }
        }
    };
    // How a neighbour sets blanks apart: `None` for text, and for an element or the edge of
    // the parent: whether it is block-level, whether the reader splits a run at it, and
    // whether it is the edge.
    let side = |sibling: Option<ego_tree::NodeRef<Node>>| match sibling {
        None => Some((false, false, true)),
        Some(s) => match s.value() {
            Node::Element(el) => Some((
                is_block_level_name(el.name()),
                is_block_in_the_reader(el.name()),
                false,
            )),
            _ => None,
        },
    };
    match (side(neighbour(false)), side(neighbour(true))) {
        (Some(before), Some(after)) if before.0 || after.0 => {
            if before.1 || after.1 || before.2 || after.2 {
                BlankText::Layout
            } else {
                BlankText::Boundary
            }
        }
        _ => BlankText::Content,
    }
}

/// Write `text`, a run of a paragraph's text, into `out` as HTML: each of its lines (it
/// separates them with [`LINE_SEPARATOR`]) escaped by `escape`, the lines joined by
/// `line_break`, and a line whose whitespace an HTML reader would collapse put in a
/// `<span style="white-space: pre-wrap">`, where browsers, Qt and this crate's own HTML
/// reader keep it as it is.
///
/// A line is wrapped when it holds a tab or another HTML space but the plain space, two
/// spaces in a row, a space at its start when `after_space` (the text before it on its
/// line ends in whitespace, or the line starts with it), or a space at its end when
/// `ends_line` (its line ends with it). Written bare, a tab came back as a space and a
/// writer's double space as one, so the HTML of a text read back was not the HTML it had
/// been read from. Text an HTML reader keeps as it is goes out bare, as before.
///
/// `after_space` for the next run is [`html_text_ends_open`].
pub fn push_html_text(
    out: &mut String,
    text: &str,
    after_space: bool,
    ends_line: bool,
    escape: impl Fn(&str) -> String,
    line_break: &str,
) {
    let mut lines = text.split(LINE_SEPARATOR).peekable();
    let mut first = true;
    while let Some(line) = lines.next() {
        if !first {
            out.push_str(line_break);
        }
        let opens_on_space = if first { after_space } else { true };
        let ends = ends_line || lines.peek().is_some();
        first = false;
        let collapses = line.chars().any(|ch| ch != ' ' && is_html_space(ch))
            || line.contains("  ")
            || (opens_on_space && line.starts_with(' '))
            || (ends && line.ends_with(' '));
        if collapses {
            out.push_str("<span style=\"white-space: pre-wrap\">");
            out.push_str(&escape(line));
            out.push_str("</span>");
        } else {
            out.push_str(&escape(line));
        }
    }
}

/// Whether a paragraph's line ends before `rest`, the inline content after a run of its
/// text: the `ends_line` of that run (see [`push_html_text`]). It does when nothing but
/// empty content follows, or when the next text starts a line.
pub fn html_line_ends_before<'a>(
    rest: impl IntoIterator<Item = &'a crate::format_runs::InlineContent>,
) -> bool {
    use crate::format_runs::InlineContent;
    for content in rest {
        match content {
            InlineContent::Empty => {}
            InlineContent::Text(text) if text.is_empty() => {}
            InlineContent::Text(text) => return text.starts_with(LINE_SEPARATOR),
            _ => return false,
        }
    }
    true
}

/// Whether the text of a paragraph's line, `text` added to what came before it on its
/// line, ends in whitespace an HTML reader would collapse a space after it into: the
/// `after_space` of the run after it (see [`push_html_text`]). `after_space` says the same
/// of the text before `text`, and is the answer when `text` is empty.
pub fn html_text_ends_open(text: &str, after_space: bool) -> bool {
    match text.chars().next_back() {
        None => after_space,
        Some(ch) => ch == LINE_SEPARATOR || is_html_space(ch),
    }
}

/// The five characters HTML calls whitespace.
///
/// Deliberately not `char::is_whitespace`, which is `true` for U+00A0: a
/// no-break space is a character the writer typed — the one French typography
/// puts before a colon, holding "Attention&nbsp;:" together — and collapsing it
/// away is a silent edit of the text, not of its layout.
fn is_html_space(ch: char) -> bool {
    matches!(ch, ' ' | '\t' | '\n' | '\r' | '\u{0C}')
}

/// The box a Markdown task item writes after its list marker: `[x] ` or `[ ] `, nothing for
/// an item that is no task. Shared by the two Markdown writers.
pub fn task_box_markdown(marker: Option<&MarkerType>) -> &'static str {
    match marker {
        Some(MarkerType::Checked) => "[x] ",
        Some(MarkerType::Unchecked) => "[ ] ",
        _ => "",
    }
}

/// The checkbox an HTML list item opens with when it is a task, nothing otherwise, inside
/// the `<label>` that names it: the item's text, which [`task_checkbox_end_html`] closes the
/// label after, is the checkbox's accessible name (a bare checkbox reads as "checkbox,
/// checked" and the text as another item). Written as well-formed XML (`disabled="disabled"`,
/// a closed tag) because the same markup is packaged into EPUB XHTML chapters, which a
/// minimised attribute and an unclosed input would break in a strict reader. Shared by the
/// two HTML writers; `html_item_checkbox` reads it back, through the label.
pub fn task_checkbox_html(marker: Option<&MarkerType>) -> &'static str {
    match marker {
        Some(MarkerType::Checked) => {
            "<label><input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\"/> "
        }
        Some(MarkerType::Unchecked) => "<label><input type=\"checkbox\" disabled=\"disabled\"/> ",
        _ => "",
    }
}

/// What closes [`task_checkbox_html`]'s label, after the item's inline text.
pub fn task_checkbox_end_html(marker: Option<&MarkerType>) -> &'static str {
    match marker {
        Some(MarkerType::Checked | MarkerType::Unchecked) => "</label>",
        _ => "",
    }
}

/// The task-list checkbox an HTML list item opens with: the first thing it holds, apart
/// from blanks, is an `<input type="checkbox">`, bare or inside inline wrappers such as
/// `<label>` and `<span>`. `checked` makes it [`MarkerType::Checked`]; `disabled` and
/// every other attribute change nothing. A checkbox further on is a form control in the
/// text, not the item's box. A box inside a `<p>` of the item, which is how a loose GitHub task
/// list is rendered (`<li><p><input type="checkbox"> text</p></li>`), is not read.
fn html_item_checkbox(item: ego_tree::NodeRef<Node>) -> Option<MarkerType> {
    /// `Some(answer)` once something decided it, `None` while only blanks were seen.
    fn scan(node: ego_tree::NodeRef<Node>, depth: usize) -> Option<Option<MarkerType>> {
        for child in node.children() {
            match child.value() {
                Node::Text(text) => {
                    if !text.chars().all(is_html_space) {
                        return Some(None);
                    }
                }
                Node::Element(el) => {
                    let name = el.name();
                    if name == "input" {
                        let is_checkbox = el
                            .attr("type")
                            .is_some_and(|t| t.trim().eq_ignore_ascii_case("checkbox"));
                        return Some(is_checkbox.then(|| {
                            if el.attr("checked").is_some() {
                                MarkerType::Checked
                            } else {
                                MarkerType::Unchecked
                            }
                        }));
                    }
                    if matches!(name, "label" | "span") && depth < 8 {
                        if let Some(decided) = scan(child, depth + 1) {
                            return Some(decided);
                        }
                    } else {
                        return Some(None);
                    }
                }
                _ => {}
            }
        }
        None
    }
    scan(item, 0).flatten()
}

/// Whether a span run holds anything a reader would see.
fn spans_carry_content(spans: &[ParsedSpan]) -> bool {
    spans
        .iter()
        .any(|s| !s.text.is_empty() || s.image.is_some() || s.footnote_ref.is_some())
}

/// Apply the CSS `white-space: normal` rules to one block's inline spans: every
/// run of HTML whitespace collapses to a single space, and the runs at the
/// block's two ends disappear entirely.
///
/// Per block rather than per text node, because a run crosses span boundaries —
/// `<b>word</b>\n<i>next</i>` is one collapsible run split over three nodes —
/// and because only the block knows where its own ends are.
///
/// Without this, an exporter's own source formatting reaches the document as
/// text. LibreOffice writes a newline after every `<p …>` and hard-wraps the
/// prose inside it at about seventy columns, so pasting a whole book put a
/// blank line before every paragraph and a break through the middle of every
/// sentence.
fn collapse_inline_whitespace(spans: &mut [ParsedSpan]) {
    fn is_replaced(span: &ParsedSpan) -> bool {
        span.image.is_some() || span.footnote_ref.is_some()
    }

    // Collapse within each span first. Nothing non-empty is emptied here, which
    // is what lets the second pass read `is_empty()` as "this span held no text
    // to begin with" — the marker a `<br>` leaves behind.
    for span in spans.iter_mut() {
        if is_replaced(span) || span.preserve_whitespace {
            continue;
        }
        let mut out = String::with_capacity(span.text.len());
        let mut in_run = false;
        for ch in span.text.chars() {
            if is_html_space(ch) {
                if !in_run {
                    out.push(' ');
                    in_run = true;
                }
            } else {
                out.push(ch);
                in_run = false;
            }
        }
        span.text = out;
    }

    // The block's start counts as "already ended on a space", so a run opening
    // the block is dropped outright. A line break (the span a `<br>` leaves) ends one
    // line and starts the next, so the runs either side of it go as the block's own
    // ends do: `x <br> y` is two lines, "x" and "y".
    let mut prev_ends_with_space = true;
    // The last span holding text since the line began, whose trailing space a line
    // break takes off.
    let mut line_end: Option<usize> = None;
    for i in 0..spans.len() {
        let span = &mut spans[i];
        if is_replaced(span) {
            prev_ends_with_space = false;
            line_end = None;
            continue;
        }
        if span.text.is_empty() {
            // A span that held no text to begin with. It contributes none, but it
            // still ends the collapsible run either side of it.
            prev_ends_with_space = false;
            continue;
        }
        if span.text == LINE_SEPARATOR_STR {
            if let Some(end) = line_end.take()
                && spans[end].text.ends_with(' ')
            {
                spans[end].text.pop();
            }
            prev_ends_with_space = true;
            continue;
        }
        // Kept whitespace is the text's own, as a replaced element is content: a space
        // after it is not collapsed into it, and a line break takes none of it off.
        if span.preserve_whitespace {
            prev_ends_with_space = false;
            line_end = None;
            continue;
        }
        if prev_ends_with_space && span.text.starts_with(' ') {
            span.text.remove(0);
        }
        // A span emptied by that drop leaves the run open rather than closing
        // it: the space it carried belongs to the run its neighbours share.
        if !span.text.is_empty() {
            prev_ends_with_space = span.text.ends_with(' ');
            line_end = Some(i);
        }
    }

    // The block's trailing run, in whichever span still holds it. Replaced
    // content stops the walk: a space before a trailing image is between two
    // pieces of content, not after the last one. A line break at the very end
    // starts no line of its own, as a browser shows it (a second one does: `a<br><br>`
    // is "a" and an empty line), so the last one goes, with the run before it.
    let mut trailing_break_dropped = false;
    for span in spans.iter_mut().rev() {
        if is_replaced(span) {
            break;
        }
        if span.text.is_empty() {
            continue;
        }
        if span.text == LINE_SEPARATOR_STR {
            if trailing_break_dropped {
                break;
            }
            span.text.clear();
            trailing_break_dropped = true;
            continue;
        }
        if span.preserve_whitespace {
            break;
        }
        if span.text.ends_with(' ') {
            span.text.pop();
        }
        break;
    }
}

/// `block` as one block per line of its text, a line ending at each `\n`.
///
/// A `\n` is not a line break inside a block, which the model holds as
/// [`LINE_SEPARATOR`]: in the rope it separates two blocks, so a new line of this kind
/// is a new block, which is what a plain-text paste makes of one. Text that kept its
/// source's line breaks (`<pre>`, `white-space: pre`) held them inside one block, where
/// the editor showed them as lines and the Djot writer had nothing to write them as.
/// Written raw, a blank line ended the paragraph and the lines after it were read back
/// as the lists, headings and quotations their markers spelled, however deep.
///
/// Each line keeps the block's own formatting and its spans' formatting, but for the
/// page break, which stays with the first line: it is where the block starts, and on
/// every line it put each on a page of its own. An empty line is an empty block. The
/// line break that ends the last line starts no line of its own, as a browser shows it.
/// A block without a line break is returned as it is.
///
/// Every line is built from copies of the block and of the span it comes from that
/// hold no text, so the split costs in proportion to the text: built from whole copies,
/// each line copied the whole text again, and a paste of 64,000 preformatted lines
/// took seconds.
pub fn split_block_at_line_breaks(mut block: ParsedBlock) -> Vec<ParsedBlock> {
    let breaks = |span: &ParsedSpan| span.image.is_none() && span.text.contains('\n');
    if !block.spans.iter().any(breaks) {
        return vec![block];
    }
    let mut lines: Vec<Vec<ParsedSpan>> = vec![Vec::new()];
    for mut span in std::mem::take(&mut block.spans) {
        if !breaks(&span) {
            push_to_last_line(&mut lines, span);
            continue;
        }
        let text = std::mem::take(&mut span.text);
        for (i, piece) in text.split('\n').enumerate() {
            if i > 0 {
                lines.push(Vec::new());
            }
            if !piece.is_empty() {
                push_to_last_line(
                    &mut lines,
                    ParsedSpan {
                        text: piece.to_string(),
                        ..span.clone()
                    },
                );
            }
        }
    }
    if lines.len() > 1 && lines.last().is_some_and(Vec::is_empty) {
        lines.pop();
    }
    let later_line = ParsedBlock {
        page_break_before: None,
        blockquote_opens: 0,
        // The box is the item's, which its first line holds.
        marker: None,
        ..block.clone()
    };
    let mut first_line = Some(block);
    lines
        .into_iter()
        .map(|spans| {
            let spans = if spans.is_empty() {
                vec![ParsedSpan::default()]
            } else {
                spans
            };
            match first_line.take() {
                Some(first) => ParsedBlock { spans, ..first },
                None => ParsedBlock {
                    spans,
                    ..later_line.clone()
                },
            }
        })
        .collect()
}

/// Append `span` to the last of `lines`, which [`split_block_at_line_breaks`] keeps
/// non-empty.
fn push_to_last_line(lines: &mut Vec<Vec<ParsedSpan>>, span: ParsedSpan) {
    match lines.last_mut() {
        Some(line) => line.push(span),
        None => lines.push(vec![span]),
    }
}

pub fn parse_html(html: &str) -> Vec<ParsedBlock> {
    ParsedElement::flatten_to_blocks(parse_html_elements(html))
}

/// The attribute an HTML producer uses to say "this element *is* a footnote
/// reference", and the label it names.
///
/// A data attribute on an otherwise-ordinary element, rather than a literal
/// `[^label]` in the text, because the text a producer emits is *escaped* on the
/// way to Djot — [`escape_djot_inline`](crate::parser_tools::djot_escape::escape_djot_inline)
/// neutralises `[`, `]` and `^`, so a reference smuggled through as characters
/// arrives as `\[\^1\]` and is prose rather than a reference. It has to be a
/// node before it is serialised, which is what this makes it.
///
/// [`ParsedSpan::footnote_ref`] is the same field the Markdown/Djot reader
/// already fills, so everything downstream — the document model, the Djot
/// writer, numbering — needs no change at all.
pub const HTML_FOOTNOTE_ATTR: &str = "data-footnote-ref";

/// Build a footnote-reference span from any element carrying
/// [`HTML_FOOTNOTE_ATTR`].
fn html_footnote_span(
    el: &scraper::node::Element,
    link_href: Option<String>,
) -> Option<ParsedSpan> {
    let label = el.attr(HTML_FOOTNOTE_ATTR)?.trim();
    if label.is_empty() {
        return None;
    }
    Some(ParsedSpan {
        text: String::new(),
        link_href,
        footnote_ref: Some(label.to_string()),
        ..Default::default()
    })
}

/// Build an inline-image span from an `<img>` element, if it has a usable
/// source.
///
/// `<img>` was matched by none of the HTML walker's three tag dispatches, so it
/// fell into their wildcard arms and was dropped whole — silently, and unlike
/// Markdown not even leaving its alt text behind. That is the path a browser or
/// Word paste travels.
fn html_img_span(el: &scraper::node::Element, link_href: Option<String>) -> Option<ParsedSpan> {
    let src = el.attr("src")?;
    if src.is_empty() {
        return None;
    }
    let dim = |name: &str| -> i64 {
        el.attr(name)
            .and_then(|v| v.trim().trim_end_matches("px").parse::<i64>().ok())
            .filter(|n| *n > 0)
            .unwrap_or(0)
    };
    Some(ParsedSpan {
        text: String::new(),
        link_href,
        image: Some(ParsedImage {
            src: src.to_string(),
            alt: el.attr("alt").unwrap_or_default().to_string(),
            width: dim("width"),
            height: dim("height"),
        }),
        ..Default::default()
    })
}

/// Whether `node` is a `<ul>` or an `<ol>`.
fn is_html_list(node: &Node) -> bool {
    node.as_element()
        .is_some_and(|el| matches!(el.name(), "ul" | "ol"))
}

/// Move every list item nested deeper than
/// [`MAX_LIST_LEVELS`](crate::parser_tools::list_depth::MAX_LIST_LEVELS)
/// lists into the deepest list kept, right after the item holding it, in
/// document order: a list nested past that depth comes out flattened to it,
/// every item kept, in its order.
///
/// The reader recurses a few elements deep per list level and stops at
/// `MAX_RECURSION_DEPTH`, so a list nested past some eighty levels lost every
/// item below that, pasted or loaded. The editing gestures nest no deeper than
/// this depth, and an insertion brings any list back to it (see `list_depth`),
/// so HTML holding more is read at that depth to begin with. The tree is
/// changed in place, without recursion, before it is read.
fn flatten_deep_html_lists(tree: &mut ego_tree::Tree<Node>) {
    use crate::parser_tools::list_depth::MAX_LIST_LEVELS;
    use ego_tree::iter::Edge;

    // The lists one level past the deepest kept, in document order.
    let mut too_deep: Vec<ego_tree::NodeId> = Vec::new();
    let mut depth = 0usize;
    for edge in tree.root().traverse() {
        match edge {
            Edge::Open(node) if is_html_list(node.value()) => {
                if depth == MAX_LIST_LEVELS {
                    too_deep.push(node.id());
                }
                depth += 1;
            }
            Edge::Close(node) if is_html_list(node.value()) => {
                depth = depth.saturating_sub(1);
            }
            _ => {}
        }
    }

    for list in too_deep {
        // The child of the deepest kept list that holds `list` (its `<li>`, or
        // `list` itself when a list sits right inside another): the items go
        // after it.
        let Some(list_ref) = tree.get(list) else {
            continue;
        };
        let mut holder = None;
        let mut current = list_ref;
        while let Some(parent) = current.parent() {
            if is_html_list(parent.value()) {
                holder = Some(current.id());
                break;
            }
            current = parent;
        }
        let Some(holder) = holder else {
            continue;
        };
        let mut after = holder;
        let items: Vec<ego_tree::NodeId> = list_ref
            .descendants()
            .filter(|node| {
                node.value()
                    .as_element()
                    .is_some_and(|el| el.name() == "li")
            })
            .map(|node| node.id())
            .collect();
        for item in items {
            // Each node placed here sits in the deepest kept list, so it has a
            // parent to be placed beside.
            if tree.get(after).and_then(|node| node.parent()).is_none() {
                break;
            }
            if let Some(mut node) = tree.get_mut(after) {
                node.insert_id_after(item);
                after = item;
            }
        }
        // What is left of the list is its items' wrappers, or text standing in
        // it outside any item. It goes after the items when it holds any text
        // (it is already beside them when it sits right in the deepest kept
        // list), and away when it holds none.
        let holds_text = tree.get(list).is_some_and(|node| {
            node.descendants()
                .any(|d| d.value().as_text().is_some_and(|t| !t.trim().is_empty()))
        });
        if !holds_text {
            if let Some(mut node) = tree.get_mut(list) {
                node.detach();
            }
        } else if holder != list
            && tree.get(after).and_then(|node| node.parent()).is_some()
            && let Some(mut node) = tree.get_mut(after)
        {
            node.insert_id_after(list);
        }
    }
}

/// How deep a walk of an HTML tree may go on the caller's own stack: a debug build spends
/// between 5 and 8 KiB of stack on each element level it follows, so this is at most some
/// 256 KiB, what any thread a host reads on has to spare.
const HTML_DEPTH_ON_THE_CALLERS_STACK: usize = 32;

/// The deepest an HTML reader follows elements: a subtree nested deeper is read as its
/// text, without recursing (see `read_html_tree`).
const MAX_HTML_READ_DEPTH: usize = 256;

/// The stack a reader of a deeper tree gets on a thread of its own, before what each level
/// adds: a spawned thread's.
const HTML_READER_STACK: usize = 2 << 20;

/// The stack each element level of a deeper tree adds for its reader: two to three times
/// what a debug build spends on one.
const HTML_STACK_PER_LEVEL: usize = 16 << 10;

/// `value` read as HTML reads an integer attribute such as `<ol start>`: after any leading
/// white space, an optional sign and the digits up to the first character that is not one.
/// `None` when no digit follows. A number past what 64 bits hold reads as the largest they
/// hold on its side of 0. A browser numbers `<ol start="5x">` from 5, where a strict parse
/// of the whole value numbered it from 1.
fn html_integer(value: &str) -> Option<i64> {
    let value = value.trim_start_matches(|c: char| c.is_ascii_whitespace());
    let (negative, digits) = match value.as_bytes().first() {
        Some(b'-') => (true, &value[1..]),
        Some(b'+') => (false, &value[1..]),
        _ => (false, value),
    };
    let digits: &str = digits
        .split(|c: char| !c.is_ascii_digit())
        .next()
        .unwrap_or("");
    if digits.is_empty() {
        return None;
    }
    let magnitude = digits.bytes().fold(0i64, |number, digit| {
        number
            .saturating_mul(10)
            .saturating_add(i64::from(digit - b'0'))
    });
    Some(if negative { -magnitude } else { magnitude })
}

/// How many elements deep the deepest node of `tree` is. A loop over the tree's edges,
/// never a recursion.
fn html_tree_depth(tree: &ego_tree::Tree<Node>) -> usize {
    use ego_tree::iter::Edge;
    let mut depth = 0usize;
    let mut deepest = 0usize;
    for edge in tree.root().traverse() {
        match edge {
            Edge::Open(node) if node.value().is_element() => {
                depth += 1;
                deepest = deepest.max(depth);
            }
            Edge::Close(node) if node.value().is_element() => depth = depth.saturating_sub(1),
            _ => {}
        }
    }
    deepest
}

/// Read `html` into blocks and tables.
///
/// The reader recurses once per element level it follows, up to `MAX_HTML_READ_DEPTH`,
/// and a stack overflow aborts the process: a debug build ran out of a spawned thread's
/// stack on a paste nested some 200 elements deep, and a host reading on a smaller thread
/// sooner. A tree nested deeper than a caller's stack is sure to hold is read on a thread
/// of its own, with a stack sized for its depth; when no thread can be started, it is read
/// on the caller's stack to `HTML_DEPTH_ON_THE_CALLERS_STACK` levels, each subtree
/// deeper than that read as its text. The quotations it holds are kept within
/// [`MAX_QUOTE_LEVELS`](crate::parser_tools::quote_depth::MAX_QUOTE_LEVELS) (see
/// `quote_depth`).
///
/// Whitespace is kept where an element's `white-space` says so: in its inline `style`, and
/// in a `<style>` block whose rule names only `*` or bare tags, as
/// `p, li { white-space: pre-wrap; }` does (what Qt writes). A rule with a class, id,
/// attribute, pseudo-class or combinator in its selector, a rule inside an at-rule such as
/// `@media`, a `<style>` of another `type` or for `print`, and every other property, is
/// ignored. Blanks alone between two blocks stay layout; between two inline siblings they are
/// the space between two words. Known limits: a rule with a class or id is not applied, and
/// neither is what it overrides (`p.plain { white-space: normal }` over `p { white-space: pre }`
/// leaves the paragraphs of the class kept); `pre-line` and `nowrap` collapse all whitespace,
/// as they do in an inline `style`; and a `pre` from a sheet, unlike one in an inline `style`,
/// does not mark the block non-breakable.
pub fn parse_html_elements(html: &str) -> Vec<ParsedElement> {
    use scraper::Html;

    let read = |depth_limit: usize, html: &str| {
        let mut fragment = Html::parse_fragment(html);
        flatten_deep_html_lists(&mut fragment.tree);
        let mut elements = read_html_tree(&fragment, depth_limit);
        crate::parser_tools::quote_depth::clamp_quote_depths(&mut elements);
        elements
    };
    let mut fragment = Html::parse_fragment(html);
    flatten_deep_html_lists(&mut fragment.tree);
    let depth = html_tree_depth(&fragment.tree);
    if depth <= HTML_DEPTH_ON_THE_CALLERS_STACK {
        let mut elements = read_html_tree(&fragment, MAX_HTML_READ_DEPTH);
        crate::parser_tools::quote_depth::clamp_quote_depths(&mut elements);
        return elements;
    }
    drop(fragment);
    let stack = HTML_READER_STACK + depth.min(MAX_HTML_READ_DEPTH + 1) * HTML_STACK_PER_LEVEL;
    let owned = html.to_owned();
    let on_its_own_thread = std::thread::Builder::new()
        .stack_size(stack)
        .spawn(move || read(MAX_HTML_READ_DEPTH, &owned))
        .ok()
        .and_then(|reader| reader.join().ok());
    on_its_own_thread.unwrap_or_else(|| read(HTML_DEPTH_ON_THE_CALLERS_STACK, html))
}

/// Read `fragment`, following its elements `depth_limit` deep at most: a subtree nested
/// deeper is read as its text (see `subtree_text`). The walk recurses once per element
/// level it follows.
fn read_html_tree(fragment: &scraper::Html, depth_limit: usize) -> Vec<ParsedElement> {
    let mut elements: Vec<ParsedElement> = Vec::new();

    // Walk the DOM tree starting from the root
    let root = fragment.root_element();

    #[derive(Clone, Default)]
    struct FmtState {
        bold: bool,
        italic: bool,
        underline: bool,
        strikeout: bool,
        code: bool,
        superscript: bool,
        subscript: bool,
        link_href: Option<String>,
        /// Under an element whose `white-space` keeps whitespace as it is.
        preserve_whitespace: bool,
        /// The `white-space` rules of the document's `<style>` blocks, shared by every state.
        sheet: Option<std::rc::Rc<SheetWhitespace>>,
    }

    /// The list an element stands in: its style, and the number its first item wears when
    /// it is not 1 (`<ol start="3">`).
    type HtmlList = (Option<ListStyle>, Option<i64>);

    /// Elements whose text content is machinery, not prose.
    ///
    /// `<style>` and `<script>` hold raw text the HTML spec never renders, and
    /// `<head>` and its metadata children carry none a reader is meant to see.
    /// Every walker below recurses into unknown tags on the assumption that
    /// they are inline wrappers, which turns a stylesheet into paragraphs —
    /// and Word, Google Docs and Chrome all put a `<style>` block in the
    /// `text/html` flavour they publish to the clipboard, so this is the
    /// ordinary paste, not an exotic one.
    fn is_metadata_tag(tag: &str) -> bool {
        matches!(
            tag,
            "head"
                | "style"
                | "script"
                | "title"
                | "meta"
                | "link"
                | "base"
                | "noscript"
                | "template"
        )
    }

    /// The text of everything under `node`, for a subtree nested deeper than
    /// the reader follows (see [`MAX_HTML_READ_DEPTH`]): read without recursing,
    /// with a space where a block ends so that words do not run together, and
    /// without what [`is_metadata_tag`] names.
    ///
    /// Past that depth the reader used to stop and keep nothing: text nested
    /// deeply enough in lists, quotations or plain `<div>`s was dropped from a
    /// paste without a trace.
    fn subtree_text(node: ego_tree::NodeRef<Node>) -> String {
        use ego_tree::iter::Edge;
        let mut text = String::new();
        // How many elements deep the walk is inside a metadata element.
        let mut hidden = 0usize;
        for edge in node.traverse() {
            match edge {
                Edge::Open(open) => match open.value() {
                    Node::Element(el) => {
                        if hidden > 0 || is_metadata_tag(el.name()) {
                            hidden += 1;
                        } else if !text.is_empty() && !text.ends_with(char::is_whitespace) {
                            text.push(' ');
                        }
                    }
                    Node::Text(t) if hidden == 0 => text.push_str(t),
                    _ => {}
                },
                Edge::Close(close) => {
                    if hidden > 0 && close.value().is_element() {
                        hidden -= 1;
                    }
                }
            }
        }
        text
    }

    /// A span of `text` in `state`'s formatting.
    fn text_span(text: String, state: &FmtState) -> ParsedSpan {
        ParsedSpan {
            text,
            bold: state.bold,
            italic: state.italic,
            underline: state.underline,
            strikeout: state.strikeout,
            code: state.code,
            superscript: state.superscript,
            subscript: state.subscript,
            link_href: state.link_href.clone(),
            image: None,
            footnote_ref: None,
            preserve_whitespace: state.preserve_whitespace,
        }
    }

    /// The space a cell puts between two blocks of its own: layout, collapsed whatever the
    /// state's whitespace, where a kept one would stay a space at the cell's edge.
    fn separator_span(state: &FmtState) -> ParsedSpan {
        let mut span = text_span(" ".to_string(), state);
        span.preserve_whitespace = false;
        span
    }

    /// Collect inline spans from a `<td>` or `<th>` cell element.
    fn collect_cell_spans(
        node: ego_tree::NodeRef<Node>,
        state: &FmtState,
        spans: &mut Vec<ParsedSpan>,
        depth: usize,
        limit: usize,
    ) {
        if depth > limit {
            let text = subtree_text(node);
            if !text.is_empty() {
                spans.push(text_span(text, state));
            }
            return;
        }
        for child in node.children() {
            match child.value() {
                Node::Text(text) => {
                    let t = text.text.to_string();
                    // The blanks between two blocks are layout, even where whitespace is kept.
                    let blank = if state.preserve_whitespace {
                        blank_text_kind(&t, child)
                    } else {
                        BlankText::Content
                    };
                    if blank == BlankText::Boundary {
                        // One space between two texts, none where the cell has no text yet
                        // or the text before ends in a blank already.
                        if spans
                            .last()
                            .is_some_and(|last| !last.text.ends_with(is_html_space))
                        {
                            spans.push(separator_span(state));
                        }
                    } else if !t.is_empty() && blank == BlankText::Content {
                        spans.push(ParsedSpan {
                            text: t,
                            bold: state.bold,
                            italic: state.italic,
                            underline: state.underline,
                            strikeout: state.strikeout,
                            code: state.code,
                            superscript: state.superscript,
                            subscript: state.subscript,
                            link_href: state.link_href.clone(),
                            image: None,
                            footnote_ref: None,
                            preserve_whitespace: state.preserve_whitespace,
                        });
                    }
                }
                Node::Element(el) => {
                    let tag = el.name();
                    if is_metadata_tag(tag) {
                        continue;
                    }
                    let mut new_state = state.clone();
                    if let Some(keep) = element_keeps_whitespace(el, state.sheet.as_deref()) {
                        new_state.preserve_whitespace = keep;
                    }
                    match tag {
                        // Checked by attribute rather than by tag name: the
                        // producer picks the element (a `<sup>` renders sensibly
                        // in a browser), and only the attribute is a contract.
                        _ if el.attr(HTML_FOOTNOTE_ATTR).is_some() => {
                            if let Some(span) = html_footnote_span(el, new_state.link_href.clone())
                            {
                                spans.push(span);
                            }
                            continue;
                        }
                        "b" | "strong" => new_state.bold = true,
                        "i" | "em" => new_state.italic = true,
                        "u" | "ins" => new_state.underline = true,
                        "s" | "del" | "strike" => new_state.strikeout = true,
                        "code" => new_state.code = true,
                        "sup" => new_state.superscript = true,
                        "sub" => new_state.subscript = true,
                        "a" => {
                            if let Some(href) = el.attr("href") {
                                new_state.link_href = Some(href.to_string());
                            }
                        }
                        "img" => {
                            if let Some(span) = html_img_span(el, new_state.link_href.clone()) {
                                spans.push(span);
                            }
                            continue;
                        }
                        // A cell is read as one paragraph, so a line break, or a
                        // paragraph or list item of its own, is a space between
                        // words. Read as nothing, a cell of two paragraphs, which
                        // the HTML writer puts out as `One<br/>Two`, came back as
                        // the one word `OneTwo`.
                        "br" => {
                            spans.push(separator_span(state));
                            continue;
                        }
                        "p" | "div" | "li" | "ul" | "ol" | "blockquote" | "pre" | "h1" | "h2"
                        | "h3" | "h4" | "h5" | "h6" => {
                            spans.push(separator_span(state));
                        }
                        _ => {}
                    }
                    collect_cell_spans(child, &new_state, spans, depth + 1, limit);
                }
                _ => {}
            }
        }
    }

    /// The widest a `colspan` makes a row: HTML's own limit on the attribute. A row of
    /// ordinary cells may be wider; only the empty cells a span adds stop here.
    const MAX_HTML_COLSPAN: usize = 1000;

    /// Parse a `<table>` element into a ParsedTable, with the number of `<td>` and `<th>`
    /// cells it has (see `push_table`). `depth` is how deep the walk stands at the table,
    /// and `limit` how deep it goes.
    fn parse_table_element(
        table_node: ego_tree::NodeRef<Node>,
        state: &FmtState,
        depth: usize,
        limit: usize,
    ) -> (ParsedTable, usize) {
        let mut rows: Vec<Vec<ParsedTableCell>> = Vec::new();
        let mut header_rows: usize = 0;
        let mut own_cells: usize = 0;

        /// `walk` is how deep the walk stands at `node` and how deep it goes.
        fn collect_rows(
            node: ego_tree::NodeRef<Node>,
            state: &FmtState,
            rows: &mut Vec<Vec<ParsedTableCell>>,
            header_rows: &mut usize,
            own_cells: &mut usize,
            in_thead: bool,
            walk: (usize, usize),
        ) {
            let (depth, limit) = walk;
            for child in node.children() {
                if let Node::Element(el) = child.value() {
                    match el.name() {
                        "thead" => collect_rows(
                            child,
                            state,
                            rows,
                            header_rows,
                            own_cells,
                            true,
                            (depth + 1, limit),
                        ),
                        "tbody" | "tfoot" => collect_rows(
                            child,
                            state,
                            rows,
                            header_rows,
                            own_cells,
                            false,
                            (depth + 1, limit),
                        ),
                        "tr" => {
                            let mut cells: Vec<ParsedTableCell> = Vec::new();
                            // The cells of the row up to its last `<td>` or `<th>`: the
                            // empty cells a span adds after it hold no cell's place.
                            let mut own_end = 0;
                            for td in child.children() {
                                if let Node::Element(td_el) = td.value()
                                    && matches!(td_el.name(), "td" | "th")
                                {
                                    let mut spans = Vec::new();
                                    // A cell starts from the whitespace the table stands in
                                    // (the sheet's, with the table's own), then its own. It
                                    // takes none of the formatting around the table: a table
                                    // in a `<b>` or an `<a>` is not made of bold cells or links.
                                    let mut cell_state = FmtState {
                                        preserve_whitespace: state.preserve_whitespace,
                                        sheet: state.sheet.clone(),
                                        ..FmtState::default()
                                    };
                                    if let Some(keep) =
                                        element_keeps_whitespace(td_el, state.sheet.as_deref())
                                    {
                                        cell_state.preserve_whitespace = keep;
                                    }
                                    collect_cell_spans(
                                        td,
                                        &cell_state,
                                        &mut spans,
                                        depth + 2,
                                        limit,
                                    );
                                    collapse_inline_whitespace(&mut spans);
                                    if spans.is_empty() {
                                        spans.push(ParsedSpan::default());
                                    }
                                    cells.push(ParsedTableCell { spans });
                                    *own_cells += 1;
                                    own_end = cells.len();
                                    // A cell spanning columns covers the cells after it
                                    // in its row: empty ones hold its place, so the cells
                                    // that follow stay in their own columns and the
                                    // table is as wide as the columns it spans.
                                    let span = td_el
                                        .attr("colspan")
                                        .and_then(|value| value.trim().parse::<usize>().ok())
                                        .unwrap_or(1)
                                        .clamp(1, MAX_HTML_COLSPAN);
                                    for _ in 1..span {
                                        if cells.len() >= MAX_HTML_COLSPAN {
                                            break;
                                        }
                                        cells.push(ParsedTableCell {
                                            spans: vec![ParsedSpan::default()],
                                        });
                                    }
                                }
                            }
                            // A span closing a row widens the table only as far as another
                            // row's cells reach: `<th colspan="1000">` over rows of one
                            // cell is a table of one column, not of a thousand.
                            cells.truncate(own_end);
                            if !cells.is_empty() {
                                rows.push(cells);
                                if in_thead {
                                    *header_rows += 1;
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        collect_rows(
            table_node,
            state,
            &mut rows,
            &mut header_rows,
            &mut own_cells,
            false,
            (depth, limit),
        );

        // Tables without explicit <thead> but with <th> cells: treat first row as header
        if header_rows == 0 && !rows.is_empty() {
            header_rows = 1;
        }

        (
            ParsedTable {
                header_rows,
                rows,
                // The caller (`walk_node`) sets the real depth: this helper has
                // no visibility into the surrounding blockquote nesting.
                blockquote_depth: 0,
                blockquote_opens: 0,
            },
            own_cells,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn walk_node(
        node: ego_tree::NodeRef<Node>,
        state: &FmtState,
        elements: &mut Vec<ParsedElement>,
        current_list: &HtmlList,
        blockquote_depth: u32,
        list_depth: u32,
        depth: usize,
        limit: usize,
    ) {
        if depth > limit {
            let text = subtree_text(node);
            if !text.trim().is_empty() {
                let mut spans = vec![text_span(text, state)];
                collapse_inline_whitespace(&mut spans);
                elements.push(ParsedElement::Block(ParsedBlock {
                    spans,
                    blockquote_depth,
                    blockquote_opens: 0,
                    ..ParsedBlock::default()
                }));
            }
            return;
        }
        match node.value() {
            Node::Element(el) => {
                let tag = el.name();
                if is_metadata_tag(tag) {
                    return;
                }
                let mut new_state = state.clone();
                if let Some(keep) = element_keeps_whitespace(el, state.sheet.as_deref()) {
                    new_state.preserve_whitespace = keep;
                }
                let mut new_list = current_list.clone();
                let mut bq_depth = blockquote_depth;
                let mut new_list_depth = list_depth;

                // Determine if this is a block-level element
                let is_block_tag = matches!(
                    tag,
                    "p" | "div"
                        | "h1"
                        | "h2"
                        | "h3"
                        | "h4"
                        | "h5"
                        | "h6"
                        | "li"
                        | "pre"
                        | "br"
                        | "blockquote"
                        | "body"
                        | "html"
                );

                // Update formatting state
                match tag {
                    "b" | "strong" => new_state.bold = true,
                    "i" | "em" => new_state.italic = true,
                    "u" | "ins" => new_state.underline = true,
                    "s" | "del" | "strike" => new_state.strikeout = true,
                    "code" => new_state.code = true,
                    "sup" => new_state.superscript = true,
                    "sub" => new_state.subscript = true,
                    "a" => {
                        if let Some(href) = el.attr("href") {
                            new_state.link_href = Some(href.to_string());
                        }
                    }
                    "ul" => {
                        new_list = (Some(ListStyle::Disc), None);
                        new_list_depth = list_depth + 1;
                    }
                    "ol" => {
                        // A list numbered from below 0 is kept from 1: no saved syntax
                        // but HTML writes one. A start past what every format holds is
                        // read as the largest it holds (see `list_start`).
                        let start = el
                            .attr("start")
                            .and_then(html_integer)
                            .and_then(crate::parser_tools::list_start::list_start);
                        new_list = (Some(ListStyle::Decimal), start);
                        new_list_depth = list_depth + 1;
                    }
                    "blockquote" => {
                        bq_depth += 1;
                    }
                    _ => {}
                }

                // Determine heading level
                let heading_level = match tag {
                    "h1" => Some(1),
                    "h2" => Some(2),
                    "h3" => Some(3),
                    "h4" => Some(4),
                    "h5" => Some(5),
                    "h6" => Some(6),
                    _ => None,
                };

                let is_code_block = tag == "pre";

                // Extract code language from <pre><code class="language-xxx">
                let code_language = if is_code_block {
                    node.children().find_map(|child| {
                        if let Node::Element(cel) = child.value()
                            && cel.name() == "code"
                            && let Some(cls) = cel.attr("class")
                        {
                            return cls
                                .split_whitespace()
                                .find_map(|c| c.strip_prefix("language-"))
                                .map(|l| l.to_string());
                        }
                        None
                    })
                } else {
                    None
                };

                // Extract CSS styles from block-level elements
                let css = if is_block_tag {
                    el.attr("style").map(parse_block_styles).unwrap_or_default()
                } else {
                    BlockStyles::default()
                };

                if tag == "table" {
                    // Parse table structure into a ParsedTable
                    let (mut parsed_table, own_cells) =
                        parse_table_element(node, &new_state, depth, limit);
                    if !parsed_table.rows.is_empty() {
                        parsed_table.blockquote_depth = bq_depth;
                        push_table(elements, parsed_table, own_cells);
                    }
                    return;
                }

                if tag == "br" {
                    // <br> creates a new block
                    elements.push(ParsedElement::Block(ParsedBlock {
                        spans: vec![ParsedSpan {
                            text: String::new(),
                            ..Default::default()
                        }],
                        heading_level: None,
                        list_style: None,
                        list_indent: 0,
                        list_prefix: String::new(),
                        list_suffix: String::new(),
                        list_start: None,
                        marker: None,
                        is_code_block: false,
                        code_language: None,
                        blockquote_depth: bq_depth,
                        blockquote_opens: 0,
                        line_height: None,
                        non_breakable_lines: None,
                        page_break_before: None,
                        front_matter: None,
                        direction: None,
                        background_color: None,
                        alignment: None,
                        top_margin: None,
                        text_indent: None,
                        semantic_role: None,
                    }));
                    return;
                }

                if tag == "blockquote" {
                    let first = elements.len();
                    // A quotation holding inline content alone is one paragraph of it, as
                    // a `<p>` is, its `<br>`s line breaks: verse is pasted from the web as
                    // `<blockquote>a line<br>the next</blockquote>`. Walked child by child,
                    // each line was a paragraph of its own and each `<br>` an empty one.
                    let inline_only = node.children().all(|child| match child.value() {
                        Node::Element(el) => !matches!(
                            el.name(),
                            "p" | "div"
                                | "h1"
                                | "h2"
                                | "h3"
                                | "h4"
                                | "h5"
                                | "h6"
                                | "li"
                                | "pre"
                                | "blockquote"
                                | "ul"
                                | "ol"
                                | "table"
                        ),
                        _ => true,
                    });
                    if inline_only {
                        let mut spans: Vec<ParsedSpan> = Vec::new();
                        let mut nested_elements: Vec<ParsedElement> = Vec::new();
                        collect_inline_spans(
                            node,
                            &new_state,
                            &mut spans,
                            &new_list,
                            &mut nested_elements,
                            bq_depth,
                            new_list_depth,
                            depth + 1,
                            limit,
                        );
                        collapse_inline_whitespace(&mut spans);
                        if spans_carry_content(&spans) {
                            elements.push(ParsedElement::Block(ParsedBlock {
                                spans,
                                blockquote_depth: bq_depth,
                                blockquote_opens: 0,
                                ..ParsedBlock::default()
                            }));
                        }
                        elements.append(&mut nested_elements);
                    } else {
                        // A container: recurse into children with increased depth.
                        for child in node.children() {
                            walk_node(
                                child,
                                &new_state,
                                elements,
                                &new_list,
                                bq_depth,
                                new_list_depth,
                                depth + 1,
                                limit,
                            );
                        }
                    }
                    mark_quotation_opened(elements, first);
                } else if is_block_tag && tag != "br" {
                    // Start collecting spans for a new block.
                    // Use a temporary buffer so that nested block-level
                    // elements (e.g. sub-lists inside <li>) are collected
                    // separately and appended *after* the parent block.
                    let mut spans: Vec<ParsedSpan> = Vec::new();
                    let mut nested_elements: Vec<ParsedElement> = Vec::new();
                    collect_inline_spans(
                        node,
                        &new_state,
                        &mut spans,
                        &new_list,
                        &mut nested_elements,
                        bq_depth,
                        new_list_depth,
                        depth + 1,
                        limit,
                    );

                    let (list_style_for_block, list_start_for_block) = if tag == "li" {
                        new_list.clone()
                    } else {
                        (None, None)
                    };

                    let list_indent_for_block = if tag == "li" {
                        new_list_depth.saturating_sub(1)
                    } else {
                        0
                    };

                    if !(is_code_block || css.preserve_whitespace == Some(true)) {
                        collapse_inline_whitespace(&mut spans);
                    }

                    // Whitespace between block-level children is layout, not
                    // content. `<body>` holds a newline after every `</p>` an
                    // exporter writes, and keeping those as the container's own
                    // inline run is what closed a pasted book with a paragraph
                    // of four thousand blank lines. A block that produced no
                    // children keeps its empty run instead: `<p> </p>` is an
                    // empty paragraph the writer meant to be there.
                    let own_run_is_content =
                        spans_carry_content(&spans) || nested_elements.is_empty();

                    if (!spans.is_empty() && own_run_is_content) || heading_level.is_some() {
                        // A code block is verbatim text, which no save can write an image or
                        // a note's reference into: a `<pre>` holding one is read as prose,
                        // which keeps it, one paragraph for each of its lines (see below).
                        let is_code_block = is_code_block
                            && !spans
                                .iter()
                                .any(|span| span.image.is_some() || span.footnote_ref.is_some());
                        let list_style_for_block_is_some = list_style_for_block.is_some();
                        let block = ParsedBlock {
                            spans,
                            heading_level,
                            list_suffix: list_style_for_block
                                .as_ref()
                                .map(|style| default_list_suffix(style).to_string())
                                .unwrap_or_default(),
                            list_style: list_style_for_block,
                            list_indent: list_indent_for_block,
                            list_prefix: String::new(),
                            list_start: list_start_for_block,
                            // Only a list item has a box: an `<li>` outside any list is read
                            // as a paragraph (see `is_inline_only`) and has none to keep.
                            marker: if list_style_for_block_is_some {
                                html_item_checkbox(node)
                            } else {
                                None
                            },
                            is_code_block,
                            code_language: code_language.filter(|_| is_code_block),
                            blockquote_depth: bq_depth,
                            blockquote_opens: 0,
                            line_height: css.line_height,
                            non_breakable_lines: css.non_breakable_lines,
                            page_break_before: css.page_break_before,
                            front_matter: None,
                            direction: css.direction,
                            background_color: css.background_color,
                            alignment: None,
                            top_margin: None,
                            text_indent: None,
                            semantic_role: None,
                        };
                        // Kept whitespace keeps its line breaks, and a line break in
                        // prose is a new block to the model. Only a code block holds
                        // its lines in one block.
                        if is_code_block {
                            elements.push(ParsedElement::Block(block));
                        } else {
                            elements.extend(
                                split_block_at_line_breaks(block)
                                    .into_iter()
                                    .map(ParsedElement::Block),
                            );
                        }
                    }
                    // Append nested block elements after the parent block
                    elements.append(&mut nested_elements);
                } else if matches!(tag, "ul" | "ol" | "thead" | "tbody" | "tr") {
                    // Container elements: recurse into children
                    for child in node.children() {
                        walk_node(
                            child,
                            &new_state,
                            elements,
                            &new_list,
                            bq_depth,
                            new_list_depth,
                            depth + 1,
                            limit,
                        );
                    }
                } else {
                    // Inline element or unknown: recurse
                    for child in node.children() {
                        walk_node(
                            child,
                            &new_state,
                            elements,
                            current_list,
                            bq_depth,
                            list_depth,
                            depth + 1,
                            limit,
                        );
                    }
                }
            }
            Node::Text(text) => {
                // Bare text not in a block (a list's text outside any item, `<ol>a</ol>`)
                // is a paragraph, its whitespace collapsed as a paragraph's is. Only
                // trimmed, a tab in it reached the model, and a newline, which separates
                // two blocks there, and the HTML written from it read back otherwise.
                //
                // Blanks alone are the layout between blocks, whitespace kept or not.
                let mut spans = vec![text_span(text.text.to_string(), state)];
                collapse_inline_whitespace(&mut spans);
                if spans_carry_content(&spans) && !text.chars().all(is_html_space) {
                    let block = ParsedBlock {
                        spans,
                        blockquote_depth,
                        blockquote_opens: 0,
                        ..ParsedBlock::default()
                    };
                    elements.extend(
                        split_block_at_line_breaks(block)
                            .into_iter()
                            .map(ParsedElement::Block),
                    );
                }
            }
            _ => {
                // Document, Comment, etc. — recurse children
                for child in node.children() {
                    walk_node(
                        child,
                        state,
                        elements,
                        current_list,
                        blockquote_depth,
                        list_depth,
                        depth + 1,
                        limit,
                    );
                }
            }
        }
    }

    /// Collect inline spans from a block-level element's children.
    /// If a nested block-level element is encountered, it is flushed as a
    /// separate block.
    #[allow(clippy::too_many_arguments)]
    fn collect_inline_spans(
        node: ego_tree::NodeRef<Node>,
        state: &FmtState,
        spans: &mut Vec<ParsedSpan>,
        current_list: &HtmlList,
        elements: &mut Vec<ParsedElement>,
        blockquote_depth: u32,
        list_depth: u32,
        depth: usize,
        limit: usize,
    ) {
        if depth > limit {
            let text = subtree_text(node);
            if !text.is_empty() {
                spans.push(text_span(text, state));
            }
            return;
        }
        for child in node.children() {
            match child.value() {
                Node::Text(text) => {
                    let t = text.text.to_string();
                    // The blanks between two blocks are layout, even where whitespace is kept.
                    let blank = if state.preserve_whitespace {
                        blank_text_kind(&t, child)
                    } else {
                        BlankText::Content
                    };
                    if blank == BlankText::Boundary {
                        // One line break where whitespace is kept: the block is split there,
                        // as it is at any other newline. None when nothing comes before it
                        // in the run (the elements before it hold blocks of their own), or
                        // when the line has ended already; one that ends the run is no line
                        // of its own, as a browser shows it.
                        if spans.last().is_some_and(|last| !last.text.ends_with('\n')) {
                            spans.push(text_span("\n".to_string(), state));
                        }
                    } else if !t.is_empty() && blank == BlankText::Content {
                        spans.push(ParsedSpan {
                            text: t,
                            bold: state.bold,
                            italic: state.italic,
                            underline: state.underline,
                            strikeout: state.strikeout,
                            code: state.code,
                            superscript: state.superscript,
                            subscript: state.subscript,
                            link_href: state.link_href.clone(),
                            image: None,
                            footnote_ref: None,
                            preserve_whitespace: state.preserve_whitespace,
                        });
                    }
                }
                Node::Element(el) => {
                    let tag = el.name();
                    if is_metadata_tag(tag) {
                        continue;
                    }
                    let mut new_state = state.clone();
                    if let Some(keep) = element_keeps_whitespace(el, state.sheet.as_deref()) {
                        new_state.preserve_whitespace = keep;
                    }

                    match tag {
                        // Checked by attribute rather than by tag name: the
                        // producer picks the element (a `<sup>` renders sensibly
                        // in a browser), and only the attribute is a contract.
                        _ if el.attr(HTML_FOOTNOTE_ATTR).is_some() => {
                            if let Some(span) = html_footnote_span(el, new_state.link_href.clone())
                            {
                                spans.push(span);
                            }
                            continue;
                        }
                        "b" | "strong" => new_state.bold = true,
                        "i" | "em" => new_state.italic = true,
                        "u" | "ins" => new_state.underline = true,
                        "s" | "del" | "strike" => new_state.strikeout = true,
                        "code" => new_state.code = true,
                        "sup" => new_state.superscript = true,
                        "sub" => new_state.subscript = true,
                        "a" => {
                            if let Some(href) = el.attr("href") {
                                new_state.link_href = Some(href.to_string());
                            }
                        }
                        "img" => {
                            if let Some(span) = html_img_span(el, new_state.link_href.clone()) {
                                spans.push(span);
                            }
                            continue;
                        }
                        _ => {}
                    }

                    // Check for nested block elements
                    let nested_block = matches!(
                        tag,
                        "p" | "div"
                            | "h1"
                            | "h2"
                            | "h3"
                            | "h4"
                            | "h5"
                            | "h6"
                            | "li"
                            | "pre"
                            | "blockquote"
                            | "ul"
                            | "ol"
                    );

                    if tag == "br" {
                        // A line break inside the block, in the formatting around it.
                        // `collapse_inline_whitespace` then drops the spaces either side,
                        // as a browser lays them out.
                        spans.push(text_span(LINE_SEPARATOR_STR.to_string(), state));
                    } else if nested_block || tag == "table" {
                        // Flush as separate element
                        walk_node(
                            child,
                            &new_state,
                            elements,
                            current_list,
                            blockquote_depth,
                            list_depth,
                            depth + 1,
                            limit,
                        );
                    } else {
                        // Inline element: recurse
                        collect_inline_spans(
                            child,
                            &new_state,
                            spans,
                            current_list,
                            elements,
                            blockquote_depth,
                            list_depth,
                            depth + 1,
                            limit,
                        );
                    }
                }
                _ => {}
            }
        }
    }

    // What the sheet says of `<body>` and `<html>` is where the walk starts: the HTML parser
    // drops both elements from a fragment, so no element is left to carry it. Bare text
    // outside any element inherits only that.
    let sheet = html_sheet(fragment).map(std::rc::Rc::new);
    let initial_state = FmtState {
        preserve_whitespace: sheet
            .as_deref()
            .and_then(SheetWhitespace::initial_keeps_whitespace)
            .unwrap_or(false),
        sheet,
        ..FmtState::default()
    };
    // Treat the root element as a block-level container so that
    // top-level inline elements (e.g. `<b>Bold</b> <em>Italic</em>`)
    // are grouped into a single block instead of becoming separate blocks.
    let mut root_spans: Vec<ParsedSpan> = Vec::new();
    collect_inline_spans(
        *root,
        &initial_state,
        &mut root_spans,
        &(None, None),
        &mut elements,
        0,
        0,
        0,
        depth_limit,
    );
    collapse_inline_whitespace(&mut root_spans);
    // Only when something survived: the root's direct text children are the
    // newlines between top-level blocks in every document an exporter writes.
    if spans_carry_content(&root_spans) {
        // Text that kept its line breaks is one block for each line, as for any other block.
        let root_block = ParsedBlock {
            spans: root_spans,
            heading_level: None,
            list_style: None,
            list_indent: 0,
            list_prefix: String::new(),
            list_suffix: String::new(),
            list_start: None,
            marker: None,
            is_code_block: false,
            code_language: None,
            blockquote_depth: 0,
            blockquote_opens: 0,
            line_height: None,
            non_breakable_lines: None,
            page_break_before: None,
            front_matter: None,
            direction: None,
            background_color: None,
            alignment: None,
            top_margin: None,
            text_indent: None,
            semantic_role: None,
        };
        elements.extend(
            split_block_at_line_breaks(root_block)
                .into_iter()
                .map(ParsedElement::Block),
        );
    }

    // If no elements were parsed, create a single empty paragraph
    if elements.is_empty() {
        elements.push(ParsedElement::Block(ParsedBlock {
            spans: vec![ParsedSpan {
                text: String::new(),
                ..Default::default()
            }],
            heading_level: None,
            list_style: None,
            list_indent: 0,
            list_prefix: String::new(),
            list_suffix: String::new(),
            list_start: None,
            marker: None,
            is_code_block: false,
            code_language: None,
            blockquote_depth: 0,
            blockquote_opens: 0,
            line_height: None,
            non_breakable_lines: None,
            page_break_before: None,
            front_matter: None,
            direction: None,
            background_color: None,
            alignment: None,
            top_margin: None,
            text_indent: None,
            semantic_role: None,
        }));
    }

    elements
}

/// Convert a `ParsedSpan` (parser output) into the `CharacterFormat` used by
/// `FormatRun`. `is_code_block` forces `monospace` as the font family for
/// every span inside a code block.
pub fn character_format_from_span(
    span: &ParsedSpan,
    is_code_block: bool,
) -> crate::format_runs::CharacterFormat {
    use crate::entities::CharVerticalAlignment;
    crate::format_runs::CharacterFormat {
        font_bold: if span.bold { Some(true) } else { None },
        font_italic: if span.italic { Some(true) } else { None },
        font_underline: if span.underline { Some(true) } else { None },
        font_strikeout: if span.strikeout { Some(true) } else { None },
        font_family: if span.code || is_code_block {
            Some("monospace".to_string())
        } else {
            None
        },
        anchor_href: span.link_href.clone(),
        is_anchor: if span.link_href.is_some() {
            Some(true)
        } else {
            None
        },
        vertical_alignment: if span.superscript {
            Some(CharVerticalAlignment::SuperScript)
        } else if span.subscript {
            Some(CharVerticalAlignment::SubScript)
        } else {
            None
        },
        ..Default::default()
    }
}

/// Translate a slice of parsed spans into `(plain_text, format_runs)`.
///
/// One non-default span yields one `FormatRun`; spans with empty
/// `CharacterFormat` (no decoration, no link, no code) emit no run, since an
/// absent run means "inherit default formatting" in the new model. Adjacent
/// runs with identical formats are coalesced via `coalesce_in_place` so the
/// resulting vector satisfies `debug_assert_well_formed`.
///
/// Returns the concatenated `plain_text` of all spans and a sorted,
/// non-overlapping, coalesced `Vec<FormatRun>`. Both safe to feed straight
/// into the store under the dual-write bridge.
pub fn format_runs_from_spans(spans: &[ParsedSpan], is_code_block: bool) -> ParsedInline {
    use crate::format_runs::{
        CharacterFormat, FootnoteRefAnchor, FormatRun, ImageAnchor, coalesce_in_place,
    };

    let mut plain_text = String::new();
    let mut runs: Vec<FormatRun> = Vec::new();
    let mut images: Vec<ImageAnchor> = Vec::new();
    let mut footnote_refs: Vec<FootnoteRefAnchor> = Vec::new();
    let default = CharacterFormat::default();

    for span in spans {
        let byte_start = plain_text.len() as u32;

        if let Some(label) = &span.footnote_ref {
            // A reference occupies one U+FFFC, exactly as an image does, so
            // every downstream offset treats the two alike.
            plain_text.push('\u{FFFC}');
            // Raised, always — whatever the surrounding run is doing.
            //
            // A footnote marker is superscript by definition, in every
            // typographic tradition and in every reader that renders djot. The
            // ambient `superscript` flag the span carries is the *prose's*, and
            // prose is not superscript, so taking it verbatim sets a note's
            // number on the baseline in the middle of a sentence — which reads
            // as a stray digit the writer typed rather than as a reference.
            //
            // It is set here, on the anchor, rather than at render time so that
            // every consumer agrees: the editor raises it, the exporters that
            // carry character formatting carry it, and the djot writer knows to
            // emit `[^label]` *without* wrapping it in `^…^` (see
            // `a_reference_is_not_wrapped_in_superscript_markers`).
            let mut format = character_format_from_span(span, is_code_block);
            format.vertical_alignment = Some(crate::entities::CharVerticalAlignment::SuperScript);
            footnote_refs.push(FootnoteRefAnchor {
                byte_offset: byte_start,
                label: label.clone(),
                format,
            });
            continue;
        }

        if let Some(image) = &span.image {
            // An image occupies one U+FFFC in the text, exactly as
            // `insert_image` mirrors into the rope, so every downstream offset
            // calculation treats a parsed image and an inserted one alike.
            plain_text.push('\u{FFFC}');
            images.push(ImageAnchor {
                byte_offset: byte_start,
                name: image.src.clone(),
                alt: image.alt.clone(),
                width: image.width,
                height: image.height,
                quality: 100,
                format: character_format_from_span(span, is_code_block),
            });
            continue;
        }

        plain_text.push_str(&span.text);
        let byte_end = plain_text.len() as u32;
        if byte_start == byte_end {
            continue;
        }
        let format = character_format_from_span(span, is_code_block);
        if format == default {
            continue;
        }
        runs.push(FormatRun {
            byte_start,
            byte_end,
            format,
        });
    }
    coalesce_in_place(&mut runs);
    ParsedInline {
        plain_text,
        runs,
        images,
        footnote_refs,
    }
}

/// The three parallel things a block stores, as recovered from parsed spans.
///
/// Returned as a struct rather than a tuple because it grew a third member
/// (images) after nine call sites already destructured a pair — and every one
/// of those sites has to decide what to do with images, so a silent
/// tuple-arity change would have been the wrong kind of easy.
#[derive(Debug, Clone, Default)]
pub struct ParsedInline {
    pub plain_text: String,
    pub runs: Vec<crate::format_runs::FormatRun>,
    pub images: Vec<crate::format_runs::ImageAnchor>,
    pub footnote_refs: Vec<crate::format_runs::FootnoteRefAnchor>,
}

// ─── Djot parsing ────────────────────────────────────────────────────

/// Map a jotdown unordered/task bullet marker to a model `ListStyle`.
///
/// The mapping is a stable bijection (`-`↔Disc, `*`↔Circle, `+`↔Square) so the
/// djot exporter can recover the exact bullet character for a lossless
/// round-trip.
fn djot_bullet_style(b: jotdown::ListBulletType) -> ListStyle {
    use jotdown::ListBulletType as B;
    match b {
        B::Dash => ListStyle::Disc,
        B::Star => ListStyle::Circle,
        B::Plus => ListStyle::Square,
    }
}

/// Map a jotdown ordered-list numbering scheme to a model `ListStyle`.
fn djot_ordered_style(n: jotdown::OrderedListNumbering) -> ListStyle {
    use jotdown::OrderedListNumbering as N;
    match n {
        N::Decimal => ListStyle::Decimal,
        N::AlphaLower => ListStyle::LowerAlpha,
        N::AlphaUpper => ListStyle::UpperAlpha,
        N::RomanLower => ListStyle::LowerRoman,
        N::RomanUpper => ListStyle::UpperRoman,
    }
}

/// Map a jotdown ordered-list delimiter to the `(prefix, suffix)` affixes
/// stored on the `List` entity (`1.` → `("", ".")`, `1)` → `("", ")")`,
/// `(1)` → `("(", ")")`).
fn djot_ordered_affixes(style: jotdown::OrderedListStyle) -> (String, String) {
    use jotdown::OrderedListStyle as S;
    match style {
        S::Period => (String::new(), ".".to_string()),
        S::Paren => (String::new(), ")".to_string()),
        S::ParenParen => ("(".to_string(), ")".to_string()),
    }
}

/// Optional block-level style attributes carried on a djot block through its
/// `{key=value}` block attributes. All `None` when the block has no such
/// attributes (or they were filtered out by [`DjotImportOptions`]).
#[derive(Debug, Clone, Default)]
struct DjotBlockStyle {
    alignment: Option<Alignment>,
    line_height: Option<i64>,
    non_breakable_lines: Option<bool>,
    page_break_before: Option<bool>,
    front_matter: Option<bool>,
    direction: Option<TextDirection>,
    background_color: Option<String>,
    top_margin: Option<i64>,
    text_indent: Option<i64>,
    semantic_role: Option<SemanticRole>,
}

impl DjotBlockStyle {
    /// Overlay the `Some` fields of `other` onto `self`, leaving `self`'s
    /// existing values for any field `other` does not set. Used to combine a
    /// heading's enclosing-`Section` attributes with any on the heading itself.
    fn merge_from(&mut self, other: DjotBlockStyle) {
        if other.alignment.is_some() {
            self.alignment = other.alignment;
        }
        if other.line_height.is_some() {
            self.line_height = other.line_height;
        }
        if other.non_breakable_lines.is_some() {
            self.non_breakable_lines = other.non_breakable_lines;
        }
        if other.page_break_before.is_some() {
            self.page_break_before = other.page_break_before;
        }
        if other.front_matter.is_some() {
            self.front_matter = other.front_matter;
        }
        if other.direction.is_some() {
            self.direction = other.direction;
        }
        if other.background_color.is_some() {
            self.background_color = other.background_color;
        }
        if other.top_margin.is_some() {
            self.top_margin = other.top_margin;
        }
        if other.text_indent.is_some() {
            self.text_indent = other.text_indent;
        }
        if other.semantic_role.is_some() {
            self.semantic_role = other.semantic_role.clone();
        }
    }
}

/// Read the round-tripped block-style attributes off a djot block's
/// [`jotdown::Attributes`], honouring the import [`DjotImportOptions`]. Keys are
/// the model field names (`alignment`, `line_height`, `direction`,
/// `non_breakable_lines`, `page_break_before`, `front_matter`, `background_color`,
/// `top_margin`, `text_indent`, `semantic_role`); unrecognised values are ignored.
fn block_attrs_to_style(attrs: &jotdown::Attributes, opts: &DjotImportOptions) -> DjotBlockStyle {
    let mut style = DjotBlockStyle::default();

    if opts.alignment
        && let Some(v) = attrs.get_value("alignment")
    {
        style.alignment = match v.to_string().as_str() {
            "left" => Some(Alignment::Left),
            "right" => Some(Alignment::Right),
            "center" => Some(Alignment::Center),
            "justify" => Some(Alignment::Justify),
            _ => None,
        };
    }
    if opts.line_height
        && let Some(v) = attrs.get_value("line_height")
    {
        style.line_height = v.to_string().parse::<i64>().ok();
    }
    if opts.direction
        && let Some(v) = attrs.get_value("direction")
    {
        style.direction = match v.to_string().as_str() {
            "ltr" => Some(TextDirection::LeftToRight),
            "rtl" => Some(TextDirection::RightToLeft),
            _ => None,
        };
    }
    if opts.non_breakable_lines
        && let Some(v) = attrs.get_value("non_breakable_lines")
    {
        style.non_breakable_lines = match v.to_string().as_str() {
            "true" => Some(true),
            "false" => Some(false),
            _ => None,
        };
    }
    if opts.page_break_before
        && let Some(v) = attrs.get_value("page_break_before")
    {
        style.page_break_before = match v.to_string().as_str() {
            "true" => Some(true),
            "false" => Some(false),
            _ => None,
        };
    }
    if opts.front_matter
        && let Some(v) = attrs.get_value("front_matter")
    {
        style.front_matter = match v.to_string().as_str() {
            "true" => Some(true),
            "false" => Some(false),
            _ => None,
        };
    }
    if opts.background_color
        && let Some(v) = attrs.get_value("background_color")
    {
        style.background_color = Some(v.to_string());
    }
    if opts.top_margin
        && let Some(v) = attrs.get_value("top_margin")
    {
        style.top_margin = v.to_string().parse::<i64>().ok();
    }
    if opts.text_indent
        && let Some(v) = attrs.get_value("text_indent")
    {
        style.text_indent = v.to_string().parse::<i64>().ok();
    }
    if opts.semantic_role
        && let Some(v) = attrs.get_value("semantic_role")
    {
        style.semantic_role = match v.to_string().as_str() {
            "epigraph" => Some(SemanticRole::Epigraph),
            // An unknown role is dropped, not guessed at — the same way an unknown
            // alignment value above is. A future role read by an older build then
            // degrades to a plain blockquote, which is what it looks like anyway.
            _ => None,
        };
    }

    style
}

/// The start a list keeps on its entity, from the number its first item is written with:
/// `None` for 1, which every list starts at unless it says otherwise, and a number past what
/// every format holds read as the largest it holds (see `list_start`).
fn list_start_of(start: u64) -> Option<i64> {
    crate::parser_tools::list_start::list_start_from_unsigned(start)
}

/// Push a finished block into `elements`, applying the djot block-level fields
/// plus any round-tripped block-style attributes carried in `style`.
#[allow(clippy::too_many_arguments)]
fn djot_push_block(
    elements: &mut Vec<ParsedElement>,
    spans: Vec<ParsedSpan>,
    heading_level: Option<i64>,
    list_style: Option<ListStyle>,
    list_indent: u32,
    list_prefix: String,
    list_suffix: String,
    list_start: Option<i64>,
    marker: Option<MarkerType>,
    is_code_block: bool,
    code_language: Option<String>,
    blockquote_depth: u32,
    style: DjotBlockStyle,
) {
    elements.push(ParsedElement::Block(ParsedBlock {
        spans,
        heading_level,
        list_style,
        list_indent,
        list_prefix,
        list_suffix,
        list_start,
        marker,
        is_code_block,
        code_language,
        blockquote_depth,
        blockquote_opens: 0,
        line_height: style.line_height,
        non_breakable_lines: style.non_breakable_lines,
        page_break_before: style.page_break_before,
        front_matter: style.front_matter,
        direction: style.direction,
        background_color: style.background_color,
        alignment: style.alignment,
        top_margin: style.top_margin,
        text_indent: style.text_indent,
        semantic_role: style.semantic_role.clone(),
    }));
}

/// A Djot document whose structure cannot be computed, as its source: one plain
/// paragraph for each line that holds text, with that line's text as it is written, its
/// leading and trailing whitespace aside.
///
/// A paragraph a line is what keeps the text the same through a save. The Djot writer
/// escapes whatever a paragraph opens with and whatever in it would read as markup, so
/// the saved text reads back as these same paragraphs. Shown as one paragraph, the
/// source kept its line breaks through a save, read back as the same document the parser
/// could not take, and every save escaped its backslashes once more. Blank lines are left
/// out because the reader keeps no block for an empty paragraph, and a line's outer
/// whitespace because it is the source's indentation, which the parser does not read as
/// text in a line it can take either.
fn djot_source_lines(djot: &str) -> Vec<ParsedElement> {
    djot.lines()
        .map(|line| line.trim_matches(|c: char| c.is_ascii_whitespace()))
        .filter(|line| !line.is_empty())
        .map(|line| {
            ParsedElement::Block(ParsedBlock {
                spans: vec![ParsedSpan {
                    text: line.to_string(),
                    ..Default::default()
                }],
                ..Default::default()
            })
        })
        .collect()
}

/// Parse djot source into the shared [`ParsedElement`] intermediate, mirroring
/// [`parse_markdown`]. Uses the [`jotdown`] pull parser.
///
/// Constructs the document model cannot represent are dropped, and their text
/// content is discarded so it never leaks into the document: footnotes, math,
/// fenced divs, raw blocks/inline, thematic breaks, description lists,
/// captions, symbols, link-reference definitions, and highlight/`mark`. Inline
/// images keep their alt text as plain text (the image itself is not modelled),
/// matching the Markdown importer. Smart-punctuation events are normalised to
/// their canonical Unicode characters so the model→djot→model round-trip is a
/// fixpoint.
///
/// Standalone paragraphs and headings additionally carry the optional
/// block-style attributes selected by `options` — paragraph alignment, line
/// height, text direction, non-breakable lines and background color — read from
/// djot `{key=value}` block attributes (see [`DjotImportOptions`]). List items,
/// code blocks and table cells normalise their block styling away.
///
/// Known model limitations (normalised, not preserved on round-trip):
/// table column alignment, and list tight/loose.
///
/// A CRLF line ending is read as the LF it stands for, so a source with Windows line endings
/// reads as the same text with Unix ones. A lone carriage return is kept as text: it is how
/// [`plain_text_to_djot`](crate::parser_tools::plain_text_to_djot) writes one, and what it
/// guarantees to give back.
pub fn parse_djot(djot: &str, options: &DjotImportOptions) -> Vec<ParsedElement> {
    use jotdown::{Container as C, Event as E, ListKind};

    let djot = crate::parser_tools::djot_depth::crlf_to_lf(djot);
    let djot: &str = &djot;

    // `jotdown` recurses with no limit of its own, once per nested container and
    // once per line of a paragraph that leaves an inline opener waiting, and a
    // stack overflow **aborts the process**: it cannot be caught by
    // `catch_unwind`, so there is no recovering from it after the fact and the
    // only safe move is not to start. It also panics on a heading deeper than it
    // can number, which a caller on an interface thread does not survive either.
    // Input is not always the author's own: a project bundle is mailed and
    // shared, and an imported document comes from whoever sent it.
    //
    // `djot_depth::parsable` is the door that keeps the parser within its limits,
    // and it degrades rather than refuses, so the signature stays the same and no
    // word is lost. Such a heading is escaped into text, as it was written. A
    // document nested too deeply by its indentation, which is how a deeply nested
    // list is written, is read with the indentation cut: the list comes out
    // flattened below a fixed depth, every item and word kept. This used to hand
    // back a pasted list nested two hundred levels as one paragraph of markup. A
    // long paragraph is parsed on a stack sized for it, and the block holding one
    // too long for any stack is shown as its lines, the rest of the document as it
    // was.
    //
    // A document still nested too deeply once flattened, or one the parser panicked
    // on, comes back as its lines, one plain paragraph each (see
    // `djot_source_lines`). Its structure is not computed, which is the honest
    // answer for a document whose structure cannot be computed without ending the
    // process, and its text is still there for the writer to see and repair.
    let parsable = crate::parser_tools::djot_depth::parsable(djot);
    let Some(events) = parsable.as_ref().and_then(|parsable| parsable.events()) else {
        return djot_source_lines(djot);
    };

    let mut elements: Vec<ParsedElement> = Vec::new();
    let mut current_spans: Vec<ParsedSpan> = Vec::new();
    let mut current_heading: Option<i64> = None;
    let mut is_code_block = false;
    let mut code_language: Option<String> = None;
    let mut blockquote_depth: u32 = 0;
    // Where `elements` stood as each open quotation began (see `mark_quotation_opened`).
    let mut quotations_open: Vec<usize> = Vec::new();
    // Block-style attributes captured from a standalone paragraph/heading's djot
    // `{…}` block attributes, consumed when that block is flushed.
    let mut pending_style = DjotBlockStyle::default();

    // Inline formatting state.
    let mut bold = false;
    let mut italic = false;
    let mut underline = false;
    let mut strikeout = false;
    let mut code = false;
    let mut superscript = false;
    let mut subscript = false;
    let mut link_href: Option<String> = None;
    // Set between an image's Start and End. Its alt text arrives as ordinary
    // `Str` events in between, so it has to be captured rather than emitted.
    let mut pending_image: Option<ParsedImage> = None;

    // List nesting: each entry is (style, prefix, suffix, start); depth = indent + 1.
    let mut list_stack: Vec<(ListStyle, String, String, Option<i64>)> = Vec::new();
    // Context applied to the next flushed block while inside a list item.
    let mut cur_list_style: Option<ListStyle> = None;
    let mut cur_list_prefix = String::new();
    let mut cur_list_suffix = String::new();
    let mut cur_list_start: Option<i64> = None;
    let mut cur_list_indent: u32 = 0;
    let mut cur_marker: Option<MarkerType> = None;

    // Table accumulation.
    let mut in_table_cell = false;
    let mut table_rows: Vec<Vec<ParsedTableCell>> = Vec::new();
    let mut current_row: Vec<ParsedTableCell> = Vec::new();
    let mut current_cell_spans: Vec<ParsedSpan> = Vec::new();
    let mut table_header_rows: usize = 0;
    let mut row_is_head = false;

    // Subtree-skip depth for unrepresentable containers (their entire content
    // is dropped). Incremented on the dropped container's `Start` and on every
    // nested `Start`; decremented on every `End`.
    let mut skip_depth: u32 = 0;

    // The label and element index of the footnote definition currently open,
    // if any. Djot has no footnote inside a footnote, so one slot is enough
    // where the dropped containers need a depth counter.
    let mut footnote_open: Option<(String, usize)> = None;

    // Push one inline span carrying the current formatting state into the
    // active sink (table cell or block). A macro (not a closure) to avoid
    // borrowing `current_spans`/`current_cell_spans` across the formatting
    // state reads.
    macro_rules! push_text {
        ($t:expr) => {{
            // Alt text belongs to the image, not to the paragraph. While an
            // image is open every text event is diverted into its description,
            // which is what keeps a photo's caption out of the manuscript's
            // word count and out of the search corpus.
            if let Some(img) = pending_image.as_mut() {
                img.alt.push_str(($t).as_ref());
            } else {
                let sp = ParsedSpan {
                    text: ($t).to_string(),
                    bold,
                    italic,
                    underline,
                    strikeout,
                    code,
                    superscript,
                    subscript,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: None,
                    preserve_whitespace: false,
                };
                if in_table_cell {
                    current_cell_spans.push(sp);
                } else {
                    current_spans.push(sp);
                }
            }
        }};
    }

    // Push a completed inline image span into the active sink.
    macro_rules! push_image {
        ($img:expr) => {{
            let sp = ParsedSpan {
                text: String::new(),
                bold,
                italic,
                underline,
                strikeout,
                code,
                superscript,
                subscript,
                link_href: link_href.clone(),
                image: Some($img),
                footnote_ref: None,
                preserve_whitespace: false,
            };
            if in_table_cell {
                current_cell_spans.push(sp);
            } else {
                current_spans.push(sp);
            }
        }};
    }

    // Enter a list item, flushing any unterminated inline content first and
    // capturing the list context + task marker for the item's block.
    macro_rules! enter_item {
        ($marker:expr) => {{
            if !current_spans.is_empty() {
                djot_push_block(
                    &mut elements,
                    std::mem::take(&mut current_spans),
                    None,
                    cur_list_style.clone(),
                    cur_list_indent,
                    cur_list_prefix.clone(),
                    cur_list_suffix.clone(),
                    cur_list_start,
                    cur_marker.clone(),
                    false,
                    None,
                    blockquote_depth,
                    DjotBlockStyle::default(),
                );
            }
            let (style, prefix, suffix, start) = list_stack.last().cloned().unwrap_or((
                ListStyle::Disc,
                String::new(),
                String::new(),
                None,
            ));
            cur_list_style = Some(style);
            cur_list_prefix = prefix;
            cur_list_suffix = suffix;
            cur_list_start = start;
            cur_list_indent = list_stack.len().saturating_sub(1) as u32;
            cur_marker = $marker;
        }};
    }

    // A hard break: a line break inside the block, [`LINE_SEPARATOR`] in its text, so the
    // next line is more of the same paragraph, list item, quotation or heading. A table
    // cell is one line, and an image's description is one too, so there it is a space.
    macro_rules! line_break {
        () => {{
            if in_table_cell || pending_image.is_some() {
                push_text!(" ");
            } else {
                push_text!(LINE_SEPARATOR_STR);
            }
        }};
    }

    // A line ending inside a code span (see `E::Str` below): a new block, with the list,
    // quotation, heading and block style the line was in. A table cell is one line, so
    // there it is a space.
    //
    // The page break is the one style that stays with the first line: it is where the
    // block starts, and copied to every line it put each on a page of its own.
    macro_rules! split_line {
        () => {{
            if in_table_cell {
                push_text!(" ");
            } else if !current_spans.is_empty() && current_heading.is_some() {
                // The line before a hard break in a heading is a line of that
                // heading, as the line after it is when the heading ends. Read as
                // a paragraph, a heading written over two lines came back as a
                // paragraph and a heading.
                djot_push_block(
                    &mut elements,
                    std::mem::take(&mut current_spans),
                    current_heading,
                    None,
                    0,
                    String::new(),
                    String::new(),
                    None,
                    None,
                    false,
                    None,
                    blockquote_depth,
                    pending_style.clone(),
                );
                pending_style.page_break_before = None;
            } else if !current_spans.is_empty() {
                djot_push_block(
                    &mut elements,
                    std::mem::take(&mut current_spans),
                    None,
                    cur_list_style.clone(),
                    cur_list_indent,
                    cur_list_prefix.clone(),
                    cur_list_suffix.clone(),
                    cur_list_start,
                    cur_marker.clone(),
                    is_code_block,
                    code_language.clone(),
                    blockquote_depth,
                    pending_style.clone(),
                );
                pending_style.page_break_before = None;
            }
        }};
    }

    for event in events {
        if skip_depth > 0 {
            match event {
                E::Start(..) => skip_depth += 1,
                E::End(_) => skip_depth -= 1,
                _ => {}
            }
            continue;
        }

        match event {
            // ── Transparent wrappers (unwrap, keep content) ──
            E::Start(C::Document, _) | E::End(C::Document) => {}
            E::Start(C::Section { .. }, attrs) => {
                // A heading's block attributes attach to its enclosing Section,
                // not the heading itself; capture them for the heading's flush.
                if list_stack.is_empty() {
                    pending_style.merge_from(block_attrs_to_style(&attrs, options));
                }
            }
            E::End(C::Section { .. }) => {}
            E::Start(C::Div { .. }, _) | E::End(C::Div { .. }) => {}

            // ── Blockquote ──
            E::Start(C::Blockquote, _) => {
                blockquote_depth += 1;
                quotations_open.push(elements.len());
            }
            E::End(C::Blockquote) => {
                blockquote_depth = blockquote_depth.saturating_sub(1);
                if let Some(first) = quotations_open.pop() {
                    mark_quotation_opened(&mut elements, first);
                }
            }

            // ── Lists ──
            E::Start(C::List { kind, .. }, _) => {
                let (style, prefix, suffix, start) = match kind {
                    ListKind::Unordered(b) | ListKind::Task(b) => {
                        (djot_bullet_style(b), String::new(), String::new(), None)
                    }
                    ListKind::Ordered {
                        numbering,
                        style,
                        start,
                    } => {
                        let (p, s) = djot_ordered_affixes(style);
                        (djot_ordered_style(numbering), p, s, list_start_of(start))
                    }
                };
                list_stack.push((style, prefix, suffix, start));
            }
            E::End(C::List { .. }) => {
                list_stack.pop();
                cur_list_style = None;
                cur_list_start = None;
                cur_marker = None;
            }
            E::Start(C::ListItem, _) => enter_item!(None),
            E::Start(C::TaskListItem { checked }, _) => enter_item!(Some(if checked {
                MarkerType::Checked
            } else {
                MarkerType::Unchecked
            })),
            E::End(C::ListItem) | E::End(C::TaskListItem { .. }) => {
                // Tight item without a wrapping paragraph (defensive flush).
                if !current_spans.is_empty() {
                    djot_push_block(
                        &mut elements,
                        std::mem::take(&mut current_spans),
                        None,
                        cur_list_style.clone(),
                        cur_list_indent,
                        cur_list_prefix.clone(),
                        cur_list_suffix.clone(),
                        cur_list_start,
                        cur_marker.clone(),
                        false,
                        None,
                        blockquote_depth,
                        DjotBlockStyle::default(),
                    );
                }
                cur_list_style = None;
                cur_list_start = None;
                cur_marker = None;
            }

            // ── Headings, paragraphs, code blocks ──
            E::Start(C::Heading { level, .. }, attrs) => {
                current_heading = Some(level as i64);
                // The block-style attributes live on the enclosing Section;
                // merge any placed directly on the heading without clearing them.
                pending_style.merge_from(block_attrs_to_style(&attrs, options));
            }
            E::End(C::Heading { .. }) => {
                djot_push_block(
                    &mut elements,
                    std::mem::take(&mut current_spans),
                    current_heading.take(),
                    None,
                    0,
                    String::new(),
                    String::new(),
                    None,
                    None,
                    false,
                    None,
                    blockquote_depth,
                    std::mem::take(&mut pending_style),
                );
            }
            E::Start(C::Paragraph, attrs) => {
                current_heading = None;
                // Block attributes only apply to standalone paragraphs;
                // list-item paragraphs normalise their styling away (matching
                // the exporter).
                pending_style = if list_stack.is_empty() {
                    block_attrs_to_style(&attrs, options)
                } else {
                    DjotBlockStyle::default()
                };
            }
            E::End(C::Paragraph) => {
                if !current_spans.is_empty() {
                    djot_push_block(
                        &mut elements,
                        std::mem::take(&mut current_spans),
                        None,
                        cur_list_style.clone(),
                        cur_list_indent,
                        cur_list_prefix.clone(),
                        cur_list_suffix.clone(),
                        cur_list_start,
                        cur_marker.clone(),
                        false,
                        None,
                        blockquote_depth,
                        std::mem::take(&mut pending_style),
                    );
                }
                cur_list_style = None;
                cur_list_start = None;
                cur_marker = None;
            }
            E::Start(C::CodeBlock { language }, _) => {
                is_code_block = true;
                code_language = if language.is_empty() {
                    None
                } else {
                    Some(language.to_string())
                };
            }
            E::End(C::CodeBlock { .. }) => {
                // Strip the single trailing newline jotdown appends.
                if let Some(last) = current_spans.last_mut()
                    && last.text.ends_with('\n')
                {
                    last.text.pop();
                }
                djot_push_block(
                    &mut elements,
                    std::mem::take(&mut current_spans),
                    None,
                    None,
                    0,
                    String::new(),
                    String::new(),
                    None,
                    None,
                    true,
                    code_language.take(),
                    blockquote_depth,
                    DjotBlockStyle::default(),
                );
                is_code_block = false;
            }

            // ── Tables ──
            E::Start(C::Table, _) => {
                table_rows.clear();
                current_row.clear();
                current_cell_spans.clear();
                table_header_rows = 0;
            }
            E::End(C::Table) => {
                let own_cells = cells_in_rows(&table_rows);
                push_table(
                    &mut elements,
                    ParsedTable {
                        header_rows: table_header_rows,
                        rows: std::mem::take(&mut table_rows),
                        blockquote_depth,
                        blockquote_opens: 0,
                    },
                    own_cells,
                );
            }
            E::Start(C::TableRow { head }, _) => {
                row_is_head = head;
                current_row.clear();
            }
            E::End(C::TableRow { .. }) => {
                if row_is_head {
                    table_header_rows += 1;
                }
                table_rows.push(std::mem::take(&mut current_row));
            }
            E::Start(C::TableCell { .. }, _) => {
                in_table_cell = true;
                current_cell_spans.clear();
            }
            E::End(C::TableCell { .. }) => {
                in_table_cell = false;
                current_row.push(ParsedTableCell {
                    spans: std::mem::take(&mut current_cell_spans),
                });
            }

            // ── Inline formatting ──
            E::Start(C::Strong, _) => bold = true,
            E::End(C::Strong) => bold = false,
            E::Start(C::Emphasis, _) => italic = true,
            E::End(C::Emphasis) => italic = false,
            E::Start(C::Verbatim, _) => code = true,
            E::End(C::Verbatim) => code = false,
            E::Start(C::Superscript, _) => superscript = true,
            E::End(C::Superscript) => superscript = false,
            E::Start(C::Subscript, _) => subscript = true,
            E::End(C::Subscript) => subscript = false,
            E::Start(C::Insert, _) => underline = true,
            E::End(C::Insert) => underline = false,
            E::Start(C::Delete, _) => strikeout = true,
            E::End(C::Delete) => strikeout = false,
            // Highlight/mark and bare spans have no model field — keep the text.
            E::Start(C::Mark, _) | E::End(C::Mark) => {}
            E::Start(C::Span, _) | E::End(C::Span) => {}
            E::Start(C::Link(dst, _), _) => link_href = Some(dst.to_string()),
            E::End(C::Link(..)) => link_href = None,
            // Inline images. Djot writes display size as inline attributes
            // (`![alt](src){width=800 height=600}`), which jotdown hands over
            // on the `Start` event — verified against jotdown 0.10, including
            // quoted values and images mid-sentence.
            E::Start(C::Image(src, _), attrs) => {
                let attr_num = |key: &str| -> i64 {
                    attrs
                        .get_value(key)
                        .map(|v| v.to_string())
                        .and_then(|v| v.trim().parse::<i64>().ok())
                        .filter(|n| *n > 0)
                        .unwrap_or(0)
                };
                pending_image = Some(ParsedImage {
                    // The writer percent-encodes what the lexer would act on in a
                    // source; the source is a name, so it is decoded back.
                    src: crate::parser_tools::djot_escape::decode_djot_image_source(&src),
                    alt: String::new(),
                    width: attr_num("width"),
                    height: attr_num("height"),
                });
            }
            E::End(C::Image(..)) => {
                if let Some(img) = pending_image.take() {
                    push_image!(img);
                }
            }

            // ── Footnote definitions ──
            //
            // The body is ordinary block content, so it is parsed by the
            // ordinary machinery: flush whatever inline run is open, note where
            // this definition's blocks start, and let them accumulate. `End`
            // lifts them back out. Nesting cannot occur — djot has no footnote
            // inside a footnote — so one mark suffices where the dropped
            // containers below need a depth counter.
            E::Start(C::Footnote { label }, _) => {
                if !current_spans.is_empty() {
                    djot_push_block(
                        &mut elements,
                        std::mem::take(&mut current_spans),
                        None,
                        cur_list_style.clone(),
                        cur_list_indent,
                        cur_list_prefix.clone(),
                        cur_list_suffix.clone(),
                        cur_list_start,
                        cur_marker.clone(),
                        false,
                        None,
                        blockquote_depth,
                        DjotBlockStyle::default(),
                    );
                }
                footnote_open = Some((label.to_string(), elements.len()));
            }
            E::End(C::Footnote { .. }) => {
                if !current_spans.is_empty() {
                    djot_push_block(
                        &mut elements,
                        std::mem::take(&mut current_spans),
                        None,
                        cur_list_style.clone(),
                        cur_list_indent,
                        cur_list_prefix.clone(),
                        cur_list_suffix.clone(),
                        cur_list_start,
                        cur_marker.clone(),
                        false,
                        None,
                        blockquote_depth,
                        DjotBlockStyle::default(),
                    );
                }
                if let Some((label, start)) = footnote_open.take() {
                    let blocks: Vec<ParsedBlock> = elements
                        .drain(start..)
                        .filter_map(|e| match e {
                            ParsedElement::Block(b) => Some(b),
                            // A table inside a footnote is not representable as
                            // note content; its cells would have to become
                            // blocks and lose their structure either way.
                            _ => None,
                        })
                        .collect();
                    elements.push(ParsedElement::FootnoteDefinition { label, blocks });
                }
            }

            // ── Unrepresentable containers: drop the entire subtree ──
            E::Start(
                C::Math { .. }
                | C::RawBlock { .. }
                | C::RawInline { .. }
                | C::DescriptionList
                | C::DescriptionDetails
                | C::DescriptionTerm
                | C::Caption
                | C::LinkDefinition { .. },
                _,
            ) => skip_depth = 1,

            // ── Text + atoms ──
            // A line ending inside a code span is kept by the parser as a `\n` in the
            // span's text. It is read as a new block with the list, quotation or heading
            // the line was in, as a `\n` pasted into a block is (see
            // `split_block_at_line_breaks`). Kept inside the span, the Djot writer saved
            // the code run's lines as blocks of their own, which reloaded as two blocks: a
            // document that did not settle into the Djot it saves as. Such a span is what
            // an older version wrote for a code run holding a line break, a pasted
            // `<pre><code>` passage above all, and reading its lines as blocks is how that
            // passage reads in the editor. It is not a hard break: nothing in it asked for
            // a line of the same paragraph.
            E::Str(s) if code && !is_code_block && pending_image.is_none() && s.contains('\n') => {
                for (i, piece) in s.split('\n').enumerate() {
                    if i > 0 {
                        split_line!();
                    }
                    if !piece.is_empty() {
                        push_text!(piece);
                    }
                }
            }
            E::Str(s) => push_text!(s.as_ref()),
            E::Softbreak => push_text!(" "),
            E::LeftSingleQuote => push_text!("\u{2018}"),
            E::RightSingleQuote => push_text!("\u{2019}"),
            E::LeftDoubleQuote => push_text!("\u{201C}"),
            E::RightDoubleQuote => push_text!("\u{201D}"),
            E::Ellipsis => push_text!("\u{2026}"),
            E::EnDash => push_text!("\u{2013}"),
            E::EmDash => push_text!("\u{2014}"),
            E::NonBreakingSpace => push_text!("\u{00A0}"),
            E::Hardbreak => line_break!(),
            // A footnote reference. jotdown emits this purely syntactically —
            // it never checks that a matching `[^label]:` exists anywhere — so a
            // reference whose definition lives outside this document (the normal
            // state for a host that owns note bodies itself) arrives here just
            // the same, and must survive.
            E::FootnoteReference(label) => {
                let sp = ParsedSpan {
                    text: String::new(),
                    bold,
                    italic,
                    underline,
                    strikeout,
                    code,
                    superscript,
                    subscript,
                    link_href: link_href.clone(),
                    image: None,
                    footnote_ref: Some(label.to_string()),
                    preserve_whitespace: false,
                };
                if in_table_cell {
                    current_cell_spans.push(sp);
                } else {
                    current_spans.push(sp);
                }
            }
            // Symbols, escapes, blanklines, thematic breaks and dangling block
            // attributes carry no representable content.
            E::Symbol(_) => {}
            E::Escape | E::Blankline => {}
            E::ThematicBreak(_) | E::Attributes(_) => {}

            // Ends of dropped containers (never reached at skip_depth 0) and any
            // future variants.
            _ => {}
        }
    }

    // Flush any trailing inline content (defensive — Document End closes blocks).
    if !current_spans.is_empty() {
        djot_push_block(
            &mut elements,
            std::mem::take(&mut current_spans),
            current_heading.take(),
            cur_list_style.clone(),
            cur_list_indent,
            cur_list_prefix.clone(),
            cur_list_suffix.clone(),
            cur_list_start,
            cur_marker.clone(),
            is_code_block,
            code_language.take(),
            blockquote_depth,
            std::mem::take(&mut pending_style),
        );
    }

    // An empty document still yields a single empty paragraph (matches
    // `parse_markdown`).
    if elements.is_empty() {
        djot_push_block(
            &mut elements,
            vec![ParsedSpan {
                text: String::new(),
                ..Default::default()
            }],
            None,
            None,
            0,
            String::new(),
            String::new(),
            None,
            None,
            false,
            None,
            0,
            DjotBlockStyle::default(),
        );
    }

    elements
}

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

    #[test]
    fn test_style_keeps_whitespace_reads_important() {
        assert_eq!(style_keeps_whitespace("white-space: pre-wrap"), Some(true));
        assert_eq!(
            style_keeps_whitespace("white-space: pre-wrap !important"),
            Some(true)
        );
        assert_eq!(
            style_keeps_whitespace("white-space:PRE!IMPORTANT; color: red"),
            Some(true)
        );
        assert_eq!(
            style_keeps_whitespace("white-space: pre ! important; white-space: normal"),
            Some(true),
            "a later declaration that is not important does not win"
        );
        assert_eq!(
            style_keeps_whitespace("white-space: pre; white-space: normal !important"),
            Some(false)
        );
        assert_eq!(
            style_keeps_whitespace("white-space: pre; white-space: normal"),
            Some(false)
        );
        assert_eq!(style_keeps_whitespace("white-space: inherit"), None);
    }

    /// Helper: flatten parse_markdown output to blocks for tests that don't care about tables.
    fn parse_markdown_blocks(md: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_markdown(md))
    }

    #[test]
    fn test_parse_markdown_simple_paragraph() {
        let blocks = parse_markdown_blocks("Hello **world**");
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].spans.len() >= 2);
        // "Hello " is plain, "world" is bold
        let plain_span = blocks[0]
            .spans
            .iter()
            .find(|s| s.text.contains("Hello"))
            .unwrap();
        assert!(!plain_span.bold);
        let bold_span = blocks[0].spans.iter().find(|s| s.text == "world").unwrap();
        assert!(bold_span.bold);
    }

    #[test]
    fn test_parse_markdown_heading() {
        let blocks = parse_markdown_blocks("# Title");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].heading_level, Some(1));
        assert_eq!(blocks[0].spans[0].text, "Title");
    }

    #[test]
    fn test_parse_markdown_list() {
        let blocks = parse_markdown_blocks("- item1\n- item2");
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0].list_style, Some(ListStyle::Disc));
        assert_eq!(blocks[1].list_style, Some(ListStyle::Disc));
    }

    /// Helper: extract (is_table, blockquote_depth) per element for nesting assertions.
    fn element_depths(elements: &[ParsedElement]) -> Vec<(bool, u32)> {
        elements
            .iter()
            .map(|e| match e {
                ParsedElement::Block(b) => (false, b.blockquote_depth),
                ParsedElement::Table(t) => (true, t.blockquote_depth),
                // Definitions carry no blockquote nesting of their own; this
                // helper exists for the nesting assertions and never sees one.
                ParsedElement::FootnoteDefinition { .. } => (false, 0),
            })
            .collect()
    }

    #[test]
    fn test_parse_markdown_table_in_blockquote_records_depth() {
        let elements = parse_markdown("> | a | b |\n> |---|---|\n> | c | d |");
        assert_eq!(element_depths(&elements), vec![(true, 1)]);
    }

    #[test]
    fn test_parse_markdown_text_then_table_in_blockquote() {
        let elements = parse_markdown("> Para\n>\n> | a | b |\n> |---|---|\n> | c | d |");
        assert_eq!(element_depths(&elements), vec![(false, 1), (true, 1)]);
    }

    #[test]
    fn test_parse_markdown_table_after_blockquote_closes() {
        let elements = parse_markdown("> Para\n\n| a | b |\n|---|---|\n| c | d |");
        assert_eq!(element_depths(&elements), vec![(false, 1), (true, 0)]);
    }

    #[test]
    fn test_parse_markdown_table_in_nested_blockquote() {
        let elements = parse_markdown(">> | a | b |\n>> |---|---|\n>> | c | d |");
        assert_eq!(element_depths(&elements), vec![(true, 2)]);
    }

    #[test]
    fn test_parse_markdown_list_in_blockquote_records_depth() {
        let elements = parse_markdown("> - item1\n> - item2");
        let depths = element_depths(&elements);
        assert_eq!(depths, vec![(false, 1), (false, 1)]);
        for e in &elements {
            if let ParsedElement::Block(b) = e {
                assert_eq!(b.list_style, Some(ListStyle::Disc));
            }
        }
    }

    #[test]
    fn test_parse_html_table_in_blockquote_records_depth() {
        let elements = parse_html_elements(
            "<blockquote><table><tr><th>A</th></tr><tr><td>x</td></tr></table></blockquote>",
        );
        assert_eq!(element_depths(&elements), vec![(true, 1)]);
    }

    #[test]
    fn test_parse_html_table_after_blockquote() {
        let elements = parse_html_elements(
            "<blockquote><p>Para</p></blockquote><table><tr><td>X</td></tr></table>",
        );
        let depths = element_depths(&elements);
        // The blockquote paragraph carries depth 1; the table is outside (depth 0).
        assert!(depths.contains(&(false, 1)), "depths: {depths:?}");
        assert!(depths.contains(&(true, 0)), "depths: {depths:?}");
    }

    #[test]
    fn test_flatten_to_blocks_propagates_blockquote_depth() {
        let elements = parse_markdown("> | a | b |\n> |---|---|\n> | c | d |");
        let blocks = ParsedElement::flatten_to_blocks(elements);
        assert!(!blocks.is_empty());
        for b in &blocks {
            assert_eq!(b.blockquote_depth, 1);
        }
    }

    #[test]
    fn test_parse_html_simple() {
        let blocks = parse_html("<p>Hello <b>world</b></p>");
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].spans.len() >= 2);
        let bold_span = blocks[0].spans.iter().find(|s| s.text == "world").unwrap();
        assert!(bold_span.bold);
    }

    #[test]
    fn test_parse_html_multiple_paragraphs() {
        let blocks = parse_html("<p>A</p><p>B</p>");
        assert_eq!(blocks.len(), 2);
    }

    #[test]
    fn test_parse_html_heading() {
        let blocks = parse_html("<h2>Subtitle</h2>");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].heading_level, Some(2));
    }

    #[test]
    fn test_parse_html_list() {
        let blocks = parse_html("<ul><li>one</li><li>two</li></ul>");
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0].list_style, Some(ListStyle::Disc));
    }

    #[test]
    fn test_parse_markdown_code_block() {
        let blocks = parse_markdown_blocks("```\nfn main() {}\n```");
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].is_code_block);
        assert!(blocks[0].spans[0].code);
        // pulldown-cmark appends a trailing \n to code block text — verify it's stripped
        let text: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(
            text, "fn main() {}",
            "code block text should not have trailing newline"
        );
    }

    #[test]
    fn test_parse_markdown_nested_formatting() {
        let blocks = parse_markdown_blocks("***bold italic***");
        assert_eq!(blocks.len(), 1);
        let span = &blocks[0].spans[0];
        assert!(span.bold);
        assert!(span.italic);
    }

    #[test]
    fn test_parse_markdown_link() {
        let blocks = parse_markdown_blocks("[click](http://example.com)");
        assert_eq!(blocks.len(), 1);
        let span = &blocks[0].spans[0];
        assert_eq!(span.text, "click");
        assert_eq!(span.link_href, Some("http://example.com".to_string()));
    }

    #[test]
    fn test_parse_markdown_empty() {
        let blocks = parse_markdown_blocks("");
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].spans[0].text.is_empty());
    }

    #[test]
    fn test_parse_html_empty() {
        let blocks = parse_html("");
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].spans[0].text.is_empty());
    }

    #[test]
    fn test_parse_html_nested_formatting() {
        let blocks = parse_html("<p><b><i>bold italic</i></b></p>");
        assert_eq!(blocks.len(), 1);
        let span = &blocks[0].spans[0];
        assert!(span.bold);
        assert!(span.italic);
    }

    #[test]
    fn test_parse_html_link() {
        let blocks = parse_html("<p><a href=\"http://example.com\">click</a></p>");
        assert_eq!(blocks.len(), 1);
        let span = &blocks[0].spans[0];
        assert_eq!(span.text, "click");
        assert_eq!(span.link_href, Some("http://example.com".to_string()));
    }

    #[test]
    fn test_parse_html_ordered_list() {
        let blocks = parse_html("<ol><li>first</li><li>second</li></ol>");
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0].list_style, Some(ListStyle::Decimal));
    }

    #[test]
    fn test_parse_markdown_ordered_list() {
        let blocks = parse_markdown_blocks("1. first\n2. second");
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0].list_style, Some(ListStyle::Decimal));
    }

    #[test]
    fn test_parse_html_blockquote_nested() {
        let blocks = parse_html("<p>before</p><blockquote>quoted</blockquote><p>after</p>");
        assert!(blocks.len() >= 3);
    }

    #[test]
    fn test_parse_block_styles_line_height() {
        let styles = parse_block_styles("line-height: 1.5");
        assert_eq!(styles.line_height, Some(1500));
    }

    #[test]
    fn test_parse_block_styles_direction_rtl() {
        let styles = parse_block_styles("direction: rtl");
        assert_eq!(styles.direction, Some(TextDirection::RightToLeft));
    }

    #[test]
    fn test_parse_block_styles_background_color() {
        let styles = parse_block_styles("background-color: #ff0000");
        assert_eq!(styles.background_color, Some("#ff0000".to_string()));
    }

    #[test]
    fn test_parse_block_styles_white_space_pre() {
        let styles = parse_block_styles("white-space: pre");
        assert_eq!(styles.non_breakable_lines, Some(true));
    }

    #[test]
    fn test_parse_block_styles_multiple() {
        let styles = parse_block_styles("line-height: 2.0; direction: rtl; background-color: blue");
        assert_eq!(styles.line_height, Some(2000));
        assert_eq!(styles.direction, Some(TextDirection::RightToLeft));
        assert_eq!(styles.background_color, Some("blue".to_string()));
    }

    #[test]
    fn test_parse_html_block_styles_extracted() {
        let blocks = parse_html(
            r#"<p style="line-height: 1.5; direction: rtl; background-color: #ccc">text</p>"#,
        );
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].line_height, Some(1500));
        assert_eq!(blocks[0].direction, Some(TextDirection::RightToLeft));
        assert_eq!(blocks[0].background_color, Some("#ccc".to_string()));
    }

    /// The shape every word processor publishes on the clipboard: a newline
    /// after the opening tag, the prose hard-wrapped inside it, and a newline
    /// between every pair of blocks. None of it is text.
    #[test]
    fn test_parse_html_collapses_exporter_line_wrapping() {
        let blocks = parse_html(
            "<body>\n<p style=\"line-height: 200%\">\nfirst line wrapped\nhere</p>\n\
             <p>\nsecond para</p>\n</body>",
        );
        let texts: Vec<String> = blocks
            .iter()
            .map(|b| b.spans.iter().map(|s| s.text.as_str()).collect())
            .collect();
        assert_eq!(texts, vec!["first line wrapped here", "second para"]);
    }

    /// The reported bug: pasting a whole LibreOffice book ended in one block
    /// holding every newline the exporter had written between its paragraphs —
    /// four thousand blank lines under the last sentence.
    #[test]
    fn test_parse_html_drops_whitespace_between_blocks() {
        let blocks = parse_html("<html>\n<body>\n<p>a</p>\n<p>b</p>\n</body>\n</html>");
        assert_eq!(blocks.len(), 2, "no block for the whitespace between them");
        assert!(
            blocks
                .iter()
                .all(|b| b.spans.iter().all(|s| !s.text.contains('\n'))),
            "no block keeps a literal newline"
        );
    }

    /// A run split across inline elements is still one run, and it collapses to
    /// one space rather than disappearing between them.
    #[test]
    fn test_parse_html_collapses_across_span_boundaries() {
        let blocks = parse_html("<p>  <b>bold</b>\n  <i>italic</i>  </p>");
        assert_eq!(blocks.len(), 1);
        let text: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "bold italic");
    }

    /// U+00A0 is content, not layout — the space holding "Attention&nbsp;:"
    /// together in French must survive a paste.
    #[test]
    fn test_parse_html_keeps_no_break_space() {
        let blocks = parse_html("<p>\nAttention&nbsp;: ici</p>");
        let text: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "Attention\u{a0}: ici");
    }

    /// An empty paragraph is a paragraph. Only the whitespace *between* blocks
    /// goes; a block the writer opened stays, however little is in it.
    #[test]
    fn test_parse_html_keeps_empty_paragraph() {
        let blocks = parse_html("<body>\n<p>a</p>\n<p> </p>\n<p>b</p>\n</body>");
        assert_eq!(blocks.len(), 3);
        let text: String = blocks[1].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "");
    }

    /// `<pre>` and `white-space: pre` mean the source's spacing *is* the
    /// content. `nowrap` does not: it forbids wrapping and collapses runs like
    /// any other block, which is why it is not in the same test.
    #[test]
    fn test_parse_html_preserves_whitespace_in_pre() {
        // The newline straight after `<pre>` is the one the HTML parser itself
        // drops, per spec; every newline after that is content.
        let blocks = parse_html("<pre>\nfn main() {\n    let x = 1;\n}</pre>");
        let text: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "fn main() {\n    let x = 1;\n}");

        // Outside a code block a line break is a new block, and each line keeps its
        // spaces.
        let styled = parse_html("<p style=\"white-space: pre-wrap\">a\n  b</p>");
        let texts: Vec<String> = styled
            .iter()
            .map(|b| b.spans.iter().map(|s| s.text.as_str()).collect())
            .collect();
        assert_eq!(texts, ["a", "  b"]);

        let nowrap = parse_html("<p style=\"white-space: nowrap\">a\n  b</p>");
        let text: String = nowrap[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "a b");
    }

    /// Table cells are blocks too, and an exporter indents them just as
    /// generously as it indents paragraphs.
    #[test]
    fn test_parse_html_collapses_table_cell_whitespace() {
        let elements = parse_html_elements(
            "<table>\n<tr>\n<td>\n  one\n</td>\n<td>\n  two\n</td>\n</tr>\n</table>",
        );
        let table = elements
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => Some(t),
                _ => None,
            })
            .expect("a table");
        let cells: Vec<String> = table.rows[0]
            .iter()
            .map(|c| c.spans.iter().map(|s| s.text.as_str()).collect())
            .collect();
        assert_eq!(cells, vec!["one", "two"]);
    }

    #[test]
    fn test_parse_html_white_space_pre() {
        let blocks = parse_html(r#"<p style="white-space: pre">code</p>"#);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].non_breakable_lines, Some(true));
    }

    #[test]
    fn test_parse_html_no_styles_returns_none() {
        let blocks = parse_html("<p>plain</p>");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].line_height, None);
        assert_eq!(blocks[0].direction, None);
        assert_eq!(blocks[0].background_color, None);
        assert_eq!(blocks[0].non_breakable_lines, None);
    }

    #[test]
    fn test_parse_markdown_nested_list_indent() {
        let md = "- top\n  - nested\n    - deep";
        let blocks = parse_markdown_blocks(md);
        assert_eq!(blocks.len(), 3);
        assert_eq!(blocks[0].list_style, Some(ListStyle::Disc));
        assert_eq!(blocks[0].list_indent, 0);
        assert_eq!(blocks[1].list_style, Some(ListStyle::Disc));
        assert_eq!(blocks[1].list_indent, 1);
        assert_eq!(blocks[2].list_style, Some(ListStyle::Disc));
        assert_eq!(blocks[2].list_indent, 2);
    }

    #[test]
    fn test_parse_markdown_nested_ordered_list_indent() {
        let md = "1. first\n   1. nested\n   2. nested2";
        let blocks = parse_markdown_blocks(md);
        assert_eq!(blocks.len(), 3);
        assert_eq!(blocks[0].list_indent, 0);
        assert_eq!(blocks[1].list_indent, 1);
        assert_eq!(blocks[2].list_indent, 1);
    }

    #[test]
    fn test_parse_html_nested_list_indent() {
        let html = "<ul><li>top</li><ul><li>nested</li></ul></ul>";
        let blocks = parse_html(html);
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0].list_indent, 0);
        assert_eq!(blocks[1].list_indent, 1);
    }

    #[test]
    fn test_parse_markdown_table() {
        let md = "| A | B |\n|---|---|\n| 1 | 2 |";
        let elements = parse_markdown(md);
        assert_eq!(elements.len(), 1);
        match &elements[0] {
            ParsedElement::Table(table) => {
                assert_eq!(table.header_rows, 1);
                assert_eq!(table.rows.len(), 2); // 1 header + 1 body
                // Header row
                assert_eq!(table.rows[0].len(), 2);
                assert_eq!(table.rows[0][0].spans[0].text, "A");
                assert_eq!(table.rows[0][1].spans[0].text, "B");
                // Body row
                assert_eq!(table.rows[1].len(), 2);
                assert_eq!(table.rows[1][0].spans[0].text, "1");
                assert_eq!(table.rows[1][1].spans[0].text, "2");
            }
            _ => panic!("Expected ParsedElement::Table"),
        }
    }

    #[test]
    fn test_parse_markdown_table_with_formatting() {
        let md = "| **bold** | `code` | *italic* |\n|---|---|---|\n| ~~strike~~ | plain | [link](http://x.com) |";
        let elements = parse_markdown(md);
        assert_eq!(elements.len(), 1);
        match &elements[0] {
            ParsedElement::Table(table) => {
                assert_eq!(table.rows.len(), 2);
                // Header: bold cell
                assert!(table.rows[0][0].spans[0].bold);
                // Header: code cell
                assert!(table.rows[0][1].spans[0].code);
                // Header: italic cell
                assert!(table.rows[0][2].spans[0].italic);
                // Body: strikeout cell
                assert!(table.rows[1][0].spans[0].strikeout);
                // Body: link cell
                assert_eq!(
                    table.rows[1][2].spans[0].link_href,
                    Some("http://x.com".to_string())
                );
            }
            _ => panic!("Expected ParsedElement::Table"),
        }
    }

    #[test]
    fn test_parse_markdown_mixed_content_with_table() {
        let md = "Before\n\n| A | B |\n|---|---|\n| 1 | 2 |\n\nAfter";
        let elements = parse_markdown(md);
        assert_eq!(elements.len(), 3);
        assert!(matches!(&elements[0], ParsedElement::Block(_)));
        assert!(matches!(&elements[1], ParsedElement::Table(_)));
        assert!(matches!(&elements[2], ParsedElement::Block(_)));
    }
}

#[cfg(test)]
mod djot_tests {
    use super::*;
    use crate::entities::MarkerType;

    fn blocks(d: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_djot(d, &DjotImportOptions::default()))
    }

    fn first_span_with(b: &ParsedBlock, pred: impl Fn(&ParsedSpan) -> bool) -> &ParsedSpan {
        b.spans.iter().find(|s| pred(s)).expect("span not found")
    }

    #[test]
    fn paragraph_bold_italic() {
        let b = blocks("normal *bold* _italic_");
        assert_eq!(b.len(), 1);
        assert!(first_span_with(&b[0], |s| s.text == "bold").bold);
        assert!(first_span_with(&b[0], |s| s.text == "italic").italic);
    }

    #[test]
    fn heading_levels() {
        assert_eq!(blocks("# H1")[0].heading_level, Some(1));
        assert_eq!(blocks("### H3")[0].heading_level, Some(3));
        assert_eq!(blocks("###### H6")[0].heading_level, Some(6));
    }

    #[test]
    fn unordered_bullet_styles_are_distinct() {
        assert_eq!(blocks("- a")[0].list_style, Some(ListStyle::Disc));
        assert_eq!(blocks("* a")[0].list_style, Some(ListStyle::Circle));
        assert_eq!(blocks("+ a")[0].list_style, Some(ListStyle::Square));
    }

    #[test]
    fn ordered_delimiters() {
        let period = blocks("1. a");
        assert_eq!(period[0].list_style, Some(ListStyle::Decimal));
        assert_eq!(period[0].list_prefix, "");
        assert_eq!(period[0].list_suffix, ".");

        let paren = blocks("1) a");
        assert_eq!(paren[0].list_suffix, ")");
        assert_eq!(paren[0].list_prefix, "");

        let paren_paren = blocks("(1) a");
        assert_eq!(paren_paren[0].list_prefix, "(");
        assert_eq!(paren_paren[0].list_suffix, ")");
    }

    #[test]
    fn task_list_markers() {
        let b = blocks("- [ ] a\n- [x] b");
        assert_eq!(b.len(), 2);
        assert_eq!(b[0].marker, Some(MarkerType::Unchecked));
        assert_eq!(b[1].marker, Some(MarkerType::Checked));
    }

    #[test]
    fn code_block_with_language() {
        let b = blocks("```rust\nfn main() {}\n```");
        assert_eq!(b.len(), 1);
        assert!(b[0].is_code_block);
        assert_eq!(b[0].code_language.as_deref(), Some("rust"));
        let text: String = b[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "fn main() {}");
    }

    #[test]
    fn link_href() {
        let b = blocks("[text](http://example.com)");
        let s = first_span_with(&b[0], |s| s.text == "text");
        assert_eq!(s.link_href.as_deref(), Some("http://example.com"));
    }

    #[test]
    fn superscript_subscript() {
        assert!(first_span_with(&blocks("a^b^")[0], |s| s.text == "b").superscript);
        assert!(first_span_with(&blocks("a~b~")[0], |s| s.text == "b").subscript);
    }

    #[test]
    fn delete_insert_verbatim() {
        assert!(first_span_with(&blocks("{-x-}")[0], |s| s.text == "x").strikeout);
        assert!(first_span_with(&blocks("{+x+}")[0], |s| s.text == "x").underline);
        assert!(first_span_with(&blocks("`x`")[0], |s| s.text == "x").code);
    }

    #[test]
    fn blockquote_depth() {
        let els = parse_djot("> quoted", &DjotImportOptions::default());
        match &els[0] {
            ParsedElement::Block(b) => assert_eq!(b.blockquote_depth, 1),
            _ => panic!("expected block"),
        }
    }

    #[test]
    fn nested_list_indent() {
        // Djot nests a sub-list only when a blank line separates it from the
        // parent item and it is indented to the parent's content column
        // (2 spaces per level). Without the blank line the markers fold into
        // the paragraph as lazy continuation.
        let b = blocks("- a\n\n  - b\n\n    - c");
        assert_eq!(b.len(), 3);
        assert_eq!(b[0].list_indent, 0);
        assert_eq!(b[1].list_indent, 1);
        assert_eq!(b[2].list_indent, 2);
    }

    #[test]
    fn table_parsed_as_table() {
        let els = parse_djot(
            "| a | b |\n|---|---|\n| c | d |",
            &DjotImportOptions::default(),
        );
        assert_eq!(els.len(), 1);
        match &els[0] {
            ParsedElement::Table(t) => {
                assert_eq!(t.header_rows, 1);
                assert_eq!(t.rows.len(), 2);
                assert_eq!(t.rows[0][0].spans[0].text, "a");
                assert_eq!(t.rows[1][1].spans[0].text, "d");
            }
            _ => panic!("expected table"),
        }
    }

    #[test]
    fn smart_punctuation_normalised_to_unicode() {
        let text: String = blocks("a... b---c")[0]
            .spans
            .iter()
            .map(|s| s.text.as_str())
            .collect();
        assert!(text.contains('\u{2026}'), "ellipsis: {text:?}");
        assert!(text.contains('\u{2014}'), "em dash: {text:?}");
    }

    #[test]
    fn unrepresentable_constructs_dropped_without_leaking_text() {
        // Thematic break between two paragraphs: no extra block, no stray text.
        let b = blocks("para1\n\n---\n\npara2");
        assert_eq!(b.len(), 2);
        assert_eq!(
            b[0].spans
                .iter()
                .map(|s| s.text.as_str())
                .collect::<String>(),
            "para1"
        );
        assert_eq!(
            b[1].spans
                .iter()
                .map(|s| s.text.as_str())
                .collect::<String>(),
            "para2"
        );

        // Fenced div is unwrapped: its content survives, the fence does not.
        let d = blocks(":::\ninside\n:::");
        let joined: String = d
            .iter()
            .flat_map(|b| b.spans.iter())
            .map(|s| s.text.as_str())
            .collect();
        assert_eq!(joined, "inside");

        // Inline math content is dropped, surrounding text kept.
        let m = blocks("before $`E=mc^2` after");
        let joined: String = m
            .iter()
            .flat_map(|b| b.spans.iter())
            .map(|s| s.text.as_str())
            .collect();
        assert!(joined.contains("before"), "{joined:?}");
        assert!(joined.contains("after"), "{joined:?}");
        assert!(!joined.contains("E=mc"), "math leaked: {joined:?}");
    }

    #[test]
    fn empty_document_yields_one_empty_block() {
        let b = blocks("");
        assert_eq!(b.len(), 1);
        assert!(b[0].spans.iter().all(|s| s.text.is_empty()));
    }

    #[test]
    fn block_attributes_parse_into_block() {
        let b = blocks(
            "{alignment=center line_height=1500 direction=rtl non_breakable_lines=true background_color=\"#ff0000\"}\nhello",
        );
        assert_eq!(b.len(), 1);
        assert_eq!(b[0].alignment, Some(Alignment::Center));
        assert_eq!(b[0].line_height, Some(1500));
        assert_eq!(b[0].direction, Some(TextDirection::RightToLeft));
        assert_eq!(b[0].non_breakable_lines, Some(true));
        assert_eq!(b[0].background_color, Some("#ff0000".to_string()));
    }

    #[test]
    fn spacing_block_attributes_parse_into_block() {
        // `top_margin` / `text_indent` let one block override the document-wide
        // paragraph spacing and first-line indent — what a scene break needs for
        // the paragraph that follows it.
        let b = blocks("{top_margin=24 text_indent=0}\nhello");
        assert_eq!(b.len(), 1);
        assert_eq!(b[0].top_margin, Some(24));
        assert_eq!(b[0].text_indent, Some(0));
    }

    #[test]
    fn a_zero_text_indent_is_distinct_from_an_absent_one() {
        // The whole point of the attribute: `Some(0)` means "explicitly no
        // indent", which must not collapse to `None` ("use the document
        // default") — otherwise a scene break could not suppress the indent.
        let explicit = blocks("{text_indent=0}\nhello");
        let absent = blocks("hello");
        assert_eq!(explicit[0].text_indent, Some(0));
        assert_eq!(absent[0].text_indent, None);
        assert!(!explicit[0].is_inline_only());
        assert!(absent[0].is_inline_only());
    }

    #[test]
    fn spacing_block_attributes_respect_import_options() {
        let src = "{top_margin=24 text_indent=0}\nhello";
        let b = ParsedElement::flatten_to_blocks(parse_djot(src, &DjotImportOptions::none()));
        assert_eq!(b[0].top_margin, None);
        assert_eq!(b[0].text_indent, None);
    }

    #[test]
    fn block_attributes_on_heading() {
        let b = blocks("{alignment=right}\n# Title");
        assert_eq!(b[0].heading_level, Some(1));
        assert_eq!(b[0].alignment, Some(Alignment::Right));
    }

    #[test]
    fn block_attributes_respect_import_options() {
        // With every optional attribute disabled, the `{…}` block attributes are
        // parsed and discarded — only the core paragraph survives.
        let src = "{alignment=center line_height=1500}\nhello";
        let b = ParsedElement::flatten_to_blocks(parse_djot(src, &DjotImportOptions::none()));
        assert_eq!(b[0].alignment, None);
        assert_eq!(b[0].line_height, None);
        assert_eq!(
            b[0].spans
                .iter()
                .map(|s| s.text.as_str())
                .collect::<String>(),
            "hello"
        );
    }

    #[test]
    fn list_item_block_attributes_are_dropped() {
        // Block attributes only bind to standalone paragraphs/headings; a list
        // item normalises them away (symmetric with the exporter).
        let b = blocks("{alignment=center}\n- item");
        assert!(b.iter().all(|blk| blk.alignment.is_none()));
    }

    #[test]
    fn unknown_alignment_value_is_ignored() {
        let b = blocks("{alignment=sideways}\nhello");
        assert_eq!(b[0].alignment, None);
    }
}

// ─── Cheap plain-text extraction ─────────────────────────────────────

/// The `U+FFFC OBJECT REPLACEMENT CHARACTER` that stands for a table in the document's
/// text.
///
/// A table is not prose, but it *occupies a position* in the flow: the import mirrors this
/// single sentinel into the rope where the table sits
/// (`rope_helpers::rope_append_table_anchor`), then the cells as ordinary blocks. Anything
/// reconstructing the text a search runs against has to reproduce it, or every offset after
/// the first table is short by the two characters (the sentinel and its separator) that the
/// document really holds there.
pub const TABLE_ANCHOR: &str = "\u{FFFC}";

/// `U+2028 LINE SEPARATOR`: a line break inside a block, the one Shift+Enter makes in a
/// word processor.
///
/// A block's text holds it as one character, where it starts a new line of the same
/// paragraph, list item, quotation or heading. It is the character Qt's `QTextDocument`
/// uses for the same thing. It cannot be `\n`: the rope already uses `\n` between two
/// blocks, so a search, an offset or a plain-text export could not tell a line from a
/// paragraph.
///
/// The readers make one of every hard break they meet (Djot's `\` at the end of a line,
/// Markdown's, an HTML `<br>` inside a paragraph), and each writer puts it back as its
/// format's own line break. A table cell is one line in Djot and Markdown, so there a
/// line break is read as a space.
pub const LINE_SEPARATOR: char = '\u{2028}';

/// [`LINE_SEPARATOR`] as text, for the readers' span builders.
const LINE_SEPARATOR_STR: &str = "\u{2028}";

/// One span's contribution to the addressable text.
///
/// An inline object carries no prose but **does** occupy one `U+FFFC` in the
/// document (`format_runs_from_spans` mirrors it there), so it has to occupy one
/// here too. Leaving it out makes this string shorter than the document it
/// claims to be byte-identical to, and every offset past the object — every
/// search hit, every comment anchor — lands a character early.
fn span_prose(span: &ParsedSpan, out: &mut String) {
    if span.image.is_some() || span.footnote_ref.is_some() {
        out.push('\u{FFFC}');
        return;
    }
    out.push_str(&span.text);
}

fn block_prose(block: &ParsedBlock) -> String {
    let mut prose = String::new();
    for span in &block.spans {
        span_prose(span, &mut prose);
    }
    prose
}

fn cell_prose(cell: &ParsedTableCell) -> String {
    let mut prose = String::new();
    for span in &cell.spans {
        span_prose(span, &mut prose);
    }
    prose
}

/// The prose of a Djot document, with no entities, no store, and no threads.
///
/// [`parse_djot`] and [`ParsedElement::flatten_to_blocks`] were both already `pub`;
/// nothing chained them. This does, and that is the whole trick: it stops at the
/// *parse*, where a full import goes on to create a `Block` entity per paragraph, list
/// item and table cell, mirror each into the rope, and write its format runs.
///
/// # Why a project-wide search needs this
///
/// A host app searching a manuscript must ask "does this scene contain that word" of
/// **thousands** of Djot rows, on every keystroke. Doing that by importing each one into
/// a document is not a slow feature, it is a frozen app.
///
/// And searching the Djot *source* instead — the tempting shortcut — is simply wrong:
/// the source is markup. `http` matches inside a link's URL, `*` matches an emphasis
/// marker, and an occurrence count taken from the source does not agree with what a
/// replace re-derives inside the parsed document. Where a replace guards itself with
/// "the text moved under me, skip this field", a count taken from markup makes that
/// guard fire on perfectly good rows.
///
/// # The contract
///
/// The result is **byte-identical to the text the document searches** for the same Djot:
/// each block's spans concatenated, blocks joined by a single `\n`, and a table announced
/// by its [`TABLE_ANCHOR`] sentinel — exactly the string the import mirrors into the rope
/// and that `build_full_text_via_store` recomposes. So an offset found here is an offset
/// the document agrees with. On a live document the same string is served by
/// `TextDocument::to_addressable_text()`, which reads it off the search plumbing itself.
///
/// A property in `djot_roundtrip_tests` pins that across the whole generated feature set;
/// without it this would be a second, silently-diverging definition of "the text".
///
/// **Known exception: a footnote *definition*'s body.** This function treats it as out of
/// flow and omits it — matching `character_count()`, which does not count it — but the
/// live document mirrors its blocks into the rope, so today an in-document search DOES
/// run over the note's body and `to_addressable_text()` includes it. The live document
/// holds every body after the main text, so this string is the start of that one, and
/// every offset of the main text agrees between the two; they differ by the bodies at
/// the end. Which side is wrong is an open product question (should search see notes?),
/// pinned as current behaviour in
/// `addressable_text_tests::footnote_bodies_are_searched_in_the_live_document`.
///
/// âš  It is **not** the same as `TextDocument::to_plain_text()`. That is the human-readable
/// *export* — same prose, same order, but with every object anchor omitted, so its offsets
/// drift by two characters per preceding table. The authority is what a search sees,
/// because that is what a replace edits.
/// (`to_plain_text` once also *ordered* a blockquote's prose differently; that was a bug,
/// fixed and pinned by `plain_text_order_tests` — see
/// `claude_reviews/text-document-plain-text-ordering.md`. The anchors are now the two
/// views' only difference.)
pub fn djot_to_plain_text(djot: &str, options: &DjotImportOptions) -> String {
    // Deliberately NOT `ParsedElement::flatten_to_blocks`: that helper drops a table's
    // anchor and yields only its cells, which would silently shift every offset in a
    // document containing a table by the two characters the document actually holds there.
    let elements = parse_djot(djot, options);

    // Sized from the source: the prose is always shorter than the markup that carries it,
    // so this allocates once and never grows.
    let mut out = String::with_capacity(djot.len());

    // The separator is decided by "is this the first block", NOT by "is the output still
    // empty". An EMPTY block (an empty code fence, say) is still a block: the document
    // holds an empty line for it, and an emptiness test would swallow both the block and
    // its separator, shifting every offset after it by one.
    let mut first = true;
    let push = |text: &str, out: &mut String, first: &mut bool| {
        if *first {
            *first = false;
        } else {
            out.push('\n');
        }
        out.push_str(text);
    };

    for element in &elements {
        match element {
            ParsedElement::Block(block) => {
                push(&block_prose(block), &mut out, &mut first);
            }
            // A note's body is **out of flow**: it is not laid out where its
            // definition was written, it is not part of a copied fragment, and
            // the document does not count its characters. So it is not part of
            // the addressable text either — which is the one thing that has to
            // stay true here, since `character_count()` and this string are
            // compared directly.
            //
            // Its blocks do live in the rope, because they have to live
            // somewhere; they are simply not addressable prose.
            ParsedElement::FootnoteDefinition { .. } => {}
            ParsedElement::Table(table) => {
                // The import mirrors a table into the rope as a lone `U+FFFC` sentinel
                // followed by its cells, one per block (`rope_append_table_anchor`). The
                // sentinel occupies a real position in the text the document searches, so
                // it has to occupy one here too — otherwise every offset after a table is
                // short by two characters, and a snippet taken from this string would be
                // sliced in the wrong place.
                push(TABLE_ANCHOR, &mut out, &mut first);
                // A row shorter than the widest holds empty cells in the document, one
                // position each: the import completes it (see `ParsedTable::column_count`).
                let columns = table.column_count();
                for row in &table.rows {
                    for column in 0..columns {
                        let prose = row.get(column).map(cell_prose).unwrap_or_default();
                        push(&prose, &mut out, &mut first);
                    }
                }
            }
        }
    }
    out
}

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

    /// The reason this exists at all: a producer cannot smuggle a reference
    /// through as text, because the Djot escaper neutralises `[`, `^` and `]`.
    /// Pinned here so the escaping rule and this workaround cannot drift apart.
    #[test]
    fn a_literal_reference_in_text_would_be_escaped_into_prose() {
        let blocks = parse_html("<p>The ferry.[^1]</p>");
        let text: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert!(
            text.contains("[^1]"),
            "the parser keeps it as characters: {text:?}"
        );
        assert!(
            blocks[0].spans.iter().all(|s| s.footnote_ref.is_none()),
            "text is text — it must not become a reference by accident"
        );
    }

    #[test]
    fn an_attributed_element_becomes_a_real_footnote_reference() {
        let blocks = parse_html(
            r#"<p>The ferry was late.<sup data-footnote-ref="3"></sup> She waited.</p>"#,
        );
        let refs: Vec<&str> = blocks
            .iter()
            .flat_map(|b| b.spans.iter())
            .filter_map(|s| s.footnote_ref.as_deref())
            .collect();
        assert_eq!(refs, vec!["3"]);

        // And it carries no text of its own, so it cannot be counted as words or
        // matched by a search.
        let marker = blocks[0]
            .spans
            .iter()
            .find(|s| s.footnote_ref.is_some())
            .expect("the reference span");
        assert!(marker.text.is_empty(), "{marker:?}");
    }

    /// The attribute is the contract, not the tag: a producer may use whatever
    /// element renders sensibly in a browser.
    #[test]
    fn any_element_carrying_the_attribute_works() {
        for html in [
            r#"<p>a<sup data-footnote-ref="1"></sup></p>"#,
            r#"<p>a<span data-footnote-ref="1"></span></p>"#,
            r##"<p>a<a data-footnote-ref="1" href="#fn1"></a></p>"##,
        ] {
            let blocks = parse_html(html);
            assert_eq!(
                blocks
                    .iter()
                    .flat_map(|b| b.spans.iter())
                    .filter_map(|s| s.footnote_ref.as_deref())
                    .collect::<Vec<_>>(),
                vec!["1"],
                "failed for {html}"
            );
        }
    }

    #[test]
    fn an_empty_or_absent_label_is_not_a_reference() {
        for html in [
            r#"<p>a<sup data-footnote-ref=""></sup></p>"#,
            r#"<p>a<sup></sup></p>"#,
        ] {
            let blocks = parse_html(html);
            assert!(
                blocks
                    .iter()
                    .flat_map(|b| b.spans.iter())
                    .all(|s| s.footnote_ref.is_none()),
                "failed for {html}"
            );
        }
    }
}

#[cfg(test)]
mod html_depth_tests {
    //! HTML nested more deeply than the reader follows, and HTML table cells.

    use super::*;

    /// The text of each block, in order.
    fn block_texts(blocks: &[ParsedBlock]) -> Vec<String> {
        blocks
            .iter()
            .map(|block| block.spans.iter().map(|span| span.text.as_str()).collect())
            .collect()
    }

    /// An HTML list nested past the deepest level an insertion keeps comes in with every
    /// item, in its order, the deeper ones side by side at that level. The reader stopped
    /// descending at its recursion limit and dropped every item below it (the eighty-sixth
    /// level on).
    #[test]
    fn a_deep_html_list_keeps_every_item_at_the_deepest_level() {
        use crate::parser_tools::list_depth::MAX_LIST_INDENT;
        let depth = 300;
        let mut html: String = (0..depth).map(|i| format!("<ol><li>item {i}")).collect();
        html.push_str(&"</li></ol>".repeat(depth));
        let blocks = parse_html(&html);
        let items: Vec<(String, u32)> = blocks
            .iter()
            .filter(|block| block.list_style.is_some())
            .map(|block| {
                let text: String = block.spans.iter().map(|span| span.text.as_str()).collect();
                (text, block.list_indent)
            })
            .collect();
        let expected: Vec<(String, u32)> = (0..depth)
            .map(|i| (format!("item {i}"), (i as i64).min(MAX_LIST_INDENT) as u32))
            .collect();
        assert_eq!(items, expected);
    }

    /// Text outside any item of a list nested too deeply stays, after the items.
    #[test]
    fn text_in_a_deep_html_list_outside_its_items_is_kept() {
        let depth = 40;
        let mut html: String = (0..depth).map(|i| format!("<ul><li>item {i}")).collect();
        html.push_str("<ul>loose words<li>last</li></ul>");
        html.push_str(&"</li></ul>".repeat(depth));
        let texts = block_texts(&parse_html(&html)).join(" ");
        assert!(texts.contains("loose words"), "{texts:?}");
        assert!(texts.contains("last"), "{texts:?}");
        assert!(texts.contains("item 39"), "{texts:?}");
    }

    /// Past the depth the reader follows, it used to keep nothing: the words at the bottom
    /// of three hundred nested `<div>`s, or quotations, were dropped from the paste.
    #[test]
    fn html_nested_past_the_reader_depth_keeps_its_words() {
        for (open, close) in [("<div>", "</div>"), ("<blockquote>", "</blockquote>")] {
            let html = format!(
                "<p>Top.</p>{}<p>Deep <b>words</b></p><p>here.</p>{}<p>End.</p>",
                open.repeat(300),
                close.repeat(300)
            );
            let texts = block_texts(&parse_html(&html)).join(" ");
            let words: Vec<&str> = texts.split_whitespace().collect();
            assert_eq!(
                words,
                ["Top.", "Deep", "words", "here.", "End."],
                "for {open}"
            );
        }
    }

    /// The same inside a table cell, read as one paragraph.
    #[test]
    fn a_table_cell_nested_past_the_reader_depth_keeps_its_words() {
        let html = format!(
            "<table><tr><td>{}deep words{}</td></tr></table>",
            "<span>".repeat(300),
            "</span>".repeat(300)
        );
        let elements = parse_html_elements(&html);
        let Some(ParsedElement::Table(table)) = elements.first() else {
            panic!("a table: {elements:?}");
        };
        let text: String = table.rows[0][0]
            .spans
            .iter()
            .map(|span| span.text.as_str())
            .collect();
        assert_eq!(text, "deep words");
    }

    /// A cell is one paragraph, so its line breaks and paragraphs are spaces between
    /// words. The HTML writer puts a cell's two paragraphs out as `One<br/>Two`, and the
    /// reader read them back as the one word `OneTwo`.
    #[test]
    fn line_breaks_and_paragraphs_in_an_html_cell_separate_words() {
        for cell in [
            "One<br/>Two",
            "<p>One</p><p>Two</p>",
            "<ul><li>One</li><li>Two</li></ul>",
        ] {
            let elements = parse_html_elements(&format!("<table><tr><td>{cell}</td></tr></table>"));
            let Some(ParsedElement::Table(table)) = elements.first() else {
                panic!("a table: {elements:?}");
            };
            let text: String = table.rows[0][0]
                .spans
                .iter()
                .map(|span| span.text.as_str())
                .collect();
            assert_eq!(text, "One Two", "for {cell}");
        }
    }
}

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

    fn md(source: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_markdown(source))
    }

    fn html(source: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_html_elements(source))
    }

    /// The suffix of every list item of `source`, in order.
    fn suffixes(source: &str) -> Vec<String> {
        md(source)
            .into_iter()
            .filter(|b| b.list_style.is_some())
            .map(|b| b.list_suffix)
            .collect()
    }

    #[test]
    fn a_period_and_a_paren_are_told_apart() {
        assert_eq!(suffixes("1. a\n2. b"), [".", "."]);
        assert_eq!(suffixes("1) a\n2) b"), [")", ")"]);
        let numbered = md("3) a");
        assert_eq!(numbered[0].list_suffix, ")");
        assert_eq!(numbered[0].list_start, Some(3));
        let numbered = md("3. a");
        assert_eq!(numbered[0].list_suffix, ".");
        assert_eq!(numbered[0].list_start, Some(3));
    }

    #[test]
    fn a_bullet_list_has_no_suffix() {
        assert_eq!(suffixes("- a\n- b"), ["", ""]);
        assert_eq!(suffixes("- 1) x"), [")"]);
        let nested = md("- x\n  1) y");
        assert_eq!(nested[0].list_suffix, "");
        assert_eq!(nested[1].list_suffix, ")");
    }

    /// The suffix is read from the range pulldown-cmark reports for the list. Each case
    /// writes `)` so that a range starting anywhere else than at the marker reads `.` and
    /// fails here, and the cases cover every place a list can open.
    #[test]
    fn the_range_of_a_list_starts_at_its_marker_wherever_it_stands() {
        for source in [
            "1) a",
            "  1) a\n  2) b",
            "> 1) a",
            "> > 1) a",
            "> - 1) y\n>   2) z",
            "text\n1) a",
            "1) a\n\n2) b",
            "1. a\n   1) b",
            "- x\n  1) y",
            "- a\n\n    1) b",
            "- 1) y",
            "1. 2) y",
            "x[^n]\n\n[^n]: 1) a",
            "[^x] text\n\n1) a",
            "1) a\n2) b\n   - c\n3) d",
        ] {
            // The blocks of the flow and those of the notes.
            let mut every_block = Vec::new();
            for element in parse_markdown(source) {
                match element {
                    ParsedElement::Block(b) => every_block.push(b),
                    ParsedElement::FootnoteDefinition { blocks, .. } => every_block.extend(blocks),
                    ParsedElement::Table(_) => {}
                }
            }
            let found = every_block
                .into_iter()
                .filter(|b| b.list_style == Some(ListStyle::Decimal))
                .map(|b| b.list_suffix)
                .collect::<Vec<_>>();
            assert!(!found.is_empty(), "no numbered item in {source:?}");
            assert!(
                found.iter().any(|s| s == ")"),
                "{source:?} read {found:?}, the innermost ordered list wears ')'"
            );
        }
    }

    #[test]
    fn nested_lists_each_wear_their_own_delimiter() {
        let blocks = md("1. a\n   1) b\n2. c");
        let suffixes: Vec<_> = blocks.iter().map(|b| b.list_suffix.as_str()).collect();
        assert_eq!(suffixes, [".", ")", "."]);
    }

    #[test]
    fn a_change_of_delimiter_starts_a_new_list() {
        // `1. a` then `1) b`: both start at 1, so only the suffix tells them apart.
        let blocks = md("1. a\n1) b");
        let suffixes: Vec<_> = blocks.iter().map(|b| b.list_suffix.as_str()).collect();
        assert_eq!(suffixes, [".", ")"]);
    }

    #[test]
    fn the_suffix_does_not_leak_into_a_following_block() {
        let blocks = md("1) a\n\nplain");
        assert_eq!(blocks[0].list_suffix, ")");
        assert_eq!(blocks[1].list_style, None);
        assert_eq!(blocks[1].list_suffix, "");
    }

    #[test]
    fn a_loose_list_keeps_the_suffix_on_every_item() {
        assert_eq!(suffixes("1) a\n\n2) b\n\n3) c"), [")", ")", ")"]);
    }

    #[test]
    fn the_html_reader_gives_an_ordered_list_a_period() {
        let blocks = html("<ol><li>a</li></ol>");
        assert_eq!(blocks[0].list_style, Some(ListStyle::Decimal));
        assert_eq!(blocks[0].list_suffix, ".");
        let blocks = html("<ol start=\"3\"><li>a</li></ol>");
        assert_eq!(blocks[0].list_suffix, ".");
        assert_eq!(blocks[0].list_start, Some(3));
        let blocks = html("<ul><li>a</li></ul>");
        assert_eq!(blocks[0].list_suffix, "");
    }

    #[test]
    fn the_default_suffix_follows_the_style() {
        for style in [
            ListStyle::Decimal,
            ListStyle::LowerAlpha,
            ListStyle::UpperAlpha,
            ListStyle::LowerRoman,
            ListStyle::UpperRoman,
        ] {
            assert_eq!(default_list_suffix(&style), ".");
        }
        for style in [ListStyle::Disc, ListStyle::Circle, ListStyle::Square] {
            assert_eq!(default_list_suffix(&style), "");
        }
    }
}

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

    fn md(source: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_markdown(source))
    }

    fn html(source: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_html_elements(source))
    }

    fn text(block: &ParsedBlock) -> String {
        block.spans.iter().map(|s| s.text.as_str()).collect()
    }

    fn markers(blocks: &[ParsedBlock]) -> Vec<Option<MarkerType>> {
        blocks.iter().map(|b| b.marker.clone()).collect()
    }

    #[test]
    fn markdown_task_items_keep_their_state_and_lose_the_box_text() {
        let blocks = md("- [x] done\n- [ ] todo\n- plain");
        assert_eq!(
            markers(&blocks),
            [Some(MarkerType::Checked), Some(MarkerType::Unchecked), None]
        );
        assert_eq!(text(&blocks[0]), "done");
        assert_eq!(text(&blocks[1]), "todo");
        assert_eq!(text(&blocks[2]), "plain");
    }

    #[test]
    fn ordered_and_starred_items_and_a_capital_x() {
        assert_eq!(markers(&md("1. [x] a")), [Some(MarkerType::Checked)]);
        assert_eq!(markers(&md("* [X] cap")), [Some(MarkerType::Checked)]);
        assert_eq!(markers(&md("3) [ ] a")), [Some(MarkerType::Unchecked)]);
    }

    #[test]
    fn a_nested_tight_item_has_its_own_box_and_the_parent_keeps_its_own() {
        let blocks = md("- [x] a\n  - [ ] b\n- c");
        assert_eq!(
            markers(&blocks),
            [Some(MarkerType::Checked), Some(MarkerType::Unchecked), None]
        );
        assert_eq!(text(&blocks[0]), "a");
        assert_eq!(text(&blocks[1]), "b");
    }

    #[test]
    fn only_the_first_block_of_an_item_wears_the_box() {
        let blocks = md("- [ ] a\n\n  second para\n- [x] b");
        assert_eq!(text(&blocks[0]), "a");
        assert_eq!(blocks[0].marker, Some(MarkerType::Unchecked));
        assert_eq!(text(&blocks[1]), "second para");
        assert_eq!(blocks[1].marker, None);
        assert_eq!(text(&blocks[2]), "b");
        assert_eq!(blocks[2].marker, Some(MarkerType::Checked));
    }

    #[test]
    fn a_task_item_in_a_quotation_is_read() {
        let blocks = md("> - [x] a");
        assert_eq!(blocks[0].marker, Some(MarkerType::Checked));
        assert_eq!(blocks[0].blockquote_depth, 1);
    }

    #[test]
    fn an_empty_task_item_does_not_leak_into_the_next_one() {
        // `- [x]` alone is text; `- [x] ` is a box with nothing after it, which makes no
        // block (an empty item never did) and must not hand its box to the next item.
        let blocks = md("- [x]");
        assert_eq!(markers(&blocks), [None]);
        assert_eq!(text(&blocks[0]), "[x]");
        let blocks = md("- [x] \n- next");
        assert_eq!(text(blocks.last().unwrap()), "next");
        assert_eq!(blocks.last().unwrap().marker, None);
    }

    #[test]
    fn a_footnote_with_a_task_list_reads_the_boxes() {
        let elements = parse_markdown("x[^n]\n\n[^n]: - [x] a\n");
        let note = elements
            .into_iter()
            .find_map(|e| match e {
                ParsedElement::FootnoteDefinition { blocks, .. } => Some(blocks),
                _ => None,
            })
            .expect("a note");
        assert!(note.iter().any(|b| b.marker == Some(MarkerType::Checked)));
    }

    #[test]
    fn a_line_break_split_keeps_the_box_on_the_first_line_only() {
        let block = ParsedBlock {
            spans: vec![ParsedSpan {
                text: "one\ntwo".into(),
                preserve_whitespace: true,
                ..Default::default()
            }],
            list_style: Some(ListStyle::Disc),
            marker: Some(MarkerType::Checked),
            ..Default::default()
        };
        let lines = split_block_at_line_breaks(block);
        assert_eq!(
            markers(&lines),
            [Some(MarkerType::Checked), None],
            "the item's box is its first line's"
        );
    }

    #[test]
    fn html_checkbox_items_are_read() {
        let blocks = html(
            r#"<ul><li><input type="checkbox" checked> a</li><li><input type="checkbox" disabled> b</li><li>c</li></ul>"#,
        );
        assert_eq!(
            markers(&blocks),
            [Some(MarkerType::Checked), Some(MarkerType::Unchecked), None]
        );
        assert_eq!(text(&blocks[0]), "a");
        assert_eq!(text(&blocks[1]), "b");
    }

    #[test]
    fn html_checkbox_through_a_label_and_in_any_case() {
        let blocks = html(r#"<ul><li><label><input type="checkbox"> a</label></li></ul>"#);
        assert_eq!(blocks[0].marker, Some(MarkerType::Unchecked));
        assert_eq!(text(&blocks[0]), "a");
        let blocks = html(r#"<ul><li><input type="CHECKBOX" checked="checked"/> a</li></ul>"#);
        assert_eq!(blocks[0].marker, Some(MarkerType::Checked));
    }

    #[test]
    fn html_checkbox_that_is_not_first_or_not_a_checkbox_sets_nothing() {
        let blocks = html(r#"<ul><li>a <input type="checkbox" checked></li></ul>"#);
        assert_eq!(blocks[0].marker, None);
        assert_eq!(text(&blocks[0]), "a");
        let blocks = html(r#"<ul><li><input type="text"> a</li></ul>"#);
        assert_eq!(blocks[0].marker, None);
    }

    #[test]
    fn html_checkbox_outside_a_list_sets_nothing() {
        let blocks = html(r#"<li><input type="checkbox" checked> a</li>"#);
        assert!(blocks.iter().all(|b| b.marker.is_none()));
    }

    #[test]
    fn the_checkbox_writers_are_read_back() {
        for marker in [MarkerType::Checked, MarkerType::Unchecked] {
            let written = task_checkbox_html(Some(&marker));
            let blocks = html(&format!("<ul><li>{written}a</li></ul>"));
            assert_eq!(blocks[0].marker, Some(marker));
            assert_eq!(text(&blocks[0]), "a");
        }
        assert_eq!(task_checkbox_html(None), "");
        assert_eq!(task_box_markdown(Some(&MarkerType::Checked)), "[x] ");
        assert_eq!(task_box_markdown(Some(&MarkerType::Unchecked)), "[ ] ");
        assert_eq!(task_box_markdown(None), "");
    }
}

#[cfg(test)]
mod html_sheet_whitespace_tests {
    use super::*;
    use proptest::prelude::*;

    fn blocks(source: &str) -> Vec<ParsedBlock> {
        ParsedElement::flatten_to_blocks(parse_html_elements(source))
    }

    fn texts(source: &str) -> Vec<String> {
        blocks(source)
            .iter()
            .map(|b| b.spans.iter().map(|s| s.text.as_str()).collect())
            .collect()
    }

    /// `QTextDocument::toHtml` as Qt 6 writes it, its sheet included.
    const QT_HEAD: &str = r#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"><html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css">
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: "\2610"; }
li.checked::marker { content: "\2612"; }
</style></head>"#;

    fn with_sheet(css: &str, body: &str) -> String {
        format!("<html><head><style>{css}</style></head><body>{body}</body></html>")
    }

    #[test]
    fn the_qt_sheet_keeps_tabs_and_runs_of_spaces() {
        let html = format!(
            "{QT_HEAD}<body style=\" font-family:'Sans'; font-size:9pt;\">\n<p style=\" margin-top:0px;\">a\tb   c</p></body></html>"
        );
        assert_eq!(texts(&html), ["a\tb   c"]);
        let html = format!("{QT_HEAD}<body>\n<ul>\n<li>a\tb   c</li></ul></body></html>");
        assert_eq!(texts(&html), ["a\tb   c"]);
        let html = format!(
            "{QT_HEAD}<body><table><tr><td><p>a\tb   c</p></td></tr></table></body></html>"
        );
        let elements = parse_html_elements(&html);
        let cell = elements
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => Some(
                    t.rows[0][0]
                        .spans
                        .iter()
                        .map(|s| s.text.as_str())
                        .collect::<String>(),
                ),
                _ => None,
            })
            .expect("a table");
        assert!(cell.contains("a\tb   c"), "{cell:?}");
    }

    #[test]
    fn without_a_sheet_the_whitespace_collapses_as_before() {
        assert_eq!(texts("<p>a\tb   c</p>"), ["a b c"]);
    }

    /// The sheet must not change the block: a `white-space` in a block's own `style` makes
    /// it non-breakable, and a sheet's must not.
    #[test]
    fn a_sheet_rule_does_not_make_blocks_non_breakable() {
        let html = format!("{QT_HEAD}<body><p>a\tb</p></body></html>");
        let parsed = blocks(&html);
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0].non_breakable_lines, None);
        assert!(parsed[0].is_inline_only());
    }

    #[test]
    fn only_simple_selectors_count() {
        for css in [
            "p.x { white-space: pre }",
            "#a p { white-space: pre }",
            "p:first-child { white-space: pre }",
            "p > b { white-space: pre }",
            "p[class] { white-space: pre }",
            "p, p.x { white-space: pre }",
            "@media print { p { white-space: pre } }",
            "@font-face { font-family: x; white-space: pre }",
            "/* p { white-space: pre } */",
            "/* } */ p { color: red }",
            "p { color: red }",
            "p { white-space: inherit }",
        ] {
            assert_eq!(texts(&with_sheet(css, "<p>a\tb</p>")), ["a b"], "{css}");
        }
    }

    #[test]
    fn a_style_of_another_kind_is_not_read() {
        for head in [
            r#"<style type="text/less">p { white-space: pre }</style>"#,
            r#"<style media="print">p { white-space: pre }</style>"#,
        ] {
            assert_eq!(
                texts(&format!(
                    "<html><head>{head}</head><body><p>a\tb</p></body></html>"
                )),
                ["a b"],
                "{head}"
            );
        }
        for head in [
            r#"<style type="TEXT/CSS" media="screen">p { white-space: pre }</style>"#,
            r#"<style media="all">p { white-space: pre }</style>"#,
        ] {
            assert_eq!(
                texts(&format!(
                    "<html><head>{head}</head><body><p>a\tb</p></body></html>"
                )),
                ["a\tb"],
                "{head}"
            );
        }
    }

    #[test]
    fn the_later_rule_and_the_more_specific_one_win() {
        let case = |css: &str| texts(&with_sheet(css, "<p>a\tb</p>"));
        assert_eq!(
            case("p { white-space: pre } p { white-space: normal }"),
            ["a b"]
        );
        assert_eq!(
            case("p { white-space: normal } p { white-space: pre }"),
            ["a\tb"]
        );
        // A rule naming the tag beats `*`, whichever comes first.
        assert_eq!(
            case("* { white-space: pre-wrap } p { white-space: normal }"),
            ["a b"]
        );
        assert_eq!(
            case("p { white-space: normal } * { white-space: pre-wrap }"),
            ["a b"]
        );
        assert_eq!(case("p { white-space: pre; white-space: normal }"), ["a b"]);
        assert_eq!(case("p { white-space: pre; white-space: bogus }"), ["a\tb"]);
        assert_eq!(case("P { white-space: pre }"), ["a\tb"]);
        assert_eq!(case("p, b { white-space: pre }"), ["a\tb"]);
        // One member too complex for us drops the whole rule, as an unknown one would.
        assert_eq!(case("p, p.x { white-space: pre }"), ["a b"]);
    }

    #[test]
    fn inline_style_and_importance_decide_between_sheet_and_element() {
        let case = |css: &str, style: &str| {
            texts(&with_sheet(css, &format!("<p style=\"{style}\">a\tb</p>")))
        };
        assert_eq!(
            case("p { white-space: pre-wrap }", "white-space: normal"),
            ["a b"]
        );
        assert_eq!(
            case(
                "p { white-space: pre-wrap !important }",
                "white-space: normal"
            ),
            ["a\tb"]
        );
        assert_eq!(
            case(
                "p { white-space: pre-wrap !important }",
                "white-space: normal !important"
            ),
            ["a b"]
        );
    }

    #[test]
    fn body_html_and_star_start_the_walk_without_making_blocks_of_the_layout() {
        for selector in ["body", "html", "*", "body, html"] {
            let html = with_sheet(
                &format!("{selector} {{ white-space: pre-wrap }}"),
                "<p>a\tb</p>\n<p>c</p>\n<ul>\n<li>d</li>\n<li>e<ul>\n<li>f</li>\n</ul></li>\n</ul>\n<table>\n<tr>\n<td>g\th</td>\n</tr>\n</table>\n",
            );
            let parsed = ParsedElement::flatten_to_blocks(parse_html_elements(&html));
            let found: Vec<String> = parsed
                .iter()
                .map(|b| b.spans.iter().map(|s| s.text.as_str()).collect())
                .collect();
            assert_eq!(
                found,
                ["a\tb", "c", "d", "e", "f", "g\th"],
                "{selector}: {found:?}"
            );
        }
    }

    /// Text outside any element inherits what the sheet says of `<body>`: the rule naming
    /// it beats `*`, and `*` reaches the body before `html` does.
    #[test]
    fn bare_text_starts_from_the_body_as_the_cascade_gives_it() {
        let case = |css: &str| texts(&with_sheet(css, "a\tb"));
        assert_eq!(
            case("body { white-space: normal } * { white-space: pre }"),
            ["a b"]
        );
        assert_eq!(
            case("* { white-space: pre } body { white-space: normal }"),
            ["a b"]
        );
        assert_eq!(
            case("html { white-space: normal } * { white-space: pre }"),
            ["a\tb"]
        );
        assert_eq!(case("html { white-space: pre }"), ["a\tb"]);
        assert_eq!(
            case("body { white-space: pre } html { white-space: normal }"),
            ["a\tb"]
        );
    }

    #[test]
    fn the_style_may_come_after_the_content_or_be_split() {
        let html = "<p>a\tb</p><style>p { white-space: pre-wrap }</style>";
        assert_eq!(texts(html), ["a\tb"]);
        let html =
            "<style>p { color: red }</style><style>p { white-space: pre-wrap }</style><p>a\tb</p>";
        assert_eq!(texts(html), ["a\tb"]);
    }

    #[test]
    fn a_deep_document_with_a_sheet_is_read_on_its_own_thread() {
        let depth = HTML_DEPTH_ON_THE_CALLERS_STACK + 20;
        let html = format!(
            "<style>p {{ white-space: pre-wrap }}</style>{}<p>a\tb</p>{}",
            "<div>".repeat(depth),
            "</div>".repeat(depth)
        );
        assert_eq!(texts(&html), ["a\tb"]);
    }

    /// The sheets Word and Qt put on the clipboard, wrapped as they are.
    #[test]
    fn a_word_sheet_in_comment_markers_is_skipped_without_losing_the_next_rule() {
        let css = "\n<!--\n /* Font Definitions */\n @font-face\n\t{font-family:\"Cambria Math\";\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n /* Style Definitions */\n p.MsoNormal, li.MsoNormal, div.MsoNormal\n\t{margin:0cm; font-size:11.0pt;}\n@page WordSection1\n\t{size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt;}\ndiv.WordSection1\n\t{page:WordSection1;}\n /* List Definitions */\n @list l0:level1\n\t{mso-level-number-format:bullet;}\np { white-space: pre-wrap }\n-->\n";
        assert_eq!(texts(&with_sheet(css, "<p>a\tb</p>")), ["a\tb"]);
        // The first rule right after the opening marker is not lost either.
        assert_eq!(
            texts(&with_sheet(
                "<!-- p { white-space: pre-wrap } -->",
                "<p>a\tb</p>"
            )),
            ["a\tb"]
        );
    }

    #[test]
    fn a_blank_paragraph_stays_content_and_blanks_between_blocks_do_not() {
        let html = with_sheet(
            "p { white-space: pre-wrap }",
            "<p>a</p>\n  <p>   </p>\n<p>b</p>",
        );
        assert_eq!(texts(&html), ["a", "   ", "b"]);
    }

    #[test]
    fn a_hostile_sheet_neither_panics_nor_loops() {
        for css in [
            "/* unterminated",
            "p { white-space: pre",
            "p { \"unterminated",
            "} } }",
            "{ { {",
            "p {",
            "@media {",
            "\u{e9}\u{4e2d}\u{1f600} p { white-space: pre }",
            "p\\",
            "\\",
            "'",
            "<!-- -->",
            "p { white-space: \u{e9}pre }",
        ] {
            let _ = texts(&with_sheet(css, "<p>a</p>"));
        }
    }

    /// A document with no <style> block and no inline `white-space` reads as it did before
    /// sheets were read. What an inline `white-space` changes is that blanks next to a block
    /// are layout, but the blank between two inline siblings is still the space between two
    /// words, whatever the element's own `white-space` says.
    #[test]
    fn blanks_between_inline_siblings_are_content() {
        for html in [
            r#"<div style="white-space: pre-wrap"><b>a</b> <i>b</i><p>c</p></div>"#,
            r#"<ul style="white-space: pre-wrap"><li><b>a</b> <i>b</i><ul><li>c</li></ul></li></ul>"#,
            "<style>li { white-space: pre-wrap }</style><ul><li><b>a</b> <i>b</i><ul><li>c</li></ul></li></ul>",
            "<style>* { white-space: pre-wrap }</style><div><b>a</b> <i>b</i><p>c</p></div>",
        ] {
            let read = texts(html);
            assert!(read.iter().any(|t| t == "a b"), "{html}: {read:?}");
            assert!(!read.iter().any(|t| t == "ab"), "{html}: {read:?}");
        }
        // And inside a cell.
        let cell = ParsedElement::flatten_to_blocks(parse_html_elements(
            r#"<table><tr><td style="white-space: pre-wrap"><b>a</b> <b>b</b><p>c</p></td></tr></table>"#,
        ));
        let cell: String = cell
            .iter()
            .flat_map(|b| b.spans.iter().map(|s| s.text.as_str()))
            .collect();
        assert!(cell.contains("a b"), "{cell:?}");
    }

    /// Blanks between blocks are layout whatever the block elements are called, HTML5
    /// sectioning and grouping included.
    #[test]
    fn blanks_between_html5_containers_are_layout() {
        let sections = "<section>\n<p>a</p>\n</section>\n<section>\n<p>b</p>\n</section>\n";
        for selector in ["body", "html", "*"] {
            let html = with_sheet(&format!("{selector} {{ white-space: pre-wrap }}"), sections);
            assert_eq!(texts(&html), ["a", "b"], "{selector}");
        }
        for tag in [
            "article", "main", "header", "footer", "nav", "aside", "figure", "dl",
        ] {
            let html = with_sheet(
                "* { white-space: pre-wrap }",
                &format!("<{tag}>\n<p>a</p>\n</{tag}>\n<{tag}>\n<p>b</p>\n</{tag}>\n"),
            );
            assert_eq!(texts(&html), ["a", "b"], "{tag}");
        }
    }

    /// The decision is made from the two neighbours of a blank, so a parent with a hundred
    /// thousand children is read in proportion to them.
    #[test]
    #[ignore = "a timing test: its bound is wide, but wall-clock time depends on the machine; run with --ignored"]
    fn a_parent_of_many_children_is_read_in_linear_time() {
        let children = "<b>x</b> ".repeat(100_000);
        let html =
            format!("<style>* {{ white-space: pre-wrap }}</style><div>{children}<p>end</p></div>");
        let started = std::time::Instant::now();
        let read = texts(&html);
        assert!(read.len() >= 2);
        assert!(
            started.elapsed() < std::time::Duration::from_secs(60),
            "100,000 children took {:?}",
            started.elapsed()
        );
    }

    /// A table in a bold or a link makes no bold or linked cells: a cell starts from the
    /// whitespace around the table and nothing else.
    #[test]
    fn a_cell_takes_the_whitespace_around_the_table_and_no_formatting() {
        let parsed = parse_html_elements(
            r#"<b><a href="http://x"><table><tr><td>x</td></tr></table></a></b>"#,
        );
        let cell = parsed
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => Some(t.rows[0][0].spans.clone()),
                _ => None,
            })
            .expect("a table");
        assert!(
            cell.iter().all(|s| !s.bold && s.link_href.is_none()),
            "{cell:?}"
        );
    }

    /// A kept whitespace does not keep the space a cell puts between its blocks.
    #[test]
    fn a_cell_of_blocks_under_kept_whitespace_has_no_leading_space() {
        let parsed = parse_html_elements(&with_sheet(
            "* { white-space: pre-wrap }",
            "<table><tr><td><p>x</p></td></tr></table>",
        ));
        let cell: String = parsed
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => {
                    Some(t.rows[0][0].spans.iter().map(|s| s.text.as_str()).collect())
                }
                _ => None,
            })
            .expect("a table");
        assert_eq!(cell, "x");
        let parsed = parse_html_elements(&with_sheet(
            "td { white-space: pre-wrap }",
            "<table><tr><td>a\tb   c</td></tr></table>",
        ));
        let cell: String = parsed
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => {
                    Some(t.rows[0][0].spans.iter().map(|s| s.text.as_str()).collect())
                }
                _ => None,
            })
            .expect("a table");
        assert_eq!(cell, "a\tb   c");
    }

    /// Bare text that kept its line breaks is one block for each line, as it is anywhere else.
    #[test]
    fn bare_text_with_a_line_break_under_a_kept_whitespace_is_two_blocks() {
        let html = "<style>body { white-space: pre-wrap }</style>a\nb";
        assert_eq!(texts(html), ["a", "b"]);
    }

    /// The declarations of a nested rule are that rule's, not the rule that holds it.
    #[test]
    fn a_nested_rule_does_not_leak_into_the_rule_around_it() {
        let case = |css: &str| texts(&with_sheet(css, "<p>a\tb</p>"));
        assert_eq!(
            case("p { span { white-space: normal; white-space: pre; } }"),
            ["a b"]
        );
        assert_eq!(
            case("p { span { white-space: pre } white-space: pre-wrap }"),
            ["a\tb"]
        );
        assert_eq!(
            case("p { white-space: pre-wrap; & span { white-space: normal } }"),
            ["a\tb"]
        );
    }

    /// `pre-line` and `nowrap` from a sheet collapse the whitespace, as they do inline, and a
    /// sheet's `pre` marks no block non-breakable, as an inline one does.
    #[test]
    fn pre_line_nowrap_and_pre_from_a_sheet() {
        for keyword in ["pre-line", "nowrap"] {
            // `*` keeps the whitespace and the tag's rule, which comes after it, collapses it:
            // the text collapses only if the sheet's `pre-line` or `nowrap` was read.
            let html = with_sheet(
                &format!("* {{ white-space: pre }} p {{ white-space: {keyword} }}"),
                "<p>a\tb   c</p>",
            );
            assert_eq!(texts(&html), ["a b c"], "{keyword}");
        }
        let html = with_sheet("p { white-space: pre }", "<p>a\tb</p>");
        let parsed = blocks(&html);
        assert_eq!(parsed[0].non_breakable_lines, None);
        assert_eq!(texts(&html), ["a\tb"]);
    }

    /// Block-level elements the reader does not split a run at (a definition list, the HTML5
    /// sections) join their texts into one run, and a browser shows each on a line of its
    /// own: where whitespace is kept, the blank between two of them is one line break, so
    /// each is a line, and none is merged into a word with the next or given a stray space.
    #[test]
    fn block_level_elements_the_reader_does_not_split_are_a_line_each() {
        let dl = "<dl>\n<dt>a</dt>\n<dd>b</dd>\n</dl>";
        assert_eq!(
            texts(&format!("<div style=\"white-space: pre-wrap\">{dl}</div>")),
            ["a", "b"]
        );
        assert_eq!(
            texts(&with_sheet(
                "* { white-space: pre-wrap }",
                &format!("<div>{dl}</div>")
            )),
            ["a", "b"]
        );
        assert_eq!(
            texts(&with_sheet(
                "* { white-space: pre-wrap }",
                "<section>A</section> <section>B</section>"
            )),
            ["A", "B"]
        );
        assert_eq!(
            texts(&with_sheet(
                "body { white-space: pre-wrap }",
                "<header>Title</header>\n<main>Body</main>"
            )),
            ["Title", "Body"]
        );
        assert_eq!(
            texts(&with_sheet(
                "* { white-space: pre-wrap }",
                "<div><article>A</article>\n<aside>B</aside></div>"
            )),
            ["A", "B"]
        );
        // A block-level element beside an inline one.
        assert_eq!(
            texts(&with_sheet(
                "* { white-space: pre-wrap }",
                "<div><section>A</section> <b>x</b></div>"
            )),
            ["A", "x"]
        );
    }

    /// Where whitespace collapses the same blank is one space, and there is never two.
    #[test]
    fn block_level_elements_the_reader_does_not_split_are_a_word_gap_where_whitespace_collapses() {
        let dl = "<dl>\n<dt>a</dt>\n<dd>b</dd>\n</dl>";
        assert_eq!(
            texts(&with_sheet(
                "body { white-space: pre-wrap }",
                &format!("<div style=\"white-space: normal\">{dl}</div>")
            )),
            ["a b"]
        );
        assert_eq!(texts(&format!("<div>{dl}</div>")), ["a b"]);
        // Text that ends in a blank before the blank does not double it.
        assert_eq!(
            texts("<div><dl><dt>a </dt>\n<dd> b</dd></dl></div>"),
            ["a b"]
        );
        assert_eq!(
            texts(&with_sheet(
                "body { white-space: pre-wrap }",
                "<div style=\"white-space: normal\"><dl><dt>a </dt>\n<dd> b</dd></dl></div>"
            )),
            ["a b"]
        );
        // And in a cell, the one space.
        let parsed = parse_html_elements(&with_sheet(
            "* { white-space: pre-wrap }",
            "<table><tr><td><section>A</section> <section>B</section></td></tr></table>",
        ));
        let cell: String = parsed
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => {
                    Some(t.rows[0][0].spans.iter().map(|s| s.text.as_str()).collect())
                }
                _ => None,
            })
            .expect("a table");
        assert_eq!(cell, "A B");
        // The text before the blank ends in a blank already: no second space.
        let parsed = parse_html_elements(&with_sheet(
            "* { white-space: pre-wrap }",
            "<table><tr><td><section>A </section> <section>B</section></td></tr></table>",
        ));
        let cell: String = parsed
            .iter()
            .find_map(|e| match e {
                ParsedElement::Table(t) => {
                    Some(t.rows[0][0].spans.iter().map(|s| s.text.as_str()).collect())
                }
                _ => None,
            })
            .expect("a table");
        assert_eq!(cell, "A B");
    }

    /// A boundary adds a line only where there is a line to end: not at the start of a run,
    /// not twice in a row, and not for elements that hold blocks of their own.
    #[test]
    fn boundaries_add_no_empty_lines() {
        let html = with_sheet(
            "* { white-space: pre-wrap }",
            "<section>x</section>\n<section><p>p</p></section>\n<section>y</section>",
        );
        let mut read = texts(&html);
        read.sort();
        assert_eq!(read, ["p", "x", "y"], "{html}");
        let html = with_sheet(
            "* { white-space: pre-wrap }",
            "<section><p>p</p></section>\n<section><p>q</p></section>",
        );
        let mut read = texts(&html);
        read.sort();
        assert_eq!(read, ["p", "q"], "{html}");
    }

    /// Where whitespace is kept, a line keeps the blank that ends its own text: one
    /// line break is added between two elements and no space either side of it.
    #[test]
    fn a_kept_line_keeps_its_own_blanks_and_gains_none() {
        assert_eq!(
            texts(&with_sheet(
                "* { white-space: pre-wrap }",
                "<dl><dt>a </dt>\n<dd> b</dd></dl>"
            )),
            ["a ", " b"]
        );
    }

    /// A sheet is resolved once into one winning declaration per tag, a small value and not
    /// the text of a style. (The cost in bytes allocated is pinned in
    /// `tests/html_sheet_cost_tests.rs`, where it is counted and not timed.)
    #[test]
    fn a_large_sheet_resolves_into_one_declaration_per_tag() {
        let mut css = String::new();
        for i in 0..4000 {
            css.push_str("* { white-space: pre }\n");
            css.push_str(&format!("t{i} {{ white-space: pre }}\n"));
        }
        let sheet = SheetWhitespace::from_css(&css);
        assert_eq!(sheet.per_tag.len(), 4000);
        assert_eq!(sheet.declaration_for("t17"), Some(("pre", false)));
        assert_eq!(sheet.declaration_for("p"), Some(("pre", false)));

        let selectors: Vec<String> = (0..20_000).map(|i| format!("t{i}")).collect();
        let css = format!("{} {{ white-space: pre }}", selectors.join(","));
        let sheet = SheetWhitespace::from_css(&css);
        assert_eq!(sheet.per_tag.len(), 20_000);
        assert_eq!(sheet.declaration_for("p"), None);
    }

    proptest! {
        #[test]
        fn the_rule_scanner_survives_any_text(css in "\\PC{0,200}") {
            let _ = parse_whitespace_rules(&css);
        }

        #[test]
        fn the_rule_scanner_survives_css_shaped_noise(
            css in "[a-z*.#:>,@{}();!/\"' \\\\\n\u{e9}-]{0,120}"
        ) {
            let _ = parse_whitespace_rules(&css);
        }
    }
}

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

    fn parse(djot: &str) -> Vec<ParsedElement> {
        parse_djot(djot, &DjotImportOptions::default())
    }

    /// The text of every span of every block, table cell and note, in order.
    fn all_text(elements: &[ParsedElement]) -> Vec<String> {
        let mut out = Vec::new();
        for element in elements {
            match element {
                ParsedElement::Block(b) => out.extend(b.spans.iter().map(|s| s.text.clone())),
                ParsedElement::Table(t) => {
                    for row in &t.rows {
                        for cell in row {
                            out.extend(cell.spans.iter().map(|s| s.text.clone()));
                        }
                    }
                }
                ParsedElement::FootnoteDefinition { blocks, .. } => {
                    for b in blocks {
                        out.extend(b.spans.iter().map(|s| s.text.clone()));
                    }
                }
            }
        }
        out
    }

    /// A CRLF source reads as its LF twin, for every construct that holds a line ending.
    #[test]
    fn a_crlf_document_reads_as_its_lf_twin() {
        for lf in [
            "a\nb\n\nc",
            "```rust\nlet x = 1;\nlet y = 2;\n```\n",
            "`a\nb` and *c\nd*",
            "a\\\nb",
            "- one\n  - nested\n- two\n",
            "1. one\n2. two\n",
            "| a | b |\n|---|---|\n| c | d |\n",
            "> quoted\n> lines\n",
            "# Heading\n\nbody\n",
            "text[^n]\n\n[^n]: the note\n",
        ] {
            let crlf = lf.replace('\n', "\r\n");
            let from_crlf = parse(&crlf);
            let from_lf = parse(lf);
            assert_eq!(format!("{from_crlf:?}"), format!("{from_lf:?}"), "{lf:?}");
            assert!(
                all_text(&from_crlf).iter().all(|t| !t.contains('\r')),
                "a carriage return in {lf:?}: {:?}",
                all_text(&from_crlf)
            );
        }
    }

    /// The first paragraph of "a\r\nb\r\n\r\nc" is "a", a soft break, "b", not two lines.
    #[test]
    fn a_soft_break_stays_a_soft_break() {
        let blocks = ParsedElement::flatten_to_blocks(parse("a\r\nb\r\n\r\nc"));
        assert_eq!(blocks.len(), 2);
        let first: String = blocks[0].spans.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(first, "a b");
    }

    /// Pin, not mechanism: a document too deep to parse is shown as its lines, and
    /// `str::lines` takes a CRLF off already.
    #[test]
    fn a_too_deep_crlf_document_shows_lines_without_carriage_returns() {
        let deep = format!("{}deep\r\nsecond line\r\n", "- ".repeat(700));
        assert!(
            all_text(&parse(&deep)).iter().all(|t| !t.contains('\r')),
            "{:?}",
            all_text(&parse(&deep))
        );
    }

    /// The guarantee the carriage-return rule is cut around: any string comes back as it
    /// was through `plain_text_to_djot` and `djot_to_plain_text`, a lone `\r` and a `\r\n`
    /// included. Goes red if a lone `\r` is read as a line ending.
    #[test]
    fn the_plain_text_round_trip_keeps_every_carriage_return() {
        use crate::parser_tools::plain_text_to_djot;
        for text in [
            "a\rb",
            "\rcarriage return\r",
            "a\r\nb",
            "a\r\n\r\nb",
            "x\r",
            "a\r\rb",
        ] {
            let djot = plain_text_to_djot(text);
            let back = djot_to_plain_text(&djot, &DjotImportOptions::default());
            assert_eq!(back, text, "{text:?} written as {djot:?}");
        }
    }

    /// A lone carriage return in Djot source stays text.
    #[test]
    fn a_lone_carriage_return_is_text() {
        let text = all_text(&parse("a\rb")).concat();
        assert_eq!(text, "a\rb");
    }

    /// Pins of the other readers, which normalise a CRLF themselves: not mechanism tests of
    /// this change.
    #[test]
    fn markdown_and_html_already_read_a_crlf_as_a_line_ending() {
        let md = ParsedElement::flatten_to_blocks(parse_markdown("a\r\nb\r\n\r\nc"));
        let md_text: Vec<String> = md
            .iter()
            .flat_map(|b| b.spans.iter().map(|s| s.text.clone()))
            .collect();
        assert!(md_text.iter().all(|t| !t.contains('\r')), "{md_text:?}");
        let md = ParsedElement::flatten_to_blocks(parse_markdown("a\rb\n\nc"));
        assert!(
            md.iter()
                .all(|b| b.spans.iter().all(|s| !s.text.contains('\r'))),
            "a lone CR in Markdown"
        );
        let html = ParsedElement::flatten_to_blocks(parse_html_elements("<pre>a\r\nb</pre>"));
        let html_text: Vec<String> = html
            .iter()
            .flat_map(|b| b.spans.iter().map(|s| s.text.clone()))
            .collect();
        assert!(html_text.iter().all(|t| !t.contains('\r')), "{html_text:?}");
    }

    #[test]
    fn crlf_to_lf_borrows_when_there_is_nothing_to_change() {
        use crate::parser_tools::djot_depth::crlf_to_lf;
        assert!(matches!(
            crlf_to_lf("a\nb\rc"),
            std::borrow::Cow::Borrowed(_)
        ));
        assert_eq!(crlf_to_lf("a\r\nb\r\n\r\n"), "a\nb\n\n");
        assert_eq!(crlf_to_lf("\r\r\n\r"), "\r\n\r");
    }
}