summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/modules/accessibility/ax_object.cc
blob: b87468dfb09e19e6296267a24e44951fa0f33b59 (plain)
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
/*
 * Copyright (C) 2008, 2009, 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/modules/accessibility/ax_object.h"

#include <algorithm>
#include <ostream>

#include "base/auto_reset.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"
#include "third_party/blink/public/common/input/web_menu_source_type.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-blink.h"
#include "third_party/blink/public/mojom/input/focus_type.mojom-blink.h"
#include "third_party/blink/public/mojom/scroll/scroll_into_view_params.mojom-blink.h"
#include "third_party/blink/renderer/core/aom/accessible_node.h"
#include "third_party/blink/renderer/core/aom/accessible_node_list.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/display_lock/display_lock_utilities.h"
#include "third_party/blink/renderer/core/dom/dom_node_ids.h"
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
#include "third_party/blink/renderer/core/dom/focus_params.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/slot_assignment_engine.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/fullscreen/fullscreen.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/core/html/custom/element_internals.h"
#include "third_party/blink/renderer/core/html/fenced_frame/html_fenced_frame_element.h"
#include "third_party/blink/renderer/core/html/forms/html_form_control_element.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/core/html/forms/html_opt_group_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/core/html/forms/html_text_area_element.h"
#include "third_party/blink/renderer/core/html/forms/text_control_element.h"
#include "third_party/blink/renderer/core/html/html_dialog_element.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html/html_frame_owner_element.h"
#include "third_party/blink/renderer/core/html/html_head_element.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/html/html_map_element.h"
#include "third_party/blink/renderer/core/html/html_script_element.h"
#include "third_party/blink/renderer/core/html/html_slot_element.h"
#include "third_party/blink/renderer/core/html/html_style_element.h"
#include "third_party/blink/renderer/core/html/html_table_cell_element.h"
#include "third_party/blink/renderer/core/html/html_table_element.h"
#include "third_party/blink/renderer/core/html/html_table_row_element.h"
#include "third_party/blink/renderer/core/html/html_table_section_element.h"
#include "third_party/blink/renderer/core/html/html_title_element.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/core/html/portal/html_portal_element.h"
#include "third_party/blink/renderer/core/html/shadow/shadow_element_names.h"
#include "third_party/blink/renderer/core/input/context_menu_allowed_scope.h"
#include "third_party/blink/renderer/core/input/event_handler.h"
#include "third_party/blink/renderer/core/input_type_names.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/layout/layout_box_model_object.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller.h"
#include "third_party/blink/renderer/core/scroll/scroll_into_view_util.h"
#include "third_party/blink/renderer/core/svg/svg_element.h"
#include "third_party/blink/renderer/core/svg/svg_g_element.h"
#include "third_party/blink/renderer/core/svg/svg_style_element.h"
#if DCHECK_IS_ON()
#include "third_party/blink/renderer/modules/accessibility/ax_debug_utils.h"
#endif
#include "third_party/blink/renderer/modules/accessibility/ax_image_map_link.h"
#include "third_party/blink/renderer/modules/accessibility/ax_inline_text_box.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_option.h"
#include "third_party/blink/renderer/modules/accessibility/ax_menu_list_popup.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"
#include "third_party/blink/renderer/modules/accessibility/ax_range.h"
#include "third_party/blink/renderer/modules/accessibility/ax_selection.h"
#include "third_party/blink/renderer/modules/accessibility/ax_sparse_attribute_setter.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/keyboard_codes.h"
#include "third_party/blink/renderer/platform/language.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/text/platform_locale.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_common.h"
#include "ui/accessibility/ax_enums.mojom-blink-forward.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/gfx/geometry/transform.h"

namespace blink {

namespace {

#if defined(AX_FAIL_FAST_BUILD)
// TODO(accessibility) Move this out of DEBUG by having a new enum in
// ax_enums.mojom, and a matching ToString() in ax_enum_utils, as well as move
// out duplicate code of String IgnoredReasonName(AXIgnoredReason reason) in
// inspector_type_builder_helper.cc.
String IgnoredReasonName(AXIgnoredReason reason) {
  switch (reason) {
    case kAXActiveFullscreenElement:
      return "activeFullscreenElement";
    case kAXActiveModalDialog:
      return "activeModalDialog";
    case kAXAriaModalDialog:
      return "activeAriaModalDialog";
    case kAXAriaHiddenElement:
      return "ariaHiddenElement";
    case kAXAriaHiddenSubtree:
      return "ariaHiddenSubtree";
    case kAXEmptyAlt:
      return "emptyAlt";
    case kAXEmptyText:
      return "emptyText";
    case kAXInertElement:
      return "inertElement";
    case kAXInertSubtree:
      return "inertSubtree";
    case kAXLabelContainer:
      return "labelContainer";
    case kAXLabelFor:
      return "labelFor";
    case kAXNotRendered:
      return "notRendered";
    case kAXNotVisible:
      return "notVisible";
    case kAXPresentational:
      return "presentationalRole";
    case kAXProbablyPresentational:
      return "probablyPresentational";
    case kAXUninteresting:
      return "uninteresting";
  }
  NOTREACHED();
  return "";
}

String GetIgnoredReasonsDebugString(AXObject::IgnoredReasons& reasons) {
  if (reasons.size() == 0)
    return "";
  String string_builder = "(";
  for (wtf_size_t count = 0; count < reasons.size(); count++) {
    if (count > 0)
      string_builder = string_builder + ',';
    string_builder = string_builder + IgnoredReasonName(reasons[count].reason);
  }
  string_builder = string_builder + ")";
  return string_builder;
}

#endif

String GetNodeString(Node* node) {
  if (node->IsTextNode()) {
    String string_builder = "\"";
    string_builder = string_builder + node->nodeValue();
    string_builder = string_builder + "\"";
    return string_builder;
  }

  Element* element = DynamicTo<Element>(node);
  if (!element) {
    return To<Document>(node)->IsLoadCompleted() ? "#document"
                                                 : "#document (loading)";
  }

  String string_builder = "<";

  string_builder = string_builder + element->tagName().LowerASCII();
  // Cannot safely get @class from SVG elements.
  if (!element->IsSVGElement() &&
      element->FastHasAttribute(html_names::kClassAttr)) {
    string_builder = string_builder + "." +
                     element->FastGetAttribute(html_names::kClassAttr);
  }
  if (element->FastHasAttribute(html_names::kIdAttr)) {
    string_builder =
        string_builder + "#" + element->FastGetAttribute(html_names::kIdAttr);
  }
  return string_builder + ">";
}

#if DCHECK_IS_ON()
bool IsValidRole(ax::mojom::blink::Role role) {
  // Check for illegal roles that should not be assigned in Blink.
  switch (role) {
    case ax::mojom::blink::Role::kCaret:
    case ax::mojom::blink::Role::kClient:
    case ax::mojom::blink::Role::kColumn:
    case ax::mojom::blink::Role::kDesktop:
    case ax::mojom::blink::Role::kKeyboard:
    case ax::mojom::blink::Role::kImeCandidate:
    case ax::mojom::blink::Role::kListGrid:
    case ax::mojom::blink::Role::kPane:
    case ax::mojom::blink::Role::kPdfActionableHighlight:
    case ax::mojom::blink::Role::kPdfRoot:
    case ax::mojom::blink::Role::kTableHeaderContainer:
    case ax::mojom::blink::Role::kTitleBar:
    case ax::mojom::blink::Role::kUnknown:
    case ax::mojom::blink::Role::kWebView:
    case ax::mojom::blink::Role::kWindow:
      return false;
    default:
      return true;
  }
}
#endif

using RoleHashTraits =
    EnumHashTraits<ax::mojom::blink::Role, ax::mojom::blink::Role::kUnknown>;

constexpr wtf_size_t kNumRoles =
    static_cast<wtf_size_t>(ax::mojom::blink::Role::kMaxValue) + 1;

using ARIARoleMap = HashMap<String,
                            ax::mojom::blink::Role,
                            CaseFoldingHashTraits<String>,
                            RoleHashTraits>;

struct RoleEntry {
  const char* role_name;
  ax::mojom::blink::Role role;
};

// Mapping of ARIA role name to internal role name.
// This is used for the following:
// 1. Map from an ARIA role to the internal role when building tree.
// 2. Map from an internal role to an ARIA role name, for debugging, the
//    xml-roles object attribute and element.computedRole.
const RoleEntry kAriaRoles[] = {
    {"alert", ax::mojom::blink::Role::kAlert},
    {"alertdialog", ax::mojom::blink::Role::kAlertDialog},
    {"application", ax::mojom::blink::Role::kApplication},
    {"article", ax::mojom::blink::Role::kArticle},
    {"banner", ax::mojom::blink::Role::kBanner},
    {"blockquote", ax::mojom::blink::Role::kBlockquote},
    {"button", ax::mojom::blink::Role::kButton},
    {"caption", ax::mojom::blink::Role::kCaption},
    {"cell", ax::mojom::blink::Role::kCell},
    {"code", ax::mojom::blink::Role::kCode},
    {"checkbox", ax::mojom::blink::Role::kCheckBox},
    {"columnheader", ax::mojom::blink::Role::kColumnHeader},
    {"combobox", ax::mojom::blink::Role::kComboBoxGrouping},
    {"comment", ax::mojom::blink::Role::kComment},
    {"complementary", ax::mojom::blink::Role::kComplementary},
    {"contentinfo", ax::mojom::blink::Role::kContentInfo},
    {"definition", ax::mojom::blink::Role::kDefinition},
    {"deletion", ax::mojom::blink::Role::kContentDeletion},
    {"dialog", ax::mojom::blink::Role::kDialog},
    {"directory", ax::mojom::blink::Role::kDirectory},
    // -------------------------------------------------
    // DPub Roles:
    // www.w3.org/TR/dpub-aam-1.0/#mapping_role_table
    {"doc-abstract", ax::mojom::blink::Role::kDocAbstract},
    {"doc-acknowledgments", ax::mojom::blink::Role::kDocAcknowledgments},
    {"doc-afterword", ax::mojom::blink::Role::kDocAfterword},
    {"doc-appendix", ax::mojom::blink::Role::kDocAppendix},
    {"doc-backlink", ax::mojom::blink::Role::kDocBackLink},
    // Deprecated in DPUB-ARIA 1.1. Use a listitem inside of a doc-bibliography.
    {"doc-biblioentry", ax::mojom::blink::Role::kDocBiblioEntry},
    {"doc-bibliography", ax::mojom::blink::Role::kDocBibliography},
    {"doc-biblioref", ax::mojom::blink::Role::kDocBiblioRef},
    {"doc-chapter", ax::mojom::blink::Role::kDocChapter},
    {"doc-colophon", ax::mojom::blink::Role::kDocColophon},
    {"doc-conclusion", ax::mojom::blink::Role::kDocConclusion},
    {"doc-cover", ax::mojom::blink::Role::kDocCover},
    {"doc-credit", ax::mojom::blink::Role::kDocCredit},
    {"doc-credits", ax::mojom::blink::Role::kDocCredits},
    {"doc-dedication", ax::mojom::blink::Role::kDocDedication},
    // Deprecated in DPUB-ARIA 1.1. Use a listitem inside of a doc-endnotes.
    {"doc-endnote", ax::mojom::blink::Role::kDocEndnote},
    {"doc-endnotes", ax::mojom::blink::Role::kDocEndnotes},
    {"doc-epigraph", ax::mojom::blink::Role::kDocEpigraph},
    {"doc-epilogue", ax::mojom::blink::Role::kDocEpilogue},
    {"doc-errata", ax::mojom::blink::Role::kDocErrata},
    {"doc-example", ax::mojom::blink::Role::kDocExample},
    {"doc-footnote", ax::mojom::blink::Role::kDocFootnote},
    {"doc-foreword", ax::mojom::blink::Role::kDocForeword},
    {"doc-glossary", ax::mojom::blink::Role::kDocGlossary},
    {"doc-glossref", ax::mojom::blink::Role::kDocGlossRef},
    {"doc-index", ax::mojom::blink::Role::kDocIndex},
    {"doc-introduction", ax::mojom::blink::Role::kDocIntroduction},
    {"doc-noteref", ax::mojom::blink::Role::kDocNoteRef},
    {"doc-notice", ax::mojom::blink::Role::kDocNotice},
    {"doc-pagebreak", ax::mojom::blink::Role::kDocPageBreak},
    {"doc-pagefooter", ax::mojom::blink::Role::kDocPageFooter},
    {"doc-pageheader", ax::mojom::blink::Role::kDocPageHeader},
    {"doc-pagelist", ax::mojom::blink::Role::kDocPageList},
    {"doc-part", ax::mojom::blink::Role::kDocPart},
    {"doc-preface", ax::mojom::blink::Role::kDocPreface},
    {"doc-prologue", ax::mojom::blink::Role::kDocPrologue},
    {"doc-pullquote", ax::mojom::blink::Role::kDocPullquote},
    {"doc-qna", ax::mojom::blink::Role::kDocQna},
    {"doc-subtitle", ax::mojom::blink::Role::kDocSubtitle},
    {"doc-tip", ax::mojom::blink::Role::kDocTip},
    {"doc-toc", ax::mojom::blink::Role::kDocToc},
    // End DPub roles.
    // -------------------------------------------------
    {"document", ax::mojom::blink::Role::kDocument},
    {"emphasis", ax::mojom::blink::Role::kEmphasis},
    {"feed", ax::mojom::blink::Role::kFeed},
    {"figure", ax::mojom::blink::Role::kFigure},
    {"form", ax::mojom::blink::Role::kForm},
    {"generic", ax::mojom::blink::Role::kGenericContainer},
    // -------------------------------------------------
    // ARIA Graphics module roles:
    // https://rawgit.com/w3c/graphics-aam/master/
    {"graphics-document", ax::mojom::blink::Role::kGraphicsDocument},
    {"graphics-object", ax::mojom::blink::Role::kGraphicsObject},
    {"graphics-symbol", ax::mojom::blink::Role::kGraphicsSymbol},
    // End ARIA Graphics module roles.
    // -------------------------------------------------
    {"grid", ax::mojom::blink::Role::kGrid},
    {"gridcell", ax::mojom::blink::Role::kCell},
    {"group", ax::mojom::blink::Role::kGroup},
    {"heading", ax::mojom::blink::Role::kHeading},
    {"img", ax::mojom::blink::Role::kImage},
    // role="image" is listed after role="img" to treat the synonym img
    // as a computed name image
    {"image", ax::mojom::blink::Role::kImage},
    {"insertion", ax::mojom::blink::Role::kContentInsertion},
    {"link", ax::mojom::blink::Role::kLink},
    {"list", ax::mojom::blink::Role::kList},
    {"listbox", ax::mojom::blink::Role::kListBox},
    {"listitem", ax::mojom::blink::Role::kListItem},
    {"log", ax::mojom::blink::Role::kLog},
    {"main", ax::mojom::blink::Role::kMain},
    {"marquee", ax::mojom::blink::Role::kMarquee},
    {"math", ax::mojom::blink::Role::kMath},
    {"menu", ax::mojom::blink::Role::kMenu},
    {"menubar", ax::mojom::blink::Role::kMenuBar},
    {"menuitem", ax::mojom::blink::Role::kMenuItem},
    {"menuitemcheckbox", ax::mojom::blink::Role::kMenuItemCheckBox},
    {"menuitemradio", ax::mojom::blink::Role::kMenuItemRadio},
    {"mark", ax::mojom::blink::Role::kMark},
    {"meter", ax::mojom::blink::Role::kMeter},
    {"navigation", ax::mojom::blink::Role::kNavigation},
    // role="presentation" is the same as role="none".
    {"presentation", ax::mojom::blink::Role::kNone},
    // role="none" is listed after role="presentation", so that it is the
    // canonical name in devtools and tests.
    {"none", ax::mojom::blink::Role::kNone},
    {"note", ax::mojom::blink::Role::kNote},
    {"option", ax::mojom::blink::Role::kListBoxOption},
    {"paragraph", ax::mojom::blink::Role::kParagraph},
    {"progressbar", ax::mojom::blink::Role::kProgressIndicator},
    {"radio", ax::mojom::blink::Role::kRadioButton},
    {"radiogroup", ax::mojom::blink::Role::kRadioGroup},
    {"region", ax::mojom::blink::Role::kRegion},
    {"row", ax::mojom::blink::Role::kRow},
    {"rowgroup", ax::mojom::blink::Role::kRowGroup},
    {"rowheader", ax::mojom::blink::Role::kRowHeader},
    {"scrollbar", ax::mojom::blink::Role::kScrollBar},
    {"search", ax::mojom::blink::Role::kSearch},
    {"searchbox", ax::mojom::blink::Role::kSearchBox},
    {"separator", ax::mojom::blink::Role::kSplitter},
    {"slider", ax::mojom::blink::Role::kSlider},
    {"spinbutton", ax::mojom::blink::Role::kSpinButton},
    {"status", ax::mojom::blink::Role::kStatus},
    {"strong", ax::mojom::blink::Role::kStrong},
    {"subscript", ax::mojom::blink::Role::kSubscript},
    {"suggestion", ax::mojom::blink::Role::kSuggestion},
    {"superscript", ax::mojom::blink::Role::kSuperscript},
    {"switch", ax::mojom::blink::Role::kSwitch},
    {"tab", ax::mojom::blink::Role::kTab},
    {"table", ax::mojom::blink::Role::kTable},
    {"tablist", ax::mojom::blink::Role::kTabList},
    {"tabpanel", ax::mojom::blink::Role::kTabPanel},
    {"term", ax::mojom::blink::Role::kTerm},
    {"textbox", ax::mojom::blink::Role::kTextField},
    {"time", ax::mojom::blink::Role::kTime},
    {"timer", ax::mojom::blink::Role::kTimer},
    {"toolbar", ax::mojom::blink::Role::kToolbar},
    {"tooltip", ax::mojom::blink::Role::kTooltip},
    {"tree", ax::mojom::blink::Role::kTree},
    {"treegrid", ax::mojom::blink::Role::kTreeGrid},
    {"treeitem", ax::mojom::blink::Role::kTreeItem}};

// More friendly names for debugging. These are roles which don't map from
// the ARIA role name to the internal role when building the tree, but when
// debugging, we want to show the ARIA role name, since it is close in meaning.
const RoleEntry kReverseRoles[] = {
    {"banner", ax::mojom::blink::Role::kHeader},
    {"button", ax::mojom::blink::Role::kToggleButton},
    {"button", ax::mojom::blink::Role::kPopUpButton},
    {"contentinfo", ax::mojom::blink::Role::kFooter},
    {"menuitem", ax::mojom::blink::Role::kMenuListOption},
    {"combobox", ax::mojom::blink::Role::kComboBoxMenuButton},
    {"combobox", ax::mojom::blink::Role::kComboBoxSelect},
    {"combobox", ax::mojom::blink::Role::kTextFieldWithComboBox}};

static ARIARoleMap* CreateARIARoleMap() {
  ARIARoleMap* role_map = new ARIARoleMap;

  for (auto aria_role : kAriaRoles)
    role_map->Set(String(aria_role.role_name), aria_role.role);

  return role_map;
}

// The role name vector contains only ARIA roles, and no internal roles.
static Vector<AtomicString>* CreateARIARoleNameVector() {
  Vector<AtomicString>* role_name_vector = new Vector<AtomicString>(kNumRoles);
  role_name_vector->Fill(g_null_atom, kNumRoles);

  for (auto aria_role : kAriaRoles) {
    (*role_name_vector)[static_cast<wtf_size_t>(aria_role.role)] =
        AtomicString(aria_role.role_name);
  }

  for (auto reverse_role : kReverseRoles) {
    (*role_name_vector)[static_cast<wtf_size_t>(reverse_role.role)] =
        AtomicString(reverse_role.role_name);
  }

  return role_name_vector;
}

void AddIntListAttributeFromObjects(ax::mojom::blink::IntListAttribute attr,
                                    const AXObject::AXObjectVector& objects,
                                    ui::AXNodeData* node_data) {
  DCHECK(node_data);
  std::vector<int32_t> ids;
  for (const auto& obj : objects) {
    if (!obj->AccessibilityIsIgnored())
      ids.push_back(obj->AXObjectID());
  }
  if (!ids.empty())
    node_data->AddIntListAttribute(attr, ids);
}

// Max length for attributes such as aria-label.
static constexpr uint32_t kMaxStringAttributeLength = 10000;
// Max length for a static text name.
// Length of War and Peace (http://www.gutenberg.org/files/2600/2600-0.txt).
static constexpr uint32_t kMaxStaticTextLength = 3227574;

void TruncateAndAddStringAttribute(
    ui::AXNodeData* dst,
    ax::mojom::blink::StringAttribute attribute,
    const String& value,
    uint32_t max_len = kMaxStringAttributeLength) {
  if (value.empty())
    return;
  std::string value_utf8 = value.Utf8(kStrictUTF8Conversion);
  if (value_utf8.size() > max_len) {
    std::string truncated;
    base::TruncateUTF8ToByteSize(value_utf8, max_len, &truncated);
    dst->AddStringAttribute(attribute, truncated);
  } else {
    dst->AddStringAttribute(attribute, value_utf8);
  }
}

void AddIntListAttributeFromOffsetVector(
    ax::mojom::blink::IntListAttribute attr,
    const Vector<int> offsets,
    ui::AXNodeData* node_data) {
  std::vector<int32_t> offset_values;
  for (int offset : offsets)
    offset_values.push_back(static_cast<int32_t>(offset));
  DCHECK(node_data);
  if (!offset_values.empty())
    node_data->AddIntListAttribute(attr, offset_values);
}

}  // namespace

int32_t ToAXMarkerType(DocumentMarker::MarkerType marker_type) {
  ax::mojom::blink::MarkerType result;
  switch (marker_type) {
    case DocumentMarker::kSpelling:
      result = ax::mojom::blink::MarkerType::kSpelling;
      break;
    case DocumentMarker::kGrammar:
      result = ax::mojom::blink::MarkerType::kGrammar;
      break;
    case DocumentMarker::kTextFragment:
    case DocumentMarker::kTextMatch:
      result = ax::mojom::blink::MarkerType::kTextMatch;
      break;
    case DocumentMarker::kActiveSuggestion:
      result = ax::mojom::blink::MarkerType::kActiveSuggestion;
      break;
    case DocumentMarker::kSuggestion:
      result = ax::mojom::blink::MarkerType::kSuggestion;
      break;
    case DocumentMarker::kCustomHighlight:
      result = ax::mojom::blink::MarkerType::kHighlight;
      break;
    default:
      result = ax::mojom::blink::MarkerType::kNone;
      break;
  }

  return static_cast<int32_t>(result);
}

int32_t ToAXHighlightType(const AtomicString& highlight_type) {
  DEFINE_STATIC_LOCAL(const AtomicString, type_highlight, ("highlight"));
  DEFINE_STATIC_LOCAL(const AtomicString, type_spelling_error,
                      ("spelling-error"));
  DEFINE_STATIC_LOCAL(const AtomicString, type_grammar_error,
                      ("grammar-error"));
  ax::mojom::blink::HighlightType result =
      ax::mojom::blink::HighlightType::kNone;
  if (highlight_type == type_highlight)
    result = ax::mojom::blink::HighlightType::kHighlight;
  else if (highlight_type == type_spelling_error)
    result = ax::mojom::blink::HighlightType::kSpellingError;
  else if (highlight_type == type_grammar_error)
    result = ax::mojom::blink::HighlightType::kGrammarError;

  // Check that |highlight_type| is one of the static AtomicStrings defined
  // above or "none", so if there are more HighlightTypes added, they should
  // also be taken into account in this function.
  DCHECK(result != ax::mojom::blink::HighlightType::kNone ||
         highlight_type == "none");
  return static_cast<int32_t>(result);
}

const AXObject* FindAncestorWithAriaHidden(const AXObject* start) {
  for (const AXObject* object = start; object && !object->IsWebArea();
       object = object->ParentObject()) {
    if (object->AOMPropertyOrARIAAttributeIsTrue(AOMBooleanProperty::kHidden))
      return object;
  }

  return nullptr;
}

// static
unsigned AXObject::number_of_live_ax_objects_ = 0;

AXObject::AXObject(AXObjectCacheImpl& ax_object_cache)
    : id_(0),
      parent_(nullptr),
      role_(ax::mojom::blink::Role::kUnknown),
      explicit_container_id_(0),
      cached_values_need_update_(true),
      cached_is_ignored_(false),
      cached_is_ignored_but_included_in_tree_(false),
      cached_is_inert_(false),
      cached_is_aria_hidden_(false),
      cached_is_descendant_of_disabled_node_(false),
      cached_can_set_focus_attribute_(false),
      cached_live_region_root_(nullptr),
      ax_object_cache_(&ax_object_cache) {
  ++number_of_live_ax_objects_;
}

AXObject::~AXObject() {
  DCHECK(IsDetached());
  --number_of_live_ax_objects_;
}

void AXObject::SetHasDirtyDescendants(bool dirty) const {
  CHECK(!dirty || LastKnownIsIncludedInTreeValue())
      << "Only included nodes can be marked as having dirty descendants: "
      << ToString(true, true);
  has_dirty_descendants_ = dirty;
}

void AXObject::SetAncestorsHaveDirtyDescendants() const {
  DCHECK(!IsDetached());
  DCHECK(!AXObjectCache().HasBeenDisposed());
  DCHECK(!AXObjectCache().IsFrozen());
  DCHECK(!AXObjectCache().UpdatingTree());

  if (!RuntimeEnabledFeatures::AccessibilityEagerAXTreeUpdateEnabled()) {
    return;
  }

  // Set the dirty bit for the root AX object when created. For all other
  // objects, this is set by a descendant needing to be updated, and
  // AXObjectCacheImpl::UpdateTreeIfNeeded will therefore process an object
  // if its parent has has_dirty_descendants_ set. The root, however, has no
  // parent, so there is no parent to mark in order to cause the root to update
  // itself. Therefore this bit serves a second purpose of determining
  // whether AXObjectCacheImpl::UpdateTreeIfNeeded needs to update the root
  // object.
  if (IsRoot()) {
    // Need at least the root object to be flagged in order for
    // UpdateTreeIfNeeded() to do anything.
    SetHasDirtyDescendants(true);
    return;
  }

  if (AXObjectCache().EntireDocumentIsDirty()) {
    // No need to walk parent chain when marking the entire document dirty,
    // as every node will have the bit set. In addition, attempting to repair
    // the parent chain while marking everything dirty is actually against
    // the point, because all child-parent relationships will be rebuilt
    // from the top down.
    if (LastKnownIsIncludedInTreeValue()) {
      SetHasDirtyDescendants(true);
    }
    return;
  }

  const AXObject* ancestor = this;
  bool can_repair_parents = AXObjectCache().IsProcessingDeferredEvents();

  while (true) {
    if (can_repair_parents && ancestor->IsMissingParent()) {
      // RepairMissingParent() will finish setting the
      // "has dirty descendants" flag the rest of the way of the parent chain
      // by calling back into this method.
      ancestor->RepairMissingParent();
      break;
    }
    ancestor = ancestor->CachedParentObject();
    if (!ancestor) {
      break;
    }
    DCHECK(!ancestor->IsDetached());

    // We need to to continue setting bits through AX objects for which
    // LastKnownIsIncludedInTreeValue is false, since those objects are omitted
    // from the generated tree. However, don't set the bit on unincluded
    // objects, during the clearing phase in
    // AXObjectCacheImpl::UpdateTreeIfNeeded(), only included nodes are
    // visited.
    if (!ancestor->LastKnownIsIncludedInTreeValue()) {
      continue;
    }
    if (ancestor->has_dirty_descendants_) {
      break;
    }
    ancestor->SetHasDirtyDescendants(true);
  }
#if DCHECK_IS_ON()
  // Walk up the tree looking for dirty bits that failed to be set. If any
  // are found, this is a bug.
  if (!AXObjectCache().UpdatingTree()) {
    bool fail = false;
    for (auto* obj = CachedParentObject(); obj;
         obj = obj->CachedParentObject()) {
      if (obj->LastKnownIsIncludedInTreeValue() &&
          !obj->has_dirty_descendants_) {
        fail = true;
        break;
      }
    }
    if (fail) {
      LOG(ERROR) << "Failed to set dirty bits on some objects in the ancestor"
                    "chain. Bits set: ";
      for (auto* obj = this; obj; obj = obj->CachedParentObject()) {
        LOG(ERROR) << "* has_dirty_descendants_: "
                   << obj->has_dirty_descendants_
                   << " object: " << obj->ToString(true, true);
      }
      DCHECK(false);
    }
  }
#endif
}

void AXObject::Init(AXObject* parent) {
#if DCHECK_IS_ON()
  DCHECK(!parent_) << "Should not already have a cached parent:"
                   << "\n* Child = " << GetNode() << " / " << GetLayoutObject()
                   << "\n* Parent = " << parent_->ToString(true, true)
                   << "\n* Equal to passed-in parent? " << (parent == parent_);
  DCHECK(!is_initializing_);
  base::AutoReset<bool> reentrancy_protector(&is_initializing_, true);
#endif  // DCHECK_IS_ON()
  // The role must be determined immediately.
  // Note: in order to avoid reentrancy, the role computation cannot use the
  // ParentObject(), although it can use the DOM parent.
  role_ = DetermineAccessibilityRole();
#if DCHECK_IS_ON()
  DCHECK(IsValidRole(role_)) << "Illegal " << role_ << " for\n"
                             << GetNode() << '\n'
                             << GetLayoutObject();

  HTMLOptGroupElement* optgroup = DynamicTo<HTMLOptGroupElement>(GetNode());
  if (optgroup && optgroup->OwnerSelectElement()) {
    // We do not currently create accessible objects for an <optgroup> inside of
    // a <select size=1>.
    // TODO(accessibility) Remove this once we refactor HTML <select> to use
    // the shadow DOM and AXNodeObject instead of AXMenuList* classes.
    DCHECK(!optgroup->OwnerSelectElement()->UsesMenuList());
  }
#endif  // DCHECK_IS_ON()

  // Determine the parent as soon as possible.
  // Every AXObject must have a parent unless it's the root.
  SetParent(parent);
  DCHECK(parent_ || IsRoot())
      << "The following node should have a parent: " << GetNode();

  // The parent cannot have children. This object must be destroyed.
  DCHECK(!parent_ || parent_->CanHaveChildren())
      << "Tried to set a parent that cannot have children:"
      << "\n* Parent = " << parent_->ToString(true, true)
      << "\n* Child = " << ToString(true, true);

  children_dirty_ = true;

  UpdateCachedAttributeValuesIfNeeded(false);

  DCHECK(GetDocument()) << "All AXObjects must have a document: "
                        << ToString(true, true);

  // Set the dirty bit for the root AX object when created. For all other
  // objects, this is set by a descendant needing to be updated, and
  // AXObjectCacheImpl::UpdateTreeIfNeeded will therefore process an object
  // if its parent has has_dirty_descendants_ set. The root, however, has no
  // parent, so there is no parent to mark in order to cause the root to update
  // itself. Therefore this bit serves a second purpose of determining
  // whether AXObjectCacheImpl::UpdateTreeIfNeeded needs to update the root
  // object.
  if (IsRoot()) {
    SetHasDirtyDescendants(true);
  } else if (AXObjectCache().UpdatingTree()) {
    if (children_dirty_ && LastKnownIsIncludedInTreeValue()) {
      // If we're in the updating tree loop, and a new object is created, we
      // need to make sure to fill out all descendants of the new object.
      SetHasDirtyDescendants(true);
      // We also need to tell the new object's parent to iterate through
      // all of its children to look for the has dirty descendants flag.
      // However, we do not set the flag on higher ancestors since
      // they have already been walked by the tree update loop.
      if (AXObject* ax_included_parent = ParentObjectIncludedInTree()) {
        ax_included_parent->SetHasDirtyDescendants(true);
      }
    }
  } else {
    DCHECK(ParentObjectIncludedInTree()->HasDirtyDescendants())
        << "When adding a new child to a parent, and not updating the entire "
           "tree from the top down, the included parent must be "
           "flagged as having dirty descendants:"
        << "\n* Object: " << ToString(true, true) << "* Included parent: "
        << ParentObjectIncludedInTree()->ToString(true, true);
  }
}

void AXObject::Detach() {
  // Prevents LastKnown*() methods from returning the wrong values.
  cached_is_ignored_ = true;
  cached_is_ignored_but_included_in_tree_ = false;

  if (IsDetached()) {
    // Only mock objects can end up being detached twice, because their owner
    // may have needed to detach them when they were detached, but couldn't
    // remove them from the object cache yet.
    DCHECK(IsMockObject()) << "Object detached twice: " << RoleValue();
    return;
  }

#if defined(AX_FAIL_FAST_BUILD)
  SANITIZER_CHECK(ax_object_cache_);
  SANITIZER_CHECK(!ax_object_cache_->IsFrozen())
      << "Do not detach children while the tree is frozen, in order to avoid "
         "an object detaching itself in the middle of computing its own "
         "accessibility properties.";
  SANITIZER_CHECK(!is_adding_children_) << ToString(true, true);
#endif

#if !defined(NDEBUG)
  // Facilitates debugging of detached objects by providing info on what it was.
  if (!ax_object_cache_->HasBeenDisposed()) {
    detached_object_debug_info_ = ToString(true, true);
  }
#endif

  // Clear any children and call DetachFromParent() on them so that
  // no children are left with dangling pointers to their parent.
  ClearChildren();

  parent_ = nullptr;
  ax_object_cache_ = nullptr;
  children_dirty_ = false;
  has_dirty_descendants_ = false;
  id_ = 0;
}

bool AXObject::IsDetached() const {
  return !ax_object_cache_;
}

bool AXObject::IsRoot() const {
  return GetNode() && GetNode() == &AXObjectCache().GetDocument();
}

void AXObject::SetParent(AXObject* new_parent) const {
// TODO(crbug.com/1353205): Re-enable DCHECK for all platforms.
#if DCHECK_IS_ON() && !BUILDFLAG(IS_CHROMEOS_ASH)
  if (!new_parent && !IsRoot()) {
    std::ostringstream message;
    message << "Parent cannot be null, except at the root."
            << "\nThis: " << ToString(true, true)
            << "\nDOM parent chain , starting at |this->GetNode()|:";
    int count = 0;
    for (Node* node = GetNode(); node;
         node = GetParentNodeForComputeParent(AXObjectCache(), node)) {
      message << "\n"
              << (++count) << ". " << node
              << "\n  LayoutObject=" << node->GetLayoutObject();
      if (AXObject* obj = AXObjectCache().Get(node))
        message << "\n  " << obj->ToString(true, true);
      if (!node->isConnected()) {
        break;
      }
    }
    NOTREACHED() << message.str();
  }

  if (new_parent) {
    DCHECK(!new_parent->IsDetached())
        << "Cannot set parent to a detached object:"
        << "\n* Child: " << ToString(true, true)
        << "\n* New parent: " << new_parent->ToString(true, true);

    DCHECK(!IsAXInlineTextBox() ||
           ui::CanHaveInlineTextBoxChildren(new_parent->RoleValue()))
        << "Unexpected parent of inline text box: " << new_parent->RoleValue();
  }

  // Check to ensure that if the parent is changing from a previous parent,
  // that |this| is not still a child of that one.
  // This is similar to the IsParentUnignoredOf() check in
  // BlinkAXTreeSource, but closer to where the problem would occur.
  if (parent_ && new_parent != parent_ && !parent_->NeedsToUpdateChildren() &&
      !parent_->IsDetached()) {
    for (const auto& child : parent_->ChildrenIncludingIgnored()) {
      DCHECK(child != this) << "Previous parent still has |this| child:\n"
                            << ToString(true, true) << " should be a child of "
                            << new_parent->ToString(true, true) << " not of "
                            << parent_->ToString(true, true);
    }
    // TODO(accessibility) This should not be reached unless this method is
    // called on an AXObject of role kRootWebArea or when the parent's
    // children are dirty, aka parent_->NeedsToUpdateChildren());
    // Ideally we will also ensure |this| is in the parent's children now, so
    // that ClearChildren() can later find the child to detach from the parent.
  }

#endif
  parent_ = new_parent;
  // TODO(accessibility) Is it necessary to do this while updating the tree?
  if (AXObjectCache().UpdatingTree()) {
    // If updating tree, tell the newly included parent to iterate through
    // all of its children to look for the has dirty descendants flag.
    // However, we do not set the flag on higher ancestors since
    // they have already been walked by the tree update loop.
    if (AXObject* ax_included_parent = ParentObjectIncludedInTree()) {
      ax_included_parent->SetHasDirtyDescendants(true);
    }
  } else {
    SetAncestorsHaveDirtyDescendants();
  }
}

bool AXObject::IsMissingParent() const {
  if (!parent_) {
    // Do not attempt to repair the ParentObject() of a validation message
    // object, because hidden ones are purposely kept around without being in
    // the tree, and without a parent, for potential later reuse.
    // TODO(accessibility) This is ugly. Consider destroying validation message
    // objects between uses instead. See GetOrCreateValidationMessageObject().
    return !IsRoot() && !IsValidationMessage();
  }

  if (parent_->IsDetached())
    return true;

  return false;
}

void AXObject::RepairMissingParent() const {
  DCHECK(IsMissingParent());
  DCHECK(!AXObjectCache().HasBeenDisposed());

  SetParent(ComputeParent());

  SANITIZER_CHECK(!parent_ ||
                  parent_->RoleValue() != ax::mojom::blink::Role::kIframe ||
                  RoleValue() == ax::mojom::blink::Role::kDocument)
      << "An iframe can only have a document child."
      << "\n* Child = " << ToString(true, true)
      << "\n* Parent =  " << parent_->ToString(true, true);
}

// In many cases, ComputeParent() is not called, because the parent adding
// the parent adding the child will pass itself into AXObjectCacheImpl.
// ComputeParent() is still necessary because some parts of the code,
// especially web tests, result in AXObjects being created in the middle of
// the tree before their parents are created.
// TODO(accessibility) Consider forcing all ax objects to be created from
// the top down, eliminating the need for ComputeParent().
AXObject* AXObject::ComputeParent() const {
  AXObject* ax_parent = ComputeParentOrNull();

  CHECK(!ax_parent || !ax_parent->IsDetached())
      << "Computed parent should never be detached:"
      << "\n* Child: " << GetNode()
      << "\n* Parent: " << ax_parent->ToString(true, true);

  return ax_parent;
}

// Same as ComputeParent, but without the extra check for valid parent in the
// end. This is for use in RestoreParentOrPrune.
AXObject* AXObject::ComputeParentOrNull() const {
#if defined(AX_FAIL_FAST_BUILD)
  SANITIZER_CHECK(!IsDetached());

  SANITIZER_CHECK(!IsMockObject())
      << "A mock object must have a parent, and cannot exist without one. "
         "The parent is set when the object is constructed.";

  SANITIZER_CHECK(GetNode() || GetLayoutObject() || IsVirtualObject())
      << "Can't compute parent on AXObjects without a backing Node "
         "LayoutObject, "
         " or AccessibleNode. Objects without those must set the "
         "parent in Init(), |this| = "
      << RoleValue();
#endif

  AXObject* ax_parent = nullptr;
  if (IsAXInlineTextBox()) {
    NOTREACHED()
        << "AXInlineTextBox box tried to compute a new parent, but they are "
           "not allowed to exist even temporarily without a parent, as their "
           "existence depends on the parent text object. Parent text = "
        << (AXObjectCache().SafeGet(GetNode())
                ? AXObjectCache().SafeGet(GetNode())->ToString(true, true)
                : "");
  } else if (AXObjectCache().IsAriaOwned(this)) {
    ax_parent = AXObjectCache().ValidatedAriaOwner(this);
  } else if (IsVirtualObject()) {
    ax_parent =
        ComputeAccessibleNodeParent(AXObjectCache(), *GetAccessibleNode());
  }
  if (!ax_parent) {
    ax_parent = ComputeNonARIAParent(AXObjectCache(), GetNode());
  }

  return ax_parent;
}

// static
Node* AXObject::GetParentNodeForComputeParent(AXObjectCacheImpl& cache,
                                              Node* node) {
  if (!node) {
    return nullptr;
  }

  DCHECK(node->isConnected())
      << "Should not call with disconnected node: " << node;

  // A document's parent should be the page popup owner, if any, otherwise null.
  if (auto* document = DynamicTo<Document>(node)) {
    LocalFrame* frame = document->GetFrame();
    DCHECK(frame);
    Node* popup_owner = frame->PagePopupOwner();
    if (!popup_owner) {
      return nullptr;
    }
    // TODO(accessibility) Remove this rule once we stop using AXMenuList*.
    if (IsA<HTMLSelectElement>(popup_owner) &&
        AXObjectCacheImpl::ShouldCreateAXMenuListFor(
            popup_owner->GetLayoutObject())) {
      return nullptr;
    }
    return popup_owner;
  }

  // Use LayoutTreeBuilderTraversal::Parent(), which handles pseudo content.
  // This can return nullptr for a node that is never visited by
  // LayoutTreeBuilderTraversal's child traversal. For example, while an element
  // can be appended as a <textarea>'s child, it is never visited by
  // LayoutTreeBuilderTraversal's child traversal. Therefore, returning null in
  // this case is appropriate, because that child content is not attached to any
  // parent as far as rendering or accessibility are concerned.
  // Whenever null is returned from this function, then a parent cannot be
  // computed, and when a parent is not provided or computed, the accessible
  // object will not be created.
  Node* parent = LayoutTreeBuilderTraversal::Parent(*node);
  if (!parent) {
    return nullptr;
  }

  // Descendants of pseudo elements must only be created by walking the tree via
  // AXNodeObject::AddChildren(), which already knows the parent. Therefore, the
  // parent must not be computed. This helps avoid situations with certain
  // elements where there is asymmetry between what considers this a child vs
  // what the this considers its parent. An example of this kind of situation is
  // a ::first-letter within a ::before.
  if (node->GetLayoutObject() && node->GetLayoutObject()->Parent() &&
      node->GetLayoutObject()->Parent()->IsPseudoElement()) {
    return nullptr;
  }

  HTMLMapElement* map_element = DynamicTo<HTMLMapElement>(parent);
  if (map_element) {
    // For a <map>, return the <img> associated with it. This is necessary
    // because the AX tree is flat, adding image map children as children of the
    // <img>, whereas in the DOM they are actually children of the <map>.
    // Therefore, if a node is a DOM child of a map, its AX parent is the image.
    // This code double checks that the image actually uses the map.
    HTMLImageElement* image_element = map_element->ImageElement();
    return AXObject::GetMapForImage(image_element) == map_element
               ? image_element
               : nullptr;
  }

  return CanComputeAsNaturalParent(parent) ? parent : nullptr;
}

// static
bool AXObject::CanComputeAsNaturalParent(Node* node) {
  if (IsA<Document>(node)) {
    return true;
  }

  DCHECK(IsA<Element>(node)) << "Expected element: " << node;

  // When the flag to use AXMenuList in on, a menu list is only allowed to
  // parent an AXMenuListPopup, which is added as a child on creation. No other
  // children are allowed, and false is returned for anything else where the
  // parent would be AXMenuList.
  if (AXObjectCacheImpl::ShouldCreateAXMenuListFor(node->GetLayoutObject())) {
    return false;
  }

  // An image cannot be the natural DOM parent of another AXObject, it can only
  // have <area> children, which are from another part of the DOM tree.
  if (IsA<HTMLImageElement>(node)) {
    return false;
  }

  return CanHaveChildren(*To<Element>(node));
}

// static
bool AXObject::CanHaveChildren(Element& element) {
  // Image map parent-child relationships work as follows:
  // - The image is the parent
  // - The DOM children of the associated <map> are the children
  // This is accomplished by having GetParentNodeForComputeParent() return the
  // <img> instead of the <map> for the map's children.
  if (IsA<HTMLMapElement>(element)) {
    return false;
  }

  if (IsA<HTMLImageElement>(element)) {
    return GetMapForImage(&element);
  }

  // Placeholder gets exposed as an attribute on the input accessibility node,
  // so there's no need to add its text children. Placeholder text is a separate
  // node that gets removed when it disappears, so this will only be present if
  // the placeholder is visible.
  if (Element* host = element.OwnerShadowHost()) {
    if (auto* ancestor_input = DynamicTo<TextControlElement>(host)) {
      if (ancestor_input->PlaceholderElement() == &element) {
        // |element| is a placeholder.
        return false;
      }
    }
  }

  if (IsA<HTMLBRElement>(element)) {
    // Normally, a <br> is allowed to have a single inline text box child.
    // However, a <br> element that has DOM children can occur only if a script
    // adds the children, and Blink will not render those children. This is an
    // obscure edge case that should only occur during fuzzing, but to maintain
    // tree consistency and prevent DCHECKs, AXObjects for <br> elements are not
    // allowed to have children if there are any DOM children at all.
    return !element.hasChildren();
  }

  if (IsA<HTMLHRElement>(element)) {
    return false;
  }

  if (auto* input = DynamicTo<HTMLInputElement>(&element)) {
    // False for checkbox, radio and range.
    return !input->IsCheckable() && input->type() != input_type_names::kRange;
  }

  if (IsA<HTMLOptionElement>(element)) {
    return false;
  }

  if (IsA<HTMLProgressElement>(element)) {
    return false;
  }

  return true;
}

// static
AXObject* AXObject::ComputeAccessibleNodeParent(
    AXObjectCacheImpl& cache,
    AccessibleNode& accessible_node) {
  if (AccessibleNode* parent_accessible_node = accessible_node.GetParent()) {
    if (AXObject* parent = cache.Get(parent_accessible_node))
      return parent;

    // Compute grandparent first, since constructing parent AXObject for
    // |accessible_node| requires grandparent to be provided.
    AXObject* grandparent_object =
        AXObject::ComputeAccessibleNodeParent(cache, *parent_accessible_node);

    if (grandparent_object)
      return cache.GetOrCreate(parent_accessible_node, grandparent_object);
  }

  return nullptr;
}

// static
HTMLMapElement* AXObject::GetMapForImage(Node* image) {
  if (!IsA<HTMLImageElement>(image))
    return nullptr;

  LayoutImage* layout_image = DynamicTo<LayoutImage>(image->GetLayoutObject());
  if (!layout_image)
    return nullptr;

  HTMLMapElement* map_element = layout_image->ImageMap();
  if (!map_element)
    return nullptr;

  // Don't allow images that are actually children of a map, as this could lead
  // to an infinite loop, where the descendant image points to the ancestor map,
  // yet the descendant image is being returned here as an ancestor.
  if (Traversal<HTMLMapElement>::FirstAncestor(*image))
    return nullptr;

  // The image has an associated <map> and does not have a <map> ancestor.
  return map_element;
}

// static
AXObject* AXObject::ComputeNonARIAParent(AXObjectCacheImpl& cache,
                                         Node* current_node) {
  if (!current_node) {
    return nullptr;
  }

  // For <option> in <select size=1>, return the popup.
  if (AXObjectCacheImpl::UseAXMenuList()) {
    if (auto* option = DynamicTo<HTMLOptionElement>(current_node)) {
      if (AXObject* ax_select =
              AXMenuListOption::ComputeParentAXMenuPopupFor(cache, option)) {
        return ax_select;
      }
    }
  }

  Node* parent_node = GetParentNodeForComputeParent(cache, current_node);

  // Will not create an object if no valid parent node is found. This occurs
  // when a DOM child isn't visited by LayoutTreeBuilderTraversal, such as an
  // element child of a <textarea>, which only supports plain text.
  return cache.GetOrCreate(parent_node);
}

#if DCHECK_IS_ON()
void AXObject::EnsureCorrectParentComputation() {
  if (!parent_)
    return;

  DCHECK(!parent_->IsDetached());

  DCHECK(parent_->CanHaveChildren());

  // Don't check the computed parent if the cached parent is a mock object.
  // It is expected that a computed parent could never be a mock object,
  // which has no backing DOM node or layout object, and therefore cannot be
  // found by traversing DOM/layout ancestors.
  if (parent_->IsMockObject())
    return;

  // Cannot compute a parent for an object that has no backing node or layout
  // object to start from.
  if (!GetNode() || !GetLayoutObject())
    return;

  // Don't check the computed parent if the cached parent is an image:
  // <area> children's location in the DOM and HTML hierarchy does not match.
  // TODO(aleventhal) Try to remove this rule, it may be unnecessary now.
  if (parent_->RoleValue() == ax::mojom::blink::Role::kImage)
    return;

  // TODO(aleventhal) Different in test fast/css/first-letter-removed-added.html
  // when run with --force-renderer-accessibility.
  if (GetNode() && GetNode()->IsPseudoElement())
    return;

  // Verify that the algorithm in ComputeParent() provides same results as
  // parents that init their children with themselves as the parent.
  // Inconsistency indicates a problem could potentially exist where a child's
  // parent does not include the child in its children.
  AXObject* computed_parent = ComputeParent();

  DCHECK(computed_parent) << "Computed parent was null for " << this
                          << ", expected " << parent_;
  DCHECK_EQ(computed_parent, parent_)
      << "\n**** ComputeParent should have provided the same result as "
         "the known parent.\n**** Computed parent layout object was "
      << computed_parent->GetLayoutObject()
      << "\n**** Actual parent's layout object was "
      << parent_->GetLayoutObject() << "\n**** Child was " << this;
}

std::string AXObject::GetAXTreeForThis() const {
  return TreeToStringWithMarkedObjectHelper(AXObjectCache().Root(), this);
}

void AXObject::ShowAXTreeForThis() const {
  DLOG(INFO) << "\n" << GetAXTreeForThis();
}

#endif

const AtomicString& AXObject::GetAOMPropertyOrARIAAttribute(
    AOMStringProperty property) const {
  Element* element = GetElement();
  if (!element)
    return g_null_atom;

  return AccessibleNode::GetPropertyOrARIAAttribute(element, property);
}

Element* AXObject::GetAOMPropertyOrARIAAttribute(
    AOMRelationProperty property) const {
  Element* element = GetElement();
  if (!element)
    return nullptr;

  return AccessibleNode::GetPropertyOrARIAAttribute(element, property);
}

bool AXObject::HasAOMProperty(AOMRelationListProperty property,
                              HeapVector<Member<Element>>& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  return AccessibleNode::GetProperty(element, property, result);
}

bool AXObject::HasAOMPropertyOrARIAAttribute(
    AOMRelationListProperty property,
    HeapVector<Member<Element>>& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  return AccessibleNode::GetPropertyOrARIAAttribute(element, property, result);
}

bool AXObject::HasAOMPropertyOrARIAAttribute(AOMBooleanProperty property,
                                             bool& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  bool is_null = true;
  result =
      AccessibleNode::GetPropertyOrARIAAttribute(element, property, is_null);
  return !is_null;
}

bool AXObject::AOMPropertyOrARIAAttributeIsTrue(
    AOMBooleanProperty property) const {
  bool result;
  if (HasAOMPropertyOrARIAAttribute(property, result))
    return result;
  return false;
}

bool AXObject::AOMPropertyOrARIAAttributeIsFalse(
    AOMBooleanProperty property) const {
  bool result;
  if (HasAOMPropertyOrARIAAttribute(property, result))
    return !result;
  return false;
}

bool AXObject::HasAOMPropertyOrARIAAttribute(AOMUIntProperty property,
                                             uint32_t& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  bool is_null = true;
  result =
      AccessibleNode::GetPropertyOrARIAAttribute(element, property, is_null);
  return !is_null;
}

bool AXObject::HasAOMPropertyOrARIAAttribute(AOMIntProperty property,
                                             int32_t& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  bool is_null = true;
  result =
      AccessibleNode::GetPropertyOrARIAAttribute(element, property, is_null);
  return !is_null;
}

bool AXObject::HasAOMPropertyOrARIAAttribute(AOMFloatProperty property,
                                             float& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  bool is_null = true;
  result =
      AccessibleNode::GetPropertyOrARIAAttribute(element, property, is_null);
  return !is_null;
}

bool AXObject::HasAOMPropertyOrARIAAttribute(AOMStringProperty property,
                                             AtomicString& result) const {
  Element* element = GetElement();
  if (!element)
    return false;

  result = AccessibleNode::GetPropertyOrARIAAttribute(element, property);
  return !result.IsNull();
}

AccessibleNode* AXObject::GetAccessibleNode() const {
  Element* element = GetElement();
  if (!element)
    return nullptr;

  return element->ExistingAccessibleNode();
}

void AXObject::Serialize(ui::AXNodeData* node_data,
                         ui::AXMode accessibility_mode) {
  // Reduce redundant ancestor chain walking for display lock computations.
  auto memoization_scope =
      DisplayLockUtilities::CreateLockCheckMemoizationScope();

  node_data->role = ComputeFinalRoleForSerialization();
  node_data->id = AXObjectID();

  PreSerializationConsistencyCheck();

  // Serialize a few things that we need even for ignored nodes.
  bool is_focusable = CanSetFocusAttribute();
  if (is_focusable)
    node_data->AddState(ax::mojom::blink::State::kFocusable);

  bool is_visible = IsVisible();
  if (!is_visible)
    node_data->AddState(ax::mojom::blink::State::kInvisible);

  if (is_visible || is_focusable) {
    // If the author applied the ARIA "textbox" role on something that is not
    // (currently) editable, this may be a read-only rich-text object. Or it
    // might just be bad authoring. Either way, we want to expose its
    // descendants, especially the interactive ones which might gain focus.
    bool is_non_atomic_textfield_root = IsARIATextField();

    // Preserve continuity in subtrees of richly editable content by including
    // richlyEditable state even if ignored.
    if (IsEditable()) {
      node_data->AddState(ax::mojom::blink::State::kEditable);
      if (!is_non_atomic_textfield_root)
        is_non_atomic_textfield_root = IsEditableRoot();

      if (IsRichlyEditable())
        node_data->AddState(ax::mojom::blink::State::kRichlyEditable);
    }
    if (is_non_atomic_textfield_root) {
      node_data->AddBoolAttribute(
          ax::mojom::blink::BoolAttribute::kNonAtomicTextFieldRoot, true);
    }
  }

  if (accessibility_mode.has_mode(ui::AXMode::kHTML))
    SerializeHTMLTagAndClass(node_data);  // Used for test readability.

  if (accessibility_mode.has_mode(ui::AXMode::kScreenReader))
    SerializeColorAttributes(node_data);  // Blends using all nodes' values.

  if (accessibility_mode.has_mode(ui::AXMode::kScreenReader) ||
      accessibility_mode.has_mode(ui::AXMode::kPDF)) {
    SerializeLangAttribute(node_data);  // Propagates using all nodes' values.
  }

  // Always try to serialize child tree ids.
  SerializeChildTreeID(node_data);

  if (!accessibility_mode.has_mode(ui::AXMode::kPDF))
    SerializeBoundingBoxAttributes(*node_data);

  // Return early. The following attributes are unnecessary for ignored nodes.
  // Exception: focusable ignored nodes are fully serialized, so that reasonable
  // verbalizations can be made if they actually receive focus.
  if (AccessibilityIsIgnored()) {
    node_data->AddState(ax::mojom::blink::State::kIgnored);
    // Early return for ignored, unfocusable nodes, avoiding unnecessary work.
    if (!is_focusable &&
        !RuntimeEnabledFeatures::AccessibilityExposeIgnoredNodesEnabled()) {
      // The name is important for exposing the selection around ignored nodes.
      // TODO(accessibility) Remove this and still pass this
      // content_browsertest:
      // All/DumpAccessibilityTreeTest.AccessibilityIgnoredSelection/blink
      if (RoleValue() == ax::mojom::blink::Role::kStaticText)
        SerializeNameAndDescriptionAttributes(accessibility_mode, node_data);
      return;
    }
  }

  if (accessibility_mode.has_mode(ui::AXMode::kScreenReader))
    SerializeScreenReaderAttributes(node_data);

  SerializeUnignoredAttributes(node_data, accessibility_mode);

  if (accessibility_mode.has_mode(ui::AXMode::kPDF)) {
    SerializeNameAndDescriptionAttributes(accessibility_mode, node_data);
    // Return early. None of the following attributes are needed for PDFs.
    return;
  }

  SerializeNameAndDescriptionAttributes(accessibility_mode, node_data);

  if (!accessibility_mode.has_mode(ui::AXMode::kScreenReader))
    return;

  if (LiveRegionRoot())
    SerializeLiveRegionAttributes(node_data);

  SerializeOtherScreenReaderAttributes(node_data);

  // Return early. The following attributes are unnecessary for ignored nodes.
  // Exception: focusable ignored nodes are fully serialized, so that reasonable
  // verbalizations can be made if they actually receive focus.
  if (AccessibilityIsIgnored() &&
      !node_data->HasState(ax::mojom::blink::State::kFocusable)) {
    return;
  }
}

void AXObject::SerializeBoundingBoxAttributes(ui::AXNodeData& dst) const {
  bool clips_children = false;
  PopulateAXRelativeBounds(dst.relative_bounds, &clips_children);
  if (clips_children) {
    dst.AddBoolAttribute(ax::mojom::blink::BoolAttribute::kClipsChildren, true);
  }

  if (IsLineBreakingObject()) {
    dst.AddBoolAttribute(ax::mojom::blink::BoolAttribute::kIsLineBreakingObject,
                         true);
  }
  AXObjectCache().SetCachedBoundingBox(AXObjectID(), dst.relative_bounds);
}

static bool AXShouldIncludePageScaleFactorInRoot() {
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
  return true;
#else
  return false;
#endif
}

void AXObject::PopulateAXRelativeBounds(ui::AXRelativeBounds& bounds,
                                        bool* clips_children) const {
  AXObject* offset_container;
  gfx::RectF bounds_in_container;
  gfx::Transform container_transform;
  GetRelativeBounds(&offset_container, bounds_in_container, container_transform,
                    clips_children);
  bounds.bounds = bounds_in_container;
  if (offset_container && !offset_container->IsDetached())
    bounds.offset_container_id = offset_container->AXObjectID();

  if (AXShouldIncludePageScaleFactorInRoot() && IsRoot()) {
    const Page* page = GetDocument()->GetPage();
    container_transform.Scale(page->PageScaleFactor(), page->PageScaleFactor());
    container_transform.Translate(
        -page->GetVisualViewport().VisibleRect().origin().OffsetFromOrigin());
  }

  if (!container_transform.IsIdentity())
    bounds.transform = std::make_unique<gfx::Transform>(container_transform);
}

void AXObject::MarkAllImageAXObjectsDirty() {
  if (RoleValue() == ax::mojom::blink::Role::kImage) {
    AXObjectCache().MarkAXObjectDirtyWithCleanLayoutAndEvent(
        this, ax::mojom::blink::EventFrom::kNone,
        ax::mojom::Action::kAnnotatePageImages);
  }

  for (auto& child : UnignoredChildren())
    child->MarkAllImageAXObjectsDirty();
}

void AXObject::SerializeActionAttributes(ui::AXNodeData* node_data) {
  if (CanSetValueAttribute())
    node_data->AddAction(ax::mojom::blink::Action::kSetValue);
  if (IsSlider()) {
    node_data->AddAction(ax::mojom::blink::Action::kDecrement);
    node_data->AddAction(ax::mojom::blink::Action::kIncrement);
  }
  if (IsUserScrollable()) {
    node_data->AddAction(ax::mojom::blink::Action::kScrollUp);
    node_data->AddAction(ax::mojom::blink::Action::kScrollDown);
    node_data->AddAction(ax::mojom::blink::Action::kScrollLeft);
    node_data->AddAction(ax::mojom::blink::Action::kScrollRight);
    node_data->AddAction(ax::mojom::blink::Action::kScrollForward);
    node_data->AddAction(ax::mojom::blink::Action::kScrollBackward);
  }
}

void AXObject::SerializeChildTreeID(ui::AXNodeData* node_data) {
  // If this is an HTMLFrameOwnerElement (such as an iframe), we may need
  // to embed the ID of the child frame.
  if (!IsChildTreeOwner()) {
    // TODO(crbug.com/1342603) Determine why these are firing in the wild and,
    // once fixed, turn into a DCHECK.
    SANITIZER_CHECK(!IsFrame(GetNode()))
        << "If this is an iframe, it should also be a child tree owner: "
        << ToString(true, true);
    return;
  }

  // Do not attach hidden child trees.
  if (!IsVisible()) {
    return;
  }

  auto* html_frame_owner_element = To<HTMLFrameOwnerElement>(GetElement());

  Frame* child_frame = html_frame_owner_element->ContentFrame();
  if (!child_frame) {
    // TODO(crbug.com/1342603) Determine why these are firing in the wild and,
    // once fixed, turn into a DCHECK.
    SANITIZER_CHECK(IsDisabled()) << ToString(true, true);
    return;
  }

  absl::optional<base::UnguessableToken> child_token =
      child_frame->GetEmbeddingToken();
  if (!child_token)
    return;  // No child token means that the connection isn't ready yet.

  DCHECK_EQ(ChildCountIncludingIgnored(), 0)
      << "Children won't exist until the trees are stitched together in the "
         "browser process. A failure means that a child node was incorrectly "
         "considered relevant by AXObjectCacheImpl."
      << "\n* Parent: " << ToString(true)
      << "\n* Frame owner: " << IsA<HTMLFrameOwnerElement>(GetNode())
      << "\n* Element src: " << GetAttribute(html_names::kSrcAttr)
      << "\n* First child: " << FirstChildIncludingIgnored()->ToString(true);

  ui::AXTreeID child_tree_id = ui::AXTreeID::FromToken(child_token.value());
  node_data->AddChildTreeId(child_tree_id);
}

void AXObject::SerializeChooserPopupAttributes(ui::AXNodeData* node_data) {
  AXObject* chooser_popup = ChooserPopup();
  if (!chooser_popup)
    return;

  int32_t chooser_popup_id = chooser_popup->AXObjectID();
  auto controls_ids = node_data->GetIntListAttribute(
      ax::mojom::blink::IntListAttribute::kControlsIds);
  controls_ids.push_back(chooser_popup_id);
  node_data->AddIntListAttribute(
      ax::mojom::blink::IntListAttribute::kControlsIds, controls_ids);
}

void AXObject::SerializeColorAttributes(ui::AXNodeData* node_data) {
  // Text attributes.
  if (RGBA32 bg_color = BackgroundColor()) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kBackgroundColor,
                               bg_color);
  }

  if (RGBA32 color = GetColor())
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kColor, color);
}

void AXObject::SerializeElementAttributes(ui::AXNodeData* node_data) {
  Element* element = GetElement();
  if (!element)
    return;

  if (const AtomicString& class_name = element->GetClassAttribute()) {
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kClassName, class_name);
  }

  // Expose StringAttribute::kRole, which is used for the xml-roles object
  // attribute. Prefer the raw ARIA role attribute value, otherwise, the ARIA
  // equivalent role is used, if it is a role that is exposed in xml-roles.
  const AtomicString& role_str =
      GetRoleAttributeStringForObjectAttribute(node_data);
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kRole, role_str);
}

void AXObject::SerializeHTMLTagAndClass(ui::AXNodeData* node_data) {
  Element* element = GetElement();
  if (!element) {
    if (IsA<Document>(GetNode())) {
      TruncateAndAddStringAttribute(
          node_data, ax::mojom::blink::StringAttribute::kHtmlTag, "#document");
    }
    return;
  }

  TruncateAndAddStringAttribute(node_data,
                                ax::mojom::blink::StringAttribute::kHtmlTag,
                                element->tagName().LowerASCII());

  if (const AtomicString& class_name = element->GetClassAttribute()) {
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kClassName, class_name);
  }
}

void AXObject::SerializeHTMLAttributes(ui::AXNodeData* node_data) {
  Element* element = GetElement();
  DCHECK(element);
  for (const Attribute& attr : element->Attributes()) {
    std::string name = attr.LocalName().LowerASCII().Utf8();
    if (name == "class") {  // class already in kClassName
      continue;
    }
    std::string value = attr.Value().Utf8();
    node_data->html_attributes.push_back(std::make_pair(name, value));
  }

// TODO(nektar): Turn off kHTMLAccessibilityMode for automation and Mac
// and remove ifdef.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
  if (node_data->role == ax::mojom::blink::Role::kMath ||
      node_data->role == ax::mojom::blink::Role::kMathMLMath) {
    TruncateAndAddStringAttribute(node_data,
                                  ax::mojom::blink::StringAttribute::kInnerHtml,
                                  element->innerHTML(), kMaxStaticTextLength);
  }
#endif
}

void AXObject::SerializeInlineTextBoxAttributes(
    ui::AXNodeData* node_data) const {
  DCHECK_EQ(ax::mojom::blink::Role::kInlineTextBox, node_data->role);

  Vector<int> character_offsets;
  TextCharacterOffsets(character_offsets);
  AddIntListAttributeFromOffsetVector(
      ax::mojom::blink::IntListAttribute::kCharacterOffsets, character_offsets,
      node_data);

  Vector<int> word_starts;
  Vector<int> word_ends;
  GetWordBoundaries(word_starts, word_ends);
  AddIntListAttributeFromOffsetVector(
      ax::mojom::blink::IntListAttribute::kWordStarts, word_starts, node_data);
  AddIntListAttributeFromOffsetVector(
      ax::mojom::blink::IntListAttribute::kWordEnds, word_ends, node_data);
}

void AXObject::SerializeLangAttribute(ui::AXNodeData* node_data) {
  AXObject* parent = ParentObject();
  if (Language().length()) {
    // TODO(chrishall): should we still trim redundant languages off here?
    if (!parent || parent->Language() != Language()) {
      TruncateAndAddStringAttribute(
          node_data, ax::mojom::blink::StringAttribute::kLanguage, Language());
    }
  }
}

void AXObject::SerializeListAttributes(ui::AXNodeData* node_data) {
  if (SetSize()) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kSetSize,
                               SetSize());
  }

  if (PosInSet()) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kPosInSet,
                               PosInSet());
  }
}

void AXObject::SerializeListMarkerAttributes(ui::AXNodeData* node_data) const {
  DCHECK_EQ(ax::mojom::blink::Role::kListMarker, node_data->role);

  Vector<int> word_starts;
  Vector<int> word_ends;
  GetWordBoundaries(word_starts, word_ends);
  AddIntListAttributeFromOffsetVector(
      ax::mojom::blink::IntListAttribute::kWordStarts, word_starts, node_data);
  AddIntListAttributeFromOffsetVector(
      ax::mojom::blink::IntListAttribute::kWordEnds, word_ends, node_data);
}

void AXObject::SerializeLiveRegionAttributes(ui::AXNodeData* node_data) const {
  DCHECK(LiveRegionRoot());

  node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kLiveAtomic,
                              LiveRegionAtomic());
  TruncateAndAddStringAttribute(node_data,
                                ax::mojom::blink::StringAttribute::kLiveStatus,
                                LiveRegionStatus());
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kLiveRelevant,
      LiveRegionRelevant());
  // If we are not at the root of an atomic live region.
  if (ContainerLiveRegionAtomic() && !LiveRegionRoot()->IsDetached() &&
      !LiveRegionAtomic()) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kMemberOfId,
                               LiveRegionRoot()->AXObjectID());
  }
  node_data->AddBoolAttribute(
      ax::mojom::blink::BoolAttribute::kContainerLiveAtomic,
      ContainerLiveRegionAtomic());
  node_data->AddBoolAttribute(
      ax::mojom::blink::BoolAttribute::kContainerLiveBusy,
      ContainerLiveRegionBusy());
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kContainerLiveStatus,
      ContainerLiveRegionStatus());
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kContainerLiveRelevant,
      ContainerLiveRegionRelevant());
}

void AXObject::SerializeNameAndDescriptionAttributes(
    ui::AXMode accessibility_mode,
    ui::AXNodeData* node_data) const {
  ax::mojom::blink::NameFrom name_from;
  AXObjectVector name_objects;
  String name = GetName(name_from, &name_objects);
  if (name_from == ax::mojom::blink::NameFrom::kAttributeExplicitlyEmpty) {
    node_data->AddStringAttribute(ax::mojom::blink::StringAttribute::kName,
                                  std::string());
    node_data->SetNameFrom(
        ax::mojom::blink::NameFrom::kAttributeExplicitlyEmpty);
  } else if (!name.empty()) {
    int max_length = node_data->role == ax::mojom::blink::Role::kStaticText
                         ? kMaxStaticTextLength
                         : kMaxStringAttributeLength;
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kName, name, max_length);
    node_data->SetNameFrom(name_from);
    AddIntListAttributeFromObjects(
        ax::mojom::blink::IntListAttribute::kLabelledbyIds, name_objects,
        node_data);
  }

  ax::mojom::blink::DescriptionFrom description_from;
  AXObjectVector description_objects;
  String description =
      Description(name_from, description_from, &description_objects);
  if (!description.empty()) {
    DCHECK(description_from != ax::mojom::blink::DescriptionFrom::kNone);
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kDescription,
        description);
    node_data->SetDescriptionFrom(description_from);
    AddIntListAttributeFromObjects(
        ax::mojom::blink::IntListAttribute::kDescribedbyIds,
        description_objects, node_data);
  }

  String title = Title(name_from);
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kTooltip, title);

  if (!accessibility_mode.has_mode(ui::AXMode::kScreenReader))
    return;

  String placeholder = Placeholder(name_from);
  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kPlaceholder, placeholder);
}

void AXObject::SerializeScreenReaderAttributes(ui::AXNodeData* node_data) {
  if (ui::IsText(RoleValue())) {
    // Don't serialize these attributes on text, where it is uninteresting.
    return;
  }
  String display_style;
  if (Node* node = GetNode(); node && !node->IsDocumentNode()) {
    if (const ComputedStyle* computed_style = node->GetComputedStyle()) {
      display_style = CSSProperty::Get(CSSPropertyID::kDisplay)
                          .CSSValueFromComputedStyle(
                              *computed_style, /* layout_object */ nullptr,
                              /* allow_visited_style */ false)
                          ->CssText();
      if (!display_style.empty()) {
        TruncateAndAddStringAttribute(
            node_data, ax::mojom::blink::StringAttribute::kDisplay,
            display_style);
      }
    }

    // Whether it has ARIA attributes at all.
    if (HasAriaAttribute()) {
      node_data->AddBoolAttribute(
          ax::mojom::blink::BoolAttribute::kHasAriaAttribute, true);
    }
  }

  if (KeyboardShortcut().length() &&
      !node_data->HasStringAttribute(
          ax::mojom::blink::StringAttribute::kKeyShortcuts)) {
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kKeyShortcuts,
        KeyboardShortcut());
  }

  if (AXObject* active_descendant = ActiveDescendant()) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kActivedescendantId,
        active_descendant->AXObjectID());
  }
}

