forma-ir 3.0.0

Forma Module IR: binary format, parser, and server-side HTML walker
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
//! Static HTML walker — walks the IR opcode stream to produce HTML output.
//!
//! This is the core SSR rendering engine. It takes a parsed `IrModule` plus
//! populated `SlotData`, walks the opcode stream linearly, and produces an
//! HTML string suitable for embedding in the page.

pub use crate::contracts::state::IslandState;
use crate::contracts::state::StateBuilder;
#[cfg(feature = "tracing")]
use crate::format::SlotSource;
use crate::format::{IrError, IslandTrigger, ListKeyReason, ListKeyViolation, Opcode};
use crate::parser::IrModule;
use crate::slot::{SlotData, SlotValue};
use crate::text::{normalize_text, normalized_chars};
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;

// ---------------------------------------------------------------------------
// Walk state
// ---------------------------------------------------------------------------

/// Pending island info to be injected on the next OPEN_TAG or VOID_TAG.
struct IslandPending {
    id: u16,
    component_name: String,
    occurrence: usize,
    hash: String,
    trigger: IslandTrigger,
}

/// Default cap on the HTML a single walk may produce.
///
/// A structurally valid module can otherwise force an arbitrarily large
/// `String` allocation inside one request handler, and Rust answers allocation
/// failure with `handle_alloc_error` → process abort, which kills every
/// in-flight request. Raise it via [`WalkLimits`] for an app with a
/// legitimately large data-driven page rather than accepting a silent
/// degradation.
pub const DEFAULT_MAX_OUTPUT_BYTES: usize = 8 * 1024 * 1024;

/// Default cap on total LIST item iterations per render, across all depths.
///
/// `MAX_LIST_DEPTH` bounds nesting but not volume: four LISTs bound to the
/// same array give n^4 body walks from one small module plus one large
/// request array.
pub const DEFAULT_MAX_LIST_ITERATIONS: usize = 50_000;

/// Resource budgets for a single walk.
///
/// Both are per-render totals, not per-nesting-level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WalkLimits {
    /// Maximum bytes of HTML the walk may emit.
    pub max_output_bytes: usize,
    /// Maximum total LIST item iterations across all nesting depths.
    pub max_list_iterations: usize,
}

impl Default for WalkLimits {
    fn default() -> Self {
        WalkLimits {
            max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
            max_list_iterations: DEFAULT_MAX_LIST_ITERATIONS,
        }
    }
}

/// One row of a keyed LIST while its body is walked (FORMA-181, DR-8).
///
/// The row-owned guard for `data-forma-key`: the key is attached to the
/// first element opened at the depth the row was entered at, which is the
/// row's own root, never a descendant and never an element a later row or
/// the code after the list opens. At its own level the row admits exactly
/// that one root, optionally wholly enclosed by one registered child's
/// `<!--f:iN-->` envelope (the child's host is the root); text, a
/// conditional or list range, a second element, an envelope that does not
/// enclose the root, a close of an element the row did not open, or an
/// element left open refuse the render. The compiler proves the shape from
/// the source (BUILD-613), so this is the defence for a hand-built module,
/// under the same code the key itself refuses with (RENDER-503).
struct KeyedRow {
    /// Byte offset of the LIST, as section `0x0006` names it.
    list_offset: u32,
    /// Zero-based position of the row in the array this execution iterates.
    ordinal: usize,
    /// The key path, dotted, for the row's diagnostics.
    path: Rc<str>,
    /// The canonical key, until the row's root element takes it.
    key: Option<Rc<str>>,
    /// [`WalkState::element_depth`] when the row's body started.
    entry_depth: usize,
    /// Elements opened at `entry_depth` so far.
    roots: usize,
    /// Whether anything but the root and its envelope was written at
    /// `entry_depth`.
    stray_content: bool,
    /// The registered child whose island envelope opened at `entry_depth`
    /// before the root and has not closed yet.
    envelope: Option<u16>,
}

impl KeyedRow {
    fn refuse(&self, reason: ListKeyReason) -> IrError {
        list_key_error(self.list_offset, self.ordinal, &self.path, reason)
    }
}

/// Mutable state threaded through recursive `walk_range` calls.
///
/// This avoids adding extra parameters every time we need new walker state
/// (e.g., list depth, output budget).
struct WalkState {
    /// Current nesting depth for LIST opcodes.
    list_depth: u8,
    /// Pending island info to inject on the next OPEN_TAG/VOID_TAG.
    pending_island: Option<IslandPending>,
    /// Elements opened (`OPEN_TAG`) and not yet closed (`CLOSE_TAG`) at this
    /// point of the walk. A keyed row's root is the element opened at the
    /// depth the row was entered at ([`KeyedRow::entry_depth`]).
    element_depth: usize,
    /// The keyed rows the walk is inside, outermost first (FORMA-181). Only
    /// the innermost can still be waiting for its root: an inner row is
    /// entered at a deeper element depth than the root its outer row already
    /// opened. Pushed and popped by the LIST item loop, never cleared at a
    /// range end.
    keyed_rows: Vec<KeyedRow>,
    /// Whether the start tag still open is a keyed row's root, so a DYN_ATTR
    /// named `data-forma-key` on it is refused as an authored identity.
    open_tag_is_row_root: bool,
    /// Checked, owned snapshots in execution order, separate from body HTML.
    island_state: StateBuilder,
    /// True when an OpenTag or VoidTag has been opened but the `>` has not
    /// been written yet. DYN_ATTR opcodes may still append attributes.
    pending_tag_close: bool,
    /// Tag name of the element currently being opened — needed to decide
    /// whether a `data:image/svg+xml` URL is in an image context.
    pending_tag_name: String,
    /// The output length right after an HTML `pre` or `listing` start tag
    /// closed, while nothing has been written since. The parser drops one
    /// line feed that is the first content token of those elements, so the
    /// first text written at exactly this length gets an extra LF when its
    /// normalized text starts with one. Any serialized token (a marker
    /// comment, a child element, a closing tag) moves the length past it and
    /// so consumes the boundary; an empty text or a value-only opcode writes
    /// nothing and leaves it. Selected by tag spelling, as `TextOnlyTag` is:
    /// the walker has no namespace stack, and neither SVG nor MathML has an
    /// element of either name.
    ///
    /// Verified by: crates/forma-ir/src/walker.rs > "pre_children_protect_only_an_initial_normalized_line_feed"
    pre_boundary: Option<usize>,
    /// Output-size budget. Lives on the state (not as a `walk_range`
    /// parameter) so every recursive call sharing the same `out` enforces the
    /// same cumulative budget by construction.
    max_output_bytes: usize,
    /// Running total of LIST item iterations, counted across all depths and
    /// never decremented.
    list_iterations: usize,
    /// Cap for `list_iterations`.
    max_list_iterations: usize,
    /// Undo log for slot writes made inside a LIST body.
    ///
    /// A list row must render from its own scope: the item binding and any
    /// PROP the body extracts from it are visible to that row and to nothing
    /// else. The loop used to buy that by deep-cloning the whole `SlotData`
    /// per row, which costs O(rows x slots) — the walk was measurably
    /// quadratic in row count. Instead each write swaps the new value in and
    /// pushes the displaced one here; the row's `Drop`-equivalent pops back to
    /// the mark it took on entry, restoring exactly the state the caller had.
    ///
    /// Entries are strictly LIFO across nesting: an inner list unwinds its own
    /// entries before its enclosing row unwinds. The `Vec` keeps its capacity
    /// between rows, so steady-state pushes do not allocate.
    slot_journal: Vec<(u16, SlotValue)>,
    /// The text-only element (`textarea`, `title`, `style`, `script`) the
    /// walk is inside, if any. Inside one, no hydration marker is written; see
    /// [`TextOnlyTag`].
    text_only: Option<TextOnlyParent>,
    /// Form-control state gathered from the attributes of the start tag that
    /// is still open, resolved when its `>` is written ([`close_start_tag`]).
    pending_control: PendingControl,
    /// The bound `select` the walk is inside, if any.
    select: Option<SelectBinding>,
    /// An `option` of a bound select with no `value` attribute: its value is
    /// its text, which is known only when the option ends.
    option_by_text: Option<OptionByText>,
    /// Elements and text nodes written so far ([`WalkCounts::nodes`]).
    nodes: usize,
    /// The islands open at this point of the walk, outermost first: the
    /// component path a failure reports ([`WalkFailure::component_path`]).
    islands: Vec<u16>,
    /// Whether `fault_injection::FAULT_TRIGGER` may fire: set only by
    /// [`walk_to_html_counted`], which catches the panic, so the uncaught
    /// `walk_to_html` path can never take it.
    #[cfg(any(test, feature = "__fault-injection"))]
    fault_armed: bool,
}

impl WalkState {
    fn new(limits: WalkLimits) -> Self {
        WalkState {
            list_depth: 0,
            pending_island: None,
            element_depth: 0,
            keyed_rows: Vec::new(),
            open_tag_is_row_root: false,
            island_state: StateBuilder::new(),
            pending_tag_close: false,
            pending_tag_name: String::new(),
            pre_boundary: None,
            max_output_bytes: limits.max_output_bytes,
            list_iterations: 0,
            max_list_iterations: limits.max_list_iterations,
            slot_journal: Vec::new(),
            text_only: None,
            pending_control: PendingControl::None,
            select: None,
            option_by_text: None,
            nodes: 0,
            islands: Vec::new(),
            #[cfg(any(test, feature = "__fault-injection"))]
            fault_armed: false,
        }
    }

    /// Record a slot write so the enclosing LIST row can undo it.
    ///
    /// Outside a list there is nothing to undo — a PROP at depth 0 writes to
    /// the walk's own `SlotData` copy for the rest of the page, which is what
    /// the clone-per-row implementation did too.
    fn journal_write(&mut self, slot_id: u16, previous: SlotValue) {
        if self.list_depth > 0 {
            self.slot_journal.push((slot_id, previous));
        }
    }

    /// Undo every journalled write back to `mark`, restoring `slots`.
    fn unwind_slot_journal(&mut self, slots: &mut SlotData, mark: usize) {
        while self.slot_journal.len() > mark {
            // `len() > mark >= 0`, so the pop cannot be None.
            if let Some((slot_id, previous)) = self.slot_journal.pop() {
                slots.set(slot_id, previous);
            }
        }
    }

    /// The innermost keyed row, when the walk is at the depth it was
    /// entered at: the level where its root element and nothing else belongs.
    fn keyed_row_at_entry_depth(&mut self) -> Option<&mut KeyedRow> {
        let depth = self.element_depth;
        self.keyed_rows
            .last_mut()
            .filter(|row| row.entry_depth == depth)
    }

    /// An element is being opened: if it is the innermost keyed row's root,
    /// count it and hand back the key it carries.
    fn open_row_root(&mut self) -> Option<Rc<str>> {
        let row = self.keyed_row_at_entry_depth()?;
        row.roots += 1;
        row.key.take()
    }

    /// Text or a marker is being written: at a keyed row's own level that is
    /// content beside its root, which the row's shape does not admit.
    fn note_row_level_content(&mut self) {
        if let Some(row) = self.keyed_row_at_entry_depth() {
            row.stray_content = true;
        }
    }

    /// An island starts: at a keyed row's own level it is admitted only as
    /// the envelope of a registered child whose host is the row's root, so
    /// only before that root and only once.
    fn note_row_level_island_start(&mut self, island_id: u16) {
        if let Some(row) = self.keyed_row_at_entry_depth() {
            if row.roots == 0 && row.envelope.is_none() && !row.stray_content {
                row.envelope = Some(island_id);
            } else {
                row.stray_content = true;
            }
        }
    }

    /// An island ends: at a keyed row's own level it must close the envelope
    /// that opened before the root, right after that one root.
    fn note_row_level_island_end(&mut self, island_id: u16) {
        if let Some(row) = self.keyed_row_at_entry_depth() {
            if row.envelope == Some(island_id) && row.roots == 1 {
                row.envelope = None;
            } else {
                row.stray_content = true;
            }
        }
    }

    /// A CLOSE_TAG at a keyed row's own level closes an element the row did
    /// not open (the list's parent): the row is not balanced.
    fn closes_outside_keyed_row(&self) -> bool {
        self.keyed_rows
            .last()
            .is_some_and(|row| row.entry_depth == self.element_depth)
    }
}

// ---------------------------------------------------------------------------
// Dev-mode slot source check
// ---------------------------------------------------------------------------

/// Log a warning when a server-sourced slot has no value at render time.
///
/// This is a dev-mode diagnostic: if a slot is declared as `SlotSource::Server`
/// but the handler never populated it (still `SlotValue::Null`), the page
/// handler likely has a bug. The warning is informational and does not affect
/// output.
#[cfg_attr(not(feature = "tracing"), allow(unused_variables))]
fn check_slot_source(module: &IrModule, slot_id: u16, value: &SlotValue) {
    // Gated as a whole: with `tracing` off there is nothing to report, so the
    // lookup should not run at all. (It is an O(1) index lookup now either
    // way, but the source should say what it means.)
    #[cfg(feature = "tracing")]
    if let Some(entry) = module.slot_entry(slot_id) {
        if entry.source == SlotSource::Server && matches!(value, SlotValue::Null) {
            tracing::warn!(
                slot_id = slot_id,
                "Server-sourced slot has no value at render time — handler may have a bug"
            );
        }
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Walk the IR opcode stream and produce an HTML string.
///
/// This is the main entry point for SSR rendering. It processes every opcode
/// in the module's bytecode section, looking up strings in the string table
/// and slot values in `slots`.
pub fn walk_to_html(module: &IrModule, slots: &SlotData) -> Result<String, IrError> {
    walk_to_html_with_limits(module, slots, WalkLimits::default())
}

/// Walk the IR opcode stream with explicit resource budgets.
///
/// Use this when the default 8 MiB output cap or 50,000-iteration LIST budget
/// is wrong for the page — raising the limit is the right answer for a
/// legitimately large list, rather than letting the render fail and silently
/// degrade to a client-mounted page.
pub fn walk_to_html_with_limits(
    module: &IrModule,
    slots: &SlotData,
    limits: WalkLimits,
) -> Result<String, IrError> {
    let ops = &module.opcodes;
    let len = ops.len();
    let mut out = String::with_capacity(initial_capacity(len));
    let mut state = WalkState::new(limits);
    let mut slots_mut = slots.clone();
    slots_mut.apply_module_constants(module);
    walk_range(module, &mut slots_mut, ops, 0, len, &mut out, &mut state, 0)?;
    Ok(out)
}

/// The output buffer's first reservation for a module's opcode stream.
///
/// Capped: `len * 4` is a fine estimate for a real page (measured modules are
/// tens of bytes to low KiB) but a 4x reservation on an attacker-sized section
/// is a free amplifier, and `len * 4` can itself overflow on wasm32 where
/// `usize` is 32 bits. Beyond the cap, String's amortized doubling costs
/// O(log n) reallocs.
fn initial_capacity(opcodes_len: usize) -> usize {
    opcodes_len.saturating_mul(4).min(1 << 20)
}

/// What a walk wrote, or what it had reached when it stopped.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct WalkCounts {
    /// Elements and text nodes written from the module's opcodes. Hydration
    /// markers and comments are not nodes. State is counted by the server.
    pub nodes: usize,
    /// Bytes of HTML written: what [`WalkLimits::max_output_bytes`] bounds.
    pub bytes: usize,
    /// LIST rows walked, across every nesting depth: what
    /// [`WalkLimits::max_list_iterations`] bounds.
    pub list_iterations: usize,
}

/// A whole page's HTML and what writing it took.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WalkOutput {
    /// The page's HTML, as [`walk_to_html_with_limits`] writes it.
    pub html: String,
    /// Declared inputs captured at each executed ISLAND_START. The server
    /// places this envelope once in the head before sending body HTML.
    pub island_state: IslandState,
    /// What the walk wrote.
    pub counts: WalkCounts,
}

/// A walk that stopped: why, where, and how far it had got.
///
/// The partial HTML is dropped: nothing a failed walk wrote leaves it.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WalkFailure {
    /// Why it stopped.
    pub error: IrError,
    /// The component names of the islands open where it stopped, outermost
    /// first; empty when it stopped outside every island. The decoded v3
    /// source-position metadata is available for later diagnostic integration;
    /// this path continues to name declared islands.
    pub component_path: Vec<String>,
    /// What it had written by then.
    pub counts: WalkCounts,
}

impl fmt::Display for WalkFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.error)?;
        if !self.component_path.is_empty() {
            write!(f, " (in {})", self.component_path.join(" > "))?;
        }
        Ok(())
    }
}

impl std::error::Error for WalkFailure {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.error)
    }
}

/// [`walk_to_html_with_limits`], reporting what the walk wrote, and on
/// failure where it stopped and how far it had got.
///
/// The same HTML, byte for byte, and the same errors, with two additions: the
/// counts (nodes, bytes, list rows), returned on success and on failure alike
/// so an over-budget page reports how far it got; and the component
/// path, the islands open at the failure. A panic inside the walk is caught
/// and returned as [`IrError::Panicked`] with the same path and counts, so
/// one bad page cannot take its request's connection down with it.
/// That holds only while the application unwinds on panic: under
/// `panic = "abort"` the process ends instead.
///
/// Verified by: crates/forma-ir/src/walker.rs > "counted_walk_returns_the_counts_reached_when_a_budget_stops_it"
/// Verified by: crates/forma-ir/src/walker.rs > "counted_walk_catches_a_panic_and_names_its_island"
pub fn walk_to_html_counted(
    module: &IrModule,
    slots: &SlotData,
    limits: WalkLimits,
) -> Result<WalkOutput, WalkFailure> {
    let ops = &module.opcodes;
    let len = ops.len();
    let mut out = String::with_capacity(initial_capacity(len));
    let mut state = WalkState::new(limits);
    #[cfg(any(test, feature = "__fault-injection"))]
    {
        state.fault_armed = true;
    }
    let mut slots_mut = slots.clone();
    slots_mut.apply_module_constants(module);

    // The closure only borrows: after an unwind, `state` and `out` still say
    // where the walk was and how much it had written. Nothing they hold is
    // used again except to read those counts.
    let walked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        walk_range(module, &mut slots_mut, ops, 0, len, &mut out, &mut state, 0)
    }));
    let counts = WalkCounts {
        nodes: state.nodes,
        bytes: out.len(),
        list_iterations: state.list_iterations,
    };
    let error = match walked {
        Ok(Ok(())) => match state.island_state.finish() {
            Ok(island_state) => {
                return Ok(WalkOutput {
                    html: out,
                    island_state,
                    counts,
                })
            }
            Err(error) => {
                return Err(WalkFailure {
                    error,
                    component_path: Vec::new(),
                    counts,
                })
            }
        },
        Ok(Err(error)) => error,
        Err(payload) => IrError::Panicked {
            message: panic_message(payload.as_ref()),
        },
    };
    let component_path = state
        .islands
        .iter()
        .map(|&id| {
            module
                .island_entry(id)
                .and_then(|entry| module.strings.get(entry.name_str_idx).ok())
                .map_or_else(|| format!("island {id}"), str::to_string)
        })
        .collect();
    Err(WalkFailure {
        error,
        component_path,
        counts,
    })
}

/// A panic payload's message: `panic!` with a literal gives a `&str`, with
/// formatting a `String`.
fn panic_message(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(message) = payload.downcast_ref::<&str>() {
        (*message).to_string()
    } else if let Some(message) = payload.downcast_ref::<String>() {
        message.clone()
    } else {
        "a panic with no message".to_string()
    }
}

/// Walk a single island's content and produce an HTML string.
///
/// This internal fragment path has no supported caller (DR-10). It finds
/// the island by `island_id` in the module's island table, seeks to its
/// `byte_offset` in the opcode stream, and walks from after the ISLAND_START
/// opcode until the matching ISLAND_END opcode.
///
/// Returns the island ROOT element carrying the full `data-forma-*` attribute
/// set (`data-forma-island`, `-component`, `-status`, `-hydrate`, and
/// the occurrence/hash markers). Internal diagnostics may inspect this root with
/// `replaceWith`/`outerHTML` and MUST NOT use `innerHTML` — that would discard
/// exactly the attributes the runtime selects on.
///
/// The `<!--f:iN-->` / `<!--/f:iN-->` marker comments are deliberately NOT
/// emitted: they live outside the root element and survive an `outerHTML`
/// swap, so re-emitting them would duplicate them.
///
/// This internal diagnostic fragment returns body HTML only; it is not a
/// supported page-response path. The complete counted walk returns the state
/// envelope required to activate its occurrence markers.
#[allow(dead_code)] // Internal fragment path; no supported caller since the WASM walker retired.
pub(crate) fn walk_island(
    module: &IrModule,
    slots: &SlotData,
    island_id: u16,
) -> Result<String, IrError> {
    walk_island_with_limits(module, slots, island_id, WalkLimits::default())
}

/// [`walk_island`] with explicit resource budgets.
#[allow(dead_code)]
pub(crate) fn walk_island_with_limits(
    module: &IrModule,
    slots: &SlotData,
    island_id: u16,
    limits: WalkLimits,
) -> Result<String, IrError> {
    // 1. Find the island entry by id
    let entry = module
        .island_entry(island_id)
        .ok_or(IrError::IslandNotFound(island_id))?;

    let byte_offset = entry.byte_offset as usize;
    let ops = &module.opcodes;

    // 2. Validate byte_offset is in bounds. Saturating arithmetic: byte_offset
    //    is u32-wide and `usize` is 32 bits on wasm32.
    if byte_offset >= ops.len() {
        return Err(IrError::BufferTooShort {
            expected: byte_offset.saturating_add(1),
            actual: ops.len(),
        });
    }

    // 3. Verify we're at an ISLAND_START opcode
    let opcode = Opcode::from_byte(ops[byte_offset])?;
    if opcode != Opcode::IslandStart {
        return Err(IrError::InvalidOpcode(ops[byte_offset]));
    }

    // 4. Skip ISLAND_START opcode(1) + island_id(2) = 3 bytes
    let content_start = byte_offset.saturating_add(3);
    if content_start > ops.len() {
        return Err(IrError::BufferTooShort {
            expected: content_start,
            actual: ops.len(),
        });
    }

    // 5. Replicate the ISLAND_START handler: seeking past the opcode means it
    //    never runs, so without this the island root comes back with none of
    //    its data-forma-* attributes and would never hydrate.
    let mut out = String::with_capacity(256);
    let mut state = WalkState::new(limits);
    let mut slots_mut = slots.clone();
    slots_mut.apply_module_constants(module);

    let component_name = module.strings.get(entry.name_str_idx)?.to_string();
    let (occurrence, hash) = state.island_state.snapshot(module, entry, &slots_mut)?;
    state.pending_island = Some(IslandPending {
        id: island_id,
        component_name,
        occurrence,
        hash,
        trigger: entry.trigger,
    });

    // 6. Walk until the matching ISLAND_END, which stops the range.
    walk_range_until_island_end(
        module,
        &mut slots_mut,
        ops,
        content_start,
        island_id,
        &mut out,
        &mut state,
        0,
    )?;

    Ok(out)
}

// ---------------------------------------------------------------------------
// Internal: recursive sub-range walker
// ---------------------------------------------------------------------------

/// Maximum nesting depth for LIST opcodes. Prevents stack overflow from
/// deeply nested or malicious input.
const MAX_LIST_DEPTH: u8 = crate::format::MAX_LIST_DEPTH as u8;

/// Maximum recursion depth for control-flow opcodes (SHOW_IF, SWITCH, LIST,
/// TRY/FALLBACK). Prevents stack overflow from deeply nested or malicious IR.
const MAX_RECURSION_DEPTH: usize = crate::format::MAX_CONTROL_DEPTH;

/// Controls how the walker decides when to stop processing opcodes.
enum WalkMode {
    /// Walk a fixed byte range `ops[start..end]`.  Used for the top-level
    /// full-page walk **and** for all recursive sub-range calls inside
    /// control-flow opcodes (ShowIf, Switch, List).
    Range { end: usize },
    /// Walk until we encounter an ISLAND_END opcode whose `island_id` matches
    /// `target_island_id`.  Used by `walk_island()` for fragment rendering.
    UntilIslandEnd { target_island_id: u16 },
}

/// Walk a sub-range of the opcode stream `ops[start..end]`, appending HTML to `out`.
///
/// This is structured as a sub-range function so that control-flow opcodes
/// (ShowIf, List, Switch) can recurse into their body ranges.
///
/// `state` carries mutable walker context (list depth, output budget, etc.).
#[allow(clippy::too_many_arguments)]
fn walk_range(
    module: &IrModule,
    slots: &mut SlotData,
    ops: &[u8],
    start: usize,
    end: usize,
    out: &mut String,
    state: &mut WalkState,
    depth: usize,
) -> Result<(), IrError> {
    walk_range_impl(
        module,
        slots,
        ops,
        start,
        WalkMode::Range { end },
        out,
        state,
        depth,
    )
}

/// Walk the opcode stream starting at `start` until we encounter an ISLAND_END
/// with the matching `target_island_id`. Appends HTML output to `out`.
///
/// This is used by `walk_island()` to render just one island's content.
/// It shares the same opcode interpretation as `walk_range` but has a different
/// termination condition: it stops at ISLAND_END instead of a fixed end position.
#[allow(clippy::too_many_arguments)]
fn walk_range_until_island_end(
    module: &IrModule,
    slots: &mut SlotData,
    ops: &[u8],
    start: usize,
    target_island_id: u16,
    out: &mut String,
    state: &mut WalkState,
    depth: usize,
) -> Result<(), IrError> {
    walk_range_impl(
        module,
        slots,
        ops,
        start,
        WalkMode::UntilIslandEnd { target_island_id },
        out,
        state,
        depth,
    )
}

