asciidoc-parser 0.23.4

Parser for AsciiDoc format
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
// Adapted from Asciidoctor's attributes test suite, found in
// https://github.com/asciidoctor/asciidoctor/blob/main/test/attributes_test.rb.
//
// The tests in this tree are adapted from the Ruby implementation of
// Asciidoctor, which comes with the following license:
//
// MIT License
//
// Copyright (C) 2012-present Dan Allen, Sarah White, Ryan Waldron, and the
// individual contributors to Asciidoctor.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//! Port of Asciidoctor's `attributes_test.rb`.
//!
//! Each Ruby `test '..'` becomes a `#[test] fn`, driven through
//! [`Parser`](crate::Parser) and asserted with the `assert_*` DOM helpers or
//! the document/block attribute accessors. `document_from_string(input,
//! attributes: {..})` maps to `Parser::default().with_intrinsic_attribute(..)`
//! (the Ruby `@` soft-set / `!` unset modifiers map to
//! [`ModificationContext`](crate::parser::ModificationContext) variants and the
//! `_bool` setters), and `safe:` maps to
//! [`with_safe_mode`](crate::Parser::with_safe_mode).
//!
//! Tests that exercise Ruby-only surface are reproduced verbatim in
//! `non_normative!` blocks (no `#[test]`), each tagged with a one-line reason:
//! the mutable node/document attribute API (`Block.new` / `set_attr` /
//! `remove_attr` / `set_attribute` / `role=` / `add_role` / `remove_role`), the
//! DocBook backend, `max-attribute-value-size`, the `user-home` intrinsic, and
//! the `Asciidoctor::INTRINSIC_ATTRIBUTES` constant enumeration.
//!
//! A number of tests surface behavior differences from Asciidoctor. These keep
//! their `#[test]` but move the Ruby source into a `non_normative!` block and
//! assert this crate's *actual* output, each annotated inline.
//!
//! Two of these are **intentional** divergences this crate does not plan to
//! match:
//!
//! * The `{set:..}` inline attribute assignment/unassignment reference is not
//!   processed (left literal).
//! * Compat mode does not change monospaced-span substitution (`+..+` /
//!   backtick) or the legacy syntax it enables.
//!
//! The remainder are tracked as issues (see the `sddbugfind` label). Each such
//! test carries an inline `Divergence:` note; the notable ones are:
//!
//! * Attribute *references* are case-sensitive, so `{He-Man}` does not resolve
//!   the lowercased `:He-Man:` entry.
//! * Multi-line attribute values fuse only the modern backslash continuation,
//!   not the legacy `+`, so a legacy `+`-continued value keeps only its first
//!   line.
//! * Non-ASCII attribute names, names with spaces, and named tokens beginning
//!   with `_` or containing `.` are not accepted.
//! * A counter modifies a locked (API-set or built-in) attribute rather than
//!   leaving it unchanged.
//! * The derived `backend*` / `basebackend*` / `filetype*` / `outfilesuffix` /
//!   `toc-position` / `toc-placement` / `toc-class` attributes are not
//!   materialized as queryable document attributes (this crate resolves them
//!   through dedicated accessors).
//! * Safe-mode masking of `docdir`/`docfile`, the unterminated-header-comment
//!   behavior, and the transfer of trailing anchors/attributes onto a following
//!   section all differ.
//!
//! Separately, block captions/titles are exposed via `caption()` / `title()`
//! rather than as `<div class="title">` DOM nodes — a documented architectural
//! choice (this crate does not render whole blocks to HTML), not a divergence.

use crate::tests::sdd::*;

track_file!("ref/asciidoctor/test/attributes_test.rb");

non_normative!(
    r#"
# frozen_string_literal: true
require_relative 'test_helper'

context 'Attributes' do
  default_logger = Asciidoctor::LoggerManager.logger

  setup do
    Asciidoctor::LoggerManager.logger = (@logger = Asciidoctor::MemoryLogger.new)
  end

  teardown do
    Asciidoctor::LoggerManager.logger = default_logger
  end

  context 'Assignment' do
"#
);
mod assignment {
    use crate::tests::prelude::*;

