text-document-common 1.15.0

Shared entities, database, events, and undo/redo infrastructure for text-document
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
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
use crate::entities::{Alignment, ListStyle, MarkerType, SemanticRole, TextDirection};
use crate::parser_tools::djot_options::DjotImportOptions;

/// 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).
    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()
}

/// 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);

    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 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();
    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 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: String::new(),
                        list_start: current_list_start,
                        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;
                current_list_style = None;
                current_list_start = 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));
            }
            Event::End(TagEnd::List(_)) => {
                list_stack.pop();
                list_start_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: String::new(),
                        list_start: current_list_start,
                        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 = true;
                current_list_style = list_stack.last().cloned().flatten();
                current_list_start = list_start_stack.last().copied().flatten();
                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: String::new(),
                        list_start: current_list_start,
                        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;
                current_list_style = None;
                current_list_start = 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: String::new(),
                        list_start: current_list_start,
                        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,
                    }));
                }
                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: String::new(),
                        list_start: current_list_start,
                        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,
                    }));
                }
                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: String::new(),
            list_start: current_list_start,
            marker: None,
            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
}

/// 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}')
}

/// 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,
        ..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`).
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 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,
        }
    }

    /// 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();
                    if !t.is_empty() {
                        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) = el.attr("style").and_then(style_keeps_whitespace) {
                        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(text_span(" ".to_string(), state));
                            continue;
                        }
                        "p" | "div" | "li" | "ul" | "ol" | "blockquote" | "pre" | "h1" | "h2"
                        | "h3" | "h4" | "h5" | "h6" => {
                            spans.push(text_span(" ".to_string(), 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>,
        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>,
            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,
                            rows,
                            header_rows,
                            own_cells,
                            true,
                            (depth + 1, limit),
                        ),
                        "tbody" | "tfoot" => collect_rows(
                            child,
                            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();
                                    let state = FmtState::default();
                                    collect_cell_spans(td, &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,
            &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) = el.attr("style").and_then(style_keeps_whitespace) {
                    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, 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 block = ParsedBlock {
                            spans,
                            heading_level,
                            list_style: list_style_for_block,
                            list_indent: list_indent_for_block,
                            list_prefix: String::new(),
                            list_suffix: String::new(),
                            list_start: list_start_for_block,
                            marker: 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.
                let mut spans = vec![text_span(text.text.to_string(), state)];
                collapse_inline_whitespace(&mut spans);
                if spans_carry_content(&spans) {
                    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();
                    if !t.is_empty() {
                        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) = el.attr("style").and_then(style_keeps_whitespace) {
                        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,
                        );
                    }
                }
                _ => {}
            }
        }
    }

    let initial_state = 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) {
        elements.push(ParsedElement::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,
        }));
    }

    // 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.
pub fn parse_djot(djot: &str, options: &DjotImportOptions) -> Vec<ParsedElement> {
    use jotdown::{Container as C, Event as E, ListKind};

    // `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}");
        }
    }
}