String AXObject::KeyboardShortcut() const {
  const AtomicString& access_key = AccessKey();
  if (access_key.IsNull())
    return String();

  DEFINE_STATIC_LOCAL(String, modifier_string, ());
  if (modifier_string.IsNull()) {
    unsigned modifiers = KeyboardEventManager::kAccessKeyModifiers;
    // Follow the same order as Mozilla MSAA implementation:
    // Ctrl+Alt+Shift+Meta+key. MSDN states that keyboard shortcut strings
    // should not be localized and defines the separator as "+".
    StringBuilder modifier_string_builder;
    if (modifiers & WebInputEvent::kControlKey)
      modifier_string_builder.Append("Ctrl+");
    if (modifiers & WebInputEvent::kAltKey)
      modifier_string_builder.Append("Alt+");
    if (modifiers & WebInputEvent::kShiftKey)
      modifier_string_builder.Append("Shift+");
    if (modifiers & WebInputEvent::kMetaKey)
      modifier_string_builder.Append("Win+");
    modifier_string = modifier_string_builder.ToString();
  }

  return String(modifier_string + access_key);
}

void AXObject::SerializeOtherScreenReaderAttributes(
    ui::AXNodeData* node_data) const {
  DCHECK_NE(node_data->role, ax::mojom::blink::Role::kUnknown);
  DCHECK_NE(node_data->role, ax::mojom::blink::Role::kNone);

  if (IsA<Document>(GetNode())) {
    if (!IsLoaded()) {
      node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kBusy, true);
    }
    if (AXObject* parent = ParentObject()) {
      DCHECK(parent->ChooserPopup() == this)
          << "ChooserPopup missing for: " << parent->ToString(true);
      node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kPopupForId,
                                 parent->AXObjectID());
    }
  }

  if (node_data->role == ax::mojom::blink::Role::kColorWell) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kColorValue,
                               ColorValue());
  }

  if (node_data->role == ax::mojom::blink::Role::kLink) {
    AXObject* target = InPageLinkTarget();
    if (target) {
      int32_t target_id = target->AXObjectID();
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kInPageLinkTargetId, target_id);
    }

    // `ax::mojom::blink::StringAttribute::kLinkTarget` is only valid on <a> and
    // <area> elements. <area> elements should link to something in order to be
    // considered, see `AXImageMap::Role()`.
    TruncateAndAddStringAttribute(
        node_data, ax::mojom::blink::StringAttribute::kLinkTarget,
        EffectiveTarget());
  }

  if (node_data->role == ax::mojom::blink::Role::kRadioButton) {
    AddIntListAttributeFromObjects(
        ax::mojom::blink::IntListAttribute::kRadioGroupIds,
        RadioButtonsInGroup(), node_data);
  }

  if (GetAriaCurrentState() != ax::mojom::blink::AriaCurrentState::kNone) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kAriaCurrentState,
        static_cast<int32_t>(GetAriaCurrentState()));
  }

  if (GetInvalidState() != ax::mojom::blink::InvalidState::kNone)
    node_data->SetInvalidState(GetInvalidState());

  if (CheckedState() != ax::mojom::blink::CheckedState::kNone) {
    node_data->SetCheckedState(CheckedState());
  }

  if (node_data->role == ax::mojom::blink::Role::kInlineTextBox) {
    SerializeInlineTextBoxAttributes(node_data);
  }

  if (node_data->role == ax::mojom::blink::Role::kListMarker) {
    SerializeListMarkerAttributes(node_data);
  }

  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kAccessKey, AccessKey());

  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kAutoComplete,
      AutoComplete());

  if (Action() != ax::mojom::blink::DefaultActionVerb::kNone) {
    node_data->SetDefaultActionVerb(Action());
  }

  if (NextOnLine() && !NextOnLine()->IsDetached()) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kNextOnLineId,
                               NextOnLine()->AXObjectID());
  }

  if (PreviousOnLine() && !PreviousOnLine()->IsDetached()) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kPreviousOnLineId,
        PreviousOnLine()->AXObjectID());
  }

  AXObjectVector error_messages = ErrorMessage();
  if (error_messages.size() > 0) {
    AddIntListAttributeFromObjects(
        ax::mojom::blink::IntListAttribute::kErrormessageIds, error_messages,
        node_data);
  }

  if (ui::SupportsHierarchicalLevel(node_data->role) && HierarchicalLevel()) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kHierarchicalLevel,
        HierarchicalLevel());
  }

  if (CanvasHasFallbackContent()) {
    node_data->AddBoolAttribute(
        ax::mojom::blink::BoolAttribute::kCanvasHasFallback, true);
  }

  if (IsRangeValueSupported()) {
    float value;
    if (ValueForRange(&value)) {
      node_data->AddFloatAttribute(
          ax::mojom::blink::FloatAttribute::kValueForRange, value);
    }

    float max_value;
    if (MaxValueForRange(&max_value)) {
      node_data->AddFloatAttribute(
          ax::mojom::blink::FloatAttribute::kMaxValueForRange, max_value);
    }

    float min_value;
    if (MinValueForRange(&min_value)) {
      node_data->AddFloatAttribute(
          ax::mojom::blink::FloatAttribute::kMinValueForRange, min_value);
    }

    float step_value;
    if (StepValueForRange(&step_value)) {
      node_data->AddFloatAttribute(
          ax::mojom::blink::FloatAttribute::kStepValueForRange, step_value);
    }
  }

  if (ui::IsDialog(node_data->role)) {
    node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kModal,
                                IsModal());
  }

  // aria-dropeffect is deprecated in WAI-ARIA 1.1.
  Vector<ax::mojom::blink::Dropeffect> dropeffects;
  Dropeffects(dropeffects);
  if (!dropeffects.empty()) {
    for (auto&& dropeffect : dropeffects) {
      node_data->AddDropeffect(dropeffect);
    }
  }
}