    #[test]
    fn creates_an_attribute() {
        verifies!(
            r#"
    test 'creates an attribute' do
      doc = document_from_string(':frog: Tanglefoot')
      assert_equal 'Tanglefoot', doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default().parse(":frog: Tanglefoot");
        assert_eq!(
            doc.attribute_value("frog"),
            InterpretedValue::Value("Tanglefoot")
        );
    }

    #[test]
    fn requires_a_space_after_colon_following_attribute_name() {
        verifies!(
            r#"
    test 'requires a space after colon following attribute name' do
      doc = document_from_string 'foo:bar'
      assert_nil doc.attributes['foo']
    end

"#
        );

        let doc = Parser::default().parse("foo:bar");
        assert_eq!(doc.attribute_value("foo"), InterpretedValue::Unset);
    }

    non_normative!(
        r#"
    # NOTE AsciiDoc.py recognizes this entry
"#
    );
    // Tracked in #728.
    #[test]
    fn does_not_recognize_attribute_entry_if_name_contains_colon() {
        non_normative!(
            r#"
    test 'does not recognize attribute entry if name contains colon' do
      input = ':foo:bar: baz'
      doc = document_from_string input
      refute doc.attr?('foo:bar')
      assert_equal 1, doc.blocks.size
      assert_equal :paragraph, doc.blocks[0].context
    end

"#
        );

        // Divergence: Asciidoctor renders `:foo:bar: baz` as a paragraph (a name
        // containing a colon isn't a valid attribute entry). This crate also
        // rejects `foo:bar` as an attribute, but consumes the line into the
        // document header, yielding no blocks — so the block count/context
        // assertions can't be verified against it.
        let doc = Parser::default().parse(":foo:bar: baz");
        assert!(!doc.has_attribute("foo:bar"));
        assert_eq!(doc.nested_blocks().count(), 0);
    }

    non_normative!(
        r#"
    # NOTE AsciiDoc.py recognizes this entry
"#
    );
    // Tracked in #728.
    #[test]
    fn does_not_recognize_attribute_entry_if_name_ends_with_colon() {
        non_normative!(
            r#"
    test 'does not recognize attribute entry if name ends with colon' do
      input = ':foo:: bar'
      doc = document_from_string input
      refute doc.attr?('foo:')
      assert_equal 1, doc.blocks.size
      assert_equal :dlist, doc.blocks[0].context
    end

"#
        );

        // Divergence: Asciidoctor renders `:foo:: bar` as a description list (a
        // name ending with a colon isn't a valid attribute entry). This crate
        // consumes the line into the document header, yielding no blocks.
        let doc = Parser::default().parse(":foo:: bar");
        assert!(!doc.has_attribute("foo:"));
        assert_eq!(doc.nested_blocks().count(), 0);
    }

    non_normative!(
        r#"
    # NOTE AsciiDoc.py does not recognize this entry
"#
    );
    // Tracked in #726.
    #[test]
    fn allows_any_word_character_defined_by_unicode_in_an_attribute_name() {
        non_normative!(
            r#"
    test 'allows any word character defined by Unicode in an attribute name' do
      [['café', 'a coffee shop'], ['سمن', %(سازمان مردمنهاد)]].each do |(name, value)|
        str = <<~EOS
        :#{name}: #{value}

        {#{name}}
        EOS
        result = convert_string_to_embedded str
        assert_includes result, %(<p>#{value}</p>)
      end
    end

"#
        );

        // Divergence: Asciidoctor accepts any Unicode word character in an
        // attribute name; this crate restricts names to ASCII word characters,
        // so `:café: …` / `:سمن: …` are treated as literal paragraphs and the
        // `{café}` / `{سمن}` references are left unresolved.
        for (name, value) in [("café", "a coffee shop"), ("سمن", "سازمان مردمنهاد")]
        {
            let _ = value;
            let doc = Parser::default().parse(&format!(":{name}: {value}\n\n{{{name}}}"));
            assert_xpath(&doc, &format!("//p[text()=\"{{{name}}}\"]"), 1);
        }
    }

    // Tracked in #729.
    #[test]
    fn creates_an_attribute_by_fusing_a_legacy_multi_line_value() {
        non_normative!(
            r#"
    test 'creates an attribute by fusing a legacy multi-line value' do
      str = <<~'EOS'
      :description: This is the first      +
                    Ruby implementation of +
                    AsciiDoc.
      EOS
      doc = document_from_string(str)
      assert_equal 'This is the first Ruby implementation of AsciiDoc.', doc.attributes['description']
    end

"#
        );

        // Divergence: Asciidoctor fuses a legacy `+`-terminated multi-line
        // attribute value; this crate only fuses the modern backslash
        // continuation, so the `+`-continued lines are not joined, and the
        // trailing `+` is treated as a hard-break marker and stripped, leaving
        // just the first line's text.
        let doc = Parser::default().parse(
            ":description: This is the first      +\n              Ruby implementation of +\n              AsciiDoc.",
        );
        assert_eq!(
            doc.attribute_value("description"),
            InterpretedValue::Value("This is the first")
        );
    }

    #[test]
    fn creates_an_attribute_by_fusing_a_multi_line_value() {
        verifies!(
            r#"
    test 'creates an attribute by fusing a multi-line value' do
      str = <<~'EOS'
      :description: This is the first \
                    Ruby implementation of \
                    AsciiDoc.
      EOS
      doc = document_from_string(str)
      assert_equal 'This is the first Ruby implementation of AsciiDoc.', doc.attributes['description']
    end

"#
        );

        let doc = Parser::default().parse(
            ":description: This is the first \\\n              Ruby implementation of \\\n              AsciiDoc.",
        );
        assert_eq!(
            doc.attribute_value("description"),
            InterpretedValue::Value("This is the first Ruby implementation of AsciiDoc.")
        );
    }

    #[test]
    fn honors_line_break_characters_in_multi_line_values() {
        verifies!(
            r#"
    test 'honors line break characters in multi-line values' do
      str = <<~'EOS'
      :signature: Linus Torvalds + \
      Linux Hacker + \
      linus.torvalds@example.com
      EOS
      doc = document_from_string(str)
      assert_equal %(Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com), doc.attributes['signature']
    end

"#
        );

        let doc = Parser::default().parse(
            ":signature: Linus Torvalds + \\\nLinux Hacker + \\\nlinus.torvalds@example.com",
        );
        assert_eq!(
            doc.attribute_value("signature"),
            InterpretedValue::Value("Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com")
        );
    }

    #[test]
    fn should_allow_pass_macro_to_surround_a_multi_line_value_that_contains_line_breaks() {
        verifies!(
            r#"
    test 'should allow pass macro to surround a multi-line value that contains line breaks' do
      str = <<~'EOS'
      :signature: pass:a[{author} + \
      {title} + \
      {email}]
      EOS
      doc = document_from_string str, attributes: { 'author' => 'Linus Torvalds', 'title' => 'Linux Hacker', 'email' => 'linus.torvalds@example.com' }
      assert_equal %(Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com), (doc.attr 'signature')
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("author", "Linus Torvalds", ModificationContext::ApiOnly)
            .with_intrinsic_attribute("title", "Linux Hacker", ModificationContext::ApiOnly)
            .with_intrinsic_attribute(
                "email",
                "linus.torvalds@example.com",
                ModificationContext::ApiOnly,
            )
            .parse(":signature: pass:a[{author} + \\\n{title} + \\\n{email}]");
        assert_eq!(
            doc.attribute_value("signature"),
            InterpretedValue::Value("Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com")
        );
    }

    #[test]
    fn should_delete_an_attribute_that_ends_with() {
        verifies!(
            r#"
    test 'should delete an attribute that ends with !' do
      doc = document_from_string(":frog: Tanglefoot\n:frog!:")
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default().parse(":frog: Tanglefoot\n:frog!:");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn should_delete_an_attribute_that_ends_with_set_via_api() {
        verifies!(
            r#"
    test 'should delete an attribute that ends with ! set via API' do
      doc = document_from_string(":frog: Tanglefoot", attributes: { 'frog!' => '' })
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute_bool("frog", false, ModificationContext::ApiOnly)
            .parse(":frog: Tanglefoot");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn should_delete_an_attribute_that_begins_with() {
        verifies!(
            r#"
    test 'should delete an attribute that begins with !' do
      doc = document_from_string(":frog: Tanglefoot\n:!frog:")
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default().parse(":frog: Tanglefoot\n:!frog:");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn should_delete_an_attribute_that_begins_with_set_via_api() {
        verifies!(
            r#"
    test 'should delete an attribute that begins with ! set via API' do
      doc = document_from_string(":frog: Tanglefoot", attributes: { '!frog' => '' })
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute_bool("frog", false, ModificationContext::ApiOnly)
            .parse(":frog: Tanglefoot");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn should_delete_an_attribute_set_via_api_to_nil_value() {
        verifies!(
            r#"
    test 'should delete an attribute set via API to nil value' do
      doc = document_from_string(":frog: Tanglefoot", attributes: { 'frog' => nil })
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute_bool("frog", false, ModificationContext::ApiOnly)
            .parse(":frog: Tanglefoot");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn doesn_t_choke_when_deleting_a_non_existing_attribute() {
        verifies!(
            r#"
    test "doesn't choke when deleting a non-existing attribute" do
      doc = document_from_string(':frog!:')
      assert_nil doc.attributes['frog']
    end

"#
        );

        let doc = Parser::default().parse(":frog!:");
        assert_eq!(doc.attribute_value("frog"), InterpretedValue::Unset);
    }

    #[test]
    fn replaces_special_characters_in_attribute_value() {
        verifies!(
            r#"
    test "replaces special characters in attribute value" do
      doc = document_from_string(":xml-busters: <>&")
      assert_equal '&lt;&gt;&amp;', doc.attributes['xml-busters']
    end

"#
        );

        let doc = Parser::default().parse(":xml-busters: <>&");
        assert_eq!(
            doc.attribute_value("xml-busters"),
            InterpretedValue::Value("&lt;&gt;&amp;")
        );
    }

    #[test]
    fn performs_attribute_substitution_on_attribute_value() {
        verifies!(
            r#"
    test "performs attribute substitution on attribute value" do
      doc = document_from_string(":version: 1.0\n:release: Asciidoctor {version}")
      assert_equal 'Asciidoctor 1.0', doc.attributes['release']
    end

"#
        );

        let doc = Parser::default().parse(":version: 1.0\n:release: Asciidoctor {version}");
        assert_eq!(
            doc.attribute_value("release"),
            InterpretedValue::Value("Asciidoctor 1.0")
        );
    }

    #[test]
    fn assigns_attribute_to_empty_string_if_substitution_fails_to_resolve_attribute() {
        verifies!(
            r#"
    test 'assigns attribute to empty string if substitution fails to resolve attribute' do
      input = ':release: Asciidoctor {version}'
      document_from_string input, attributes: { 'attribute-missing' => 'drop-line' }
      assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: version'
    end

"#
        );

        // This crate does not surface the INFO "dropping line…" log message, so
        // only the observable result (the missing reference resolves to empty)
        // is checked.
        let doc = Parser::default()
            .with_intrinsic_attribute(
                "attribute-missing",
                "drop-line",
                ModificationContext::ApiOnly,
            )
            .parse(":release: Asciidoctor {version}");
        assert_eq!(doc.attribute_value("release"), InterpretedValue::Value(""));
    }

    // Tracked in #729.
    #[test]
    fn assigns_multi_line_attribute_to_empty_string_if_substitution_fails_to_resolve_attribute() {
        non_normative!(
            r#"
    test 'assigns multi-line attribute to empty string if substitution fails to resolve attribute' do
      input = <<~'EOS'
      :release: Asciidoctor +
                {version}
      EOS
      doc = document_from_string input, attributes: { 'attribute-missing' => 'drop-line' }
      assert_equal '', doc.attributes['release']
      assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: version'
    end

"#
        );

        // Divergence: this crate only fuses the modern backslash continuation,
        // not the legacy `+` continuation, so the `{version}` line never
        // participates in the header value; the trailing `+` is treated as a
        // hard-break marker and stripped, leaving just `Asciidoctor`. (The INFO
        // drop-line log is also not modeled.)
        let doc = Parser::default()
            .with_intrinsic_attribute(
                "attribute-missing",
                "drop-line",
                ModificationContext::ApiOnly,
            )
            .parse(":release: Asciidoctor +\n          {version}\n");
        assert_eq!(
            doc.attribute_value("release"),
            InterpretedValue::Value("Asciidoctor")
        );
    }

    #[test]
    fn resolves_attributes_inside_attribute_value_within_header() {
        verifies!(
            r#"
    test 'resolves attributes inside attribute value within header' do
      input = <<~'EOS'
      = Document Title
      :big: big
      :bigfoot: {big}foot

      {bigfoot}
      EOS

      result = convert_string_to_embedded input
      assert_includes result, 'bigfoot'
    end

"#
        );

        let doc = Parser::default()
            .parse("= Document Title\n:big: big\n:bigfoot: {big}foot\n\n{bigfoot}");
        assert_xpath(&doc, "//p[text()=\"bigfoot\"]", 1);
    }

    #[test]
    fn resolves_attributes_and_pass_macro_inside_attribute_value_outside_header() {
        verifies!(
            r#"
    test 'resolves attributes and pass macro inside attribute value outside header' do
      input = <<~'EOS'
      = Document Title

      content

      :big: pass:a,q[_big_]
      :bigfoot: {big}foot
      {bigfoot}
      EOS

      result = convert_string_to_embedded input
      assert_includes result, '<em>big</em>foot'
    end

"#
        );

        let doc = Parser::default().parse(
            "= Document Title\n\ncontent\n\n:big: pass:a,q[_big_]\n:bigfoot: {big}foot\n{bigfoot}",
        );
        assert_eq!(
            doc.attribute_value("bigfoot"),
            InterpretedValue::Value("<em>big</em>foot")
        );
        assert_xpath(&doc, "//em[text()=\"big\"]", 1);
    }

    // Tracked in #736.
    // Out of scope: this crate does not implement the `max-attribute-value-size`
    // limit.
    non_normative!(
        r#"
    test 'should limit maximum size of attribute value if safe mode is SECURE' do
      expected = 'a' * 4096
      input = <<~EOS
      :name: #{'a' * 5000}

      {name}
      EOS

      result = convert_inline_string input
      assert_equal expected, result
      assert_equal 4096, result.bytesize
    end

"#
    );

    // Tracked in #736.
    // Out of scope: this crate does not implement the `max-attribute-value-size`
    // limit.
    non_normative!(
        r#"
    test 'should handle multibyte characters when limiting attribute value size' do
      expected = '日本'
      input = <<~'EOS'
      :name: 日本語

      {name}
      EOS

      result = convert_inline_string input, attributes: { 'max-attribute-value-size' => 6 }
      assert_equal expected, result
      assert_equal 6, result.bytesize
    end

"#
    );

    // Tracked in #736.
    // Out of scope: this crate does not implement the `max-attribute-value-size`
    // limit.
    non_normative!(
        r#"
    test 'should not mangle multibyte characters when limiting attribute value size' do
      expected = '日本'
      input = <<~'EOS'
      :name: 日本語

      {name}
      EOS

      result = convert_inline_string input, attributes: { 'max-attribute-value-size' => 8 }
      assert_equal expected, result
      assert_equal 6, result.bytesize
    end

"#
    );

    // Tracked in #736.
    // Out of scope: this crate does not implement the `max-attribute-value-size`
    // limit.
    non_normative!(
        r#"
    test 'should allow maximize size of attribute value to be disabled' do
      expected = 'a' * 5000
      input = <<~EOS
      :name: #{'a' * 5000}

      {name}
      EOS

      result = convert_inline_string input, attributes: { 'max-attribute-value-size' => nil }
      assert_equal expected, result
      assert_equal 5000, result.bytesize
    end

"#
    );

    // Tracked in #737.
    // Out of scope: this crate does not define the built-in `user-home` intrinsic
    // attribute.
    non_normative!(
        r#"
    test 'resolves user-home attribute if safe mode is less than SERVER' do
      input = <<~'EOS'
      :imagesdir: {user-home}/etc/images

      {imagesdir}
      EOS
      output = convert_inline_string input, safe: :safe
      assert_equal %(#{Asciidoctor::USER_HOME}/etc/images), output
    end

"#
    );

    // Tracked in #737.
    // Out of scope: this crate does not define the built-in `user-home` intrinsic
    // attribute.
    non_normative!(
        r#"
    test 'user-home attribute resolves to . if safe mode is SERVER or greater' do
      input = <<~'EOS'
      :imagesdir: {user-home}/etc/images

      {imagesdir}
      EOS
      output = convert_inline_string input, safe: :server
      assert_equal './etc/images', output
    end

"#
    );

    #[test]
    fn user_home_attribute_can_be_overridden_by_api_if_safe_mode_is_less_than_server() {
        verifies!(
            r#"
    test 'user-home attribute can be overridden by API if safe mode is less than SERVER' do
      input = <<~'EOS'
      Go {user-home}!
      EOS
      output = convert_inline_string input, attributes: { 'user-home' => '/home' }
      assert_equal 'Go /home!', output
    end

"#
        );

        let doc = Parser::default()
            .with_safe_mode(SafeMode::Safe)
            .with_intrinsic_attribute("user-home", "/home", ModificationContext::ApiOnly)
            .parse("Go {user-home}!");
        assert_xpath(&doc, "//p[text()=\"Go /home!\"]", 1);
    }

    #[test]
    fn user_home_attribute_can_be_overridden_by_api_if_safe_mode_is_server_or_greater() {
        verifies!(
            r#"
    test 'user-home attribute can be overridden by API if safe mode is SERVER or greater' do
      input = <<~'EOS'
      Go {user-home}!
      EOS
      output = convert_inline_string input, safe: :server, attributes: { 'user-home' => '/home' }
      assert_equal 'Go /home!', output
    end

"#
        );

        let doc = Parser::default()
            .with_safe_mode(SafeMode::Server)
            .with_intrinsic_attribute("user-home", "/home", ModificationContext::ApiOnly)
            .parse("Go {user-home}!");
        assert_xpath(&doc, "//p[text()=\"Go /home!\"]", 1);
    }

    #[test]
    fn apply_custom_substitutions_to_text_in_passthrough_macro_and_assign_to_attribute() {
        verifies!(
            r#"
    test "apply custom substitutions to text in passthrough macro and assign to attribute" do
      doc = document_from_string(":xml-busters: pass:[<>&]")
      assert_equal '<>&', doc.attributes['xml-busters']
      doc = document_from_string(":xml-busters: pass:none[<>&]")
      assert_equal '<>&', doc.attributes['xml-busters']
      doc = document_from_string(":xml-busters: pass:specialcharacters[<>&]")
      assert_equal '&lt;&gt;&amp;', doc.attributes['xml-busters']
      doc = document_from_string(":xml-busters: pass:n,-c[<(C)>]")
      assert_equal '<&#169;>', doc.attributes['xml-busters']
    end

"#
        );

        let doc = Parser::default().parse(":xml-busters: pass:[<>&]");
        assert_eq!(
            doc.attribute_value("xml-busters"),
            InterpretedValue::Value("<>&")
        );
        let doc = Parser::default().parse(":xml-busters: pass:none[<>&]");
        assert_eq!(
            doc.attribute_value("xml-busters"),
            InterpretedValue::Value("<>&")
        );
        let doc = Parser::default().parse(":xml-busters: pass:specialcharacters[<>&]");
        assert_eq!(
            doc.attribute_value("xml-busters"),
            InterpretedValue::Value("&lt;&gt;&amp;")
        );
        let doc = Parser::default().parse(":xml-busters: pass:n,-c[<(C)>]");
        assert_eq!(
            doc.attribute_value("xml-busters"),
            InterpretedValue::Value("<&#169;>")
        );
    }

    #[test]
    fn should_not_recognize_pass_macro_with_invalid_substitution_list_in_attribute_value() {
        verifies!(
            r#"
    test 'should not recognize pass macro with invalid substitution list in attribute value' do
      [',', '42', 'a,'].each do |subs|
        doc = document_from_string %(:pass-fail: pass:#{subs}[whale])
        assert_equal %(pass:#{subs}[whale]), doc.attributes['pass-fail']
      end
    end

"#
        );

        let doc = Parser::default().parse(":pass-fail: pass:,[whale]");
        assert_eq!(
            doc.attribute_value("pass-fail"),
            InterpretedValue::Value("pass:,[whale]")
        );
        let doc = Parser::default().parse(":pass-fail: pass:42[whale]");
        assert_eq!(
            doc.attribute_value("pass-fail"),
            InterpretedValue::Value("pass:42[whale]")
        );
        let doc = Parser::default().parse(":pass-fail: pass:a,[whale]");
        assert_eq!(
            doc.attribute_value("pass-fail"),
            InterpretedValue::Value("pass:a,[whale]")
        );
    }

    #[test]
    fn attribute_is_treated_as_defined_until_it_s_not() {
        verifies!(
            r#"
    test "attribute is treated as defined until it's not" do
      input = <<~'EOS'
      :holygrail:
      ifdef::holygrail[]
      The holy grail has been found!
      endif::holygrail[]

      :holygrail!:
      ifndef::holygrail[]
      Buggers! What happened to the grail?
      endif::holygrail[]
      EOS
      output = convert_string input
      assert_xpath '//p', output, 2
      assert_xpath '(//p)[1][text() = "The holy grail has been found!"]', output, 1
      assert_xpath '(//p)[2][text() = "Buggers! What happened to the grail?"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(
            ":holygrail:\nifdef::holygrail[]\nThe holy grail has been found!\nendif::holygrail[]\n\n:holygrail!:\nifndef::holygrail[]\nBuggers! What happened to the grail?\nendif::holygrail[]",
        );
        assert_xpath(&doc, "//p", 2);
        assert_xpath(
            &doc,
            "(//p)[1][text() = \"The holy grail has been found!\"]",
            1,
        );
        assert_xpath(
            &doc,
            "(//p)[2][text() = \"Buggers! What happened to the grail?\"]",
            1,
        );
    }

    #[test]
    fn attribute_set_via_api_overrides_attribute_set_in_document() {
        verifies!(
            r#"
    test 'attribute set via API overrides attribute set in document' do
      doc = document_from_string(':cash: money', attributes: { 'cash' => 'heroes' })
      assert_equal 'heroes', doc.attributes['cash']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::ApiOnly)
            .parse(":cash: money");
        assert_eq!(
            doc.attribute_value("cash"),
            InterpretedValue::Value("heroes")
        );
    }

    #[test]
    fn attribute_set_via_api_cannot_be_unset_by_document() {
        verifies!(
            r#"
    test 'attribute set via API cannot be unset by document' do
      doc = document_from_string(':cash!:', attributes: { 'cash' => 'heroes' })
      assert_equal 'heroes', doc.attributes['cash']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::ApiOnly)
            .parse(":cash!:");
        assert_eq!(
            doc.attribute_value("cash"),
            InterpretedValue::Value("heroes")
        );
    }

    #[test]
    fn attribute_soft_set_via_api_using_modifier_on_name_can_be_overridden_by_document() {
        verifies!(
            r#"
    test 'attribute soft set via API using modifier on name can be overridden by document' do
      doc = document_from_string(':cash: money', attributes: { 'cash@' => 'heroes' })
      assert_equal 'money', doc.attributes['cash']
    end

"#
        );

        // The Ruby `@` modifier (soft set) maps to a value that the document may
        // override anywhere, i.e. `ModificationContext::Anywhere`.
        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::Anywhere)
            .parse(":cash: money");
        assert_eq!(
            doc.attribute_value("cash"),
            InterpretedValue::Value("money")
        );
    }

    #[test]
    fn attribute_soft_set_via_api_using_modifier_on_value_can_be_overridden_by_document() {
        verifies!(
            r#"
    test 'attribute soft set via API using modifier on value can be overridden by document' do
      doc = document_from_string(':cash: money', attributes: { 'cash' => 'heroes@' })
      assert_equal 'money', doc.attributes['cash']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::Anywhere)
            .parse(":cash: money");
        assert_eq!(
            doc.attribute_value("cash"),
            InterpretedValue::Value("money")
        );
    }

    #[test]
    fn attribute_soft_set_via_api_using_modifier_on_name_can_be_unset_by_document() {
        verifies!(
            r#"
    test 'attribute soft set via API using modifier on name can be unset by document' do
      doc = document_from_string(':cash!:', attributes: { 'cash@' => 'heroes' })
      assert_nil doc.attributes['cash']
      doc = document_from_string(':cash!:', attributes: { 'cash@' => true })
      assert_nil doc.attributes['cash']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::Anywhere)
            .parse(":cash!:");
        assert_eq!(doc.attribute_value("cash"), InterpretedValue::Unset);
        let doc = Parser::default()
            .with_intrinsic_attribute_bool("cash", true, ModificationContext::Anywhere)
            .parse(":cash!:");
        assert_eq!(doc.attribute_value("cash"), InterpretedValue::Unset);
    }

    #[test]
    fn attribute_soft_set_via_api_using_modifier_on_value_can_be_unset_by_document() {
        verifies!(
            r#"
    test 'attribute soft set via API using modifier on value can be unset by document' do
      doc = document_from_string(':cash!:', attributes: { 'cash' => 'heroes@' })
      assert_nil doc.attributes['cash']
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("cash", "heroes", ModificationContext::Anywhere)
            .parse(":cash!:");
        assert_eq!(doc.attribute_value("cash"), InterpretedValue::Unset);
    }

    #[test]
    fn attribute_unset_via_api_cannot_be_set_by_document() {
        verifies!(
            r#"
    test 'attribute unset via API cannot be set by document' do
      [
        { 'cash!' => '' },
        { '!cash' => '' },
        { 'cash' => nil },
      ].each do |attributes|
        doc = document_from_string(':cash: money', attributes: attributes)
        assert_nil doc.attributes['cash']
      end
    end

"#
        );

        // The three Ruby forms (`cash!`, `!cash`, `cash => nil`) all mean "unset
        // via API, locked", which maps to a single locked boolean-false intrinsic.
        let doc = Parser::default()
            .with_intrinsic_attribute_bool("cash", false, ModificationContext::ApiOnly)
            .parse(":cash: money");
        assert_eq!(doc.attribute_value("cash"), InterpretedValue::Unset);
    }

    #[test]
    fn attribute_soft_unset_via_api_can_be_set_by_document() {
        verifies!(
            r#"
    test 'attribute soft unset via API can be set by document' do
      [
        { 'cash!@' => '' },
        { '!cash@' => '' },
        { 'cash!' => '@' },
        { '!cash' => '@' },
        { 'cash' => false },
      ].each do |attributes|
        doc = document_from_string(':cash: money', attributes: attributes)
        assert_equal 'money', doc.attributes['cash']
      end
    end

"#
        );

        // All five Ruby forms are a soft unset (unset via API, overridable by the
        // document), which maps to a boolean-false intrinsic modifiable anywhere.
        let doc = Parser::default()
            .with_intrinsic_attribute_bool("cash", false, ModificationContext::Anywhere)
            .parse(":cash: money");
        assert_eq!(
            doc.attribute_value("cash"),
            InterpretedValue::Value("money")
        );
    }

    // Tracked in #734.
    #[test]
    fn can_soft_unset_built_in_attribute_from_api_and_still_override_in_document() {
        verifies!(
            r#"
    test 'can soft unset built-in attribute from API and still override in document' do
      [
        { 'sectids!@' => '' },
        { '!sectids@' => '' },
        { 'sectids!' => '@' },
        { '!sectids' => '@' },
        { 'sectids' => false },
      ].each do |attributes|
        doc = document_from_string '== Heading', attributes: attributes
        refute doc.attr?('sectids')
        assert_css '#_heading', (doc.convert standalone: false), 0
        doc = document_from_string %(:sectids:\n\n== Heading), attributes: attributes
        assert doc.attr?('sectids')
        assert_css '#_heading', (doc.convert standalone: false), 1
      end
    end

"#
        );

        // All five Ruby forms are a soft unset of the built-in `sectids`
        // attribute (unset via API, overridable by the document), which maps to
        // a boolean-false intrinsic modifiable anywhere.
        let doc = Parser::default()
            .with_intrinsic_attribute_bool("sectids", false, ModificationContext::Anywhere)
            .parse("== Heading");
        assert!(!doc.is_attribute_set("sectids"));
        assert_css(&doc, "#_heading", 0);

        let doc = Parser::default()
            .with_intrinsic_attribute_bool("sectids", false, ModificationContext::Anywhere)
            .parse(":sectids:\n\n== Heading");
        assert!(doc.is_attribute_set("sectids"));
        // The generated section id lands on the heading `h2` (this crate also
        // mirrors it onto the section wrapper, so a bare `#_heading` matches
        // twice — the heading anchor is the meaningful target here).
        assert_css(&doc, "h2#_heading", 1);
    }

    // Tracked in #738.
    #[test]
    fn backend_and_doctype_attributes_are_set_by_default_in_default_configuration() {
        non_normative!(
            r#"
    test 'backend and doctype attributes are set by default in default configuration' do
      input = <<~'EOS'
      = Document Title
      Author Name

      content
      EOS

      doc = document_from_string input
      expect = {
        'backend' => 'html5',
        'backend-html5' => '',
        'backend-html5-doctype-article' => '',
        'outfilesuffix' => '.html',
        'basebackend' => 'html',
        'basebackend-html' => '',
        'basebackend-html-doctype-article' => '',
        'doctype' => 'article',
        'doctype-article' => '',
        'filetype' => 'html',
        'filetype-html' => '',
      }
      expect.each do |key, val|
        assert doc.attributes.key? key
        assert_equal val, doc.attributes[key]
      end
    end

"#
        );

        // Divergence: this crate materializes only `doctype` as a queryable
        // document attribute. It does not populate the derived `backend`,
        // `basebackend`, `filetype`, `outfilesuffix`, or `*-doctype-*` /
        // `*-html5` attributes that Asciidoctor sets by default, so only
        // `doctype` can be verified here.
        let doc = Parser::default().parse("= Document Title\nAuthor Name\n\ncontent");
        assert_eq!(
            doc.attribute_value("doctype"),
            InterpretedValue::Value("article")
        );
        assert_eq!(doc.attribute_value("backend"), InterpretedValue::Unset);
    }

    // Out of scope: targets the DocBook backend, which is out of scope for this
    // crate.
    non_normative!(
        r#"
    test 'backend and doctype attributes are set by default in custom configuration' do
      input = <<~'EOS'
      = Document Title
      Author Name

      content
      EOS

      doc = document_from_string input, doctype: 'book', backend: 'docbook'
      expect = {
        'backend' => 'docbook5',
        'backend-docbook5' => '',
        'backend-docbook5-doctype-book' => '',
        'outfilesuffix' => '.xml',
        'basebackend' => 'docbook',
        'basebackend-docbook' => '',
        'basebackend-docbook-doctype-book' => '',
        'doctype' => 'book',
        'doctype-book' => '',
        'filetype' => 'xml',
        'filetype-xml' => '',
      }
      expect.each do |key, val|
        assert doc.attributes.key? key
        assert_equal val, doc.attributes[key]
      end
    end

"#
    );

    // Out of scope: targets the DocBook backend, which is out of scope for this
    // crate.
    non_normative!(
        r#"
    test 'backend attributes are updated if backend attribute is defined in document and safe mode is less than SERVER' do
      input = <<~'EOS'
      = Document Title
      Author Name
      :backend: docbook
      :doctype: book

      content
      EOS

      doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE
      expect = {
        'backend' => 'docbook5',
        'backend-docbook5' => '',
        'backend-docbook5-doctype-book' => '',
        'outfilesuffix' => '.xml',
        'basebackend' => 'docbook',
        'basebackend-docbook' => '',
        'basebackend-docbook-doctype-book' => '',
        'doctype' => 'book',
        'doctype-book' => '',
        'filetype' => 'xml',
        'filetype-xml' => '',
      }
      expect.each do |key, val|
        assert doc.attributes.key?(key)
        assert_equal val, doc.attributes[key]
      end

      refute doc.attributes.key?('backend-html5')
      refute doc.attributes.key?('backend-html5-doctype-article')
      refute doc.attributes.key?('basebackend-html')
      refute doc.attributes.key?('basebackend-html-doctype-article')
      refute doc.attributes.key?('doctype-article')
      refute doc.attributes.key?('filetype-html')
    end

"#
    );

    // Tracked in #738.
    #[test]
    fn backend_attributes_defined_in_document_options_overrides_backend_attribute_in_document() {
        non_normative!(
            r#"
    test 'backend attributes defined in document options overrides backend attribute in document' do
      doc = document_from_string(':backend: docbook5', safe: Asciidoctor::SafeMode::SAFE, attributes: { 'backend' => 'html5' })
      assert_equal 'html5', doc.attributes['backend']
      assert doc.attributes.key? 'backend-html5'
      assert_equal 'html', doc.attributes['basebackend']
      assert doc.attributes.key? 'basebackend-html'
    end

"#
        );

        // Divergence: the API-set `backend` value wins over the document
        // assignment, but this crate does not materialize the derived
        // `backend-html5` / `basebackend` attributes that Asciidoctor
        // synthesizes, so only the winning `backend` value is verified.
        let doc = Parser::default()
            .with_safe_mode(SafeMode::Safe)
            .with_intrinsic_attribute("backend", "html5", ModificationContext::ApiOnly)
            .parse(":backend: docbook5");
        assert_eq!(
            doc.attribute_value("backend"),
            InterpretedValue::Value("html5")
        );
    }

    // Out of scope: exercises Ruby's mutable node attribute API (`Block.new` /
    // positional `attr`), which this crate does not expose.
    non_normative!(
        r#"
    test 'can only access a positional attribute from the attributes hash' do
      node = Asciidoctor::Block.new nil, :paragraph, attributes: { 1 => 'position 1' }
      assert_nil node.attr(1)
      refute node.attr?(1)
      assert_equal 'position 1', node.attributes[1]
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr should not retrieve attribute from document if not set on block' do
      doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
      para = doc.blocks[0]
      assert_nil para.attr 'name'
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr looks for attribute on document if fallback name is true' do
      doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
      para = doc.blocks[0]
      assert_equal 'value', (para.attr 'name', nil, true)
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr uses fallback name when looking for attribute on document' do
      doc = document_from_string 'paragraph', attributes: { 'alt-name' => 'value' }
      para = doc.blocks[0]
      assert_equal 'value', (para.attr 'name', nil, 'alt-name')
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr?` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr? should not check for attribute on document if not set on block' do
      doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
      para = doc.blocks[0]
      refute para.attr? 'name'
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr?` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr? checks for attribute on document if fallback name is true' do
      doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
      para = doc.blocks[0]
      assert para.attr? 'name', nil, true
    end

"#
    );

    // Out of scope: exercises Ruby's node `attr?` document-fallback API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'attr? checks for fallback name when looking for attribute on document' do
      doc = document_from_string 'paragraph', attributes: { 'alt-name' => 'value' }
      para = doc.blocks[0]
      assert para.attr? 'name', nil, 'alt-name'
    end

"#
    );

    // Out of scope: exercises Ruby's mutable node `set_attr` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'set_attr should set value to empty string if no value is specified' do
      node = Asciidoctor::Block.new nil, :paragraph, attributes: {}
      node.set_attr 'foo'
      assert_equal '', (node.attr 'foo')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable node `remove_attr` API, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'remove_attr should remove attribute and return previous value' do
      doc = empty_document
      node = Asciidoctor::Block.new doc, :paragraph, attributes: { 'foo' => 'bar' }
      assert_equal 'bar', (node.remove_attr 'foo')
      assert_nil node.attr('foo')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable node `set_attr` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'set_attr should not overwrite existing key if overwrite is false' do
      node = Asciidoctor::Block.new nil, :paragraph, attributes: { 'foo' => 'bar' }
      assert_equal 'bar', (node.attr 'foo')
      node.set_attr 'foo', 'baz', false
      assert_equal 'bar', (node.attr 'foo')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable node `set_attr` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'set_attr should overwrite existing key by default' do
      node = Asciidoctor::Block.new nil, :paragraph, attributes: { 'foo' => 'bar' }
      assert_equal 'bar', (node.attr 'foo')
      node.set_attr 'foo', 'baz'
      assert_equal 'baz', (node.attr 'foo')
    end

"#
    );

    // Out of scope: exercises Ruby's post-load `set_attr` mutation and
    // re-conversion, which this crate does not expose.
    non_normative!(
        r#"
    test 'set_attr should set header attribute in loaded document' do
      input = <<~'EOS'
      :uri: http://example.org

      {uri}
      EOS

      doc = Asciidoctor.load input, attributes: { 'uri' => 'https://github.com' }
      doc.set_attr 'uri', 'https://google.com'
      output = doc.convert
      assert_xpath '//a[@href="https://google.com"]', output, 1
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `Document#set_attribute` API, which
    // this crate does not expose.
    non_normative!(
        r#"
    test 'set_attribute should set attribute if key is not locked' do
      doc = empty_document
      refute doc.attr? 'foo'
      res = doc.set_attribute 'foo', 'baz'
      assert res
      assert_equal 'baz', (doc.attr 'foo')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `Document#set_attribute` API, which
    // this crate does not expose.
    non_normative!(
        r#"
    test 'set_attribute should not set key if key is locked' do
      doc = empty_document attributes: { 'foo' => 'bar' }
      assert_equal 'bar', (doc.attr 'foo')
      res = doc.set_attribute 'foo', 'baz'
      refute res
      assert_equal 'bar', (doc.attr 'foo')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `Document#set_attribute` API, which
    // this crate does not expose.
    non_normative!(
        r#"
    test 'set_attribute should update backend attributes' do
      doc = empty_document attributes: { 'backend' => 'html5@' }
      assert_equal '', (doc.attr 'backend-html5')
      res = doc.set_attribute 'backend', 'docbook5'
      assert res
      refute doc.attr? 'backend-html5'
      assert_equal '', (doc.attr 'backend-docbook5')
    end

"#
    );

    // Tracked in #738.
    #[test]
    fn verify_toc_attribute_matrix() {
        non_normative!(
            r#"
    test 'verify toc attribute matrix' do
      expected_data = <<~'EOS'
      #attributes                               |toc|toc-position|toc-placement|toc-class
      toc                                       |   |nil         |auto         |nil
      toc=header                                |   |nil         |auto         |nil
      toc=beeboo                                |   |nil         |auto         |nil
      toc=left                                  |   |left        |auto         |toc2
      toc2                                      |   |left        |auto         |toc2
      toc=right                                 |   |right       |auto         |toc2
      toc=preamble                              |   |content     |preamble     |nil
      toc=macro                                 |   |content     |macro        |nil
      toc toc-placement=macro toc-position=left |   |content     |macro        |nil
      toc toc-placement!                        |   |content     |macro        |nil
      EOS

      expected = expected_data.lines.map do |l|
        next if l.start_with? '#'
        l.split('|').map {|e| (e = e.strip) == 'nil' ? nil : e }
      end.compact

      expected.each do |expect|
        raw_attrs, toc, toc_position, toc_placement, toc_class = expect
        attrs = Hash[*raw_attrs.split.map {|e| e.include?('=') ? e.split('=', 2) : [e, ''] }.flatten]
        doc = document_from_string '', attributes: attrs
        toc ? (assert doc.attr?('toc', toc)) : (refute doc.attr?('toc'))
        toc_position ? (assert doc.attr?('toc-position', toc_position)) : (refute doc.attr?('toc-position'))
        toc_placement ? (assert doc.attr?('toc-placement', toc_placement)) : (refute doc.attr?('toc-placement'))
        toc_class ? (assert doc.attr?('toc-class', toc_class)) : (refute doc.attr?('toc-class'))
      end
    end
"#
        );

        // Divergence: this crate resolves TOC configuration through the
        // `Document::toc_*` accessors rather than materializing the
        // `toc-position` / `toc-placement` / `toc-class` document attributes
        // that Asciidoctor's matrix inspects. It also folds Asciidoctor's
        // separate `toc-position` and `toc-placement` into a single `TocMode`,
        // resolved from the `toc` value with a fallback to the `toc-placement`
        // attribute when the `toc` value carries no placement keyword. So each
        // matrix row is exercised here through `toc_mode()` / `toc_class()`,
        // asserting this crate's actual behavior.
        //
        // Three rows still diverge from Asciidoctor's matrix (and are asserted
        // as this crate's actual behavior): the `toc2` alias is not recognized
        // (it resolves to `Disabled`, not a left-placed TOC — #748); `toc-class`
        // is never switched to `toc2` (#749); and a soft-unset `toc-placement!`
        // (with `toc` set) resolves to `Auto` here rather than Asciidoctor's
        // `macro` (#750). The `toc toc-placement=macro` row, by contrast, now
        // matches Asciidoctor (`macro`) since this crate honors an explicit
        // `toc-placement`.
        use crate::document::TocMode;
        // Each row is the raw attribute spec (as it appears in the vendored
        // matrix, parsed the same way Ruby does — space-separated entries,
        // `name=value`, or `name!` for an unset) paired with the `TocMode` this
        // crate resolves for it.
        let rows: &[(&str, TocMode)] = &[
            ("toc", TocMode::Auto),
            ("toc=header", TocMode::Auto),
            ("toc=beeboo", TocMode::Auto),
            ("toc=left", TocMode::Left),
            // `toc2` alias unrecognized (#748); Asciidoctor: left-placed TOC.
            ("toc2", TocMode::Disabled),
            ("toc=right", TocMode::Right),
            ("toc=preamble", TocMode::Preamble),
            ("toc=macro", TocMode::Macro),
            ("toc toc-placement=macro toc-position=left", TocMode::Macro),
            // Soft-unset `toc-placement!` (#750); Asciidoctor: `macro`.
            ("toc toc-placement!", TocMode::Auto),
        ];
        for (spec, expected_mode) in rows {
            let mut parser = Parser::default();
            for entry in spec.split(' ') {
                parser = if let Some(name) = entry.strip_suffix('!') {
                    parser.with_intrinsic_attribute_bool(name, false, ModificationContext::Anywhere)
                } else if let Some((name, value)) = entry.split_once('=') {
                    parser.with_intrinsic_attribute(name, value, ModificationContext::Anywhere)
                } else {
                    parser.with_intrinsic_attribute(entry, "", ModificationContext::Anywhere)
                };
            }
            let doc = parser.parse("");
            assert_eq!(doc.toc_mode(), *expected_mode, "toc_mode for {spec:?}");
            // `toc-class` is never switched to `toc2`; it stays the default
            // (#749); Asciidoctor sets `toc2` for left/right placement.
            assert_eq!(doc.toc_class(), "toc", "toc_class for {spec:?}");
        }
    }
}

non_normative!(
    r#"
  end

  context 'Interpolation' do
"#
);
mod interpolation {
    use crate::tests::prelude::*;

    non_normative!(
        r#"

"#
    );
    #[test]
    fn convert_properly_with_simple_names() {
        verifies!(
            r#"
    test "convert properly with simple names" do
      html = convert_string(":frog: Tanglefoot\n:my_super-hero: Spiderman\n\nYo, {frog}!\nBeat {my_super-hero}!")
      assert_xpath %(//p[text()="Yo, Tanglefoot!\nBeat Spiderman!"]), html, 1
    end

"#
        );

        let doc = Parser::default().parse(
            ":frog: Tanglefoot\n:my_super-hero: Spiderman\n\nYo, {frog}!\nBeat {my_super-hero}!",
        );
        assert_xpath(&doc, "//p[text()=\"Yo, Tanglefoot!\nBeat Spiderman!\"]", 1);
    }

    // Tracked in #724.
    #[test]
    fn attribute_lookup_is_not_case_sensitive() {
        non_normative!(
            r#"
    test 'attribute lookup is not case sensitive' do
      input = <<~'EOS'
      :He-Man: The most powerful man in the universe

      He-Man: {He-Man}

      She-Ra: {She-Ra}
      EOS
      result = convert_string_to_embedded input, attributes: { 'She-Ra' => 'The Princess of Power' }
      assert_xpath '//p[text()="He-Man: The most powerful man in the universe"]', result, 1
      assert_xpath '//p[text()="She-Ra: The Princess of Power"]', result, 1
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute(
                "She-Ra",
                "The Princess of Power",
                ModificationContext::ApiOnly,
            )
            .parse(
                ":He-Man: The most powerful man in the universe\n\nHe-Man: {He-Man}\n\nShe-Ra: {She-Ra}",
            );
        // Divergence: attribute *references* are case-sensitive in this crate,
        // so `{He-Man}` does not resolve against the lowercased `:He-Man:` entry
        // and `{She-Ra}` does not match the API attribute stored as `she-ra`;
        // both are left literal. Asciidoctor performs a case-insensitive lookup.
        assert_xpath(&doc, "//p[text()=\"He-Man: {He-Man}\"]", 1);
        assert_xpath(&doc, "//p[text()=\"She-Ra: {She-Ra}\"]", 1);
    }

    #[test]
    fn convert_properly_with_single_character_name() {
        verifies!(
            r#"
    test "convert properly with single character name" do
      html = convert_string(":r: Ruby\n\nR is for {r}!")
      assert_xpath %(//p[text()="R is for Ruby!"]), html, 1
    end

"#
        );

        let doc = Parser::default().parse(":r: Ruby\n\nR is for {r}!");
        assert_xpath(&doc, "//p[text()=\"R is for Ruby!\"]", 1);
    }

    // Tracked in #726.
    #[test]
    fn collapses_spaces_in_attribute_names() {
        non_normative!(
            r#"
    test "collapses spaces in attribute names" do
      input = <<~'EOS'
      Main Header
      ===========
      :My frog: Tanglefoot

      Yo, {myfrog}!
      EOS
      output = convert_string input
      assert_xpath '(//p)[1][text()="Yo, Tanglefoot!"]', output, 1
    end

"#
        );

        // Divergence: this crate does not collapse spaces in attribute names, so
        // `:My frog:` is not a valid attribute entry and `{myfrog}` is left
        // unresolved.
        let doc = Parser::default()
            .parse("Main Header\n===========\n:My frog: Tanglefoot\n\nYo, {myfrog}!");
        assert_xpath(&doc, "//p[text()=\"Yo, {myfrog}!\"]", 1);
    }

    #[test]
    fn ignores_lines_with_bad_attributes_if_attribute_missing_is_drop_line() {
        verifies!(
            r#"
    test 'ignores lines with bad attributes if attribute-missing is drop-line' do
      input = <<~'EOS'
      :attribute-missing: drop-line

      This is
      blah blah {foobarbaz}
      all there is.
      EOS
      output = convert_string_to_embedded input
      para = xmlnodes_at_css 'p', output, 1
      refute_includes 'blah blah', para.content
      assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: foobarbaz'
    end

"#
        );

        // Only the observable line drop is checked; the INFO log is not modeled.
        let doc = Parser::default().parse(
            ":attribute-missing: drop-line\n\nThis is\nblah blah {foobarbaz}\nall there is.",
        );
        assert_xpath(&doc, "//p[text()=\"This is\nall there is.\"]", 1);
    }

    #[test]
    fn attribute_value_gets_interpreted_when_converting() {
        verifies!(
            r#"
    test 'attribute value gets interpreted when converting' do
      doc = document_from_string(":google: http://google.com[Google]\n\n{google}")
      assert_equal 'http://google.com[Google]', doc.attributes['google']
      output = doc.convert
      assert_xpath '//a[@href="http://google.com"][text() = "Google"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(":google: http://google.com[Google]\n\n{google}");
        assert_eq!(
            doc.attribute_value("google"),
            InterpretedValue::Value("http://google.com[Google]")
        );
        assert_xpath(
            &doc,
            "//a[@href=\"http://google.com\"][text() = \"Google\"]",
            1,
        );
    }

    #[test]
    fn should_drop_line_with_reference_to_missing_attribute_if_attribute_missing_attribute_is_drop_line()
     {
        verifies!(
            r#"
    test 'should drop line with reference to missing attribute if attribute-missing attribute is drop-line' do
      input = <<~'EOS'
      :attribute-missing: drop-line

      Line 1: This line should appear in the output.
      Line 2: Oh no, a {bogus-attribute}! This line should not appear in the output.
      EOS

      output = convert_string_to_embedded input
      assert_match(/Line 1/, output)
      refute_match(/Line 2/, output)
      assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: bogus-attribute'
    end

"#
        );

        let doc = Parser::default().parse(
            ":attribute-missing: drop-line\n\nLine 1: This line should appear in the output.\nLine 2: Oh no, a {bogus-attribute}! This line should not appear in the output.",
        );
        assert_rendered_contains(&doc, "Line 1");
        refute_rendered_contains(&doc, "Line 2");
    }

    #[test]
    fn should_not_drop_line_with_reference_to_missing_attribute_by_default() {
        verifies!(
            r#"
    test 'should not drop line with reference to missing attribute by default' do
      input = <<~'EOS'
      Line 1: This line should appear in the output.
      Line 2: A {bogus-attribute}! This time, this line should appear in the output.
      EOS

      output = convert_string_to_embedded input
      assert_match(/Line 1/, output)
      assert_match(/Line 2/, output)
      assert_match(/\{bogus-attribute\}/, output)
    end

"#
        );

        let doc = Parser::default().parse(
            "Line 1: This line should appear in the output.\nLine 2: A {bogus-attribute}! This time, this line should appear in the output.",
        );
        assert_rendered_contains(&doc, "Line 1");
        assert_rendered_contains(&doc, "Line 2");
        assert_rendered_contains(&doc, "{bogus-attribute}");
    }

    #[test]
    fn should_drop_line_with_attribute_unassignment_by_default() {
        non_normative!(
            r#"
    test 'should drop line with attribute unassignment by default' do
      input = <<~'EOS'
      :a:

      Line 1: This line should appear in the output.
      Line 2: {set:a!}This line should not appear in the output.
      EOS

      output = convert_string_to_embedded input
      assert_match(/Line 1/, output)
      refute_match(/Line 2/, output)
    end

"#
        );

        // Intentional divergence: this crate does not implement the `{set:…}` attribute
        // assignment/unassignment reference, so `{set:a!}` is left literal and
        // its line is not dropped.
        let doc = Parser::default().parse(
            ":a:\n\nLine 1: This line should appear in the output.\nLine 2: {set:a!}This line should not appear in the output.",
        );
        assert_rendered_contains(&doc, "Line 1");
        assert_rendered_contains(&doc, "Line 2");
    }

    #[test]
    fn should_not_drop_line_with_attribute_unassignment_if_attribute_undefined_is_drop() {
        non_normative!(
            r#"
    test 'should not drop line with attribute unassignment if attribute-undefined is drop' do
      input = <<~'EOS'
      :attribute-undefined: drop
      :a:

      Line 1: This line should appear in the output.
      Line 2: {set:a!}This line should appear in the output.
      EOS

      output = convert_string_to_embedded input
      assert_match(/Line 1/, output)
      assert_match(/Line 2/, output)
      refute_match(/\{set:a!\}/, output)
    end

"#
        );

        // Intentional divergence: `{set:…}` is not implemented, so `{set:a!}` remains
        // literal in the output rather than being consumed.
        let doc = Parser::default().parse(
            ":attribute-undefined: drop\n:a:\n\nLine 1: This line should appear in the output.\nLine 2: {set:a!}This line should appear in the output.",
        );
        assert_rendered_contains(&doc, "Line 1");
        assert_rendered_contains(&doc, "Line 2");
        assert_rendered_contains(&doc, "{set:a!}");
    }

    #[test]
    fn should_drop_line_that_only_contains_attribute_assignment() {
        non_normative!(
            r#"
    test 'should drop line that only contains attribute assignment' do
      input = <<~'EOS'
      Line 1
      {set:a}
      Line 2
      EOS

      output = convert_string_to_embedded input
      assert_xpath %(//p[text()="Line 1\nLine 2"]), output, 1
    end

"#
        );

        // Intentional divergence: `{set:…}` is not implemented, so the `{set:a}` line
        // is kept literally instead of being dropped.
        let doc = Parser::default().parse("Line 1\n{set:a}\nLine 2");
        assert_xpath(&doc, "//p[text()=\"Line 1\n{set:a}\nLine 2\"]", 1);
    }

    // Tracked in #730.
    #[test]
    fn should_drop_line_that_only_contains_unresolved_attribute_when_attribute_missing_is_drop() {
        non_normative!(
            r#"
    test 'should drop line that only contains unresolved attribute when attribute-missing is drop' do
      input = <<~'EOS'
      Line 1
      {unresolved}
      Line 2
      EOS

      output = convert_string_to_embedded input, attributes: { 'attribute-missing' => 'drop' }
      assert_xpath %(//p[text()="Line 1\nLine 2"]), output, 1
    end

"#
        );

        // Divergence: with `attribute-missing=drop` this crate removes the
        // missing reference but keeps the now-empty line, yielding
        // `Line 1\n\nLine 2` rather than dropping the whole line.
        let doc = Parser::default()
            .with_intrinsic_attribute("attribute-missing", "drop", ModificationContext::ApiOnly)
            .parse("Line 1\n{unresolved}\nLine 2");
        assert_eq!(
            rendered_paragraphs(&doc),
            vec!["Line 1\n\nLine 2".to_string()]
        );
    }

    #[test]
    fn substitutes_inside_unordered_list_items() {
        verifies!(
            r#"
    test "substitutes inside unordered list items" do
      html = convert_string(":foo: bar\n* snort at the {foo}\n* yawn")
      assert_xpath %(//li/p[text()="snort at the bar"]), html, 1
    end

"#
        );

        let doc = Parser::default().parse(":foo: bar\n* snort at the {foo}\n* yawn");
        assert_xpath(&doc, "//li/p[text()=\"snort at the bar\"]", 1);
    }

    #[test]
    fn substitutes_inside_section_title() {
        verifies!(
            r#"
    test 'substitutes inside section title' do
      output = convert_string(":prefix: Cool\n\n== {prefix} Title\n\ncontent")
      assert_xpath '//h2[text()="Cool Title"]', output, 1
      assert_css 'h2#_cool_title', output, 1
    end

"#
        );

        let doc = Parser::default().parse(":prefix: Cool\n\n== {prefix} Title\n\ncontent");
        assert_xpath(&doc, "//h2[text()=\"Cool Title\"]", 1);
        assert_css(&doc, "h2#_cool_title", 1);
    }

    #[test]
    fn interpolates_attribute_defined_in_header_inside_attribute_entry_in_header() {
        verifies!(
            r#"
    test 'interpolates attribute defined in header inside attribute entry in header' do
      input = <<~'EOS'
      = Title
      Author Name
      :attribute-a: value
      :attribute-b: {attribute-a}

      preamble
      EOS
      doc = document_from_string(input, parse_header_only: true)
      assert_equal 'value', doc.attributes['attribute-b']
    end

"#
        );

        // `parse_header_only` isn't needed here: a full parse resolves the same
        // header attribute value.
        let doc = Parser::default().parse(
            "= Title\nAuthor Name\n:attribute-a: value\n:attribute-b: {attribute-a}\n\npreamble",
        );
        assert_eq!(
            doc.attribute_value("attribute-b"),
            InterpretedValue::Value("value")
        );
    }

    #[test]
    fn interpolates_author_attribute_inside_attribute_entry_in_header() {
        verifies!(
            r#"
    test 'interpolates author attribute inside attribute entry in header' do
      input = <<~'EOS'
      = Title
      Author Name
      :name: {author}

      preamble
      EOS
      doc = document_from_string(input, parse_header_only: true)
      assert_equal 'Author Name', doc.attributes['name']
    end

"#
        );

        let doc = Parser::default().parse("= Title\nAuthor Name\n:name: {author}\n\npreamble");
        assert_eq!(
            doc.attribute_value("name"),
            InterpretedValue::Value("Author Name")
        );
    }

    #[test]
    fn interpolates_revinfo_attribute_inside_attribute_entry_in_header() {
        verifies!(
            r#"
    test 'interpolates revinfo attribute inside attribute entry in header' do
      input = <<~'EOS'
      = Title
      Author Name
      2013-01-01
      :date: {revdate}

      preamble
      EOS
      doc = document_from_string(input, parse_header_only: true)
      assert_equal '2013-01-01', doc.attributes['date']
    end

"#
        );

        let doc = Parser::default()
            .parse("= Title\nAuthor Name\n2013-01-01\n:date: {revdate}\n\npreamble");
        assert_eq!(
            doc.attribute_value("date"),
            InterpretedValue::Value("2013-01-01")
        );
    }

    #[test]
    fn attribute_entries_can_resolve_previously_defined_attributes() {
        verifies!(
            r#"
    test 'attribute entries can resolve previously defined attributes' do
      input = <<~'EOS'
      = Title
      Author Name
      v1.0, 2010-01-01: First release!
      :a: value
      :a2: {a}
      :revdate2: {revdate}

      {a} == {a2}

      {revdate} == {revdate2}
      EOS

      doc = document_from_string input
      assert_equal '2010-01-01', doc.attr('revdate')
      assert_equal '2010-01-01', doc.attr('revdate2')
      assert_equal 'value', doc.attr('a')
      assert_equal 'value', doc.attr('a2')

      output = doc.convert
      assert_includes output, 'value == value'
      assert_includes output, '2010-01-01 == 2010-01-01'
    end

"#
        );

        let doc = Parser::default().parse(
            "= Title\nAuthor Name\nv1.0, 2010-01-01: First release!\n:a: value\n:a2: {a}\n:revdate2: {revdate}\n\n{a} == {a2}\n\n{revdate} == {revdate2}",
        );
        assert_eq!(
            doc.attribute_value("revdate"),
            InterpretedValue::Value("2010-01-01")
        );
        assert_eq!(
            doc.attribute_value("revdate2"),
            InterpretedValue::Value("2010-01-01")
        );
        assert_eq!(doc.attribute_value("a"), InterpretedValue::Value("value"));
        assert_eq!(doc.attribute_value("a2"), InterpretedValue::Value("value"));
        assert_rendered_contains(&doc, "value == value");
        assert_rendered_contains(&doc, "2010-01-01 == 2010-01-01");
    }

    // Tracked in #731.
    #[test]
    fn should_warn_if_unterminated_block_comment_is_detected_in_document_header() {
        non_normative!(
            r#"
    test 'should warn if unterminated block comment is detected in document header' do
      input = <<~'EOS'
      = Document Title
      :foo: bar
      ////
      :hey: there

      content
      EOS
      doc = document_from_string input
      assert_nil doc.attr('hey')
      assert_message @logger, :WARN, '<stdin>: line 3: unterminated comment block', Hash
    end

"#
        );

        // Divergence: Asciidoctor treats the unterminated `////` as a comment
        // block that swallows the rest of the header, so `:hey: there` is never
        // applied. This crate instead applies `:hey: there`, so the attribute is
        // set. (The unterminated-comment warning is also not modeled here.)
        let doc =
            Parser::default().parse("= Document Title\n:foo: bar\n////\n:hey: there\n\ncontent");
        assert_eq!(doc.attribute_value("hey"), InterpretedValue::Value("there"));
    }

    #[test]
    fn substitutes_inside_block_title() {
        non_normative!(
            r#"
    test 'substitutes inside block title' do
      input = <<~'EOS'
      :gem_name: asciidoctor

      .Require the +{gem_name}+ gem
      To use {gem_name}, the first thing to do is to import it in your Ruby source file.
      EOS
      output = convert_string_to_embedded input, attributes: { 'compat-mode' => '' }
      assert_xpath '//*[@class="title"]/code[text()="asciidoctor"]', output, 1

      input = <<~'EOS'
      :gem_name: asciidoctor

      .Require the `{gem_name}` gem
      To use {gem_name}, the first thing to do is to import it in your Ruby source file.
      EOS
      output = convert_string_to_embedded input
      assert_xpath '//*[@class="title"]/code[text()="asciidoctor"]', output, 1
    end

"#
        );

        // A block title is exposed via the `title()` accessor (rendered with
        // inline substitutions applied) rather than as a queryable DOM node.
        //
        // The modern backtick form (`` `{gem_name}` ``) substitutes and wraps in
        // `<code>` as Asciidoctor does. The compat-mode `+…+` monospace form is
        // an intentional divergence — compat mode is not supported, so the `+…+`
        // markers are dropped and the attribute reference is left unsubstituted;
        // that first case is recorded as the crate's actual output.
        let doc = Parser::default()
            .with_intrinsic_attribute("compat-mode", "", ModificationContext::Anywhere)
            .parse(
                ":gem_name: asciidoctor\n\n.Require the +{gem_name}+ gem\nTo use {gem_name}, the first thing to do is to import it in your Ruby source file.",
            );
        assert_eq!(
            doc.nested_blocks().next().unwrap().title(),
            Some("Require the {gem_name} gem")
        );

        let doc = Parser::default().parse(
            ":gem_name: asciidoctor\n\n.Require the `{gem_name}` gem\nTo use {gem_name}, the first thing to do is to import it in your Ruby source file.",
        );
        assert_eq!(
            doc.nested_blocks().next().unwrap().title(),
            Some("Require the <code>asciidoctor</code> gem")
        );
    }

    #[test]
    fn sets_attribute_until_it_is_deleted() {
        verifies!(
            r#"
    test 'sets attribute until it is deleted' do
      input = <<~'EOS'
      :foo: bar

      Crossing the {foo}.

      :foo!:

      Belly up to the {foo}.
      EOS
      output = convert_string_to_embedded input
      assert_xpath '//p[text()="Crossing the bar."]', output, 1
      assert_xpath '//p[text()="Belly up to the bar."]', output, 0
    end

"#
        );

        let doc = Parser::default()
            .parse(":foo: bar\n\nCrossing the {foo}.\n\n:foo!:\n\nBelly up to the {foo}.");
        assert_xpath(&doc, "//p[text()=\"Crossing the bar.\"]", 1);
        assert_xpath(&doc, "//p[text()=\"Belly up to the bar.\"]", 0);
    }

    #[test]
    fn should_allow_compat_mode_to_be_set_and_unset_in_middle_of_document() {
        non_normative!(
            r#"
    test 'should allow compat-mode to be set and unset in middle of document' do
      input = <<~'EOS'
      :foo: bar

      [[paragraph-a]]
      `{foo}`

      :compat-mode!:

      [[paragraph-b]]
      `{foo}`

      :compat-mode:

      [[paragraph-c]]
      `{foo}`
      EOS

      result = convert_string_to_embedded input, attributes: { 'compat-mode' => '@' }
      assert_xpath '/*[@id="paragraph-a"]//code[text()="{foo}"]', result, 1
      assert_xpath '/*[@id="paragraph-b"]//code[text()="bar"]', result, 1
      assert_xpath '/*[@id="paragraph-c"]//code[text()="{foo}"]', result, 1
    end

"#
        );

        let doc = Parser::default()
            .with_intrinsic_attribute("compat-mode", "", ModificationContext::Anywhere)
            .parse(
                ":foo: bar\n\n[[paragraph-a]]\n`{foo}`\n\n:compat-mode!:\n\n[[paragraph-b]]\n`{foo}`\n\n:compat-mode:\n\n[[paragraph-c]]\n`{foo}`",
            );
        // Intentional divergence: this crate does not change backtick/monospace
        // attribute substitution based on compat-mode, so `{foo}` resolves to `bar` in
        // all three paragraphs (Asciidoctor keeps it literal where compat-mode
        // is on, i.e. paragraphs a and c).
        assert_xpath(&doc, "//*[@id=\"paragraph-a\"]//code[text()=\"bar\"]", 1);
        assert_xpath(&doc, "//*[@id=\"paragraph-b\"]//code[text()=\"bar\"]", 1);
        assert_xpath(&doc, "//*[@id=\"paragraph-c\"]//code[text()=\"bar\"]", 1);
    }

    #[test]
    fn does_not_disturb_attribute_looking_things_escaped_with_backslash() {
        verifies!(
            r#"
    test 'does not disturb attribute-looking things escaped with backslash' do
      html = convert_string(":foo: bar\nThis is a \\{foo} day.")
      assert_xpath '//p[text()="This is a {foo} day."]', html, 1
    end

"#
        );

        let doc = Parser::default().parse(":foo: bar\nThis is a \\{foo} day.");
        assert_xpath(&doc, "//p[text()=\"This is a {foo} day.\"]", 1);
    }

    #[test]
    fn does_not_disturb_attribute_looking_things_escaped_with_literals() {
        verifies!(
            r#"
    test 'does not disturb attribute-looking things escaped with literals' do
      html = convert_string(":foo: bar\nThis is a +++{foo}+++ day.")
      assert_xpath '//p[text()="This is a {foo} day."]', html, 1
    end

"#
        );

        let doc = Parser::default().parse(":foo: bar\nThis is a +++{foo}+++ day.");
        assert_xpath(&doc, "//p[text()=\"This is a {foo} day.\"]", 1);
    }

    #[test]
    fn does_not_substitute_attributes_inside_listing_blocks() {
        verifies!(
            r#"
    test 'does not substitute attributes inside listing blocks' do
      input = <<~'EOS'
      :forecast: snow

      ----
      puts 'The forecast for today is {forecast}'
      ----
      EOS
      output = convert_string(input)
      assert_match(/\{forecast\}/, output)
    end

"#
        );

        let doc = Parser::default()
            .parse(":forecast: snow\n\n----\nputs 'The forecast for today is {forecast}'\n----");
        assert_rendered_contains(&doc, "{forecast}");
    }

    #[test]
    fn does_not_substitute_attributes_inside_literal_blocks() {
        verifies!(
            r#"
    test 'does not substitute attributes inside literal blocks' do
      input = <<~'EOS'
      :foo: bar

      ....
      You insert the text {foo} to expand the value
      of the attribute named foo in your document.
      ....
      EOS
      output = convert_string(input)
      assert_match(/\{foo\}/, output)
    end

"#
        );

        let doc = Parser::default().parse(
            ":foo: bar\n\n....\nYou insert the text {foo} to expand the value\nof the attribute named foo in your document.\n....",
        );
        assert_rendered_contains(&doc, "{foo}");
    }

    // Tracked in #735.
    #[test]
    fn does_not_show_docdir_and_shows_relative_docfile_if_safe_mode_is_server_or_greater() {
        non_normative!(
            r#"
    test 'does not show docdir and shows relative docfile if safe mode is SERVER or greater' do
      input = <<~'EOS'
      * docdir: {docdir}
      * docfile: {docfile}
      EOS

      docdir = Dir.pwd
      docfile = File.join(docdir, 'sample.adoc')
      output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => docdir, 'docfile' => docfile }
      assert_xpath '//li[1]/p[text()="docdir: "]', output, 1
      assert_xpath '//li[2]/p[text()="docfile: sample.adoc"]', output, 1
    end

"#
        );

        let doc = Parser::default()
            .with_safe_mode(SafeMode::Server)
            .with_intrinsic_attribute("docdir", "/some/dir", ModificationContext::ApiOnly)
            .with_intrinsic_attribute(
                "docfile",
                "/some/dir/sample.adoc",
                ModificationContext::ApiOnly,
            )
            .parse("* docdir: {docdir}\n* docfile: {docfile}");
        // Divergence: this crate does not apply Asciidoctor's SERVER-safe-mode
        // masking of `docdir` (blanked) and `docfile` (relativized); the
        // API-provided values are substituted verbatim.
        assert_eq!(
            rendered_paragraphs(&doc),
            vec![
                "docdir: /some/dir".to_string(),
                "docfile: /some/dir/sample.adoc".to_string(),
            ]
        );
    }

    #[test]
    fn shows_absolute_docdir_and_docfile_paths_if_safe_mode_is_less_than_server() {
        verifies!(
            r#"
    test 'shows absolute docdir and docfile paths if safe mode is less than SERVER' do
      input = <<~'EOS'
      * docdir: {docdir}
      * docfile: {docfile}
      EOS

      docdir = Dir.pwd
      docfile = File.join(docdir, 'sample.adoc')
      output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => docdir, 'docfile' => docfile }
      assert_xpath %(//li[1]/p[text()="docdir: #{docdir}"]), output, 1
      assert_xpath %(//li[2]/p[text()="docfile: #{docfile}"]), output, 1
    end

"#
        );

        let doc = Parser::default()
            .with_safe_mode(SafeMode::Safe)
            .with_intrinsic_attribute("docdir", "/some/dir", ModificationContext::ApiOnly)
            .with_intrinsic_attribute(
                "docfile",
                "/some/dir/sample.adoc",
                ModificationContext::ApiOnly,
            )
            .parse("* docdir: {docdir}\n* docfile: {docfile}");
        assert_eq!(
            rendered_paragraphs(&doc),
            vec![
                "docdir: /some/dir".to_string(),
                "docfile: /some/dir/sample.adoc".to_string(),
            ]
        );
    }

    #[test]
    fn assigns_attribute_defined_in_attribute_reference_with_set_prefix_and_value() {
        non_normative!(
            r#"
    test 'assigns attribute defined in attribute reference with set prefix and value' do
      input = '{set:foo:bar}{foo}'
      output = convert_string_to_embedded input
      assert_xpath '//p', output, 1
      assert_xpath '//p[text()="bar"]', output, 1
    end

"#
        );

        // Intentional divergence: this crate does not implement the `{set:…}` attribute
        // assignment reference, so it stays literal and `{foo}` is never
        // assigned (it remains an unresolved reference).
        let doc = Parser::default().parse("{set:foo:bar}{foo}");
        assert_xpath(&doc, "//p", 1);
        assert_xpath(&doc, "//p[text()=\"{set:foo:bar}{foo}\"]", 1);
    }

    #[test]
    fn assigns_attribute_defined_in_attribute_reference_with_set_prefix_and_no_value() {
        non_normative!(
            r#"
    test 'assigns attribute defined in attribute reference with set prefix and no value' do
      input = "{set:foo}\n{foo}yes"
      output = convert_string_to_embedded input
      assert_xpath '//p', output, 1
      assert_xpath '//p[normalize-space(text())="yes"]', output, 1
    end

"#
        );

        // Intentional divergence: `{set:…}` is not implemented (see the
        // `…_set_prefix_and_value` test), so it is left literal.
        let doc = Parser::default().parse("{set:foo}\n{foo}yes");
        assert_xpath(&doc, "//p", 1);
        assert_xpath(&doc, "//p[text()=\"{set:foo}\n{foo}yes\"]", 1);
    }

    #[test]
    fn assigns_attribute_defined_in_attribute_reference_with_set_prefix_and_empty_value() {
        non_normative!(
            r#"
    test 'assigns attribute defined in attribute reference with set prefix and empty value' do
      input = "{set:foo:}\n{foo}yes"
      output = convert_string_to_embedded input
      assert_xpath '//p', output, 1
      assert_xpath '//p[normalize-space(text())="yes"]', output, 1
    end

"#
        );

        // Intentional divergence: `{set:…}` is not implemented (see the
        // `…_set_prefix_and_value` test), so it is left literal.
        let doc = Parser::default().parse("{set:foo:}\n{foo}yes");
        assert_xpath(&doc, "//p", 1);
        assert_xpath(&doc, "//p[text()=\"{set:foo:}\n{foo}yes\"]", 1);
    }

    #[test]
    fn unassigns_attribute_defined_in_attribute_reference_with_set_prefix() {
        non_normative!(
            r#"
    test 'unassigns attribute defined in attribute reference with set prefix' do
      input = <<~'EOS'
      :attribute-missing: drop-line
      :foo:

      {set:foo!}
      {foo}yes
      EOS
      output = convert_string_to_embedded input
      assert_xpath '//p', output, 1
      assert_xpath '//p/child::text()', output, 0
      assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: foo'
    end
"#
        );

        // Intentional divergence: `{set:foo!}` unassignment is not implemented; it
        // stays literal, and because `foo` is still set (to empty) `{foo}`
        // resolves to nothing, so no line is dropped.
        let doc =
            Parser::default().parse(":attribute-missing: drop-line\n:foo:\n\n{set:foo!}\n{foo}yes");
        assert_eq!(
            rendered_paragraphs(&doc),
            vec!["{set:foo!}\nyes".to_string()]
        );
    }
}

non_normative!(
    r#"
  end

  context "Intrinsic attributes" do
"#
);
mod intrinsic_attributes {
    use crate::tests::prelude::*;

    /// The rendered lines of the document's first raw (passthrough) block. The
    /// counter-increment tests wrap their references in a `[subs=attributes]`
    /// passthrough block, so the output is that block's substituted text split
    /// into lines (Asciidoctor's `output.lines.map(&:rstrip)`).
    fn raw_block_lines(doc: &crate::Document<'_>) -> Vec<String> {
        for b in doc.nested_blocks() {
            if let crate::blocks::Block::RawDelimited(raw) = b {
                return raw
                    .content()
                    .rendered()
                    .lines()
                    .map(str::to_string)
                    .collect();
            }
        }
        vec![]
    }

    non_normative!(
        r#"

"#
    );
    // Out of scope: iterates Ruby's `Asciidoctor::INTRINSIC_ATTRIBUTES` constant,
    // which this crate does not expose as an enumerable set.
    non_normative!(
        r#"
    test "substitute intrinsics" do
      Asciidoctor::INTRINSIC_ATTRIBUTES.each_pair do |key, value|
        html = convert_string("Look, a {#{key}} is here")
        # can't use Nokogiri because it interprets the HTML entities and we can't match them
        assert_match(/Look, a #{Regexp.escape(value)} is here/, html)
      end
    end

"#
    );

    #[test]
    fn don_t_escape_intrinsic_substitutions() {
        verifies!(
            r#"
    test "don't escape intrinsic substitutions" do
      html = convert_string('happy{nbsp}together')
      assert_match(/happy&#160;together/, html)
    end

"#
        );

        let doc = Parser::default().parse("happy{nbsp}together");
        assert!(
            rendered_paragraphs(&doc)
                .iter()
                .any(|p| p.contains("happy&#160;together"))
        );
    }

    #[test]
    fn escape_special_characters() {
        verifies!(
            r#"
    test "escape special characters" do
      html = convert_string('<node>&</node>')
      assert_match(/&lt;node&gt;&amp;&lt;\/node&gt;/, html)
    end

"#
        );

        let doc = Parser::default().parse("<node>&</node>");
        assert!(
            rendered_paragraphs(&doc)
                .iter()
                .any(|p| p.contains("&lt;node&gt;&amp;&lt;/node&gt;"))
        );
    }

    #[test]
    fn creates_counter() {
        verifies!(
            r#"
    test 'creates counter' do
      input = '{counter:mycounter}'

      doc = document_from_string input
      output = doc.convert
      assert_equal 1, doc.attributes['mycounter']
      assert_xpath '//p[text()="1"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse("{counter:mycounter}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("1")
        );
        assert_xpath(&doc, "//p[text()=\"1\"]", 1);
    }

    #[test]
    fn creates_counter_silently() {
        verifies!(
            r#"
    test 'creates counter silently' do
      input = '{counter2:mycounter}'

      doc = document_from_string input
      output = doc.convert
      assert_equal 1, doc.attributes['mycounter']
      assert_xpath '//p[text()="1"]', output, 0
    end

"#
        );

        let doc = Parser::default().parse("{counter2:mycounter}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("1")
        );
        assert_xpath(&doc, "//p[text()=\"1\"]", 0);
    }

    #[test]
    fn creates_counter_with_numeric_seed_value() {
        verifies!(
            r#"
    test 'creates counter with numeric seed value' do
      input = '{counter2:mycounter:10}'

      doc = document_from_string input
      doc.convert
      assert_equal 10, doc.attributes['mycounter']
    end

"#
        );

        let doc = Parser::default().parse("{counter2:mycounter:10}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("10")
        );
    }

    #[test]
    fn creates_counter_with_character_seed_value() {
        verifies!(
            r#"
    test 'creates counter with character seed value' do
      input = '{counter2:mycounter:A}'

      doc = document_from_string input
      doc.convert
      assert_equal 'A', doc.attributes['mycounter']
    end

"#
        );

        let doc = Parser::default().parse("{counter2:mycounter:A}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("A")
        );
    }

    #[test]
    fn can_seed_counter_to_start_at_1() {
        verifies!(
            r#"
    test 'can seed counter to start at 1' do
      input = <<~'EOS'
      :mycounter: 0

      {counter:mycounter}
      EOS

      output = convert_string_to_embedded input
      assert_xpath '//p[text()="1"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(":mycounter: 0\n\n{counter:mycounter}");
        assert_xpath(&doc, "//p[text()=\"1\"]", 1);
    }

    #[test]
    fn can_seed_counter_to_start_at_a() {
        verifies!(
            r#"
    test 'can seed counter to start at A' do
      input = <<~'EOS'
      :mycounter: @

      {counter:mycounter}
      EOS

      output = convert_string_to_embedded input
      assert_xpath '//p[text()="A"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(":mycounter: @\n\n{counter:mycounter}");
        assert_xpath(&doc, "//p[text()=\"A\"]", 1);
    }

    #[test]
    fn increments_counter_with_positive_numeric_value() {
        verifies!(
            r#"
    test 'increments counter with positive numeric value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:mycounter:1}
      {counter:mycounter}
      {counter:mycounter}
      {mycounter}
      ++++
      EOS

      doc = document_from_string input, standalone: false
      output = doc.convert
      assert_equal 3, doc.attributes['mycounter']
      assert_equal %w(1 2 3 3), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:mycounter:1}\n{counter:mycounter}\n{counter:mycounter}\n{mycounter}\n++++",
        );
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("3")
        );
        assert_eq!(raw_block_lines(&doc), vec!["1", "2", "3", "3"]);
    }

    #[test]
    fn increments_counter_with_negative_numeric_value() {
        verifies!(
            r#"
    test 'increments counter with negative numeric value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:mycounter:-2}
      {counter:mycounter}
      {counter:mycounter}
      {mycounter}
      ++++
      EOS

      doc = document_from_string input, standalone: false
      output = doc.convert
      assert_equal 0, doc.attributes['mycounter']
      assert_equal %w(-2 -1 0 0), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:mycounter:-2}\n{counter:mycounter}\n{counter:mycounter}\n{mycounter}\n++++",
        );
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("0")
        );
        assert_eq!(raw_block_lines(&doc), vec!["-2", "-1", "0", "0"]);
    }

    #[test]
    fn increments_counter_with_ascii_character_value() {
        verifies!(
            r#"
    test 'increments counter with ASCII character value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:mycounter:A}
      {counter:mycounter}
      {counter:mycounter}
      {mycounter}
      ++++
      EOS

      output = convert_string_to_embedded input
      assert_equal %w(A B C C), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:mycounter:A}\n{counter:mycounter}\n{counter:mycounter}\n{mycounter}\n++++",
        );
        assert_eq!(raw_block_lines(&doc), vec!["A", "B", "C", "C"]);
    }

    #[test]
    fn increments_counter_with_non_ascii_character_value() {
        verifies!(
            r#"
    test 'increments counter with non-ASCII character value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:mycounter:é}
      {counter:mycounter}
      {counter:mycounter}
      {mycounter}
      ++++
      EOS

      output = convert_string_to_embedded input
      assert_equal %w(é ê ë ë), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:mycounter:é}\n{counter:mycounter}\n{counter:mycounter}\n{mycounter}\n++++",
        );
        assert_eq!(raw_block_lines(&doc), vec!["é", "ê", "ë", "ë"]);
    }

    #[test]
    fn increments_counter_with_emoji_character_value() {
        verifies!(
            r#"
    test 'increments counter with emoji character value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:smiley:😋}
      {counter:smiley}
      {counter:smiley}
      {smiley}
      ++++
      EOS

      output = convert_string_to_embedded input
      assert_equal %w(😋 😌 😍 😍), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:smiley:😋}\n{counter:smiley}\n{counter:smiley}\n{smiley}\n++++",
        );
        assert_eq!(raw_block_lines(&doc), vec!["😋", "😌", "😍", "😍"]);
    }

    #[test]
    fn increments_counter_with_multi_character_value() {
        verifies!(
            r#"
    test 'increments counter with multi-character value' do
      input = <<~'EOS'
      [subs=attributes]
      ++++
      {counter:math:1x}
      {counter:math}
      {counter:math}
      {math}
      ++++
      EOS

      output = convert_string_to_embedded input
      assert_equal %w(1x 1y 1z 1z), output.lines.map {|l| l.rstrip }
    end

"#
        );

        let doc = Parser::default().parse(
            "[subs=attributes]\n++++\n{counter:math:1x}\n{counter:math}\n{counter:math}\n{math}\n++++",
        );
        assert_eq!(raw_block_lines(&doc), vec!["1x", "1y", "1z", "1z"]);
    }

    #[test]
    fn counter_uses_0_as_seed_value_if_seed_attribute_is_nil() {
        verifies!(
            r#"
    test 'counter uses 0 as seed value if seed attribute is nil' do
      input = <<~'EOS'
      :mycounter:

      {counter:mycounter}

      {mycounter}
      EOS

      doc = document_from_string input
      output = doc.convert standalone: false
      assert_equal 1, doc.attributes['mycounter']
      assert_xpath '//p[text()="1"]', output, 2
    end

"#
        );

        let doc = Parser::default().parse(":mycounter:\n\n{counter:mycounter}\n\n{mycounter}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("1")
        );
        assert_xpath(&doc, "//p[text()=\"1\"]", 2);
    }

    #[test]
    fn counter_value_can_be_reset_by_attribute_entry() {
        verifies!(
            r#"
    test 'counter value can be reset by attribute entry' do
      input = <<~'EOS'
      :mycounter:

      before: {counter:mycounter} {counter:mycounter} {counter:mycounter}

      :mycounter!:

      after: {counter:mycounter}
      EOS

      doc = document_from_string input
      output = doc.convert standalone: false
      assert_equal 1, doc.attributes['mycounter']
      assert_xpath '//p[text()="before: 1 2 3"]', output, 1
      assert_xpath '//p[text()="after: 1"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(
            ":mycounter:\n\nbefore: {counter:mycounter} {counter:mycounter} {counter:mycounter}\n\n:mycounter!:\n\nafter: {counter:mycounter}",
        );
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("1")
        );
        assert_xpath(&doc, "//p[text()=\"before: 1 2 3\"]", 1);
        assert_xpath(&doc, "//p[text()=\"after: 1\"]", 1);
    }

    #[test]
    fn counter_value_can_be_advanced_by_attribute_entry() {
        verifies!(
            r#"
    test 'counter value can be advanced by attribute entry' do
      input = <<~'EOS'
      before: {counter:mycounter}

      :mycounter: 10

      after: {counter:mycounter}
      EOS

      doc = document_from_string input
      output = doc.convert standalone: false
      assert_equal 11, doc.attributes['mycounter']
      assert_xpath '//p[text()="before: 1"]', output, 1
      assert_xpath '//p[text()="after: 11"]', output, 1
    end

"#
        );

        let doc = Parser::default()
            .parse("before: {counter:mycounter}\n\n:mycounter: 10\n\nafter: {counter:mycounter}");
        assert_eq!(
            doc.attribute_value("mycounter"),
            InterpretedValue::Value("11")
        );
        assert_xpath(&doc, "//p[text()=\"before: 1\"]", 1);
        assert_xpath(&doc, "//p[text()=\"after: 11\"]", 1);
    }

    #[test]
    fn nested_document_should_use_counter_from_parent_document() {
        non_normative!(
            r#"
    test 'nested document should use counter from parent document' do
      input = <<~'EOS'
      .Title for Foo
      image::foo.jpg[]

      [cols="2*a"]
      |===
      |
      .Title for Bar
      image::bar.jpg[]

      |
      .Title for Baz
      image::baz.jpg[]
      |===

      .Title for Qux
      image::qux.jpg[]
      EOS

      output = convert_string_to_embedded input
      assert_xpath '//div[@class="title"]', output, 4
      assert_xpath '//div[@class="title"][text() = "Figure 1. Title for Foo"]', output, 1
      assert_xpath '//div[@class="title"][text() = "Figure 2. Title for Bar"]', output, 1
      assert_xpath '//div[@class="title"][text() = "Figure 3. Title for Baz"]', output, 1
      assert_xpath '//div[@class="title"][text() = "Figure 4. Title for Qux"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse(
            ".Title for Foo\nimage::foo.jpg[]\n\n[cols=\"2*a\"]\n|===\n|\n.Title for Bar\nimage::bar.jpg[]\n\n|\n.Title for Baz\nimage::baz.jpg[]\n|===\n\n.Title for Qux\nimage::qux.jpg[]",
        );
        // This crate exposes a block's caption/number via the `caption()` /
        // `number()` accessors rather than as a `<div class="title">` DOM node,
        // so the shared-counter behavior is verified through those accessors.
        // The figure counter is shared with the nested table-cell documents: the
        // two top-level images are numbered 1 and 4, leaving 2 and 3 for the
        // images inside the table cells.
        let blocks: Vec<_> = doc.nested_blocks().collect();
        assert_eq!(blocks[0].title(), Some("Title for Foo"));
        assert_eq!(blocks[0].caption(), Some("Figure 1. "));
        assert_eq!(blocks[2].title(), Some("Title for Qux"));
        assert_eq!(blocks[2].caption(), Some("Figure 4. "));
    }

    // Tracked in #725.
    #[test]
    fn should_not_allow_counter_to_modify_locked_attribute() {
        non_normative!(
            r#"
    test 'should not allow counter to modify locked attribute' do
      input = <<~'EOS'
      {counter:foo:ignored} is not {foo}
      EOS

      output = convert_string_to_embedded input, attributes: { 'foo' => 'bar' }
      assert_xpath '//p[text()="bas is not bar"]', output, 1
    end

"#
        );

        // Divergence: Asciidoctor does not let a counter modify a locked
        // (API-set) attribute, so `{foo}` stays `bar`. This crate's counter
        // reads and increments the current value (`bar` → `bas`) and also stores
        // it, so both references render `bas`.
        let doc = Parser::default()
            .with_intrinsic_attribute("foo", "bar", ModificationContext::ApiOnly)
            .parse("{counter:foo:ignored} is not {foo}");
        assert_xpath(&doc, "//p[text()=\"bas is not bas\"]", 1);
    }

    // Tracked in #725.
    #[test]
    fn should_not_allow_counter2_to_modify_locked_attribute() {
        non_normative!(
            r#"
    test 'should not allow counter2 to modify locked attribute' do
      input = <<~'EOS'
      {counter2:foo:ignored}{foo}
      EOS

      output = convert_string_to_embedded input, attributes: { 'foo' => 'bar' }
      assert_xpath '//p[text()="bar"]', output, 1
    end

"#
        );

        // Divergence: as with `counter`, this crate's `counter2` modifies the
        // locked `foo` (Asciidoctor leaves it `bar`), so `{foo}` renders `bas`.
        let doc = Parser::default()
            .with_intrinsic_attribute("foo", "bar", ModificationContext::ApiOnly)
            .parse("{counter2:foo:ignored}{foo}");
        assert_xpath(&doc, "//p[text()=\"bas\"]", 1);
    }

    // Tracked in #725.
    #[test]
    fn should_not_allow_counter_to_modify_built_in_locked_attribute() {
        non_normative!(
            r#"
    test 'should not allow counter to modify built-in locked attribute' do
      input = <<~'EOS'
      {counter:max-include-depth:128} is one more than {max-include-depth}
      EOS

      doc = document_from_string input, standalone: false
      output = doc.convert
      assert_xpath '//p[text()="65 is one more than 64"]', output, 1
      assert_equal 64, doc.attributes['max-include-depth']
    end

"#
        );

        // Divergence: Asciidoctor does not let a counter modify the built-in
        // locked `max-include-depth`, so it stays 64. This crate increments and
        // stores it, so both the counter output and the later reference read 65.
        let doc = Parser::default()
            .parse("{counter:max-include-depth:128} is one more than {max-include-depth}");
        assert_xpath(&doc, "//p[text()=\"65 is one more than 65\"]", 1);
        assert_eq!(
            doc.attribute_value("max-include-depth"),
            InterpretedValue::Value("65")
        );
    }

    // Tracked in #725.
    #[test]
    fn should_not_allow_counter2_to_modify_built_in_locked_attribute() {
        non_normative!(
            r#"
    test 'should not allow counter2 to modify built-in locked attribute' do
      input = <<~'EOS'
      {counter2:max-include-depth:128}{max-include-depth}
      EOS

      doc = document_from_string input, standalone: false
      output = doc.convert
      assert_xpath '//p[text()="64"]', output, 1
      assert_equal 64, doc.attributes['max-include-depth']
    end
"#
        );

        // Divergence: as with `counter`, this crate's `counter2` modifies the
        // built-in locked `max-include-depth` (Asciidoctor leaves it 64).
        let doc = Parser::default().parse("{counter2:max-include-depth:128}{max-include-depth}");
        assert_xpath(&doc, "//p[text()=\"65\"]", 1);
        assert_eq!(
            doc.attribute_value("max-include-depth"),
            InterpretedValue::Value("65")
        );
    }
}

non_normative!(
    r#"
  end

  context 'Block attributes' do
"#
);
mod block_attributes {
    use crate::tests::prelude::*;

    /// The document's first top-level block.
    fn first_block<'src>(doc: &'src crate::Document) -> &'src crate::blocks::Block<'src> {
        doc.nested_blocks().next().expect("expected a block")
    }

    /// The document's first top-level block, as a [`QuoteBlock`].
    ///
    /// [`QuoteBlock`]: crate::blocks::QuoteBlock
    fn first_quote<'src>(doc: &'src crate::Document) -> &'src crate::blocks::QuoteBlock<'src> {
        match first_block(doc) {
            crate::blocks::Block::Quote(q) => q,
            b => panic!("expected a quote block, got: {b:?}"),
        }
    }

    /// Value of a block's named attribute, if present.
    fn named<'src>(block: &'src crate::blocks::Block<'src>, name: &str) -> Option<&'src str> {
        block
            .attrlist()
            .and_then(|al| al.named_attribute(name))
            .map(|a| a.value())
    }

    // Tracked in #727.
    #[test]
    fn parses_attribute_names_as_name_token() {
        non_normative!(
            r#"
    test 'parses attribute names as name token' do
      input = <<~'EOS'
      [normal,foo="bar",_foo="_bar",foo1="bar1",foo-foo="bar-bar",foo.foo="bar.bar"]
      content
      EOS

      block = block_from_string input
      assert_equal 'bar', block.attr('foo')
      assert_equal '_bar', block.attr('_foo')
      assert_equal 'bar1', block.attr('foo1')
      assert_equal 'bar-bar', block.attr('foo-foo')
      assert_equal 'bar.bar', block.attr('foo.foo')
    end

"#
        );

        let doc = Parser::default().parse(
            "[normal,foo=\"bar\",_foo=\"_bar\",foo1=\"bar1\",foo-foo=\"bar-bar\",foo.foo=\"bar.bar\"]\ncontent",
        );
        // Divergence: this crate does not accept attribute names that begin with
        // `_` or contain `.` (`_foo`, `foo.foo`) as named tokens, so those
        // entries are not recognized. `foo`, `foo1`, and `foo-foo` parse as
        // expected.
        let block = first_block(&doc);
        assert_eq!(named(block, "foo"), Some("bar"));
        assert_eq!(named(block, "foo1"), Some("bar1"));
        assert_eq!(named(block, "foo-foo"), Some("bar-bar"));
        assert_eq!(named(block, "_foo"), None);
        assert_eq!(named(block, "foo.foo"), None);
    }

    #[test]
    fn positional_attributes_assigned_to_block() {
        verifies!(
            r#"
    test 'positional attributes assigned to block' do
      input = <<~'EOS'
      [quote, author, source]
      ____
      A famous quote.
      ____
      EOS
      doc = document_from_string(input)
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attr('attribution')
      assert_equal 'author', qb.attr(:attribution)
      assert_equal 'author', qb.attributes['attribution']
      assert_equal 'source', qb.attributes['citetitle']
    end

"#
        );

        let doc = Parser::default().parse("[quote, author, source]\n____\nA famous quote.\n____");
        let qb = first_quote(&doc);
        assert_eq!(qb.declared_style(), Some("quote"));
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(qb.citetitle(), Some("source"));
    }

    #[test]
    fn normal_substitutions_are_performed_on_single_quoted_positional_attribute() {
        verifies!(
            r#"
    test 'normal substitutions are performed on single-quoted positional attribute' do
      input = <<~'EOS'
      [quote, author, 'http://wikipedia.org[source]']
      ____
      A famous quote.
      ____
      EOS
      doc = document_from_string(input)
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attr('attribution')
      assert_equal 'author', qb.attr(:attribution)
      assert_equal 'author', qb.attributes['attribution']
      assert_equal '<a href="http://wikipedia.org">source</a>', qb.attributes['citetitle']
    end

"#
        );

        let doc = Parser::default()
            .parse("[quote, author, 'http://wikipedia.org[source]']\n____\nA famous quote.\n____");
        let qb = first_quote(&doc);
        assert_eq!(qb.declared_style(), Some("quote"));
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(
            qb.citetitle(),
            Some("<a href=\"http://wikipedia.org\">source</a>")
        );
    }

    #[test]
    fn normal_substitutions_are_performed_on_single_quoted_named_attribute() {
        verifies!(
            r#"
    test 'normal substitutions are performed on single-quoted named attribute' do
      input = <<~'EOS'
      [quote, author, citetitle='http://wikipedia.org[source]']
      ____
      A famous quote.
      ____
      EOS
      doc = document_from_string(input)
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attr('attribution')
      assert_equal 'author', qb.attr(:attribution)
      assert_equal 'author', qb.attributes['attribution']
      assert_equal '<a href="http://wikipedia.org">source</a>', qb.attributes['citetitle']
    end

"#
        );

        let doc = Parser::default().parse(
            "[quote, author, citetitle='http://wikipedia.org[source]']\n____\nA famous quote.\n____",
        );
        let qb = first_quote(&doc);
        assert_eq!(qb.declared_style(), Some("quote"));
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(
            qb.citetitle(),
            Some("<a href=\"http://wikipedia.org\">source</a>")
        );
    }

    #[test]
    fn normal_substitutions_are_performed_once_on_single_quoted_named_title_attribute() {
        verifies!(
            r#"
    test 'normal substitutions are performed once on single-quoted named title attribute' do
      input = <<~'EOS'
      [title='*title*']
      content
      EOS
      output = convert_string_to_embedded input
      assert_xpath '//*[@class="title"]/strong[text()="title"]', output, 1
    end

"#
        );

        // The block title is exposed via the `title()` accessor rather than as a
        // `<div class="title">` DOM node; the single-quoted title has normal
        // substitutions applied once (`*title*` → `<strong>title</strong>`).
        let doc = Parser::default().parse("[title='*title*']\ncontent");
        assert_eq!(first_block(&doc).title(), Some("<strong>title</strong>"));
    }

    #[test]
    fn attribute_list_may_not_begin_with_space() {
        verifies!(
            r#"
    test 'attribute list may not begin with space' do
      input = <<~'EOS'
      [ quote]
      ____
      A famous quote.
      ____
      EOS

      doc = document_from_string input
      b1 = doc.blocks.first
      assert_equal ['[ quote]'], b1.lines
    end

"#
        );

        // `[ quote]` (leading space) is not a valid attribute list, so the line
        // is kept as literal paragraph text.
        let doc = Parser::default().parse("[ quote]\n____\nA famous quote.\n____");
        assert_rendered_contains(&doc, "[ quote]");
    }

    #[test]
    fn attribute_list_may_begin_with_comma() {
        verifies!(
            r#"
    test 'attribute list may begin with comma' do
      input = <<~'EOS'
      [, author, source]
      ____
      A famous quote.
      ____
      EOS

      doc = document_from_string input
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attributes['attribution']
      assert_equal 'source', qb.attributes['citetitle']
    end

"#
        );

        // The block is a quote (delimited by `____`) even though the style
        // positional is empty; `declared_style()` reflects only an explicitly
        // declared bare style, so it is `None` here — the positional
        // attribution/citetitle are what this test cares about.
        let doc = Parser::default().parse("[, author, source]\n____\nA famous quote.\n____");
        let qb = first_quote(&doc);
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(qb.citetitle(), Some("source"));
    }

    #[test]
    fn first_attribute_in_list_may_be_double_quoted() {
        verifies!(
            r#"
    test 'first attribute in list may be double quoted' do
      input = <<~'EOS'
      ["quote", "author", "source", role="famous"]
      ____
      A famous quote.
      ____
      EOS

      doc = document_from_string input
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attributes['attribution']
      assert_equal 'source', qb.attributes['citetitle']
      assert_equal 'famous', qb.attributes['role']
    end

"#
        );

        // A quoted style token (`"quote"`) still produces a quote block, but
        // `declared_style()` reports `None` (it reflects only a bare declared
        // style); the positional attribution/citetitle and role are verified.
        let doc = Parser::default().parse(
            "[\"quote\", \"author\", \"source\", role=\"famous\"]\n____\nA famous quote.\n____",
        );
        let qb = first_quote(&doc);
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(qb.citetitle(), Some("source"));
        assert_eq!(qb.roles(), vec!["famous"]);
    }

    #[test]
    fn first_attribute_in_list_may_be_single_quoted() {
        verifies!(
            r#"
    test 'first attribute in list may be single quoted' do
      input = <<~'EOS'
      ['quote', 'author', 'source', role='famous']
      ____
      A famous quote.
      ____
      EOS

      doc = document_from_string input
      qb = doc.blocks.first
      assert_equal 'quote', qb.style
      assert_equal 'author', qb.attributes['attribution']
      assert_equal 'source', qb.attributes['citetitle']
      assert_equal 'famous', qb.attributes['role']
    end

"#
        );

        // As with the double-quoted form, a single-quoted style token produces a
        // quote block whose `declared_style()` is `None`.
        let doc = Parser::default()
            .parse("['quote', 'author', 'source', role='famous']\n____\nA famous quote.\n____");
        let qb = first_quote(&doc);
        assert_eq!(qb.attribution(), Some("author"));
        assert_eq!(qb.citetitle(), Some("source"));
        assert_eq!(qb.roles(), vec!["famous"]);
    }

    #[test]
    fn attribute_with_value_none_without_quotes_is_ignored() {
        verifies!(
            r#"
    test 'attribute with value None without quotes is ignored' do
      input = <<~'EOS'
      [id=None]
      paragraph
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      refute para.attributes.key?('id')
    end

"#
        );

        let doc = Parser::default().parse("[id=None]\nparagraph");
        assert_eq!(first_block(&doc).id(), None);
    }

    #[test]
    fn role_returns_true_if_role_is_assigned() {
        verifies!(
            r#"
    test 'role? returns true if role is assigned' do
      input = <<~'EOS'
      [role="lead"]
      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      assert p.role?
    end

"#
        );

        // Ruby's `role?` (no argument) maps to "has any role" here.
        let doc = Parser::default().parse("[role=\"lead\"]\nA paragraph");
        assert!(!first_block(&doc).roles().is_empty());
    }

    #[test]
    fn role_does_not_return_true_if_role_attribute_is_set_on_document() {
        verifies!(
            r#"
    test 'role? does not return true if role attribute is set on document' do
      input = <<~'EOS'
      :role: lead

      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      refute p.role?
    end

"#
        );

        let doc = Parser::default().parse(":role: lead\n\nA paragraph");
        assert!(first_block(&doc).roles().is_empty());
    }

    #[test]
    fn role_can_check_for_exact_role_name_match() {
        verifies!(
            r#"
    test 'role? can check for exact role name match' do
      input = <<~'EOS'
      [role="lead"]
      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      assert p.role?('lead')
      p2 = doc.blocks.last
      refute p2.role?('final')
    end

"#
        );

        // Ruby's `role?(name)` matches when the whole `role` attribute equals
        // `name`; here that is `roles() == [name]`.
        let doc = Parser::default().parse("[role=\"lead\"]\nA paragraph");
        let p = first_block(&doc);
        assert_eq!(p.roles(), vec!["lead"]);
        assert_ne!(p.roles(), vec!["final"]);
    }

    #[test]
    fn has_role_can_check_for_presence_of_role_name() {
        verifies!(
            r#"
    test 'has_role? can check for presence of role name' do
      input = <<~'EOS'
      [role="lead abstract"]
      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      refute p.role?('lead')
      assert p.has_role?('lead')
    end

"#
        );

        // `role?('lead')` is false (the whole role string is "lead abstract"),
        // but `has_role?('lead')` (membership) is true.
        let doc = Parser::default().parse("[role=\"lead abstract\"]\nA paragraph");
        let p = first_block(&doc);
        assert_ne!(p.roles().join(" "), "lead");
        assert!(p.roles().contains(&"lead"));
    }

    #[test]
    fn has_role_does_not_look_for_role_defined_as_document_attribute() {
        verifies!(
            r#"
    test 'has_role? does not look for role defined as document attribute' do
      input = <<~'EOS'
      :role: lead abstract

      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      refute p.has_role?('lead')
    end

"#
        );

        let doc = Parser::default().parse(":role: lead abstract\n\nA paragraph");
        assert!(!first_block(&doc).roles().contains(&"lead"));
    }

    #[test]
    fn roles_returns_array_of_role_names() {
        verifies!(
            r#"
    test 'roles returns array of role names' do
      input = <<~'EOS'
      [role="story lead"]
      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      assert_equal ['story', 'lead'], p.roles
    end

"#
        );

        let doc = Parser::default().parse("[role=\"story lead\"]\nA paragraph");
        assert_eq!(first_block(&doc).roles(), vec!["story", "lead"]);
    }

    #[test]
    fn roles_returns_empty_array_if_role_attribute_is_not_set() {
        verifies!(
            r#"
    test 'roles returns empty array if role attribute is not set' do
      input = 'a paragraph'

      doc = document_from_string input
      p = doc.blocks.first
      assert_equal [], p.roles
    end

"#
        );

        let doc = Parser::default().parse("a paragraph");
        assert!(first_block(&doc).roles().is_empty());
    }

    #[test]
    fn roles_does_not_return_value_of_roles_document_attribute() {
        verifies!(
            r#"
    test 'roles does not return value of roles document attribute' do
      input = <<~'EOS'
      :role: story lead

      A paragraph
      EOS

      doc = document_from_string input
      p = doc.blocks.first
      assert_equal [], p.roles
    end

"#
        );

        let doc = Parser::default().parse(":role: story lead\n\nA paragraph");
        assert!(first_block(&doc).roles().is_empty());
    }

    // Out of scope: exercises Ruby's mutable `role=` setter on a node, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'roles= sets the role attribute on the node' do
      doc = document_from_string 'a paragraph'
      p = doc.blocks.first
      p.role = 'foobar'
      assert_equal 'foobar', (p.attr 'role')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `role=` setter on a node, which this
    // crate does not expose.
    non_normative!(
        r#"
    test 'roles= coerces array value to a space-separated string' do
      doc = document_from_string 'a paragraph'
      p = doc.blocks.first
      p.role = %w(foo bar)
      assert_equal 'foo bar', (p.attr 'role')
    end

"#
    );

    #[test]
    fn attribute_substitutions_are_performed_on_attribute_list_before_parsing_attributes() {
        verifies!(
            r#"
    test "Attribute substitutions are performed on attribute list before parsing attributes" do
      input = <<~'EOS'
      :lead: role="lead"

      [{lead}]
      A paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      assert_equal 'lead', para.attributes['role']
    end

"#
        );

        let doc = Parser::default().parse(":lead: role=\"lead\"\n\n[{lead}]\nA paragraph");
        assert_eq!(first_block(&doc).roles(), vec!["lead"]);
    }

    #[test]
    fn id_role_and_options_attributes_can_be_specified_on_block_style_using_shorthand_syntax() {
        verifies!(
            r#"
    test 'id, role and options attributes can be specified on block style using shorthand syntax' do
      input = <<~'EOS'
      [literal#first.lead%step]
      A literal paragraph.
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      assert_equal :literal, para.context
      assert_equal 'first', para.attributes['id']
      assert_equal 'lead', para.attributes['role']
      assert para.attributes.key?('step-option')
      refute para.attributes.key?('options')
    end

"#
        );

        let doc = Parser::default().parse("[literal#first.lead%step]\nA literal paragraph.");
        let p = first_block(&doc);
        assert_eq!(p.declared_style(), Some("literal"));
        assert_eq!(p.id(), Some("first"));
        assert_eq!(p.roles(), vec!["lead"]);
        assert!(p.has_option("step"));
        assert_eq!(named(p, "options"), None);
    }

    #[test]
    fn id_role_and_options_attributes_can_be_specified_using_shorthand_syntax_on_block_style_using_multiple_block_attribute_lines()
     {
        verifies!(
            r#"
    test 'id, role and options attributes can be specified using shorthand syntax on block style using multiple block attribute lines' do
      input = <<~'EOS'
      [literal]
      [#first]
      [.lead]
      [%step]
      A literal paragraph.
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      assert_equal :literal, para.context
      assert_equal 'first', para.attributes['id']
      assert_equal 'lead', para.attributes['role']
      assert para.attributes.key?('step-option')
      refute para.attributes.key?('options')
    end

"#
        );

        let doc =
            Parser::default().parse("[literal]\n[#first]\n[.lead]\n[%step]\nA literal paragraph.");
        let p = first_block(&doc);
        assert_eq!(p.declared_style(), Some("literal"));
        assert_eq!(p.id(), Some("first"));
        assert_eq!(p.roles(), vec!["lead"]);
        assert!(p.has_option("step"));
        assert_eq!(named(p, "options"), None);
    }

    #[test]
    fn multiple_roles_and_options_can_be_specified_in_block_style_using_shorthand_syntax() {
        verifies!(
            r#"
    test 'multiple roles and options can be specified in block style using shorthand syntax' do
      input = <<~'EOS'
      [.role1%option1.role2%option2]
      Text
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      assert_equal 'role1 role2', para.attributes['role']
      assert para.attributes.key?('option1-option')
      assert para.attributes.key?('option2-option')
      refute para.attributes.key?('options')
    end

"#
        );

        let doc = Parser::default().parse("[.role1%option1.role2%option2]\nText");
        let p = first_block(&doc);
        assert_eq!(p.roles(), vec!["role1", "role2"]);
        assert!(p.has_option("option1"));
        assert!(p.has_option("option2"));
        assert_eq!(named(p, "options"), None);
    }

    #[test]
    fn options_specified_using_shorthand_syntax_on_block_style_across_multiple_lines_should_be_additive()
     {
        verifies!(
            r#"
    test 'options specified using shorthand syntax on block style across multiple lines should be additive' do
      input = <<~'EOS'
      [%option1]
      [%option2]
      Text
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      assert para.attributes.key?('option1-option')
      assert para.attributes.key?('option2-option')
      refute para.attributes.key?('options')
    end

"#
        );

        let doc = Parser::default().parse("[%option1]\n[%option2]\nText");
        let p = first_block(&doc);
        assert!(p.has_option("option1"));
        assert!(p.has_option("option2"));
        assert_eq!(named(p, "options"), None);
    }

    #[test]
    fn roles_specified_using_shorthand_syntax_on_block_style_across_multiple_lines_should_be_additive()
     {
        verifies!(
            r#"
    test 'roles specified using shorthand syntax on block style across multiple lines should be additive' do
      input = <<~'EOS'
      [.role1]
      [.role2.role3]
      Text
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      assert_equal 'role1 role2 role3', para.attributes['role']
    end

"#
        );

        let doc = Parser::default().parse("[.role1]\n[.role2.role3]\nText");
        assert_eq!(first_block(&doc).roles(), vec!["role1", "role2", "role3"]);
    }

    // Tracked in #732.
    #[test]
    fn setting_a_role_using_the_role_attribute_replaces_any_existing_roles() {
        non_normative!(
            r#"
    test 'setting a role using the role attribute replaces any existing roles' do
      input = <<~'EOS'
      [.role1]
      [role=role2]
      [.role3]
      Text
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      assert_equal 'role2 role3', para.attributes['role']
    end

"#
        );

        // Divergence: in Asciidoctor a formal `role=` entry replaces any roles
        // already set by shorthand `.role`, so `.role1` is cleared and the
        // result is `role2 role3`. This crate treats every role source as
        // additive, so all three accumulate (in encounter order).
        let doc = Parser::default().parse("[.role1]\n[role=role2]\n[.role3]\nText");
        assert_eq!(first_block(&doc).roles(), vec!["role1", "role3", "role2"]);
    }

    #[test]
    fn setting_a_role_using_the_shorthand_syntax_on_block_style_should_not_clear_the_id() {
        verifies!(
            r#"
    test 'setting a role using the shorthand syntax on block style should not clear the ID' do
      input = <<~'EOS'
      [#id]
      [.role]
      Text
      EOS

      doc = document_from_string input
      para = doc.blocks.first
      assert_equal 'id', para.id
      assert_equal 'role', para.role
    end

"#
        );

        let doc = Parser::default().parse("[#id]\n[.role]\nText");
        let p = first_block(&doc);
        assert_eq!(p.id(), Some("id"));
        assert_eq!(p.roles(), vec!["role"]);
    }

    // Out of scope: exercises Ruby's mutable `add_role` API, which this crate does
    // not expose.
    non_normative!(
        r#"
    test 'a role can be added using add_role when the node has no roles' do
      input = 'A normal paragraph'
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.add_role 'role1'
      assert res
      assert_equal 'role1', para.attributes['role']
      assert para.has_role? 'role1'
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `add_role` API, which this crate does
    // not expose.
    non_normative!(
        r#"
    test 'a role can be added using add_role when the node already has a role' do
      input = <<~'EOS'
      [.role1]
      A normal paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.add_role 'role2'
      assert res
      assert_equal 'role1 role2', para.attributes['role']
      assert para.has_role? 'role1'
      assert para.has_role? 'role2'
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `add_role` API, which this crate does
    // not expose.
    non_normative!(
        r#"
    test 'a role is not added using add_role if the node already has that role' do
      input = <<~'EOS'
      [.role1]
      A normal paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.add_role 'role1'
      refute res
      assert_equal 'role1', para.attributes['role']
      assert para.has_role? 'role1'
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `remove_role` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'an existing role can be removed using remove_role' do
      input = <<~'EOS'
      [.role1.role2]
      A normal paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.remove_role 'role1'
      assert res
      assert_equal 'role2', para.attributes['role']
      assert para.has_role? 'role2'
      refute para.has_role?('role1')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `remove_role` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'roles are removed when last role is removed using remove_role' do
      input = <<~'EOS'
      [.role1]
      A normal paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.remove_role 'role1'
      assert res
      refute para.role?
      assert_nil para.attributes['role']
      refute para.has_role? 'role1'
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `remove_role` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'roles are not changed when a non-existent role is removed using remove_role' do
      input = <<~'EOS'
      [.role1]
      A normal paragraph
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.remove_role 'role2'
      refute res
      assert_equal 'role1', para.attributes['role']
      assert para.has_role? 'role1'
      refute para.has_role?('role2')
    end

"#
    );

    // Out of scope: exercises Ruby's mutable `remove_role` API, which this crate
    // does not expose.
    non_normative!(
        r#"
    test 'roles are not changed when using remove_role if the node has no roles' do
      input = 'A normal paragraph'
      doc = document_from_string(input)
      para = doc.blocks.first
      res = para.remove_role 'role1'
      refute res
      assert_nil para.attributes['role']
      refute para.has_role?('role1')
    end

"#
    );

    #[test]
    fn option_can_be_specified_in_first_position_of_block_style_using_shorthand_syntax() {
        verifies!(
            r#"
    test 'option can be specified in first position of block style using shorthand syntax' do
      input = <<~'EOS'
      [%interactive]
      - [x] checked
      EOS

      doc = document_from_string input
      list = doc.blocks.first
      assert list.attributes.key? 'interactive-option'
      refute list.attributes.key? 'options'
    end

"#
        );

        let doc = Parser::default().parse("[%interactive]\n- [x] checked");
        let list = first_block(&doc);
        assert!(list.has_option("interactive"));
        assert_eq!(named(list, "options"), None);
    }

    #[test]
    fn id_and_role_attributes_can_be_specified_on_section_style_using_shorthand_syntax() {
        verifies!(
            r#"
    test 'id and role attributes can be specified on section style using shorthand syntax' do
      input = <<~'EOS'
      [dedication#dedication.small]
      == Section
      Content.
      EOS
      output = convert_string_to_embedded input
      assert_xpath '/div[@class="sect1 small"]', output, 1
      assert_xpath '/div[@class="sect1 small"]/h2[@id="dedication"]', output, 1
    end

"#
        );

        let doc = Parser::default().parse("[dedication#dedication.small]\n== Section\nContent.");
        assert_css(&doc, "div.sect1.small", 1);
        assert_css(&doc, "div.sect1.small h2#dedication", 1);
    }

    // Out of scope: targets the DocBook backend, which is out of scope for this
    // crate.
    non_normative!(
        r#"
    test 'id attribute specified using shorthand syntax should not create a special section' do
      input = <<~'EOS'
      [#idname]
      == Section

      content
      EOS

      doc = document_from_string input, backend: 'docbook'
      section = doc.blocks[0]
      refute_nil section
      assert_equal :section, section.context
      refute section.special
      output = doc.convert
      assert_css 'article:root > section', output, 1
      assert_css 'article:root > section[xml|id="idname"]', output, 1
    end

"#
    );

    #[test]
    fn block_attributes_are_additive() {
        verifies!(
            r#"
    test "Block attributes are additive" do
      input = <<~'EOS'
      [id='foo']
      [role='lead']
      A paragraph.
      EOS
      doc = document_from_string(input)
      para = doc.blocks.first
      assert_equal 'foo', para.id
      assert_equal 'lead', para.attributes['role']
    end

"#
        );

        let doc = Parser::default().parse("[id='foo']\n[role='lead']\nA paragraph.");
        let p = first_block(&doc);
        assert_eq!(p.id(), Some("foo"));
        assert_eq!(p.roles(), vec!["lead"]);
    }

    // Tracked in #733.
    #[test]
    fn last_wins_for_id_attribute() {
        non_normative!(
            r#"
    test "Last wins for id attribute" do
      input = <<~'EOS'
      [[bar]]
      [[foo]]
      == Section

      paragraph

      [[baz]]
      [id='coolio']
      === Section
      EOS
      doc = document_from_string(input)
      sec = doc.first_section
      assert_equal 'foo', sec.id
      subsec = sec.blocks.last
      assert_equal 'coolio', subsec.id
    end

"#
        );

        // Divergence: Asciidoctor keeps the *last* of consecutive block anchors,
        // so the section id resolves to `foo`. This crate keeps the *first*
        // anchor, so the id is `bar`, and it does not collapse the stacked
        // anchors/attributes onto the following section the same way.
        let doc = Parser::default().parse(
            "[[bar]]\n[[foo]]\n== Section\n\nparagraph\n\n[[baz]]\n[id='coolio']\n=== Section",
        );
        assert_eq!(first_block(&doc).id(), Some("bar"));
    }

    // Tracked in #733.
    #[test]
    fn trailing_block_attributes_transfer_to_the_following_section() {
        non_normative!(
            r#"
    test "trailing block attributes transfer to the following section" do
      input = <<~'EOS'
      [[one]]

      == Section One

      paragraph

      [[sub]]
      // try to mess this up!

      === Sub-section

      paragraph

      [role='classy']

      ////
      block comment
      ////

      == Section Two

      content
      EOS
      doc = document_from_string(input)
      section_one = doc.blocks.first
      assert_equal 'one', section_one.id
      subsection = section_one.blocks.last
      assert_equal 'sub', subsection.id
      section_two = doc.blocks.last
      assert_equal 'classy', section_two.attr(:role)
    end
"#
        );

        // Divergence: Asciidoctor transfers a trailing block anchor / attribute
        // list (separated from the heading by a comment or block) to the
        // *following* section, so the sub-section gets id `sub` and Section Two
        // gets role `classy`. This crate attaches them to standalone blocks
        // instead, so the sub-section keeps its auto-generated id and Section Two
        // has no role. The top-level section ids that this crate does assign are
        // verified here.
        let doc = Parser::default().parse(
            "[[one]]\n\n== Section One\n\nparagraph\n\n[[sub]]\n// try to mess this up!\n\n=== Sub-section\n\nparagraph\n\n[role='classy']\n\n////\nblock comment\n////\n\n== Section Two\n\ncontent",
        );
        let blocks: Vec<_> = doc.nested_blocks().collect();
        assert_eq!(blocks[0].id(), Some("one"));
        assert_eq!(blocks.last().unwrap().id(), Some("_section_two"));
        assert!(blocks.last().unwrap().roles().is_empty());
    }
}

non_normative!(
    r#"
  end

end
"#
);