/// Shared implementation for both `walk_range` and `walk_range_until_island_end`.
///
/// The `mode` parameter controls the termination condition:
/// - `WalkMode::Range { end }` — stop when `pos >= end` (with bounds check)
/// - `WalkMode::UntilIslandEnd { target_island_id }` — stop when we hit an
///   ISLAND_END opcode matching the target id
///
/// All opcode handling is identical regardless of mode. The only differences
/// are:
/// 1. The loop termination condition
/// 2. How ISLAND_END is handled (in Range mode it emits the comment marker
///    and continues; in UntilIslandEnd mode a match causes an early return)
/// 3. What happens when the loop ends without an explicit stop (Range returns
///    Ok, UntilIslandEnd returns an error)
#[allow(clippy::too_many_arguments)]
fn walk_range_impl(
    module: &IrModule,
    slots: &mut SlotData,
    ops: &[u8],
    start: usize,
    mode: WalkMode,
    out: &mut String,
    state: &mut WalkState,
    depth: usize,
) -> Result<(), IrError> {
    if depth > MAX_RECURSION_DEPTH {
        return Err(IrError::RecursionLimitExceeded);
    }

    let strings = &module.strings;
    let mut pos = start;
    let limit = state.max_output_bytes;
    // Range-local, not shared on WalkState: a TRY_START and its FALLBACK are
    // emitted in the same opcode range by construction, so a FALLBACK reached
    // inside a nested range must never pop an entry pushed by an outer
    // TRY_START and skip that many bytes of the wrong region. A stray FALLBACK
    // then finds an empty stack and is a no-op, mirroring how a stray
    // SHOW_ELSE is already tolerated. This also removes the unbounded growth
    // when a TRY_START is walked but its FALLBACK never is.

    // The loop bound depends on the mode:
    // - Range: stop at `end`, but also guard against `pos >= ops.len()`
    // - UntilIslandEnd: stop at `ops.len()` (or earlier via ISLAND_END match)
    let loop_bound = match mode {
        WalkMode::Range { end } => end,
        WalkMode::UntilIslandEnd { .. } => usize::MAX, // guarded by ops.len() check below
    };

    // Upper bound every derived offset (branch/body ends) must respect. In
    // Range mode that is the enclosing region; on the island path there is no
    // `end`, so the opcode stream itself is the bound.
    let region_limit = match mode {
        WalkMode::Range { end } => end.min(ops.len()),
        WalkMode::UntilIslandEnd { .. } => ops.len(),
    };

    while pos < loop_bound {
        // Cheap redundancy: every append also goes through a checked push, so
        // a single opcode cannot blow past the budget between iterations.
        if out.len() > limit {
            return Err(IrError::OutputLimitExceeded { limit });
        }

        // In Range mode, `end` could exceed ops.len()` for a malformed stream.
        // In UntilIslandEnd mode, loop_bound is MAX so we always need this check.
        if pos >= ops.len() {
            match mode {
                WalkMode::Range { .. } => {
                    return Err(IrError::BufferTooShort {
                        expected: pos + 1,
                        actual: ops.len(),
                    });
                }
                WalkMode::UntilIslandEnd { .. } => break, // fall through to IslandNotFound error
            }
        }

        let opcode = Opcode::from_byte(ops[pos])?;
        pos += 1; // advance past the opcode byte

        // Flush pending tag close before any opcode that is NOT DYN_ATTR.
        // DYN_ATTR needs the tag still open so it can append attributes.
        if state.pending_tag_close && opcode != Opcode::DynAttr {
            close_start_tag(out, state, limit)?;
        }

        match opcode {
            // ----- Fully implemented opcodes -----
            Opcode::OpenTag | Opcode::VoidTag => {
                let (tag_str_idx, attrs, new_pos) = read_tag_with_attrs(ops, pos, strings)?;
                let tag = strings.get(tag_str_idx)?;
                let has_content = opcode == Opcode::OpenTag;
                emit_open_tag(out, tag, &attrs, has_content, state, limit)?;
                state.nodes += 1;
                pos = new_pos;
            }

            Opcode::CloseTag => {
                let str_idx = read_u32(ops, pos)?;
                pos += 4;
                let tag = strings.get(str_idx)?;
                // Names are written to the output stream unescaped, so an
                // IR-authored name is markup injection unless it is validated.
                if !is_valid_name(tag) {
                    return Err(IrError::InvalidName(tag.to_string()));
                }
                // A keyed row closes only elements it opened (FORMA-181): a
                // CLOSE_TAG at its own level would end the list's parent from
                // inside one row.
                if state.closes_outside_keyed_row() {
                    return Err(innermost_row_error(
                        state,
                        ListKeyReason::UnbalancedRow,
                        IrError::InvalidSection,
                    ));
                }
                leave_element(out, tag, state, limit)?;
                // Saturating: a CLOSE_TAG without its OPEN_TAG outside any
                // keyed row is tolerated as before.
                state.element_depth = state.element_depth.saturating_sub(1);
                push_checked(out, "</", limit)?;
                push_checked(out, tag, limit)?;
                push_checked(out, ">", limit)?;
            }

            Opcode::Text => {
                let str_idx = read_u32(ops, pos)?;
                pos += 4;
                let text = strings.get(str_idx)?;
                state.note_row_level_content();
                if let Some(parent) = state.text_only.as_mut() {
                    if !parent.value_bound {
                        write_text_only(out, parent, text, limit)?;
                    }
                } else {
                    if state.pre_boundary == Some(out.len())
                        && normalized_chars(text).next() == Some('\n')
                    {
                        // First content token of a `pre`/`listing` starting
                        // with a line feed: the parser drops one, so write one
                        // more (the textarea rule, by output position).
                        push_checked(out, "\n", limit)?;
                    }
                    push_escaped_html(out, text, limit)?;
                    if let Some(option) = state.option_by_text.as_mut() {
                        // The text as the parser reads it: normalized.
                        option.text.extend(normalized_chars(text));
                    }
                }
                state.nodes += 1;
            }

            Opcode::DynText => {
                // slot_id(u16) + marker_id(u16)
                let slot_id = read_u16(ops, pos)?;
                pos += 2;
                let marker_id = read_u16(ops, pos)?;
                pos += 2;

                check_slot_source(module, slot_id, slots.get(slot_id));
                state.note_row_level_content();
                let value = slots.get(slot_id).to_text();

                // SR44's stand-in for a component that panics: this crate's
                // tests and the internal `__fault-injection` feature only, and
                // armed only inside `walk_to_html_counted`, which catches it.
                #[cfg(any(test, feature = "__fault-injection"))]
                #[allow(clippy::panic)]
                if state.fault_armed && value == crate::fault_injection::FAULT_TRIGGER {
                    panic!("stand-in component panicked (FAULT_TRIGGER)");
                }

                if let Some(parent) = state.text_only.as_mut() {
                    // Inside a text-only element a marker comment is literal
                    // text, so the value is written on its own (TextOnlyTag).
                    if !parent.value_bound {
                        write_text_only(out, parent, &value, limit)?;
                    }
                } else {
                    push_checked(out, "<!--f:t", limit)?;
                    push_u16(out, marker_id, limit)?;
                    push_checked(out, "-->", limit)?;
                    // Always emit at least a zero-width space so the browser
                    // creates a Text node between the markers. Without this,
                    // <!--f:t0--><!--/f:t0--> produces no Text node in the DOM
                    // and the hydration walker has nothing to bind the
                    // reactive effect to.
                    let written = if value.is_empty() {
                        "\u{200B}"
                    } else {
                        value.as_str()
                    };
                    push_escaped_html(out, written, limit)?;
                    push_checked(out, "<!--/f:t", limit)?;
                    push_u16(out, marker_id, limit)?;
                    push_checked(out, "-->", limit)?;
                    if let Some(option) = state.option_by_text.as_mut() {
                        option.text.extend(normalized_chars(written));
                    }
                }
                state.nodes += 1;
            }

            Opcode::DynAttr => {
                // attr_str_idx(u32) + slot_id(u16) = 6 bytes
                let attr_str_idx = read_u32(ops, pos)?;
                let slot_id = read_u16(ops, pos + 4)?;
                pos += 6;
                check_slot_source(module, slot_id, slots.get(slot_id));

                if state.pending_tag_close {
                    let attr_name = strings.get(attr_str_idx)?;
                    if state.open_tag_is_row_root
                        && attr_name.eq_ignore_ascii_case("data-forma-key")
                    {
                        return Err(innermost_row_error(
                            state,
                            ListKeyReason::AuthoredKey,
                            IrError::InvalidName(attr_name.to_string()),
                        ));
                    }
                    // `value` on a textarea or select, and `selected` on an
                    // option of a bound select, are written another way.
                    if consume_control_attr(state, attr_name, Prop::Slot(slots.get(slot_id))) {
                        continue;
                    }
                    // `text` outlives the borrow only for the Valued case; it
                    // is declared here so the &str can point into it.
                    let text;
                    let value = match slots.get(slot_id) {
                        // An HTML boolean attribute is ON whenever it is
                        // present, so the old `disabled="false"` meant
                        // DISABLED: scalar truthiness selects presence. Any
                        // other attribute takes the word `h()` writes for
                        // true, `data-live="true"` (FORMA-182 finding F2,
                        // docs/parity-rules.md rule 6's attribute table).
                        SlotValue::Bool(false) | SlotValue::Null => None,
                        other if is_html_boolean_attribute(attr_name) => {
                            other.as_bool().then_some(AttrValue::Bare)
                        }
                        SlotValue::Bool(true) => Some(AttrValue::Valued("true")),
                        other => {
                            text = other.to_text();
                            // An empty string is a value, not an absence:
                            // `renderAttr` emits `value=""`, and dropping the
                            // attribute instead is a hydration mismatch on the
                            // common `value=""` / `alt=""` case.
                            Some(AttrValue::Valued(text.as_str()))
                        }
                    };
                    if let Some(value) = value {
                        // `pending_tag_name` is the element this attribute is
                        // being written to — it decides whether a
                        // data:image/svg+xml value is in an image context.
                        let tag = std::mem::take(&mut state.pending_tag_name);
                        emit_attr(out, &tag, attr_name, value, limit)?;
                        state.pending_tag_name = tag;
                    }
                }
                // If not inside a tag opening (shouldn't happen in well-formed IR), skip silently.
            }

            Opcode::IslandStart => {
                // island_id(u16)
                let island_id = read_u16(ops, pos)?;
                pos += 2;

                // Only one element can carry a `data-forma-island` set. If an
                // island's body starts another island before opening a tag of
                // its own, the outer island's attributes are silently
                // discarded and it never hydrates, never disposes, and cannot
                // be found by `activateIslands`. Fail the render instead: the
                // compiler owes every island a shell element.
                if let Some(outer) = &state.pending_island {
                    return Err(IrError::NestedIslandRoot {
                        outer: outer.id,
                        inner: island_id,
                    });
                }
                state.islands.push(island_id);
                // A registered child's envelope may wrap a keyed row's root
                // (its host); anything else at the row's level is refused.
                state.note_row_level_island_start(island_id);

                // No marker inside a text-only element, where it is text.
                if state.text_only.is_none() {
                    push_checked(out, "<!--f:i", limit)?;
                    push_u16(out, island_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }

                // Look up island entry from the island table (O(1) index).
                // `IrModule::validate` rejects an ISLAND_START naming an
                // island the table does not declare, so the `else` below is
                // reachable only for a module that skipped validation.
                // Verified by: crates/forma-ir/src/parser.rs > "validate_rejects_island_start_missing_from_table"
                if let Some(entry) = module.island_entry(island_id) {
                    let component_name = strings.get(entry.name_str_idx)?.to_string();

                    // Capture ordered declared inputs before the island body or
                    // its surrounding list can mutate scratch/row slots.
                    let (occurrence, hash) = state.island_state.snapshot(module, entry, slots)?;

                    state.pending_island = Some(IslandPending {
                        id: island_id,
                        component_name,
                        occurrence,
                        hash,
                        trigger: entry.trigger,
                    });
                } else {
                    #[cfg(feature = "tracing")]
                    tracing::warn!(
                        island_id = island_id,
                        "ISLAND_START names an island that is not in the island table — \
                         markers emitted but no data-forma-* attributes, so it will not hydrate \
                         (module was not validated)"
                    );
                }
            }

            Opcode::IslandEnd => {
                // island_id(u16)
                let island_id = read_u16(ops, pos)?;
                pos += 2;
                if state.islands.last() == Some(&island_id) {
                    state.islands.pop();
                }
                state.note_row_level_island_end(island_id);

                // The island is over: anything still pending belongs to no
                // element inside it. Without this, an ISLAND_START not
                // followed by a tag leaks its identity attributes onto
                // whatever element comes next — possibly one emitted after the
                // island's own closing marker.
                if state.pending_island.take().is_some() {
                    #[cfg(feature = "tracing")]
                    tracing::warn!(
                        island_id = island_id,
                        "ISLAND_START was not followed by an element — island attributes dropped, \
                         this island will not hydrate (compiler should emit a shell element)"
                    );
                }

                // In UntilIslandEnd mode, a matching island_id means we are done.
                if let WalkMode::UntilIslandEnd { target_island_id } = mode {
                    if island_id == target_island_id {
                        // Flush any pending tag close before returning.
                        if state.pending_tag_close {
                            close_start_tag(out, state, limit)?;
                        }
                        return Ok(());
                    }
                }

                // Either Range mode, or a nested (non-matching) island end —
                // emit the closing marker comment and continue (no marker
                // inside a text-only element, where it is text).
                if state.text_only.is_none() {
                    push_checked(out, "<!--/f:i", limit)?;
                    push_u16(out, island_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }
            }

            Opcode::ShowIf => {
                // slot_id(2) + then_len(4) + else_len(4) = 10 bytes header
                let slot_id = read_u16(ops, pos)?;
                let then_len = read_u32(ops, pos + 2)? as usize;
                let else_len = read_u32(ops, pos + 6)? as usize;
                pos += 10;
                // A conditional at a keyed row's own level is a range, not
                // the one stable root the key needs (BUILD-613 from source).
                state.note_row_level_content();

                check_slot_source(module, slot_id, slots.get(slot_id));
                let condition = slots.get(slot_id).as_bool();
                let then_start = pos;
                // Checked, and bounded by the enclosing region: a corrupt
                // length would otherwise silently relocate content across the
                // hydration markers, and plain `+` can wrap on wasm32.
                let then_end = bounded_offset(pos, then_len, region_limit)?;
                // The SHOW_ELSE marker byte must itself be inside the region
                // (`<` not `<=`), or a SHOW_IF at the tail of a sub-range would
                // read a byte its parent never granted.
                if then_end >= region_limit || ops[then_end] != Opcode::ShowElse as u8 {
                    return Err(IrError::InvalidSection);
                }
                let else_start = then_end + 1; // skip SHOW_ELSE byte
                let else_end = bounded_offset(else_start, else_len, region_limit)?;

                // Emit conditional marker — none inside a text-only element,
                // where it is text. Decided once, so the pair always matches.
                let markers = state.text_only.is_none();
                if markers {
                    push_checked(out, "<!--f:s", limit)?;
                    push_u16(out, slot_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }

                if condition {
                    walk_range(
                        module,
                        slots,
                        ops,
                        then_start,
                        then_end,
                        out,
                        state,
                        depth + 1,
                    )?;
                } else {
                    walk_range(
                        module,
                        slots,
                        ops,
                        else_start,
                        else_end,
                        out,
                        state,
                        depth + 1,
                    )?;
                }

                if markers {
                    push_checked(out, "<!--/f:s", limit)?;
                    push_u16(out, slot_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }

                // Advance pos past both branches + SHOW_ELSE marker byte
                pos = else_end;
            }

            Opcode::ShowElse => {
                // SHOW_ELSE should never be reached during normal walking — it is
                // skipped over by the SHOW_IF handler. If we reach it, the opcode
                // stream is malformed, but we tolerate it gracefully by doing nothing.
            }

            // LIMITATION, not an oversight: SWITCH emits no `<!--f:…-->`
            // marker pair, because the hydration wire contract has no switch
            // marker — it defines exactly `f:tN` (text), `f:sN` (conditional),
            // `f:lN` (list) and `f:iN` (island), and those bytes are shared
            // with @getforma/core and ksx Studio. A switched region is
            // therefore server-rendered only: the client cannot locate it to
            // re-render when the slot changes. Adding one is a coordinated
            // change to both renderers and the client walker, not a
            // walker-local fix. SWITCH also has no default case in the opcode
            // layout; a non-matching value renders nothing.
            Opcode::List => {
                // `pos` already passed the opcode byte: the LIST's own offset,
                // the one section 0x0006 names, is one before it.
                let list_offset = (pos - 1) as u32;
                // slot_id(2) + item_slot_id(2) + body_len(4) = 8 bytes header
                let slot_id = read_u16(ops, pos)?;
                let item_slot_id = read_u16(ops, pos + 2)?;
                let body_len = read_u32(ops, pos + 4)? as usize;
                pos += 8;

                check_slot_source(module, slot_id, slots.get(slot_id));
                // A list at a keyed row's own level is a range beside the
                // row's root, not the root (BUILD-613 from source).
                state.note_row_level_content();

                let body_start = pos;
                let body_end = bounded_offset(pos, body_len, region_limit)?;

                // The key path section 0x0006 attaches to this LIST, if any,
                // resolved once per LIST execution, not per row: the entries
                // are sorted by offset (the parser refuses anything else), so
                // this is one binary search, and every segment index was
                // checked against the string table at load. A LIST without an
                // entry is unkeyed and renders exactly as before.
                let key_path = match module
                    .key_paths
                    .binary_search_by_key(&list_offset, |path| path.list_offset)
                {
                    Ok(index) => Some(ListKey::resolve(
                        list_offset,
                        &module.key_paths[index].segments,
                        strings,
                    )?),
                    Err(_) => None,
                };

                // Emit list markers — none inside a text-only element, where
                // they are text. Decided once, so the pair always matches.
                let list_marker_id = slot_id;
                let markers = state.text_only.is_none();
                if markers {
                    push_checked(out, "<!--f:l", limit)?;
                    push_u16(out, list_marker_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }

                // Take a cheap handle on the array to release the borrow on
                // `slots` before the loop mutates it. The payload is
                // Arc-backed, so this is a refcount bump — and holding it
                // means a body that writes to the list's own slot cannot
                // change what we are iterating.
                let items = match slots.get(slot_id) {
                    SlotValue::Array(a) => Some(a.clone()),
                    _ => None,
                };

                if let Some(items) = items.filter(|a| !a.is_empty()) {
                    state.list_depth += 1;
                    let walked = if state.list_depth > MAX_LIST_DEPTH {
                        Err(IrError::ListDepthExceeded {
                            max: MAX_LIST_DEPTH,
                        })
                    } else {
                        walk_list_rows(
                            module,
                            slots,
                            ops,
                            ListRows {
                                items: &items,
                                item_slot_id,
                                body_start,
                                body_end,
                                key: key_path.as_ref(),
                            },
                            out,
                            state,
                            depth,
                        )
                    };
                    // Restored on every path, as the slot journal and the
                    // keyed-row stack are inside `walk_list_rows`: an error
                    // leaves the walk state as the LIST found it.
                    state.list_depth -= 1;
                    walked?;
                }

                if markers {
                    push_checked(out, "<!--/f:l", limit)?;
                    push_u16(out, list_marker_id, limit)?;
                    push_checked(out, "-->", limit)?;
                }

                pos = body_end;
            }

            Opcode::Coalesce => {
                let source = read_u16(ops, pos)?;
                let fallback = read_u16(ops, pos + 2)?;
                let destination = read_u16(ops, pos + 4)?;
                pos += 6;
                let chosen = if matches!(slots.get(source), SlotValue::Null) {
                    slots.get(fallback)
                } else {
                    slots.get(source)
                };
                let previous = slots.replace(destination, chosen.clone());
                state.journal_write(destination, previous);
            }

            Opcode::Prop => {
                // src_slot_id(u16) + prop_str_idx(u32) + target_slot_id(u16) = 8 bytes
                let src_slot_id = read_u16(ops, pos)?;
                let prop_str_idx = read_u32(ops, pos + 2)?;
                let target_slot_id = read_u16(ops, pos + 6)?;
                pos += 8;

                let prop_name = strings.get(prop_str_idx)?;
                let value = slots.get(src_slot_id).get_property(prop_name);
                // Mutate `slots` directly and journal the displaced value, so
                // the enclosing LIST row restores it when the row ends. A PROP
                // outside any list writes for the rest of the page, which is
                // what it did when each row got its own clone.
                let previous = slots.replace(target_slot_id, value);
                state.journal_write(target_slot_id, previous);
            }
        }
    }

    // Post-loop handling depends on the mode:
    match mode {
        WalkMode::Range { .. } => {
            // Flush any pending tag close at the end of the opcode range.
            //
            // NOTE: `pending_island` / `keyed_rows` are deliberately NOT
            // cleared here. This function runs for every nested control-flow
            // body with the same state, so clearing at range end would wipe
            // the island attributes of a legitimate
            // ISLAND_START → SHOW_IF → OPEN_TAG stream (the island root would
            // silently never hydrate). They are cleared at their owning
            // boundaries instead: ISLAND_END and the LIST item loop.
            if state.pending_tag_close {
                close_start_tag(out, state, limit)?;
            }
            Ok(())
        }
        WalkMode::UntilIslandEnd { target_island_id } => {
            // If we reach the end of the opcode stream without finding ISLAND_END,
            // that means the IR is malformed. Return an error.
            Err(IrError::IslandNotFound(target_island_id))
        }
    }
}

// ---------------------------------------------------------------------------
// Keyed list rows (FORMA-181, DR-8)
// ---------------------------------------------------------------------------

/// A keyed LIST's identity source, resolved once per execution of the LIST.
struct ListKey<'m> {
    /// Byte offset of the LIST, as section `0x0006` names it.
    list_offset: u32,
    /// The key path's segments, as field names.
    segments: Vec<&'m str>,
    /// The path dotted, for diagnostics: shared by every row of the
    /// execution, so a row costs a refcount, not a copy.
    path: Rc<str>,
}

impl<'m> ListKey<'m> {
    fn resolve(
        list_offset: u32,
        segments: &[u32],
        strings: &'m crate::parser::StringTable,
    ) -> Result<Self, IrError> {
        let segments = segments
            .iter()
            .map(|segment| strings.get(*segment))
            .collect::<Result<Vec<&str>, IrError>>()?;
        let path = Rc::from(dotted_path(&segments));
        Ok(ListKey {
            list_offset,
            segments,
            path,
        })
    }
}

/// A key path for a diagnostic: segments joined by dots, and a segment that
/// a dot would make ambiguous (empty, or holding `.`, `[` or `]`) quoted in
/// brackets, so `user.id` is two fields and `["user.id"]` is one.
fn dotted_path(segments: &[&str]) -> String {
    let mut path = String::new();
    for segment in segments {
        if segment.is_empty() || segment.contains(['.', '[', ']']) {
            path.push_str(&format!("[{segment:?}]"));
        } else {
            if !path.is_empty() {
                path.push('.');
            }
            path.push_str(segment);
        }
    }
    path
}

/// What one execution of a LIST iterates (see [`walk_list_rows`]).
struct ListRows<'a, 'm> {
    items: &'a [SlotValue],
    item_slot_id: u16,
    body_start: usize,
    body_end: usize,
    key: Option<&'a ListKey<'m>>,
}

/// Walk every row of one LIST execution.
///
/// Before any error propagates, the slot journal is unwound to the row's mark
/// and a keyed row's guard is popped, and the caller restores the list depth.
/// The rest of the walk state (the element depth, an open start tag, the
/// island stack) is not restored: a `WalkState` serves one walk, and an error
/// ends that walk, which returns no partial page.
fn walk_list_rows(
    module: &IrModule,
    slots: &mut SlotData,
    ops: &[u8],
    rows: ListRows<'_, '_>,
    out: &mut String,
    state: &mut WalkState,
    depth: usize,
) -> Result<(), IrError> {
    let limit = state.max_output_bytes;
    // A list longer than what is left of the budget fails here, before its
    // first row, instead of walking up to the budget and failing there
    // (DR-13, SR38): a hostile 60,000-row list costs a comparison, not 50,000
    // rows. It fails exactly when the per-row check below would have, sooner,
    // and before any error the rows would have raised first (an output
    // budget, a malformed row, a keyed row's identity). That check stays: rows
    // of nested lists can spend the budget after this list was let in.
    //
    // Verified by: crates/forma-ir/src/walker.rs > "a_list_longer_than_the_remaining_budget_fails_before_its_first_row"
    // Verified by: crates/forma-ir/src/walker.rs > "a_list_exactly_at_the_remaining_budget_still_renders"
    let remaining = state
        .max_list_iterations
        .saturating_sub(state.list_iterations);
    if rows.items.len() > remaining {
        return Err(IrError::ListBudgetExceeded {
            max: state.max_list_iterations,
        });
    }
    // The keys this execution of the list has rendered, each with the row
    // that rendered it. Scoped to one execution: an inner list under two
    // outer rows, or two lists over the same array, may reuse a key. Bounded
    // by the output budget: a key is admitted only when its normalized text
    // fits in what is left of the budget, and every admitted key is then
    // written, escaped, on its row's root, so the keys held here never
    // outgrow the output.
    // One allocation per key: the set and the row's guard share it.
    let mut seen_keys: HashMap<Rc<str>, usize> = HashMap::new();
    for (ordinal, item) in rows.items.iter().enumerate() {
        // Total across all depths, never decremented: depth alone does not
        // bound volume, and four nested LISTs over the same array give n^4
        // body walks.
        //
        // Checked before counting, so at the error the count is the rows
        // actually walked (`WalkCounts`).
        if state.list_iterations >= state.max_list_iterations {
            return Err(IrError::ListBudgetExceeded {
                max: state.max_list_iterations,
            });
        }
        state.list_iterations += 1;

        // A keyed row's identity is read from the row's own data before its
        // body runs, so a row without one writes nothing, and the whole render
        // is refused: no page carries a list the client could not adopt.
        if let Some(key) = rows.key {
            let refuse = |reason| list_key_error(key.list_offset, ordinal, &key.path, reason);
            let value = crate::keys::key_value_at(item, &key.segments).map_err(refuse)?;
            // The text is built only once it is known to fit: a key longer
            // than what is left of the output budget could never be written,
            // and copying it first would let one row allocate past the budget.
            if crate::keys::normalized_len_exceeds(value, limit.saturating_sub(out.len())) {
                return Err(IrError::OutputLimitExceeded { limit });
            }
            let text: Rc<str> = crate::keys::canonical_key_text(value)
                .map_err(refuse)?
                .into();
            if let Some(&first_row) = seen_keys.get(&text) {
                // The two ordinals, never the key: it is the row's data.
                return Err(refuse(ListKeyReason::Duplicate { first_row }));
            }
            seen_keys.insert(Rc::clone(&text), ordinal);
            state.keyed_rows.push(KeyedRow {
                list_offset: key.list_offset,
                ordinal,
                path: Rc::clone(&key.path),
                key: Some(text),
                entry_depth: state.element_depth,
                roots: 0,
                stray_content: false,
                envelope: None,
            });
        }

        // Scoped binding instead of a per-row `SlotData` clone: swap the item
        // in, walk, swap back. Same isolation between rows, without copying
        // every other slot on the page once per row.
        let mark = state.slot_journal.len();
        let previous = slots.replace(rows.item_slot_id, item.clone());
        state.slot_journal.push((rows.item_slot_id, previous));

        let walked = walk_range(
            module,
            slots,
            ops,
            rows.body_start,
            rows.body_end,
            out,
            state,
            depth + 1,
        );
        // Unwind before propagating: an aborted walk must still hand `slots`
        // back exactly as it was, because an enclosing row is about to unwind
        // on top of it.
        state.unwind_slot_journal(slots, mark);
        // The row's guard ends with the row, whatever the walk returned; a key
        // it still holds met no root element.
        let keyed_row = if rows.key.is_some() {
            state.keyed_rows.pop()
        } else {
            None
        };
        walked?;

        if let Some(row) = keyed_row {
            if state.element_depth != row.entry_depth {
                // An element the row opened is still open: its root (or what
                // follows it) did not close where the row ends.
                return Err(row.refuse(ListKeyReason::UnbalancedRow));
            }
            if row.roots != 1 || row.stray_content || row.envelope.is_some() {
                return Err(row.refuse(ListKeyReason::RowShape {
                    roots: row.roots,
                    stray_content: row.stray_content || row.envelope.is_some(),
                }));
            }
        }
    }
    Ok(())
}

/// The whole-render refusal for one keyed row (RENDER-503).
fn list_key_error(list_offset: u32, row: usize, path: &str, reason: ListKeyReason) -> IrError {
    IrError::InvalidListKey(Box::new(ListKeyViolation::new(
        list_offset,
        row,
        path,
        reason,
    )))
}

/// A refusal inside the innermost keyed row, or `fallback` when the walk is in
/// none (unreachable through the callers, which act only inside a row).
fn innermost_row_error(state: &WalkState, reason: ListKeyReason, fallback: IrError) -> IrError {
    match state.keyed_rows.last() {
        Some(row) => row.refuse(reason),
        None => fallback,
    }
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Offset arithmetic
// ---------------------------------------------------------------------------

/// `base + len`, rejected if it overflows or leaves the enclosing region.
///
/// Plain `+` on `usize` can overflow on a 32-bit target. The parser accepts
/// arbitrary bytes, so the offset must be checked before indexing.
fn bounded_offset(base: usize, len: usize, limit: usize) -> Result<usize, IrError> {
    base.checked_add(len)
        .filter(|e| *e <= limit)
        .ok_or(IrError::InvalidSection)
}

// ---------------------------------------------------------------------------
// Name and URL safety
// ---------------------------------------------------------------------------

/// True if `name` is safe to emit verbatim as a tag or attribute name.
///
/// Names are written to the output stream unescaped (only *values* go through
/// the escapers), so a string-table entry used as a tag name — `div onload=x`
/// — or as an attribute key — `x"><script` — is direct markup injection. The
/// accepted class is `[A-Za-z_:][-A-Za-z0-9_:.]*`, byte-identical to
/// core's `isSafeAttrName`/`isSafeTagName`. This deliberately admits an ASCII
/// subset (`data-*`, `aria-*`, `xlink:href`, `viewBox`, dotted names); the DOM
/// accepting a Unicode name does not expand this contract (docs/INV1.md).
fn is_valid_name(name: &str) -> bool {
    let mut chars = name.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' || c == ':' => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | ':' | '.'))
}

/// Attribute names also satisfy the qualified-name requirement of core's
/// xlink setAttributeNS writer. Tag-name admission remains unchanged.
fn is_valid_attr_name(name: &str) -> bool {
    is_valid_name(name)
        && name
            .strip_prefix("xlink:")
            .map_or(true, |local| is_valid_name(local) && !local.contains(':'))
}

/// True for any `on…` event-handler attribute name (case-insensitive).
fn is_event_handler_attr(name: &str) -> bool {
    name.len() >= 2 && name.as_bytes()[..2].eq_ignore_ascii_case(b"on")
}

/// Attributes whose value the browser resolves as a URL.
///
/// Mirrors formajs's `URL_ATTRS` exactly. `srcset` is deliberately absent (a
/// comma-separated candidate list that a single scheme test would misparse,
/// and always fetched in image mode) and so is `cite`; adding either would
/// make the Rust walker stricter than the JS renderer, which is itself a
/// hydration divergence.
const URL_ATTRS: [&str; 8] = [
    "href",
    "src",
    "action",
    "formaction",
    "xlink:href",
    "poster",
    "background",
    "data",
];

/// Tags whose URL-bearing attributes are always fetched as an image or media
/// resource and never parsed as a document — the only places a
/// `data:image/svg+xml` value is safe. Mirrors formajs's `IMAGE_CONTEXT_TAGS`.
const IMAGE_CONTEXT_TAGS: [&str; 13] = [
    "img", "image", "video", "audio", "source", "body", "table", "thead", "tbody", "tfoot", "tr",
    "td", "th",
];

/// True if `name` is a URL-bearing attribute (case-insensitive).
fn is_url_attr(name: &str) -> bool {
    URL_ATTRS.iter().any(|a| name.eq_ignore_ascii_case(a))
}

/// True if writing `value` to a URL attribute on `<tag>` would create an XSS
/// sink.
///
/// Browsers strip ASCII whitespace and C0/C1 control characters out of a URL
/// before resolving its scheme, so `java\tscript:alert(1)` executes even
/// though a naive prefix test does not match it. Forma uses a conservative
/// classification transform, shared with core's `isDangerousUrl`, so
/// the server emits exactly what the client would have produced.
fn is_dangerous_url(value: &str, tag: &str) -> bool {
    // A NUL anywhere in a URL attribute value is refused outright (FORMA-182
    // review S1): no author writes one, and the normalized U+FFFD it would
    // become is not ignored by the URL parser, so `java\0script:` would be
    // written as `java\u{FFFD}script:` and resolved and fetched as a
    // same-origin relative reference in every engine. The guard therefore
    // classifies the raw value; for every other character the strip below
    // makes the raw and normalized views agree.
    // Verified by: crates/forma-ir/src/walker.rs > "url_attributes_are_checked_as_the_normalized_string_that_is_written"
    if value.contains('\0') {
        return true;
    }
    let normalized: String = value
        .chars()
        .filter(|c| {
            let n = *c as u32;
            !((n <= 0x20) || (0x7F..=0x9F).contains(&n))
        })
        .collect();
    let lower = normalized.to_ascii_lowercase();

    if lower.starts_with("javascript") || lower.starts_with("vbscript") {
        return true;
    }
    if lower.starts_with("data:text/html") {
        return true;
    }
    // `data:image/svg+xml` is a *conditional* hazard: through an image sink it
    // decodes in image mode where script never runs; through a document sink
    // an `onload=` inside it fires. It is not blanket-blocked because the
    // stock CSP explicitly permits `img-src data:`.
    if lower.starts_with("data:image/svg+xml") {
        return !IMAGE_CONTEXT_TAGS
            .iter()
            .any(|t| tag.eq_ignore_ascii_case(t));
    }
    false
}

/// How an attribute's value is written out.
///
/// The distinction exists because `key` and `key=""` are the same DOM but
/// `key` and `key="false"` are not: an HTML boolean attribute is ON whenever
/// it is present, whatever its value, so `disabled="false"` disables the
/// control. See [`AttrValue::from_slot`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AttrValue<'a> {
    /// Write the bare name, no `="…"`.
    Bare,
    /// Write `key="escaped"`, including when `value` is empty.
    Valued(&'a str),
}

/// Core's HTML boolean attribute set (`BOOLEAN_ATTRS` in
/// packages/core/src/dom/element.ts): the attributes a `true` writes bare.
/// Matched as written, as core matches its prop keys.
/// Verified by: crates/forma-ir/src/walker.rs > "rule_6_attribute_values_follow_one_table"
const HTML_BOOLEAN_ATTRIBUTES: [&str; 22] = [
    "async",
    "autofocus",
    "autoplay",
    "checked",
    "controls",
    "default",
    "defer",
    "disabled",
    "formnovalidate",
    "hidden",
    "ismap",
    "loop",
    "multiple",
    "muted",
    "nomodule",
    "novalidate",
    "open",
    "playsinline",
    "readonly",
    "required",
    "reversed",
    "selected",
];

fn is_html_boolean_attribute(name: &str) -> bool {
    HTML_BOOLEAN_ATTRIBUTES.contains(&name)
}

/// Emit one attribute, dropping it entirely if it is unsafe.
///
/// Dropping (rather than emitting an empty value) is what the client does:
/// `renderAttr` returns null and hydration calls `removeAttribute`. An
/// SSR-emitted `href=""` resolves to the current page — a real navigation —
/// and would not match the client DOM.
fn emit_attr(
    out: &mut String,
    tag: &str,
    key: &str,
    val: AttrValue<'_>,
    limit: usize,
) -> Result<(), IrError> {
    if !is_valid_attr_name(key) {
        // Skip silently (matching the walker's existing convention for
        // malformed attribute operands) rather than failing the whole page
        // for one bad key.
        #[cfg(feature = "tracing")]
        tracing::warn!(attr = key, "Dropping attribute with an unsafe name");
        return Ok(());
    }
    if is_event_handler_attr(key) {
        #[cfg(feature = "tracing")]
        tracing::warn!(attr = key, "Dropping event-handler attribute from IR");
        return Ok(());
    }
    if let AttrValue::Valued(v) = val {
        if is_url_attr(key) && is_dangerous_url(v, tag) {
            #[cfg(feature = "tracing")]
            tracing::warn!(attr = key, "Dropping URL attribute with a dangerous scheme");
            return Ok(());
        }
    }

    push_checked(out, " ", limit)?;
    push_checked(out, key, limit)?;
    if let AttrValue::Valued(v) = val {
        push_checked(out, "=\"", limit)?;
        push_escaped_attr(out, v, limit)?;
        push_checked(out, "\"", limit)?;
    }
    Ok(())
}

/// Emit `<tag ...attrs`, plus any pending island / list-item attributes.
///
/// Shared by OPEN_TAG and VOID_TAG, which differ only in whether a matching
/// CLOSE_TAG follows (`has_content`). The `>` is deliberately not written yet:
/// DYN_ATTR opcodes may still append attributes.
fn emit_open_tag(
    out: &mut String,
    tag: &str,
    attrs: &[(&str, &str)],
    has_content: bool,
    state: &mut WalkState,
    limit: usize,
) -> Result<(), IrError> {
    if !is_valid_name(tag) {
        return Err(IrError::InvalidName(tag.to_string()));
    }
    // Decided before the depth moves: an element opened at the depth the
    // innermost keyed row was entered at is that row's root (FORMA-181).
    let row_key = state.open_row_root();
    state.open_tag_is_row_root = row_key.is_some();
    if row_key.is_some()
        && attrs
            .iter()
            .any(|(key, _)| key.eq_ignore_ascii_case("data-forma-key"))
    {
        // The identity attribute is the walker's to write, once, from the
        // key path; an authored one would be a second, silently first-wins
        // identity on the same root.
        return Err(innermost_row_error(
            state,
            ListKeyReason::AuthoredKey,
            IrError::InvalidName("data-forma-key".to_string()),
        ));
    }
    if has_content {
        enter_element(out, tag, state, limit)?;
        state.element_depth += 1;
    }
    push_checked(out, "<", limit)?;
    push_checked(out, tag, limit)?;

    for (key, val) in attrs {
        // `value` on a textarea or select, and `selected` on an option of a
        // bound select, are written another way.
        if consume_control_attr(state, key, Prop::Static(val)) {
            continue;
        }
        // A static attribute is authored text, never a boolean. An empty value
        // stays the bare name: `key` and `key=""` parse to the same DOM, and
        // keeping the shorthand leaves already-compiled modules byte-stable.
        let value = if val.is_empty() {
            AttrValue::Bare
        } else {
            AttrValue::Valued(val)
        };
        emit_attr(out, tag, key, value, limit)?;
    }

    // Inject pending island attributes
    if let Some(island) = state.pending_island.take() {
        push_checked(out, " data-forma-island=\"", limit)?;
        push_u16(out, island.id, limit)?;
        push_checked(out, "\" data-forma-component=\"", limit)?;
        push_escaped_attr(out, &island.component_name, limit)?;
        push_checked(out, "\" data-forma-status=\"pending\"", limit)?;
        let trigger_str = match island.trigger {
            IslandTrigger::Load => "load",
            IslandTrigger::Visible => "visible",
            IslandTrigger::Interaction => "interaction",
            IslandTrigger::Idle => "idle",
        };
        push_checked(out, " data-forma-hydrate=\"", limit)?;
        push_checked(out, trigger_str, limit)?;
        push_checked(out, "\"", limit)?;
        push_checked(out, " data-forma-state=\"", limit)?;
        push_checked(out, &island.occurrence.to_string(), limit)?;
        push_checked(out, "\" data-forma-h=\"", limit)?;
        push_checked(out, &island.hash, limit)?;
        push_checked(out, "\"", limit)?;
    }

    // The keyed row's identity, on its root (FORMA-181, DR-8). The canonical
    // text is already normalized (value rule 1); `push_escaped_attr` applies
    // the same rule again, which is idempotent, and escapes for the
    // attribute, so the browser decodes exactly the canonical text.
    if let Some(key) = row_key {
        push_checked(out, " data-forma-key=\"", limit)?;
        push_escaped_attr(out, &key, limit)?;
        push_checked(out, "\"", limit)?;
    }

    // Don't close the tag yet — DYN_ATTR opcodes may follow.
    state.pending_tag_close = true;
    state.pending_tag_name.clear();
    state.pending_tag_name.push_str(tag);
    Ok(())
}

// ---------------------------------------------------------------------------
// Parsing context: text-only parents and form controls
// ---------------------------------------------------------------------------

/// An element whose content the HTML tokenizer reads as text, not markup.
///
/// `textarea` and `title` are RCDATA: character references are decoded, but
/// a comment is not a comment. `style` (RAWTEXT) and `script` (script data)
/// decode nothing. A hydration marker inside any of them is literal text:
/// before JavaScript it is the control's value, what a form submits, and the
/// title the reader sees. So inside these four the walker writes no `f:`
/// marker of any kind (text, conditional, list or island) and no zero-width
/// space: the content is the element's static and dynamic text, in order.
///
/// The client adopts `textarea` and `title` text as a whole, by the element
/// (formajs, FORMA-003). A reactive child of `style` or `script` is not
/// supported for adoption in 0.2.x: its text is right before JavaScript, but
/// adoption may rewrite it. DR-7 makes it a build error in 3.0.0.
///
/// Dynamic text is still HTML-escaped. In RCDATA the parser decodes it back,
/// so the text is exact. In `style` and `script` nothing is decoded, so a
/// value holding `&`, `<` or `>` reads as the entity until the client writes
/// the element; that is the price of a hole that can neither close its
/// element (`</style>`) nor, in a script, swallow the rest of the page. A
/// hole there is still a code sink: the escaping does not make the value safe
/// as JavaScript or CSS.
///
/// Matched ASCII case-insensitively and in any namespace. An SVG `title` has
/// ordinary content, but its text written without markers is equally right,
/// and a single rule is one the client can match exactly.
///
/// Verified by: crates/forma-ir/src/walker.rs > "dyn_text_in_a_text_only_parent_is_written_without_markers"
/// Verified by: crates/forma-ir/src/walker.rs > "no_marker_kind_is_written_inside_a_text_only_parent"
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TextOnlyTag {
    Textarea,
    Title,
    Style,
    Script,
}

impl TextOnlyTag {
    fn from_tag(tag: &str) -> Option<Self> {
        [Self::Textarea, Self::Title, Self::Style, Self::Script]
            .into_iter()
            .find(|t| tag.eq_ignore_ascii_case(t.name()))
    }

    fn name(self) -> &'static str {
        match self {
            TextOnlyTag::Textarea => "textarea",
            TextOnlyTag::Title => "title",
            TextOnlyTag::Style => "style",
            TextOnlyTag::Script => "script",
        }
    }
}

/// The text-only element the walk is inside.
#[derive(Debug)]
struct TextOnlyParent {
    tag: TextOnlyTag,
    /// Whether any content has been written: the leading-LF rule looks at the
    /// first byte only.
    started: bool,
    /// A `textarea` whose `value` is bound: the value is its whole content,
    /// as it is on the live control, so its children are not written.
    value_bound: bool,
}

/// Write text into a text-only parent: escaped, with no marker and no
/// zero-width space, under the textarea's leading-LF rule.
///
/// The parser drops one line feed straight after `<textarea>`. A marker used
/// to stand between the tag and the first line; without one, content that
/// starts with a line break gets one more, so the parsed value is the value
/// written. Under rule 1 a leading CR or CRLF is already a line feed when it
/// is written, so only an initial LF of the normalized text is doubled. An
/// empty chunk does not consume the boundary.
///
/// Verified by: crates/forma-ir/src/walker.rs > "a_textarea_doubles_a_leading_line_feed"
/// Verified by: crates/forma-ir/src/walker.rs > "textarea_children_protect_only_an_initial_normalized_line_feed"
fn write_text_only(
    out: &mut String,
    parent: &mut TextOnlyParent,
    text: &str,
    limit: usize,
) -> Result<(), IrError> {
    if text.is_empty() {
        return Ok(());
    }
    if parent.tag == TextOnlyTag::Textarea
        && !parent.started
        && normalized_chars(text).next() == Some('\n')
    {
        push_checked(out, "\n", limit)?;
    }
    parent.started = true;
    push_escaped_html(out, text, limit)
}

/// Form-control state gathered from a start tag that is still open.
///
/// A `value` on a `textarea` or a `select` is a property: HTML never reads it
/// as an attribute, so the server shipped an empty textarea and a select
/// showing its first option, and a form submitted those before JavaScript.
/// The client's writer (`writeFormControlProperty` in `@getforma/core`) sets
/// `value == null ? '' : String(value)`; the walker writes the HTML that
/// parses to the same control, and no `value` attribute.
#[derive(Debug, Default)]
enum PendingControl {
    #[default]
    None,
    /// A `textarea`; `value` is the text its bound value stands for.
    Textarea { value: Option<String> },
    /// A `select`; `multiple` records whether that attribute is written.
    Select {
        value: Option<SlotValue>,
        multiple: bool,
    },
    /// An `option` of a bound select; `value` is its `value` attribute as
    /// written, `None` when it has none.
    OptionTag { value: Option<String> },
}

/// What a bound `select` selects.
///
/// Mirrors the client's writer. A `multiple` select given an array selects
/// every option whose value is in it (`value.map(String)`). Anything else is
/// `select.value = String(value)`, which selects the FIRST option with that
/// value; a second `selected` in the HTML would win instead, since the parser
/// keeps the last. An option's value is its `value` attribute or, without
/// one, its text, stripped and collapsed. A value no option has selects
/// nothing, and the browser then shows the first option: HTML cannot write
/// the client's `selectedIndex = -1`.
///
/// Verified by: crates/forma-ir/src/walker.rs > "a_select_value_binding_selects_the_matching_option"
/// Verified by: crates/forma-ir/src/walker.rs > "a_single_select_selects_only_the_first_matching_option"
/// Verified by: crates/forma-ir/src/walker.rs > "a_multiple_select_selects_every_option_in_an_array_value"
#[derive(Debug)]
struct SelectBinding {
    wanted: SelectWanted,
    /// A single select has selected its option.
    matched: bool,
}

#[derive(Debug)]
enum SelectWanted {
    One(String),
    Any(Vec<String>),
}

impl SelectBinding {
    fn new(value: &SlotValue, multiple: bool) -> Self {
        // Each wanted value is compared with option values that are written
        // normalized (rule 1), so it is normalized the same way: a requested
        // NUL matches the option whose attribute the browser reads as U+FFFD.
        let wanted = match value {
            SlotValue::Array(items) if multiple => {
                SelectWanted::Any(items.iter().map(js_string).collect())
            }
            // `value == null ? '' : String(value)` is exactly `to_text`.
            other => SelectWanted::One(normalize_text(&other.to_text()).into_owned()),
        };
        SelectBinding {
            wanted,
            matched: false,
        }
    }

    /// Whether the option valued `option_value` is selected.
    fn selects(&mut self, option_value: &str) -> bool {
        match &self.wanted {
            SelectWanted::Any(values) => values.iter().any(|v| v == option_value),
            SelectWanted::One(value) => {
                let hit = !self.matched && value == option_value;
                self.matched |= hit;
                hit
            }
        }
    }
}

/// `String(value)` for one member of an array: unlike a text hole, `null` is
/// `"null"`.
fn js_string(value: &SlotValue) -> String {
    match value {
        SlotValue::Null => "null".to_string(),
        other => normalize_text(&other.to_text()).into_owned(),
    }
}

/// An option of a bound select with no `value` attribute. Its value is its
/// text, known only when the option ends, so ` selected` is inserted then.
#[derive(Debug)]
struct OptionByText {
    /// Byte offset of the `>` that ends the option's start tag. The walker
    /// only ever appends to its output, so the offset stays valid.
    insert_at: usize,
    /// The option's text as the parser will read it.
    text: String,
}

/// An attribute value as the IR gives it: authored text, or a slot.
#[derive(Clone, Copy)]
enum Prop<'a> {
    Static(&'a str),
    Slot(&'a SlotValue),
}

impl Prop<'_> {
    /// The client's control text: `value == null ? '' : String(value)`, under
    /// rule 1, since it is compared with and written as DOM text.
    fn control_text(self) -> String {
        match self {
            Prop::Static(s) => normalize_text(s).into_owned(),
            Prop::Slot(v) => normalize_text(&v.to_text()).into_owned(),
        }
    }

    /// Whether the generic path writes the attribute at all (DYN_ATTR's
    /// boolean rule: `false` and `null` omit it).
    fn is_written(self) -> bool {
        !matches!(
            self,
            Prop::Slot(SlotValue::Null) | Prop::Slot(SlotValue::Bool(false))
        )
    }

    /// The value the attribute is written with: `None` when omitted, `""`
    /// when bare.
    fn written(self) -> Option<String> {
        match self {
            _ if !self.is_written() => None,
            Prop::Slot(SlotValue::Bool(true)) => Some(String::new()),
            other => Some(other.control_text()),
        }
    }

    fn to_slot(self) -> SlotValue {
        match self {
            Prop::Static(s) => SlotValue::Text(s.to_string()),
            Prop::Slot(v) => v.clone(),
        }
    }
}

/// Apply the form-control rules to one attribute of the open start tag.
/// Returns true when the attribute is consumed and must not be written.
///
/// Keys match exactly, as the client's `isFormControlProperty` does.
fn consume_control_attr(state: &mut WalkState, key: &str, prop: Prop<'_>) -> bool {
    match &mut state.pending_control {
        PendingControl::None => false,
        PendingControl::Textarea { value } => {
            if key != "value" {
                return false;
            }
            *value = Some(prop.control_text());
            true
        }
        PendingControl::Select { value, multiple } => match key {
            "value" => {
                *value = Some(prop.to_slot());
                true
            }
            "multiple" => {
                *multiple |= match prop {
                    Prop::Static(_) => true,
                    Prop::Slot(value) => value.as_bool(),
                };
                false
            }
            _ => false,
        },
        PendingControl::OptionTag { value } => match key {
            // The binding decides, as on the client, where the select's
            // value is written after its options.
            "selected" => true,
            "value" => {
                // The parser keeps the first of two same-named attributes.
                if value.is_none() {
                    *value = prop.written();
                }
                false
            }
            _ => false,
        },
    }
}

/// Set up the parsing context for an element whose start tag (OPEN_TAG) is
/// about to be written.
fn enter_element(
    out: &mut String,
    tag: &str,
    state: &mut WalkState,
    limit: usize,
) -> Result<(), IrError> {
    let is = |name: &str| tag.eq_ignore_ascii_case(name);
    // A new option or optgroup ends the open option: `</option>` is optional.
    if is("option") || is("optgroup") {
        finish_option_by_text(out, state, limit)?;
    }
    state.pending_control = if is("textarea") {
        PendingControl::Textarea { value: None }
    } else if is("select") {
        PendingControl::Select {
            value: None,
            multiple: false,
        }
    } else if is("option") && state.select.is_some() {
        PendingControl::OptionTag { value: None }
    } else {
        PendingControl::None
    };
    if state.text_only.is_none() {
        state.text_only = TextOnlyTag::from_tag(tag).map(|tag| TextOnlyParent {
            tag,
            started: false,
            value_bound: false,
        });
    }
    Ok(())
}

/// Tear down the parsing context at a CLOSE_TAG, before `</tag>` is written.
fn leave_element(
    out: &mut String,
    tag: &str,
    state: &mut WalkState,
    limit: usize,
) -> Result<(), IrError> {
    let is = |name: &str| tag.eq_ignore_ascii_case(name);
    if is("option") || is("optgroup") || is("select") {
        finish_option_by_text(out, state, limit)?;
    }
    if is("select") {
        state.select = None;
    }
    // `</pre>` is a token too; the length check would already miss, but an
    // explicit clear keeps the state's meaning local to its element.
    state.pre_boundary = None;
    if state
        .text_only
        .as_ref()
        .is_some_and(|parent| is(parent.tag.name()))
    {
        state.text_only = None;
    }
    Ok(())
}

/// Write the `>` that ends the open start tag, resolving the form-control
/// state gathered from its attributes.
///
/// Every place that used to write a bare `>` for `pending_tag_close` calls
/// this instead, so a control is resolved whichever opcode ends its tag.
fn close_start_tag(out: &mut String, state: &mut WalkState, limit: usize) -> Result<(), IrError> {
    state.pending_tag_close = false;
    state.open_tag_is_row_root = false;
    match std::mem::take(&mut state.pending_control) {
        PendingControl::None => {}
        PendingControl::Textarea { value } => {
            push_checked(out, ">", limit)?;
            if let (Some(value), Some(parent)) = (value, state.text_only.as_mut()) {
                if parent.tag == TextOnlyTag::Textarea {
                    parent.value_bound = true;
                    write_text_only(out, parent, &value, limit)?;
                }
            }
            return Ok(());
        }
        PendingControl::Select { value, multiple } => {
            state.select = value.map(|value| SelectBinding::new(&value, multiple));
        }
        PendingControl::OptionTag { value: Some(value) } => {
            if state.select.as_mut().is_some_and(|s| s.selects(&value)) {
                push_checked(out, " selected", limit)?;
            }
        }
        PendingControl::OptionTag { value: None } => {
            state.option_by_text = Some(OptionByText {
                insert_at: out.len(),
                text: String::new(),
            });
        }
    }
    push_checked(out, ">", limit)?;
    let tag = &state.pending_tag_name;
    if tag.eq_ignore_ascii_case("pre") || tag.eq_ignore_ascii_case("listing") {
        state.pre_boundary = Some(out.len());
    }
    Ok(())
}

/// End an option whose value is its text, selecting it now if the text
/// matches. Runs at `</option>` and wherever the parser ends one implicitly.
///
/// Verified by: crates/forma-ir/src/walker.rs > "a_select_value_binding_matches_an_option_without_a_value_by_its_text"
fn finish_option_by_text(
    out: &mut String,
    state: &mut WalkState,
    limit: usize,
) -> Result<(), IrError> {
    let Some(option) = state.option_by_text.take() else {
        return Ok(());
    };
    let value = strip_and_collapse_ascii_whitespace(&option.text);
    if state.select.as_mut().is_some_and(|s| s.selects(&value)) {
        const SELECTED: &str = " selected";
        if out.len().saturating_add(SELECTED.len()) > limit {
            return Err(IrError::OutputLimitExceeded { limit });
        }
        out.insert_str(option.insert_at, SELECTED);
    }
    Ok(())
}

/// HTML's "strip and collapse ASCII whitespace", which turns an option's text
/// into its value.
fn strip_and_collapse_ascii_whitespace(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for word in text.split_ascii_whitespace() {
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(word);
    }
    out
}

// ---------------------------------------------------------------------------
// Byte-reading helpers
// ---------------------------------------------------------------------------

/// Read a little-endian u16 from `data` at `pos`.
pub(crate) fn read_u16(data: &[u8], pos: usize) -> Result<u16, IrError> {
    if pos + 2 > data.len() {
        return Err(IrError::BufferTooShort {
            expected: pos + 2,
            actual: data.len(),
        });
    }
    Ok(u16::from_le_bytes([data[pos], data[pos + 1]]))
}

/// Read a little-endian u32 from `data` at `pos`.
pub(crate) fn read_u32(data: &[u8], pos: usize) -> Result<u32, IrError> {
    if pos + 4 > data.len() {
        return Err(IrError::BufferTooShort {
            expected: pos + 4,
            actual: data.len(),
        });
    }
    Ok(u32::from_le_bytes([
        data[pos],
        data[pos + 1],
        data[pos + 2],
        data[pos + 3],
    ]))
}

/// Read an OPEN_TAG or VOID_TAG operand block:
/// `str_idx(u32) + attr_count(u16) + [(key_str_idx(u32), val_str_idx(u32))] x attr_count`
///
/// Returns `(tag_str_idx, vec of (key, val) string refs, new position)`.
pub(crate) fn read_tag_with_attrs<'a>(
    ops: &[u8],
    pos: usize,
    strings: &'a crate::parser::StringTable,
) -> Result<(u32, Vec<(&'a str, &'a str)>, usize), IrError> {
    let tag_str_idx = read_u32(ops, pos)?;
    let attr_count = read_u16(ops, pos + 4)? as usize;
    let mut cursor = pos + 6; // past str_idx(4) + attr_count(2)

    // `attr_count` is u16-bounded but each pair costs 8 bytes on disk and a
    // fat-pointer pair in memory — bound the reservation by what is actually
    // left. A valid stream still gets one exact allocation.
    let mut attrs = Vec::with_capacity(attr_count.min(ops.len().saturating_sub(cursor) / 8));
    for _ in 0..attr_count {
        let key_idx = read_u32(ops, cursor)?;
        let val_idx = read_u32(ops, cursor + 4)?;
        let key = strings.get(key_idx)?;
        let val = strings.get(val_idx)?;
        attrs.push((key, val));
        cursor += 8;
    }

    Ok((tag_str_idx, attrs, cursor))
}

// ---------------------------------------------------------------------------
// Budgeted output helpers
// ---------------------------------------------------------------------------

/// Longest escape sequence any of the escapers can emit for one character
/// (`&amp;`, `&quot;`, `<`).
const MAX_ESCAPE_EXPANSION: usize = 6;

/// Append `s`, refusing to exceed the walk's output budget.
///
/// Every append routes through here (or one of the escaping variants below)
/// because a *single* opcode can emit gigabytes: OPEN_TAG accepts up to 65,535
/// attributes, each with a 65,535-character value that escapes 5x, so a check
/// only at the top of the opcode loop never runs in time.
fn push_checked(out: &mut String, s: &str, limit: usize) -> Result<(), IrError> {
    if out.len().saturating_add(s.len()) > limit {
        return Err(IrError::OutputLimitExceeded { limit });
    }
    out.push_str(s);
    Ok(())
}

/// Append a `u16` in decimal, within the output budget, without allocating.
///
/// Marker ids are the hottest thing the walker writes — every DYN_TEXT emits
/// its id twice, every SHOW_IF and LIST likewise, and a list body repeats all
/// of that once per row. `to_string()` heap-allocated for each one.
fn push_u16(out: &mut String, value: u16, limit: usize) -> Result<(), IrError> {
    // u16::MAX is 65535: five digits, never more.
    let mut buf = [0u8; 5];
    let mut remaining = value;
    let mut start = buf.len();
    loop {
        start -= 1;
        buf[start] = b'0' + (remaining % 10) as u8;
        remaining /= 10;
        if remaining == 0 {
            break;
        }
    }
    // ASCII digits are always UTF-8; the error arm is unreachable and costs
    // nothing, where `expect` would put a panic on the hottest write.
    match core::str::from_utf8(&buf[start..]) {
        Ok(digits) => push_checked(out, digits, limit),
        Err(e) => Err(IrError::InvalidUtf8(e.to_string())),
    }
}

/// Append one normalized character, refusing to exceed the output budget by
/// its actual encoded length: U+FFFD (NUL's replacement) is three bytes, an
/// astral character four, so a check of `out.len() >= limit` before the push
/// could overshoot the budget by up to three bytes.
///
/// Verified by: crates/forma-ir/src/walker.rs > "the_output_budget_counts_the_normalized_and_escaped_bytes_actually_written"
fn push_char_checked(out: &mut String, ch: char, limit: usize) -> Result<(), IrError> {
    if out.len().saturating_add(ch.len_utf8()) > limit {
        return Err(IrError::OutputLimitExceeded { limit });
    }
    out.push(ch);
    Ok(())
}

/// Escape text for HTML content (`& < >`), within the output budget.
///
/// The text is written under rule 1 (`crate::text`): CRLF and CR become LF
/// and NUL becomes U+FFFD, per value, before escaping. A NUL grows from one
/// byte to three, which the fast path's worst-case bound (six per input
/// byte) already covers.
///
/// Verified by: crates/forma-ir/src/walker.rs > "text_and_dyn_text_write_each_value_normalized"
fn push_escaped_html(out: &mut String, text: &str, limit: usize) -> Result<(), IrError> {
    // Fast path: even worst-case expansion fits, so no per-character checks.
    if out
        .len()
        .saturating_add(text.len().saturating_mul(MAX_ESCAPE_EXPANSION))
        <= limit
    {
        for ch in normalized_chars(text) {
            match ch {
                '&' => out.push_str("&amp;"),
                '<' => out.push_str("&lt;"),
                '>' => out.push_str("&gt;"),
                _ => out.push(ch),
            }
        }
        return Ok(());
    }
    for ch in normalized_chars(text) {
        match ch {
            '&' => push_checked(out, "&amp;", limit)?,
            '<' => push_checked(out, "&lt;", limit)?,
            '>' => push_checked(out, "&gt;", limit)?,
            _ => push_char_checked(out, ch, limit)?,
        }
    }
    Ok(())
}

/// Escape text for HTML attribute values (`& " < >`), within the output budget.
/// Normalized under rule 1 exactly as [`push_escaped_html`] is.
///
/// Verified by: crates/forma-ir/src/walker.rs > "static_and_dynamic_attributes_write_each_value_normalized"
fn push_escaped_attr(out: &mut String, text: &str, limit: usize) -> Result<(), IrError> {
    if out
        .len()
        .saturating_add(text.len().saturating_mul(MAX_ESCAPE_EXPANSION))
        <= limit
    {
        for ch in normalized_chars(text) {
            match ch {
                '&' => out.push_str("&amp;"),
                '"' => out.push_str("&quot;"),
                '<' => out.push_str("&lt;"),
                '>' => out.push_str("&gt;"),
                _ => out.push(ch),
            }
        }
        return Ok(());
    }
    for ch in normalized_chars(text) {
        match ch {
            '&' => push_checked(out, "&amp;", limit)?,
            '"' => push_checked(out, "&quot;", limit)?,
            '<' => push_checked(out, "&lt;", limit)?,
            '>' => push_checked(out, "&gt;", limit)?,
            _ => push_char_checked(out, ch, limit)?,
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::test_helpers::{
        build_declared_fixture, encode_close_tag, encode_list, encode_open_tag, encode_show_if,
        encode_text, encode_void_tag,
    };
    use crate::slot::{SlotData, SlotValue};

    // -- Opcode encoding helpers local to walker tests ----------------------

    /// Encode a DYN_TEXT opcode: opcode(1) + slot_id(2) + marker_id(2)
    fn encode_dyn_text(slot_id: u16, marker_id: u16) -> Vec<u8> {
        let mut buf = Vec::new();
        buf.push(0x05); // Opcode::DynText
        buf.extend_from_slice(&slot_id.to_le_bytes());
        buf.extend_from_slice(&marker_id.to_le_bytes());
        buf
    }

    /// Encode an ISLAND_START opcode: opcode(1) + island_id(2)
    fn encode_island_start(island_id: u16) -> Vec<u8> {
        let mut buf = vec![0x0B];
        buf.extend_from_slice(&island_id.to_le_bytes());
        buf
    }

    /// Encode an ISLAND_END opcode: opcode(1) + island_id(2)
    fn encode_island_end(island_id: u16) -> Vec<u8> {
        let mut buf = vec![0x0C];
        buf.extend_from_slice(&island_id.to_le_bytes());
        buf
    }

    /// Helper: build an IrModule, walk it with empty slots, return the HTML.
    fn walk_static(strings: &[&str], opcodes: &[u8]) -> String {
        let data = build_declared_fixture(strings, &[], opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);
        walk_to_html(&module, &slots).unwrap()
    }

    /// Helper: build an IrModule with slot declarations, walk with given SlotData.
    fn walk_with_slots(
        strings: &[&str],
        slot_decls: &[(u16, u32, u8, u8, &[u8])],
        opcodes: &[u8],
        slots: &SlotData,
    ) -> String {
        let data = build_declared_fixture(strings, slot_decls, opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        walk_to_html(&module, slots).unwrap()
    }

    /// Helper: build an IrModule with slots AND islands, walk with given SlotData.
    fn walk_with_islands(
        strings: &[&str],
        slot_decls: &[(u16, u32, u8, u8, &[u8])],
        island_decls: &[(u16, u8, u8, u32, u32, &[u16])],
        opcodes: &[u8],
        slots: &SlotData,
    ) -> String {
        let data = build_declared_fixture(strings, slot_decls, opcodes, island_decls);
        let module = IrModule::parse(&data).unwrap();
        walk_to_html(&module, slots).unwrap()
    }

    fn walk_with_islands_output(
        strings: &[&str],
        slot_decls: &[(u16, u32, u8, u8, &[u8])],
        island_decls: &[(u16, u8, u8, u32, u32, &[u16])],
        opcodes: &[u8],
        slots: &SlotData,
    ) -> WalkOutput {
        let data = build_declared_fixture(strings, slot_decls, opcodes, island_decls);
        let module = IrModule::parse(&data).unwrap();
        walk_to_html_counted(&module, slots, WalkLimits::default()).unwrap()
    }

    /// Encode a DYN_ATTR opcode: opcode(1) + attr_str_idx(4) + slot_id(2)
    fn encode_dyn_attr(attr_str_idx: u32, slot_id: u16) -> Vec<u8> {
        let mut bytes = vec![0x06]; // Opcode::DynAttr
        bytes.extend_from_slice(&attr_str_idx.to_le_bytes());
        bytes.extend_from_slice(&slot_id.to_le_bytes());
        bytes
    }

    // -- Test 1: walk_static_div -------------------------------------------

    #[test]
    fn walk_static_div() {
        // <div class="container">Hello</div>
        // strings: 0="div", 1="class", 2="container", 3="Hello"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2)]));
        opcodes.extend_from_slice(&encode_text(3));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let html = walk_static(&["div", "class", "container", "Hello"], &opcodes);
        assert_eq!(html, r#"<div class="container">Hello</div>"#);
    }

    // -- Test 2: walk_void_tag ---------------------------------------------

    #[test]
    fn walk_void_tag() {
        // <input type="email">
        // strings: 0="input", 1="type", 2="email"
        let opcodes = encode_void_tag(0, &[(1, 2)]);

        let html = walk_static(&["input", "type", "email"], &opcodes);
        assert_eq!(html, r#"<input type="email">"#);
    }

    // -- Test 3: walk_nested_elements --------------------------------------

    #[test]
    fn walk_nested_elements() {
        // <div><span>Hi</span></div>
        // strings: 0="div", 1="span", 2="Hi"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_text(2));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let html = walk_static(&["div", "span", "Hi"], &opcodes);
        assert_eq!(html, "<div><span>Hi</span></div>");
    }

    // -- Test 4: walk_text_escaping ----------------------------------------

    #[test]
    fn walk_text_escaping() {
        // TEXT with "<script>alert('xss')</script>"
        let opcodes = encode_text(0);

        let html = walk_static(&["<script>alert('xss')</script>"], &opcodes);
        assert_eq!(html, "&lt;script&gt;alert('xss')&lt;/script&gt;");
    }

    // -- Test 5: walk_attr_escaping ----------------------------------------

    #[test]
    fn walk_attr_escaping() {
        // OPEN_TAG with attr value containing a double quote
        // strings: 0="div", 1="title", 2=r#"a"b"#
        let opcodes = encode_open_tag(0, &[(1, 2)]);

        let html = walk_static(&["div", "title", "a\"b"], &opcodes);
        assert_eq!(html, r#"<div title="a&quot;b">"#);
    }

    #[test]
    fn security_probe_bounded_text_escaping() {
        let data = build_declared_fixture(&["xxxxxxxxxx<"], &[], &encode_text(0), &[]);
        let module = IrModule::parse(&data).unwrap();
        let expected = "xxxxxxxxxx&lt;";
        let limits = WalkLimits {
            max_output_bytes: expected.len(),
            ..WalkLimits::default()
        };
        assert_eq!(
            walk_to_html_with_limits(&module, &SlotData::new(0), limits).as_deref(),
            Ok(expected)
        );
        let short = WalkLimits {
            max_output_bytes: expected.len() - 1,
            ..limits
        };
        assert!(matches!(
            walk_to_html_with_limits(&module, &SlotData::new(0), short),
            Err(IrError::OutputLimitExceeded { .. })
        ));
    }

    #[test]
    fn security_probe_bounded_attribute_escaping() {
        let opcodes = encode_open_tag(0, &[(1, 2)]);
        let data = build_declared_fixture(&["div", "title", "xxxxxxxxxx\""], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let expected = "<div title=\"xxxxxxxxxx&quot;\">";
        let limits = WalkLimits {
            max_output_bytes: expected.len(),
            ..WalkLimits::default()
        };
        assert_eq!(
            walk_to_html_with_limits(&module, &SlotData::new(0), limits).as_deref(),
            Ok(expected)
        );
        let short = WalkLimits {
            max_output_bytes: expected.len() - 1,
            ..limits
        };
        assert!(matches!(
            walk_to_html_with_limits(&module, &SlotData::new(0), short),
            Err(IrError::OutputLimitExceeded { .. })
        ));
    }

    // -- Test 6: walk_empty_element ----------------------------------------

    #[test]
    fn walk_empty_element() {
        // <div></div> (no children)
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let html = walk_static(&["div"], &opcodes);
        assert_eq!(html, "<div></div>");
    }

    // -- Test 7: walk_dyn_text ---------------------------------------------

    #[test]
    fn walk_dyn_text() {
        // DYN_TEXT with slot data produces marker comments + value
        // strings: 0="greeting" (slot name)
        // slot decl: slot_id=0, name_str_idx=0, type=Text(0x01)
        let opcodes = encode_dyn_text(0, 0);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("World".to_string()));

        let html = walk_with_slots(&["greeting"], &[(0, 0, 0x01, 0x00, &[])], &opcodes, &slots);
        assert_eq!(html, "<!--f:t0-->World<!--/f:t0-->");
    }

    #[test]
    fn walk_dyn_text_null_emits_zwsp() {
        // DYN_TEXT with null slot value must emit a zero-width space between
        // markers so the browser creates a Text node for hydration to bind to.
        let opcodes = encode_dyn_text(0, 0);
        let slots = SlotData::new(1); // slot 0 defaults to Null

        let html = walk_with_slots(&["msg"], &[(0, 0, 0x01, 0x00, &[])], &opcodes, &slots);
        assert_eq!(html, "<!--f:t0-->\u{200B}<!--/f:t0-->");
    }

    #[test]
    fn walk_dyn_text_empty_string_emits_zwsp() {
        // DYN_TEXT with explicit empty string also needs the ZWSP placeholder.
        let opcodes = encode_dyn_text(0, 0);
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text(String::new()));

        let html = walk_with_slots(&["msg"], &[(0, 0, 0x01, 0x00, &[])], &opcodes, &slots);
        assert_eq!(html, "<!--f:t0-->\u{200B}<!--/f:t0-->");
    }

    // -- Test 8: walk_dyn_text_escaping ------------------------------------

    #[test]
    fn walk_dyn_text_escaping() {
        // DYN_TEXT value with HTML chars is escaped
        let opcodes = encode_dyn_text(0, 1);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("<b>bold</b>".to_string()));

        let html = walk_with_slots(&["content"], &[(0, 0, 0x01, 0x00, &[])], &opcodes, &slots);
        assert_eq!(html, "<!--f:t1-->&lt;b&gt;bold&lt;/b&gt;<!--/f:t1-->");
    }

    // -- Test 9: walk_island_markers ---------------------------------------

    #[test]
    fn walk_island_markers() {
        // ISLAND_START(0) + TEXT content + ISLAND_END(0). The island body
        // opens no element, so the island contributes markers only — its
        // attributes are dropped at ISLAND_END with a warning.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_text(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["Hello"],
            &[],
            &[(0, 0x01, 0x01, 0, 0, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(html, "<!--f:i0-->Hello<!--/f:i0-->");
    }

    // -- Test 10: walk_comment ---------------------------------------------

    // -- Test 11: walk_boolean_attr ----------------------------------------

    #[test]
    fn walk_boolean_attr() {
        // <input disabled> — attribute with empty string value renders as valueless
        // strings: 0="input", 1="disabled", 2="" (empty string for boolean attr)
        let opcodes = encode_void_tag(0, &[(1, 2)]);

        let html = walk_static(&["input", "disabled", ""], &opcodes);
        assert_eq!(html, "<input disabled>");
    }

    // -- DYN_ATTR value semantics (W2 / F15) --------------------------------

    /// Helper: `<input DYN_ATTR(disabled, slot 0)>` with slot 0 set to `value`.
    fn walk_dyn_attr_with(value: SlotValue) -> String {
        let mut opcodes = encode_void_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let mut slots = SlotData::new(1);
        slots.set(0, value);
        walk_with_slots(
            &["input", "disabled"],
            &[(0, 1, 0x02, 0x00, &[])],
            &opcodes,
            &slots,
        )
    }

    #[test]
    fn dyn_attr_bool_matches_the_client_renderer() {
        // An HTML boolean attribute is ON whenever it is PRESENT, whatever its
        // value — `disabled="false"` disables the control. The walker used to
        // emit exactly that, so SSR shipped a disabled/hidden/checked control
        // that only un-broke itself after hydration, and never at all for a
        // visible/idle/interaction island the user had not reached, or with JS
        // off. These are byte-for-byte what `renderAttr` in @getforma/core
        // produces for the same values.
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Bool(true)),
            "<input disabled>",
            "true must render the bare attribute"
        );
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Bool(false)),
            "<input>",
            "false must omit the attribute, not write disabled=\"false\""
        );
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Null),
            "<input>",
            "null must omit the attribute"
        );
    }

    #[test]
    fn dyn_attr_empty_string_and_zero_follow_boolean_presence() {
        // The original expected ordinary value semantics on `disabled`.
        // Its exact source is archived with the FORMA-182 review evidence.
        // Ordinary empty values remain covered by attributes.json.
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Text(String::new())),
            "<input>"
        );
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Text("yes".into())),
            "<input disabled>"
        );
        assert_eq!(walk_dyn_attr_with(SlotValue::Number(0.0)), "<input>");
        assert_eq!(
            walk_dyn_attr_with(SlotValue::Number(1.0)),
            "<input disabled>"
        );
    }

    // -- Test 12: walk_multiple_attrs --------------------------------------

    #[test]
    fn walk_multiple_attrs() {
        // <div id="main" class="container" data-x="42"></div>
        // strings: 0="div", 1="id", 2="main", 3="class", 4="container", 5="data-x", 6="42"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2), (3, 4), (5, 6)]));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let html = walk_static(
            &["div", "id", "main", "class", "container", "data-x", "42"],
            &opcodes,
        );
        assert_eq!(
            html,
            r#"<div id="main" class="container" data-x="42"></div>"#
        );
    }

    // -- Test 13: walk_show_if_true_branch ---------------------------------

    #[test]
    fn walk_show_if_true_branch() {
        // SHOW_IF(slot=0): then=TEXT("Yes"), else=TEXT("No")
        // slot 0 = Bool(true) → render "Yes"
        // strings: 0="visible", 1="Yes", 2="No"
        let then_ops = encode_text(1); // TEXT "Yes"
        let else_ops = encode_text(2); // TEXT "No"
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Bool(true));

        let html = walk_with_slots(
            &["visible", "Yes", "No"],
            &[(0, 0, 0x02, 0x00, &[])], // slot 0, name="visible", type=Bool
            &opcodes,
            &slots,
        );
        assert!(html.contains("Yes"), "should contain then-branch text");
        assert!(!html.contains("No"), "should NOT contain else-branch text");
        assert!(html.contains("<!--f:s0-->"), "should have opening marker");
        assert!(html.contains("<!--/f:s0-->"), "should have closing marker");
    }

    // -- Test 14: walk_show_if_false_branch --------------------------------

    #[test]
    fn walk_show_if_false_branch() {
        // SHOW_IF(slot=0): then=TEXT("Yes"), else=TEXT("No")
        // slot 0 = Bool(false) → render "No"
        let then_ops = encode_text(1);
        let else_ops = encode_text(2);
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Bool(false));

        let html = walk_with_slots(
            &["visible", "Yes", "No"],
            &[(0, 0, 0x02, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert!(html.contains("No"), "should contain else-branch text");
        assert!(!html.contains("Yes"), "should NOT contain then-branch text");
        assert!(html.contains("<!--f:s0-->"), "should have opening marker");
        assert!(html.contains("<!--/f:s0-->"), "should have closing marker");
    }

    // -- Test 15: walk_show_if_truthy_text ---------------------------------

    #[test]
    fn walk_show_if_truthy_text() {
        // slot 0 = Text("hello") → truthy → render then branch
        let then_ops = encode_text(1);
        let else_ops = encode_text(2);
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("hello".to_string()));

        let html = walk_with_slots(
            &["greeting", "Shown", "Hidden"],
            &[(0, 0, 0x01, 0x00, &[])], // type=Text
            &opcodes,
            &slots,
        );
        assert!(
            html.contains("Shown"),
            "truthy text should render then branch"
        );
        assert!(
            !html.contains("Hidden"),
            "truthy text should NOT render else branch"
        );
    }

    // -- Test 16: walk_show_if_falsy_null ----------------------------------

    #[test]
    fn walk_show_if_falsy_null() {
        // slot 0 = Null (default) → falsy → render else branch
        let then_ops = encode_text(1);
        let else_ops = encode_text(2);
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let slots = SlotData::new(1); // slot 0 is Null by default

        let html = walk_with_slots(
            &["maybe", "Present", "Missing"],
            &[(0, 0, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert!(html.contains("Missing"), "null should render else branch");
        assert!(
            !html.contains("Present"),
            "null should NOT render then branch"
        );
    }

    // -- Test 17: walk_show_if_no_else ------------------------------------

    #[test]
    fn walk_show_if_no_else() {
        // SHOW_IF with empty else branch. When false, nothing rendered between markers.
        let then_ops = encode_text(1);
        let else_ops: Vec<u8> = vec![]; // empty else
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let slots = SlotData::new(1); // Null → false

        let html = walk_with_slots(
            &["flag", "Content"],
            &[(0, 0, 0x02, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html, "<!--f:s0--><!--/f:s0-->",
            "empty else should produce only markers"
        );
    }

    // -- Test 18: walk_show_if_nested -------------------------------------

    #[test]
    fn walk_show_if_nested() {
        // Outer SHOW_IF(slot=0, true) → then branch contains inner SHOW_IF(slot=1, false)
        // Inner then=TEXT("inner-yes"), inner else=TEXT("inner-no")
        // strings: 0="outer", 1="inner", 2="outer-yes", 3="outer-no", 4="inner-yes", 5="inner-no"
        let inner_then = encode_text(4);
        let inner_else = encode_text(5);
        let inner_show_if = encode_show_if(1, &inner_then, &inner_else);

        let outer_then = inner_show_if; // outer then = inner SHOW_IF
        let outer_else = encode_text(3);
        let opcodes = encode_show_if(0, &outer_then, &outer_else);

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Bool(true)); // outer true
        slots.set(1, SlotValue::Bool(false)); // inner false

        let html = walk_with_slots(
            &[
                "outer",
                "inner",
                "outer-yes",
                "outer-no",
                "inner-yes",
                "inner-no",
            ],
            &[(0, 0, 0x02, 0x00, &[]), (1, 1, 0x02, 0x00, &[])],
            &opcodes,
            &slots,
        );
        // Outer is true → walk inner. Inner is false → render "inner-no"
        assert!(
            html.contains("inner-no"),
            "nested: inner false should render inner else"
        );
        assert!(
            !html.contains("inner-yes"),
            "nested: inner true branch should not render"
        );
        assert!(
            !html.contains("outer-no"),
            "nested: outer else should not render"
        );
        // Verify correct nesting of markers
        assert!(
            html.contains("<!--f:s0-->"),
            "should have outer opening marker"
        );
        assert!(
            html.contains("<!--/f:s0-->"),
            "should have outer closing marker"
        );
        assert!(
            html.contains("<!--f:s1-->"),
            "should have inner opening marker"
        );
        assert!(
            html.contains("<!--/f:s1-->"),
            "should have inner closing marker"
        );
    }

    // -- Test 19: walk_show_if_with_elements ------------------------------

    #[test]
    fn walk_show_if_with_elements() {
        // Then branch contains OPEN_TAG("span") + TEXT("Hello") + CLOSE_TAG("span")
        // strings: 0="flag", 1="span", 2="Hello", 3="Bye"
        let mut then_ops = Vec::new();
        then_ops.extend_from_slice(&encode_open_tag(1, &[]));
        then_ops.extend_from_slice(&encode_text(2));
        then_ops.extend_from_slice(&encode_close_tag(1));

        let else_ops = encode_text(3);
        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Bool(true));

        let html = walk_with_slots(
            &["flag", "span", "Hello", "Bye"],
            &[(0, 0, 0x02, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html, "<!--f:s0--><span>Hello</span><!--/f:s0-->",
            "then branch should produce full HTML element"
        );
    }

    // -- Test 20: walk_list_renders_items -----------------------------------

    #[test]
    fn walk_list_renders_items() {
        // LIST(slot=0, item_slot=1): body = DYN_TEXT(slot=1, marker=0)
        // slot 0 = Array([Text("Alice"), Text("Bob")])
        // strings: 0="items", 1="item"
        let body = encode_dyn_text(1, 0);
        let opcodes = encode_list(0, 1, &body);

        let mut slots = SlotData::new(2);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::Text("Alice".to_string()),
                SlotValue::Text("Bob".to_string()),
            ]),
        );

        let html = walk_with_slots(
            &["items", "item"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])], // slot 0=Array, slot 1=Text
            &opcodes,
            &slots,
        );
        assert!(html.contains("Alice"), "should contain first item");
        assert!(html.contains("Bob"), "should contain second item");
        assert!(
            html.starts_with("<!--f:l0-->"),
            "should start with list open marker"
        );
        assert!(
            html.ends_with("<!--/f:l0-->"),
            "should end with list close marker"
        );
    }

    // -- Test 21: walk_list_empty_array ------------------------------------

    #[test]
    fn walk_list_empty_array() {
        // LIST(slot=0, item_slot=1): body = DYN_TEXT(slot=1, marker=0)
        // slot 0 = Array([])
        let body = encode_dyn_text(1, 0);
        let opcodes = encode_list(0, 1, &body);

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::array(vec![]));

        let html = walk_with_slots(
            &["items", "item"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html, "<!--f:l0--><!--/f:l0-->",
            "empty array should produce only markers"
        );
    }

    // -- Test 22: walk_list_with_elements ----------------------------------

    #[test]
    fn walk_list_with_elements() {
        // LIST(slot=0, item_slot=1): body = <li>DYN_TEXT(slot=1)</li>
        // slot 0 = Array([Text("A"), Text("B")])
        // strings: 0="items", 1="item", 2="li"
        let mut body = Vec::new();
        body.extend_from_slice(&encode_open_tag(2, &[]));
        body.extend_from_slice(&encode_dyn_text(1, 0));
        body.extend_from_slice(&encode_close_tag(2));

        let opcodes = encode_list(0, 1, &body);

        let mut slots = SlotData::new(2);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::Text("A".to_string()),
                SlotValue::Text("B".to_string()),
            ]),
        );

        let html = walk_with_slots(
            &["items", "item", "li"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert!(
            html.contains("<li><!--f:t0-->A<!--/f:t0--></li><li><!--f:t0-->B<!--/f:t0--></li>"),
            "should render li elements with content, got: {html}"
        );
    }

    // -- Test 23: walk_list_null_slot --------------------------------------

    #[test]
    fn walk_list_null_slot() {
        // LIST(slot=0, item_slot=1): body = DYN_TEXT(slot=1, marker=0)
        // slot 0 = Null (not Array) → no iteration, empty markers
        let body = encode_dyn_text(1, 0);
        let opcodes = encode_list(0, 1, &body);

        let slots = SlotData::new(2); // slot 0 remains Null

        let html = walk_with_slots(
            &["items", "item"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html, "<!--f:l0--><!--/f:l0-->",
            "null slot should produce only markers"
        );
    }

    // -- Test 24: walk_list_nested -----------------------------------------

    #[test]
    fn walk_list_nested() {
        // Outer LIST(slot=0, item_slot=1): each item is an Array
        // Inner LIST(slot=1, item_slot=2): each sub-item is Text
        // Body of inner: DYN_TEXT(slot=2, marker=0)
        // strings: 0="outer", 1="inner", 2="item"
        let inner_body = encode_dyn_text(2, 0);
        let inner_list = encode_list(1, 2, &inner_body);
        let opcodes = encode_list(0, 1, &inner_list);

        let mut slots = SlotData::new(3);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::array(vec![
                    SlotValue::Text("a".to_string()),
                    SlotValue::Text("b".to_string()),
                ]),
                SlotValue::array(vec![SlotValue::Text("c".to_string())]),
            ]),
        );

        let html = walk_with_slots(
            &["outer", "inner", "item"],
            &[
                (0, 0, 0x04, 0x00, &[]),
                (1, 1, 0x04, 0x00, &[]),
                (2, 2, 0x01, 0x00, &[]),
            ],
            &opcodes,
            &slots,
        );
        // Verify all items rendered
        assert!(html.contains("a"), "should contain nested item 'a'");
        assert!(html.contains("b"), "should contain nested item 'b'");
        assert!(html.contains("c"), "should contain nested item 'c'");
        // Verify outer list markers
        assert!(
            html.starts_with("<!--f:l0-->"),
            "should have outer list open marker"
        );
        assert!(
            html.ends_with("<!--/f:l0-->"),
            "should have outer list close marker"
        );
        // Verify inner list markers present
        assert!(
            html.contains("<!--f:l1-->"),
            "should have inner list open marker"
        );
        assert!(
            html.contains("<!--/f:l1-->"),
            "should have inner list close marker"
        );
    }

    // -- Test 25: walk_list_depth_exceeded ---------------------------------

    #[test]
    fn walk_list_depth_exceeded() {
        // Nest 5 lists deep (exceeds MAX_LIST_DEPTH of 4)
        // Innermost body is a DYN_TEXT
        // strings: 0..=5 = "s0".."s5"
        let innermost_body = encode_dyn_text(5, 0);
        // Build from inside out: level 5 (innermost) → level 1 (outermost)
        let level5 = encode_list(4, 5, &innermost_body);
        let level4 = encode_list(3, 4, &level5);
        let level3 = encode_list(2, 3, &level4);
        let level2 = encode_list(1, 2, &level3);
        let level1 = encode_list(0, 1, &level2);

        let mut slots = SlotData::new(6);
        // Each level needs an array with at least one item to trigger iteration
        slots.set(
            0,
            SlotValue::array(vec![SlotValue::array(vec![SlotValue::array(vec![
                SlotValue::array(vec![SlotValue::array(vec![SlotValue::Text(
                    "deep".to_string(),
                )])]),
            ])])]),
        );
        // Inner slots get shadowed during iteration

        let data = build_declared_fixture(
            &["s0", "s1", "s2", "s3", "s4", "s5"],
            &[
                (0, 0, 0x04, 0x00, &[]),
                (1, 1, 0x04, 0x00, &[]),
                (2, 2, 0x04, 0x00, &[]),
                (3, 3, 0x04, 0x00, &[]),
                (4, 4, 0x04, 0x00, &[]),
                (5, 5, 0x01, 0x00, &[]),
            ],
            &level1,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();
        let result = walk_to_html(&module, &slots);
        assert!(result.is_err(), "should fail with depth exceeded");
        match result.unwrap_err() {
            IrError::ListDepthExceeded { max: 4 } => {}
            other => panic!("expected ListDepthExceeded {{ max: 4 }}, got {other:?}"),
        }
    }

    // -- Test 26: walk_switch_matching_case --------------------------------

    // -- Test 27: walk_switch_no_match ------------------------------------

    // -- Test 28: walk_switch_first_case ----------------------------------

    // -- Test 29: walk_switch_with_elements -------------------------------

    // -- Test 30: walk_try_renders_main -----------------------------------

    // -- Test 31: walk_preload_font ---------------------------------------

    // -- Test 32: walk_preload_style --------------------------------------

    // =========================================================================
    // Hydration-roundtrip tests — verify walker produces exact expected HTML
    // from programmatically-built IR. These tests complement the TS-side
    // roundtrip tests (ir-roundtrip.test.ts) to ensure the binary contract
    // between the TS emitter and the Rust walker is correct.
    // =========================================================================

    // -- Roundtrip 1: Full page skeleton ------------------------------------

    #[test]
    fn roundtrip_page_structure() {
        // Build IR for a realistic page skeleton:
        // <html lang="en"><head><meta charset="utf-8"><title>Test</title></head>
        // <body><div id="app"></div></body></html>
        //
        // strings: 0="html", 1="lang", 2="en", 3="head", 4="meta",
        //          5="charset", 6="utf-8", 7="title", 8="Test",
        //          9="body", 10="div", 11="id", 12="app"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2)])); // <html lang="en">
        opcodes.extend_from_slice(&encode_open_tag(3, &[])); // <head>
        opcodes.extend_from_slice(&encode_void_tag(4, &[(5, 6)])); // <meta charset="utf-8">
        opcodes.extend_from_slice(&encode_open_tag(7, &[])); // <title>
        opcodes.extend_from_slice(&encode_text(8)); // Test
        opcodes.extend_from_slice(&encode_close_tag(7)); // </title>
        opcodes.extend_from_slice(&encode_close_tag(3)); // </head>
        opcodes.extend_from_slice(&encode_open_tag(9, &[])); // <body>
        opcodes.extend_from_slice(&encode_open_tag(10, &[(11, 12)])); // <div id="app">
        opcodes.extend_from_slice(&encode_close_tag(10)); // </div>
        opcodes.extend_from_slice(&encode_close_tag(9)); // </body>
        opcodes.extend_from_slice(&encode_close_tag(0)); // </html>

        let ir = build_declared_fixture(
            &[
                "html", "lang", "en", "head", "meta", "charset", "utf-8", "title", "Test", "body",
                "div", "id", "app",
            ],
            &[],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&ir).unwrap();
        let slots = SlotData::new(0);
        let html = walk_to_html(&module, &slots).unwrap();
        assert_eq!(
            html,
            r#"<html lang="en"><head><meta charset="utf-8"><title>Test</title></head><body><div id="app"></div></body></html>"#
        );
    }

    // -- Roundtrip 2: Dynamic greeting with DYN_TEXT ------------------------

    #[test]
    fn roundtrip_dynamic_greeting() {
        // Build IR for: <div class="greeting"><!--f:t0-->Hello, World<!--/f:t0--></div>
        // with slot 0 = Text("Hello, World")
        //
        // strings: 0="div", 1="class", 2="greeting", 3="content"
        // slot decl: slot_id=0, name_str_idx=3 ("content"), type=Text
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2)])); // <div class="greeting">
        opcodes.extend_from_slice(&encode_dyn_text(0, 0)); // DYN_TEXT slot=0 marker=0
        opcodes.extend_from_slice(&encode_close_tag(0)); // </div>

        let ir = build_declared_fixture(
            &["div", "class", "greeting", "content"],
            &[(0, 3, 0x01, 0x00, &[])], // slot 0, name="content", type=Text
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&ir).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("Hello, World".into()));
        let html = walk_to_html(&module, &slots).unwrap();
        assert_eq!(
            html,
            r#"<div class="greeting"><!--f:t0-->Hello, World<!--/f:t0--></div>"#
        );
    }

    // -- Roundtrip 3: Conditional content (SHOW_IF both branches) -----------

    #[test]
    fn roundtrip_conditional_content() {
        // SHOW_IF slot=0
        //   then: <span class="badge">Logged In</span>
        //   else: <span class="badge">Guest</span>
        //
        // strings: 0="auth", 1="span", 2="class", 3="badge",
        //          4="Logged In", 5="Guest"
        let mut then_ops = Vec::new();
        then_ops.extend_from_slice(&encode_open_tag(1, &[(2, 3)]));
        then_ops.extend_from_slice(&encode_text(4));
        then_ops.extend_from_slice(&encode_close_tag(1));

        let mut else_ops = Vec::new();
        else_ops.extend_from_slice(&encode_open_tag(1, &[(2, 3)]));
        else_ops.extend_from_slice(&encode_text(5));
        else_ops.extend_from_slice(&encode_close_tag(1));

        let opcodes = encode_show_if(0, &then_ops, &else_ops);

        let ir = build_declared_fixture(
            &["auth", "span", "class", "badge", "Logged In", "Guest"],
            &[(0, 0, 0x02, 0x00, &[])], // slot 0, name="auth", type=Bool
            &opcodes,
            &[],
        );

        // Test true branch
        {
            let module = IrModule::parse(&ir).unwrap();
            let mut slots = SlotData::new(1);
            slots.set(0, SlotValue::Bool(true));
            let html = walk_to_html(&module, &slots).unwrap();
            assert_eq!(
                html,
                r#"<!--f:s0--><span class="badge">Logged In</span><!--/f:s0-->"#
            );
        }

        // Test false branch
        {
            let module = IrModule::parse(&ir).unwrap();
            let mut slots = SlotData::new(1);
            slots.set(0, SlotValue::Bool(false));
            let html = walk_to_html(&module, &slots).unwrap();
            assert_eq!(
                html,
                r#"<!--f:s0--><span class="badge">Guest</span><!--/f:s0-->"#
            );
        }
    }

    // -- Roundtrip 4: List rendering ----------------------------------------

    #[test]
    fn roundtrip_list_rendering() {
        // LIST slot=0 item=1 → <li><!--f:t0-->{item}<!--/f:t0--></li>
        // Items: ["Alice", "Bob", "Charlie"]
        //
        // strings: 0="items", 1="item", 2="li"
        // slot decls: slot 0=Array, slot 1=Text (item)
        let mut body = Vec::new();
        body.extend_from_slice(&encode_open_tag(2, &[])); // <li>
        body.extend_from_slice(&encode_dyn_text(1, 0)); // DYN_TEXT slot=1 marker=0
        body.extend_from_slice(&encode_close_tag(2)); // </li>

        let opcodes = encode_list(0, 1, &body);

        let ir = build_declared_fixture(
            &["items", "item", "li"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])], // slot 0=Array, slot 1=Text
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&ir).unwrap();
        let mut slots = SlotData::new(2);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::Text("Alice".into()),
                SlotValue::Text("Bob".into()),
                SlotValue::Text("Charlie".into()),
            ]),
        );
        let html = walk_to_html(&module, &slots).unwrap();
        assert_eq!(
            html,
            "<!--f:l0-->\
             <li><!--f:t0-->Alice<!--/f:t0--></li>\
             <li><!--f:t0-->Bob<!--/f:t0--></li>\
             <li><!--f:t0-->Charlie<!--/f:t0--></li>\
             <!--/f:l0-->"
        );
    }

    // -- Roundtrip 5: Switch view -------------------------------------------

    // -- Roundtrip 6: Nested realistic structure ----------------------------

    #[test]
    fn roundtrip_nested_structure() {
        // Realistic page fragment: nav with conditional auth + list of links
        //
        // <nav class="main-nav">
        //   <!--f:s0-->  (SHOW_IF: logged in?)
        //     <span>Welcome</span>
        //   <!--/f:s0-->
        //   <ul>
        //     <!--f:l1-->  (LIST: nav items)
        //       <li><!--f:t0-->{item}<!--/f:t0--></li>
        //     <!--/f:l1-->
        //   </ul>
        // </nav>
        //
        // strings: 0="nav", 1="class", 2="main-nav", 3="auth",
        //          4="span", 5="Welcome", 6="items", 7="item",
        //          8="ul", 9="li"
        //
        // slots: 0=Bool (auth), 1=Array (items), 2=Text (item shadow)

        // SHOW_IF then body: <span>Welcome</span>
        let mut then_ops = Vec::new();
        then_ops.extend_from_slice(&encode_open_tag(4, &[]));
        then_ops.extend_from_slice(&encode_text(5));
        then_ops.extend_from_slice(&encode_close_tag(4));

        let show_if_ops = encode_show_if(0, &then_ops, &[]);

        // LIST body: <li>DYN_TEXT(slot=2)</li>
        let mut list_body = Vec::new();
        list_body.extend_from_slice(&encode_open_tag(9, &[]));
        list_body.extend_from_slice(&encode_dyn_text(2, 0));
        list_body.extend_from_slice(&encode_close_tag(9));

        let list_ops = encode_list(1, 2, &list_body);

        // Full opcode stream
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2)])); // <nav class="main-nav">
        opcodes.extend_from_slice(&show_if_ops); // SHOW_IF auth
        opcodes.extend_from_slice(&encode_open_tag(8, &[])); // <ul>
        opcodes.extend_from_slice(&list_ops); // LIST items
        opcodes.extend_from_slice(&encode_close_tag(8)); // </ul>
        opcodes.extend_from_slice(&encode_close_tag(0)); // </nav>

        let ir = build_declared_fixture(
            &[
                "nav", "class", "main-nav", "auth", "span", "Welcome", "items", "item", "ul", "li",
            ],
            &[
                (0, 3, 0x02, 0x00, &[]), // slot 0 = Bool (auth)
                (1, 6, 0x04, 0x00, &[]), // slot 1 = Array (items)
                (2, 7, 0x01, 0x00, &[]), // slot 2 = Text (item)
            ],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&ir).unwrap();
        let mut slots = SlotData::new(3);
        slots.set(0, SlotValue::Bool(true));
        slots.set(
            1,
            SlotValue::array(vec![
                SlotValue::Text("Home".into()),
                SlotValue::Text("About".into()),
            ]),
        );

        let html = walk_to_html(&module, &slots).unwrap();
        assert_eq!(
            html,
            r#"<nav class="main-nav"><!--f:s0--><span>Welcome</span><!--/f:s0--><ul><!--f:l1--><li><!--f:t0-->Home<!--/f:t0--></li><li><!--f:t0-->About<!--/f:t0--></li><!--/f:l1--></ul></nav>"#
        );
    }

    // =========================================================================
    // Island attribute injection tests
    // =========================================================================

    // -- Test 33: walk_island_attrs_on_open_tag --------------------------------

    #[test]
    fn walk_island_attrs_on_open_tag() {
        // ISLAND_START(0) + OPEN_TAG("div") + TEXT("Hello") + CLOSE_TAG("div") + ISLAND_END(0)
        // With island table entry: id=0, trigger=Load, props=Inline, name="AuthForm", no slots
        // strings: 0="div", 1="Hello", 2="AuthForm"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_text(1));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["div", "Hello", "AuthForm"],
            &[],
            // island: id=0, trigger=Load(0x01), props=Inline(0x01), name_str_idx=2, byte_offset=0, no slot_ids
            &[(0, 0x01, 0x01, 2, 0, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html,
            r#"<!--f:i0--><div data-forma-island="0" data-forma-component="AuthForm" data-forma-status="pending" data-forma-hydrate="load" data-forma-state="0" data-forma-h="0000000000000000">Hello</div><!--/f:i0-->"#
        );
    }

    // -- Test 34: walk_island_attrs_with_existing_attrs -----------------------

    #[test]
    fn walk_island_attrs_with_existing_attrs() {
        // ISLAND_START(0) + OPEN_TAG("div" class="form") + CLOSE_TAG + ISLAND_END(0)
        // strings: 0="div", 1="class", 2="form", 3="AuthForm"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[(1, 2)]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["div", "class", "form", "AuthForm"],
            &[],
            &[(0, 0x01, 0x01, 3, 0, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html,
            r#"<!--f:i0--><div class="form" data-forma-island="0" data-forma-component="AuthForm" data-forma-status="pending" data-forma-hydrate="load" data-forma-state="0" data-forma-h="0000000000000000"></div><!--/f:i0-->"#
        );
    }

    // -- Test 35: walk_island_attrs_on_void_tag -------------------------------

    #[test]
    fn walk_island_attrs_on_void_tag() {
        // ISLAND_START(0) + VOID_TAG("input") + ISLAND_END(0)
        // strings: 0="input", 1="Widget"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_void_tag(0, &[]));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["input", "Widget"],
            &[],
            &[(0, 0x01, 0x01, 1, 0, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(
            html,
            r#"<!--f:i0--><input data-forma-island="0" data-forma-component="Widget" data-forma-status="pending" data-forma-hydrate="load" data-forma-state="0" data-forma-h="0000000000000000"><!--/f:i0-->"#
        );
    }

    // -- Declared state is separate from body markup -------------------------

    #[test]
    fn declared_props_are_separate_from_body_html() {
        // ISLAND_START(0) + OPEN_TAG("div") + CLOSE_TAG + ISLAND_END(0)
        // Island entry has ordered inputs [0, 1]; all props use typed state.
        // Slot 0 = "title" (Text), Slot 1 = "count" (Number)
        // strings: 0="div", 1="MyComponent", 2="title", 3="count"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Text("Hello".to_string()));
        slots.set(1, SlotValue::Number(42.0));

        let output = walk_with_islands_output(
            &["div", "MyComponent", "title", "count"],
            &[
                (0, 2, 0x01, 0x00, &[]), // slot 0, name="title", Text, Server
                (1, 3, 0x03, 0x00, &[]), // slot 1, name="count", Number, Server
            ],
            // island: id=0, Load, Inline, name_str_idx=1, byte_offset=0, slot_ids=[0, 1]
            &[(0, 0x01, 0x01, 1, 0, &[0, 1])],
            &opcodes,
            &slots,
        );
        assert!(!output.html.contains("data-forma-props"));
        assert!(!output.html.contains("<script"));
        assert!(output.html.contains("data-forma-component=\"MyComponent\""));
        let json: serde_json::Value = serde_json::from_str(output.island_state.as_json()).unwrap();
        assert_eq!(json["i"][0]["p"]["title"], "Hello");
        assert_eq!(json["i"][0]["p"]["count"], 42);
    }

    // -- Test 37: walk_list_item_key_on_first_open_tag -----------------------

    // -- Test 38: walk_list_item_key_on_void_tag -----------------------------

    // -- Test 39: walk_list_item_key_with_attrs ------------------------------

    // -- Test 40: walk_script_tag_props_mode ---------------------------------

    // -- Test 41: walk_deferred_props_mode -----------------------------------

    // -- Test 42: walk_island_no_entry_in_table -------------------------------

    #[test]
    fn walk_island_no_entry_in_table() {
        // An ISLAND_START whose id is absent from the island table used to
        // render happily: markers, but no `data-forma-*` attributes, so the
        // client could never hydrate that island and nothing said why. It is
        // now a load-time error instead of an invisible one at runtime.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(5));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_text(1));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(5));

        let data = build_declared_fixture(&["div", "Hello"], &[], &opcodes, &[]);
        assert_eq!(
            IrModule::parse(&data).unwrap_err(),
            IrError::UnknownIslandId(5)
        );
    }

    #[test]
    fn walk_nested_island_start_before_a_root_element_is_an_error() {
        // Only one element can carry a data-forma-island set, so an island
        // that opens another island before its own root element would have
        // silently lost its identity — never hydrated, never disposed.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_island_start(1));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(1));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let data = build_declared_fixture(
            &["div", "Outer", "Inner"],
            &[],
            &opcodes,
            &[(0, 0x01, 0x01, 1, 0, &[]), (1, 0x01, 0x01, 2, 3, &[])],
        );
        let module = IrModule::parse(&data).unwrap();
        assert_eq!(
            walk_to_html(&module, &slots).unwrap_err(),
            IrError::NestedIslandRoot { outer: 0, inner: 1 }
        );
    }

    #[test]
    fn walk_island_nested_inside_another_islands_root_is_fine() {
        // The legal shape: each island opens its own element first.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_island_start(1));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(1));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let inner_offset = (encode_island_start(0).len() + encode_open_tag(0, &[]).len()) as u32;
        let html = walk_with_islands(
            &["div", "Outer", "Inner"],
            &[],
            &[
                (0, 0x01, 0x01, 1, 0, &[]),
                (1, 0x01, 0x01, 2, inner_offset, &[]),
            ],
            &opcodes,
            &slots,
        );
        assert!(html.contains(r#"data-forma-component="Outer""#));
        assert!(html.contains(r#"data-forma-component="Inner""#));
    }

    // -- Empty declared props still have an occurrence ---------------------

    #[test]
    fn empty_declared_inputs_do_not_emit_inline_props() {
        // An empty declaration carries identity but no inferred slot props.
        // strings: 0="div", 1="Simple"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["div", "Simple"],
            &[],
            &[(0, 0x01, 0x01, 1, 0, &[])], // no slot_ids
            &opcodes,
            &slots,
        );
        assert!(
            !html.contains("data-forma-props"),
            "empty slot_ids should not emit props, got: {html}"
        );
        assert!(
            html.contains(r#"data-forma-island="0""#),
            "should contain island id"
        );
    }

    // -- State always has one channel, independent of payload size -----------

    /// Build a one-island page whose single prop slot holds `payload`.
    fn walk_island_with_prop_payload(payload: &str) -> WalkOutput {
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text(payload.to_string()));
        walk_with_islands_output(
            &["div", "Big", "blob"],
            &[(0, 2, 0x01, 0x00, &[])],
            &[(0, 0x01, 0x01, 1, 0, &[0])],
            &opcodes,
            &slots,
        )
    }

    #[test]
    fn duplicate_slot_names_do_not_invent_declared_island_props() {
        // Both SERVER slots have the same lookup name. This island declares
        // no inputs, so neither slot is implicitly serialized by name.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Text("first".into()));
        slots.set(1, SlotValue::Text("second".into()));

        let output = walk_with_islands_output(
            &["div", "Comp", "dup"],
            &[(0, 2, 0x01, 0x00, &[]), (1, 2, 0x01, 0x00, &[])],
            &[(0, 0x01, 0x01, 1, 0, &[])],
            &opcodes,
            &slots,
        );
        let json: serde_json::Value = serde_json::from_str(output.island_state.as_json()).unwrap();
        assert_eq!(json["i"][0]["p"], serde_json::json!({}));
        assert!(!output.html.contains("data-forma-props"));
    }

    #[test]
    fn small_props_use_the_owned_state_channel() {
        let output = walk_island_with_prop_payload("small");
        assert!(output.island_state.as_json().contains("\"blob\":\"small\""));
        assert!(!output.html.contains("data-forma-props"));
        assert!(!output.html.contains("__forma_islands"));
    }

    #[test]
    fn larger_props_use_the_same_owned_state_channel() {
        let payload = "x".repeat(1025);
        let output = walk_island_with_prop_payload(&payload);
        assert!(!output.html.contains("data-forma-props"));
        assert!(!output.html.contains("__forma_islands"));
        assert!(output.island_state.as_json().contains(&payload));
    }

    #[test]
    fn diagnostic_fragment_does_not_restore_an_inline_state_channel() {
        // Diagnostic fragments cannot restore either retired props channel.
        // Supported page rendering consumes the counted walk's state separately.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let payload = "x".repeat(1024 + 1);
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text(payload));

        let data = build_declared_fixture(
            &["div", "Big", "blob"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[(0, 0x01, 0x01, 1, 0, &[0])],
        );
        let module = IrModule::parse(&data).unwrap();
        let fragment = walk_island(&module, &slots, 0).unwrap();
        assert!(!fragment.contains("data-forma-props"));
        assert!(!fragment.contains("__forma_islands"));
    }

    // -- Test 44: walk_island_and_list_key_combined --------------------------

    // -- Test 45: walk_multiple_islands_script_tag ---------------------------

    #[test]
    fn multiple_islands_share_one_owned_envelope() {
        // Two islands append entries to the same independently owned envelope.
        // strings: 0="div", 1="span", 2="CompA", 3="CompB", 4="msg", 5="flag"
        let mut opcodes = Vec::new();
        // Island 0
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));
        // Island 1 — its byte_offset must point at its own ISLAND_START.
        let island1_offset = opcodes.len() as u32;
        opcodes.extend_from_slice(&encode_island_start(1));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_island_end(1));

        let mut slots = SlotData::new(3);
        slots.set(0, SlotValue::Text("hello".to_string()));
        slots.set(1, SlotValue::Bool(true));
        slots.set(2, SlotValue::Text("x".repeat(1024)));

        let output = walk_with_islands_output(
            &["div", "span", "CompA", "CompB", "msg", "flag", "padding"],
            &[
                (0, 4, 0x01, 0x00, &[]), // slot 0, name="msg", Text
                (1, 5, 0x02, 0x00, &[]),
                (2, 6, 0x01, 0, &[]), // slot 1, name="flag", Bool
            ],
            &[
                (0, 0x01, 0x02, 2, 0, &[0, 2]), // island 0, ScriptTag, name="CompA", byte_offset=0, slots=[0]
                (1, 0x01, 0x02, 3, island1_offset, &[1, 2]), // island 1, ScriptTag, name="CompB", slots=[1]
            ],
            &opcodes,
            &slots,
        );
        let json: serde_json::Value = serde_json::from_str(output.island_state.as_json()).unwrap();
        assert_eq!(json["i"][0]["p"]["msg"], "hello");
        assert_eq!(json["i"][1]["p"]["flag"], true);
        assert_eq!(output.island_state.len(), 2);
        assert!(!output.html.contains("<script"));
    }

    // -- Test 46: walk_list_item_key_consumed_once --------------------------

    // -- Test 47: walk_slot_value_to_json -----------------------------------

    #[test]
    fn slot_value_to_json_all_types() {
        // Verify SlotValue::to_json() for all variants
        assert_eq!(SlotValue::Null.to_json(), serde_json::Value::Null);
        assert_eq!(
            SlotValue::Text("hello".to_string()).to_json(),
            serde_json::Value::String("hello".to_string())
        );
        assert_eq!(
            SlotValue::Bool(true).to_json(),
            serde_json::Value::Bool(true)
        );
        assert_eq!(
            SlotValue::Bool(false).to_json(),
            serde_json::Value::Bool(false)
        );
        assert_eq!(SlotValue::Number(42.0).to_json(), serde_json::json!(42));
        assert_eq!(SlotValue::Number(3.15).to_json(), serde_json::json!(3.15));
        assert_eq!(
            SlotValue::array(vec![
                SlotValue::Text("a".to_string()),
                SlotValue::Number(1.0)
            ])
            .to_json(),
            serde_json::json!(["a", 1])
        );
        assert_eq!(
            SlotValue::object(vec![(
                "key".to_string(),
                SlotValue::Text("val".to_string())
            ),])
            .to_json(),
            serde_json::json!({"key": "val"})
        );
    }

    // -- Test 48: dyn_attr_emits_attribute_with_value ----------------------

    #[test]
    fn dyn_attr_emits_attribute_with_value() {
        // OPEN_TAG("input") + DYN_ATTR("type", slot=0) + CLOSE_TAG("input")
        // slot 0 = Text("password") → <input type="password"></input>
        // strings: 0="input", 1="type", 2="input_type" (slot name)
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("password".to_string()));

        let html = walk_with_slots(
            &["input", "type", "input_type"],
            &[(0, 2, 0x01, 0x01, b"password")], // slot_id=0, name_str_idx=2, Text, Client, default="password"
            &opcodes,
            &slots,
        );
        assert_eq!(html, r#"<input type="password"></input>"#);
    }

    // -- Test 49: dyn_attr_skips_when_slot_null ----------------------------

    #[test]
    fn dyn_attr_skips_when_slot_null() {
        // DYN_ATTR with Null slot value should emit no attribute at all.
        // <div> (no class attr) </div>
        // strings: 0="div", 1="class", 2="cls" (slot name)
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let slots = SlotData::new(1); // slot 0 is Null by default

        let html = walk_with_slots(
            &["div", "class", "cls"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert_eq!(html, "<div></div>");
    }

    // -- Test 50: dyn_attr_multiple_on_same_element ------------------------

    #[test]
    fn dyn_attr_multiple_on_same_element() {
        // OPEN_TAG("input") + DYN_ATTR("type", slot=0) + DYN_ATTR("class", slot=1) + CLOSE_TAG
        // strings: 0="input", 1="type", 2="class", 3="slot_type", 4="slot_class"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        opcodes.extend_from_slice(&encode_dyn_attr(2, 1));
        opcodes.extend_from_slice(&encode_close_tag(0));

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Text("email".to_string()));
        slots.set(1, SlotValue::Text("form-input".to_string()));

        let html = walk_with_slots(
            &["input", "type", "class", "slot_type", "slot_class"],
            &[(0, 3, 0x01, 0x01, b"text"), (1, 4, 0x01, 0x01, b"")],
            &opcodes,
            &slots,
        );
        assert_eq!(html, r#"<input type="email" class="form-input"></input>"#);
    }

    // -- Test 51: dyn_attr_on_void_tag -------------------------------------

    #[test]
    fn dyn_attr_on_void_tag() {
        // VOID_TAG("input") + DYN_ATTR("type", slot=0)
        // slot 0 = Text("checkbox") → <input type="checkbox">
        // strings: 0="input", 1="type", 2="slot_type"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_void_tag(0, &[]));
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("checkbox".to_string()));

        let html = walk_with_slots(
            &["input", "type", "slot_type"],
            &[(0, 2, 0x01, 0x01, b"text")],
            &opcodes,
            &slots,
        );
        assert_eq!(html, r#"<input type="checkbox">"#);
    }

    // -- walk_island tests -------------------------------------------------

    #[test]
    fn walk_island_basic() {
        // Build IR with an island containing <p>hello</p>
        // strings: 0="MyIsland", 1="div", 2="p", 3="hello"
        // We need a wrapper div before the island, the island content, and wrapper close.
        //
        // Opcodes layout:
        // OPEN_TAG "div" []       -> offset 0
        // ISLAND_START id=0       -> offset X (byte_offset for island table)
        // OPEN_TAG "p" []
        // TEXT "hello"
        // CLOSE_TAG "p"
        // ISLAND_END id=0
        // CLOSE_TAG "div"

        let pre_island = encode_open_tag(1, &[]); // OPEN_TAG "div" -> str_idx=1
        let island_start = encode_island_start(0);
        let open_p = encode_open_tag(2, &[]); // OPEN_TAG "p" -> str_idx=2
        let text_hello = encode_text(3);
        let close_p = encode_close_tag(2);
        let island_end = encode_island_end(0);
        let close_div = encode_close_tag(1);

        let byte_offset = pre_island.len() as u32; // island starts after the div open

        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&pre_island);
        opcodes.extend_from_slice(&island_start);
        opcodes.extend_from_slice(&open_p);
        opcodes.extend_from_slice(&text_hello);
        opcodes.extend_from_slice(&close_p);
        opcodes.extend_from_slice(&island_end);
        opcodes.extend_from_slice(&close_div);

        let data = build_declared_fixture(
            &["MyIsland", "div", "p", "hello"],
            &[],
            &opcodes,
            &[(0, 0x01, 0x01, 0, byte_offset, &[])], // island 0, Load, Inline, name="MyIsland"
        );
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);

        let html = walk_island(&module, &slots, 0).unwrap();
        // The fragment is the island ROOT carrying the full data-forma-* set —
        // seeking past ISLAND_START used to drop all of it, so a fragment
        // swapped in via outerHTML never hydrated.
        assert_eq!(
            html,
            "<p data-forma-island=\"0\" data-forma-component=\"MyIsland\" \
             data-forma-status=\"pending\" data-forma-hydrate=\"load\" data-forma-state=\"0\" data-forma-h=\"0000000000000000\">hello</p>"
        );
        // The f:iN markers live outside the root and survive an outerHTML
        // swap, so the fragment must not re-emit them.
        assert!(!html.contains("<!--f:i0-->"));
        assert!(!html.contains("<!--/f:i0-->"));
    }

    #[test]
    fn walk_island_attrs_match_walk_to_html() {
        // Parity: the fragment's root must carry the same data-forma-*
        // attribute set that the full-page walk produces for the same island.
        let island_start = encode_island_start(0);
        let open_div = encode_open_tag(1, &[]);
        let dyn_text = encode_dyn_text(0, 0);
        let close_div = encode_close_tag(1);
        let island_end = encode_island_end(0);

        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&island_start);
        opcodes.extend_from_slice(&open_div);
        opcodes.extend_from_slice(&dyn_text);
        opcodes.extend_from_slice(&close_div);
        opcodes.extend_from_slice(&island_end);

        let data = build_declared_fixture(
            &["IslandComp", "div", "greeting"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[(0, 0x02, 0x01, 0, 0, &[0])], // trigger=Visible, Inline props
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("World".to_string()));

        let page = walk_to_html(&module, &slots).unwrap();
        let fragment = walk_island(&module, &slots, 0).unwrap();

        for attr in [
            "data-forma-island=\"0\"",
            "data-forma-component=\"IslandComp\"",
            "data-forma-status=\"pending\"",
            "data-forma-hydrate=\"visible\"",
            "data-forma-state=\"0\"",
            "data-forma-h=\"0000000000000000\"",
        ] {
            assert!(page.contains(attr), "page missing {attr}: {page}");
            assert!(
                fragment.contains(attr),
                "fragment missing {attr}: {fragment}"
            );
        }
    }

    #[test]
    fn obsolete_mode_cannot_restore_fragment_inline_props() {
        // The obsolete tuple mode is ignored by the declared fixture builder.
        // It cannot reactivate either retired body/inline props channel.
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_island_end(0));

        let data = build_declared_fixture(
            &["IslandComp", "div", "greeting"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[(0, 0x01, 0x02, 0, 0, &[0])], // props_mode = ScriptTag
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("World".to_string()));

        let fragment = walk_island(&module, &slots, 0).unwrap();
        assert!(
            !fragment.contains("data-forma-props="),
            "diagnostic fragment must not inline props: {fragment}"
        );
        assert!(
            !fragment.contains("__forma_islands"),
            "fragment must not emit a second islands block: {fragment}"
        );
    }

    #[test]
    fn nested_diagnostic_fragment_has_occurrence_markers() {
        // Nested diagnostic roots retain distinct occurrence identities.
        // Actual page state is carried separately by the counted whole walk.
        let mut opcodes = encode_island_start(0);
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        let inner_offset = opcodes.len() as u32;
        opcodes.extend_from_slice(&encode_island_start(1));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_island_end(1));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_island_end(0));

        let data = build_declared_fixture(
            &["Outer", "div", "greeting", "Inner"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[
                (0, 0x01, 0x01, 0, 0, &[]),
                (1, 0x01, 0x02, 3, inner_offset, &[0]), // nested, ScriptTag
            ],
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("World".into()));

        let fragment = walk_island(&module, &slots, 0).unwrap();
        assert!(
            fragment.contains("data-forma-component=\"Inner\""),
            "nested island must render: {fragment}"
        );
        assert!(
            fragment.contains("data-forma-state=\"1\""),
            "nested island must retain its own occurrence: {fragment}"
        );
        assert!(!fragment.contains("__forma_islands"), "{fragment}");
    }

    #[test]
    fn walk_island_not_found() {
        // Build IR with no islands, then request island_id=5
        let opcodes = encode_text(0);
        let data = build_declared_fixture(&["Hello"], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);

        let result = walk_island(&module, &slots, 5);
        assert!(result.is_err());
        match result.unwrap_err() {
            IrError::IslandNotFound(5) => {} // expected
            other => panic!("expected IslandNotFound(5), got {other:?}"),
        }
    }

    #[test]
    fn walk_island_with_dyn_text() {
        // Island containing DYN_TEXT that references a slot
        // strings: 0="IslandComp", 1="div", 2="greeting"
        // slot 0: name_str_idx=2 ("greeting"), type=Text

        let island_start = encode_island_start(0);
        let open_div = encode_open_tag(1, &[]); // str_idx=1 -> "div"
        let dyn_text = encode_dyn_text(0, 0); // slot_id=0, marker_id=0
        let close_div = encode_close_tag(1);
        let island_end = encode_island_end(0);

        let byte_offset = 0u32; // island starts at beginning of opcodes

        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&island_start);
        opcodes.extend_from_slice(&open_div);
        opcodes.extend_from_slice(&dyn_text);
        opcodes.extend_from_slice(&close_div);
        opcodes.extend_from_slice(&island_end);

        let data = build_declared_fixture(
            &["IslandComp", "div", "greeting"],
            &[(0, 2, 0x01, 0x00, &[])], // slot_id=0, name="greeting", Text, Server
            &opcodes,
            &[(0, 0x01, 0x01, 0, byte_offset, &[0])], // island 0, slot_ids=[0]
        );
        let module = IrModule::parse(&data).unwrap();

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("World".to_string()));

        let html = walk_island(&module, &slots, 0).unwrap();
        // Root element keeps its island identity AND its props.
        assert_eq!(
            html,
            "<div data-forma-island=\"0\" data-forma-component=\"IslandComp\" \
             data-forma-status=\"pending\" data-forma-hydrate=\"load\" \
             data-forma-state=\"0\" data-forma-h=\"0000000000000000\">\
             <!--f:t0-->World<!--/f:t0--></div>"
        );
    }

    #[test]
    fn walk_island_byte_offset_out_of_bounds() {
        // Island table entry with byte_offset pointing past the opcode stream.
        // This is now rejected at parse time: a byte_offset must point at that
        // island's own ISLAND_START opcode, which is both a bounds check and a
        // structural one (an in-range offset landing mid-operand on a stray
        // 0x0B byte would pass a bare bounds check).
        let opcodes = encode_text(0); // just a small opcode stream

        let data = build_declared_fixture(
            &["BadIsland", "hello"],
            &[],
            &opcodes,
            &[(0, 0x01, 0x01, 0, 9999, &[])], // byte_offset=9999, way past end
        );
        let err = IrModule::parse(&data).unwrap_err();
        assert_eq!(
            err,
            IrError::IslandOffsetMismatch {
                island_id: 0,
                byte_offset: 9999
            }
        );
    }

    // -- PROP opcode tests -------------------------------------------------

    /// Encode a PROP opcode: opcode(1) + src_slot_id(2) + prop_str_idx(4) + target_slot_id(2)
    fn encode_prop(src_slot_id: u16, prop_str_idx: u32, target_slot_id: u16) -> Vec<u8> {
        let mut buf = vec![0x12]; // Opcode::Prop
        buf.extend_from_slice(&src_slot_id.to_le_bytes());
        buf.extend_from_slice(&prop_str_idx.to_le_bytes());
        buf.extend_from_slice(&target_slot_id.to_le_bytes());
        buf
    }

    // -- Test: PROP extracts object property inside LIST body ----------------

    #[test]
    fn walk_prop_in_list_body() {
        // LIST(slot=0, item_slot=1):
        //   PROP(src=1, "name", target=2)
        //   DYN_TEXT(slot=2, marker=0)
        //
        // Slot 0 = Array([Object{name:"Alice"}, Object{name:"Bob"}])
        // Strings: 0="items", 1="item", 2="name"
        let mut body = Vec::new();
        body.extend_from_slice(&encode_prop(1, 2, 2)); // PROP(item_slot=1, prop="name", target=2)
        body.extend_from_slice(&encode_dyn_text(2, 0)); // DYN_TEXT(slot=2)
        let opcodes = encode_list(0, 1, &body);

        let mut slots = SlotData::new(3);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::object(vec![(
                    "name".to_string(),
                    SlotValue::Text("Alice".to_string()),
                )]),
                SlotValue::object(vec![(
                    "name".to_string(),
                    SlotValue::Text("Bob".to_string()),
                )]),
            ]),
        );

        let html = walk_with_slots(
            &["items", "item", "name"],
            &[
                (0, 0, 0x04, 0x00, &[]), // slot 0: Array, Server
                (1, 1, 0x05, 0x00, &[]), // slot 1: Object, Server
                (2, 2, 0x01, 0x00, &[]), // slot 2: Text, Server
            ],
            &opcodes,
            &slots,
        );
        assert!(html.contains("Alice"), "should contain Alice, got: {html}");
        assert!(html.contains("Bob"), "should contain Bob, got: {html}");
    }

    // -- Test: PROP with multiple properties ---------------------------------

    #[test]
    fn walk_prop_multiple_properties() {
        // LIST(slot=0, item_slot=1):
        //   PROP(src=1, "name", target=2)
        //   PROP(src=1, "email", target=3)
        //   <tr><td>DYN_TEXT(2)</td><td>DYN_TEXT(3)</td></tr>
        //
        // Strings: 0="rows", 1="row", 2="name", 3="email", 4="tr", 5="td"
        let mut body = Vec::new();
        body.extend_from_slice(&encode_prop(1, 2, 2)); // PROP(1, "name", 2)
        body.extend_from_slice(&encode_prop(1, 3, 3)); // PROP(1, "email", 3)
        body.extend_from_slice(&encode_open_tag(4, &[])); // <tr>
        body.extend_from_slice(&encode_open_tag(5, &[])); // <td>
        body.extend_from_slice(&encode_dyn_text(2, 0)); // name
        body.extend_from_slice(&encode_close_tag(5)); // </td>
        body.extend_from_slice(&encode_open_tag(5, &[])); // <td>
        body.extend_from_slice(&encode_dyn_text(3, 1)); // email
        body.extend_from_slice(&encode_close_tag(5)); // </td>
        body.extend_from_slice(&encode_close_tag(4)); // </tr>
        let opcodes = encode_list(0, 1, &body);

        let mut slots = SlotData::new(4);
        slots.set(
            0,
            SlotValue::array(vec![
                SlotValue::object(vec![
                    ("name".to_string(), SlotValue::Text("Alice".to_string())),
                    (
                        "email".to_string(),
                        SlotValue::Text("alice@test.com".to_string()),
                    ),
                ]),
                SlotValue::object(vec![
                    ("name".to_string(), SlotValue::Text("Bob".to_string())),
                    (
                        "email".to_string(),
                        SlotValue::Text("bob@test.com".to_string()),
                    ),
                ]),
            ]),
        );

        let html = walk_with_slots(
            &["rows", "row", "name", "email", "tr", "td"],
            &[
                (0, 0, 0x04, 0x00, &[]),
                (1, 1, 0x05, 0x00, &[]),
                (2, 2, 0x01, 0x00, &[]),
                (3, 3, 0x01, 0x00, &[]),
            ],
            &opcodes,
            &slots,
        );
        assert!(
            html.contains("<td><!--f:t0-->Alice<!--/f:t0--></td>"),
            "should contain Alice td, got: {html}"
        );
        assert!(
            html.contains("<td><!--f:t1-->alice@test.com<!--/f:t1--></td>"),
            "should contain alice email, got: {html}"
        );
        assert!(
            html.contains("<td><!--f:t0-->Bob<!--/f:t0--></td>"),
            "should contain Bob td, got: {html}"
        );
        assert!(
            html.contains("<td><!--f:t1-->bob@test.com<!--/f:t1--></td>"),
            "should contain bob email, got: {html}"
        );
    }

    // -- Test: PROP on non-Object returns Null (empty string for DYN_TEXT) ----

    #[test]
    fn walk_prop_on_non_object() {
        // PROP(src=0, "name", target=1) then DYN_TEXT(1)
        // Slot 0 = Text("hello") — not an object
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_prop(0, 1, 1));
        opcodes.extend_from_slice(&encode_dyn_text(1, 0));

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Text("hello".to_string()));

        let html = walk_with_slots(
            &["src", "name"],
            &[(0, 0, 0x01, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        // PROP on non-Object produces Null → DYN_TEXT renders zero-width space (U+200B)
        assert!(
            html.contains("<!--f:t0-->\u{200B}<!--/f:t0-->"),
            "non-object PROP should produce empty text, got: {html}"
        );
    }

    // -- Test: PROP with missing property returns Null -----------------------

    #[test]
    fn walk_prop_missing_property() {
        // PROP(src=0, "missing", target=1) then DYN_TEXT(1)
        // Slot 0 = Object with only "name" key
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_prop(0, 2, 1));
        opcodes.extend_from_slice(&encode_dyn_text(1, 0));

        let mut slots = SlotData::new(2);
        slots.set(
            0,
            SlotValue::object(vec![(
                "name".to_string(),
                SlotValue::Text("Alice".to_string()),
            )]),
        );

        let html = walk_with_slots(
            &["src", "target", "missing"],
            &[(0, 0, 0x05, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &slots,
        );
        assert!(
            html.contains("<!--f:t0-->\u{200B}<!--/f:t0-->"),
            "missing property should produce empty text, got: {html}"
        );
    }

    // -- Recursion depth limit tests ----------------------------------------

    #[test]
    fn walk_nested_show_if_within_limit() {
        // 5 levels of nested SHOW_IF — well within the 64-level limit.
        // Each level wraps the next; innermost renders TEXT "deep".
        // strings: 0..=4 = "s0".."s4", 5 = "deep"
        let inner = encode_text(5);
        let level4 = encode_show_if(4, &inner, &[]);
        let level3 = encode_show_if(3, &level4, &[]);
        let level2 = encode_show_if(2, &level3, &[]);
        let level1 = encode_show_if(1, &level2, &[]);
        let level0 = encode_show_if(0, &level1, &[]);

        let mut slots = SlotData::new(5);
        for i in 0..5 {
            slots.set(i, SlotValue::Bool(true));
        }

        let html = walk_with_slots(
            &["s0", "s1", "s2", "s3", "s4", "deep"],
            &[
                (0, 0, 0x02, 0x00, &[]),
                (1, 1, 0x02, 0x00, &[]),
                (2, 2, 0x02, 0x00, &[]),
                (3, 3, 0x02, 0x00, &[]),
                (4, 4, 0x02, 0x00, &[]),
            ],
            &level0,
            &slots,
        );
        assert!(
            html.contains("deep"),
            "5 levels of nesting should succeed, got: {html}"
        );
    }

    /// Return body HTML and the separately owned, script-safe state JSON.
    fn script_tag_props_html(payload: &str) -> (String, String) {
        // ISLAND_START(0) + OPEN_TAG("div") + CLOSE_TAG + ISLAND_END(0)
        // strings: 0="div", 1="Comp", 2="payload"
        let mut opcodes = Vec::new();
        opcodes.extend_from_slice(&encode_island_start(0));
        opcodes.extend_from_slice(&encode_open_tag(0, &[]));
        opcodes.extend_from_slice(&encode_close_tag(0));
        opcodes.extend_from_slice(&encode_island_end(0));

        let mut slots = SlotData::new(2);
        slots.set(0, SlotValue::Text(payload.to_string()));
        slots.set(1, SlotValue::Text("x".repeat(1024)));

        let output = walk_with_islands_output(
            &["div", "Comp", "payload", "padding"],
            &[(0, 2, 0x01, 0x00, &[]), (1, 3, 0x01, 0, &[])],
            &[(0, 0x01, 0x01, 1, 0, &[0, 1])],
            &opcodes,
            &slots,
        );

        (output.html, output.island_state.as_json().to_owned())
    }

    #[test]
    fn owned_state_has_no_raw_script_openers() {
        // No bare '<' survives, so the JSON cannot terminate or reopen a
        // script element. Greater-than remains ordinary value-preserving text.
        let (_html, body) = script_tag_props_html("</script>alert(1)");
        assert!(
            !body.contains('<'),
            "state envelope must contain no raw script openers, got: {body}"
        );
        assert!(body.contains("\\u003c/script>"), "got: {body}");

        // ...and the escaping is value-preserving: JSON.parse (here
        // serde_json::from_str, which decodes \u003c identically) round-trips
        // back to the original prop value.
        let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(parsed["i"][0]["p"]["payload"], "</script>alert(1)");
    }

    #[test]
    fn owned_state_escapes_existing_comment_script_regression() {
        // The payload the old "</" replace did NOT handle. `<!--` followed by
        // `<script` drives a spec-compliant HTML tokenizer into the
        // script-data-double-escaped state, where our own trailing </script>
        // is consumed as text instead of ending the element — swallowing the
        // whole rest of the page and leaving an inert page.
        let payload = "<!--<script>PAYLOAD";
        let (html, body) = script_tag_props_html(payload);

        assert!(
            !html.contains("<!--<script"),
            "must not emit the script-data-double-escape trigger, got: {html}"
        );
        assert!(!body.contains('<'), "got: {body}");

        let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(parsed["i"][0]["p"]["payload"], payload);
    }

    #[test]
    #[ignore = "requires admin/dist IR files — run in monorepo or after `npm run build`"]
    fn walk_real_benchmark_ir() {
        // Find the benchmark IR file dynamically (hash changes on rebuild)
        let dist_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../admin/dist");
        let ir_path = std::fs::read_dir(&dist_dir)
            .expect("admin/dist must exist")
            .filter_map(|e| e.ok())
            .find(|e| {
                let name = e.file_name();
                let s = name.to_string_lossy();
                s.starts_with("platform-benchmark.") && s.ends_with(".ir")
            })
            .map(|e| e.path())
            .expect("platform-benchmark.*.ir must exist in admin/dist");
        let data = std::fs::read(&ir_path).expect("benchmark IR file must exist");
        let module = IrModule::parse(&data).expect("IR must parse");

        // Walk with default slots
        let slots = SlotData::new_from_defaults(&module.slots);
        let html = walk_to_html(&module, &slots).expect("walk must succeed");

        // Verify island markers exist
        assert!(html.contains("<!--f:i0-->"), "missing island 0 start");
        assert!(html.contains("<!--/f:i0-->"), "missing island 0 end");
        assert!(
            html.contains("data-forma-island=\"0\""),
            "missing island 0 attr"
        );

        // The key assertion: island content must include FilterBar's children
        // (not just an empty shell div)
        assert!(
            html.contains("Search"),
            "FilterBar must contain 'Search' label text in SSR"
        );
        assert!(
            html.contains("filter-group"),
            "FilterBar must contain filter-group divs in SSR"
        );
        assert!(
            html.contains("Sort by"),
            "FilterBar must contain 'Sort by' label text in SSR"
        );

        // PerfPanel content
        assert!(
            html.contains("Performance"),
            "PerfPanel must contain 'Performance' heading in SSR"
        );

        // BenchmarkDataTable — SHOW_IF may pick either branch depending on slot defaults
        // Just check the structure exists
        assert!(
            html.contains("benchmark-data-table"),
            "BenchmarkDataTable root class must exist"
        );

        // The FilterBar island must be a real, closed region, not just a
        // stray opening marker. (This replaced a pair of `eprintln!`s that
        // printed the region "for manual inspection" and asserted nothing.)
        let start = html
            .find("<!--f:i0-->")
            .expect("FilterBar island start marker");
        let end = html
            .find("<!--/f:i0-->")
            .expect("FilterBar island end marker");
        assert!(end > start, "island end marker must follow its start");
    }

    // -- Output budget (F-02) -----------------------------------------------

    #[test]
    fn walk_output_limit_trips_inside_one_opcode() {
        // The case a top-of-loop check cannot catch: ONE OpenTag opcode can
        // emit hundreds of megabytes, because attr_count goes to u16::MAX and
        // every value escapes up to 5x. The budget must be enforced by the
        // append itself, not once per iteration.
        let big = "&".repeat(4096); // escapes to 5x = 20 KiB each
        let attrs: Vec<(u32, u32)> = (0..200).map(|_| (1u32, 2u32)).collect();
        let opcodes = encode_open_tag(0, &attrs);

        let data = build_declared_fixture(&["div", "data-x", &big], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);

        let limits = WalkLimits {
            max_output_bytes: 64 * 1024,
            ..WalkLimits::default()
        };
        let err = walk_to_html_with_limits(&module, &slots, limits).unwrap_err();
        assert_eq!(err, IrError::OutputLimitExceeded { limit: 64 * 1024 });

        // ...and a generous budget still renders it.
        let ok = WalkLimits {
            max_output_bytes: 64 * 1024 * 1024,
            ..WalkLimits::default()
        };
        assert!(walk_to_html_with_limits(&module, &slots, ok).is_ok());
    }

    #[test]
    fn walk_output_limit_trips_on_a_single_dyn_text_value() {
        // DynText emits a whole slot value in one iteration too.
        let opcodes = encode_dyn_text(0, 0);
        let data = build_declared_fixture(&["msg"], &[(0, 0, 0x01, 0x00, &[])], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();

        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("x".repeat(100_000)));

        let limits = WalkLimits {
            max_output_bytes: 4096,
            ..WalkLimits::default()
        };
        assert!(matches!(
            walk_to_html_with_limits(&module, &slots, limits),
            Err(IrError::OutputLimitExceeded { .. })
        ));
    }

    // -- LIST iteration budget (F-08) ---------------------------------------

    #[test]
    fn walk_list_budget_exceeded() {
        // Depth bounds nesting, not volume: two nested LISTs over the same
        // 300-element array is 90,000 body walks from a tiny module.
        let body = encode_text(2);
        let inner = encode_list(0, 1, &body);
        let opcodes = encode_list(0, 1, &inner);

        let data = build_declared_fixture(
            &["items", "item", "x"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();

        let mut slots = SlotData::new(2);
        slots.set(
            0,
            SlotValue::array((0..300).map(|_| SlotValue::Text("a".into())).collect()),
        );

        let err = walk_to_html(&module, &slots).unwrap_err();
        assert_eq!(
            err,
            IrError::ListBudgetExceeded {
                max: DEFAULT_MAX_LIST_ITERATIONS
            }
        );
        // Deliberately a DIFFERENT error from ListDepthExceeded — an operator
        // reading "nested LIST depth exceeded maximum of 4" for this case
        // would be chasing the wrong thing.
        assert_ne!(
            err,
            IrError::ListDepthExceeded {
                max: MAX_LIST_DEPTH
            }
        );
    }

    // -- Offset arithmetic and structural tiling (F-06 / F-07) --------------

    /// Corrupt a u32 length field inside an opcode buffer.
    fn patch_u32(buf: &mut [u8], at: usize, value: u32) {
        buf[at..at + 4].copy_from_slice(&value.to_le_bytes());
    }

    #[test]
    fn parse_rejects_show_if_then_len_past_the_stream() {
        // SHOW_IF: opcode(1) slot_id(2) then_len(4) else_len(4)
        let mut opcodes = encode_show_if(0, &encode_text(1), &encode_text(1));
        patch_u32(&mut opcodes, 3, 0xFFFF_FF00); // then_len
        let data =
            build_declared_fixture(&["flag", "hi"], &[(0, 0, 0x02, 0x00, &[])], &opcodes, &[]);
        assert!(matches!(
            IrModule::parse(&data),
            Err(IrError::MalformedControlFlow { .. })
        ));
    }

    #[test]
    fn parse_rejects_show_if_marker_byte_that_is_not_show_else() {
        // then_len short by one, so the byte at then_end is a TEXT opcode
        // rather than the SHOW_ELSE marker.
        let mut opcodes = encode_show_if(0, &encode_text(1), &encode_text(1));
        patch_u32(&mut opcodes, 3, 4); // then body is 5 bytes; claim 4
        let data =
            build_declared_fixture(&["flag", "hi"], &[(0, 0, 0x02, 0x00, &[])], &opcodes, &[]);
        assert!(matches!(
            IrModule::parse(&data),
            Err(IrError::MalformedControlFlow { .. })
        ));
    }

    #[test]
    fn parse_rejects_list_body_len_past_the_stream() {
        // LIST: opcode(1) slot_id(2) item_slot_id(2) body_len(4)
        let mut opcodes = encode_list(0, 1, &encode_text(2));
        patch_u32(&mut opcodes, 5, 0xFFFF_FF00);
        let data = build_declared_fixture(
            &["items", "item", "x"],
            &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        assert!(matches!(
            IrModule::parse(&data),
            Err(IrError::MalformedControlFlow { .. })
        ));
    }

    #[test]
    fn parse_accepts_zero_length_else_branch_known_residual() {
        // DOCUMENTED RESIDUAL: an under-long length whose orphaned bytes still
        // decode as a legal sibling sequence is structurally indistinguishable
        // from a legitimate module. No length check can reject it; only module
        // integrity (a CRC that the producer did not restamp) would. This test exists so the
        // case is not later mistaken for a fixed hole.
        let opcodes = encode_show_if(0, &encode_text(1), &[]);
        let data =
            build_declared_fixture(&["flag", "hi"], &[(0, 0, 0x02, 0x00, &[])], &opcodes, &[]);
        assert!(IrModule::parse(&data).is_ok());
    }

    // -- TRY/FALLBACK scoping (F-10) ----------------------------------------

    // -- Pending island / list-key scoping (F-11) ---------------------------

    #[test]
    fn walk_island_attrs_survive_an_empty_show_if_before_the_root() {
        // ISLAND_START, SHOW_IF (both branches empty), then the root element.
        // Clearing pending state at the end of every walk_range would wipe the
        // island attributes here and the island would never hydrate — the
        // reason the clear lives on ISLAND_END instead.
        let mut opcodes = encode_island_start(0);
        opcodes.extend_from_slice(&encode_show_if(0, &[], &[]));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_close_tag(1));
        opcodes.extend_from_slice(&encode_island_end(0));

        let slots = SlotData::new(1);
        let html = walk_with_islands(
            &["Comp", "div", "flag"],
            &[(0, 2, 0x02, 0x00, &[])],
            &[(0, 0x01, 0x01, 0, 0, &[])],
            &opcodes,
            &slots,
        );
        assert!(
            html.contains("<div data-forma-island=\"0\""),
            "island root must keep its attributes, got: {html}"
        );
    }

    #[test]
    fn walk_island_attrs_do_not_leak_past_the_island_end() {
        // ISLAND_START with no element inside, then an element AFTER the
        // island's closing marker. The island's identity attributes must not
        // land on it.
        let mut opcodes = encode_island_start(0);
        opcodes.extend_from_slice(&encode_island_end(0));
        opcodes.extend_from_slice(&encode_open_tag(1, &[]));
        opcodes.extend_from_slice(&encode_close_tag(1));

        let slots = SlotData::new(0);
        let html = walk_with_islands(
            &["Comp", "div"],
            &[],
            &[(0, 0x01, 0x01, 0, 0, &[])],
            &opcodes,
            &slots,
        );
        assert!(
            !html.contains("data-forma-island"),
            "island attributes must not leak onto an element outside the island, got: {html}"
        );
    }

    // -- Name validation (S13) ----------------------------------------------

    #[test]
    fn walk_rejects_hostile_tag_name() {
        // Tag names are written unescaped, so an IR-authored name is direct
        // markup injection.
        let opcodes = encode_open_tag(0, &[]);
        let data = build_declared_fixture(&["div onload=\"alert(1)\""], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);
        assert!(matches!(
            walk_to_html(&module, &slots),
            Err(IrError::InvalidName(_))
        ));
    }

    #[test]
    fn walk_rejects_hostile_close_tag_name() {
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_close_tag(1));
        let data = build_declared_fixture(&["div", "div><script"], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);
        assert!(matches!(
            walk_to_html(&module, &slots),
            Err(IrError::InvalidName(_))
        ));
    }

    #[test]
    fn walk_drops_hostile_attribute_names() {
        // Attribute names are skippable, so one bad key costs the attribute
        // rather than the whole page.
        let opcodes = encode_open_tag(0, &[(1, 2), (3, 2)]);
        let data =
            build_declared_fixture(&["div", "x\"><script", "v", "ok-attr"], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);
        let html = walk_to_html(&module, &slots).unwrap();
        assert_eq!(html, "<div ok-attr=\"v\">");
    }

    #[test]
    fn walk_drops_hostile_boolean_attribute_name() {
        // Same, via the boolean (empty-value) branch.
        let opcodes = encode_open_tag(0, &[(1, 2)]);
        let data = build_declared_fixture(&["div", "y\"><script", ""], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        let slots = SlotData::new(0);
        assert_eq!(walk_to_html(&module, &slots).unwrap(), "<div>");
    }

    #[test]
    fn walk_drops_hostile_dyn_attr_name() {
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let data = build_declared_fixture(
            &["div", "z\"><script", "val"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("x".into()));
        assert_eq!(walk_to_html(&module, &slots).unwrap(), "<div>");
    }

    // -- URL / event-handler attribute safety (S14) -------------------------

    #[test]
    fn walk_drops_javascript_url_in_dyn_attr() {
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let data = build_declared_fixture(
            &["a", "href", "url"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();

        // Also covers the normalization step: browsers strip control chars
        // before resolving the scheme, so `java\tscript:` executes.
        for payload in [
            "javascript:alert(1)",
            "JaVaScRiPt:alert(1)",
            "java\tscript:alert(1)",
            "  javascript:alert(1)",
            "vbscript:msgbox(1)",
            "data:text/html,<script>alert(1)</script>",
        ] {
            let mut slots = SlotData::new(1);
            slots.set(0, SlotValue::Text(payload.to_string()));
            let html = walk_to_html(&module, &slots).unwrap();
            // Dropped entirely, not emitted empty: href="" resolves to the
            // current page and would not match the client DOM either.
            assert_eq!(html, "<a>", "payload {payload:?} was not dropped");
        }
    }

    #[test]
    fn walk_keeps_safe_urls_and_data_images() {
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let data = build_declared_fixture(
            &["img", "src", "url"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();

        for payload in [
            "/about",
            "https://example.com/x?a=1",
            "data:image/png;base64,AAAA",
            // svg+xml is allowed here because <img> is an image context — the
            // stock CSP permits img-src data:, so a blanket data: block would
            // break legitimate inline images.
            "data:image/svg+xml,%3Csvg%3E%3C/svg%3E",
        ] {
            let mut slots = SlotData::new(1);
            slots.set(0, SlotValue::Text(payload.to_string()));
            let html = walk_to_html(&module, &slots).unwrap();
            assert!(html.contains("src="), "payload {payload:?} was dropped");
        }
    }

    #[test]
    fn walk_blocks_svg_data_url_outside_image_context() {
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let data = build_declared_fixture(
            &["a", "href", "url"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(
            0,
            SlotValue::Text("data:image/svg+xml,%3Csvg onload=alert(1)%3E".into()),
        );
        // Through a document sink the SVG is parsed as a document and its
        // onload fires, so it is blocked here even though <img> allows it.
        assert_eq!(walk_to_html(&module, &slots).unwrap(), "<a>");
    }

    #[test]
    fn walk_drops_event_handler_attributes() {
        // Static attribute path...
        let opcodes = encode_open_tag(0, &[(1, 2)]);
        let data = build_declared_fixture(&["div", "onmouseover", "alert(1)"], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        assert_eq!(
            walk_to_html(&module, &SlotData::new(0)).unwrap(),
            "<div>",
            "static on* attribute must be dropped"
        );

        // ...and the dynamic one.
        let mut opcodes = encode_open_tag(0, &[]);
        opcodes.extend_from_slice(&encode_dyn_attr(1, 0));
        let data = build_declared_fixture(
            &["div", "ONCLICK", "h"],
            &[(0, 2, 0x01, 0x00, &[])],
            &opcodes,
            &[],
        );
        let module = IrModule::parse(&data).unwrap();
        let mut slots = SlotData::new(1);
        slots.set(0, SlotValue::Text("alert(1)".into()));
        assert_eq!(walk_to_html(&module, &slots).unwrap(), "<div>");
    }

    #[test]
    fn walk_drops_javascript_url_in_static_attr() {
        // Static values come from the string table (template-author level),
        // but renderAttr applies identical rules to static props — leaving
        // them unchecked would preserve an SSR/CSR divergence.
        let opcodes = encode_open_tag(0, &[(1, 2)]);
        let data =
            build_declared_fixture(&["a", "href", "javascript:alert(1)"], &[], &opcodes, &[]);
        let module = IrModule::parse(&data).unwrap();
        assert_eq!(walk_to_html(&module, &SlotData::new(0)).unwrap(), "<a>");
    }

    // -- Number rendering parity (F-05) -------------------------------------

    // -- Parsing context: text-only parents and form controls (FORMA-002) ---
    //
    // The tokenizer reads the content of `textarea` and `title` (RCDATA) and
    // of `style` and `script` (RAWTEXT, script data) as text, so a marker
    // comment there is literal text: it becomes the control's value and what a
    // form submits before JavaScript. A bound `value` on a `select` or a
    // `textarea` is a property HTML never reads as an attribute. Each test
    // asserts exact bytes for the affected element AND for a sibling in normal
    // content, so a rule cannot pass by dropping markers everywhere (P3).

    /// A small opcode assembler with an interning string table, so these
    /// tests read as markup rather than as string indices.
    struct Page {
        strings: Vec<String>,
        ops: Vec<u8>,
        slots: Vec<(u8, SlotValue)>,
        islands: Vec<(u16, u32, u32)>,
    }

    impl Page {
        fn new() -> Self {
            Page {
                strings: Vec::new(),
                ops: Vec::new(),
                slots: Vec::new(),
                islands: Vec::new(),
            }
        }

        fn intern(&mut self, s: &str) -> u32 {
            let index = match self.strings.iter().position(|x| x == s) {
                Some(i) => i,
                None => {
                    self.strings.push(s.to_string());
                    self.strings.len() - 1
                }
            };
            u32::try_from(index).unwrap()
        }

        /// Declare the next slot holding `value`; ids are assigned in order.
        fn slot(&mut self, value: SlotValue) -> u16 {
            let type_byte = match &value {
                SlotValue::Null | SlotValue::Text(_) => 0x01,
                SlotValue::Bool(_) => 0x02,
                SlotValue::Number(_) => 0x03,
                SlotValue::Array(_) => 0x04,
                SlotValue::Object(_) => 0x05,
            };
            self.slots.push((type_byte, value));
            u16::try_from(self.slots.len() - 1).unwrap()
        }

        fn open(&mut self, tag: &str, attrs: &[(&str, &str)]) -> &mut Self {
            let tag = self.intern(tag);
            let attrs: Vec<(u32, u32)> = attrs
                .iter()
                .map(|(k, v)| (self.intern(k), self.intern(v)))
                .collect();
            self.ops.extend_from_slice(&encode_open_tag(tag, &attrs));
            self
        }

        fn close(&mut self, tag: &str) -> &mut Self {
            let tag = self.intern(tag);
            self.ops.extend_from_slice(&encode_close_tag(tag));
            self
        }

        fn text(&mut self, text: &str) -> &mut Self {
            let text = self.intern(text);
            self.ops.extend_from_slice(&encode_text(text));
            self
        }

        /// DYN_TEXT whose marker id is its slot id, so ids stay distinct.
        fn dyn_text(&mut self, slot: u16) -> &mut Self {
            self.ops.extend_from_slice(&encode_dyn_text(slot, slot));
            self
        }

        fn dyn_attr(&mut self, name: &str, slot: u16) -> &mut Self {
            let name = self.intern(name);
            self.ops.extend_from_slice(&encode_dyn_attr(name, slot));
            self
        }

        /// PROP: `target = source[name]`.
        fn prop(&mut self, source: u16, name: &str, target: u16) -> &mut Self {
            let name = self.intern(name);
            self.ops
                .extend_from_slice(&encode_prop(source, name, target));
            self
        }

        /// Assemble `f`'s opcodes on their own, sharing the string table.
        fn body(&mut self, f: impl FnOnce(&mut Self)) -> Vec<u8> {
            let outer = std::mem::take(&mut self.ops);
            f(self);
            std::mem::replace(&mut self.ops, outer)
        }

        fn show_if(
            &mut self,
            slot: u16,
            then_ops: impl FnOnce(&mut Self),
            else_ops: impl FnOnce(&mut Self),
        ) -> &mut Self {
            let then_ops = self.body(then_ops);
            let else_ops = self.body(else_ops);
            self.ops
                .extend_from_slice(&encode_show_if(slot, &then_ops, &else_ops));
            self
        }

        fn list(&mut self, slot: u16, item: u16, body: impl FnOnce(&mut Self)) -> &mut Self {
            let body = self.body(body);
            self.ops.extend_from_slice(&encode_list(slot, item, &body));
            self
        }

        /// An island at the top level (its table offset is taken here, so it
        /// must not be nested inside a `body`).
        fn island(&mut self, id: u16, body: impl FnOnce(&mut Self)) -> &mut Self {
            let name = self.intern(&format!("Island{id}"));
            let offset = u32::try_from(self.ops.len()).unwrap();
            self.islands.push((id, name, offset));
            self.ops.extend_from_slice(&encode_island_start(id));
            body(self);
            self.ops.extend_from_slice(&encode_island_end(id));
            self
        }

        fn module_and_slots(&self) -> (IrModule, SlotData) {
            let mut strings = self.strings.clone();
            let mut decls = Vec::new();
            for (id, (type_byte, _)) in self.slots.iter().enumerate() {
                strings.push(format!("slot{id}"));
                let name = u32::try_from(strings.len() - 1).unwrap();
                decls.push((u16::try_from(id).unwrap(), name, *type_byte, 0x00, &[][..]));
            }
            let islands: Vec<(u16, u8, u8, u32, u32, &[u16])> = self
                .islands
                .iter()
                .map(|&(id, name, offset)| (id, 0x01, 0x01, name, offset, &[][..]))
                .collect();
            let strings: Vec<&str> = strings.iter().map(String::as_str).collect();
            let data = build_declared_fixture(&strings, &decls, &self.ops, &islands);
            let module = IrModule::parse(&data).unwrap();
            let mut slots = SlotData::new(self.slots.len());
            for (id, (_, value)) in self.slots.iter().enumerate() {
                slots.set(u16::try_from(id).unwrap(), value.clone());
            }
            (module, slots)
        }

        fn render(&self) -> String {
            let (module, slots) = self.module_and_slots();
            walk_to_html(&module, &slots).unwrap()
        }

        fn render_island(&self, id: u16) -> String {
            let (module, slots) = self.module_and_slots();
            walk_island(&module, &slots, id).unwrap()
        }
    }

    fn text(s: &str) -> SlotValue {
        SlotValue::Text(s.to_string())
    }

    #[test]
    fn dyn_text_in_a_text_only_parent_is_written_without_markers() {
        // One table over every text-only parent (P2), plus an upper-case tag:
        // HTML tag names are case-insensitive. The value is still escaped:
        // RCDATA decodes the references back, and in RAWTEXT the escaping is
        // what stops a value from closing the element.
        for parent in ["textarea", "title", "style", "script", "TEXTAREA"] {
            let mut page = Page::new();
            let inside = page.slot(text("a<b&c"));
            let outside = page.slot(text("world"));
            page.open("div", &[])
                .open(parent, &[])
                .dyn_text(inside)
                .close(parent)
                .open("p", &[])
                .dyn_text(outside)
                .close("p")
                .close("div");
            assert_eq!(
                page.render(),
                format!(
                    "<div><{parent}>a&lt;b&amp;c</{parent}>\
                     <p><!--f:t1-->world<!--/f:t1--></p></div>"
                ),
                "inside <{parent}>"
            );
        }
    }

    #[test]
    fn dyn_text_outside_the_text_only_parents_keeps_its_markers() {
        // Negative space (P5): the rule is four exact names, not a prefix or
        // substring match, and not every element with special parsing.
        for parent in ["pre", "option", "span", "titles", "xtextarea", "noscript"] {
            let mut page = Page::new();
            let value = page.slot(text("hello"));
            page.open(parent, &[]).dyn_text(value).close(parent);
            assert_eq!(
                page.render(),
                format!("<{parent}><!--f:t0-->hello<!--/f:t0--></{parent}>"),
                "inside <{parent}>"
            );
        }
    }

    #[test]
    fn an_empty_value_in_a_text_only_parent_writes_nothing() {
        // The zero-width space exists so a marker pair encloses a Text node.
        // With no markers it would only become part of the control's value.
        let mut page = Page::new();
        let inside = page.slot(text(""));
        let outside = page.slot(text(""));
        page.open("textarea", &[])
            .dyn_text(inside)
            .close("textarea")
            .open("p", &[])
            .dyn_text(outside)
            .close("p");
        assert_eq!(
            page.render(),
            "<textarea></textarea><p><!--f:t1-->\u{200B}<!--/f:t1--></p>"
        );
    }

    #[test]
    fn a_textarea_doubles_a_leading_line_feed() {
        // The parser drops one LF straight after `<textarea>` (CR and CRLF are
        // LF by then). The marker used to shield it; without one, the first
        // line of the value would be lost before JavaScript runs.
        fn textarea(build: impl FnOnce(&mut Page)) -> String {
            let mut page = Page::new();
            page.open("textarea", &[]);
            build(&mut page);
            page.close("textarea");
            page.render()
        }
        let cases: [(&str, String, &str); 5] = [
            (
                "a dynamic value",
                textarea(|p| {
                    let v = p.slot(text("\nhello"));
                    p.dyn_text(v);
                }),
                "<textarea>\n\nhello</textarea>",
            ),
            (
                "a CRLF value",
                textarea(|p| {
                    let v = p.slot(text("\r\nhello"));
                    p.dyn_text(v);
                }),
                // Rule 1 (FORMA-182): the CRLF is written as LF, and that
                // initial LF is the one doubled.
                "<textarea>\n\nhello</textarea>",
            ),
            (
                "an empty value first: the first written byte decides",
                textarea(|p| {
                    let empty = p.slot(text(""));
                    let v = p.slot(text("\nx"));
                    p.dyn_text(empty).dyn_text(v);
                }),
                "<textarea>\n\nx</textarea>",
            ),
            (
                "an LF after other content is kept as is",
                textarea(|p| {
                    let v = p.slot(text("\nb"));
                    p.text("a").dyn_text(v);
                }),
                "<textarea>a\nb</textarea>",
            ),
            (
                "static text",
                textarea(|p| {
                    p.text("\nstatic");
                }),
                "<textarea>\n\nstatic</textarea>",
            ),
        ];
        for (case, html, expected) in cases {
            assert_eq!(html, expected, "{case}");
        }

        // The rule is the textarea's alone: the parser drops no LF in a title.
        let mut page = Page::new();
        let v = page.slot(text("\nhello"));
        page.open("title", &[]).dyn_text(v).close("title");
        assert_eq!(page.render(), "<title>\nhello</title>");
    }

    #[test]
    fn no_marker_kind_is_written_inside_a_text_only_parent() {
        // SHOW_IF still chooses its branch and LIST still repeats its body;
        // only their markers go. The same SHOW_IF in a `p` keeps them.
        let mut page = Page::new();
        let loading = page.slot(SlotValue::Bool(false));
        let name = page.slot(text("Ada"));
        let tags = page.slot(SlotValue::array(vec![text("x"), text("y")]));
        let tag = page.slot(SlotValue::Null);
        page.open("title", &[])
            .show_if(
                loading,
                |p| {
                    p.text("Loading");
                },
                |p| {
                    p.dyn_text(name);
                },
            )
            .text(" | ")
            .list(tags, tag, |p| {
                p.dyn_text(tag).text(";");
            })
            .close("title")
            .open("p", &[])
            .show_if(
                loading,
                |p| {
                    p.text("Loading");
                },
                |p| {
                    p.dyn_text(name);
                },
            )
            .close("p");
        assert_eq!(
            page.render(),
            "<title>Ada | x;y;</title>\
             <p><!--f:s0--><!--f:t1-->Ada<!--/f:t1--><!--/f:s0--></p>"
        );

        // An island marker would be text too.
        let mut page = Page::new();
        page.open("textarea", &[])
            .island(0, |p| {
                p.text("x");
            })
            .close("textarea")
            .island(1, |p| {
                p.open("div", &[]).close("div");
            });
        assert_eq!(
            page.render(),
            "<textarea>x</textarea><!--f:i1--><div data-forma-island=\"1\" \
             data-forma-component=\"Island1\" data-forma-status=\"pending\" \
             data-forma-hydrate=\"load\" data-forma-state=\"1\" data-forma-h=\"0101010101010101\"></div><!--/f:i1-->"
        );
    }

    #[test]
    fn a_textarea_value_binding_is_written_as_its_text() {
        // `<textarea value>` is a property: HTML ignores the attribute, so the
        // server shipped an empty control. The client writes
        // `value == null ? '' : String(value)`, and so does the walker.
        let cases = [
            (text("hello"), "hello"),
            (
                text("</textarea><script>x</script>"),
                "&lt;/textarea&gt;&lt;script&gt;x&lt;/script&gt;",
            ),
            (text("\nhello"), "\n\nhello"),
            (SlotValue::Null, ""),
            (SlotValue::Number(0.0), "0"),
            (SlotValue::Bool(false), "false"),
        ];
        for (value, inner) in cases {
            let mut page = Page::new();
            let v = page.slot(value.clone());
            let note = page.slot(text("note"));
            page.open("textarea", &[("name", "t")])
                .dyn_attr("value", v)
                .close("textarea")
                .open("p", &[])
                .dyn_text(note)
                .close("p");
            assert_eq!(
                page.render(),
                format!(
                    "<textarea name=\"t\">{inner}</textarea><p><!--f:t1-->note<!--/f:t1--></p>"
                ),
                "value {value:?}"
            );
        }

        // The static form, `<textarea value="hello">`, is the same property.
        let mut page = Page::new();
        page.open("textarea", &[("name", "t"), ("value", "hello")])
            .close("textarea");
        assert_eq!(page.render(), "<textarea name=\"t\">hello</textarea>");
    }

    #[test]
    fn a_textarea_value_binding_wins_over_its_children() {
        // The live control shows the bound value whatever its children say,
        // so the server writes the value and not the children.
        let mut page = Page::new();
        let v = page.slot(text("bound"));
        let child = page.slot(text("child"));
        page.open("textarea", &[])
            .dyn_attr("value", v)
            .dyn_text(child)
            .text(" default")
            .close("textarea")
            .open("p", &[])
            .dyn_text(child)
            .close("p");
        assert_eq!(
            page.render(),
            "<textarea>bound</textarea><p><!--f:t1-->child<!--/f:t1--></p>"
        );
    }

    /// `<select name="c" value={value}>` over options a, b and c, each label
    /// distinct from its value so a match on the wrong one shows.
    fn select_page(value: SlotValue) -> String {
        let mut page = Page::new();
        let v = page.slot(value);
        page.open("select", &[("name", "c")]).dyn_attr("value", v);
        for (value, label) in [("a", "Apple"), ("b", "Banana"), ("c", "Cherry")] {
            page.open("option", &[("value", value)])
                .text(label)
                .close("option");
        }
        page.close("select");
        page.render()
    }

    fn option(value: &str, label: &str, selected: bool) -> String {
        let selected = if selected { " selected" } else { "" };
        format!("<option value=\"{value}\"{selected}>{label}</option>")
    }

    #[test]
    fn a_select_value_binding_selects_the_matching_option() {
        // HTML ignores `value` on a select, so the server used to ship the
        // first option selected, and a form submitted it before JavaScript.
        for (value, chosen) in [("a", "a"), ("b", "b"), ("c", "c"), ("z", "")] {
            let expected = format!(
                "<select name=\"c\">{}{}{}</select>",
                option("a", "Apple", chosen == "a"),
                option("b", "Banana", chosen == "b"),
                option("c", "Cherry", chosen == "c"),
            );
            assert_eq!(select_page(text(value)), expected, "value {value}");
        }
    }

    #[test]
    fn a_select_value_binding_compares_as_the_client_string() {
        // `String(2)` is "2"; `null` is '' (the placeholder option).
        for (value, chosen) in [(SlotValue::Number(2.0), "2"), (SlotValue::Null, "")] {
            let mut page = Page::new();
            let v = page.slot(value.clone());
            page.open("select", &[]).dyn_attr("value", v);
            for (value, label) in [("", "Choose"), ("1", "One"), ("2", "Two")] {
                page.open("option", &[("value", value)])
                    .text(label)
                    .close("option");
            }
            page.close("select");
            // An empty static attribute is written bare (`emit_open_tag`).
            let placeholder = if chosen.is_empty() {
                "<option value selected>Choose</option>"
            } else {
                "<option value>Choose</option>"
            };
            let expected = format!(
                "<select>{placeholder}{}{}</select>",
                option("1", "One", chosen == "1"),
                option("2", "Two", chosen == "2"),
            );
            assert_eq!(page.render(), expected, "value {value:?}");
        }
    }

    #[test]
    fn a_select_value_binding_matches_an_option_without_a_value_by_its_text() {
        // An option without `value` is valued by its text, stripped and
        // collapsed: `<option> Dark \n green </option>` is "Dark green".
        let render = |value: &str| {
            let mut page = Page::new();
            let v = page.slot(text(value));
            let label = page.slot(text("Blue"));
            page.open("select", &[])
                .dyn_attr("value", v)
                .open("option", &[])
                .text("Red")
                .close("option")
                .open("option", &[])
                .text(" Dark \n  green ")
                .close("option")
                .open("option", &[])
                .dyn_text(label)
                .close("option")
                .close("select");
            page.render()
        };
        assert_eq!(
            render("Dark green"),
            "<select><option>Red</option><option selected> Dark \n  green </option>\
             <option><!--f:t1-->Blue<!--/f:t1--></option></select>"
        );
        assert_eq!(
            render("Blue"),
            "<select><option>Red</option><option> Dark \n  green </option>\
             <option selected><!--f:t1-->Blue<!--/f:t1--></option></select>"
        );
    }

    #[test]
    fn a_select_value_binding_reaches_options_rendered_by_a_list() {
        // A combination (P2): list rows, PROP, a dynamic attribute and the
        // select. Labels differ from values, so an option that lost its
        // `value` and fell back to its text would match nothing. Three rows,
        // then 45 with the match past row 32 (TESTING.md P4's depth floor).
        let short: Vec<(String, String)> = [("a", "Apple"), ("b", "Banana"), ("c", "Cherry")]
            .iter()
            .map(|(id, label)| (id.to_string(), label.to_string()))
            .collect();
        let long: Vec<(String, String)> = (0..45)
            .map(|i| (format!("r{i}"), format!("Row {i}")))
            .collect();
        let cases: [(&[(String, String)], &str, usize); 4] = [
            (&short, "a", 0),
            (&short, "c", 2),
            (&long, "r40", 40),
            (&long, "r44", 44),
        ];
        for (rows_in, value, chosen) in cases {
            let mut page = Page::new();
            let v = page.slot(text(value));
            let rows = rows_in
                .iter()
                .map(|(id, label)| {
                    SlotValue::object(vec![
                        ("id".to_string(), text(id)),
                        ("label".to_string(), text(label)),
                    ])
                })
                .collect();
            let items = page.slot(SlotValue::array(rows));
            let item = page.slot(SlotValue::Null);
            let id = page.slot(SlotValue::Null);
            let label = page.slot(SlotValue::Null);
            page.open("select", &[])
                .dyn_attr("value", v)
                .list(items, item, |p| {
                    p.prop(item, "id", id)
                        .prop(item, "label", label)
                        .open("option", &[])
                        .dyn_attr("value", id)
                        .dyn_text(label)
                        .close("option");
                })
                .close("select");
            let expected: String = rows_in
                .iter()
                .enumerate()
                .map(|(i, (id, label))| {
                    let selected = if i == chosen { " selected" } else { "" };
                    format!(
                        "<option value=\"{id}\"{selected}><!--f:t4-->{label}<!--/f:t4--></option>"
                    )
                })
                .collect();
            assert_eq!(
                page.render(),
                format!("<select><!--f:l1-->{expected}<!--/f:l1--></select>"),
                "value {value}"
            );
        }
    }

    #[test]
    fn a_select_binding_ends_with_its_select() {
        // A `datalist` option after a bound select is a suggestion, not a
        // choice: the binding must not reach it. The select has no option
        // valued "b", so its single match is still unspent when the datalist
        // opens; only ending the binding at `</select>` keeps it off.
        let mut page = Page::new();
        let v = page.slot(text("b"));
        page.open("select", &[])
            .dyn_attr("value", v)
            .open("option", &[("value", "a")])
            .text("A")
            .close("option")
            .close("select")
            .open("datalist", &[])
            .open("option", &[("value", "b")])
            .text("B")
            .close("option")
            .close("datalist");
        assert_eq!(
            page.render(),
            format!(
                "<select>{}</select><datalist>{}</datalist>",
                option("a", "A", false),
                option("b", "B", false)
            )
        );
    }

    #[test]
    fn an_options_first_value_attribute_is_its_value() {
        // `<option value="a" value="b">`: the parser keeps the first of two
        // same-named attributes, so the option's value is "a".
        for (bound, selected) in [("a", " selected"), ("b", "")] {
            let mut page = Page::new();
            let v = page.slot(text(bound));
            let second = page.slot(text("b"));
            page.open("select", &[])
                .dyn_attr("value", v)
                .open("option", &[("value", "a")])
                .dyn_attr("value", second)
                .text("Only")
                .close("option")
                .close("select");
            assert_eq!(
                page.render(),
                format!("<select><option value=\"a\" value=\"b\"{selected}>Only</option></select>"),
                "bound to {bound}"
            );
        }
    }

    #[test]
    fn multiple_counts_only_when_the_attribute_is_written() {
        // A `DYN_ATTR multiple` that is false writes no attribute, so the
        // select is single and an array value is `String(array)`, "a,c",
        // which matches none of these options. True writes it, and the
        // array then selects each member.
        for (multiple, attr, chosen) in [
            (SlotValue::Bool(false), "", false),
            (SlotValue::Bool(true), " multiple", true),
            (SlotValue::Text(String::new()), "", false),
            (SlotValue::Number(0.0), "", false),
            (SlotValue::Number(-0.0), "", false),
            (SlotValue::Text("false".into()), " multiple", true),
            (SlotValue::Number(1.0), " multiple", true),
        ] {
            let mut page = Page::new();
            let flag = page.slot(multiple.clone());
            let v = page.slot(SlotValue::array(vec![text("a"), text("c")]));
            page.open("select", &[])
                .dyn_attr("multiple", flag)
                .dyn_attr("value", v);
            for (value, label) in [("a", "Apple"), ("b", "Banana"), ("c", "Cherry")] {
                page.open("option", &[("value", value)])
                    .text(label)
                    .close("option");
            }
            page.close("select");
            assert_eq!(
                page.render(),
                format!(
                    "<select{attr}>{}{}{}</select>",
                    option("a", "Apple", chosen),
                    option("b", "Banana", false),
                    option("c", "Cherry", chosen)
                ),
                "multiple {multiple:?}"
            );
        }
    }

    #[test]
    fn a_null_member_of_a_multiple_value_compares_as_null() {
        // The client builds its set with `value.map(String)`, and
        // `String(null)` is "null", not '' as it is for a text hole.
        let mut page = Page::new();
        let v = page.slot(SlotValue::array(vec![SlotValue::Null, text("b")]));
        page.open("select", &[("multiple", "")])
            .dyn_attr("value", v);
        for (value, label) in [("null", "Null"), ("x", "Empty"), ("b", "Banana")] {
            page.open("option", &[("value", value)])
                .text(label)
                .close("option");
        }
        page.close("select");
        // The middle option's static value cannot be empty (it would be
        // written bare), so it is `x`; the empty-string case is the
        // placeholder in `a_select_value_binding_compares_as_the_client_string`.
        assert_eq!(
            page.render(),
            format!(
                "<select multiple>{}{}{}</select>",
                option("null", "Null", true),
                option("x", "Empty", false),
                option("b", "Banana", true)
            )
        );
    }

    #[test]
    fn an_option_ends_at_the_next_option_or_optgroup() {
        // `</option>` is optional: the parser ends an option at the next
        // `<option>` or `<optgroup>`, and an option valued by its text is
        // valued by the text up to there.
        let mut page = Page::new();
        let v = page.slot(text("A"));
        page.open("select", &[])
            .dyn_attr("value", v)
            .open("option", &[])
            .text("A")
            .open("option", &[])
            .text("B")
            .close("select");
        assert_eq!(
            page.render(),
            "<select><option selected>A<option>B</select>",
            "ended by <option>"
        );

        let mut page = Page::new();
        let v = page.slot(text("A"));
        page.open("select", &[])
            .dyn_attr("value", v)
            .open("option", &[])
            .text("A")
            .open("optgroup", &[("label", "g")])
            .open("option", &[("value", "x")])
            .text("X")
            .close("option")
            .close("optgroup")
            .close("select");
        assert_eq!(
            page.render(),
            "<select><option selected>A<optgroup label=\"g\">\
             <option value=\"x\">X</option></optgroup></select>",
            "ended by <optgroup>"
        );
    }

    #[test]
    fn a_single_select_selects_only_the_first_matching_option() {
        // `select.value = v` selects the first option with that value. Two
        // `selected` options would let the parser keep the last instead.
        let mut page = Page::new();
        let v = page.slot(text("b"));
        page.open("select", &[])
            .dyn_attr("value", v)
            .open("option", &[("value", "b")])
            .text("First")
            .close("option")
            .open("option", &[("value", "b")])
            .text("Second")
            .close("option")
            .close("select");
        assert_eq!(
            page.render(),
            format!(
                "<select>{}{}</select>",
                option("b", "First", true),
                option("b", "Second", false)
            )
        );
    }

    #[test]
    fn a_multiple_select_selects_every_option_in_an_array_value() {
        // The client selects each option whose value is in the array, for a
        // `multiple` select only. A single select takes `String(array)`,
        // "a,c", which matches none of these options.
        let render = |attrs: &[(&str, &str)]| {
            let mut page = Page::new();
            let v = page.slot(SlotValue::array(vec![text("a"), text("c")]));
            page.open("select", attrs).dyn_attr("value", v);
            for (value, label) in [("a", "Apple"), ("b", "Banana"), ("c", "Cherry")] {
                page.open("option", &[("value", value)])
                    .text(label)
                    .close("option");
            }
            page.close("select");
            page.render()
        };
        assert_eq!(
            render(&[("multiple", "")]),
            format!(
                "<select multiple>{}{}{}</select>",
                option("a", "Apple", true),
                option("b", "Banana", false),
                option("c", "Cherry", true)
            )
        );
        assert_eq!(
            render(&[]),
            format!(
                "<select>{}{}{}</select>",
                option("a", "Apple", false),
                option("b", "Banana", false),
                option("c", "Cherry", false)
            )
        );
    }

    #[test]
    fn a_bound_select_overrides_authored_selected_on_its_options() {
        // The binding decides, as it does on the client. An unbound select
        // keeps what the author wrote, static and dynamic.
        let render = |bound: bool| {
            let mut page = Page::new();
            let v = page.slot(text("a"));
            let on = page.slot(SlotValue::Bool(true));
            page.open("select", &[]);
            if bound {
                page.dyn_attr("value", v);
            }
            page.open("option", &[("value", "a")])
                .text("A")
                .close("option")
                .open("option", &[("value", "b"), ("selected", "")])
                .text("B")
                .close("option")
                .open("option", &[("value", "c")])
                .dyn_attr("selected", on)
                .text("C")
                .close("option")
                .close("select");
            page.render()
        };
        assert_eq!(
            render(true),
            format!(
                "<select>{}{}{}</select>",
                option("a", "A", true),
                option("b", "B", false),
                option("c", "C", false)
            )
        );
        assert_eq!(
            render(false),
            format!(
                "<select>{}{}{}</select>",
                option("a", "A", false),
                option("b", "B", true),
                option("c", "C", true)
            )
        );
    }

    #[test]
    fn a_static_select_value_attribute_selects_its_option() {
        let mut page = Page::new();
        page.open("select", &[("value", "b")])
            .open("option", &[("value", "a")])
            .text("A")
            .close("option")
            .open("option", &[("value", "b")])
            .text("B")
            .close("option")
            .close("select");
        assert_eq!(
            page.render(),
            format!(
                "<select>{}{}</select>",
                option("a", "A", false),
                option("b", "B", true)
            )
        );
    }

    #[test]
    fn the_context_rules_hold_in_an_island_fragment() {
        // The internal fragment path must write what the
        // full page writes for the same island (P2).
        let mut page = Page::new();
        let body = page.slot(text("hello"));
        let choice = page.slot(text("b"));
        page.island(0, |p| {
            p.open("form", &[])
                .open("textarea", &[])
                .dyn_text(body)
                .close("textarea")
                .open("select", &[])
                .dyn_attr("value", choice)
                .open("option", &[("value", "a")])
                .text("A")
                .close("option")
                .open("option", &[("value", "b")])
                .text("B")
                .close("option")
                .close("select")
                .close("form");
        });
        let inner = format!(
            "<textarea>hello</textarea><select>{}{}</select>",
            option("a", "A", false),
            option("b", "B", true)
        );
        let full = page.render();
        let fragment = page.render_island(0);
        for (path, html) in [("walk_to_html", &full), ("walk_island", &fragment)] {
            assert!(html.contains(&inner), "{path}: {html}");
        }
        assert!(
            full.starts_with("<!--f:i0--><form data-forma-island=\"0\""),
            "{full}"
        );
    }

    // -- Counted walk (FORMA-120) -------------------------------------------

    /// A "Branding" island: `<div>` + the tenant's name + one `<i>` per colour.
    ///
    /// Strings: 0 div, 1 Branding, 2 i, 3 name, 4 colors, 5 color.
    /// Slots: 0 name (text), 1 colors (array), 2 color (the row's item).
    fn branding_module() -> IrModule {
        let mut row = encode_open_tag(2, &[]);
        row.extend_from_slice(&encode_dyn_text(2, 1));
        row.extend_from_slice(&encode_close_tag(2));
        let mut ops = encode_island_start(0);
        ops.extend_from_slice(&encode_open_tag(0, &[]));
        ops.extend_from_slice(&encode_dyn_text(0, 0));
        ops.extend_from_slice(&encode_list(1, 2, &row));
        ops.extend_from_slice(&encode_close_tag(0));
        ops.extend_from_slice(&encode_island_end(0));
        let data = build_declared_fixture(
            &["div", "Branding", "i", "name", "colors", "color"],
            &[
                (0, 3, 0x01, 0x00, &[]),
                (1, 4, 0x04, 0x00, &[]),
                (2, 5, 0x01, 0x01, &[]),
            ],
            &ops,
            &[(0, 0x01, 0x01, 1, 0, &[])],
        );
        IrModule::parse(&data).unwrap()
    }

    fn branding(name: &str, colors: usize) -> SlotData {
        let mut slots = SlotData::new(3);
        slots.set(0, SlotValue::Text(name.to_string()));
        slots.set(
            1,
            SlotValue::array(
                (0..colors)
                    .map(|i| SlotValue::Text(format!("c{i}")))
                    .collect(),
            ),
        );
        slots
    }

    #[test]
    fn counted_walk_writes_what_walk_to_html_writes_and_counts_it() {
        let module = branding_module();
        let slots = branding("Acme", 3);
        let out = walk_to_html_counted(&module, &slots, WalkLimits::default()).unwrap();

        assert_eq!(out.html, walk_to_html(&module, &slots).unwrap());
        assert_eq!(out.counts.bytes, out.html.len());
        // div + the name's text + 3 x (i + its text) = 8 nodes; markers and
        // comments are not nodes.
        assert_eq!(out.counts.nodes, 8);
        assert_eq!(out.counts.list_iterations, 3);
    }

    #[test]
    fn counted_walk_returns_the_counts_reached_when_a_budget_stops_it() {
        let module = branding_module();

        // The list budget: 5 rows allowed, 9 asked for. The list is refused
        // before its first row (FORMA-122): the div and the name were written.
        let limits = WalkLimits {
            max_list_iterations: 5,
            ..WalkLimits::default()
        };
        let failure = walk_to_html_counted(&module, &branding("Acme", 9), limits).unwrap_err();
        assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 5 });
        assert_eq!(failure.counts.list_iterations, 0);
        assert_eq!(failure.counts.nodes, 2);
        assert_eq!(failure.component_path, vec!["Branding".to_string()]);
        assert!(failure.counts.bytes > 0);

        // The output budget, set to stop the walk part-way down the list: it
        // stops inside the limit, never past it, and says how far it got.
        let limits = WalkLimits {
            max_output_bytes: 400,
            ..WalkLimits::default()
        };
        let failure = walk_to_html_counted(&module, &branding("Acme", 50), limits).unwrap_err();
        assert_eq!(failure.error, IrError::OutputLimitExceeded { limit: 400 });
        let counts = failure.counts;
        assert!(counts.bytes <= 400 && counts.bytes > 300, "{counts:?}");
        assert!(
            counts.list_iterations > 1 && counts.list_iterations < 50,
            "{counts:?}"
        );
        assert!(
            counts.nodes >= 2 + 2 * (counts.list_iterations - 1),
            "{counts:?}"
        );
        assert_eq!(failure.component_path, vec!["Branding".to_string()]);
    }

    #[test]
    fn counted_walk_names_no_component_for_a_failure_outside_every_island() {
        // Invalid HTML names remain a render error; bad indices now fail at load.
        let mut ops = encode_open_tag(0, &[]);
        ops.extend_from_slice(&encode_open_tag(1, &[]));
        let data = build_declared_fixture(&["p", "bad name"], &[], &ops, &[]);
        let module = IrModule::parse(&data).unwrap();
        let failure =
            walk_to_html_counted(&module, &SlotData::new(0), WalkLimits::default()).unwrap_err();
        assert_eq!(failure.error, IrError::InvalidName("bad name".into()));
        assert!(failure.component_path.is_empty());
        assert_eq!(failure.counts.nodes, 1);
    }

    #[test]
    fn counted_walk_names_every_island_around_a_failure_outermost_first() {
        // Outer "Shell" > inner "Card", the failure inside "Card", and a
        // closed sibling island before it that must not appear in the path.
        // Strings: 0 div, 1 Shell, 2 Card, 3 Done, 4 x.
        let mut ops = encode_island_start(0); // Shell, at byte 0
        ops.extend_from_slice(&encode_open_tag(0, &[]));
        let done_at = ops.len() as u32;
        ops.extend_from_slice(&encode_island_start(2)); // Done
        ops.extend_from_slice(&encode_open_tag(0, &[]));
        ops.extend_from_slice(&encode_close_tag(0));
        ops.extend_from_slice(&encode_island_end(2));
        let card_at = ops.len() as u32;
        ops.extend_from_slice(&encode_island_start(1)); // Card
        ops.extend_from_slice(&encode_open_tag(0, &[]));
        ops.extend_from_slice(&encode_open_tag(4, &[])); // invalid HTML name fails here
        ops.extend_from_slice(&encode_close_tag(0));
        ops.extend_from_slice(&encode_island_end(1));
        ops.extend_from_slice(&encode_close_tag(0));
        ops.extend_from_slice(&encode_island_end(0));
        let data = build_declared_fixture(
            &["div", "Shell", "Card", "Done", "bad name"],
            &[],
            &ops,
            &[
                (0, 0x01, 0x01, 1, 0, &[]),
                (1, 0x01, 0x01, 2, card_at, &[]),
                (2, 0x01, 0x01, 3, done_at, &[]),
            ],
        );
        let module = IrModule::parse(&data).unwrap();
        let failure =
            walk_to_html_counted(&module, &SlotData::new(0), WalkLimits::default()).unwrap_err();
        assert_eq!(
            failure.component_path,
            vec!["Shell".to_string(), "Card".to_string()]
        );
    }

    #[test]
    fn the_fault_trigger_is_armed_only_by_the_counted_walk() {
        // With the trigger compiled in (this crate's tests), the uncaught
        // 0.2.1 path renders the value as text; only the counted walk, which
        // catches the panic, ever takes it.
        let module = branding_module();
        let slots = branding(crate::fault_injection::FAULT_TRIGGER, 1);
        let html = walk_to_html(&module, &slots).expect("walk_to_html never arms it");
        assert!(html.contains("forma-test-fault"), "{html}");
        let failure = walk_to_html_counted(&module, &slots, WalkLimits::default()).unwrap_err();
        assert!(matches!(failure.error, IrError::Panicked { .. }));
    }

    #[test]
    fn counted_walk_catches_a_panic_and_names_its_island() {
        // SR44's stand-in: a component that panics on one tenant's data. The
        // trigger exists only in test builds and under the internal
        // `__fault-injection` feature.
        let module = branding_module();
        let slots = branding(crate::fault_injection::FAULT_TRIGGER, 2);
        let failure = walk_to_html_counted(&module, &slots, WalkLimits::default()).unwrap_err();
        match &failure.error {
            IrError::Panicked { message } => {
                assert!(message.contains("stand-in"), "{message}")
            }
            other => panic!("expected Panicked, got {other:?}"),
        }
        assert_eq!(failure.component_path, vec!["Branding".to_string()]);
        // ...and the next walk, on the same module, renders.
        let out =
            walk_to_html_counted(&module, &branding("Acme", 2), WalkLimits::default()).unwrap();
        assert!(out.html.contains("Acme"));
    }

    // -- A list's length checked before its first row (FORMA-122, DR-13) ------

    /// `<div>` around one LIST per entry of `lists`, each over its own slot
    /// (slot 2k, item slot 2k+1) and writing `<i>` + the item per row; a list
    /// may nest an inner LIST over the item slot's array.
    fn lists_module(lists: usize, nested: bool) -> IrModule {
        let mut ops = encode_open_tag(0, &[]);
        let mut slots: Vec<(u16, u32, u8, u8, &[u8])> = Vec::new();
        for k in 0..lists as u16 {
            let (list_slot, item_slot) = (2 * k, 2 * k + 1);
            let mut row = encode_open_tag(1, &[]);
            if nested {
                // the inner list iterates the row's item, itself an array,
                // binding its rows to slot 2*lists (shared, rebound per row)
                let inner_item = 2 * lists as u16;
                let mut inner_row = encode_open_tag(1, &[]);
                inner_row.extend_from_slice(&encode_close_tag(1));
                row.extend_from_slice(&encode_list(item_slot, inner_item, &inner_row));
            }
            row.extend_from_slice(&encode_close_tag(1));
            ops.extend_from_slice(&encode_list(list_slot, item_slot, &row));
            slots.push((list_slot, 2, 0x04, 0x00, &[]));
            slots.push((item_slot, 3, if nested { 0x04 } else { 0x01 }, 0x00, &[]));
        }
        if nested {
            slots.push((2 * lists as u16, 3, 0x01, 0x00, &[]));
        }
        ops.extend_from_slice(&encode_close_tag(0));
        let data = build_declared_fixture(&["div", "i", "items", "item"], &slots, &ops, &[]);
        IrModule::parse(&data).unwrap()
    }

    fn rows(n: usize) -> SlotValue {
        SlotValue::array((0..n).map(|_| SlotValue::Text("x".into())).collect())
    }

    fn limits(max_list_iterations: usize) -> WalkLimits {
        WalkLimits {
            max_list_iterations,
            ..WalkLimits::default()
        }
    }

    #[test]
    fn a_list_longer_than_the_remaining_budget_fails_before_its_first_row() {
        // SR38: one row too many, and not one row is walked.
        let module = lists_module(1, false);
        let mut slots = SlotData::new(2);
        slots.set(0, rows(51));
        let failure = walk_to_html_counted(&module, &slots, limits(50)).unwrap_err();
        assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 50 });
        assert_eq!(failure.counts.list_iterations, 0);
        assert_eq!(failure.counts.nodes, 1, "only the wrapping div was written");

        // The negative space: exactly the budget renders, every row.
        slots.set(0, rows(50));
        let out = walk_to_html_counted(&module, &slots, limits(50)).unwrap();
        assert_eq!(out.counts.list_iterations, 50);
        assert_eq!(out.html.matches("<i>").count(), 50);
    }

    #[test]
    fn a_list_exactly_at_the_remaining_budget_still_renders() {
        // The budget is per render: a first list spends 20 of 50 rows, so a
        // second of exactly 30 renders and one of 31 fails before its first.
        let module = lists_module(2, false);
        let mut slots = SlotData::new(4);
        slots.set(0, rows(20));
        slots.set(2, rows(30));
        let out = walk_to_html_counted(&module, &slots, limits(50)).unwrap();
        assert_eq!(out.counts.list_iterations, 50);
        assert_eq!(out.html.matches("<i>").count(), 50);

        slots.set(2, rows(31));
        let failure = walk_to_html_counted(&module, &slots, limits(50)).unwrap_err();
        assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 50 });
        assert_eq!(
            failure.counts.list_iterations, 20,
            "the first list, and no more"
        );
        assert_eq!(failure.counts.nodes, 1 + 20);
    }

    #[test]
    fn a_nested_list_is_checked_against_what_its_parents_left() {
        // 3 outer rows of 20 inner rows each, under 59: the outer list fits
        // (3 <= 59); rows and inner rows spend 1 + 20 + 1 + 20 + 1 = 43; the
        // third inner list (20 > 16 left) fails at its entry. At 0.2.1 the
        // walk ran 16 of its rows first.
        let module = lists_module(1, true);
        let mut slots = SlotData::new(3);
        slots.set(0, SlotValue::array((0..3).map(|_| rows(20)).collect()));
        let failure = walk_to_html_counted(&module, &slots, limits(59)).unwrap_err();
        assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 59 });
        assert_eq!(failure.counts.list_iterations, 43);

        // In all, 3 outer and 60 inner rows: a budget of 63 renders, 62 does not.
        let out = walk_to_html_counted(&module, &slots, limits(63)).unwrap();
        assert_eq!(out.counts.list_iterations, 63);
        assert!(walk_to_html_counted(&module, &slots, limits(62)).is_err());
    }

    #[test]
    fn the_per_row_check_still_stops_a_list_whose_nested_rows_spend_the_budget() {
        // 3 outer rows of 11 inner rows under 24: every list fits on entry
        // (3 <= 24, 11 <= 23, 11 <= 11), and the inner rows spend the rest, so
        // the outer list's third row is the one refused, by the per-row check.
        let module = lists_module(1, true);
        let mut slots = SlotData::new(3);
        slots.set(0, SlotValue::array((0..3).map(|_| rows(11)).collect()));
        let failure = walk_to_html_counted(&module, &slots, limits(24)).unwrap_err();
        assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 24 });
        assert_eq!(failure.counts.list_iterations, 24);
    }

    // -- FORMA-182 rule 1: the shared normalization vectors ------------------
    //
    // One finite JSON file, read by this crate and by @getforma/core's suite
    // (packages/core/src/dom/__tests__/normalize-text.test.ts), so a vector
    // changed on one side fails the other. Every case walks the real opcode
    // stream through `walk_to_html`; nothing here calls a helper directly.
    // Each test collects every disagreeing vector before failing, so a first
    // run reports the whole gap, not its first line.
    mod normalization_vectors {
        use super::*;
        use serde_json::Value;

        const VECTORS: &str = include_str!("../tests/fixtures/parity/normalization.json");

        fn cases(section: &str) -> Vec<Value> {
            let all: Value = serde_json::from_str(VECTORS).unwrap();
            all[section].as_array().unwrap().clone()
        }

        fn field<'a>(case: &'a Value, key: &str) -> &'a str {
            case[key].as_str().unwrap()
        }

        fn children(case: &Value) -> Vec<String> {
            case["children"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap().to_string())
                .collect()
        }

        fn escape_html(text: &str) -> String {
            text.replace('&', "&amp;")
                .replace('<', "&lt;")
                .replace('>', "&gt;")
        }

        fn escape_attr(text: &str) -> String {
            escape_html(text).replace('"', "&quot;")
        }

        /// A DYN_TEXT range as the walker writes it: an empty value is held
        /// by a zero-width space so the browser creates a Text node.
        fn marker(id: u16, text: &str) -> String {
            let inner = if text.is_empty() {
                "\u{200B}".to_string()
            } else {
                escape_html(text)
            };
            format!("<!--f:t{id}-->{inner}<!--/f:t{id}-->")
        }

        /// The text a browser would show for a marker-separated output: the
        /// marker comments and the empty-range zero-width spaces removed. No
        /// `children` vector contains a zero-width space of its own.
        fn visible(rendered: &str) -> String {
            let mut out = String::new();
            let mut rest = rendered;
            while let Some(start) = rest.find("<!--") {
                out.push_str(&rest[..start]);
                let end = rest[start..].find("-->").unwrap() + start + 3;
                rest = &rest[end..];
            }
            out.push_str(rest);
            out.replace('\u{200B}', "")
        }

        fn check(failures: &mut Vec<String>, label: &str, actual: &str, expected: &str) {
            if actual != expected {
                failures.push(format!(
                    "{label}: expected {expected:?}, walker wrote {actual:?}"
                ));
            }
        }

        fn finish(failures: Vec<String>) {
            assert!(
                failures.is_empty(),
                "{} disagreeing vector(s):\n{}",
                failures.len(),
                failures.join("\n")
            );
        }

        #[test]
        fn text_and_dyn_text_write_each_value_normalized() {
            let mut failures = Vec::new();
            for case in cases("values") {
                let (id, input, expected) = (
                    field(&case, "id"),
                    field(&case, "input"),
                    field(&case, "expected"),
                );
                let mut page = Page::new();
                let slot = page.slot(text(input));
                page.open("p", &[])
                    .text(input)
                    .close("p")
                    .open("p", &[])
                    .dyn_text(slot)
                    .close("p");
                let want = format!(
                    "<p>{}</p><p>{}</p>",
                    escape_html(expected),
                    marker(slot, expected)
                );
                check(
                    &mut failures,
                    &format!("values/{id} text+dyn_text"),
                    &page.render(),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn static_and_dynamic_attributes_write_each_value_normalized() {
            let mut failures = Vec::new();
            for case in cases("values") {
                let (id, input, expected) = (
                    field(&case, "id"),
                    field(&case, "input"),
                    field(&case, "expected"),
                );
                let mut page = Page::new();
                let slot = page.slot(text(input));
                page.open("p", &[("title", input), ("id", "ok")])
                    .close("p")
                    .open("p", &[("id", "ok")])
                    .dyn_attr("title", slot)
                    .close("p");
                let e = escape_attr(expected);
                // A static empty value is written as the bare name (the
                // walker's existing byte-stable shorthand; same DOM value).
                let static_attr = if expected.is_empty() {
                    " title".to_string()
                } else {
                    format!(" title=\"{e}\"")
                };
                let want = format!("<p{static_attr} id=\"ok\"></p><p id=\"ok\" title=\"{e}\"></p>");
                check(
                    &mut failures,
                    &format!("values/{id} attr+dyn_attr"),
                    &page.render(),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn adjacent_values_normalize_each_before_composition() {
            // `['\r', '\n']` is two line feeds and `['\r\n']` is one: the rule
            // is per authored value, so a normalizer that buffered the output
            // stream would fold the first case and fail here. Three shapes of
            // the same children: adjacent TEXT, adjacent DYN_TEXT ranges, and
            // a static first value before dynamic ones.
            let mut failures = Vec::new();
            for case in cases("children") {
                let (id, expected) = (field(&case, "id"), field(&case, "expected"));
                let values = children(&case);

                let mut page = Page::new();
                page.open("p", &[]);
                for value in &values {
                    page.text(value);
                }
                page.close("p");
                let want = format!("<p>{}</p>", escape_html(expected));
                check(
                    &mut failures,
                    &format!("children/{id} adjacent TEXT"),
                    &page.render(),
                    &want,
                );

                let mut page = Page::new();
                let slots: Vec<u16> = values.iter().map(|v| page.slot(text(v))).collect();
                page.open("p", &[]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("p");
                check(
                    &mut failures,
                    &format!("children/{id} adjacent DYN_TEXT (visible text)"),
                    &visible(&page.render()),
                    &want,
                );

                let mut page = Page::new();
                let slots: Vec<u16> = values[1..].iter().map(|v| page.slot(text(v))).collect();
                page.open("p", &[]).text(&values[0]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("p");
                check(
                    &mut failures,
                    &format!("children/{id} TEXT then DYN_TEXT (visible text)"),
                    &visible(&page.render()),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn textarea_children_protect_only_an_initial_normalized_line_feed() {
            // The parser drops one LF straight after `<textarea>`, so the
            // writer doubles an initial LF. After normalization that is the
            // only initial break there is; an empty chunk does not consume
            // the boundary, and a separate `\r` then `\n` is two values.
            let mut failures = Vec::new();
            for case in cases("textarea") {
                let (id, html) = (field(&case, "id"), field(&case, "html"));
                let values = children(&case);

                let mut page = Page::new();
                page.open("textarea", &[]);
                for value in &values {
                    page.text(value);
                }
                page.close("textarea");
                check(
                    &mut failures,
                    &format!("textarea/{id} static"),
                    &page.render(),
                    html,
                );

                let mut page = Page::new();
                let slots: Vec<u16> = values.iter().map(|v| page.slot(text(v))).collect();
                page.open("textarea", &[]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("textarea");
                check(
                    &mut failures,
                    &format!("textarea/{id} dynamic"),
                    &page.render(),
                    html,
                );
            }
            finish(failures);
        }

        #[test]
        fn url_attributes_are_checked_as_the_normalized_string_that_is_written() {
            // The URL policy classifies the raw value: a NUL anywhere in a URL
            // attribute refuses the write, a CR or CRLF inside a scheme is
            // refused as before, and an admitted value is written normalized.
            // The benign sibling attribute stays in both branches (P3).
            let mut failures = Vec::new();
            for case in cases("unsafeUrlNearMiss") {
                let (id, input) = (field(&case, "id"), field(&case, "input"));
                let attribute = case["attribute"].as_str().unwrap_or("href");
                let tag = case["tag"].as_str().unwrap_or("a");
                let refused = case["refused"].as_bool().unwrap();
                let mut page = Page::new();
                let slot = page.slot(text(input));
                page.open(tag, &[(attribute, input), ("id", "ok")])
                    .close(tag)
                    .open(tag, &[("id", "ok")])
                    .dyn_attr(attribute, slot)
                    .close(tag);
                let want = if refused {
                    format!("<{tag} id=\"ok\"></{tag}><{tag} id=\"ok\"></{tag}>")
                } else {
                    let written = escape_attr(field(&case, "written"));
                    format!(
                        "<{tag} {attribute}=\"{written}\" id=\"ok\"></{tag}>\
                         <{tag} id=\"ok\" {attribute}=\"{written}\"></{tag}>"
                    )
                };
                check(
                    &mut failures,
                    &format!("unsafeUrlNearMiss/{id}"),
                    &page.render(),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn pre_children_protect_only_an_initial_normalized_line_feed() {
            // `pre` and `listing` share the textarea rule, by output position:
            // the first content token starting with a normalized LF gets one
            // more. A DYN_TEXT writes its marker comment first, which the
            // parser treats as the first token, so the dynamic shape is never
            // doubled (`htmlDynamic`).
            let mut failures = Vec::new();
            for case in cases("pre") {
                let (id, html, dynamic) = (
                    field(&case, "id"),
                    field(&case, "html"),
                    field(&case, "htmlDynamic"),
                );
                let values = children(&case);
                for tag in ["pre", "listing", "PRE"] {
                    let mut page = Page::new();
                    page.open(tag, &[]);
                    for value in &values {
                        page.text(value);
                    }
                    page.close(tag);
                    let want = html
                        .replace("<pre>", &format!("<{tag}>"))
                        .replace("</pre>", &format!("</{tag}>"));
                    check(
                        &mut failures,
                        &format!("pre/{id} static <{tag}>"),
                        &page.render(),
                        &want,
                    );
                }
                let mut page = Page::new();
                let slots: Vec<u16> = values.iter().map(|v| page.slot(text(v))).collect();
                page.open("pre", &[]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("pre");
                check(
                    &mut failures,
                    &format!("pre/{id} dynamic"),
                    &page.render(),
                    dynamic,
                );
            }
            finish(failures);
        }

        #[test]
        fn the_pre_boundary_is_consumed_by_any_token_but_not_by_a_value_only_opcode() {
            // A child element, a conditional's marker and the closing tag of
            // an inner element are tokens: the LF after them is not the first
            // content token, so it is written once. PROP writes nothing, so
            // the boundary survives it. A sibling `pre` starts its own.
            let mut page = Page::new();
            page.open("pre", &[])
                .open("b", &[])
                .close("b")
                .text("\nx")
                .close("pre");
            assert_eq!(page.render(), "<pre><b></b>\nx</pre>");

            let mut page = Page::new();
            let shown = page.slot(SlotValue::Bool(true));
            page.open("pre", &[])
                .show_if(
                    shown,
                    |p| {
                        p.text("\nx");
                    },
                    |_| {},
                )
                .close("pre");
            let rendered = page.render();
            assert!(rendered.starts_with("<pre><!--f:s"), "{rendered:?}");
            assert!(rendered.contains("-->\nx<!--/f:s"), "{rendered:?}");
            assert!(!rendered.contains("\n\n"), "{rendered:?}");

            let mut page = Page::new();
            let object = page.slot(SlotValue::object(vec![]));
            let target = page.slot(text(""));
            page.open("pre", &[])
                .prop(object, "name", target)
                .text("\nx")
                .close("pre")
                .open("pre", &[])
                .text("\ny")
                .close("pre")
                .open("p", &[])
                .text("\nz")
                .close("p");
            assert_eq!(page.render(), "<pre>\n\nx</pre><pre>\n\ny</pre><p>\nz</p>");
        }

        #[test]
        fn the_output_budget_counts_the_normalized_and_escaped_bytes_actually_written() {
            // A top-level TEXT with no closing tag, so the budget ends inside
            // the text itself: exactly-at renders, one byte under refuses. The
            // ten-byte prefix forces the per-character slow path (worst-case
            // expansion no longer fits). Before this check a three-byte U+FFFD
            // or four-byte astral character was pushed when `out.len()` was
            // merely below the limit, overshooting it by up to three bytes.
            for (input, written) in [
                ("xxxxxxxxxx\0", "xxxxxxxxxx\u{FFFD}"),
                ("xxxxxxxxxx😀", "xxxxxxxxxx😀"),
                ("xxxxxxxxxx&", "xxxxxxxxxx&amp;"),
                ("xxxxxxxxxx\r\n", "xxxxxxxxxx\n"),
            ] {
                let mut page = Page::new();
                page.text(input);
                let (module, slots) = page.module_and_slots();
                let limits = |max_output_bytes| WalkLimits {
                    max_output_bytes,
                    ..WalkLimits::default()
                };
                let exact = written.len();
                assert_eq!(
                    walk_to_html_with_limits(&module, &slots, limits(exact)).as_deref(),
                    Ok(written),
                    "{input:?} at its exact budget"
                );
                assert_eq!(
                    walk_to_html_with_limits(&module, &slots, limits(exact - 1)),
                    Err(IrError::OutputLimitExceeded { limit: exact - 1 }),
                    "{input:?} one byte under its budget"
                );
            }
        }
    }

    // FORMA-140: both languages read this same finite INV1 specification.
    mod inv1_vectors {
        use super::*;
        use serde_json::Value;

        #[test]
        fn names_and_urls_agree_for_static_and_dynamic_attributes() {
            let vectors: Value =
                serde_json::from_str(include_str!("../tests/fixtures/parity/inv1.json")).unwrap();
            let mut failures = Vec::new();
            for (section, tag, value) in [("names", "div", "value"), ("urls", "", "")] {
                for case in vectors[section].as_array().unwrap() {
                    let name = case["name"].as_str().unwrap();
                    let tag = case["tag"].as_str().unwrap_or(tag);
                    let value = case["value"].as_str().unwrap_or(value);
                    let written = case["written"].as_bool().unwrap();
                    if section == "names" {
                        assert_eq!(
                            is_valid_attr_name(name),
                            case["valid"].as_bool().unwrap(),
                            "{}",
                            case["id"]
                        );
                    }
                    let mut escaped = String::new();
                    push_escaped_attr(&mut escaped, value, usize::MAX).unwrap();
                    let attribute = if written {
                        format!(" {name}=\"{escaped}\"")
                    } else {
                        String::new()
                    };
                    let want = format!("<{tag} id=\"ok\"{attribute}></{tag}>");
                    let mut static_page = Page::new();
                    static_page
                        .open(tag, &[("id", "ok"), (name, value)])
                        .close(tag);
                    let mut dynamic_page = Page::new();
                    let slot = dynamic_page.slot(SlotValue::Text(value.to_string()));
                    dynamic_page
                        .open(tag, &[("id", "ok")])
                        .dyn_attr(name, slot)
                        .close(tag);
                    for (writer, actual) in [
                        ("static", static_page.render()),
                        ("dynamic", dynamic_page.render()),
                    ] {
                        // Static OPEN_TAG uses the bare spelling for an empty
                        // value; DYN_ATTR uses ="". Both parse to the same DOM.
                        let expected = if writer == "static" && written && value.is_empty() {
                            format!("<{tag} id=\"ok\" {name}></{tag}>")
                        } else {
                            want.clone()
                        };
                        if actual != expected {
                            failures.push(format!(
                                "{} {writer}: {actual:?} != {expected:?}",
                                case["id"]
                            ));
                        }
                    }
                }
            }
            let names: Vec<_> = vectors["urlAttributes"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap())
                .collect();
            assert_eq!(names, URL_ATTRS);
            assert!(failures.is_empty(), "{}", failures.join("\n"));
        }

        #[test]
        fn ordinary_payloads_are_escaped_in_both_output_contexts() {
            let vectors: Value =
                serde_json::from_str(include_str!("../tests/fixtures/parity/inv1.json")).unwrap();
            for payload in vectors["payloads"].as_array().unwrap() {
                let text = payload["text"].as_str().unwrap();
                let attr = payload["attribute"].as_str().unwrap();
                let payload = payload["value"].as_str().unwrap();
                let mut page = Page::new();
                page.open("p", &[("title", payload)])
                    .text(payload)
                    .close("p");
                let html = page.render();
                assert!(!html.contains("<script"));
                assert!(!html.contains("<img"));
                assert!(!html.contains("</textarea>"));
                assert_eq!(html, format!("<p title=\"{attr}\">{text}</p>"));
            }
        }
    }

    // -- FORMA-016 F5: a conditional branch that is a scalar -----------------
    //
    // The shared vectors (`show-text-branch.json`) are read here and by
    // @getforma/core's suite, so the bytes the walker writes for a branch
    // that is a DYN_TEXT and the text core's createShow writes for a branch
    // that returns a string or a number cannot drift apart.
    mod show_text_branch_vectors {
        use super::*;
        use serde_json::Value;

        const VECTORS: &str = include_str!("../tests/fixtures/parity/show-text-branch.json");

        #[test]
        fn a_dyn_text_branch_is_written_between_the_conditional_markers() {
            let all: Value = serde_json::from_str(VECTORS).unwrap();
            let mut failures = Vec::new();
            let mut walked = 0;
            for case in all["values"].as_array().unwrap() {
                if case["clientOnly"].as_bool() == Some(true) {
                    continue;
                }
                walked += 1;
                let id = case["id"].as_str().unwrap();
                let mut page = Page::new();
                let shown = page.slot(SlotValue::Bool(true));
                let value = page.slot(crate::slot::json_to_slot_value(&case["value"]));
                page.show_if(
                    shown,
                    |p| {
                        p.dyn_text(value);
                    },
                    |_| {},
                );
                let want = format!("<!--f:s0-->{}<!--/f:s0-->", case["html"].as_str().unwrap());
                let got = page.render();
                if got != want {
                    failures.push(format!("{id}: expected {want:?}, walker wrote {got:?}"));
                }
            }
            assert!(walked >= 9, "the vectors were not read");
            assert!(failures.is_empty(), "{}", failures.join("\n"));
        }
    }

    // -- FORMA-182 rules 2 to 7: the shared value-rule vectors ---------------
    //
    // One JSON file per rule under tests/fixtures/parity/, each read by this
    // crate and by @getforma/core's per-rule suites (packages/core/src/dom/
    // __tests__/value-rule-<n>-*.test.ts); the compiled shapes of rules 4, 6
    // and 7 are read by @getforma/compiler's (packages/compiler/tests/
    // value-rule-<n>-*.test.ts). Every case walks the real opcode stream.
    mod value_rule_vectors {
        use super::*;
        use serde_json::Value;

        const NUMBERS: &str = include_str!("../tests/fixtures/parity/numbers.json");
        const INTEGERS: &str = include_str!("../tests/fixtures/parity/integers.json");
        const NULL: &str = include_str!("../tests/fixtures/parity/null.json");
        const BOOLEAN_TEXT: &str = include_str!("../tests/fixtures/parity/boolean-text.json");
        const CONDITIONS: &str = include_str!("../tests/fixtures/parity/conditions.json");
        const ATTRIBUTES: &str = include_str!("../tests/fixtures/parity/attributes.json");

        fn cases(file: &str, section: &str) -> Vec<Value> {
            let all: Value = serde_json::from_str(file).unwrap();
            all[section].as_array().unwrap().clone()
        }

        fn field<'a>(case: &'a Value, key: &str) -> &'a str {
            case[key].as_str().unwrap()
        }

        /// A DYN_TEXT range as the walker writes it (an empty value is held
        /// by a zero-width space).
        fn marker(id: u16, text: &str) -> String {
            let inner = if text.is_empty() {
                "\u{200B}".to_string()
            } else {
                text.to_string()
            };
            format!("<!--f:t{id}-->{inner}<!--/f:t{id}-->")
        }

        /// The text a browser shows: marker comments and the empty-range
        /// zero-width spaces removed.
        fn visible(rendered: &str) -> String {
            let mut out = String::new();
            let mut rest = rendered;
            while let Some(start) = rest.find("<!--") {
                out.push_str(&rest[..start]);
                let end = rest[start..].find("-->").unwrap() + start + 3;
                rest = &rest[end..];
            }
            out.push_str(rest);
            out.replace('\u{200B}', "")
        }

        fn check(failures: &mut Vec<String>, label: &str, actual: &str, expected: &str) {
            if actual != expected {
                failures.push(format!(
                    "{label}: expected {expected:?}, walker wrote {actual:?}"
                ));
            }
        }

        fn finish(failures: Vec<String>) {
            assert!(
                failures.is_empty(),
                "{} disagreeing vector(s):\n{}",
                failures.len(),
                failures.join("\n")
            );
        }

        /// What a vector's JSON value reads as on the page: null is empty,
        /// a string itself, a number or boolean its JSON spelling.
        fn vector_text(value: &Value) -> String {
            match value {
                Value::Null => String::new(),
                Value::String(s) => s.clone(),
                other => other.to_string(),
            }
        }

        /// A slot value from a vector's JSON value: a string, a number, a
        /// boolean or null (the shapes the vectors use).
        fn slot_value(value: &Value) -> SlotValue {
            match value {
                Value::Null => SlotValue::Null,
                Value::Bool(b) => SlotValue::Bool(*b),
                Value::Number(n) => SlotValue::Number(n.as_f64().unwrap()),
                Value::String(s) => SlotValue::Text(s.clone()),
                other => panic!("unsupported vector value {other}"),
            }
        }

        #[test]
        fn rule_2_numbers_are_written_in_ecmascript_formatting_as_text_and_attribute() {
            let mut failures = Vec::new();
            for case in cases(NUMBERS, "values") {
                let (id, lexeme, expected) = (
                    field(&case, "id"),
                    field(&case, "lexeme"),
                    field(&case, "expected"),
                );
                let number: f64 = lexeme.parse().unwrap();
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Number(number));
                page.open("p", &[]).dyn_text(slot).close("p");
                page.open("span", &[("id", "ok")])
                    .dyn_attr("title", slot)
                    .close("span");
                let want = format!(
                    "<p>{}</p><span id=\"ok\" title=\"{expected}\"></span>",
                    marker(slot, expected)
                );
                check(
                    &mut failures,
                    &format!("numbers/{id}"),
                    &page.render(),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn rule_3_an_admitted_exact_integer_is_written_as_its_decimal_digits() {
            // The admission itself is tests/page_props.rs (encode_props on
            // the integer origin). Here: an admitted value's text is exact,
            // and a float origin formats by rule 2 whatever its magnitude.
            let mut failures = Vec::new();
            for case in cases(INTEGERS, "exact") {
                if !case["admitted"].as_bool().unwrap() {
                    continue;
                }
                let (id, lexeme) = (field(&case, "id"), field(&case, "lexeme"));
                let number: f64 = lexeme.parse().unwrap();
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Number(number));
                page.open("p", &[]).dyn_text(slot).close("p");
                let want = format!("<p>{}</p>", marker(slot, lexeme));
                check(
                    &mut failures,
                    &format!("integers/exact/{id}"),
                    &page.render(),
                    &want,
                );
            }
            for case in cases(INTEGERS, "floatOrigin") {
                let (id, lexeme, expected) = (
                    field(&case, "id"),
                    field(&case, "lexeme"),
                    field(&case, "expected"),
                );
                let number: f64 = lexeme.parse().unwrap();
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Number(number));
                page.open("p", &[]).dyn_text(slot).close("p");
                let want = format!("<p>{}</p>", marker(slot, expected));
                check(
                    &mut failures,
                    &format!("integers/floatOrigin/{id}"),
                    &page.render(),
                    &want,
                );
            }
            finish(failures);
        }

        #[test]
        fn rule_5_null_writes_nothing_as_text_attribute_and_textarea_child() {
            let mut failures = Vec::new();
            for case in cases(NULL, "children") {
                let (id, expected) = (field(&case, "id"), field(&case, "expected"));
                let values = case["children"].as_array().unwrap();

                // Dynamic children: each value in its own range; a Null range
                // is held by the zero-width space and shows nothing.
                let mut page = Page::new();
                let slots: Vec<u16> = values.iter().map(|v| page.slot(slot_value(v))).collect();
                page.open("p", &[]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("p");
                let rendered = page.render();
                check(
                    &mut failures,
                    &format!("null/children/{id} dynamic (visible text)"),
                    &visible(&rendered),
                    &format!("<p>{expected}</p>"),
                );
                // The exact ranges, which the three-engine replay
                // (packages/core/e2e/value-rules.spec.ts) serves: a Null range
                // holds only the zero-width space.
                let ranges: String = slots
                    .iter()
                    .zip(values)
                    .map(|(slot, value)| marker(*slot, &vector_text(value)))
                    .collect();
                check(
                    &mut failures,
                    &format!("null/children/{id} dynamic"),
                    &rendered,
                    &format!("<p>{ranges}</p>"),
                );
                if rendered.contains("null") {
                    failures.push(format!(
                        "null/children/{id}: the word null was written: {rendered:?}"
                    ));
                }

                // Inside a textarea the same children compose to the value.
                let mut page = Page::new();
                let slots: Vec<u16> = values.iter().map(|v| page.slot(slot_value(v))).collect();
                page.open("textarea", &[]);
                for slot in &slots {
                    page.dyn_text(*slot);
                }
                page.close("textarea");
                check(
                    &mut failures,
                    &format!("null/children/{id} textarea"),
                    &page.render(),
                    &format!("<textarea>{expected}</textarea>"),
                );
            }
            for case in cases(NULL, "attribute") {
                let (id, attribute) = (field(&case, "id"), field(&case, "attribute"));
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Null);
                page.open("span", &[("id", "ok")])
                    .dyn_attr(attribute, slot)
                    .close("span");
                check(
                    &mut failures,
                    &format!("null/attribute/{id}"),
                    &page.render(),
                    "<span id=\"ok\"></span>",
                );
            }
            finish(failures);
        }

        #[test]
        fn rule_6_the_string_near_miss_writes_the_boolean_word_and_the_idiom_its_range() {
            // The build error itself is the compiler's (BUILD-614). What the
            // walker writes for the admitted `String(flag)` is DYN_TEXT on the
            // boolean slot. An attribute bound to a boolean follows rule 6's
            // attribute table, read by rule_6_attribute_values_follow_one_table.
            let mut failures = Vec::new();
            for case in cases(BOOLEAN_TEXT, "nearMissText") {
                let (id, expected) = (field(&case, "id"), field(&case, "expected"));
                let value = case["value"].as_bool().unwrap();
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Bool(value));
                page.open("p", &[]).dyn_text(slot).close("p");
                check(
                    &mut failures,
                    &format!("boolean-text/nearMissText/{id}"),
                    &page.render(),
                    &format!("<p>{}</p>", marker(slot, expected)),
                );
            }
            // The mixed idiom `() => ready() && 'Go'`: the compiler gives its
            // Text slot no default at false and the string at true.
            for case in cases(BOOLEAN_TEXT, "idiom") {
                let (id, expected) = (field(&case, "id"), field(&case, "expected"));
                let mut page = Page::new();
                let slot = page.slot(if expected.is_empty() {
                    SlotValue::Null
                } else {
                    SlotValue::Text(expected.to_string())
                });
                page.open("p", &[]).dyn_text(slot).close("p");
                check(
                    &mut failures,
                    &format!("boolean-text/idiom/{id}"),
                    &page.render(),
                    &format!("<p>{}</p>", marker(slot, expected)),
                );
            }
            finish(failures);
        }

        #[test]
        fn rule_6_attribute_values_follow_one_table() {
            // Findings F2 and F3: presence only for the HTML boolean set; any
            // other attribute is the value's string, `true` the word and `""`
            // a value; false and Null omit it.
            let mut failures = Vec::new();
            for case in cases(ATTRIBUTES, "values") {
                let (id, attribute, html) = (
                    field(&case, "id"),
                    field(&case, "attribute"),
                    field(&case, "html"),
                );
                let mut page = Page::new();
                let slot = page.slot(slot_value(&case["value"]));
                page.open("input", &[("id", "ok")])
                    .dyn_attr(attribute, slot)
                    .close("input");
                check(
                    &mut failures,
                    &format!("attributes/values/{id}"),
                    &page.render(),
                    &format!("<input id=\"ok\"{html}></input>"),
                );
            }
            // The walker's boolean set is the vector's: each member is bare for
            // true, and a name beside each (its `data-` twin) is the word.
            for name in cases(ATTRIBUTES, "booleanAttributes") {
                let name = name.as_str().unwrap();
                for (attribute, want) in [
                    (name.to_string(), format!(" {name}")),
                    (format!("data-{name}"), format!(" data-{name}=\"true\"")),
                ] {
                    let mut page = Page::new();
                    let slot = page.slot(SlotValue::Bool(true));
                    page.open("input", &[("id", "ok")])
                        .dyn_attr(&attribute, slot)
                        .close("input");
                    check(
                        &mut failures,
                        &format!("attributes/booleanAttributes/{attribute}"),
                        &page.render(),
                        &format!("<input id=\"ok\"{want}></input>"),
                    );
                }
            }
            finish(failures);
        }

        #[test]
        fn rule_7_a_boolean_condition_selects_the_branch() {
            // The type refusal itself is the compiler's (BUILD-615); a
            // compiled SHOW_IF reads a Bool slot, and its range is what core's
            // suite and the three-engine replay adopt.
            let mut failures = Vec::new();
            for case in cases(CONDITIONS, "walker") {
                let (id, branch) = (field(&case, "id"), field(&case, "branch"));
                let value = case["value"].as_bool().unwrap();
                let mut page = Page::new();
                let slot = page.slot(SlotValue::Bool(value));
                page.open("div", &[])
                    .show_if(
                        slot,
                        |p| {
                            p.open("b", &[]).text("then").close("b");
                        },
                        |p| {
                            p.open("i", &[]).text("else").close("i");
                        },
                    )
                    .close("div");
                let inner = if branch == "then" {
                    "<b>then</b>"
                } else {
                    "<i>else</i>"
                };
                check(
                    &mut failures,
                    &format!("conditions/walker/{id}"),
                    &page.render(),
                    &format!("<div><!--f:s{slot}-->{inner}<!--/f:s{slot}--></div>"),
                );
            }
            finish(failures);
        }
    }

    /// FORMA-181 checkpoint B: keyed rows. Hand-built modules with a real FMIR section
    /// `0x0006`, so the walker's own refusals (RENDER-503) are reached without the
    /// compiler's source proof in front of them, and the unkeyed low-level form is pinned
    /// beside each keyed case.
    mod keyed_rows {
        use super::*;
        use crate::format::{ListKeyReason, ListKeyViolation};
        use crate::parser::test_helpers::with_key_paths;

        const STRINGS: &[&str] = &[
            "rows",           // 0
            "row",            // 1
            "li",             // 2
            "id",             // 3
            "span",           // 4
            "label",          // 5
            "input",          // 6
            "type",           // 7
            "checkbox",       // 8
            "data-forma-key", // 9
            "authored",       // 10
            "tags",           // 11
            "b",              // 12
            "Child",          // 13
            "ul",             // 14
            "DATA-FORMA-KEY", // 15
            "text",           // 16
            "user",           // 17
            "tag",            // 18
        ];
        const LI: u32 = 2;
        const ID: u32 = 3;
        const SPAN: u32 = 4;
        const INPUT: u32 = 6;
        const TYPE: u32 = 7;
        const CHECKBOX: u32 = 8;
        const KEY_ATTR: u32 = 9;
        const AUTHORED: u32 = 10;
        const TAGS: u32 = 11;
        const B: u32 = 12;
        const CHILD: u32 = 13;
        const UL: u32 = 14;
        const KEY_ATTR_UPPER: u32 = 15;
        const TEXT: u32 = 16;
        const USER: u32 = 17;
        /// rows (0), row (1), tags (2, read from the row), tag (3), label (4). The row
        /// slots are client-sourced so the declared-fixture builder leaves them out of the
        /// page schema it writes for the island cases (it encodes scalars only).
        const SLOTS: &[(u16, u32, u8, u8, &[u8])] = &[
            (0, 0, 0x04, 0x00, &[]),
            (1, 1, 0x05, 0x01, &[]),
            (2, 11, 0x04, 0x01, &[]),
            (3, 18, 0x05, 0x01, &[]),
            (4, 5, 0x01, 0x00, &[]),
        ];
        /// A LIST's header: opcode, array slot, item slot, body length.
        const LIST_HEADER: usize = 9;

        fn keyed(opcodes: &[u8], paths: &[(u32, &[u32])]) -> IrModule {
            let base = build_declared_fixture(STRINGS, SLOTS, opcodes, &[]);
            IrModule::parse(&with_key_paths(&base, paths)).unwrap()
        }

        /// With an island table the module's typed flow is checked, and the fixture
        /// builder declares `rows` as `string[]`, so the row slot is a string slot. The
        /// walker binds whatever rows the data holds; the object rows below carry the key.
        fn keyed_with_islands(
            opcodes: &[u8],
            paths: &[(u32, &[u32])],
            islands: &[(u16, u8, u8, u32, u32, &[u16])],
        ) -> IrModule {
            const ISLAND_SLOTS: &[(u16, u32, u8, u8, &[u8])] =
                &[(0, 0, 0x04, 0x00, &[]), (1, 1, 0x01, 0x01, &[])];
            let base = build_declared_fixture(STRINGS, ISLAND_SLOTS, opcodes, islands);
            IrModule::parse(&with_key_paths(&base, paths)).unwrap()
        }

        fn unkeyed(opcodes: &[u8]) -> IrModule {
            IrModule::parse(&build_declared_fixture(STRINGS, SLOTS, opcodes, &[])).unwrap()
        }

        fn rows(values: Vec<SlotValue>) -> SlotData {
            let mut slots = SlotData::new(5);
            slots.set(0, SlotValue::array(values));
            slots.set(4, SlotValue::Text("x".into()));
            slots
        }

        fn object(pairs: Vec<(&str, SlotValue)>) -> SlotValue {
            SlotValue::object(pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
        }

        fn id(value: SlotValue) -> SlotValue {
            object(vec![("id", value), ("name", text("secret"))])
        }

        fn text(value: &str) -> SlotValue {
            SlotValue::Text(value.into())
        }

        fn num(value: f64) -> SlotValue {
            SlotValue::Number(value)
        }

        fn refused(module: &IrModule, slots: &SlotData) -> ListKeyViolation {
            match walk_to_html(module, slots) {
                Err(IrError::InvalidListKey(violation)) => {
                    let message = IrError::InvalidListKey(violation.clone()).to_string();
                    assert!(message.starts_with("RENDER-503: LIST at bytecode offset "));
                    // A diagnostic names positions and the path, never row data.
                    assert!(!message.contains("secret"), "{message}");
                    *violation
                }
                other => panic!("expected RENDER-503, got {other:?}"),
            }
        }

        fn violation(
            list_offset: u32,
            row: usize,
            path: &str,
            reason: ListKeyReason,
        ) -> ListKeyViolation {
            ListKeyViolation {
                list_offset,
                row,
                path: path.into(),
                reason,
            }
        }

        /// `<li><span></span></li>`: the key belongs on the `li`.
        fn li_body() -> Vec<u8> {
            [
                encode_open_tag(LI, &[]),
                encode_open_tag(SPAN, &[]),
                encode_close_tag(SPAN),
                encode_close_tag(LI),
            ]
            .concat()
        }

        #[test]
        fn a_keyed_list_writes_each_rows_canonical_key_once_on_its_root() {
            let ops = encode_list(0, 1, &li_body());
            let data = rows(vec![
                id(num(1.0)),
                id(text("a&\"<b>'")),
                id(num(-0.0)),
                id(text("x\r\ny\0")),
                id(num(-9_007_199_254_740_991.0)),
                id(text("")),
            ]);
            let html = walk_to_html(&keyed(&ops, &[(0, &[ID])]), &data).unwrap();
            assert_eq!(
                html,
                concat!(
                    "<!--f:l0-->",
                    "<li data-forma-key=\"1\"><span></span></li>",
                    "<li data-forma-key=\"a&amp;&quot;&lt;b&gt;'\"><span></span></li>",
                    "<li data-forma-key=\"0\"><span></span></li>",
                    "<li data-forma-key=\"x\ny\u{FFFD}\"><span></span></li>",
                    "<li data-forma-key=\"-9007199254740991\"><span></span></li>",
                    "<li data-forma-key=\"\"><span></span></li>",
                    "<!--/f:l0-->"
                )
            );
        }

        #[test]
        fn an_unkeyed_list_renders_byte_identically_with_no_key_and_no_key_rules() {
            let ops = encode_list(0, 1, &li_body());
            let data = rows(vec![id(num(1.0)), id(text("a"))]);
            let keyed_out =
                walk_to_html_counted(&keyed(&ops, &[(0, &[ID])]), &data, WalkLimits::default())
                    .unwrap();
            let plain = walk_to_html_counted(&unkeyed(&ops), &data, WalkLimits::default()).unwrap();
            assert_eq!(
                plain.html,
                "<!--f:l0--><li><span></span></li><li><span></span></li><!--/f:l0-->"
            );
            assert_eq!(
                keyed_out
                    .html
                    .replace(" data-forma-key=\"1\"", "")
                    .replace(" data-forma-key=\"a\"", ""),
                plain.html
            );
            assert_eq!(
                keyed_out.counts.list_iterations,
                plain.counts.list_iterations
            );
            assert_eq!(keyed_out.counts.nodes, plain.counts.nodes);
            // Without section 6 none of the key rules apply: scalar, missing and repeated
            // values, several roots, text beside a root and an authored attribute all render
            // as the low-level reader always did.
            let anything = rows(vec![text("a"), text("a"), SlotValue::Null, object(vec![])]);
            assert_eq!(
                walk_to_html(&unkeyed(&ops), &anything).unwrap(),
                format!(
                    "<!--f:l0-->{}<!--/f:l0-->",
                    "<li><span></span></li>".repeat(4)
                )
            );
            let loose = [
                encode_open_tag(LI, &[(KEY_ATTR, AUTHORED)]),
                encode_close_tag(LI),
                encode_text(TEXT),
                encode_void_tag(INPUT, &[]),
            ]
            .concat();
            assert_eq!(
                walk_to_html(&unkeyed(&encode_list(0, 1, &loose)), &rows(vec![text("a")])).unwrap(),
                "<!--f:l0--><li data-forma-key=\"authored\"></li>text<input><!--/f:l0-->"
            );
        }

        #[test]
        fn a_void_root_carries_the_key_and_admits_nothing_beside_it() {
            let void = encode_void_tag(INPUT, &[(TYPE, CHECKBOX)]);
            let html = walk_to_html(
                &keyed(&encode_list(0, 1, &void), &[(0, &[ID])]),
                &rows(vec![id(num(7.0)), id(num(8.0))]),
            )
            .unwrap();
            assert_eq!(
                html,
                "<!--f:l0--><input type=\"checkbox\" data-forma-key=\"7\"><input type=\"checkbox\" data-forma-key=\"8\"><!--/f:l0-->"
            );
            let beside = [void.clone(), encode_text(TEXT)].concat();
            assert_eq!(
                refused(
                    &keyed(&encode_list(0, 1, &beside), &[(0, &[ID])]),
                    &rows(vec![id(num(7.0))])
                ),
                violation(
                    0,
                    0,
                    "id",
                    ListKeyReason::RowShape {
                        roots: 1,
                        stray_content: true
                    }
                )
            );
        }

        /// A registered child as the row: `<!--f:iN-->` wholly encloses the host, which is
        /// the row's root and carries both the island attributes and the key.
        #[test]
        fn a_registered_child_envelope_enclosing_the_root_puts_the_key_on_the_host() {
            let body = [
                encode_island_start(0),
                encode_open_tag(B, &[]),
                encode_close_tag(B),
                encode_island_end(0),
            ]
            .concat();
            let ops = encode_list(0, 1, &body);
            let island = (0, 0x01, 0x01, CHILD, LIST_HEADER as u32, &[][..]);
            let module = keyed_with_islands(&ops, &[(0, &[ID])], &[island]);
            let html = walk_to_html(&module, &rows(vec![id(num(1.0)), id(num(2.0))])).unwrap();
            let host = |occurrence: usize, key: &str| {
                format!("<!--f:i0--><b data-forma-island=\"0\" data-forma-component=\"Child\" data-forma-status=\"pending\" data-forma-hydrate=\"load\" data-forma-state=\"{occurrence}\" data-forma-h=\"0000000000000000\" data-forma-key=\"{key}\"></b><!--/f:i0-->")
            };
            assert_eq!(
                html,
                format!("<!--f:l0-->{}{}<!--/f:l0-->", host(0, "1"), host(1, "2"))
            );
        }

        #[test]
        fn an_island_envelope_that_does_not_wholly_enclose_the_root_refuses() {
            // (body, ISLAND_START's offset inside the body, roots counted)
            let empty_island_then_root = [
                encode_island_start(0),
                encode_island_end(0),
                encode_open_tag(LI, &[]),
                encode_close_tag(LI),
            ]
            .concat();
            let root_then_child = [
                encode_open_tag(LI, &[]),
                encode_close_tag(LI),
                encode_island_start(0),
                encode_open_tag(B, &[]),
                encode_close_tag(B),
                encode_island_end(0),
            ]
            .concat();
            let cases: [(&[u8], usize, usize); 2] = [
                (&empty_island_then_root, 0, 1),
                (
                    &root_then_child,
                    encode_open_tag(LI, &[]).len() + encode_close_tag(LI).len(),
                    2,
                ),
            ];
            for (body, at, roots) in cases {
                let island = (0, 0x01, 0x01, CHILD, (LIST_HEADER + at) as u32, &[][..]);
                let module = keyed_with_islands(&encode_list(0, 1, body), &[(0, &[ID])], &[island]);
                assert_eq!(
                    refused(&module, &rows(vec![id(num(1.0))])),
                    violation(
                        0,
                        0,
                        "id",
                        ListKeyReason::RowShape {
                            roots,
                            stray_content: true
                        }
                    )
                );
            }
        }

        /// `<li>` PROP(row.tags) LIST(tags){ `<b></b>` } `</li>`, both lists keyed by `id`.
        fn nested() -> (Vec<u8>, u32) {
            let inner = encode_list(
                2,
                3,
                &[encode_open_tag(B, &[]), encode_close_tag(B)].concat(),
            );
            let outer_body = [
                encode_open_tag(LI, &[]),
                encode_prop(1, TAGS, 2),
                inner,
                encode_close_tag(LI),
            ]
            .concat();
            let inner_offset =
                LIST_HEADER + encode_open_tag(LI, &[]).len() + encode_prop(1, TAGS, 2).len();
            (encode_list(0, 1, &outer_body), inner_offset as u32)
        }

        fn group(key: f64, tags: Vec<SlotValue>) -> SlotValue {
            object(vec![("id", num(key)), ("tags", SlotValue::array(tags))])
        }

        #[test]
        fn each_execution_of_a_list_is_its_own_duplicate_scope() {
            let (ops, inner) = nested();
            let module = keyed(&ops, &[(0, &[ID]), (inner, &[ID])]);
            // The inner LIST runs once per outer row: the same keys under two outer rows,
            // and an inner key equal to its outer row's key, are not duplicates.
            let data = rows(vec![
                group(1.0, vec![id(text("a")), id(num(1.0))]),
                group(2.0, vec![id(text("a"))]),
            ]);
            assert_eq!(
                walk_to_html(&module, &data).unwrap(),
                concat!(
                    "<!--f:l0-->",
                    "<li data-forma-key=\"1\"><!--f:l2--><b data-forma-key=\"a\"></b><b data-forma-key=\"1\"></b><!--/f:l2--></li>",
                    "<li data-forma-key=\"2\"><!--f:l2--><b data-forma-key=\"a\"></b><!--/f:l2--></li>",
                    "<!--/f:l0-->"
                )
            );
            // Within one execution they are: the inner LIST's offset, its row, the path.
            let duplicate = rows(vec![
                group(1.0, vec![id(text("a"))]),
                group(2.0, vec![id(text("b")), id(text("a")), id(text("b"))]),
            ]);
            assert_eq!(
                refused(&module, &duplicate),
                violation(inner, 2, "id", ListKeyReason::Duplicate { first_row: 0 })
            );
            // Two sibling LISTs over the same rows are two executions too.
            let twice = [encode_list(0, 1, &li_body()), encode_list(0, 1, &li_body())].concat();
            let second = encode_list(0, 1, &li_body()).len() as u32;
            let html = walk_to_html(
                &keyed(&twice, &[(0, &[ID]), (second, &[ID])]),
                &rows(vec![id(num(1.0)), id(num(2.0))]),
            )
            .unwrap();
            assert_eq!(html.matches("data-forma-key=\"1\"").count(), 2, "{html}");
            assert_eq!(html.matches("data-forma-key=\"2\"").count(), 2, "{html}");
        }

        #[test]
        fn keys_collide_on_their_canonical_text_not_on_their_type_or_spelling() {
            let module = keyed(&encode_list(0, 1, &li_body()), &[(0, &[ID])]);
            let collide = [
                (num(1.0), text("1")),
                (num(0.0), num(-0.0)),
                (text("0"), num(-0.0)),
                (text("a\r\nb"), text("a\nb")),
                (text("a\rb"), text("a\nb")),
                (text("\0"), text("\u{FFFD}")),
            ];
            for (first, second) in collide {
                let label = format!("{first:?} {second:?}");
                assert_eq!(
                    refused(&module, &rows(vec![id(first), id(second)])),
                    violation(0, 1, "id", ListKeyReason::Duplicate { first_row: 0 }),
                    "{label}"
                );
            }
            let distinct = [
                (num(1.0), text("01")),
                (text("A"), text("a")),
                (text("a"), text("a ")),
                (text("\u{e9}"), text("e\u{301}")),
                (text("-0"), num(0.0)),
            ];
            for (first, second) in distinct {
                let label = format!("{first:?} {second:?}");
                assert!(
                    walk_to_html(&module, &rows(vec![id(first), id(second)])).is_ok(),
                    "{label}"
                );
            }
        }

        #[test]
        fn a_row_without_an_identity_at_the_path_refuses_with_its_reason() {
            let module = keyed(&encode_list(0, 1, &li_body()), &[(0, &[ID])]);
            let not_identity = |kind| ListKeyReason::NotAnIdentity { kind };
            let twice = SlotValue::object(vec![("id".into(), num(1.0)), ("id".into(), num(2.0))]);
            let cases = [
                (
                    object(vec![("name", text("secret"))]),
                    ListKeyReason::MissingField {
                        segment: "id".into(),
                    },
                ),
                (id(SlotValue::Null), not_identity("null")),
                (id(SlotValue::Bool(true)), not_identity("a boolean")),
                (
                    id(object(vec![("secret", num(1.0))])),
                    not_identity("an object"),
                ),
                (
                    id(SlotValue::array(vec![text("secret")])),
                    not_identity("an array"),
                ),
                (id(num(1.5)), ListKeyReason::NotASafeInteger),
                (id(num(f64::NAN)), ListKeyReason::NotASafeInteger),
                (id(num(f64::INFINITY)), ListKeyReason::NotASafeInteger),
                (id(num(f64::NEG_INFINITY)), ListKeyReason::NotASafeInteger),
                (
                    id(num(9_007_199_254_740_992.0)),
                    ListKeyReason::NotASafeInteger,
                ),
                (
                    id(num(-9_007_199_254_740_992.0)),
                    ListKeyReason::NotASafeInteger,
                ),
                (
                    text("secret"),
                    ListKeyReason::NotAnObject {
                        segment: "id".into(),
                    },
                ),
                (
                    twice,
                    ListKeyReason::DuplicateField {
                        segment: "id".into(),
                    },
                ),
            ];
            for (bad, reason) in cases {
                // The refused row is the second: the first rendered nothing the page keeps.
                assert_eq!(
                    refused(&module, &rows(vec![id(num(1.0)), bad])),
                    violation(0, 1, "id", reason)
                );
            }
            // A nested path reads objects only, and names itself dotted.
            let nested = keyed(&encode_list(0, 1, &li_body()), &[(0, &[USER, ID])]);
            let user = |value| object(vec![("user", value)]);
            assert_eq!(
                walk_to_html(&nested, &rows(vec![user(id(text("u1")))])).unwrap(),
                "<!--f:l0--><li data-forma-key=\"u1\"><span></span></li><!--/f:l0-->"
            );
            for value in [
                text("u1"),
                SlotValue::Null,
                SlotValue::array(vec![id(num(1.0))]),
            ] {
                assert_eq!(
                    refused(&nested, &rows(vec![user(value)])),
                    violation(
                        0,
                        0,
                        "user.id",
                        ListKeyReason::NotAnObject {
                            segment: "id".into()
                        }
                    )
                );
            }
        }

        #[test]
        fn a_keyed_row_renders_exactly_one_balanced_root_and_nothing_beside_it() {
            let shape = |roots, stray_content| ListKeyReason::RowShape {
                roots,
                stray_content,
            };
            let li = [encode_open_tag(LI, &[]), encode_close_tag(LI)].concat();
            let cases: Vec<(&str, Vec<u8>, ListKeyReason)> = vec![
                ("empty row", Vec::new(), shape(0, false)),
                (
                    "two roots",
                    [li.clone(), li.clone()].concat(),
                    shape(2, false),
                ),
                (
                    "text beside",
                    [li.clone(), encode_text(TEXT)].concat(),
                    shape(1, true),
                ),
                ("text only", encode_text(TEXT), shape(0, true)),
                ("dynamic text only", encode_dyn_text(4, 0), shape(0, true)),
                (
                    "conditional root",
                    encode_show_if(4, &li, &li),
                    shape(1, true),
                ),
                (
                    // Its one row's `li` is at the outer row's level too, so it counts as
                    // the outer row's root; the range around it is the refusal.
                    "list at the row's level",
                    encode_list(2, 3, &li),
                    shape(1, true),
                ),
                (
                    "closes the list's parent",
                    encode_close_tag(UL),
                    ListKeyReason::UnbalancedRow,
                ),
                (
                    "closes the parent after its root",
                    [li.clone(), encode_close_tag(UL), encode_open_tag(UL, &[])].concat(),
                    ListKeyReason::UnbalancedRow,
                ),
                (
                    "leaves its root open",
                    encode_open_tag(LI, &[]),
                    ListKeyReason::UnbalancedRow,
                ),
            ];
            for (name, body, reason) in cases {
                let ops = [
                    encode_open_tag(UL, &[]),
                    encode_list(0, 1, &body),
                    encode_close_tag(UL),
                ]
                .concat();
                let offset = encode_open_tag(UL, &[]).len() as u32;
                let module = keyed(&ops, &[(offset, &[ID])]);
                let mut data = rows(vec![id(num(1.0))]);
                data.set(2, SlotValue::array(vec![text("t")]));
                assert_eq!(
                    refused(&module, &data),
                    violation(offset, 0, "id", reason),
                    "{name}"
                );
            }
        }

        #[test]
        fn an_authored_data_forma_key_on_the_root_refuses_and_on_a_descendant_renders() {
            let authored = |body: Vec<u8>| {
                refused(
                    &keyed(&encode_list(0, 1, &body), &[(0, &[ID])]),
                    &rows(vec![id(num(1.0))]),
                )
            };
            let expected = violation(0, 0, "id", ListKeyReason::AuthoredKey);
            for attr in [KEY_ATTR, KEY_ATTR_UPPER] {
                assert_eq!(
                    authored(
                        [
                            encode_open_tag(LI, &[(attr, AUTHORED)]),
                            encode_close_tag(LI)
                        ]
                        .concat()
                    ),
                    expected
                );
                assert_eq!(
                    authored(encode_void_tag(INPUT, &[(attr, AUTHORED)])),
                    expected
                );
                assert_eq!(
                    authored(
                        [
                            encode_open_tag(LI, &[]),
                            encode_dyn_attr(attr, 4),
                            encode_close_tag(LI)
                        ]
                        .concat()
                    ),
                    expected
                );
            }
            let descendant = [
                encode_open_tag(LI, &[]),
                encode_open_tag(SPAN, &[(KEY_ATTR, AUTHORED)]),
                encode_close_tag(SPAN),
                encode_close_tag(LI),
            ]
            .concat();
            assert_eq!(
                walk_to_html(
                    &keyed(&encode_list(0, 1, &descendant), &[(0, &[ID])]),
                    &rows(vec![id(num(1.0))])
                )
                .unwrap(),
                "<!--f:l0--><li data-forma-key=\"1\"><span data-forma-key=\"authored\"></span></li><!--/f:l0-->"
            );
        }

        #[test]
        fn a_refused_row_leaves_the_slot_journal_keyed_rows_and_list_depth_as_found() {
            let (ops, inner) = nested();
            let module = keyed(&ops, &[(0, &[ID]), (inner, &[ID])]);
            let bad_inner_key = rows(vec![
                group(1.0, vec![id(text("a"))]),
                group(2.0, vec![id(text("a")), id(text("a"))]),
            ]);
            // The inner row's key is fine, but the output budget runs out inside it (50 bytes
            // end within its `<b data-forma-key="a">`): a non-key error unwinds the same way.
            let over_budget_inside = rows(vec![group(1.0, vec![id(text("a"))])]);
            let cases = [
                (
                    bad_inner_key,
                    WalkLimits::default(),
                    IrError::InvalidListKey(Box::new(violation(
                        inner,
                        1,
                        "id",
                        ListKeyReason::Duplicate { first_row: 0 },
                    ))),
                ),
                (
                    over_budget_inside,
                    WalkLimits {
                        max_output_bytes: 50,
                        ..WalkLimits::default()
                    },
                    IrError::OutputLimitExceeded { limit: 50 },
                ),
            ];
            for (data, limits, expected) in cases {
                let mut slots = data.clone();
                let before = format!("{slots:?}");
                let mut state = WalkState::new(limits);
                let mut out = String::new();
                let error = walk_range(
                    &module,
                    &mut slots,
                    &module.opcodes,
                    0,
                    module.opcodes.len(),
                    &mut out,
                    &mut state,
                    0,
                )
                .unwrap_err();
                assert_eq!(error, expected);
                assert_eq!(state.list_depth, 0, "{error:?}");
                assert!(state.keyed_rows.is_empty(), "{error:?}");
                assert!(state.slot_journal.is_empty(), "{error:?}");
                assert_eq!(format!("{slots:?}"), before, "{error:?}");
            }
        }

        #[test]
        fn a_key_is_charged_to_the_output_budget_before_it_is_built() {
            let module = keyed(&encode_list(0, 1, &li_body()), &[(0, &[ID])]);
            let long = "k".repeat(1000);
            let opened = "<!--f:l0-->".len();
            let limits = WalkLimits {
                max_output_bytes: opened + 999,
                ..WalkLimits::default()
            };
            let failure =
                walk_to_html_counted(&module, &rows(vec![id(text(&long))]), limits).unwrap_err();
            assert_eq!(
                failure.error,
                IrError::OutputLimitExceeded {
                    limit: opened + 999
                }
            );
            // Refused before the row wrote anything: only the list's opening marker.
            assert_eq!(failure.counts.bytes, opened);
            // A NUL is counted as the three bytes U+FFFD takes.
            let nul = "\0".repeat(400);
            let limits = WalkLimits {
                max_output_bytes: opened + 1000,
                ..WalkLimits::default()
            };
            let failure =
                walk_to_html_counted(&module, &rows(vec![id(text(&nul))]), limits).unwrap_err();
            assert_eq!(failure.counts.bytes, opened);
            // Exactly at the budget the page renders.
            let html = walk_to_html(&module, &rows(vec![id(text("key"))])).unwrap();
            let exact = WalkLimits {
                max_output_bytes: html.len(),
                ..WalkLimits::default()
            };
            assert_eq!(
                walk_to_html_with_limits(&module, &rows(vec![id(text("key"))]), exact).unwrap(),
                html
            );
        }

        #[test]
        fn the_iteration_budget_refuses_a_keyed_list_before_any_key_is_read() {
            let module = keyed(&encode_list(0, 1, &li_body()), &[(0, &[ID])]);
            let limits = WalkLimits {
                max_list_iterations: 1,
                ..WalkLimits::default()
            };
            let failure = walk_to_html_counted(
                &module,
                &rows(vec![id(SlotValue::Null), id(num(1.0))]),
                limits,
            )
            .unwrap_err();
            assert_eq!(failure.error, IrError::ListBudgetExceeded { max: 1 });
            assert_eq!(failure.counts.list_iterations, 0);
        }

        /// FORMA-181 checkpoint C: the `served` markup of every keys.json vector is exactly
        /// what this walker writes for that key, because the browser spec parses that markup
        /// in Chromium, Firefox and WebKit and checks it decodes to the canonical text
        /// (packages/core/e2e/keyed-adoption.spec.ts). A change to the attribute escaping
        /// fails here before it can drift from what the browsers were shown.
        #[test]
        fn every_shared_key_vector_is_written_as_its_served_markup() {
            let vectors: serde_json::Value =
                serde_json::from_str(include_str!("../tests/fixtures/parity/keys.json")).unwrap();
            let module = keyed(&encode_list(0, 1, &li_body()), &[(0, &[ID])]);
            let mut checked = 0;
            for case in vectors["values"].as_array().unwrap() {
                let Some(served) = case.get("served").and_then(serde_json::Value::as_str) else {
                    continue;
                };
                let row = object(vec![(
                    "id",
                    crate::slot::json_to_slot_value(&case["value"]),
                )]);
                assert_eq!(
                    walk_to_html(&module, &rows(vec![row])).unwrap(),
                    format!(
                        "<!--f:l0--><li data-forma-key=\"{served}\"><span></span></li><!--/f:l0-->"
                    ),
                    "{}",
                    case["id"]
                );
                checked += 1;
            }
            assert!(
                checked >= 20,
                "the vector file lost its served markup: {checked}"
            );
        }

        #[test]
        fn a_diagnostic_path_quotes_a_segment_a_dot_would_make_ambiguous() {
            assert_eq!(dotted_path(&["user", "id"]), "user.id");
            assert_eq!(dotted_path(&["user.id"]), "[\"user.id\"]");
            assert_eq!(dotted_path(&["a", "b.c", "d"]), "a[\"b.c\"].d");
            assert_eq!(dotted_path(&["", "x[0]"]), "[\"\"][\"x[0]\"]");
        }
    }
}