void AXObject::SerializeScrollAttributes(ui::AXNodeData* node_data) {
  // Only mark as scrollable if user has actual scrollbars to use.
  node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kScrollable,
                              IsUserScrollable());
  // Provide x,y scroll info if scrollable in any way (programmatically or via
  // user).
  gfx::Point scroll_offset = GetScrollOffset();
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollX,
                             scroll_offset.x());
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollY,
                             scroll_offset.y());

  gfx::Point min_scroll_offset = MinimumScrollOffset();
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollXMin,
                             min_scroll_offset.x());
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollYMin,
                             min_scroll_offset.y());

  gfx::Point max_scroll_offset = MaximumScrollOffset();
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollXMax,
                             max_scroll_offset.x());
  node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kScrollYMax,
                             max_scroll_offset.y());
}

void AXObject::SerializeSparseAttributes(ui::AXNodeData* node_data) {
  if (IsVirtualObject()) {
    AccessibleNode* accessible_node = GetAccessibleNode();
    if (accessible_node) {
      AXNodeDataAOMPropertyClient property_client(*ax_object_cache_,
                                                  *node_data);
      accessible_node->GetAllAOMProperties(&property_client);
    }
  }

  Element* element = GetElement();
  if (!element)
    return;

  AXSparseAttributeSetterMap& setter_map = GetAXSparseAttributeSetterMap();
  AttributeCollection attributes = element->AttributesWithoutUpdate();
  HashSet<QualifiedName> set_attributes;
  for (const Attribute& attr : attributes) {
    set_attributes.insert(attr.GetName());
    AXSparseSetterFunc callback;
    auto it = setter_map.find(attr.GetName());
    if (it == setter_map.end())
      continue;
    it->value.Run(this, node_data, attr.Value());
  }

  if (!element->DidAttachInternals())
    return;
  const auto& internals_attributes =
      element->EnsureElementInternals().GetAttributes();
  for (const QualifiedName& attr : internals_attributes.Keys()) {
    auto it = setter_map.find(attr);
    if (set_attributes.Contains(attr) || it == setter_map.end())
      continue;
    it->value.Run(this, node_data, internals_attributes.at(attr));
  }
}

void AXObject::SerializeStyleAttributes(ui::AXNodeData* node_data) {
  // Only serialize font family if there is one, and it is different from the
  // parent. Use the value from computed style first since that is a fast lookup
  // and comparison, and serialize the user-friendly name at points in the tree
  // where the font family changes between parent/child.
  const AtomicString& computed_family = ComputedFontFamily();
  if (computed_family.length()) {
    AXObject* parent = ParentObjectUnignored();
    if (!parent || parent->ComputedFontFamily() != computed_family) {
      TruncateAndAddStringAttribute(
          node_data, ax::mojom::blink::StringAttribute::kFontFamily,
          FontFamilyForSerialization());
    }
  }

  // Font size is in pixels.
  if (FontSize()) {
    node_data->AddFloatAttribute(ax::mojom::blink::FloatAttribute::kFontSize,
                                 FontSize());
  }

  if (FontWeight()) {
    node_data->AddFloatAttribute(ax::mojom::blink::FloatAttribute::kFontWeight,
                                 FontWeight());
  }

  if (RoleValue() == ax::mojom::blink::Role::kListItem &&
      GetListStyle() != ax::mojom::blink::ListStyle::kNone) {
    node_data->SetListStyle(GetListStyle());
  }

  if (GetTextDirection() != ax::mojom::blink::WritingDirection::kNone) {
    node_data->SetTextDirection(GetTextDirection());
  }

  if (GetTextPosition() != ax::mojom::blink::TextPosition::kNone) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kTextPosition,
                               static_cast<int32_t>(GetTextPosition()));
  }

  int32_t text_style = 0;
  ax::mojom::blink::TextDecorationStyle text_overline_style;
  ax::mojom::blink::TextDecorationStyle text_strikethrough_style;
  ax::mojom::blink::TextDecorationStyle text_underline_style;
  GetTextStyleAndTextDecorationStyle(&text_style, &text_overline_style,
                                     &text_strikethrough_style,
                                     &text_underline_style);
  if (text_style) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kTextStyle,
                               text_style);
  }

  if (text_overline_style != ax::mojom::blink::TextDecorationStyle::kNone) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kTextOverlineStyle,
        static_cast<int32_t>(text_overline_style));
  }

  if (text_strikethrough_style !=
      ax::mojom::blink::TextDecorationStyle::kNone) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kTextStrikethroughStyle,
        static_cast<int32_t>(text_strikethrough_style));
  }

  if (text_underline_style != ax::mojom::blink::TextDecorationStyle::kNone) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kTextUnderlineStyle,
        static_cast<int32_t>(text_underline_style));
  }
}

void AXObject::SerializeTableAttributes(ui::AXNodeData* node_data) {
  if (ui::IsTableLike(RoleValue())) {
    int aria_colcount = AriaColumnCount();
    if (aria_colcount) {
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kAriaColumnCount, aria_colcount);
    }
    int aria_rowcount = AriaRowCount();
    if (aria_rowcount) {
      node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kAriaRowCount,
                                 aria_rowcount);
    }
  }

  if (ui::IsTableRow(RoleValue())) {
    AXObject* header = HeaderObject();
    if (header && !header->IsDetached()) {
      // TODO(accessibility): these should be computed by ui::AXTableInfo and
      // removed here.
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kTableRowHeaderId,
          header->AXObjectID());
    }
  }

  if (ui::IsCellOrTableHeader(RoleValue())) {
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kTableCellColumnSpan, ColumnSpan());
    node_data->AddIntAttribute(
        ax::mojom::blink::IntAttribute::kTableCellRowSpan, RowSpan());
  }

  if (ui::IsCellOrTableHeader(RoleValue()) || ui::IsTableRow(RoleValue())) {
    // aria-rowindex and aria-colindex are supported on cells, headers and
    // rows.
    int aria_rowindex = AriaRowIndex();
    if (aria_rowindex) {
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kAriaCellRowIndex, aria_rowindex);
    }

    int aria_colindex = AriaColumnIndex();
    if (aria_colindex) {
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kAriaCellColumnIndex, aria_colindex);
    }
  }

  if (ui::IsTableHeader(RoleValue()) &&
      GetSortDirection() != ax::mojom::blink::SortDirection::kNone) {
    node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kSortDirection,
                               static_cast<int32_t>(GetSortDirection()));
  }
}

// Attributes that don't need to be serialized on ignored nodes.
void AXObject::SerializeUnignoredAttributes(ui::AXNodeData* node_data,
                                            ui::AXMode accessibility_mode) {
  AccessibilityExpanded expanded = IsExpanded();
  if (expanded) {
    if (expanded == kExpandedCollapsed)
      node_data->AddState(ax::mojom::blink::State::kCollapsed);
    else if (expanded == kExpandedExpanded)
      node_data->AddState(ax::mojom::blink::State::kExpanded);
  }

  ax::mojom::blink::Role role = RoleValue();
  if (HasPopup() != ax::mojom::blink::HasPopup::kFalse) {
    node_data->SetHasPopup(HasPopup());
  } else if (role == ax::mojom::blink::Role::kPopUpButton ||
             role == ax::mojom::blink::Role::kComboBoxSelect) {
    node_data->SetHasPopup(ax::mojom::blink::HasPopup::kMenu);
  } else if (ui::IsComboBox(role)) {
    node_data->SetHasPopup(ax::mojom::blink::HasPopup::kListbox);
  }

  if (IsPopup() != ax::mojom::blink::IsPopup::kNone) {
    node_data->SetIsPopup(IsPopup());
  }

  if (IsAutofillAvailable())
    node_data->AddState(ax::mojom::blink::State::kAutofillAvailable);

  if (IsDefault())
    node_data->AddState(ax::mojom::blink::State::kDefault);

  // aria-grabbed is deprecated in WAI-ARIA 1.1.
  if (IsGrabbed() != kGrabbedStateUndefined) {
    node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kGrabbed,
                                IsGrabbed() == kGrabbedStateTrue);
  }

  if (IsHovered())
    node_data->AddState(ax::mojom::blink::State::kHovered);

  if (IsLinked())
    node_data->AddState(ax::mojom::blink::State::kLinked);

  if (IsMultiline())
    node_data->AddState(ax::mojom::blink::State::kMultiline);

  if (IsMultiSelectable())
    node_data->AddState(ax::mojom::blink::State::kMultiselectable);

  if (IsPasswordField())
    node_data->AddState(ax::mojom::blink::State::kProtected);

  if (IsRequired())
    node_data->AddState(ax::mojom::blink::State::kRequired);

  if (IsSelected() != blink::kSelectedStateUndefined) {
    node_data->AddBoolAttribute(ax::mojom::blink::BoolAttribute::kSelected,
                                IsSelected() == blink::kSelectedStateTrue);
    node_data->AddBoolAttribute(
        ax::mojom::blink::BoolAttribute::kSelectedFromFocus,
        IsSelectedFromFocus());
  }

  if (IsNotUserSelectable()) {
    node_data->AddBoolAttribute(
        ax::mojom::blink::BoolAttribute::kNotUserSelectableStyle, true);
  }

  if (IsVisited())
    node_data->AddState(ax::mojom::blink::State::kVisited);

  if (Orientation() == kAccessibilityOrientationVertical)
    node_data->AddState(ax::mojom::blink::State::kVertical);
  else if (Orientation() == blink::kAccessibilityOrientationHorizontal)
    node_data->AddState(ax::mojom::blink::State::kHorizontal);

  if (GetTextAlign() != ax::mojom::blink::TextAlign::kNone) {
    node_data->SetTextAlign(GetTextAlign());
  }

  if (GetTextIndent() != 0.0f) {
    node_data->AddFloatAttribute(ax::mojom::blink::FloatAttribute::kTextIndent,
                                 GetTextIndent());
  }

  if (accessibility_mode.has_mode(ui::AXMode::kScreenReader) ||
      accessibility_mode.has_mode(ui::AXMode::kPDF)) {
    // The DOMNodeID from Blink. Currently only populated when using
    // the accessibility tree for PDF exporting. Warning, this is totally
    // unrelated to the accessibility node ID, or the ID attribute for an
    // HTML element - it's an ID used to uniquely identify nodes in Blink.
    int dom_node_id = GetDOMNodeId();
    if (dom_node_id) {
      node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kDOMNodeId,
                                 dom_node_id);
    }

    // Heading level.
    if (ui::IsHeading(role) && HeadingLevel()) {
      node_data->AddIntAttribute(
          ax::mojom::blink::IntAttribute::kHierarchicalLevel, HeadingLevel());
    }

    SerializeListAttributes(node_data);
    SerializeTableAttributes(node_data);
  }

  if (accessibility_mode.has_mode(ui::AXMode::kPDF)) {
    // Return early. None of the following attributes are needed for PDFs.
    return;
  }

  switch (Restriction()) {
    case AXRestriction::kRestrictionReadOnly:
      node_data->SetRestriction(ax::mojom::blink::Restriction::kReadOnly);
      break;
    case AXRestriction::kRestrictionDisabled:
      node_data->SetRestriction(ax::mojom::blink::Restriction::kDisabled);
      break;
    case AXRestriction::kRestrictionNone:
      SerializeActionAttributes(node_data);
      break;
  }

  TruncateAndAddStringAttribute(
      node_data, ax::mojom::blink::StringAttribute::kUrl, Url().GetString());

  if (accessibility_mode.has_mode(ui::AXMode::kScreenReader)) {
    SerializeMarkerAttributes(node_data);
    SerializeStyleAttributes(node_data);
  }

  SerializeSparseAttributes(node_data);

  if (Element* element = GetElement()) {
    // Do not send the value attribute for non-atomic text fields in order to
    // improve the performance of the cross-process communication with the
    // browser process, and since it can be easily computed in that process.
    TruncateAndAddStringAttribute(node_data,
                                  ax::mojom::blink::StringAttribute::kValue,
                                  GetValueForControl());

    if (IsA<HTMLInputElement>(element)) {
      String type = element->getAttribute(html_names::kTypeAttr);
      if (type.empty()) {
        type = "text";
      }
      TruncateAndAddStringAttribute(
          node_data, ax::mojom::blink::StringAttribute::kInputType, type);
    }

    if (IsAtomicTextField()) {
      // Selection offsets are only used for plain text controls, (input of a
      // text field type, and textarea). Rich editable areas, such as
      // contenteditables, use AXTreeData.
      //
      // TODO(nektar): Remove kTextSelStart and kTextSelEnd from the renderer.
      const auto ax_selection =
          AXSelection::FromCurrentSelection(ToTextControl(*element));
      int start = ax_selection.Base().IsTextPosition()
                      ? ax_selection.Base().TextOffset()
                      : ax_selection.Base().ChildIndex();
      int end = ax_selection.Extent().IsTextPosition()
                    ? ax_selection.Extent().TextOffset()
                    : ax_selection.Extent().ChildIndex();
      node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kTextSelStart,
                                 start);
      node_data->AddIntAttribute(ax::mojom::blink::IntAttribute::kTextSelEnd,
                                 end);
    }
  }

  SerializeComputedDetailsRelation(node_data);
  // Try to get an aria-controls listbox for an <input role="combobox">.
  if (!node_data->HasIntListAttribute(
          ax::mojom::blink::IntListAttribute::kControlsIds)) {
    if (AXObject* listbox = GetControlsListboxForTextfieldCombobox()) {
      node_data->AddIntListAttribute(
          ax::mojom::blink::IntListAttribute::kControlsIds,
          {static_cast<int32_t>(listbox->AXObjectID())});
    }
  }

  if (IsScrollableContainer())
    SerializeScrollAttributes(node_data);

  SerializeChooserPopupAttributes(node_data);

  if (GetElement()) {
    SerializeElementAttributes(node_data);
    if (accessibility_mode.has_mode(ui::AXMode::kHTML)) {
      SerializeHTMLAttributes(node_data);
    }
  }
}

void AXObject::SerializeComputedDetailsRelation(
    ui::AXNodeData* node_data) const {
  // aria-details was used -- it may have set a relation, unless the attribute
  // value did not point to valid elements (e.g aria-details=""). Whether it
  // actually set the relation or not, the author's intent in using the
  // aria-details attribute is understood to mean that no automatic relation
  // should be set.
  if (HasAttribute(html_names::kAriaDetailsAttr)) {
    return;
  }

  // Add details relation to <figure>, pointing at <figcaption>.
  if (node_data->role == ax::mojom::blink::Role::kFigure) {
    AXObject* fig_caption = GetChildFigcaption();
    if (fig_caption) {
      std::vector<int32_t> ids;
      ids.push_back(GetChildFigcaption()->AXObjectID());
      node_data->AddIntListAttribute(
          ax::mojom::blink::IntListAttribute::kDetailsIds, ids);
      return;
    }
  }

  // Add aria-details for a popover invoker.
  // TODO(https://crbug.com/1426607) Support this for non-plain hint popovers.
  if (AXObject* popover = GetTargetPopoverForInvoker()) {
    node_data->AddIntListAttribute(
        ax::mojom::blink::IntListAttribute::kDetailsIds,
        {static_cast<int32_t>(popover->AXObjectID())});
  }
}

bool AXObject::IsPlainContent() const {
  if (!ui::IsPlainContentElement(role_)) {
    return false;
  }
  for (const auto& child : ChildrenIncludingIgnored()) {
    if (!child->IsPlainContent()) {
      return false;
    }
  }
  return true;
}

// Popover invoking elements should have details relationships with their
// target popover, when that popover is a) open, and b) not the next element
// in the DOM (depth first search order).
AXObject* AXObject::GetTargetPopoverForInvoker() const {
  auto* form_element = DynamicTo<HTMLFormControlElement>(GetElement());
  if (!form_element) {
    return nullptr;
  }
  HTMLElement* target_popover = form_element->popoverTargetElement().popover;
  if (!target_popover || !target_popover->popoverOpen()) {
    return nullptr;
  }
  if (ElementTraversal::NextSkippingChildren(*form_element) == target_popover) {
    // The next element is already the popover.
    return nullptr;
  }
  return AXObjectCache().GetOrCreate(target_popover);
}

// Try to get an aria-controls for an <input role="combobox">, because it
// helps identify focusable options in the listbox using activedescendant
// detection, even though the focus is on the textbox and not on the listbox
// ancestor.
AXObject* AXObject::GetControlsListboxForTextfieldCombobox() {
  // Only perform work for textfields.
  if (!ui::IsTextField(RoleValue()))
    return nullptr;

  // Object is ignored for some reason, most likely hidden.
  if (AccessibilityIsIgnored()) {
    return nullptr;
  }

  // Authors used to be told to use aria-owns to point from the textfield to the
  // listbox. However, the aria-owns  on a textfield must be ignored for its
  // normal purpose because a textfield cannot have children. This code allows
  // the textfield's invalid aria-owns to be remapped to aria-controls.
  DCHECK(GetElement());
  HeapVector<Member<Element>> owned_elements;
  AXObject* listbox_candidate = nullptr;
  if (ElementsFromAttribute(GetElement(), owned_elements,
                            html_names::kAriaOwnsAttr) &&
      owned_elements.size() > 0) {
    DCHECK(owned_elements[0]);
    listbox_candidate = AXObjectCache().GetOrCreate(owned_elements[0]);
  }

  // Combobox grouping <div role="combobox"><input><div role="listbox"></div>.
  if (!listbox_candidate && RoleValue() == ax::mojom::blink::Role::kTextField &&
      ParentObject()->RoleValue() ==
          ax::mojom::blink::Role::kComboBoxGrouping) {
    listbox_candidate = UnignoredNextSibling();
  }

  // Heuristic: try the next sibling, but we are very strict about this in
  // order to avoid false positives such as an <input> followed by a
  // <select>.
  if (!listbox_candidate &&
      RoleValue() == ax::mojom::blink::Role::kTextFieldWithComboBox) {
    // Require an aria-activedescendant on the <input>.
    if (!GetAOMPropertyOrARIAAttribute(AOMRelationProperty::kActiveDescendant))
      return nullptr;
    listbox_candidate = UnignoredNextSibling();
    if (!listbox_candidate)
      return nullptr;
    // Require that the next sibling is not a <select>.
    if (IsA<HTMLSelectElement>(listbox_candidate->GetNode()))
      return nullptr;
    // Require an ARIA role on the next sibling.
    if (listbox_candidate->AriaRoleAttribute() !=
        ax::mojom::blink::Role::kListBox) {
      return nullptr;
    }
    // Naming a listbox within a composite combobox widget is not part of a
    // known/used pattern. If it has a name, it's an indicator that it's
    // probably a separate listbox widget.
    if (!listbox_candidate->ComputedName().empty())
      return nullptr;
  }

  if (!listbox_candidate ||
      listbox_candidate->RoleValue() != ax::mojom::blink::Role::kListBox) {
    return nullptr;
  }

  return listbox_candidate;
}

const AtomicString& AXObject::GetRoleAttributeStringForObjectAttribute(
    ui::AXNodeData* node_data) {
  // All ARIA roles are exposed in xml-roles.
  if (const AtomicString& role_str =
          GetAOMPropertyOrARIAAttribute(AOMStringProperty::kRole)) {
    return role_str;
  }

  ax::mojom::blink::Role landmark_role = node_data->role;
  if (landmark_role == ax::mojom::blink::Role::kFooter) {
    // - Treat <footer> as "contentinfo" in xml-roles object attribute.
    landmark_role = ax::mojom::blink::Role::kContentInfo;
  } else if (landmark_role == ax::mojom::blink::Role::kHeader) {
    // - Treat <header> as "banner" in xml-roles object attribute.
    landmark_role = ax::mojom::blink::Role::kBanner;
  } else if (!ui::IsLandmark(node_data->role)) {
    // Landmarks are the only roles exposed in xml-roles, matching Firefox.
    return g_null_atom;
  }
  return ARIARoleName(landmark_role);
}

void AXObject::SerializeMarkerAttributes(ui::AXNodeData* node_data) const {
  // Implemented in subclasses.
}

bool AXObject::IsAXNodeObject() const {
  return false;
}

bool AXObject::IsAXLayoutObject() const {
  return false;
}

bool AXObject::IsAXInlineTextBox() const {
  return false;
}

bool AXObject::IsList() const {
  return ui::IsList(RoleValue());
}

bool AXObject::IsAXListBox() const {
  return false;
}

bool AXObject::IsAXListBoxOption() const {
  return false;
}

bool AXObject::IsMenuList() const {
  return false;
}

bool AXObject::IsMenuListOption() const {
  return false;
}

bool AXObject::IsMenuListPopup() const {
  return false;
}

bool AXObject::IsMockObject() const {
  return false;
}

bool AXObject::IsProgressIndicator() const {
  return false;
}

bool AXObject::IsAXRadioInput() const {
  return false;
}

bool AXObject::IsSlider() const {
  return false;
}

bool AXObject::IsValidationMessage() const {
  return false;
}

bool AXObject::IsVirtualObject() const {
  return false;
}

ax::mojom::blink::Role AXObject::ComputeFinalRoleForSerialization() const {
  // An SVG with no accessible children should be exposed as an image rather
  // than a document. See https://github.com/w3c/svg-aam/issues/12.
  // We do this check here for performance purposes: When
  // AXLayoutObject::RoleFromLayoutObjectOrNode is called, that node's
  // accessible children have not been calculated. Rather than force calculation
  // there, wait until we have the full tree.
  if (role_ == ax::mojom::blink::Role::kSvgRoot && !UnignoredChildCount()) {
    return ax::mojom::blink::Role::kImage;
  }

  // DPUB ARIA 1.1 deprecated doc-biblioentry and doc-endnote, but it's still
  // possible to create these internal roles / platform mappings with a listitem
  // (native or ARIA) inside of a doc-bibliography or doc-endnotes section.
  if (role_ == ax::mojom::blink::Role::kListItem) {
    AXObject* ancestor = CachedParentObject();
    if (ancestor && ancestor->RoleValue() == ax::mojom::blink::Role::kList) {
      // Go up to the root, or next list, checking to see if the list item is
      // inside an endnote or bibliography section. If it is, remap the role.
      // The remapping does not occur for list items multiple levels deep.
      while (true) {
        ancestor = ancestor->CachedParentObject();
        if (!ancestor)
          break;
        ax::mojom::blink::Role ancestor_role = ancestor->RoleValue();
        if (ancestor_role == ax::mojom::blink::Role::kList)
          break;
        if (ancestor_role == ax::mojom::blink::Role::kDocBibliography)
          return ax::mojom::blink::Role::kDocBiblioEntry;
        if (ancestor_role == ax::mojom::blink::Role::kDocEndnotes)
          return ax::mojom::blink::Role::kDocEndnote;
      }
    }
  }

  if (role_ == ax::mojom::blink::Role::kHeader) {
    if (IsDescendantOfLandmarkDisallowedElement()) {
      return ax::mojom::blink::Role::kHeaderAsNonLandmark;
    }
  }

  if (role_ == ax::mojom::blink::Role::kFooter) {
    if (IsDescendantOfLandmarkDisallowedElement()) {
      return ax::mojom::blink::Role::kFooterAsNonLandmark;
    }
  }

  // An <aside> element should not be considered a landmark region
  // if it is a child of a landmark disallowed element, UNLESS it has
  // an accessible name.
  if (role_ == ax::mojom::blink::Role::kComplementary) {
    if (IsDescendantOfLandmarkDisallowedElement() &&
        !IsNameFromAuthorAttribute()) {
      return ax::mojom::blink::Role::kGenericContainer;
    }
  }

  // TODO(accessibility): Consider moving the image vs. image map role logic
  // here. Currently it is implemented in AXPlatformNode subclasses and thus
  // not available to the InspectorAccessibilityAgent.
  return role_;
}

ax::mojom::blink::Role AXObject::RoleValue() const {
  return role_;
}

bool AXObject::IsARIATextField() const {
  if (IsAtomicTextField())
    return false;  // Native role supercedes the ARIA one.
  return AriaRoleAttribute() == ax::mojom::blink::Role::kTextField ||
         AriaRoleAttribute() == ax::mojom::blink::Role::kSearchBox ||
         AriaRoleAttribute() == ax::mojom::blink::Role::kTextFieldWithComboBox;
}

bool AXObject::IsButton() const {
  return ui::IsButton(RoleValue());
}

bool AXObject::IsCanvas() const {
  return RoleValue() == ax::mojom::blink::Role::kCanvas;
}

bool AXObject::IsColorWell() const {
  return RoleValue() == ax::mojom::blink::Role::kColorWell;
}

bool AXObject::IsControl() const {
  return ui::IsControl(RoleValue());
}

bool AXObject::IsDefault() const {
  return false;
}

bool AXObject::IsFieldset() const {
  return false;
}

bool AXObject::IsHeading() const {
  return ui::IsHeading(RoleValue());
}

bool AXObject::IsImage() const {
  // Canvas is not currently included so that it is not exposed unless there is
  // a label, fallback content or something to make it accessible. This decision
  // may be revisited at a later date.
  return ui::IsImage(RoleValue()) &&
         RoleValue() != ax::mojom::blink::Role::kCanvas;
}

bool AXObject::IsInputImage() const {
  return false;
}

bool AXObject::IsLink() const {
  return ui::IsLink(RoleValue());
}

bool AXObject::IsImageMapLink() const {
  return false;
}

bool AXObject::IsMenu() const {
  return RoleValue() == ax::mojom::blink::Role::kMenu;
}

bool AXObject::IsCheckable() const {
  switch (RoleValue()) {
    case ax::mojom::blink::Role::kCheckBox:
    case ax::mojom::blink::Role::kMenuItemCheckBox:
    case ax::mojom::blink::Role::kMenuItemRadio:
    case ax::mojom::blink::Role::kRadioButton:
    case ax::mojom::blink::Role::kSwitch:
    case ax::mojom::blink::Role::kToggleButton:
      return true;
    case ax::mojom::blink::Role::kTreeItem:
    case ax::mojom::blink::Role::kListBoxOption:
    case ax::mojom::blink::Role::kMenuListOption:
      return AriaCheckedIsPresent();
    default:
      return false;
  }
}

// Why this is here instead of AXNodeObject:
// Because an AXMenuListOption (<option>) can
// have an ARIA role of menuitemcheckbox/menuitemradio
// yet does not inherit from AXNodeObject
ax::mojom::blink::CheckedState AXObject::CheckedState() const {
  const Node* node = GetNode();
  if (!IsCheckable() || !node) {
    return ax::mojom::blink::CheckedState::kNone;
  }

  // First test for native checked state
  if (IsA<HTMLInputElement>(*node)) {
    const auto* input = DynamicTo<HTMLInputElement>(node);
    if (!input) {
      return ax::mojom::blink::CheckedState::kNone;
    }

    const auto inputType = input->type();
    // The native checked state is processed exlusively. Aria is ignored because
    // the native checked value takes precedence for input elements with type
    // `checkbox` or `radio` according to the HTML-AAM specification.
    if (inputType == input_type_names::kCheckbox ||
        inputType == input_type_names::kRadio) {
      // Expose native checkbox mixed state as accessibility mixed state (unless
      // the role is switch). However, do not expose native radio mixed state as
      // accessibility mixed state. This would confuse the JAWS screen reader,
      // which reports a mixed radio as both checked and partially checked, but
      // a native mixed native radio button simply means no radio buttons have
      // been checked in the group yet.
      if (IsNativeCheckboxInMixedState(node)) {
        return ax::mojom::blink::CheckedState::kMixed;
      }

      return input->ShouldAppearChecked()
                 ? ax::mojom::blink::CheckedState::kTrue
                 : ax::mojom::blink::CheckedState::kFalse;
    }
  }

  // Try ARIA checked/pressed state
  const ax::mojom::blink::Role role = RoleValue();
  const auto prop = role == ax::mojom::blink::Role::kToggleButton
                        ? AOMStringProperty::kPressed
                        : AOMStringProperty::kChecked;
  const AtomicString& checked_attribute = GetAOMPropertyOrARIAAttribute(prop);
  if (checked_attribute) {
    if (EqualIgnoringASCIICase(checked_attribute, "mixed")) {
      if (role == ax::mojom::blink::Role::kCheckBox ||
          role == ax::mojom::blink::Role::kMenuItemCheckBox ||
          role == ax::mojom::blink::Role::kListBoxOption ||
          role == ax::mojom::blink::Role::kToggleButton ||
          role == ax::mojom::blink::Role::kTreeItem) {
        // Mixed value is supported in these roles: checkbox, menuitemcheckbox,
        // option, togglebutton, and treeitem.
        return ax::mojom::blink::CheckedState::kMixed;
      } else {
        // Mixed value is not supported in these roles: radio, menuitemradio,
        // and switch.
        return ax::mojom::blink::CheckedState::kFalse;
      }
    }

    // Anything other than "false" should be treated as "true".
    return EqualIgnoringASCIICase(checked_attribute, "false")
               ? ax::mojom::blink::CheckedState::kFalse
               : ax::mojom::blink::CheckedState::kTrue;
  }

  return ax::mojom::blink::CheckedState::kFalse;
}

String AXObject::GetValueForControl() const {
  return String();
}

String AXObject::SlowGetValueForControlIncludingContentEditable() const {
  return String();
}

bool AXObject::IsNativeCheckboxInMixedState(const Node* node) {
  const auto* input = DynamicTo<HTMLInputElement>(node);
  if (!input)
    return false;

  const auto inputType = input->type();
  if (inputType != input_type_names::kCheckbox)
    return false;
  return input->ShouldAppearIndeterminate();
}

bool AXObject::IsMenuRelated() const {
  return ui::IsMenuRelated(RoleValue());
}

bool AXObject::IsMeter() const {
  return RoleValue() == ax::mojom::blink::Role::kMeter;
}

bool AXObject::IsNativeImage() const {
  return false;
}

bool AXObject::IsNativeSpinButton() const {
  return false;
}

bool AXObject::IsAtomicTextField() const {
  return blink::IsTextControl(GetNode());
}

bool AXObject::IsNonAtomicTextField() const {
  // Consivably, an <input type=text> or a <textarea> might also have the
  // contenteditable attribute applied. In such cases, the <input> or <textarea>
  // tags should supercede.
  if (IsAtomicTextField())
    return false;
  return HasContentEditableAttributeSet() || IsARIATextField();
}

AXObject* AXObject::GetTextFieldAncestor() {
  AXObject* ancestor = this;
  while (ancestor && !ancestor->IsTextField()) {
    ancestor = ancestor->ParentObject();
  }
  return ancestor;
}

bool AXObject::IsPasswordField() const {
  auto* input_element = DynamicTo<HTMLInputElement>(GetNode());
  return input_element && input_element->type() == input_type_names::kPassword;
}

bool AXObject::IsPasswordFieldAndShouldHideValue() const {
  if (!IsPasswordField())
    return false;
  const Settings* settings = GetDocument()->GetSettings();
  return settings && !settings->GetAccessibilityPasswordValuesEnabled();
}

bool AXObject::IsPresentational() const {
  return ui::IsPresentational(RoleValue());
}

bool AXObject::IsTextObject() const {
  // Objects with |ax::mojom::blink::Role::kLineBreak| are HTML <br> elements
  // and are not backed by DOM text nodes. We can't mark them as text objects
  // for that reason.
  switch (RoleValue()) {
    case ax::mojom::blink::Role::kInlineTextBox:
    case ax::mojom::blink::Role::kStaticText:
      return true;
    default:
      return false;
  }
}

bool AXObject::IsRangeValueSupported() const {
  if (RoleValue() == ax::mojom::blink::Role::kSplitter) {
    // According to the ARIA spec, role="separator" acts as a splitter only
    // when focusable, and supports a range only in that case.
    return CanSetFocusAttribute();
  }
  return ui::IsRangeValueSupported(RoleValue());
}

bool AXObject::IsScrollbar() const {
  return RoleValue() == ax::mojom::blink::Role::kScrollBar;
}

bool AXObject::IsNativeSlider() const {
  return false;
}

bool AXObject::IsSpinButton() const {
  return RoleValue() == ax::mojom::blink::Role::kSpinButton;
}

bool AXObject::IsTabItem() const {
  return RoleValue() == ax::mojom::blink::Role::kTab;
}

bool AXObject::IsTextField() const {
  if (IsDetached())
    return false;
  return IsAtomicTextField() || IsNonAtomicTextField();
}

bool AXObject::IsAutofillAvailable() const {
  return false;
}

bool AXObject::IsClickable() const {
  return ui::IsClickable(RoleValue());
}

AccessibilityExpanded AXObject::IsExpanded() const {
  return kExpandedUndefined;
}

bool AXObject::IsFocused() const {
  return false;
}

AccessibilityGrabbedState AXObject::IsGrabbed() const {
  return kGrabbedStateUndefined;
}

bool AXObject::IsHovered() const {
  return false;
}

bool AXObject::IsLineBreakingObject() const {
  // Not all AXObjects have an associated node or layout object. They could be
  // virtual accessibility nodes, for example.
  //
  // We assume that most images on the Web are inline.
  return !IsImage() && ui::IsStructure(RoleValue());
}

bool AXObject::IsLinked() const {
  return false;
}

bool AXObject::IsLoaded() const {
  return false;
}

bool AXObject::IsMultiSelectable() const {
  return false;
}

bool AXObject::IsOffScreen() const {
  return false;
}

bool AXObject::IsRequired() const {
  return false;
}

AccessibilitySelectedState AXObject::IsSelected() const {
  return kSelectedStateUndefined;
}

bool AXObject::IsSelectedFromFocusSupported() const {
  return false;
}

bool AXObject::IsSelectedFromFocus() const {
  return false;
}

bool AXObject::IsSelectedOptionActive() const {
  return false;
}

bool AXObject::IsNotUserSelectable() const {
  return false;
}

bool AXObject::IsVisited() const {
  return false;
}

bool AXObject::AccessibilityIsIgnored() const {
  UpdateCachedAttributeValuesIfNeeded();
#if defined(AX_FAIL_FAST_BUILD)
  if (!cached_is_ignored_ && IsDetached()) {
    NOTREACHED()
        << "A detached node cannot be ignored: " << ToString(true)
        << "\nThe Detach() method sets cached_is_ignored_ to true, but "
           "something has recomputed it.";
  }
  if (!cached_is_ignored_ && IsA<Document>(GetNode()) && CachedParentObject() &&
      CachedParentObject()->IsMenuList()) {
    NOTREACHED() << "The menulist popup's document must be ignored.";
  }
#endif
  return cached_is_ignored_;
}

bool AXObject::AccessibilityIsIgnoredButIncludedInTree() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_is_ignored_but_included_in_tree_;
}

// AccessibilityIsIncludedInTree should be true for all nodes that should be
// included in the tree, even if they are ignored
bool AXObject::AccessibilityIsIncludedInTree() const {
  return !AccessibilityIsIgnored() || AccessibilityIsIgnoredButIncludedInTree();
}

void AXObject::InvalidateCachedValues() {
#if DCHECK_IS_ON()
  DCHECK(!AXObjectCache().IsFrozen());
  DCHECK(!is_updating_cached_values_)
      << "Should not invalidate cached values while updating them.";
#endif

  cached_values_need_update_ = true;
}

void AXObject::UpdateCachedAttributeValuesIfNeeded(
    bool notify_parent_of_ignored_changes) const {
  if (IsDetached()) {
    cached_is_ignored_ = true;
    cached_is_ignored_but_included_in_tree_ = false;
    return;
  }

  // Mock objects are created by, owned and dependent on their parents.
  // If the mock object's values change, recompute the parent's as well.
  // Note: The only remaining use of mock objects is AXMenuListPopup.
  // TODO(accessibility) Remove this when we remove AXMenuList* and create the
  // AX hierarchy for <select> from the shadow dom instead.
  if (IsMockObject()) {
    DCHECK(parent_);
    parent_->UpdateCachedAttributeValuesIfNeeded();
  }

  if (!cached_values_need_update_) {
    return;
  }

  cached_values_need_update_ = false;

#if DCHECK_IS_ON()  // Required in order to get Lifecycle().ToString()
  DCHECK(!AXObjectCache().IsFrozen())
      << "All cached values must be updated before the tree is frozen "
         "serialization, because changes to the ignored state could cause tree "
         "structure changes.";
  DCHECK(!is_computing_role_)
      << "Updating cached values while computing a role is dangerous as it "
         "can lead to code that uses the AXObject before it is ready.";
  DCHECK(!is_updating_cached_values_)
      << "Reentering UpdateCachedAttributeValuesIfNeeded() on same node: "
      << GetNode();

  base::AutoReset<bool> reentrancy_protector(&is_updating_cached_values_, true);

  DCHECK(!GetDocument() || GetDocument()->Lifecycle().GetState() >=
                               DocumentLifecycle::kAfterPerformLayout)
      << "Unclean document at lifecycle "
      << GetDocument()->Lifecycle().ToString();
#endif  // DCHECK_IS_ON()

  if (IsMissingParent())
    RepairMissingParent();

  // Mock objects are created by, owned and dependent on their parents.
  // If the mock object's values change, recompute the parent's as well.
  // Note: The only remaining use of mock objects is AXMenuListPopup.
  // TODO(accessibility) Remove this when we remove AXMenuList* and create the
  // AX hierarchy for <select> from the shadow dom instead.
  // TODO(accessibility) Can this be fixed by instead invalidating the parent
  // when invalidating the child?
  if (IsMockObject()) {
    DCHECK(parent_);
    parent_->UpdateCachedAttributeValuesIfNeeded();
  }

  const ComputedStyle* style = GetComputedStyle();

  cached_is_hidden_via_style_ = ComputeIsHiddenViaStyle(style);

  // Decisions in what subtree descendants are included (each descendant's
  // cached children_) depends on the ARIA hidden state. When it changes,
  // the entire subtree needs to recompute descendants.
  // In addition, the below computations for is_ignored_but_included_in_tree is
  // dependent on having the correct new cached value.
  bool is_inert = ComputeIsInertViaStyle(style);
  bool is_aria_hidden = ComputeIsAriaHidden();
  if (cached_is_inert_ != is_inert ||
      cached_is_aria_hidden_ != is_aria_hidden) {
    // Update children if not already dirty (e.g. during Init() time.
    SetNeedsToUpdateChildren();
    cached_is_inert_ = is_inert;
    cached_is_aria_hidden_ = is_aria_hidden;
  }

  // Must be after inert computation, because focusability depends on that, but
  // before the included in tree computation, which depends on focusability.
  cached_can_set_focus_attribute_ = ComputeCanSetFocusAttribute();

  cached_is_descendant_of_disabled_node_ = ComputeIsDescendantOfDisabledNode();

  bool is_ignored = ComputeAccessibilityIsIgnored();
  bool is_ignored_but_included_in_tree =
      is_ignored && ComputeAccessibilityIsIgnoredButIncludedInTree();
  bool is_included_in_tree = !is_ignored || is_ignored_but_included_in_tree;
  bool included_in_tree_changed =
      is_included_in_tree != LastKnownIsIncludedInTreeValue();
  bool notify_included_in_tree_changed = false;
  if (included_in_tree_changed) {
    // If the inclusion bit is changing, we need to repair the
    // has_dirty_descendants, because it is only set on included nodes.
    if (is_included_in_tree) {
      // This is being inserted in the hierarchy as an included node: if the
      // parent has dirty descendants copy that bit to this as well, so as not
      // to interrupt the chain of descendant updates.
      if (AXObject* unignored_parent = ParentObjectUnignored()) {
        if (unignored_parent->HasDirtyDescendants()) {
          has_dirty_descendants_ = true;
        }
      }
    } else {
      // The has dirty descendant bits will only be cleared on included
      // nodes, so it should not be set on nodes that becomes unincluded.
      has_dirty_descendants_ = false;
    }

    // If the child's "included in tree" state changes, we will be notifying the
    // parent to recompute its children.
    // Exceptions:
    // - Caller passes in |notify_parent_of_ignored_changes = false| -- this
    //   occurs when this is a new child, or when a parent is in the middle of
    //   adding this child, and doing this would be redundant.
    // - Inline text boxes: their "included in tree" state is entirely dependent
    //   on their static text parent.
    if (notify_parent_of_ignored_changes &&
        RoleValue() != ax::mojom::blink::Role::kInlineTextBox) {
      notify_included_in_tree_changed = true;
    }
  }

  // Presence of inline text children depends on ignored state.
  if (is_ignored != LastKnownIsIgnoredValue() &&
      ui::CanHaveInlineTextBoxChildren(RoleValue())) {
    // Update children if not already dirty (e.g. during Init() time.
    SetNeedsToUpdateChildren();
  }

  // Call children changed on included ancestor.
  // This must be called before cached_is_ignored_* are updated, otherwise a
  // performance optimization depending on LastKnownIsIncludedInTreeValue()
  // may misfire.
  if (notify_included_in_tree_changed) {
    if (AXObject* parent = CachedParentObject()) {
      SANITIZER_CHECK(!AXObjectCache().IsFrozen())
          << "Objects cannot change their inclusion state during "
             "serialization:\n"
          << "* Object: " << ToString(true, true) << "\n* Ignored will become "
          << is_ignored << "\n* Included in tree will become "
          << (!is_ignored || is_ignored_but_included_in_tree)
          << "\n* Parent: " << parent->ToString(true, true);
      // Defers a ChildrenChanged() on the first included ancestor.
      // Must defer it, otherwise it can cause reentry into
      // UpdateCachedAttributeValuesIfNeeded() on |this|.
      // ParentObjectUnignored()->SetNeedsToUpdateChildren();
      AXObjectCache().ChildrenChangedOnAncestorOf(const_cast<AXObject*>(this));
    }
  }

  cached_is_ignored_ = is_ignored;
  cached_is_ignored_but_included_in_tree_ = is_ignored_but_included_in_tree;

  // Compute live region root, which can be from any ARIA live value, including
  // "off", or from an automatic ARIA live value, e.g. from role="status".
  // TODO(dmazzoni): remove this const_cast.
  AtomicString aria_live;
  if (GetNode() && IsA<Document>(GetNode())) {
    // The document root is never a live region root.
    cached_live_region_root_ = nullptr;
  } else if (RoleValue() == ax::mojom::blink::Role::kInlineTextBox) {
    // Inline text boxes do not need live region properties.
    cached_live_region_root_ = nullptr;
  } else if (parent_) {
    // Is a live region root if this or an ancestor is a live region.
    cached_live_region_root_ = IsLiveRegionRoot() ? const_cast<AXObject*>(this)
                                                  : parent_->LiveRegionRoot();
  }

  if (GetLayoutObject() && GetLayoutObject()->IsText()) {
    cached_local_bounding_box_rect_for_accessibility_ =
        GetLayoutObject()->LocalBoundingBoxRectForAccessibility();
  }

  DCHECK(!cached_values_need_update_)
      << "While recomputing cached values, they were invalidated again.";
}

bool AXObject::ComputeAccessibilityIsIgnored(
    IgnoredReasons* ignored_reasons) const {
  return ShouldIgnoreForHiddenOrInert(ignored_reasons);
}

bool AXObject::ShouldIgnoreForHiddenOrInert(
    IgnoredReasons* ignored_reasons) const {
  DCHECK(!cached_values_need_update_)
      << "Tried to compute ignored value without up-to-date hidden/inert "
         "values on "
      << GetNode();

  // All nodes must have an unignored parent within their tree under
  // the root node of the web area, so force that node to always be unignored.
  if (IsA<Document>(GetNode())) {
    return false;
  }

  if (cached_is_aria_hidden_) {
    // Keep keyboard focusable elements that are aria-hidden in tree, so that
    // they can still fire events such as focus and value changes.
    if (!IsKeyboardFocusable()) {
      if (ignored_reasons)
        ComputeIsAriaHidden(ignored_reasons);
      return true;
    }
  }

  if (cached_is_inert_) {
    if (ignored_reasons) {
      ComputeIsInert(ignored_reasons);
    }
    return true;
  }

  // aria-hidden=false is meant to override visibility as the determinant in
  // AX hierarchy inclusion, but only for the element it is specified, and not
  // the entire subtree. See https://w3c.github.io/aria/#aria-hidden.
  if (AOMPropertyOrARIAAttributeIsFalse(AOMBooleanProperty::kHidden)) {
    return false;
  }

  if (cached_is_hidden_via_style_) {
    if (ignored_reasons) {
      ignored_reasons->push_back(
          IgnoredReason(GetLayoutObject() ? kAXNotVisible : kAXNotRendered));
    }
    return true;
  }

  // Hide nodes that are whitespace or are occluded by CSS alt text.
  if (!GetLayoutObject() && GetNode() && !IsA<HTMLAreaElement>(GetNode()) &&
      !DisplayLockUtilities::IsDisplayLockedPreventingPaint(GetNode()) &&
      (!GetElement() || !GetElement()->HasDisplayContentsStyle())) {
    if (ignored_reasons) {
      ignored_reasons->push_back(IgnoredReason(kAXNotRendered));
    }
    return true;
  }

  return false;
}

// Note: do not rely on the value of this inside of display:none.
// In practice, it does not matter because nodes in display:none subtrees are
// marked ignored either way.
bool AXObject::IsInert() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_is_inert_;
}

bool AXObject::ComputeIsInertViaStyle(const ComputedStyle* style,
                                      IgnoredReasons* ignored_reasons) const {
  // TODO(szager): This method is n^2 -- it recurses into itself via
  // ComputeIsInert(), and InertRoot() does as well.
  if (style) {
    if (style->IsInert()) {
      if (ignored_reasons) {
        const AXObject* ax_inert_root = InertRoot();
        if (ax_inert_root == this) {
          ignored_reasons->push_back(IgnoredReason(kAXInertElement));
          return true;
        }
        if (ax_inert_root) {
          ignored_reasons->push_back(
              IgnoredReason(kAXInertSubtree, ax_inert_root));
          return true;
        }
        // If there is no inert root, inertness must have been set by a modal
        // dialog or a fullscreen element (see AdjustStyleForInert).
        Document& document = GetNode()->GetDocument();
        if (HTMLDialogElement* dialog = document.ActiveModalDialog()) {
          if (AXObject* dialog_object = AXObjectCache().GetOrCreate(dialog)) {
            ignored_reasons->push_back(
                IgnoredReason(kAXActiveModalDialog, dialog_object));
            return true;
          }
        } else if (Element* fullscreen =
                       Fullscreen::FullscreenElementFrom(document)) {
          if (AXObject* fullscreen_object =
                  AXObjectCache().GetOrCreate(fullscreen)) {
            ignored_reasons->push_back(
                IgnoredReason(kAXActiveFullscreenElement, fullscreen_object));
            return true;
          }
        }
        ignored_reasons->push_back(IgnoredReason(kAXInertElement));
      }
      return true;
    } else if (IsBlockedByAriaModalDialog(ignored_reasons)) {
      if (ignored_reasons)
        ignored_reasons->push_back(IgnoredReason(kAXAriaModalDialog));
      return true;
    } else if (const LocalFrame* frame = GetNode()->GetDocument().GetFrame()) {
      // Inert frames don't expose the inertness to the style of their contents,
      // but accessibility should consider them inert anyways.
      if (frame->IsInert()) {
        if (ignored_reasons)
          ignored_reasons->push_back(IgnoredReason(kAXInertSubtree));
        return true;
      }
    }
    return false;
  }

  // Either GetNode() is null, or it's locked by content-visibility, or we
  // failed to obtain a ComputedStyle. Make a guess iterating the ancestors.
  if (const AXObject* ax_inert_root = InertRoot()) {
    if (ignored_reasons) {
      if (ax_inert_root == this) {
        ignored_reasons->push_back(IgnoredReason(kAXInertElement));
      } else {
        ignored_reasons->push_back(
            IgnoredReason(kAXInertSubtree, ax_inert_root));
      }
    }
    return true;
  } else if (IsBlockedByAriaModalDialog(ignored_reasons)) {
    if (ignored_reasons)
      ignored_reasons->push_back(IgnoredReason(kAXAriaModalDialog));
    return true;
  } else if (GetNode()) {
    if (const LocalFrame* frame = GetNode()->GetDocument().GetFrame()) {
      // Inert frames don't expose the inertness to the style of their contents,
      // but accessibility should consider them inert anyways.
      if (frame->IsInert()) {
        if (ignored_reasons)
          ignored_reasons->push_back(IgnoredReason(kAXInertSubtree));
        return true;
      }
    }
  }

  AXObject* parent = ParentObject();
  if (parent && parent->IsInert()) {
    if (ignored_reasons)
      parent->ComputeIsInert(ignored_reasons);
    return true;
  }

  return false;
}

bool AXObject::ComputeIsInert(IgnoredReasons* ignored_reasons) const {
  return ComputeIsInertViaStyle(GetComputedStyle(), ignored_reasons);
}

bool AXObject::IsAriaHidden() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_is_aria_hidden_;
}

bool AXObject::ComputeIsAriaHidden(IgnoredReasons* ignored_reasons) const {
  // The root node of a document or popup document cannot be aria-hidden.
  if (IsA<Document>(GetNode())) {
    return false;
  }

  // aria-hidden:true works a bit like display:none.
  // * aria-hidden=true affects entire subtree.
  // * aria-hidden=false cannot override aria-hidden=true on an ancestor.
  //   It can only affect elements that are styled as hidden, and only when
  //   there is no aria-hidden=true in the ancestor chain.
  // Therefore aria-hidden=true must be checked on every ancestor.
  if (AOMPropertyOrARIAAttributeIsTrue(AOMBooleanProperty::kHidden)) {
    if (ignored_reasons)
      ignored_reasons->push_back(IgnoredReason(kAXAriaHiddenElement));
    return true;
  }

  if (AXObject* parent = ParentObject()) {
    if (parent->IsAriaHidden()) {
      if (ignored_reasons) {
        ignored_reasons->push_back(
            IgnoredReason(kAXAriaHiddenSubtree, AriaHiddenRoot()));
      }
      return true;
    }
  }

  return false;
}

bool AXObject::IsModal() const {
  if (RoleValue() != ax::mojom::blink::Role::kDialog &&
      RoleValue() != ax::mojom::blink::Role::kAlertDialog)
    return false;

  bool modal = false;
  if (HasAOMPropertyOrARIAAttribute(AOMBooleanProperty::kModal, modal)) {
    return modal;
  }

  if (GetNode() && IsA<HTMLDialogElement>(*GetNode()))
    return To<Element>(GetNode())->IsInTopLayer();

  return false;
}

bool AXObject::IsBlockedByAriaModalDialog(
    IgnoredReasons* ignored_reasons) const {
  Element* active_aria_modal_dialog =
      AXObjectCache().GetActiveAriaModalDialog();

  // On platforms that don't require manual pruning of the accessibility tree,
  // the active aria modal dialog should never be set, so has no effect.
  if (!active_aria_modal_dialog) {
    return false;
  }

  if (!GetNode() || GetNode()->IsPseudoElement()) {
    return ParentObject()->IsBlockedByAriaModalDialog();
  }

  if (FlatTreeTraversal::Contains(*active_aria_modal_dialog, *GetNode())) {
    return false;
  }

  if (ignored_reasons) {
    ignored_reasons->push_back(
        IgnoredReason(kAXAriaModalDialog,
                      AXObjectCache().GetOrCreate(active_aria_modal_dialog)));
  }
  return true;
}

bool AXObject::IsVisible() const {
  // TODO(accessibility) Consider exposing inert objects as visible, since they
  // are visible. It should be fine, since the objexcts are ignored.
  return !IsAriaHidden() && !IsInert() && !IsHiddenViaStyle();
}

const AXObject* AXObject::AriaHiddenRoot() const {
  return IsAriaHidden() ? FindAncestorWithAriaHidden(this) : nullptr;
}

const AXObject* AXObject::InertRoot() const {
  const AXObject* object = this;
  if (!RuntimeEnabledFeatures::InertAttributeEnabled())
    return nullptr;

  while (object && !object->IsAXNodeObject())
    object = object->ParentObject();

  DCHECK(object);

  Node* node = object->GetNode();
  if (!node)
    return nullptr;
  auto* element = DynamicTo<Element>(node);
  if (!element)
    element = FlatTreeTraversal::ParentElement(*node);

  while (element) {
    if (element->IsInertRoot())
      return AXObjectCache().GetOrCreate(element);
    element = FlatTreeTraversal::ParentElement(*element);
  }

  return nullptr;
}

bool AXObject::DispatchEventToAOMEventListeners(Event& event) {
  HeapVector<Member<AccessibleNode>> event_path;
  for (AXObject* ancestor = this; ancestor;
       ancestor = ancestor->ParentObject()) {
    AccessibleNode* ancestor_accessible_node = ancestor->GetAccessibleNode();
    if (!ancestor_accessible_node)
      continue;

    if (!ancestor_accessible_node->HasEventListeners(event.type()))
      continue;

    event_path.push_back(ancestor_accessible_node);
  }

  // Short-circuit: if there are no AccessibleNodes attached anywhere
  // in the ancestry of this node, exit.
  if (!event_path.size())
    return false;

  // Check if the user has granted permission for this domain to use
  // AOM event listeners yet. This may trigger an infobar, but we shouldn't
  // block, so whatever decision the user makes will apply to the next
  // event received after that.
  //
  // Note that we only ask the user about this permission the first
  // time an event is received that actually would have triggered an
  // event listener. However, if the user grants this permission, it
  // persists for this origin from then on.
  if (!AXObjectCache().CanCallAOMEventListeners()) {
    AXObjectCache().RequestAOMEventListenerPermission();
    return false;
  }

  // Since we now know the AOM is being used in this document, get the
  // AccessibleNode for the target element and create it if necessary -
  // otherwise we wouldn't be able to set the event target. However note
  // that if it didn't previously exist it won't be part of the event path.
  AccessibleNode* target = GetAccessibleNode();
  if (!target) {
    if (Element* element = GetElement())
      target = element->accessibleNode();
  }
  if (!target)
    return false;
  event.SetTarget(target);

  // Capturing phase.
  event.SetEventPhase(Event::PhaseType::kCapturingPhase);
  for (int i = static_cast<int>(event_path.size()) - 1; i >= 0; i--) {
    // Don't call capturing event listeners on the target. Note that
    // the target may not necessarily be in the event path which is why
    // we check here.
    if (event_path[i] == target)
      break;

    event.SetCurrentTarget(event_path[i]);
    event_path[i]->FireEventListeners(event);
    if (event.PropagationStopped())
      return true;
  }

  // Targeting phase.
  event.SetEventPhase(Event::PhaseType::kAtTarget);
  event.SetCurrentTarget(event_path[0]);
  event_path[0]->FireEventListeners(event);
  if (event.PropagationStopped())
    return true;

  // Bubbling phase.
  event.SetEventPhase(Event::PhaseType::kBubblingPhase);
  for (wtf_size_t i = 1; i < event_path.size(); i++) {
    event.SetCurrentTarget(event_path[i]);
    event_path[i]->FireEventListeners(event);
    if (event.PropagationStopped())
      return true;
  }

  if (event.defaultPrevented())
    return true;

  return false;
}

bool AXObject::IsDescendantOfDisabledNode() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_is_descendant_of_disabled_node_;
}

bool AXObject::ComputeIsDescendantOfDisabledNode() const {
  if (IsA<Document>(GetNode()))
    return false;

  bool disabled = false;
  if (HasAOMPropertyOrARIAAttribute(AOMBooleanProperty::kDisabled, disabled))
    return disabled;

  if (AXObject* parent = ParentObject()) {
    return parent->IsDescendantOfDisabledNode() || parent->IsDisabled();
  }

  return false;
}

bool AXObject::IsExcludedByFormControlsFilter() const {
  AXObjectCacheImpl& cache = AXObjectCache();
  const ui::AXMode& mode = cache.GetAXMode();

  bool filter_to_form_controls =
      mode.HasExperimentalFlags(ui::AXMode::kExperimentalFormControls);

  if (!filter_to_form_controls) {
    return false;
  }

  // Filter out elements hidden via style.
  if (IsHiddenViaStyle()) {
    return true;
  }

  // Keep control elements.
  if (IsControl()) {
    return false;
  }

  // Keep any relevant contextual labels on form controls.
  // TODO (aldietz): this check could have further nuance to filter out
  // irrelevant text. Potential future adjustments include: Trim out text nodes
  // with length > 40 (or some threshold), as these are likely to be prose. Trim
  // out text nodes that would end up as siblings of other text in the reduced
  // tree.
  if (RoleValue() == ax::mojom::blink::Role::kStaticText) {
    return false;
  }

  // Keep generic container shadow DOM nodes inside text controls like input
  // elements.
  if (RoleValue() == ax::mojom::blink::Role::kGenericContainer &&
      EnclosingTextControl(GetNode())) {
    return false;
  }

  // Keep focusable elements to avoid breaking focus events.
  if (CanSetFocusAttribute()) {
    return false;
  }

  // Keep elements with rich text editing.
  // This is an O(1) check that will return true for matching elements and
  // avoid the O(n) IsEditable() check below.
  // It is unlikely that password managers will need elements within
  // the content editable, but if we do then consider adding a check
  // for IsEditable(). IsEditable() is O(n) where n is the number of
  // ancestors so it should only be added if necessary.
  // We may also consider caching IsEditable value so that the
  // HasContentEditableAttributeSet call can potentially be folded into a single
  // IsEditable call. See crbug/1420757.
  if (HasContentEditableAttributeSet()) {
    return false;
  }

  return true;
}

bool AXObject::ComputeAccessibilityIsIgnoredButIncludedInTree() const {
  if (RuntimeEnabledFeatures::AccessibilityExposeIgnoredNodesEnabled())
    return true;

  // If an inline text box is ignored, it is never included in the tree.
  if (IsAXInlineTextBox()) {
    return false;
  }

  if (AXObjectCache().IsAriaOwned(this) || HasARIAOwns(GetElement())) {
    // Always include an aria-owned object. It must be a child of the
    // element with aria-owns.
    return true;
  }

  const Node* node = GetNode();

  if (!node) {
    if (GetLayoutObject()) {
      // All AXObjects created for anonymous layout objects are included.
      // See IsLayoutObjectRelevantForAccessibility() in
      // ax_object_cache_impl.cc.
      // - Visible content, such as text, images and quotes (can't have
      // children).
      // - Any containers inside of pseudo-elements.
      DCHECK(GetLayoutObject()->IsAnonymous())
          << "Object has layout object but no node and is not anonymous: "
          << GetLayoutObject();
    } else {
      // Include ignored mock objects, virtual objects and inline text boxes.
      DCHECK(IsMockObject() || IsVirtualObject())
          << "Nodeless, layout-less object found with role " << RoleValue();
    }
    // By including all of these objects in the tree, it is ensured that
    // ClearChildren() will be able to find these children and detach them
    // from their parent.
    return true;
  }

  // Labels are sometimes marked ignored, to prevent duplication when the AT
  // reads the label and the control it labels (see
  // AXNodeObject::IsRedundantLabel), but we will need them to calculate the
  // name of the control.
  if (IsA<HTMLLabelElement>(node)) {
    return true;
  }

  // Custom elements and their children are included in the tree.
  // <slot>s and their children are included in the tree.
  // Also children of <label> elements, for accname calculation purposes.
  // This checks to see whether this is a child of one of those.
  if (Node* parent_node = LayoutTreeBuilderTraversal::Parent(*node)) {
    if (parent_node->IsCustomElement() ||
        ToHTMLSlotElementIfSupportsAssignmentOrNull(parent_node)) {
      return true;
    }
    // <span>s are ignored because they are considered uninteresting. Do not add
    // them back inside labels.
    if (IsA<HTMLLabelElement>(parent_node) && !IsA<HTMLSpanElement>(node)) {
      return true;
    }
    // Simplify AXNodeObject::AddImageMapChildren() -- it will only need to deal
    // with included children.
    if (IsA<HTMLMapElement>(parent_node)) {
      return true;
    }
    // Necessary to calculate the accessible description of a ruby node.
    if (IsA<HTMLRTElement>(parent_node)) {
      return true;
    }
  }

  if (const Element* owner = node->OwnerShadowHost()) {
    // The ignored state of media controls can change without a layout update.
    // Keep them in the tree at all times so that the serializer isn't
    // accidentally working with unincluded nodes, which is not allowed.
    if (IsA<HTMLMediaElement>(owner)) {
      return true;
    }

    // Do not include ignored descendants of an <input type="search"> or
    // <input type="number"> because they interfere with AXPosition code that
    // assumes a plain input field structure. Specifically, due to the ignored
    // node at the end of textfield, end of editable text position will get
    // adjusted to past text field or caret moved events will not be emitted for
    // the final offset because the associated tree position. In some cases
    // platform accessibility code will instead incorrectly emit a caret moved
    // event for the AXPosition which follows the input.
    if (IsA<HTMLInputElement>(owner) &&
        (DynamicTo<HTMLInputElement>(owner)->type() ==
             input_type_names::kSearch ||
         DynamicTo<HTMLInputElement>(owner)->type() ==
             input_type_names::kNumber)) {
      return false;
    }
  }

  Element* element = GetElement();

  // Custom elements and their children are included in the tree.
  if (element && element->IsCustomElement()) {
    return true;
  }

  // <slot>s and their children are included in the tree.
  // Detailed explanation:
  // <slot> elements are placeholders marking locations in a shadow tree where
  // users of a web component can insert their own custom nodes. Inserted nodes
  // (also known as distributed nodes) become children of their respective slots
  // in the accessibility tree. In other words, the accessibility tree mirrors
  // the flattened DOM tree or the layout tree, not the original DOM tree.
  // Distributed nodes still maintain their parent relations and computed style
  // information with their original location in the DOM. Therefore, we need to
  // ensure that in the accessibility tree no remnant information from the
  // unflattened DOM tree remains, such as the cached parent.
  if (ToHTMLSlotElementIfSupportsAssignmentOrNull(element)) {
    return true;
  }

  // Ensure clean teardown of AXMenuList.
  if (auto* option = DynamicTo<HTMLOptionElement>(element)) {
    if (option->OwnerSelectElement()) {
      return true;
    }
  }

  // Include all pseudo element content. Any anonymous subtree is included
  // from above, in the condition where there is no node.
  if (element && element->IsPseudoElement()) {
    return true;
  }

  // Include all parents of ::before/::after/::marker pseudo elements to help
  // ClearChildren() find all children, and assist naming computation.
  // It is unnecessary to include a rule for other types of pseudo elements:
  // Specifically, ::first-letter/::backdrop are not visited by
  // LayoutTreeBuilderTraversal, and cannot be in the tree, therefore do not add
  // a special rule to include their parents.
  if (element && (element->GetPseudoElement(kPseudoIdBefore) ||
                  element->GetPseudoElement(kPseudoIdAfter) ||
                  element->GetPseudoElement(kPseudoIdMarker))) {
    return true;
  }

  if (IsExcludedByFormControlsFilter()) {
    return false;
  }

  // Allow the browser side ax tree to access "visibility: [hidden|collapse]"
  // and "display: none" nodes. This is useful for APIs that return the node
  // referenced by aria-labeledby and aria-describedby.
  // The conditions are oversimplified, we will include more nodes than
  // strictly necessary for aria-labelledby and aria-describedby but we
  // avoid performing very complicated checks that could impact performance.

  // We identify nodes in display none subtrees, or nodes that are display
  // locked, because they lack a layout object.
  if (!GetLayoutObject()) {
    // Datalists and options inside them will never a layout object. They
    // match the condition above, but we don't need them for accessible
    // naming nor have any other use in the accessibility tree, so we exclude
    // them specifically. What's more, including them breaks the browser test
    // SelectToSpeakKeystrokeSelectionTest.textFieldWithComboBoxSimple.
    // Selection and position code takes into account ignored nodes, and it
    // looks like including ignored nodes for datalists and options is totally
    // unexpected, making selections misbehave.
    if (!IsA<HTMLDataListElement>(node) && !IsA<HTMLOptionElement>(node))
      return true;

  } else {  // GetLayoutObject() != null
    // We identify hidden or collapsed nodes by their associated style values.
    if (GetLayoutObject()->Style()->Visibility() != EVisibility::kVisible)
      return true;

    // Allow the browser side ax tree to access "aria-hidden" nodes.
    // This is useful for APIs that return the node referenced by
    // aria-labeledby and aria-describedby.
    // Exception: iframes, in order to stop exposing aria-hidden iframes, where
    // there is no possibility for the content within to know it's aria-hidden.
    if (IsAriaHidden()) {
      return !IsChildTreeOwner();
    }
  }

  // Portals don't directly expose their contents as the contents are not
  // focusable, but they use them to compute a default accessible name.
  if (GetDocument()->GetPage() && GetDocument()->GetPage()->InsidePortal())
    return true;

  if (!element)
    return false;

  // Use a flag to control whether or not the <html> element is included
  // in the accessibility tree. Either way it's always marked as "ignored",
  // but eventually we want to always include it in the tree to simplify
  // some logic.
  if (IsA<HTMLHtmlElement>(element))
    return RuntimeEnabledFeatures::AccessibilityExposeHTMLElementEnabled();

  // Keep the internal accessibility tree consistent for videos which lack
  // a player and also inner text.
  if (RoleValue() == ax::mojom::blink::Role::kVideo ||
      RoleValue() == ax::mojom::blink::Role::kAudio) {
    return true;
  }

  // Always pass through Line Breaking objects, this is necessary to
  // detect paragraph edges, which are defined as hard-line breaks.
  if (IsLineBreakingObject())
    return true;

  // Ruby annotations (i.e. <rt> elements) need to be included because they are
  // used for calculating an accessible description for the ruby. We explicitly
  // exclude from the tree any <rp> elements, even though they also have the
  // kRubyAnnotation role, because such elements provide fallback content for
  // browsers that do not support ruby. Hence, their contents should not be
  // included in the accessible description, unless another condition in this
  // method decides to keep them in the tree for some reason.
  if (IsA<HTMLRTElement>(element))
    return true;

  // Preserve SVG grouping elements.
  if (IsA<SVGGElement>(element))
    return true;

  // Keep table-related elements in the tree, because it's too easy for them
  // to in and out of being ignored based on their ancestry, as their role
  // can depend on several levels up in the hierarchy.
  if (IsA<HTMLTableElement>(element) || IsA<HTMLTableSectionElement>(element) ||
      IsA<HTMLTableRowElement>(element) || IsA<HTMLTableCellElement>(element)) {
    return true;
  }

  // Preserve nodes with language attributes.
  if (HasAttribute(html_names::kLangAttr))
    return true;

  return false;
}

const AXObject* AXObject::GetAtomicTextFieldAncestor(
    int max_levels_to_check) const {
  if (IsAtomicTextField())
    return this;

  if (max_levels_to_check == 0)
    return nullptr;

  if (AXObject* parent = ParentObject())
    return parent->GetAtomicTextFieldAncestor(max_levels_to_check - 1);

  return nullptr;
}

const AXObject* AXObject::DatetimeAncestor() const {
  ShadowRoot* shadow_root = GetNode()->ContainingShadowRoot();
  if (!shadow_root || shadow_root->GetType() != ShadowRootType::kUserAgent) {
    return nullptr;
  }
  auto* input = DynamicTo<HTMLInputElement>(&shadow_root->host());
  if (!input) {
    return nullptr;
  }
  if (input->type() != input_type_names::kDatetimeLocal &&
      input->type() != input_type_names::kDatetime &&
      input->type() != input_type_names::kDate &&
      input->type() != input_type_names::kTime &&
      input->type() != input_type_names::kMonth &&
      input->type() != input_type_names::kWeek) {
    return nullptr;
  }
  return AXObjectCache().GetOrCreate(input);
}

bool AXObject::LastKnownIsIgnoredValue() const {
  DCHECK(cached_is_ignored_ || !IsDetached())
      << "A detached object should always indicate that it is ignored so that "
         "it won't ever accidentally be included in the tree.";
  return cached_is_ignored_;
}

bool AXObject::LastKnownIsIgnoredButIncludedInTreeValue() const {
  DCHECK(!cached_is_ignored_but_included_in_tree_ || !IsDetached())
      << "A detached object should never be included in the tree.";
  return cached_is_ignored_but_included_in_tree_;
}

bool AXObject::LastKnownIsIncludedInTreeValue() const {
  return !LastKnownIsIgnoredValue() ||
         LastKnownIsIgnoredButIncludedInTreeValue();
}

ax::mojom::blink::Role AXObject::DetermineAccessibilityRole() {
#if DCHECK_IS_ON()
  base::AutoReset<bool> reentrancy_protector(&is_computing_role_, true);
  DCHECK(!IsDetached());
  // Check parent object to work around circularity issues during
  // AXObject::Init (DetermineAccessibilityRole is called there but before
  // the parent is set).
  if (CachedParentObject()) {
    DCHECK(GetDocument());
    DCHECK(GetDocument()->Lifecycle().GetState() >=
           DocumentLifecycle::kLayoutClean)
        << "Unclean document at lifecycle "
        << GetDocument()->Lifecycle().ToString();
  }
#endif

  return NativeRoleIgnoringAria();
}

bool AXObject::CanSetValueAttribute() const {
  switch (RoleValue()) {
    case ax::mojom::blink::Role::kColorWell:
    case ax::mojom::blink::Role::kDate:
    case ax::mojom::blink::Role::kDateTime:
    case ax::mojom::blink::Role::kInputTime:
    case ax::mojom::blink::Role::kScrollBar:
    case ax::mojom::blink::Role::kSearchBox:
    case ax::mojom::blink::Role::kSlider:
    case ax::mojom::blink::Role::kSpinButton:
    case ax::mojom::blink::Role::kSplitter:
    case ax::mojom::blink::Role::kTextField:
    case ax::mojom::blink::Role::kTextFieldWithComboBox:
      return Restriction() == kRestrictionNone;
    default:
      return false;
  }
}

bool AXObject::IsFocusableStyleUsingBestAvailableState() const {
  auto* element = GetElement();
  DCHECK(element);

  // If this element's layout tree does not need an update, it means that we can
  // rely on Element's IsFocusableStyle directly, which is the best available
  // source of information.
  // Note that we also allow this to be used if we're in a style recalc, since
  // we might get here through layout object attachment. In that case, the dirty
  // bits may not have been cleared yet, but all relevant style and layout tree
  // should be up to date. Note that this quirk can be fixed by deferring AX
  // tree updates to happen after the layout tree attachment has finished.
  if (GetDocument()->InStyleRecalc() ||
      !GetDocument()->NeedsLayoutTreeUpdateForNodeIncludingDisplayLocked(
          *element)) {
    return element->IsFocusableStyle();
  }

  // The best available source of information is now the AX tree, so use that to
  // figure out whether we have focusable style.
  return element->IsBaseElementFocusableStyle();
}

bool AXObject::CanSetFocusAttribute() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_can_set_focus_attribute_;
}

// This does not use Element::IsFocusable(), as that can sometimes recalculate
// styles because of IsFocusableStyle() check, resetting the document lifecycle.
bool AXObject::ComputeCanSetFocusAttribute() const {
  DCHECK(!IsDetached());
  DCHECK(GetDocument());

  // Objects within a portal are not focusable.
  // Note that they are ignored but can be included in the tree.
  bool inside_portal =
      GetDocument()->GetPage() && GetDocument()->GetPage()->InsidePortal();
  if (inside_portal)
    return false;

  // The portal itself is focusable. Portals are treated as buttons in platform
  // APIs, hiding their subtree.
  if (RoleValue() == ax::mojom::blink::Role::kPortal)
    return true;

  // Display-locked nodes that have content-visibility: hidden are not exposed
  // to accessibility in any way, so they are not focusable. Note that for
  // content-visibility: auto cases, `ShouldIgnoreNodeDueToDisplayLock()` would
  // return false, since we're not ignoring the element in that case.
  if (GetNode() &&
      DisplayLockUtilities::ShouldIgnoreNodeDueToDisplayLock(
          *GetNode(), DisplayLockActivationReason::kAccessibility)) {
    return false;
  }

  // Focusable: web area -- this is the only focusable non-element. Web areas
  // inside portals are not focusable though (portal contents cannot get focus).
  if (IsWebArea())
    return true;

  // NOT focusable: objects with no DOM node, e.g. extra layout blocks inserted
  // as filler, or objects where the node is not an element, such as a text
  // node or an HTML comment.
  Element* elem = GetElement();
  if (!elem)
    return false;

  if (cached_is_inert_) {
    return false;
  }

  // NOT focusable: child tree owners (it's the content area that will be marked
  // focusable in the a11y tree).
  if (IsChildTreeOwner())
    return false;

  // NOT focusable: disabled form controls.
  if (IsDisabledFormControl(elem))
    return false;

  // Option elements do not receive DOM focus but they do receive a11y focus,
  // unless they are part of a <datalist>, in which case they can be displayed
  // by the browser process, but not the renderer.
  // TODO(crbug.com/1399852) Address gaps in datalist a11y.
  if (auto* option = DynamicTo<HTMLOptionElement>(elem))
    return !option->OwnerDataListElement();

  // NOT focusable: hidden elements.
  // TODO(aleventhal) Consider caching visibility when it's safe to compute.
  if (!IsA<HTMLAreaElement>(elem) && !IsFocusableStyleUsingBestAvailableState())
    return false;

  // Focusable: element supports focus.
  if (elem->SupportsFocus())
    return true;

  // NOT focusable: everything else.
  return false;
}

// We can't use `Element::IsKeyboardFocusable()` since the downstream
// `Element::IsFocusableStyle()` call will reset the document lifecycle.
// TODO(crbug.com/1444450) This should be replaced with just a call to
// element->IsKeyboardFocusable(). This function can be guarded by a
// DocumentLifecycle::DisallowTransitionScope().
bool AXObject::IsKeyboardFocusable() const {
  if (!CanSetFocusAttribute())
    return false;

  Element* element = GetElement();
  if (!element) {
    // TODO(crbug.com/1480563) It is possible to reach this line, though it
    // isn't clear how. See the linked bug for crash reports. Since we would
    // prefer not to crash in this case, return false here instead.
    LOG(ERROR) << "Cannot be focusable without an element: "
               << ToString(true, true);
    return false;
  }
  if (element->IsScrollableContainerThatShouldBeKeyboardFocusable()) {
    return true;
  }
  return element->tabIndex() >= 0 || IsRootEditableElement(*element);
}

bool AXObject::CanSetSelectedAttribute() const {
  // Sub-widget elements can be selected if not disabled (native or ARIA)
  return IsSubWidget() && Restriction() != kRestrictionDisabled;
}

bool AXObject::IsSubWidget() const {
  switch (RoleValue()) {
    case ax::mojom::blink::Role::kCell:
    case ax::mojom::blink::Role::kColumnHeader:
    case ax::mojom::blink::Role::kRowHeader:
    case ax::mojom::blink::Role::kColumn:
    case ax::mojom::blink::Role::kRow: {
      // If it has an explicit ARIA role, it's a subwidget.
      //
      // Reasoning:
      // Static table cells are not selectable, but ARIA grid cells
      // and rows definitely are according to the spec. To support
      // ARIA 1.0, it's sufficient to just check if there's any
      // ARIA role at all, because if so then it must be a grid-related
      // role so it must be selectable.
      //
      // TODO(accessibility): an ARIA 1.1+ role of "cell", or a role of "row"
      // inside an ARIA 1.1 role of "table", should not be selectable. We may
      // need to create separate role enums for grid cells vs table cells
      // to implement this.
      if (AriaRoleAttribute() != ax::mojom::blink::Role::kUnknown)
        return true;

      // Otherwise it's only a subwidget if it's in a grid or treegrid,
      // not in a table.
      AncestorsIterator ancestor = base::ranges::find_if(
          UnignoredAncestorsBegin(), UnignoredAncestorsEnd(),
          &AXObject::IsTableLikeRole);
      return ancestor.current_ &&
             (ancestor.current_->RoleValue() == ax::mojom::blink::Role::kGrid ||
              ancestor.current_->RoleValue() ==
                  ax::mojom::blink::Role::kTreeGrid);
    }
    case ax::mojom::blink::Role::kListBoxOption:
    case ax::mojom::blink::Role::kMenuListOption:
    case ax::mojom::blink::Role::kTab:
    case ax::mojom::blink::Role::kTreeItem:
      return true;
    default:
      return false;
  }
}

bool AXObject::SupportsARIASetSizeAndPosInSet() const {
  if (RoleValue() == ax::mojom::blink::Role::kRow) {
    AncestorsIterator ancestor = base::ranges::find_if(
        UnignoredAncestorsBegin(), UnignoredAncestorsEnd(),
        &AXObject::IsTableLikeRole);
    return ancestor.current_ &&
           ancestor.current_->RoleValue() == ax::mojom::blink::Role::kTreeGrid;
  }
  return ui::IsSetLike(RoleValue()) || ui::IsItemLike(RoleValue());
}

bool AXObject::IsProhibited(ax::mojom::blink::StringAttribute attribute) const {
  // ARIA 1.2 prohibits aria-roledescription on the "generic" role.
  if (attribute == ax::mojom::blink::StringAttribute::kRoleDescription)
    return RoleValue() == ax::mojom::blink::Role::kGenericContainer;
  return false;
}

bool AXObject::IsProhibited(
    ax::mojom::blink::IntListAttribute attribute) const {
  // ARIA 1.2 prohibits exposure of aria-errormessage when aria-invalid is
  // false.
  if (attribute == ax::mojom::blink::IntListAttribute::kErrormessageIds) {
    return GetInvalidState() == ax::mojom::blink::InvalidState::kFalse;
  }
  return false;
}

// Simplify whitespace, but preserve a single leading and trailing whitespace
// character if it's present.
String AXObject::SimplifyName(const String& str) const {
  if (str.empty())
    return "";

  // Do not simplify name for text, unless it is pseudo content.
  // TODO(accessibility) There seems to be relatively little value for the
  // special pseudo content rule, and that the null check for node can
  // probably be removed without harm.
  if (GetNode() && ui::IsText(RoleValue()))
    return str;

  bool has_before_space = IsHTMLSpace<UChar>(str[0]);
  bool has_after_space = IsHTMLSpace<UChar>(str[str.length() - 1]);
  String simplified = str.SimplifyWhiteSpace(IsHTMLSpace<UChar>);
  if (!has_before_space && !has_after_space)
    return simplified;

  // Preserve a trailing and/or leading space.
  StringBuilder result;
  if (has_before_space)
    result.Append(' ');
  result.Append(simplified);
  if (has_after_space)
    result.Append(' ');
  return result.ToString();
}

String AXObject::ComputedName() const {
  ax::mojom::blink::NameFrom name_from;
  AXObjectVector name_objects;
  return GetName(name_from, &name_objects);
}

String AXObject::GetName(ax::mojom::blink::NameFrom& name_from,
                         AXObject::AXObjectVector* name_objects) const {
  HeapHashSet<Member<const AXObject>> visited;
  AXRelatedObjectVector related_objects;

  // Initialize |name_from|, as TextAlternative() might never set it in some
  // cases.
  name_from = ax::mojom::blink::NameFrom::kNone;
  String text = TextAlternative(false, nullptr, visited, name_from,
                                &related_objects, nullptr);

  if (name_objects) {
    name_objects->clear();
    for (NameSourceRelatedObject* related_object : related_objects)
      name_objects->push_back(related_object->object);
  }

  return SimplifyName(text);
}

String AXObject::GetName(NameSources* name_sources) const {
  AXObjectSet visited;
  ax::mojom::blink::NameFrom tmp_name_from;
  AXRelatedObjectVector tmp_related_objects;
  String text = TextAlternative(false, nullptr, visited, tmp_name_from,
                                &tmp_related_objects, name_sources);
  return SimplifyName(text);
}

String AXObject::RecursiveTextAlternative(
    const AXObject& ax_obj,
    const AXObject* aria_label_or_description_root,
    AXObjectSet& visited) {
  ax::mojom::blink::NameFrom tmp_name_from = ax::mojom::blink::NameFrom::kNone;
  return RecursiveTextAlternative(ax_obj, aria_label_or_description_root,
                                  visited, tmp_name_from);
}

String AXObject::RecursiveTextAlternative(
    const AXObject& ax_obj,
    const AXObject* aria_label_or_description_root,
    AXObjectSet& visited,
    ax::mojom::blink::NameFrom& name_from) {
  if (visited.Contains(&ax_obj) && !aria_label_or_description_root)
    return String();

  return ax_obj.TextAlternative(true, aria_label_or_description_root, visited,
                                name_from, nullptr, nullptr);
}

const ComputedStyle* AXObject::GetComputedStyle() const {
  Node* node = GetNode();
  if (!node)
    return nullptr;

#if DCHECK_IS_ON()
  DCHECK(GetDocument());
  DCHECK(GetDocument()->Lifecycle().GetState() >=
         DocumentLifecycle::kLayoutClean)
      << "Unclean document at lifecycle "
      << GetDocument()->Lifecycle().ToString();
#endif

  // content-visibility:hidden or content-visibility: auto.
  if (DisplayLockUtilities::IsDisplayLockedPreventingPaint(node))
    return nullptr;

  // For elements with layout objects we can get their style directly.
  if (GetLayoutObject())
    return GetLayoutObject()->Style();

  return node->GetComputedStyle();
}

// There are 4 ways to use CSS to hide something:
// * "display: none" is "destroy rendering state and don't do anything in the
//   subtree"
// * "visibility: [hidden|collapse]" are "don't visually show things, but still
//   keep all of the rendering up to date"
// * "content-visibility: hidden" is "don't show anything, skip all of the
//   work, but don't destroy the work that was already there"
// * "content-visibility: auto" is "paint when it's scrolled into the viewport,
//   but its layout information is not updated when it isn't"
bool AXObject::ComputeIsHiddenViaStyle(const ComputedStyle* style) const {
  // The the parent element of text is hidden, then the text is hidden too.
  // This helps provide more consistent results in edge cases, e.g. text inside
  // of a <canvas> or display:none content.
  if (RoleValue() == ax::mojom::blink::Role::kStaticText) {
    // TODO(accessibility) All text objects should have a parent, and therefore
    // the extra null check should be unnecessary.
    DCHECK(ParentObject());
    if (ParentObject() && ParentObject()->IsHiddenViaStyle()) {
      return true;
    }
  }

  if (style) {
    if (GetLayoutObject())
      return style->Visibility() != EVisibility::kVisible;

    // TODO(crbug.com/1286465): It's not consistent to only check
    // IsEnsuredInDisplayNone() on layoutless elements.
    return GetNode()->IsElementNode() &&
           (style->IsEnsuredInDisplayNone() ||
            style->Visibility() != EVisibility::kVisible);
  }

  Node* node = GetNode();
  if (!node)
    return false;

  // content-visibility:hidden or content-visibility: auto.
  if (DisplayLockUtilities::IsDisplayLockedPreventingPaint(node)) {
    // Ensure contents of head, style and script are not exposed when
    // display-locked --the only time they are ever exposed is if author
    // explicitly makes them visible.
    DCHECK(!Traversal<SVGStyleElement>::FirstAncestorOrSelf(*node)) << node;
    DCHECK(!Traversal<HTMLHeadElement>::FirstAncestorOrSelf(*node)) << node;
    DCHECK(!Traversal<HTMLStyleElement>::FirstAncestorOrSelf(*node)) << node;
    DCHECK(!Traversal<HTMLScriptElement>::FirstAncestorOrSelf(*node)) << node;

    // content-visibility: hidden subtrees are always hidden.
    // content-visibility: auto subtrees are treated as visible, as we must
    // make a guess since computed style is not available.
    return DisplayLockUtilities::ShouldIgnoreNodeDueToDisplayLock(
        *node, DisplayLockActivationReason::kAccessibility);
  }

  return node->IsElementNode();
}

bool AXObject::IsHiddenViaStyle() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_is_hidden_via_style_;
}

// Return true if this should be removed from accessible name computations.
// We must take into account if we are traversing an aria-labelledby or
// describedby relation, because those can use hidden subtrees. When the target
// node of the aria-labelledby or describedby relation is hidden, we contribute
// all its children, because there is no way to know if they are explicitly
// hidden or they inherited the hidden value. See:
// https://github.com/w3c/accname/issues/57
bool AXObject::IsHiddenForTextAlternativeCalculation(
    const AXObject* aria_label_or_description_root) const {
  auto* node = GetNode();
  if (!node)
    return false;

  // Display-locked elements are available for text/name resolution.
  if (DisplayLockUtilities::IsDisplayLockedPreventingPaint(node))
    return false;

  Document* document = GetDocument();
  if (!document || !document->GetFrame())
    return false;

  // Do not contribute <noscript> to text alternative of an ancestor.
  if (IsA<HTMLNoScriptElement>(node))
    return true;

  // Always contribute SVG <title> despite it having a hidden style by default.
  if (IsA<SVGTitleElement>(node))
    return false;

  // Always contribute SVG <desc> despite it having a hidden style by default.
  if (IsA<SVGDescElement>(node))
    return false;

  // Always contribute text nodes, because they don't have display-related
  // properties of their own, only their parents do. Parents should have been
  // checked for their contribution earlier in the process.
  if (IsA<Text>(node))
    return false;

  // Markers do not contribute to the accessible name.
  // TODO(accessibility): Chrome has never included markers, but that's
  // actually undefined behavior. We will have to revisit after this is
  // settled, see: https://github.com/w3c/accname/issues/76
  if (node->IsMarkerPseudoElement())
    return true;

  // Step 2A from: http://www.w3.org/TR/accname-aam-1.1
  // When traversing an aria-labelledby relation where the targeted node is
  // hidden, we must contribute its children. There is no way to know if they
  // are explicitly hidden or they inherited the hidden value, so we resort to
  // contributing them all. See also: https://github.com/w3c/accname/issues/57
  if (aria_label_or_description_root &&
      !aria_label_or_description_root->IsVisible()) {
    return false;
  }

  // aria-hidden nodes are generally excluded, with the exception:
  // when computing name/description through an aria-labelledby/describedby
  // relation, if the target of the relation is hidden it will expose the entire
  // subtree, including aria-hidden=true nodes. The exception was accounted in
  // the previous if block, so we are safe to hide any node with
  // aria-hidden=true at this point.
  if (AOMPropertyOrARIAAttributeIsTrue(AOMBooleanProperty::kHidden)) {
    // We only hide aria-hidden text if the node does not support focus as a
    // bad authoring correction.
    if (!CanSetFocusAttribute())
      return true;
  } else {
    // When IsAriaHidden() returns false, we only know the node is not in an
    // aria-hidden="true" subtree. We need to check for the case where
    // aria-hidden="false" specifically.
    if (AOMPropertyOrARIAAttributeIsFalse(AOMBooleanProperty::kHidden))
      return false;
  }

  return IsHiddenViaStyle();
}

String AXObject::AriaTextAlternative(
    bool recursive,
    const AXObject* aria_label_or_description_root,
    AXObjectSet& visited,
    ax::mojom::blink::NameFrom& name_from,
    AXRelatedObjectVector* related_objects,
    NameSources* name_sources,
    bool* found_text_alternative) const {
  String text_alternative;
  bool already_visited = visited.Contains(this);
  visited.insert(this);

  // Slots are elements that cannot be named.
  if (IsA<HTMLSlotElement>(GetNode())) {
    *found_text_alternative = false;
    return String();
  }

  // Step 2A from: http://www.w3.org/TR/accname-aam-1.1
  // If you change this logic, update AXNodeObject::nameFromLabelElement, too.
  if (IsHiddenForTextAlternativeCalculation(aria_label_or_description_root)) {
    *found_text_alternative = true;
    return String();
  }

  // Step 2B from: http://www.w3.org/TR/accname-aam-1.1
  // If you change this logic, update AXNodeObject::nameFromLabelElement, too.
  if (!aria_label_or_description_root && !already_visited) {
    name_from = ax::mojom::blink::NameFrom::kRelatedElement;

    // Check ARIA attributes.
    const QualifiedName& attr =
        HasAttribute(html_names::kAriaLabeledbyAttr) &&
                !HasAttribute(html_names::kAriaLabelledbyAttr)
            ? html_names::kAriaLabeledbyAttr
            : html_names::kAriaLabelledbyAttr;

    if (name_sources) {
      name_sources->push_back(NameSource(*found_text_alternative, attr));
      name_sources->back().type = name_from;
    }

    Element* element = GetElement();
    if (element) {
      HeapVector<Member<Element>> elements_from_attribute;
      ElementsFromAttribute(element, elements_from_attribute, attr);

      const AtomicString& aria_labelledby = GetAttribute(attr);

      if (!aria_labelledby.IsNull()) {
        if (name_sources)
          name_sources->back().attribute_value = aria_labelledby;

        // Operate on a copy of |visited| so that if |name_sources| is not
        // null, the set of visited objects is preserved unmodified for future
        // calculations.
        AXObjectSet visited_copy = visited;
        text_alternative = TextFromElements(
            true, visited_copy, elements_from_attribute, related_objects);
        if (!text_alternative.IsNull()) {
          if (name_sources) {
            NameSource& source = name_sources->back();
            source.type = name_from;
            source.related_objects = *related_objects;
            source.text = text_alternative;
            *found_text_alternative = true;
          } else {
            *found_text_alternative = true;
            return text_alternative;
          }
        } else if (name_sources) {
          name_sources->back().invalid = true;
        }
      }
    }
  }

  // Step 2C from: http://www.w3.org/TR/accname-aam-1.1
  // If you change this logic, update AXNodeObject::nameFromLabelElement, too.
  name_from = ax::mojom::blink::NameFrom::kAttribute;
  if (name_sources) {
    name_sources->push_back(
        NameSource(*found_text_alternative, html_names::kAriaLabelAttr));
    name_sources->back().type = name_from;
  }
  const AtomicString& aria_label =
      GetAOMPropertyOrARIAAttribute(AOMStringProperty::kLabel);
  if (!aria_label.empty()) {
    text_alternative = aria_label;

    if (name_sources) {
      NameSource& source = name_sources->back();
      source.text = text_alternative;
      source.attribute_value = aria_label;
      *found_text_alternative = true;
    } else {
      *found_text_alternative = true;
      return text_alternative;
    }
  }

  return text_alternative;
}

String AXObject::TextFromElements(
    bool in_aria_labelledby_traversal,
    AXObjectSet& visited,
    HeapVector<Member<Element>>& elements,
    AXRelatedObjectVector* related_objects) const {
  StringBuilder accumulated_text;
  bool found_valid_element = false;
  AXRelatedObjectVector local_related_objects;

  for (const auto& element : elements) {
    AXObject* ax_element = AXObjectCache().GetOrCreate(element);
    if (ax_element) {
      found_valid_element = true;
      AXObject* aria_labelled_by_node = nullptr;
      if (in_aria_labelledby_traversal)
        aria_labelled_by_node = ax_element;
      String result =
          RecursiveTextAlternative(*ax_element, aria_labelled_by_node, visited);
      visited.insert(ax_element);
      local_related_objects.push_back(
          MakeGarbageCollected<NameSourceRelatedObject>(ax_element, result));
      if (!result.empty()) {
        if (!accumulated_text.empty())
          accumulated_text.Append(' ');
        accumulated_text.Append(result);
      }
    }
  }
  if (!found_valid_element)
    return String();
  if (related_objects)
    *related_objects = local_related_objects;
  return accumulated_text.ToString();
}

// static
void AXObject::TokenVectorFromAttribute(Element* element,
                                        Vector<String>& tokens,
                                        const QualifiedName& attribute) {
  if (!element)
    return;

  String attribute_value = element->FastGetAttribute(attribute).GetString();
  if (attribute_value.empty())
    return;

  attribute_value = attribute_value.SimplifyWhiteSpace();
  attribute_value.Split(' ', tokens);
}

// static
bool AXObject::ElementsFromAttribute(Element* from,
                                     HeapVector<Member<Element>>& elements,
                                     const QualifiedName& attribute) {
  if (!from)
    return false;

  HeapVector<Member<Element>>* attr_associated_elements =
      from->GetElementArrayAttribute(attribute);
  if (!attr_associated_elements)
    return false;

  for (const auto& element : *attr_associated_elements)
    elements.push_back(element);

  return elements.size();
}

// static
bool AXObject::AriaLabelledbyElementVector(
    Element* from,
    HeapVector<Member<Element>>& elements) {
  // Try both spellings, but prefer aria-labelledby, which is the official spec.
  if (ElementsFromAttribute(from, elements, html_names::kAriaLabelledbyAttr) &&
      elements.size() > 0) {
    return true;
  }

  return ElementsFromAttribute(from, elements,
                               html_names::kAriaLabeledbyAttr) &&
         elements.size() > 0;
}

// static
bool AXObject::IsNameFromAriaAttribute(Element* element) {
  // TODO(accessibility) Make this work for virtual nodes.

  if (!element)
    return false;

  HeapVector<Member<Element>> elements_from_attribute;
  if (AriaLabelledbyElementVector(element, elements_from_attribute)) {
    return true;
  }

  const AtomicString& aria_label = AccessibleNode::GetPropertyOrARIAAttribute(
      element, AOMStringProperty::kLabel);
  if (!aria_label.empty())
    return true;

  return false;
}

bool AXObject::IsNameFromAuthorAttribute() const {
  return IsNameFromAriaAttribute(GetElement()) ||
         HasAttribute(html_names::kTitleAttr);
}

AXObject* AXObject::InPageLinkTarget() const {
  return nullptr;
}

const AtomicString& AXObject::EffectiveTarget() const {
  return g_null_atom;
}

AccessibilityOrientation AXObject::Orientation() const {
  // In ARIA 1.1, the default value for aria-orientation changed from
  // horizontal to undefined.
  return kAccessibilityOrientationUndefined;
}

AXObject* AXObject::GetChildFigcaption() const { return nullptr; }

bool AXObject::IsDescendantOfLandmarkDisallowedElement() const {
  return false;
}

void AXObject::LoadInlineTextBoxes() {}

void AXObject::ForceAddInlineTextBoxChildren() {}

AXObject* AXObject::NextOnLine() const {
  return nullptr;
}

AXObject* AXObject::PreviousOnLine() const {
  return nullptr;
}

absl::optional<const DocumentMarker::MarkerType>
AXObject::GetAriaSpellingOrGrammarMarker() const {
  AtomicString aria_invalid_value;
  const AncestorsIterator iter = std::find_if(
      UnignoredAncestorsBegin(), UnignoredAncestorsEnd(),
      [&aria_invalid_value](const AXObject& ancestor) {
        return ancestor.HasAOMPropertyOrARIAAttribute(
                   AOMStringProperty::kInvalid, aria_invalid_value) ||
               ancestor.IsLineBreakingObject();
      });

  if (iter == UnignoredAncestorsEnd())
    return absl::nullopt;
  if (EqualIgnoringASCIICase(aria_invalid_value, "spelling"))
    return DocumentMarker::kSpelling;
  if (EqualIgnoringASCIICase(aria_invalid_value, "grammar"))
    return DocumentMarker::kGrammar;
  return absl::nullopt;
}

void AXObject::TextCharacterOffsets(Vector<int>&) const {}

void AXObject::GetWordBoundaries(Vector<int>& word_starts,
                                 Vector<int>& word_ends) const {}

int AXObject::TextLength() const {
  if (IsAtomicTextField())
    return GetValueForControl().length();
  return 0;
}

int AXObject::TextOffsetInFormattingContext(int offset) const {
  DCHECK_GE(offset, 0);
  return offset;
}

int AXObject::TextOffsetInContainer(int offset) const {
  DCHECK_GE(offset, 0);
  return offset;
}

ax::mojom::blink::DefaultActionVerb AXObject::Action() const {
  Element* action_element = ActionElement();

  if (!action_element)
    return ax::mojom::blink::DefaultActionVerb::kNone;

  // TODO(dmazzoni): Ensure that combo box text field is handled here.
  if (IsTextField())
    return ax::mojom::blink::DefaultActionVerb::kActivate;

  if (IsCheckable()) {
    return CheckedState() != ax::mojom::blink::CheckedState::kTrue
               ? ax::mojom::blink::DefaultActionVerb::kCheck
               : ax::mojom::blink::DefaultActionVerb::kUncheck;
  }

  switch (RoleValue()) {
    case ax::mojom::blink::Role::kButton:
    case ax::mojom::blink::Role::kDisclosureTriangle:
    case ax::mojom::blink::Role::kToggleButton:
      return ax::mojom::blink::DefaultActionVerb::kPress;
    case ax::mojom::blink::Role::kListBoxOption:
    case ax::mojom::blink::Role::kMenuItemRadio:
    case ax::mojom::blink::Role::kMenuItem:
    case ax::mojom::blink::Role::kMenuListOption:
      return ax::mojom::blink::DefaultActionVerb::kSelect;
    case ax::mojom::blink::Role::kLink:
      return ax::mojom::blink::DefaultActionVerb::kJump;
    case ax::mojom::blink::Role::kComboBoxMenuButton:
    case ax::mojom::blink::Role::kComboBoxSelect:
    case ax::mojom::blink::Role::kPopUpButton:
      return ax::mojom::blink::DefaultActionVerb::kOpen;
    default:
      if (action_element == GetNode())
        return ax::mojom::blink::DefaultActionVerb::kClick;
      return ax::mojom::blink::DefaultActionVerb::kClickAncestor;
  }
}

bool AXObject::AriaPressedIsPresent() const {
  AtomicString result;
  return HasAOMPropertyOrARIAAttribute(AOMStringProperty::kPressed, result);
}

bool AXObject::AriaCheckedIsPresent() const {
  AtomicString result;
  return HasAOMPropertyOrARIAAttribute(AOMStringProperty::kChecked, result);
}

bool AXObject::SupportsARIAExpanded() const {
  switch (RoleValue()) {
    case ax::mojom::blink::Role::kApplication:
    case ax::mojom::blink::Role::kButton:
    case ax::mojom::blink::Role::kCheckBox:
    case ax::mojom::blink::Role::kColumnHeader:
    case ax::mojom::blink::Role::kComboBoxGrouping:
    case ax::mojom::blink::Role::kComboBoxMenuButton:
    case ax::mojom::blink::Role::kComboBoxSelect:
    case ax::mojom::blink::Role::kDisclosureTriangle:
    case ax::mojom::blink::Role::kListBox:
    case ax::mojom::blink::Role::kLink:
    case ax::mojom::blink::Role::kPopUpButton:
    case ax::mojom::blink::Role::kMenuItem:
    case ax::mojom::blink::Role::kMenuItemCheckBox:
    case ax::mojom::blink::Role::kMenuItemRadio:
    case ax::mojom::blink::Role::kRow:
    case ax::mojom::blink::Role::kRowHeader:
    case ax::mojom::blink::Role::kSwitch:
    case ax::mojom::blink::Role::kTab:
    case ax::mojom::blink::Role::kTextFieldWithComboBox:
    case ax::mojom::blink::Role::kToggleButton:
    case ax::mojom::blink::Role::kTreeItem:
      return true;
    case ax::mojom::blink::Role::kCell:
      // TODO(Accessibility): aria-expanded is supported on grid cells but not
      // on cells inside a static table. Consider creating separate internal
      // roles so that we can easily distinguish these two types. See also
      // IsSubWidget().
      return true;
    default:
      return false;
  }
}

bool DoesUndoRolePresentation(const AtomicString& name) {
  // This is the list of global ARIA properties that force
  // role="presentation"/"none" to be exposed, and does not contain ARIA
  // properties who's global status is being deprecated.
  // clang-format off
  DEFINE_STATIC_LOCAL(
      HashSet<AtomicString>, aria_global_properties,
      ({
        AtomicString("ARIA-ATOMIC"),
        AtomicString("ARIA-BRAILLEROLEDESCRIPTION"),
        AtomicString("ARIA-BUSY"),
        AtomicString("ARIA-CONTROLS"),
        AtomicString("ARIA-CURRENT"),
        AtomicString("ARIA-DESCRIBEDBY"),
        AtomicString("ARIA-DESCRIPTION"),
        AtomicString("ARIA-DETAILS"),
        AtomicString("ARIA-DROPEFFECT"),
        AtomicString("ARIA-FLOWTO"),
        AtomicString("ARIA-GRABBED"),
        AtomicString("ARIA-HIDDEN"),  // For aria-hidden=false.
        AtomicString("ARIA-KEYSHORTCUTS"),
        AtomicString("ARIA-LIVE"),
        AtomicString("ARIA-OWNS"),
        AtomicString("ARIA-RELEVANT"),
        AtomicString("ARIA-ROLEDESCRIPTION")
      }));
  // clang-format on

  return aria_global_properties.Contains(name);
}

bool AXObject::HasAriaAttribute(bool does_undo_role_presentation) const {
  auto* element = GetElement();
  if (!element)
    return false;

  // A role is considered an ARIA attribute.
  if (!does_undo_role_presentation &&
      AriaRoleAttribute() != ax::mojom::blink::Role::kUnknown) {
    return true;
  }

  // Check for any attribute that begins with "aria-".
  AttributeCollection attributes = element->AttributesWithoutUpdate();
  for (const Attribute& attr : attributes) {
    // Attributes cache their uppercase names.
    auto name = attr.GetName().LocalNameUpper();
    if (name.StartsWith("ARIA-")) {
      if (!does_undo_role_presentation || DoesUndoRolePresentation(name))
        return true;
    }
  }

  return false;
}

int AXObject::IndexInParent() const {
  DCHECK(AccessibilityIsIncludedInTree())
      << "IndexInParent is only valid when a node is included in the tree";
  AXObject* ax_parent_included = ParentObjectIncludedInTree();
  if (!ax_parent_included)
    return 0;

  const AXObjectVector& siblings =
      ax_parent_included->ChildrenIncludingIgnored();

  wtf_size_t index = siblings.Find(this);

  DCHECK_NE(index, kNotFound)
      << "Could not find child in parent:"
      << "\nChild: " << ToString(true)
      << "\nParent: " << ax_parent_included->ToString(true)
      << "  #children=" << siblings.size();
  return (index == kNotFound) ? 0 : static_cast<int>(index);
}

bool AXObject::IsLiveRegionRoot() const {
  const AtomicString& live_region = LiveRegionStatus();
  return !live_region.empty();
}

bool AXObject::IsActiveLiveRegionRoot() const {
  const AtomicString& live_region = LiveRegionStatus();
  return !live_region.empty() && !EqualIgnoringASCIICase(live_region, "off");
}

const AtomicString& AXObject::LiveRegionStatus() const {
  DEFINE_STATIC_LOCAL(const AtomicString, live_region_status_assertive,
                      ("assertive"));
  DEFINE_STATIC_LOCAL(const AtomicString, live_region_status_polite,
                      ("polite"));
  DEFINE_STATIC_LOCAL(const AtomicString, live_region_status_off, ("off"));

  const AtomicString& live_region_status =
      GetAOMPropertyOrARIAAttribute(AOMStringProperty::kLive);
  // These roles have implicit live region status.
  if (live_region_status.empty()) {
    switch (RoleValue()) {
      case ax::mojom::blink::Role::kAlert:
        return live_region_status_assertive;
      case ax::mojom::blink::Role::kLog:
      case ax::mojom::blink::Role::kStatus:
        return live_region_status_polite;
      case ax::mojom::blink::Role::kTimer:
      case ax::mojom::blink::Role::kMarquee:
        return live_region_status_off;
      default:
        break;
    }
  }

  return live_region_status;
}

const AtomicString& AXObject::LiveRegionRelevant() const {
  DEFINE_STATIC_LOCAL(const AtomicString, default_live_region_relevant,
                      ("additions text"));
  const AtomicString& relevant =
      GetAOMPropertyOrARIAAttribute(AOMStringProperty::kRelevant);

  // Default aria-relevant = "additions text".
  if (relevant.empty())
    return default_live_region_relevant;

  return relevant;
}

bool AXObject::IsDisabled() const {
  // <embed> or <object> with unsupported plugin, or more iframes than allowed.
  if (IsChildTreeOwner()) {
    if (IsAriaHidden()) {
      return true;
    }
    auto* html_frame_owner_element = To<HTMLFrameOwnerElement>(GetElement());
    return !html_frame_owner_element->ContentFrame();
  }

  Node* node = GetNode();
  if (node) {
    // Check for HTML form control with the disabled attribute.
    if (GetElement() && GetElement()->IsDisabledFormControl()) {
      return true;
    }
    // This is for complex pickers, such as a date picker.
    if (AXObject::IsControl()) {
      Element* owner_shadow_host = node->OwnerShadowHost();
      if (owner_shadow_host &&
          owner_shadow_host->FastHasAttribute(html_names::kDisabledAttr)) {
        return true;
      }
    }
  }
  // Check aria-disabled. According to ARIA in HTML section 3.1, aria-disabled
  // attribute does NOT override the native HTML disabled attribute.
  // https://www.w3.org/TR/html-aria/
  if (AOMPropertyOrARIAAttributeIsTrue(AOMBooleanProperty::kDisabled))
    return true;

  // A focusable object with a disabled container.
  return cached_is_descendant_of_disabled_node_ && CanSetFocusAttribute();
}

AXRestriction AXObject::Restriction() const {
  // According to ARIA, all elements of the base markup can be disabled.
  // According to CORE-AAM, any focusable descendant of aria-disabled
  // ancestor is also disabled.
  if (IsDisabled())
    return kRestrictionDisabled;

  // Check aria-readonly if supported by current role.
  bool is_read_only;
  if (SupportsARIAReadOnly() &&
      HasAOMPropertyOrARIAAttribute(AOMBooleanProperty::kReadOnly,
                                    is_read_only)) {
    // ARIA overrides other readonly state markup.
    return is_read_only ? kRestrictionReadOnly : kRestrictionNone;
  }

  // This is a node that is not readonly and not disabled.
  return kRestrictionNone;
}

ax::mojom::blink::Role AXObject::AriaRoleAttribute() const {
  return ax::mojom::blink::Role::kUnknown;
}

ax::mojom::blink::Role AXObject::RawAriaRole() const {
  const AtomicString& aria_role =
      GetAOMPropertyOrARIAAttribute(AOMStringProperty::kRole);
  if (aria_role.IsNull() || aria_role.empty())
    return ax::mojom::blink::Role::kUnknown;
  return AriaRoleStringToRoleEnum(aria_role);
}

ax::mojom::blink::Role AXObject::DetermineAriaRoleAttribute() const {
  ax::mojom::blink::Role role = RawAriaRole();

  if ((role == ax::mojom::blink::Role::kForm ||
       role == ax::mojom::blink::Role::kRegion) &&
      !IsNameFromAuthorAttribute() &&
      !HasAttribute(html_names::kAriaRoledescriptionAttr)) {
    // Nameless ARIA form and region fall back on the native element's role.
    // We only check aria-label/aria-labelledby because those are the only
    // allowed ways to name an ARIA role.
    // TODO(accessibility) The aria-roledescription logic is required, otherwise
    // ChromeVox will ignore the aria-roledescription. It only speaks the role
    // description on certain roles, and ignores it on the generic role.
    // See also https://github.com/w3c/aria/issues/1463.
    return ax::mojom::blink::Role::kUnknown;
  }

  // ARIA states if an item can get focus, it should not be presentational.
  // It also states user agents should ignore the presentational role if
  // the element has global ARIA states and properties.
  if (ui::IsPresentational(role)) {
    if (IsFrame(GetNode()))
      return ax::mojom::blink::Role::kIframePresentational;
    if ((GetElement() && GetElement()->SupportsFocus()) ||
        HasAriaAttribute(true /* does_undo_role_presentation */)) {
      // Must be exposed with a role if focusable or has a global ARIA property
      // that is allowed in this context. See
      // https://w3c.github.io/aria/#presentation for more information about the
      // conditions upon which elements with role="none"/"presentation" must be
      // included in the tree. Return Role::kUnknown, so that the native HTML
      // role is used instead.
      return ax::mojom::blink::Role::kUnknown;
    }
  }

  if (role == ax::mojom::blink::Role::kButton)
    role = ButtonRoleType();

  // Distinguish between different uses of the "combobox" role:
  //
  // ax::mojom::blink::Role::kComboBoxGrouping:
  //   <div role="combobox"><input></div>
  // ax::mojom::blink::Role::kTextFieldWithComboBox:
  //   <input role="combobox">
  // ax::mojom::blink::Role::kComboBoxMenuButton:
  //   <div tabindex=0 role="combobox">Select</div>
  if (role == ax::mojom::blink::Role::kComboBoxGrouping) {
    if (IsAtomicTextField())
      role = ax::mojom::blink::Role::kTextFieldWithComboBox;
    else if (GetElement() && GetElement()->SupportsFocus())
      role = ax::mojom::blink::Role::kComboBoxMenuButton;
  }

  return role;
}

ax::mojom::blink::HasPopup AXObject::HasPopup() const {
  return ax::mojom::blink::HasPopup::kFalse;
}

ax::mojom::blink::IsPopup AXObject::IsPopup() const {
  return ax::mojom::blink::IsPopup::kNone;
}

bool AXObject::IsEditable() const {
  const Node* node = GetNode();
  if (IsDetached() || !node)
    return false;
#if DCHECK_IS_ON()  // Required in order to get Lifecycle().ToString()
  DCHECK(GetDocument());
  DCHECK_GE(GetDocument()->Lifecycle().GetState(),
            DocumentLifecycle::kStyleClean)
      << "Unclean document style at lifecycle state "
      << GetDocument()->Lifecycle().ToString();
#endif  // DCHECK_IS_ON()

  if (blink::IsEditable(*node))
    return true;

  // For the purposes of accessibility, atomic text fields  i.e. input and
  // textarea are editable because the user can potentially enter text in them.
  if (IsAtomicTextField())
    return true;

  return false;
}

bool AXObject::IsEditableRoot() const {
  return false;
}

bool AXObject::HasContentEditableAttributeSet() const {
  return false;
}

bool AXObject::IsMultiline() const {
  if (IsDetached() || !GetNode() || !IsTextField())
    return false;

  // While the specs don't specify that we can't do <input aria-multiline=true>,
  // it is in direct contradiction to the `HTMLInputElement` which is always
  // single line. Ensure that we can't make an input report that it's multiline
  // by returning early.
  if (IsA<HTMLInputElement>(*GetNode()))
    return false;

  bool is_multiline = false;
  if (HasAOMPropertyOrARIAAttribute(AOMBooleanProperty::kMultiline,
                                    is_multiline)) {
    return is_multiline;
  }

  return IsA<HTMLTextAreaElement>(*GetNode()) ||
         HasContentEditableAttributeSet();
}

bool AXObject::IsRichlyEditable() const {
  const Node* node = GetNode();
  if (IsDetached() || !node)
    return false;

  return node->IsRichlyEditableForAccessibility();
}

AXObject* AXObject::LiveRegionRoot() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_live_region_root_;
}

bool AXObject::LiveRegionAtomic() const {
  bool atomic = false;
  if (HasAOMPropertyOrARIAAttribute(AOMBooleanProperty::kAtomic, atomic))
    return atomic;

  // ARIA roles "alert" and "status" should have an implicit aria-atomic value
  // of true.
  return RoleValue() == ax::mojom::blink::Role::kAlert ||
         RoleValue() == ax::mojom::blink::Role::kStatus;
}

const AtomicString& AXObject::ContainerLiveRegionStatus() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_live_region_root_ ? cached_live_region_root_->LiveRegionStatus()
                                  : g_null_atom;
}

const AtomicString& AXObject::ContainerLiveRegionRelevant() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_live_region_root_
             ? cached_live_region_root_->LiveRegionRelevant()
             : g_null_atom;
}

bool AXObject::ContainerLiveRegionAtomic() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_live_region_root_ &&
         cached_live_region_root_->LiveRegionAtomic();
}

bool AXObject::ContainerLiveRegionBusy() const {
  UpdateCachedAttributeValuesIfNeeded();
  return cached_live_region_root_ &&
         cached_live_region_root_->AOMPropertyOrARIAAttributeIsTrue(
             AOMBooleanProperty::kBusy);
}

AXObject* AXObject::ElementAccessibilityHitTest(const gfx::Point& point) const {
  // Check if there are any mock elements that need to be handled.
  PhysicalOffset physical_point(point);
  for (const auto& child : ChildrenIncludingIgnored()) {
    if (child->IsMockObject() &&
        child->GetBoundsInFrameCoordinates().Contains(physical_point)) {
      return child->ElementAccessibilityHitTest(point);
    }
  }

  return const_cast<AXObject*>(this);
}

AXObject::AncestorsIterator AXObject::UnignoredAncestorsBegin() const {
  AXObject* parent = ParentObjectUnignored();
  if (parent)
    return AXObject::AncestorsIterator(*parent);
  return UnignoredAncestorsEnd();
}

AXObject::AncestorsIterator AXObject::UnignoredAncestorsEnd() const {
  return AXObject::AncestorsIterator();
}

int AXObject::ChildCountIncludingIgnored() const {
  return static_cast<int>(ChildrenIncludingIgnored().size());
}

AXObject* AXObject::ChildAtIncludingIgnored(int index) const {
  DCHECK_GE(index, 0);
  DCHECK_LE(index, ChildCountIncludingIgnored());
  if (index >= ChildCountIncludingIgnored())
    return nullptr;
  return ChildrenIncludingIgnored()[index];
}

const AXObject::AXObjectVector& AXObject::ChildrenIncludingIgnored() const {
  DCHECK(!IsDetached());
  return const_cast<AXObject*>(this)->ChildrenIncludingIgnored();
}

const AXObject::AXObjectVector& AXObject::ChildrenIncludingIgnored() {
  UpdateChildrenIfNecessary();
  return children_;
}

const AXObject::AXObjectVector AXObject::UnignoredChildren() const {
  return const_cast<AXObject*>(this)->UnignoredChildren();
}

const AXObject::AXObjectVector AXObject::UnignoredChildren() {
  UpdateChildrenIfNecessary();

  if (!AccessibilityIsIncludedInTree()) {
    NOTREACHED() << "We don't support finding the unignored children of "
                    "objects excluded from the accessibility tree: "
                 << ToString(true, true);
    return {};
  }

  // Capture only descendants that are not accessibility ignored, and that are
  // one level deeper than the current object after flattening any accessibility
  // ignored descendants.
  //
  // For example :
  // ++A
  // ++++B
  // ++++C IGNORED
  // ++++++F
  // ++++D
  // ++++++G
  // ++++E IGNORED
  // ++++++H IGNORED
  // ++++++++J
  // ++++++I
  //
  // Objects [B, F, D, I, J] will be returned, since after flattening all
  // ignored objects ,those are the ones that are one level deep.

  AXObjectVector unignored_children;
  AXObject* child = FirstChildIncludingIgnored();
  while (child && child != this) {
    if (child->AccessibilityIsIgnored()) {
      child = child->NextInPreOrderIncludingIgnored(this);
      continue;
    }

    unignored_children.push_back(child);
    for (; child != this; child = child->ParentObjectIncludedInTree()) {
      if (AXObject* sibling = child->NextSiblingIncludingIgnored()) {
        child = sibling;
        break;
      }
    }
  }

  return unignored_children;
}

AXObject* AXObject::FirstChildIncludingIgnored() const {
  return ChildCountIncludingIgnored() ? *ChildrenIncludingIgnored().begin()
                                      : nullptr;
}

AXObject* AXObject::LastChildIncludingIgnored() const {
  DCHECK(!IsDetached());
  return ChildCountIncludingIgnored() ? *(ChildrenIncludingIgnored().end() - 1)
                                      : nullptr;
}

AXObject* AXObject::DeepestFirstChildIncludingIgnored() const {
  if (IsDetached()) {
    NOTREACHED();
    return nullptr;
  }
  if (!ChildCountIncludingIgnored())
    return nullptr;

  AXObject* deepest_child = FirstChildIncludingIgnored();
  while (deepest_child->ChildCountIncludingIgnored())
    deepest_child = deepest_child->FirstChildIncludingIgnored();

  return deepest_child;
}

AXObject* AXObject::DeepestLastChildIncludingIgnored() const {
  if (IsDetached()) {
    NOTREACHED();
    return nullptr;
  }
  if (!ChildCountIncludingIgnored())
    return nullptr;

  AXObject* deepest_child = LastChildIncludingIgnored();
  while (deepest_child->ChildCountIncludingIgnored())
    deepest_child = deepest_child->LastChildIncludingIgnored();

  return deepest_child;
}

AXObject* AXObject::NextSiblingIncludingIgnored() const {
  if (!AccessibilityIsIncludedInTree()) {
    NOTREACHED() << "We don't support iterating children of objects excluded "
                    "from the accessibility tree: "
                 << ToString(true, true);
    return nullptr;
  }

  const AXObject* parent_in_tree = ParentObjectIncludedInTree();
  if (!parent_in_tree)
    return nullptr;

  const int index_in_parent = IndexInParent();
  if (index_in_parent < parent_in_tree->ChildCountIncludingIgnored() - 1)
    return parent_in_tree->ChildAtIncludingIgnored(index_in_parent + 1);
  return nullptr;
}

AXObject* AXObject::PreviousSiblingIncludingIgnored() const {
  if (!AccessibilityIsIncludedInTree()) {
    NOTREACHED() << "We don't support iterating children of objects excluded "
                    "from the accessibility tree: "
                 << ToString(true, true);
    return nullptr;
  }

  const AXObject* parent_in_tree = ParentObjectIncludedInTree();
  if (!parent_in_tree)
    return nullptr;

  const int index_in_parent = IndexInParent();
  if (index_in_parent > 0)
    return parent_in_tree->ChildAtIncludingIgnored(index_in_parent - 1);
  return nullptr;
}

AXObject* AXObject::NextInPreOrderIncludingIgnored(
    const AXObject* within) const {
  if (!AccessibilityIsIncludedInTree()) {
    // TODO(crbug.com/1421052): Make sure this no longer fires then turn the
    // above into CHECK(AccessibilityIsIncludedInTree());
    DUMP_WILL_BE_NOTREACHED_NORETURN()
        << "We don't support iterating children of objects excluded "
           "from the accessibility tree: "
        << ToString(true, true);
    return nullptr;
  }

  if (ChildCountIncludingIgnored())
    return FirstChildIncludingIgnored();

  if (within == this)
    return nullptr;

  const AXObject* current = this;
  AXObject* next = current->NextSiblingIncludingIgnored();
  for (; !next; next = current->NextSiblingIncludingIgnored()) {
    current = current->ParentObjectIncludedInTree();
    if (!current || within == current)
      return nullptr;
  }
  return next;
}

AXObject* AXObject::PreviousInPreOrderIncludingIgnored(
    const AXObject* within) const {
  if (!AccessibilityIsIncludedInTree()) {
    NOTREACHED() << "We don't support iterating children of objects excluded "
                    "from the accessibility tree: "
                 << ToString(true, true);
    return nullptr;
  }
  if (within == this)
    return nullptr;

  if (AXObject* sibling = PreviousSiblingIncludingIgnored()) {
    if (sibling->ChildCountIncludingIgnored())
      return sibling->DeepestLastChildIncludingIgnored();
    return sibling;
  }

  return ParentObjectIncludedInTree();
}

AXObject* AXObject::PreviousInPostOrderIncludingIgnored(
    const AXObject* within) const {
  if (!AccessibilityIsIncludedInTree()) {
    NOTREACHED() << "We don't support iterating children of objects excluded "
                    "from the accessibility tree: "
                 << ToString(true, true);
    return nullptr;
  }

  if (ChildCountIncludingIgnored())
    return LastChildIncludingIgnored();

  if (within == this)
    return nullptr;

  const AXObject* current = this;
  AXObject* previous = current->PreviousSiblingIncludingIgnored();
  for (; !previous; previous = current->PreviousSiblingIncludingIgnored()) {
    current = current->ParentObjectIncludedInTree();
    if (!current || within == current)
      return nullptr;
  }
  return previous;
}

int AXObject::UnignoredChildCount() const {
  return static_cast<int>(UnignoredChildren().size());
}

AXObject* AXObject::UnignoredChildAt(int index) const {
  const AXObjectVector unignored_children = UnignoredChildren();
  if (index < 0 || index >= static_cast<int>(unignored_children.size()))
    return nullptr;
  return unignored_children[index];
}

AXObject* AXObject::UnignoredNextSibling() const {
  if (AccessibilityIsIgnored()) {
    // TODO(crbug.com/1407397): Make sure this no longer fires then turn this
    // block into CHECK(!AccessibilityIsIgnored());
    DUMP_WILL_BE_NOTREACHED_NORETURN()
        << "We don't support finding unignored siblings for ignored "
           "objects because it is not clear whether to search for the "
           "sibling in the unignored tree or in the whole tree: "
        << ToString(true, true);
    return nullptr;
  }

  // Find the next sibling for the same unignored parent object,
  // flattening accessibility ignored objects.
  //
  // For example :
  // ++A
  // ++++B
  // ++++C IGNORED
  // ++++++E
  // ++++D
  // Objects [B, E, D] will be siblings since C is ignored.

  const AXObject* unignored_parent = ParentObjectUnignored();
  const AXObject* current_obj = this;
  while (current_obj) {
    AXObject* sibling = current_obj->NextSiblingIncludingIgnored();
    if (sibling) {
      // If we found an ignored sibling, walk in next pre-order
      // until an unignored object is found, flattening the ignored object.
      while (sibling && sibling->AccessibilityIsIgnored()) {
        sibling = sibling->NextInPreOrderIncludingIgnored(unignored_parent);
      }
      return sibling;
    }

    // If a sibling has not been found, try again with the parent object,
    // until the unignored parent is reached.
    current_obj = current_obj->ParentObjectIncludedInTree();
    if (!current_obj || !current_obj->AccessibilityIsIgnored())
      return nullptr;
  }
  return nullptr;
}

AXObject* AXObject::UnignoredPreviousSibling() const {
  if (AccessibilityIsIgnored()) {
    NOTREACHED() << "We don't support finding unignored siblings for ignored "
                    "objects because it is not clear whether to search for the "
                    "sibling in the unignored tree or in the whole tree: "
                 << ToString(true, true);
    return nullptr;
  }

  // Find the previous sibling for the same unignored parent object,
  // flattening accessibility ignored objects.
  //
  // For example :
  // ++A
  // ++++B
  // ++++C IGNORED
  // ++++++E
  // ++++D
  // Objects [B, E, D] will be siblings since C is ignored.

  const AXObject* current_obj = this;
  while (current_obj) {
    AXObject* sibling = current_obj->PreviousSiblingIncludingIgnored();
    if (sibling) {
      const AXObject* unignored_parent = ParentObjectUnignored();
      // If we found an ignored sibling, walk in previous post-order
      // until an unignored object is found, flattening the ignored object.
      while (sibling && sibling->AccessibilityIsIgnored()) {
        sibling =
            sibling->PreviousInPostOrderIncludingIgnored(unignored_parent);
      }
      return sibling;
    }

    // If a sibling has not been found, try again with the parent object,
    // until the unignored parent is reached.
    current_obj = current_obj->ParentObjectIncludedInTree();
    if (!current_obj || !current_obj->AccessibilityIsIgnored())
      return nullptr;
  }
  return nullptr;
}

AXObject* AXObject::UnignoredNextInPreOrder() const {
  AXObject* next = NextInPreOrderIncludingIgnored();
  while (next && next->AccessibilityIsIgnored()) {
    next = next->NextInPreOrderIncludingIgnored();
  }
  return next;
}

AXObject* AXObject::UnignoredPreviousInPreOrder() const {
  AXObject* previous = PreviousInPreOrderIncludingIgnored();
  while (previous && previous->AccessibilityIsIgnored()) {
    previous = previous->PreviousInPreOrderIncludingIgnored();
  }
  return previous;
}

AXObject* AXObject::ParentObject() const {
  if (IsDetached())
    return nullptr;

  // This can happen when an object in the middle of the tree is suddenly
  // detached, but the children still exist. One example of this is when
  // a <select size="1"> changes to <select size="2">, where the
  // Role::kMenuListPopup is detached.
  if (IsMissingParent())
    RepairMissingParent();

  return parent_;
}

AXObject* AXObject::ParentObjectUnignored() const {
  AXObject* parent;
  for (parent = ParentObject(); parent && parent->AccessibilityIsIgnored();
       parent = parent->ParentObject()) {
  }

  return parent;
}

AXObject* AXObject::ParentObjectIncludedInTree() const {
  AXObject* parent;
  for (parent = ParentObject();
       parent && !parent->AccessibilityIsIncludedInTree();
       parent = parent->ParentObject()) {
  }

  return parent;
}

Element* AXObject::GetClosestElement() const {
  Element* element = GetElement();
  // Certain AXObjects, such as those created from layout tree traversal,
  // have null values for `AXObject::GetNode()` and `AXObject::GetElement()`.
  // Just look for the closest parent that can handle this request.
  if (!element) {
    for (AXObject* parent = ParentObject(); parent;
         parent = parent->ParentObject()) {
      // It's possible to have a parent without a node here if the parent is a
      // pseudo element descendant. Since we're looking for the nearest element,
      // keep going up the ancestor chain until we find a parent that has one.
      element = parent->GetElement();
      if (element) {
        return element;
      }
    }
  }

  return element;
}

// Container widgets are those that a user tabs into and arrows around
// sub-widgets
bool AXObject::IsContainerWidget() const {
  return ui::IsContainerWithSelectableChildren(RoleValue());
}

AXObject* AXObject::ContainerWidget() const {
  AXObject* ancestor = ParentObjectUnignored();
  while (ancestor && !ancestor->IsContainerWidget())
    ancestor = ancestor->ParentObjectUnignored();

  return ancestor;
}

AXObject* AXObject::ContainerListMarkerIncludingIgnored() const {
  AXObject* ancestor = ParentObject();
  while (ancestor && (!ancestor->GetLayoutObject() ||
                      !ancestor->GetLayoutObject()->IsListMarker())) {
    ancestor = ancestor->ParentObject();
  }

  return ancestor;
}

// Determine which traversal approach is used to get children of an object.
bool AXObject::ShouldUseLayoutObjectTraversalForChildren() const {
  // There are two types of traversal used to find AXObjects:
  // 1. LayoutTreeBuilderTraversal, which takes FlatTreeTraversal and adds
  // pseudo elements on top of that. This is the usual case. However, while this
  // can add pseudo elements it cannot add important content descendants such as
  // text and images. For this, LayoutObject traversal (#2) is required.
  // 2. LayoutObject traversal, which just uses the children of a LayoutObject.

  // Therefore, if the object is a pseudo element or pseudo element descendant,
  // use LayoutObject traversal (#2) to find the children.
  if (GetNode() && GetNode()->IsPseudoElement())
    return true;

  // If no node, this is an anonymous layout object. The only way this can be
  // reached is inside a pseudo element subtree.
  if (!GetNode() && GetLayoutObject()) {
    DCHECK(GetLayoutObject()->IsAnonymous());
    DCHECK(AXObjectCacheImpl::IsRelevantPseudoElementDescendant(
        *GetLayoutObject()));
    return true;
  }

  return false;
}

void AXObject::UpdateChildrenIfNecessary() {
#if DCHECK_IS_ON()
  DCHECK(GetDocument()) << ToString(true, true);
  DCHECK(GetDocument()->IsActive());
  DCHECK(!GetDocument()->IsDetached());
  DCHECK(GetDocument()->GetPage());
  DCHECK(GetDocument()->View());
  DCHECK(!AXObjectCache().HasBeenDisposed());
#endif

  if (!NeedsToUpdateChildren() || !CanHaveChildren()) {
    children_dirty_ = false;
    return;
  }

  DCHECK(!AXObjectCache().IsFrozen())
      << "Object should have already had its children updated in "
         "AXObjectCacheImpl::UpdateTreeIfNeeded(): "
      << ToString(true, true);

#if DCHECK_IS_ON()
  // Ensure there are no unexpected, preexisting children, before we add more.
  if (IsMenuList()) {
    // AXMenuList is special and keeps its popup child, even when cleared.
    DCHECK_LE(children_.size(), 1U);
  } else {
    // Ensure children have been correctly cleared.
    DCHECK_EQ(children_.size(), 0U)
        << "\nChildren should have been cleared in SetNeedsToUpdateChildren(): "
        << GetNode() << "  with " << children_.size() << " children";
  }
#endif

  UpdateCachedAttributeValuesIfNeeded();

  AddChildren();
}

bool AXObject::NeedsToUpdateChildren() const {
  return children_dirty_;
}

void AXObject::SetNeedsToUpdateChildren() const {
  DCHECK(!IsDetached()) << "Cannot update children on a detached node: "
                        << ToString(true, true);
  DCHECK(!AXObjectCache().IsFrozen());
  DCHECK(!AXObjectCache().HasBeenDisposed());
  if (children_dirty_) {
    return;
  }
  DCHECK(!AXObjectCache().UpdatingTree());
  children_dirty_ = true;
  ClearChildren();
  SetAncestorsHaveDirtyDescendants();
}

// static
bool AXObject::CanSafelyUseFlatTreeTraversalNow(Document& document) {
  return !document.IsFlatTreeTraversalForbidden() &&
         !document.GetSlotAssignmentEngine().HasPendingSlotAssignmentRecalc();
}

bool AXObject::ShouldDestroyWhenDetachingFromParent() const {
  // Do not interfere with the destruction loop in AXObjectCacheImpl::Dispose().
  if (IsDetached() || AXObjectCache().HasBeenDisposed()) {
    return false;
  }
  // Nodeless objects's children do not have a node, and a node is required for
  // parent repair. Return true so that the nodeless child is not
  // orphaned/leaked. Menulist popups are an exception as they are a single
  // child managed by the parent AXMenuList.
  if (!GetNode() && !IsMenuListPopup()) {
    return true;
  }
  // Inline textbox children are dependent on their parent's ignored state.
  if (IsAXInlineTextBox()) {
    return true;
  }

  if (CachedParentObject() &&
      CachedParentObject()->RoleValue() == ax::mojom::blink::Role::kImage) {
    return true;
  }

  return false;
}

void AXObject::DetachFromParent() {
  if (ShouldDestroyWhenDetachingFromParent()) {
    AXObjectCache().RemoveIncludedSubtree(this, /* remove_root */ true);
  }
  parent_ = nullptr;
}

void AXObject::ClearChildren() const {
  DCHECK(!IsDetached());
  DCHECK(!AXObjectCache().IsFrozen());

  // Detach all weak pointers from immediate children to their parents.
  // First check to make sure the child's parent wasn't already reassigned.
  // In addition, the immediate children are different from children_, and are
  // the objects where the parent_ points to this. For example:
  // Parent (this)
  //   Child not included in tree  (immediate child)
  //     Child included in tree (an item in |children_|)
  // These situations only occur for children that were backed by a DOM node.
  // Therefore, in addition to looping through |children_|, we must also loop
  // through any unincluded children associated with any DOM children;
  // TODO(accessibility) Try to remove ugly second loop when we transition to
  // AccessibilityExposeIgnoredNodes().

  // Loop through AXObject children.

#if defined(AX_FAIL_FAST_BUILD)
  SANITIZER_CHECK(!is_adding_children_)
      << "Should not attempt to simultaneously add and clear children on: "
      << ToString(true, true);
  SANITIZER_CHECK(!is_computing_text_from_descendants_)
      << "Should not attempt to simultaneously compute text from descendants "
         "and clear children on: "
      << ToString(true, true);
#endif

  // Detach included children from their parent (this).
  for (const auto& child : children_) {
    // Check parent first, as the child might be several levels down if there
    // are unincluded nodes in between, in which case the cached parent will
    // also be a descendant (unlike children_, parent_ does not skip levels).
    // Another case where the parent is not the same is when the child has been
    // reparented using aria-owns.
    if (child->CachedParentObject() == this)
      child->DetachFromParent();
  }

  children_.clear();

  Node* node = GetNode();
  if (!node)
    return;

  if (!CanSafelyUseFlatTreeTraversalNow(*GetDocument())) {
    // Cannot use layout tree builder traversal now, will have to rely on
    // RepairParent() at a later point.
    return;
  }

  // <slot> content is always included in the tree, so there is no need to
  // iterate through the nodes. This also protects us against slot use "after
  // poison", where attempts to access assigned nodes triggers a DCHECK.

  // Detailed explanation:
  // <slot> elements are placeholders marking locations in a shadow tree where
  // users of a web component can insert their own custom nodes. Inserted nodes
  // (also known as distributed nodes) become children of their respective slots
  // in the accessibility tree. In other words, the accessibility tree mirrors
  // the flattened DOM tree or the layout tree, not the original DOM tree.
  // Distributed nodes still maintain their parent relations and computed style
  // information with their original location in the DOM. Therefore, we need to
  // ensure that in the accessibility tree no remnant information from the
  // unflattened DOM tree remains, such as the cached parent.

  // TODO(crbug.com/1209216): Figure out why removing this causes a
  // use-after-poison and possibly replace it with a better check.
  HTMLSlotElement* slot = ToHTMLSlotElementIfSupportsAssignmentOrNull(node);
  if (slot)
    return;

  // Detach unincluded children from their parent (this).
  // These are children that were not cleared from first loop, as well as
  // children that will be included once the parent next updates its children.
  for (Node* child_node = LayoutTreeBuilderTraversal::FirstChild(*node);
       child_node;
       child_node = LayoutTreeBuilderTraversal::NextSibling(*child_node)) {
    // Get the child object that should be detached from this parent.
    // Do not invalidate from layout, because it may be unsafe to check layout
    // at this time. However, do allow invalidations if an object changes its
    // display locking (content-visibility: auto) status, as this may be the
    // only chance to do that, and it's safe to do now.
    AXObject* ax_child_from_node = AXObjectCache().SafeGet(child_node, true);
    if (ax_child_from_node &&
        ax_child_from_node->CachedParentObject() == this) {
      ax_child_from_node->DetachFromParent();
    }
  }
}

void AXObject::ChildrenChangedWithCleanLayout() {
  DCHECK(!IsDetached()) << "Don't call on detached node: "
                        << ToString(true, true);

  // When children changed on a <map> that means we need to forward the
  // children changed to the <img> that parents the <area> elements.
  // TODO(accessibility) Consider treating <img usemap> as aria-owns so that
  // we get implementation "for free" vai relation cache, etc.
  if (HTMLMapElement* map_element = DynamicTo<HTMLMapElement>(GetNode())) {
    HTMLImageElement* image_element = map_element->ImageElement();
    if (image_element) {
      AXObject* ax_image = AXObjectCache().Get(image_element);
      if (ax_image) {
        ax_image->ChildrenChangedWithCleanLayout();
        return;
      }
    }
  }

  // Always invalidate |children_| even if it was invalidated before, because
  // now layout is clean.
  SetNeedsToUpdateChildren();

  // Between the time that AXObjectCacheImpl::ChildrenChanged() determines
  // which included parent to use and now, it's possible that the parent will
  // no longer be ignored. This is rare, but is covered by this test:
  // external/wpt/accessibility/crashtests/delayed-ignored-change.html/
  // In this case, first ancestor that's still included in the tree will used.
  if (!LastKnownIsIncludedInTreeValue()) {
    if (AXObject* ax_parent = CachedParentObject()) {
      ax_parent->ChildrenChangedWithCleanLayout();
      return;
    }
  }

  // TODO(accessibility) Move this up.
  if (!CanHaveChildren()) {
    return;
  }

  DCHECK(!IsDetached()) << "None of the above should be able to detach |this|: "
                        << ToString(true, true);

  AXObjectCache().MarkAXObjectDirtyWithCleanLayout(this);

  // Special case: when the children of a layout inline are changed, it can
  // cause whitespace redundancy in the parent object to change as well.
  if (IsA<LayoutInline>(GetLayoutObject())) {
    if (AXObject* ax_parent = CachedParentObject()) {
      if (LayoutBlockFlow* layout_block_flow =
              DynamicTo<LayoutBlockFlow>(ax_parent->GetLayoutObject())) {
        ax_parent->ChildrenChangedWithCleanLayout();
      }
    }
  }
}

Node* AXObject::GetNode() const {
  return nullptr;
}

LayoutObject* AXObject::GetLayoutObject() const {
  return nullptr;
}

Element* AXObject::GetElement() const {
  return DynamicTo<Element>(GetNode());
}

AXObject* AXObject::RootScroller() const {
  Node* global_root_scroller = GetDocument()
                                   ->GetPage()
                                   ->GlobalRootScrollerController()
                                   .GlobalRootScroller();
  if (!global_root_scroller)
    return nullptr;

  // Only return the root scroller if it's part of the same document.
  if (global_root_scroller->GetDocument() != GetDocument())
    return nullptr;

  return AXObjectCache().GetOrCreate(global_root_scroller);
}

LocalFrameView* AXObject::DocumentFrameView() const {
  if (Document* document = GetDocument())
    return document->View();
  return nullptr;
}

AtomicString AXObject::Language() const {
  // This method is used when the style engine is either not available on this
  // object, e.g. for canvas fallback content, or is unable to determine the
  // document's language. We use the following signals to detect the element's
  // language, in decreasing priority:
  // 1. The [language of a node] as defined in HTML, if known.
  // 2. The list of languages the browser sends in the [Accept-Language] header.
  // 3. The browser's default language.

  const AtomicString& lang = GetAttribute(html_names::kLangAttr);
  if (!lang.empty())
    return lang;

  // Only fallback for the root node, propagating this value down the tree is
  // handled browser side within AXNode::GetLanguage.
  //
  // TODO(chrishall): Consider moving this to AXNodeObject or AXLayoutObject as
  // the web area node is currently an AXLayoutObject.
  if (IsWebArea()) {
    const Document* document = GetDocument();
    if (document) {
      // Fall back to the first content language specified in the meta tag.
      // This is not part of what the HTML5 Standard suggests but it still
      // appears to be necessary.
      if (document->ContentLanguage()) {
        const String content_languages = document->ContentLanguage();
        Vector<String> languages;
        content_languages.Split(',', languages);
        if (!languages.empty())
          return AtomicString(languages[0].StripWhiteSpace());
      }

      if (document->GetPage()) {
        // Use the first accept language preference if present.
        const String accept_languages =
            document->GetPage()->GetChromeClient().AcceptLanguages();
        Vector<String> languages;
        accept_languages.Split(',', languages);
        if (!languages.empty())
          return AtomicString(languages[0].StripWhiteSpace());
      }
    }

    // As a last resort, return the default language of the browser's UI.
    AtomicString default_language = DefaultLanguage();
    return default_language;
  }

  return g_null_atom;
}

//
// Scrollable containers.
//

bool AXObject::IsScrollableContainer() const {
  return !!GetScrollableAreaIfScrollable();
}

bool AXObject::IsUserScrollable() const {
  // TODO(accessibility) Actually expose correct info on whether a doc is
  // is scrollable or not. Unfortunately IsScrollableContainer() always returns
  // true anyway. For now, just expose as scrollable unless overflow is hidden.
  if (IsWebArea()) {
    if (!GetScrollableAreaIfScrollable() || !GetLayoutObject())
      return false;

    const ComputedStyle* style = GetLayoutObject()->Style();
    if (!style)
      return false;

    return style->ScrollsOverflowY() || style->ScrollsOverflowX();
  }

  return GetLayoutObject() && GetLayoutObject()->IsBox() &&
         To<LayoutBox>(GetLayoutObject())->IsUserScrollable();
}

gfx::Point AXObject::GetScrollOffset() const {
  ScrollableArea* area = GetScrollableAreaIfScrollable();
  if (!area)
    return gfx::Point();
  // TODO(crbug.com/1274078): Should this be converted to scroll position, or
  // should the result type be gfx::Vector2d?
  return gfx::PointAtOffsetFromOrigin(area->ScrollOffsetInt());
}

gfx::Point AXObject::MinimumScrollOffset() const {
  ScrollableArea* area = GetScrollableAreaIfScrollable();
  if (!area)
    return gfx::Point();
  // TODO(crbug.com/1274078): Should this be converted to scroll position, or
  // should the result type be gfx::Vector2d?
  return gfx::PointAtOffsetFromOrigin(area->MinimumScrollOffsetInt());
}

gfx::Point AXObject::MaximumScrollOffset() const {
  ScrollableArea* area = GetScrollableAreaIfScrollable();
  if (!area)
    return gfx::Point();
  // TODO(crbug.com/1274078): Should this be converted to scroll position, or
  // should the result type be gfx::Vector2d?
  return gfx::PointAtOffsetFromOrigin(area->MaximumScrollOffsetInt());
}

void AXObject::SetScrollOffset(const gfx::Point& offset) const {
  ScrollableArea* area = GetScrollableAreaIfScrollable();
  if (!area)
    return;

  // TODO(bokan): This should potentially be a UserScroll.
  area->SetScrollOffset(ScrollOffset(offset.OffsetFromOrigin()),
                        mojom::blink::ScrollType::kProgrammatic);
}

void AXObject::Scroll(ax::mojom::blink::Action scroll_action) const {
  AXObject* offset_container = nullptr;
  gfx::RectF bounds;
  gfx::Transform container_transform;
  GetRelativeBounds(&offset_container, bounds, container_transform);
  if (bounds.IsEmpty())
    return;

  gfx::Point initial = GetScrollOffset();
  gfx::Point min = MinimumScrollOffset();
  gfx::Point max = MaximumScrollOffset();

  // TODO(anastasi): This 4/5ths came from the Android implementation, revisit
  // to find the appropriate modifier to keep enough context onscreen after
  // scrolling.
  int page_x = std::max(base::ClampRound<int>(bounds.width() * 4 / 5), 1);
  int page_y = std::max(base::ClampRound<int>(bounds.height() * 4 / 5), 1);

  // Forward/backward defaults to down/up unless it can only be scrolled
  // horizontally.
  if (scroll_action == ax::mojom::blink::Action::kScrollForward) {
    scroll_action = max.y() > min.y() ? ax::mojom::blink::Action::kScrollDown
                                      : ax::mojom::blink::Action::kScrollRight;
  } else if (scroll_action == ax::mojom::blink::Action::kScrollBackward) {
    scroll_action = max.y() > min.y() ? ax::mojom::blink::Action::kScrollUp
                                      : ax::mojom::blink::Action::kScrollLeft;
  }

  int x = initial.x();
  int y = initial.y();
  switch (scroll_action) {
    case ax::mojom::blink::Action::kScrollUp:
      if (initial.y() == min.y())
        return;
      y = std::max(initial.y() - page_y, min.y());
      break;
    case ax::mojom::blink::Action::kScrollDown:
      if (initial.y() == max.y())
        return;
      y = std::min(initial.y() + page_y, max.y());
      break;
    case ax::mojom::blink::Action::kScrollLeft:
      if (initial.x() == min.x())
        return;
      x = std::max(initial.x() - page_x, min.x());
      break;
    case ax::mojom::blink::Action::kScrollRight:
      if (initial.x() == max.x())
        return;
      x = std::min(initial.x() + page_x, max.x());
      break;
    default:
      NOTREACHED();
  }

  SetScrollOffset(gfx::Point(x, y));

  if (!RuntimeEnabledFeatures::
          SynthesizedKeyboardEventsForAccessibilityActionsEnabled())
    return;

  // There are no keys that produce scroll left/right, so we shouldn't
  // synthesize any keyboard events for these actions.
  if (scroll_action == ax::mojom::blink::Action::kScrollLeft ||
      scroll_action == ax::mojom::blink::Action::kScrollRight)
    return;

  LocalDOMWindow* local_dom_window = GetDocument()->domWindow();
  DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kRawKeyDown,
                        scroll_action);
  DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kKeyUp,
                        scroll_action);
}

bool AXObject::IsTableLikeRole() const {
  return ui::IsTableLike(RoleValue()) ||
         RoleValue() == ax::mojom::blink::Role::kLayoutTable;
}

bool AXObject::IsTableRowLikeRole() const {
  return ui::IsTableRow(RoleValue()) ||
         RoleValue() == ax::mojom::blink::Role::kLayoutTableRow;
}

bool AXObject::IsTableCellLikeRole() const {
  return ui::IsCellOrTableHeader(RoleValue()) ||
         RoleValue() == ax::mojom::blink::Role::kLayoutTableCell;
}

unsigned AXObject::ColumnCount() const {
  if (!IsTableLikeRole())
    return 0;

  unsigned max_column_count = 0;
  for (const auto& row : TableRowChildren()) {
    unsigned column_count = row->TableCellChildren().size();
    max_column_count = std::max(column_count, max_column_count);
  }

  return max_column_count;
}

unsigned AXObject::RowCount() const {
  if (!IsTableLikeRole())
    return 0;

  return TableRowChildren().size();
}

void AXObject::ColumnHeaders(AXObjectVector& headers) const {
  if (!IsTableLikeRole())
    return;

  for (const auto& row : TableRowChildren()) {
    for (const auto& cell : row->TableCellChildren()) {
      if (cell->RoleValue() == ax::mojom::blink::Role::kColumnHeader)
        headers.push_back(cell);
    }
  }
}

void AXObject::RowHeaders(AXObjectVector& headers) const {
  if (!IsTableLikeRole())
    return;

  for (const auto& row : TableRowChildren()) {
    for (const auto& cell : row->TableCellChildren()) {
      if (cell->RoleValue() == ax::mojom::blink::Role::kRowHeader)
        headers.push_back(cell);
    }
  }
}

AXObject* AXObject::CellForColumnAndRow(unsigned target_column_index,
                                        unsigned target_row_index) const {
  if (!IsTableLikeRole())
    return nullptr;

  // Note that this code is only triggered if this is not a LayoutNGTable,
  // i.e. it's an ARIA grid/table.
  //
  // TODO(dmazzoni): delete this code or rename it "for testing only"
  // since it's only needed for Blink web tests and not for production.
  unsigned row_index = 0;
  for (const auto& row : TableRowChildren()) {
    unsigned column_index = 0;
    for (const auto& cell : row->TableCellChildren()) {
      if (target_column_index == column_index && target_row_index == row_index)
        return cell;
      column_index++;
    }
    row_index++;
  }

  return nullptr;
}

int AXObject::AriaColumnCount() const {
  if (!IsTableLikeRole())
    return 0;

  int32_t col_count;
  if (!HasAOMPropertyOrARIAAttribute(AOMIntProperty::kColCount, col_count))
    return 0;

  if (col_count > static_cast<int>(ColumnCount()))
    return col_count;

  // Spec says that if all of the columns are present in the DOM, it
  // is not necessary to set this attribute as the user agent can
  // automatically calculate the total number of columns.
  // It returns 0 in order not to set this attribute.
  if (col_count == static_cast<int>(ColumnCount()) || col_count != -1)
    return 0;

  return -1;
}

int AXObject::AriaRowCount() const {
  if (!IsTableLikeRole())
    return 0;

  int32_t row_count;
  if (!HasAOMPropertyOrARIAAttribute(AOMIntProperty::kRowCount, row_count))
    return 0;

  if (row_count > static_cast<int>(RowCount()))
    return row_count;

  // Spec says that if all of the rows are present in the DOM, it is
  // not necessary to set this attribute as the user agent can
  // automatically calculate the total number of rows.
  // It returns 0 in order not to set this attribute.
  if (row_count == static_cast<int>(RowCount()) || row_count != -1)
    return 0;

  // In the spec, -1 explicitly means an unknown number of rows.
  return -1;
}

unsigned AXObject::ColumnIndex() const {
  return 0;
}

unsigned AXObject::RowIndex() const {
  return 0;
}

unsigned AXObject::ColumnSpan() const {
  return IsTableCellLikeRole() ? 1 : 0;
}

unsigned AXObject::RowSpan() const {
  return IsTableCellLikeRole() ? 1 : 0;
}

unsigned AXObject::AriaColumnIndex() const {
  // Return the ARIA column index if it has been set. Otherwise return a default
  // value of 0.
  uint32_t col_index = 0;
  HasAOMPropertyOrARIAAttribute(AOMUIntProperty::kColIndex, col_index);
  return col_index;
}

unsigned AXObject::AriaRowIndex() const {
  // Return the ARIA row index if it has been set. Otherwise return a default
  // value of 0.
  uint32_t row_index = 0;
  HasAOMPropertyOrARIAAttribute(AOMUIntProperty::kRowIndex, row_index);
  return row_index;
}

AXObject::AXObjectVector AXObject::TableRowChildren() const {
  AXObjectVector result;
  for (const auto& child : ChildrenIncludingIgnored()) {
    if (child->IsTableRowLikeRole())
      result.push_back(child);
    else if (child->RoleValue() == ax::mojom::blink::Role::kRowGroup)
      result.AppendVector(child->TableRowChildren());
  }
  return result;
}

AXObject::AXObjectVector AXObject::TableCellChildren() const {
  AXObjectVector result;
  for (const auto& child : ChildrenIncludingIgnored()) {
    if (child->IsTableCellLikeRole())
      result.push_back(child);
    else if (child->RoleValue() == ax::mojom::blink::Role::kGenericContainer)
      result.AppendVector(child->TableCellChildren());
  }
  return result;
}

const AXObject* AXObject::TableRowParent() const {
  const AXObject* row = ParentObjectUnignored();
  while (row && !row->IsTableRowLikeRole() &&
         row->RoleValue() == ax::mojom::blink::Role::kGenericContainer)
    row = row->ParentObjectUnignored();
  return row;
}

const AXObject* AXObject::TableParent() const {
  const AXObject* table = ParentObjectUnignored();
  while (table && !table->IsTableLikeRole() &&
         table->RoleValue() == ax::mojom::blink::Role::kGenericContainer)
    table = table->ParentObjectUnignored();
  return table;
}

int AXObject::GetDOMNodeId() const {
  Node* node = GetNode();
  if (node)
    return DOMNodeIds::IdForNode(node);
  return 0;
}

void AXObject::GetRelativeBounds(AXObject** out_container,
                                 gfx::RectF& out_bounds_in_container,
                                 gfx::Transform& out_container_transform,
                                 bool* clips_children) const {
  *out_container = nullptr;
  out_bounds_in_container = gfx::RectF();
  out_container_transform.MakeIdentity();

  // First check if it has explicit bounds, for example if this element is tied
  // to a canvas path. When explicit coordinates are provided, the ID of the
  // explicit container element that the coordinates are relative to must be
  // provided too.
  if (!explicit_element_rect_.IsEmpty()) {
    *out_container = AXObjectCache().ObjectFromAXID(explicit_container_id_);
    if (*out_container) {
      out_bounds_in_container = gfx::RectF(explicit_element_rect_);
      return;
    }
  }

  LayoutObject* layout_object = GetLayoutObject();
  if (!layout_object)
    return;

  if (layout_object->IsFixedPositioned() ||
      layout_object->IsStickyPositioned()) {
    AXObjectCache().AddToFixedOrStickyNodeList(this);
  }

  if (clips_children) {
    if (IsA<Document>(GetNode())) {
      *clips_children = true;
    } else {
      *clips_children = layout_object->HasNonVisibleOverflow();
    }
  }

  if (IsA<Document>(GetNode())) {
    if (LocalFrameView* view = layout_object->GetFrame()->View()) {
      out_bounds_in_container.set_size(gfx::SizeF(view->Size()));

      // If it's a popup, account for the popup window's offset.
      if (view->GetPage()->GetChromeClient().IsPopup()) {
        gfx::Rect frame_rect = view->FrameToScreen(view->FrameRect());
        LocalFrameView* root_view =
            AXObjectCache().GetDocument().GetFrame()->View();
        gfx::Rect root_frame_rect =
            root_view->FrameToScreen(root_view->FrameRect());

        // Screen coordinates are in DIP without device scale factor applied.
        // Accessibility expects device scale factor applied here which is
        // unapplied at the destination AXTree.
        float scale_factor =
            view->GetPage()->GetChromeClient().WindowToViewportScalar(
                layout_object->GetFrame(), 1.0f);
        out_bounds_in_container.set_origin(
            gfx::PointF(scale_factor * (frame_rect.x() - root_frame_rect.x()),
                        scale_factor * (frame_rect.y() - root_frame_rect.y())));
      }
    }
    return;
  }

  // First compute the container. The container must be an ancestor in the
  // accessibility tree, and its LayoutObject must be an ancestor in the layout
  // tree. Get the first such ancestor that's either scrollable or has a paint
  // layer.
  AXObject* container = ParentObjectUnignored();
  LayoutObject* container_layout_object = nullptr;
  if (layout_object->IsFixedPositioned()) {
    // If it's a fixed position element, the container should simply be the
    // root web area.
    container = AXObjectCache().GetOrCreate(GetDocument());
  } else {
    while (container) {
      container_layout_object = container->GetLayoutObject();
      if (container_layout_object && container_layout_object->IsBox() &&
          layout_object->IsDescendantOf(container_layout_object)) {
        if (container->IsScrollableContainer() ||
            container_layout_object->HasLayer()) {
          if (layout_object->IsAbsolutePositioned()) {
            // If it's absolutely positioned, the container must be the
            // nearest positioned container, or the root.
            if (IsA<LayoutView>(layout_object)) {
              break;
            }
            if (container_layout_object->IsPositioned())
              break;
          } else {
            break;
          }
        }
      }

      container = container->ParentObjectUnignored();
    }
  }

  if (!container)
    return;
  *out_container = container;
  out_bounds_in_container =
      layout_object->LocalBoundingBoxRectForAccessibility();

  // Frames need to take their border and padding into account so the
  // child element's computed position will be correct.
  if (layout_object->IsBox() && layout_object->GetNode() &&
      layout_object->GetNode()->IsFrameOwnerElement()) {
    out_bounds_in_container =
        gfx::RectF(To<LayoutBox>(layout_object)->PhysicalContentBoxRect());
  }

  // If the container has a scroll offset, subtract that out because we want our
  // bounds to be relative to the *unscrolled* position of the container object.
  if (auto* scrollable_area = container->GetScrollableAreaIfScrollable())
    out_bounds_in_container.Offset(scrollable_area->GetScrollOffset());

  // Compute the transform between the container's coordinate space and this
  // object.
  gfx::Transform transform = layout_object->LocalToAncestorTransform(
      To<LayoutBoxModelObject>(container_layout_object));

  // If the transform is just a simple translation, apply that to the
  // bounding box, but if it's a non-trivial transformation like a rotation,
  // scaling, etc. then return the full matrix instead.
  if (transform.IsIdentityOr2dTranslation()) {
    out_bounds_in_container.Offset(transform.To2dTranslation());
  } else {
    out_container_transform = transform;
  }
}

gfx::RectF AXObject::LocalBoundingBoxRectForAccessibility() {
  if (!GetLayoutObject())
    return gfx::RectF();
  DCHECK(GetLayoutObject()->IsText());
  UpdateCachedAttributeValuesIfNeeded();
  return cached_local_bounding_box_rect_for_accessibility_;
}

PhysicalRect AXObject::GetBoundsInFrameCoordinates() const {
  AXObject* container = nullptr;
  gfx::RectF bounds;
  gfx::Transform transform;
  GetRelativeBounds(&container, bounds, transform);
  gfx::RectF computed_bounds(0, 0, bounds.width(), bounds.height());
  while (container && container != this) {
    computed_bounds.Offset(bounds.x(), bounds.y());
    if (!container->IsWebArea()) {
      computed_bounds.Offset(-container->GetScrollOffset().x(),
                             -container->GetScrollOffset().y());
    }
    computed_bounds = transform.MapRect(computed_bounds);
    container->GetRelativeBounds(&container, bounds, transform);
  }
  return PhysicalRect::FastAndLossyFromRectF(computed_bounds);
}

//
// Modify or take an action on an object.
//

bool AXObject::PerformAction(const ui::AXActionData& action_data) {
  Document* document = GetDocument();
  if (!document) {
    return false;
  }
  AXObjectCacheImpl& cache = AXObjectCache();
  Node* node = GetNode();
  if (!node) {
    node = GetClosestElement();
    if (!node) {
      return false;
    }
  }

  // In most cases, UpdateAllLifecyclePhasesExceptPaint() is enough, but if
  // the action is part of a display locked node, that will not update the node
  // because it's not part of the layout update cycle yet. In that case, calling
  // UpdateStyleAndLayoutTreeForNode() is also necessary.
  document->UpdateStyleAndLayoutTreeForNode(
      node, DocumentUpdateReason::kAccessibility);
  cache.UpdateAXForAllDocuments();

  // Updating style and layout for the node can cause it to gain layout,
  // detaching an AXNodeObject to make room for an AXLayoutObject.
  if (IsDetached()) {
    AXObject* new_object = cache.GetOrCreate(node);
    return new_object ? new_object->PerformAction(action_data) : false;
  }

  switch (action_data.action) {
    case ax::mojom::blink::Action::kBlur:
      return OnNativeBlurAction();
    case ax::mojom::blink::Action::kClearAccessibilityFocus:
      return InternalClearAccessibilityFocusAction();
    case ax::mojom::blink::Action::kCollapse:
      return RequestCollapseAction();
    case ax::mojom::blink::Action::kDecrement:
      return RequestDecrementAction();
    case ax::mojom::blink::Action::kDoDefault:
      return RequestClickAction();
    case ax::mojom::blink::Action::kExpand:
      return RequestExpandAction();
    case ax::mojom::blink::Action::kFocus:
      return RequestFocusAction();
    case ax::mojom::blink::Action::kIncrement:
      return RequestIncrementAction();
    case ax::mojom::blink::Action::kScrollToPoint:
      return RequestScrollToGlobalPointAction(action_data.target_point);
    case ax::mojom::blink::Action::kSetAccessibilityFocus:
      return InternalSetAccessibilityFocusAction();
    case ax::mojom::blink::Action::kSetScrollOffset:
      SetScrollOffset(action_data.target_point);
      return true;
    case ax::mojom::blink::Action::kSetSequentialFocusNavigationStartingPoint:
      return RequestSetSequentialFocusNavigationStartingPointAction();
    case ax::mojom::blink::Action::kSetValue:
      return RequestSetValueAction(
          WTF::String::FromUTF8(action_data.value.c_str()));
    case ax::mojom::blink::Action::kShowContextMenu:
      return RequestShowContextMenuAction();
    case ax::mojom::blink::Action::kScrollToMakeVisible:
      return RequestScrollToMakeVisibleAction();
    case ax::mojom::blink::Action::kScrollBackward:
    case ax::mojom::blink::Action::kScrollDown:
    case ax::mojom::blink::Action::kScrollForward:
    case ax::mojom::blink::Action::kScrollLeft:
    case ax::mojom::blink::Action::kScrollRight:
    case ax::mojom::blink::Action::kScrollUp:
      Scroll(action_data.action);
      return true;
    case ax::mojom::blink::Action::kAnnotatePageImages:
    case ax::mojom::blink::Action::kCustomAction:
    case ax::mojom::blink::Action::kGetImageData:
    case ax::mojom::blink::Action::kGetTextLocation:
    case ax::mojom::blink::Action::kHideTooltip:
    case ax::mojom::blink::Action::kHitTest:
    case ax::mojom::blink::Action::kInternalInvalidateTree:
    case ax::mojom::blink::Action::kLoadInlineTextBoxes:
    case ax::mojom::blink::Action::kNone:
    case ax::mojom::blink::Action::kReplaceSelectedText:
    case ax::mojom::blink::Action::kSetSelection:
    case ax::mojom::blink::Action::kShowTooltip:
    case ax::mojom::blink::Action::kSignalEndOfTest:
    case ax::mojom::blink::Action::kResumeMedia:
    case ax::mojom::blink::Action::kStartDuckingMedia:
    case ax::mojom::blink::Action::kStopDuckingMedia:
    case ax::mojom::blink::Action::kSuspendMedia:
    case ax::mojom::blink::Action::kLongClick:
    case ax::mojom::blink::Action::kScrollToPositionAtRowColumn:
      return false;
  }
}

bool AXObject::RequestDecrementAction() {
  Event* event =
      Event::CreateCancelable(event_type_names::kAccessibledecrement);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeDecrementAction();
}

bool AXObject::RequestClickAction() {
  Event* event = Event::CreateCancelable(event_type_names::kAccessibleclick);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeClickAction();
}

bool AXObject::OnNativeClickAction() {
  Document* document = GetDocument();
  if (!document)
    return false;

  LocalFrame::NotifyUserActivation(
      document->GetFrame(),
      mojom::blink::UserActivationNotificationType::kInteraction);

  if (IsTextField())
    return OnNativeFocusAction();

  Element* element = GetClosestElement();

  if (element) {
    // Always set the sequential focus navigation starting point.
    // Even if this element isn't focusable, if you press "Tab" it will
    // start the search from this element.
    GetDocument()->SetSequentialFocusNavigationStartingPoint(element);

    // Explicitly focus the element if it's focusable but not currently
    // the focused element, to be consistent with
    // EventHandler::HandleMousePressEvent.
    if (element->IsFocusable() && !element->IsFocusedElementInDocument()) {
      Page* const page = GetDocument()->GetPage();
      if (page) {
        page->GetFocusController().SetFocusedElement(
            element, GetDocument()->GetFrame(),
            FocusParams(SelectionBehaviorOnFocus::kNone,
                        mojom::blink::FocusType::kMouse, nullptr));
      }
    }

    // For most elements, AccessKeyAction triggers sending a simulated
    // click, including simulating the mousedown, mouseup, and click events.
    element->AccessKeyAction(SimulatedClickCreationScope::kFromAccessibility);
    return true;
  }

  if (CanSetFocusAttribute())
    return OnNativeFocusAction();

  return false;
}

bool AXObject::RequestFocusAction() {
  Event* event = Event::CreateCancelable(event_type_names::kAccessiblefocus);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeFocusAction();
}

bool AXObject::RequestIncrementAction() {
  Event* event =
      Event::CreateCancelable(event_type_names::kAccessibleincrement);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeIncrementAction();
}

bool AXObject::RequestScrollToGlobalPointAction(const gfx::Point& point) {
  return OnNativeScrollToGlobalPointAction(point);
}

bool AXObject::RequestScrollToMakeVisibleAction() {
  Event* event =
      Event::CreateCancelable(event_type_names::kAccessiblescrollintoview);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeScrollToMakeVisibleAction();
}

bool AXObject::RequestScrollToMakeVisibleWithSubFocusAction(
    const gfx::Rect& subfocus,
    blink::mojom::blink::ScrollAlignment horizontal_scroll_alignment,
    blink::mojom::blink::ScrollAlignment vertical_scroll_alignment) {
  Document* document = GetDocument();
  if (!document) {
    return false;
  }
  AXObjectCacheImpl& cache = AXObjectCache();
  Node* node = GetNode();
  if (!node) {
    node = GetClosestElement();
    if (!node) {
      return false;
    }
  }

  // In most cases, UpdateAllLifecyclePhasesExceptPaint() is enough, but if
  // focus is is moving to a display locked node, that will not update the node
  // because it's not part of the layout update cycle yet. In that case, calling
  // UpdateStyleAndLayoutTreeForNode() is also necessary.
  document->UpdateStyleAndLayoutTreeForNode(
      node, DocumentUpdateReason::kAccessibility);
  document->View()->UpdateAllLifecyclePhasesExceptPaint(
      DocumentUpdateReason::kAccessibility);

  // Updating style and layout for the node can cause it to gain layout,
  // detaching an AXNodeObject to make room for an AXLayoutObject.
  if (IsDetached()) {
    AXObject* new_object = cache.GetOrCreate(node);
    return new_object
               ? new_object->OnNativeScrollToMakeVisibleWithSubFocusAction(
                     subfocus, horizontal_scroll_alignment,
                     vertical_scroll_alignment)
               : false;
  }

  return OnNativeScrollToMakeVisibleWithSubFocusAction(
      subfocus, horizontal_scroll_alignment, vertical_scroll_alignment);
}

bool AXObject::RequestSetSelectedAction(bool selected) {
  return OnNativeSetSelectedAction(selected);
}

bool AXObject::RequestSetSequentialFocusNavigationStartingPointAction() {
  return OnNativeSetSequentialFocusNavigationStartingPointAction();
}

bool AXObject::RequestSetValueAction(const String& value) {
  return OnNativeSetValueAction(value);
}

bool AXObject::RequestShowContextMenuAction() {
  Event* event =
      Event::CreateCancelable(event_type_names::kAccessiblecontextmenu);
  if (DispatchEventToAOMEventListeners(*event))
    return true;

  return OnNativeShowContextMenuAction();
}

bool AXObject::RequestExpandAction() {
  if (ui::SupportsArrowKeysForExpandCollapse(RoleValue())) {
    return OnNativeKeyboardAction(ax::mojom::blink::Action::kExpand);
  }
  return RequestClickAction();
}

bool AXObject::RequestCollapseAction() {
  if (ui::SupportsArrowKeysForExpandCollapse(RoleValue())) {
    return OnNativeKeyboardAction(ax::mojom::blink::Action::kCollapse);
  }
  return RequestClickAction();
}

bool AXObject::OnNativeKeyboardAction(const ax::mojom::Action action) {
  LocalDOMWindow* local_dom_window = GetDocument()->domWindow();

  DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kRawKeyDown,
                        action);
  DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kKeyUp, action);

  return true;
}

bool AXObject::InternalSetAccessibilityFocusAction() {
  return false;
}

bool AXObject::InternalClearAccessibilityFocusAction() {
  return false;
}

LayoutObject* AXObject::GetLayoutObjectForNativeScrollAction() const {
  Node* node = GetNode();
  if (!node || !node->isConnected()) {
    return nullptr;
  }

  // Node might not have a LayoutObject due to the fact that it is in a locked
  // subtree. Force the update to create the LayoutObject (and update position
  // information) for this node.
  GetDocument()->UpdateStyleAndLayoutForNode(
      node, DocumentUpdateReason::kDisplayLock);
  return node->GetLayoutObject();
}

void AXObject::DispatchKeyboardEvent(LocalDOMWindow* local_dom_window,
                                     WebInputEvent::Type type,
                                     ax::mojom::blink::Action action) const {
  blink::WebKeyboardEvent key(type,
                              blink::WebInputEvent::Modifiers::kNoModifiers,
                              base::TimeTicks::Now());
  switch (action) {
    case ax::mojom::blink::Action::kExpand:
      DCHECK(ui::SupportsArrowKeysForExpandCollapse(RoleValue()));
      key.dom_key = ui::DomKey::ARROW_RIGHT;
      key.dom_code = static_cast<int>(ui::DomCode::ARROW_RIGHT);
      key.native_key_code = key.windows_key_code = blink::VKEY_RIGHT;
      break;
    case ax::mojom::blink::Action::kCollapse:
      DCHECK(ui::SupportsArrowKeysForExpandCollapse(RoleValue()));
      key.dom_key = ui::DomKey::ARROW_LEFT;
      key.dom_code = static_cast<int>(ui::DomCode::ARROW_LEFT);
      key.native_key_code = key.windows_key_code = blink::VKEY_LEFT;
      break;
    case ax::mojom::blink::Action::kShowContextMenu:
      key.dom_key = ui::DomKey::CONTEXT_MENU;
      key.dom_code = static_cast<int>(ui::DomCode::CONTEXT_MENU);
      key.native_key_code = key.windows_key_code = blink::VKEY_APPS;
      break;
    case ax::mojom::blink::Action::kScrollUp:
      key.dom_key = ui::DomKey::PAGE_UP;
      key.dom_code = static_cast<int>(ui::DomCode::PAGE_UP);
      key.native_key_code = key.windows_key_code = blink::VKEY_PRIOR;
      break;
    case ax::mojom::blink::Action::kScrollDown:
      key.dom_key = ui::DomKey::PAGE_DOWN;
      key.dom_code = static_cast<int>(ui::DomCode::PAGE_DOWN);
      key.native_key_code = key.windows_key_code = blink::VKEY_NEXT;
      break;
    default:
      NOTREACHED();
  }
  GetNode()->DispatchEvent(
      *blink::KeyboardEvent::Create(key, local_dom_window, true));
}

bool AXObject::OnNativeScrollToMakeVisibleAction() const {
  LayoutObject* layout_object = GetLayoutObjectForNativeScrollAction();
  if (!layout_object)
    return false;
  PhysicalRect target_rect(layout_object->AbsoluteBoundingBoxRect());
  scroll_into_view_util::ScrollRectToVisible(
      *layout_object, target_rect,
      ScrollAlignment::CreateScrollIntoViewParams(
          ScrollAlignment::CenterIfNeeded(), ScrollAlignment::CenterIfNeeded(),
          mojom::blink::ScrollType::kProgrammatic, false,
          mojom::blink::ScrollBehavior::kAuto));
  AXObjectCache().PostNotification(
      AXObjectCache().GetOrCreate(GetDocument()->GetLayoutView()),
      ax::mojom::blink::Event::kLocationChanged);
  return true;
}

bool AXObject::OnNativeScrollToMakeVisibleWithSubFocusAction(
    const gfx::Rect& rect,
    blink::mojom::blink::ScrollAlignment horizontal_scroll_alignment,
    blink::mojom::blink::ScrollAlignment vertical_scroll_alignment) const {
  LayoutObject* layout_object = GetLayoutObjectForNativeScrollAction();
  if (!layout_object)
    return false;

  PhysicalRect target_rect =
      layout_object->LocalToAbsoluteRect(PhysicalRect(rect));
  scroll_into_view_util::ScrollRectToVisible(
      *layout_object, target_rect,
      ScrollAlignment::CreateScrollIntoViewParams(
          horizontal_scroll_alignment, vertical_scroll_alignment,
          mojom::blink::ScrollType::kProgrammatic,
          false /* make_visible_in_visual_viewport */,
          mojom::blink::ScrollBehavior::kAuto));
  AXObjectCache().PostNotification(
      AXObjectCache().GetOrCreate(GetDocument()->GetLayoutView()),
      ax::mojom::blink::Event::kLocationChanged);
  return true;
}

bool AXObject::OnNativeScrollToGlobalPointAction(
    const gfx::Point& global_point) const {
  LayoutObject* layout_object = GetLayoutObjectForNativeScrollAction();
  if (!layout_object)
    return false;

  PhysicalRect target_rect(layout_object->AbsoluteBoundingBoxRect());
  target_rect.Move(-PhysicalOffset(global_point));
  scroll_into_view_util::ScrollRectToVisible(
      *layout_object, target_rect,
      ScrollAlignment::CreateScrollIntoViewParams(
          ScrollAlignment::LeftAlways(), ScrollAlignment::TopAlways(),
          mojom::blink::ScrollType::kProgrammatic, false,
          mojom::blink::ScrollBehavior::kAuto));
  AXObjectCache().PostNotification(
      AXObjectCache().GetOrCreate(GetDocument()->GetLayoutView()),
      ax::mojom::blink::Event::kLocationChanged);
  return true;
}

bool AXObject::OnNativeSetSequentialFocusNavigationStartingPointAction() {
  // Call it on the nearest ancestor that overrides this with a specific
  // implementation.
  if (ParentObject()) {
    return ParentObject()
        ->OnNativeSetSequentialFocusNavigationStartingPointAction();
  }
  return false;
}

bool AXObject::OnNativeDecrementAction() {
  return false;
}

bool AXObject::OnNativeBlurAction() {
  return false;
}

bool AXObject::OnNativeFocusAction() {
  return false;
}

bool AXObject::OnNativeIncrementAction() {
  return false;
}

bool AXObject::OnNativeSetValueAction(const String&) {
  return false;
}

bool AXObject::OnNativeSetSelectedAction(bool) {
  return false;
}

bool AXObject::OnNativeShowContextMenuAction() {
  Element* element = GetElement();
  if (!element)
    element = ParentObject() ? ParentObject()->GetElement() : nullptr;
  if (!element)
    return false;

  Document* document = GetDocument();
  if (!document || !document->GetFrame())
    return false;

  LocalDOMWindow* local_dom_window = GetDocument()->domWindow();
  if (RuntimeEnabledFeatures::
          SynthesizedKeyboardEventsForAccessibilityActionsEnabled()) {
    // To make less evident that the events are synthesized, we have to emit
    // them in this order: 1) keydown. 2) contextmenu. 3) keyup.
    DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kRawKeyDown,
                          ax::mojom::blink::Action::kShowContextMenu);
  }

  ContextMenuAllowedScope scope;
  WebInputEventResult result =
      document->GetFrame()->GetEventHandler().ShowNonLocatedContextMenu(
          element, kMenuSourceKeyboard);

  // The node may have ceased to exist due to the event handler actions, so we
  // check its detached state. We also check the result of the contextMenu
  // event: if it was consumed by the system, executing the default action, we
  // don't synthesize the keyup event because it would not be produced normally;
  // the system context menu captures it and never reaches the DOM.
  if (!IsDetached() && result != WebInputEventResult::kHandledSystem &&
      RuntimeEnabledFeatures::
          SynthesizedKeyboardEventsForAccessibilityActionsEnabled()) {
    DispatchKeyboardEvent(local_dom_window, WebInputEvent::Type::kKeyUp,
                          ax::mojom::blink::Action::kShowContextMenu);
  }

  return true;
}

// static
bool AXObject::IsARIAControl(ax::mojom::blink::Role aria_role) {
  return IsARIAInput(aria_role) ||
         aria_role == ax::mojom::blink::Role::kButton ||
         aria_role == ax::mojom::blink::Role::kComboBoxMenuButton ||
         aria_role == ax::mojom::blink::Role::kSlider;
}

// static
bool AXObject::IsARIAInput(ax::mojom::blink::Role aria_role) {
  return aria_role == ax::mojom::blink::Role::kRadioButton ||
         aria_role == ax::mojom::blink::Role::kCheckBox ||
         aria_role == ax::mojom::blink::Role::kTextField ||
         aria_role == ax::mojom::blink::Role::kSwitch ||
         aria_role == ax::mojom::blink::Role::kSearchBox ||
         aria_role == ax::mojom::blink::Role::kTextFieldWithComboBox;
}

// static
bool AXObject::IsFrame(const Node* node) {
  auto* frame_owner = DynamicTo<HTMLFrameOwnerElement>(node);
  if (!frame_owner)
    return false;
  switch (frame_owner->OwnerType()) {
    case FrameOwnerElementType::kIframe:
    case FrameOwnerElementType::kFrame:
    case FrameOwnerElementType::kFencedframe:
      return true;
    case FrameOwnerElementType::kObject:
    case FrameOwnerElementType::kEmbed:
    case FrameOwnerElementType::kPortal:
    case FrameOwnerElementType::kNone:
      return false;
  }
}

// static
bool AXObject::HasARIAOwns(Element* element) {
  if (!element)
    return false;

  // A LayoutObject is not required, because an invisible object can still
  // use aria-owns to point to visible children.

  const AtomicString& aria_owns =
      element->FastGetAttribute(html_names::kAriaOwnsAttr);

  // TODO(accessibility): do we need to check !AriaOwnsElements.empty() ? Is
  // that fundamentally different from HasExplicitlySetAttrAssociatedElements()?
  // And is an element even necessary in the case of virtual nodes?
  return !aria_owns.empty() || element->HasExplicitlySetAttrAssociatedElements(
                                   html_names::kAriaOwnsAttr);
}

// static
ax::mojom::blink::Role AXObject::AriaRoleStringToRoleEnum(const String& value) {
  DCHECK(!value.empty());

  static const ARIARoleMap* role_map = CreateARIARoleMap();

  Vector<String> role_vector;
  value.SimplifyWhiteSpace().Split(' ', role_vector);
  ax::mojom::blink::Role role = ax::mojom::blink::Role::kUnknown;
  for (const auto& child : role_vector) {
    auto it = role_map->find(child);
    if (it != role_map->end())
      return it->value;
  }

  return role;
}

bool AXObject::SupportsNameFromContents(bool recursive) const {
  // ARIA 1.1, section 5.2.7.5.
  bool result = false;

  switch (RoleValue()) {
    // ----- NameFrom: contents -------------------------
    // Get their own name from contents, or contribute to ancestors
    case ax::mojom::blink::Role::kButton:
    case ax::mojom::blink::Role::kCell:
    case ax::mojom::blink::Role::kCheckBox:
    case ax::mojom::blink::Role::kColumnHeader:
    case ax::mojom::blink::Role::kComboBoxSelect:
    case ax::mojom::blink::Role::kDocBackLink:
    case ax::mojom::blink::Role::kDocBiblioRef:
    case ax::mojom::blink::Role::kDocNoteRef:
    case ax::mojom::blink::Role::kDocGlossRef:
    case ax::mojom::blink::Role::kDisclosureTriangle:
    case ax::mojom::blink::Role::kHeading:
    case ax::mojom::blink::Role::kLayoutTableCell:
    case ax::mojom::blink::Role::kLineBreak:
    case ax::mojom::blink::Role::kLink:
    case ax::mojom::blink::Role::kListBoxOption:
    case ax::mojom::blink::Role::kMath:
    case ax::mojom::blink::Role::kMenuItem:
    case ax::mojom::blink::Role::kMenuItemCheckBox:
    case ax::mojom::blink::Role::kMenuItemRadio:
    case ax::mojom::blink::Role::kMenuListOption:
    case ax::mojom::blink::Role::kPopUpButton:
    case ax::mojom::blink::Role::kPortal:
    case ax::mojom::blink::Role::kRadioButton:
    case ax::mojom::blink::Role::kRowHeader:
    case ax::mojom::blink::Role::kStaticText:
    case ax::mojom::blink::Role::kSwitch:
    case ax::mojom::blink::Role::kTab:
    case ax::mojom::blink::Role::kToggleButton:
    case ax::mojom::blink::Role::kTreeItem:
    case ax::mojom::blink::Role::kTooltip:
      result = true;
      break;

    // ----- No name from contents -------------------------
    // These never have or contribute a name from contents, as they are
    // containers for many subobjects. Superset of nameFrom:author ARIA roles.
    case ax::mojom::blink::Role::kAlert:
    case ax::mojom::blink::Role::kAlertDialog:
    case ax::mojom::blink::Role::kApplication:
    case ax::mojom::blink::Role::kAudio:
    case ax::mojom::blink::Role::kArticle:
    case ax::mojom::blink::Role::kBanner:
    case ax::mojom::blink::Role::kBlockquote:
    case ax::mojom::blink::Role::kColorWell:
    case ax::mojom::blink::Role::kComboBoxMenuButton:  // Only value from
                                                       // content.
    case ax::mojom::blink::Role::kComboBoxGrouping:
    case ax::mojom::blink::Role::kComment:
    case ax::mojom::blink::Role::kComplementary:
    case ax::mojom::blink::Role::kContentInfo:
    case ax::mojom::blink::Role::kDate:
    case ax::mojom::blink::Role::kDateTime:
    case ax::mojom::blink::Role::kDialog:
    case ax::mojom::blink::Role::kDirectory:
    case ax::mojom::blink::Role::kDocCover:
    case ax::mojom::blink::Role::kDocBiblioEntry:
    case ax::mojom::blink::Role::kDocEndnote:
    case ax::mojom::blink::Role::kDocFootnote:
    case ax::mojom::blink::Role::kDocPageBreak:
    case ax::mojom::blink::Role::kDocPageFooter:
    case ax::mojom::blink::Role::kDocPageHeader:
    case ax::mojom::blink::Role::kDocAbstract:
    case ax::mojom::blink::Role::kDocAcknowledgments:
    case ax::mojom::blink::Role::kDocAfterword:
    case ax::mojom::blink::Role::kDocAppendix:
    case ax::mojom::blink::Role::kDocBibliography:
    case ax::mojom::blink::Role::kDocChapter:
    case ax::mojom::blink::Role::kDocColophon:
    case ax::mojom::blink::Role::kDocConclusion:
    case ax::mojom::blink::Role::kDocCredit:
    case ax::mojom::blink::Role::kDocCredits:
    case ax::mojom::blink::Role::kDocDedication:
    case ax::mojom::blink::Role::kDocEndnotes:
    case ax::mojom::blink::Role::kDocEpigraph:
    case ax::mojom::blink::Role::kDocEpilogue:
    case ax::mojom::blink::Role::kDocErrata:
    case ax::mojom::blink::Role::kDocExample:
    case ax::mojom::blink::Role::kDocForeword:
    case ax::mojom::blink::Role::kDocGlossary:
    case ax::mojom::blink::Role::kDocIndex:
    case ax::mojom::blink::Role::kDocIntroduction:
    case ax::mojom::blink::Role::kDocNotice:
    case ax::mojom::blink::Role::kDocPageList:
    case ax::mojom::blink::Role::kDocPart:
    case ax::mojom::blink::Role::kDocPreface:
    case ax::mojom::blink::Role::kDocPrologue:
    case ax::mojom::blink::Role::kDocPullquote:
    case ax::mojom::blink::Role::kDocQna:
    case ax::mojom::blink::Role::kDocSubtitle:
    case ax::mojom::blink::Role::kDocTip:
    case ax::mojom::blink::Role::kDocToc:
    case ax::mojom::blink::Role::kDocument:
    case ax::mojom::blink::Role::kEmbeddedObject:
    case ax::mojom::blink::Role::kFeed:
    case ax::mojom::blink::Role::kFigure:
    case ax::mojom::blink::Role::kForm:
    case ax::mojom::blink::Role::kGraphicsDocument:
    case ax::mojom::blink::Role::kGraphicsObject:
    case ax::mojom::blink::Role::kGraphicsSymbol:
    case ax::mojom::blink::Role::kGrid:
    case ax::mojom::blink::Role::kGroup:
    case ax::mojom::blink::Role::kHeader:
    case ax::mojom::blink::Role::kIframePresentational:
    case ax::mojom::blink::Role::kIframe:
    case ax::mojom::blink::Role::kImage:
    case ax::mojom::blink::Role::kInputTime:
    case ax::mojom::blink::Role::kListBox:
    case ax::mojom::blink::Role::kLog:
    case ax::mojom::blink::Role::kMain:
    case ax::mojom::blink::Role::kMarquee:
    case ax::mojom::blink::Role::kMathMLFraction:
    case ax::mojom::blink::Role::kMathMLIdentifier:
    case ax::mojom::blink::Role::kMathMLMath:
    case ax::mojom::blink::Role::kMathMLMultiscripts:
    case ax::mojom::blink::Role::kMathMLNoneScript:
    case ax::mojom::blink::Role::kMathMLNumber:
    case ax::mojom::blink::Role::kMathMLOperator:
    case ax::mojom::blink::Role::kMathMLOver:
    case ax::mojom::blink::Role::kMathMLPrescriptDelimiter:
    case ax::mojom::blink::Role::kMathMLRoot:
    case ax::mojom::blink::Role::kMathMLRow:
    case ax::mojom::blink::Role::kMathMLSquareRoot:
    case ax::mojom::blink::Role::kMathMLStringLiteral:
    case ax::mojom::blink::Role::kMathMLSub:
    case ax::mojom::blink::Role::kMathMLSubSup:
    case ax::mojom::blink::Role::kMathMLSup:
    case ax::mojom::blink::Role::kMathMLTable:
    case ax::mojom::blink::Role::kMathMLTableCell:
    case ax::mojom::blink::Role::kMathMLTableRow:
    case ax::mojom::blink::Role::kMathMLText:
    case ax::mojom::blink::Role::kMathMLUnder:
    case ax::mojom::blink::Role::kMathMLUnderOver:
    case ax::mojom::blink::Role::kMenuListPopup:
    case ax::mojom::blink::Role::kMenu:
    case ax::mojom::blink::Role::kMenuBar:
    case ax::mojom::blink::Role::kMeter:
    case ax::mojom::blink::Role::kNavigation:
    case ax::mojom::blink::Role::kNote:
    case ax::mojom::blink::Role::kPluginObject:
    case ax::mojom::blink::Role::kProgressIndicator:
    case ax::mojom::blink::Role::kRadioGroup:
    case ax::mojom::blink::Role::kRowGroup:
    case ax::mojom::blink::Role::kScrollBar:
    case ax::mojom::blink::Role::kScrollView:
    case ax::mojom::blink::Role::kSearch:
    case ax::mojom::blink::Role::kSearchBox:
    case ax::mojom::blink::Role::kSplitter:
    case ax::mojom::blink::Role::kSlider:
    case ax::mojom::blink::Role::kSpinButton:
    case ax::mojom::blink::Role::kStatus:
    case ax::mojom::blink::Role::kSuggestion:
    case ax::mojom::blink::Role::kSvgRoot:
    case ax::mojom::blink::Role::kTable:
    case ax::mojom::blink::Role::kTabList:
    case ax::mojom::blink::Role::kTabPanel:
    case ax::mojom::blink::Role::kTerm:
    case ax::mojom::blink::Role::kTextField:
    case ax::mojom::blink::Role::kTextFieldWithComboBox:
    case ax::mojom::blink::Role::kTimer:
    case ax::mojom::blink::Role::kToolbar:
    case ax::mojom::blink::Role::kTree:
    case ax::mojom::blink::Role::kTreeGrid:
    case ax::mojom::blink::Role::kVideo:
      result = false;
      break;

    // ----- role="row" -------
    // Spec says we should always expose the name on rows,
    // but for performance reasons we only do it
    // if the row is focusable or is the active descendant of a grid/treegrid.
    case ax::mojom::blink::Role::kRow: {
      if (CanSetFocusAttribute())
        return true;
      AXObject* ancestor = ParentObjectUnignored();
      while (ancestor) {
        if (ancestor->GetAOMPropertyOrARIAAttribute(
                AOMRelationProperty::kActiveDescendant)) {
          return true;
        }
        if (ancestor->RoleValue() !=
                ax::mojom::blink::Role::kGenericContainer &&
            ancestor->RoleValue() != ax::mojom::blink::Role::kNone &&
            ancestor->RoleValue() != ax::mojom::blink::Role::kRowGroup) {
          // Not inside a grid or a treegrid, or reached the top of one.
          return false;
        }
        ancestor = ancestor->ParentObjectUnignored();
      }
      return false;
    }

    // ----- Conditional: contribute to ancestor only, unless focusable -------
    // Some objects can contribute their contents to ancestor names, but
    // only have their own name if they are focusable
    case ax::mojom::blink::Role::kGenericContainer:
      // The <body> and <html> element can pass information up to the the root
      // for a portal name.
      if (IsA<HTMLBodyElement>(GetNode()) ||
          GetNode() == GetDocument()->documentElement()) {
        return recursive && GetDocument()->GetPage() &&
               GetDocument()->GetPage()->InsidePortal();
      }
      [[fallthrough]];
    case ax::mojom::blink::Role::kAbbr:
    case ax::mojom::blink::Role::kCanvas:
    case ax::mojom::blink::Role::kCaption:
    case ax::mojom::blink::Role::kCode:
    case ax::mojom::blink::Role::kContentDeletion:
    case ax::mojom::blink::Role::kContentInsertion:
    case ax::mojom::blink::Role::kDefinition:
    case ax::mojom::blink::Role::kDescriptionListDetail:
    case ax::mojom::blink::Role::kDescriptionList:
    case ax::mojom::blink::Role::kDescriptionListTerm:
    case ax::mojom::blink::Role::kDetails:
    case ax::mojom::blink::Role::kEmphasis:
    case ax::mojom::blink::Role::kFigcaption:
    case ax::mojom::blink::Role::kFooter:
    case ax::mojom::blink::Role::kFooterAsNonLandmark:
    case ax::mojom::blink::Role::kHeaderAsNonLandmark:
    case ax::mojom::blink::Role::kInlineTextBox:
    case ax::mojom::blink::Role::kLabelText:
    case ax::mojom::blink::Role::kLayoutTable:
    case ax::mojom::blink::Role::kLayoutTableRow:
    case ax::mojom::blink::Role::kLegend:
    case ax::mojom::blink::Role::kList:
    case ax::mojom::blink::Role::kListItem:
    case ax::mojom::blink::Role::kListMarker:
    case ax::mojom::blink::Role::kMark:
    case ax::mojom::blink::Role::kNone:
    case ax::mojom::blink::Role::kParagraph:
    case ax::mojom::blink::Role::kPre:
    case ax::mojom::blink::Role::kRegion:
    case ax::mojom::blink::Role::kRuby:
    case ax::mojom::blink::Role::kSection:
    case ax::mojom::blink::Role::kStrong:
    case ax::mojom::blink::Role::kSubscript:
    case ax::mojom::blink::Role::kSuperscript:
    case ax::mojom::blink::Role::kTime:
      if (recursive) {
        // Use contents if part of a recursive name computation.
        result = true;
      } else {
        // Use contents if tabbable, so that there is a name in the case
        // where the author mistakenly forgot to provide one.
        // Exceptions:
        // 1.Elements with contenteditable, where using the contents as a name
        //   would cause them to be double-announced.
        // 2.Containers with aria-activedescendant, where the focus is being
        //   forwarded somewhere else.
        result = false;
        if (!IsEditable() && !GetAOMPropertyOrARIAAttribute(
                                 AOMRelationProperty::kActiveDescendant)) {
          // TODO(accessibility) Scrollables are currently allowed here in order
          // to keep the current behavior. In the future, this can be removed
          // because this code will be handled in IsFocusable(), once
          // KeyboardFocusableScrollersEnabled is permanently enabled.
          // Note: this uses the same scrollable check that element.cc uses.
          if (RuntimeEnabledFeatures::KeyboardFocusableScrollersEnabled() &&
              IsUserScrollable()) {
            return true;
          }
          if (!GetElement() || !GetDocument())
            return false;
          int tab_index = GetElement()->tabIndex();
          bool is_focused = GetElement() == GetDocument()->FocusedElement();
          bool is_in_tab_order_or_focused = tab_index >= 0 || is_focused;
          // Don't repair name from contents to focusable elements unless
          // tabbable or focused, because providing a repaired accessible name
          // often leads to redundant verbalizations.
          return is_in_tab_order_or_focused && CanSetFocusAttribute();
        }
      }
      break;

    case ax::mojom::blink::Role::kRubyAnnotation:
      // Ruby annotations are removed from accessible names and instead used
      // as a description of the parent Role::kRuby object. The benefit is that
      // announcement of the description can be toggled on/off per user choice.
      // In this way, ruby annotations are treated like other annotations, e.g.
      // <mark aria-description="annotation">base text</mark>.
      // In order to achieve the above:
      // * When recursive is true:
      //   Return false, so that the ruby annotation text does not contribute to
      //   the name of the parent Role::kRuby, since it will also be in the
      //   description of that object.
      // * When recursive is false:
      //   Return true, so that text is generated for the object. This text will
      //   be assigned as the description of he parent Role::kRuby object.
      return !recursive;

    // A root web area normally only computes its name from the document title,
    // but a root web area inside a portal's main frame should compute its name
    // from its contents. This name is used by the portal element that hosts
    // this portal.
    case ax::mojom::blink::Role::kRootWebArea: {
      DCHECK(GetNode());
      const Document& document = GetNode()->GetDocument();
      bool is_portal_main_frame =
          document.GetFrame() && document.GetFrame()->IsMainFrame() &&
          !document.GetFrame()->IsFencedFrameRoot() && document.GetPage() &&
          document.GetPage()->InsidePortal();
      return is_portal_main_frame;
    }

    case ax::mojom::blink::Role::kCaret:
    case ax::mojom::blink::Role::kClient:
    case ax::mojom::blink::Role::kColumn:
    case ax::mojom::blink::Role::kDesktop:
    case ax::mojom::blink::Role::kKeyboard:
    case ax::mojom::blink::Role::kImeCandidate:
    case ax::mojom::blink::Role::kListGrid:
    case ax::mojom::blink::Role::kPane:
    case ax::mojom::blink::Role::kPdfActionableHighlight:
    case ax::mojom::blink::Role::kPdfRoot:
    case ax::mojom::blink::Role::kTableHeaderContainer:
    case ax::mojom::blink::Role::kTitleBar:
    case ax::mojom::blink::Role::kUnknown:
    case ax::mojom::blink::Role::kWebView:
    case ax::mojom::blink::Role::kWindow:
      NOTREACHED() << "Role shouldn't occur in Blink: " << ToString(true, true);
      break;
  }

  return result;
}

bool AXObject::SupportsARIAReadOnly() const {
  // Ignore the readonly state if the element is set to contenteditable and
  // aria-readonly="true" according to the HTML-AAM specification.
  if (HasContentEditableAttributeSet()) {
    return false;
  }

  if (ui::IsReadOnlySupported(RoleValue()))
    return true;

  if (ui::IsCellOrTableHeader(RoleValue())) {
    // For cells and row/column headers, readonly is supported within a grid.
    AncestorsIterator ancestor = base::ranges::find_if(
        UnignoredAncestorsBegin(), UnignoredAncestorsEnd(),
        &AXObject::IsTableLikeRole);
    return ancestor.current_ &&
           (ancestor.current_->RoleValue() == ax::mojom::blink::Role::kGrid ||
            ancestor.current_->RoleValue() ==
                ax::mojom::blink::Role::kTreeGrid);
  }

  return false;
}

ax::mojom::blink::Role AXObject::ButtonRoleType() const {
  // If aria-pressed is present, then it should be exposed as a toggle button.
  // http://www.w3.org/TR/wai-aria/states_and_properties#aria-pressed
  if (AriaPressedIsPresent())
    return ax::mojom::blink::Role::kToggleButton;

  // If aria-haspopup is present and is not "dialog", expose as a popup button,
  // which is exposed in MSAA/IA2 with a role of button menu. Note that this is
  // not done for dialog because screen readers use the button menu role as a
  // tip to turn off the virtual buffer mode.
  // Here is the GitHub issue -- ARIA WG is working to update the spec to match.
  if (HasPopup() != ax::mojom::blink::HasPopup::kFalse &&
      HasPopup() != ax::mojom::blink::HasPopup::kDialog) {
    return ax::mojom::blink::Role::kPopUpButton;
  }

  return ax::mojom::blink::Role::kButton;
}

// static
const AtomicString& AXObject::ARIARoleName(ax::mojom::blink::Role role) {
  static const Vector<AtomicString>* aria_role_name_vector =
      CreateARIARoleNameVector();

  return aria_role_name_vector->at(static_cast<wtf_size_t>(role));
}

const String AXObject::InternalRoleName(ax::mojom::blink::Role role) {
  std::ostringstream role_name;
  role_name << role;
  // Convert from std::ostringstream to std::string, while removing "k" prefix.
  // For example, kStaticText becomes StaticText.
  // Many conversions, but this isn't used in performance-sensitive code.
  std::string role_name_std = role_name.str().substr(1, std::string::npos);
  String role_name_wtf_string = role_name_std.c_str();
  return role_name_wtf_string;
}

// static
const String AXObject::RoleName(ax::mojom::blink::Role role,
                                bool* is_internal) {
  if (is_internal)
    *is_internal = false;
  if (const auto& role_name = ARIARoleName(role))
    return role_name.GetString();

  if (is_internal)
    *is_internal = true;

  return InternalRoleName(role);
}

// static
const AXObject* AXObject::LowestCommonAncestor(const AXObject& first,
                                               const AXObject& second,
                                               int* index_in_ancestor1,
                                               int* index_in_ancestor2) {
  *index_in_ancestor1 = -1;
  *index_in_ancestor2 = -1;

  if (first.IsDetached() || second.IsDetached())
    return nullptr;

  if (first == second)
    return &first;

  HeapVector<Member<const AXObject>> ancestors1;
  ancestors1.push_back(&first);
  while (ancestors1.back())
    ancestors1.push_back(ancestors1.back()->ParentObjectIncludedInTree());

  HeapVector<Member<const AXObject>> ancestors2;
  ancestors2.push_back(&second);
  while (ancestors2.back())
    ancestors2.push_back(ancestors2.back()->ParentObjectIncludedInTree());

  const AXObject* common_ancestor = nullptr;
  while (!ancestors1.empty() && !ancestors2.empty() &&
         ancestors1.back() == ancestors2.back()) {
    common_ancestor = ancestors1.back();
    ancestors1.pop_back();
    ancestors2.pop_back();
  }

  if (common_ancestor) {
    if (!ancestors1.empty())
      *index_in_ancestor1 = ancestors1.back()->IndexInParent();
    if (!ancestors2.empty())
      *index_in_ancestor2 = ancestors2.back()->IndexInParent();
  }

  return common_ancestor;
}

void AXObject::PreSerializationConsistencyCheck() {
#if defined(AX_FAIL_FAST_BUILD)
  DCHECK(!IsDetached()) << "Do not serialize detached nodes: "
                        << ToString(true, true);
  DCHECK(AccessibilityIsIncludedInTree())
      << "Do not serialize unincluded nodes: " << ToString(true, true);
  SANITIZER_CHECK(!IsDetached());
  // Extra checks that only occur during serialization.
  SANITIZER_CHECK_EQ(IsAriaHidden(), !!FindAncestorWithAriaHidden(this))
      << "IsAriaHidden() doesn't match existence of an aria-hidden ancestor: "
      << ToString(true);
#endif
}

String AXObject::ToString(bool verbose, bool cached_values_only) const {
  // Build a friendly name for debugging the object.
  // If verbose, build a longer name name in the form of:
  // CheckBox axid#28 <input.someClass#cbox1> name="checkbox"
#if !defined(NDEBUG)
  if (IsDetached() && verbose) {
    return "(detached) " + detached_object_debug_info_;
  }
#endif

  String string_builder = InternalRoleName(RoleValue()).EncodeForDebugging();

  if (IsDetached()) {
    return string_builder + " (detached)";
  }

  if (AXObjectCache().HasBeenDisposed()) {
    return string_builder + " (doc shutdown) #" + String::Number(AXObjectID());
  }

  if (verbose) {
    string_builder = string_builder + " axid#" + String::Number(AXObjectID());
    // Add useful HTML element info, like <div.myClass#myId>.
    if (GetNode()) {
      string_builder = string_builder + " " + GetNodeString(GetNode());
      if (IsRoot()) {
        string_builder = string_builder + " isRoot";
      }
      if (GetDocument()) {
        if (GetDocument()->GetFrame() &&
            GetDocument()->GetFrame()->PagePopupOwner()) {
          string_builder = string_builder + " inPopup";
        }
      } else {
        string_builder = string_builder + " missingDocument";
      }

      if (!GetNode()->isConnected()) {
        // TODO(accessibility) Do we have a handy helper for determining whether
        // a node is still in the flat tree? That would be useful to log.
        string_builder = string_builder + " nodeDisconnected";
      }
    }

    if (cached_values_need_update_) {
      string_builder = string_builder + " needsToUpdateCachedValues";
    }

    // Add properties of interest that often contribute to errors:
    if (HasARIAOwns(GetElement())) {
      string_builder =
          string_builder + " aria-owns=" +
          GetElement()->FastGetAttribute(html_names::kAriaOwnsAttr);
    }

    if (GetAOMPropertyOrARIAAttribute(AOMRelationProperty::kActiveDescendant)) {
      string_builder =
          string_builder + " aria-activedescendant=" +
          GetElement()->FastGetAttribute(html_names::kAriaOwnsAttr);
    }
    if (IsFocused())
      string_builder = string_builder + " focused";
    if (!IsDetached() && AXObjectCache().IsAriaOwned(this))
      string_builder = string_builder + " isAriaOwned";
    if (cached_values_only ? LastKnownIsIgnoredValue()
                           : AccessibilityIsIgnored()) {
      string_builder = string_builder + " isIgnored";
#if defined(AX_FAIL_FAST_BUILD)
      // TODO(accessibility) Move this out of AX_FAIL_FAST_BUILD by having a new
      // ax_enum, and a ToString() in ax_enum_utils, as well as move out of
      // String IgnoredReasonName(AXIgnoredReason reason) in
      // inspector_type_builder_helper.cc.
      if (!cached_values_only && !IsDetached()) {
        AXObject::IgnoredReasons reasons;
        ComputeAccessibilityIsIgnored(&reasons);
        string_builder = string_builder + GetIgnoredReasonsDebugString(reasons);
      }
#endif
      if (cached_values_only ? !LastKnownIsIncludedInTreeValue()
                             : !AccessibilityIsIncludedInTree())
        string_builder = string_builder + " isRemovedFromTree";
    }
    if (GetNode()) {
      if (GetNode()->OwnerShadowHost()) {
        string_builder = string_builder + (GetNode()->IsInUserAgentShadowRoot()
                                               ? " inUserAgentShadowRoot:"
                                               : " inShadowRoot:");
        string_builder =
            string_builder + GetNodeString(GetNode()->OwnerShadowHost());
      }
      if (GetNode()->GetShadowRoot()) {
        string_builder = string_builder + " hasShadowRoot";
      }

      if (GetDocument() && CanSafelyUseFlatTreeTraversalNow(*GetDocument()) &&
          DisplayLockUtilities::ShouldIgnoreNodeDueToDisplayLock(
              *GetNode(), DisplayLockActivationReason::kAccessibility)) {
        string_builder = string_builder + " isDisplayLocked";
      }
    }
    if (cached_values_only) {
      if (cached_is_aria_hidden_)
        string_builder = string_builder + " ariaHidden";
    } else if (IsAriaHidden()) {
      const AXObject* aria_hidden_root = AriaHiddenRoot();
      if (aria_hidden_root) {
        string_builder = string_builder + " ariaHiddenRoot";
        if (aria_hidden_root != this) {
          string_builder =
              string_builder + GetNodeString(aria_hidden_root->GetNode());
        }
      } else {
        string_builder = string_builder + " ariaHiddenRootMissing";
      }
    } else if (AriaHiddenRoot()) {
      string_builder = string_builder + " ariaHiddenRootExtra";
    }
    if (cached_values_only ? cached_is_hidden_via_style_ : IsHiddenViaStyle()) {
      string_builder = string_builder + " isHiddenViaCSS";
    }
    if (cached_values_only ? cached_is_inert_ : IsInert())
      string_builder = string_builder + " isInert";
    if (children_dirty_) {
      string_builder = string_builder + " needsToUpdateChildren";
    } else if (!children_.empty()) {
      string_builder = string_builder + " #children=";
      string_builder = string_builder + String::Number(children_.size());
    }
    if (parent_) {
      if (parent_->HasDirtyDescendants()) {
        string_builder = string_builder + " parentHasDirtyDescendants";
      } else if (children_dirty_) {
        string_builder = string_builder + " parentMissingHasDirtyDescendants";
      }
    } else if (IsMissingParent()) {
      string_builder = string_builder + " isMissingParent";
    }
    if (!cached_values_only && !CanHaveChildren()) {
      string_builder = string_builder + " cannotHaveChildren";
    }
    if (!GetLayoutObject())
      string_builder = string_builder + " missingLayout";

    if (!cached_values_only)
      string_builder = string_builder + " name=";
  } else {
    string_builder = string_builder + ": ";
  }

  // Append name last, in case it is long.
  if (!cached_values_only)
    string_builder = string_builder + ComputedName().EncodeForDebugging();

  return string_builder;
}

bool operator==(const AXObject& first, const AXObject& second) {
  if (first.IsDetached() || second.IsDetached())
    return false;
  if (&first == &second) {
    DCHECK_EQ(first.AXObjectID(), second.AXObjectID());
    return true;
  }
  return false;
}

bool operator!=(const AXObject& first, const AXObject& second) {
  return !(first == second);
}

bool operator<(const AXObject& first, const AXObject& second) {
  if (first.IsDetached() || second.IsDetached())
    return false;

  int index_in_ancestor1, index_in_ancestor2;
  const AXObject* ancestor = AXObject::LowestCommonAncestor(
      first, second, &index_in_ancestor1, &index_in_ancestor2);
  DCHECK_GE(index_in_ancestor1, -1);
  DCHECK_GE(index_in_ancestor2, -1);
  if (!ancestor)
    return false;
  return index_in_ancestor1 < index_in_ancestor2;
}

bool operator<=(const AXObject& first, const AXObject& second) {
  return first == second || first < second;
}

bool operator>(const AXObject& first, const AXObject& second) {
  if (first.IsDetached() || second.IsDetached())
    return false;

  int index_in_ancestor1, index_in_ancestor2;
  const AXObject* ancestor = AXObject::LowestCommonAncestor(
      first, second, &index_in_ancestor1, &index_in_ancestor2);
  DCHECK_GE(index_in_ancestor1, -1);
  DCHECK_GE(index_in_ancestor2, -1);
  if (!ancestor)
    return false;
  return index_in_ancestor1 > index_in_ancestor2;
}

bool operator>=(const AXObject& first, const AXObject& second) {
  return first == second || first > second;
}

std::ostream& operator<<(std::ostream& stream, const AXObject* obj) {
  if (obj)
    return stream << obj->ToString(true).Utf8();
  else
    return stream << "<AXObject nullptr>";
}

std::ostream& operator<<(std::ostream& stream, const AXObject& obj) {
  return stream << obj.ToString(true).Utf8();
}

void AXObject::Trace(Visitor* visitor) const {
  visitor->Trace(children_);
  visitor->Trace(parent_);
  visitor->Trace(cached_live_region_root_);
  visitor->Trace(ax_object_cache_);
}

}  // namespace blink