[c8bea5]: accessible / src / generic / HyperTextAccessible.cpp  Maximize  Restore  History

Download this file

2479 lines (2074 with data), 82.6 kB

   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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=78: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "HyperTextAccessible.h"
#include "Accessible-inl.h"
#include "nsAccessibilityService.h"
#include "nsAccUtils.h"
#include "DocAccessible.h"
#include "Role.h"
#include "States.h"
#include "TextAttrs.h"
#include "nsIClipboard.h"
#include "nsContentUtils.h"
#include "nsFocusManager.h"
#include "nsIDOMRange.h"
#include "nsIEditingSession.h"
#include "nsIEditor.h"
#include "nsIFrame.h"
#include "nsFrameSelection.h"
#include "nsILineIterator.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIPlaintextEditor.h"
#include "nsIScrollableFrame.h"
#include "nsIServiceManager.h"
#include "nsTextFragment.h"
#include "mozilla/Selection.h"
#include "gfxSkipChars.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible
////////////////////////////////////////////////////////////////////////////////
HyperTextAccessible::
HyperTextAccessible(nsIContent* aNode, DocAccessible* aDoc) :
AccessibleWrap(aNode, aDoc)
{
mGenericTypes |= eHyperText;
}
NS_IMPL_ADDREF_INHERITED(HyperTextAccessible, AccessibleWrap)
NS_IMPL_RELEASE_INHERITED(HyperTextAccessible, AccessibleWrap)
nsresult
HyperTextAccessible::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
*aInstancePtr = nullptr;
// ARIA roles that these interfaces are not appropriate for.
if (!IsTextRole())
return Accessible::QueryInterface(aIID, aInstancePtr);
if (aIID.Equals(NS_GET_IID(nsIAccessibleText))) {
*aInstancePtr = static_cast<nsIAccessibleText*>(this);
NS_ADDREF_THIS();
return NS_OK;
}
if (aIID.Equals(NS_GET_IID(nsIAccessibleHyperText))) {
*aInstancePtr = static_cast<nsIAccessibleHyperText*>(this);
NS_ADDREF_THIS();
return NS_OK;
}
if (aIID.Equals(NS_GET_IID(nsIAccessibleEditableText))) {
*aInstancePtr = static_cast<nsIAccessibleEditableText*>(this);
NS_ADDREF_THIS();
return NS_OK;
}
return Accessible::QueryInterface(aIID, aInstancePtr);
}
role
HyperTextAccessible::NativeRole()
{
nsIAtom *tag = mContent->Tag();
if (tag == nsGkAtoms::dd)
return roles::DEFINITION;
if (tag == nsGkAtoms::form)
return roles::FORM;
if (tag == nsGkAtoms::blockquote || tag == nsGkAtoms::div ||
tag == nsGkAtoms::section || tag == nsGkAtoms::nav)
return roles::SECTION;
if (tag == nsGkAtoms::h1 || tag == nsGkAtoms::h2 ||
tag == nsGkAtoms::h3 || tag == nsGkAtoms::h4 ||
tag == nsGkAtoms::h5 || tag == nsGkAtoms::h6)
return roles::HEADING;
if (tag == nsGkAtoms::article)
return roles::DOCUMENT;
// Deal with html landmark elements
if (tag == nsGkAtoms::header)
return roles::HEADER;
if (tag == nsGkAtoms::footer)
return roles::FOOTER;
if (tag == nsGkAtoms::aside)
return roles::NOTE;
// Treat block frames as paragraphs
nsIFrame *frame = GetFrame();
if (frame && frame->GetType() == nsGkAtoms::blockFrame)
return roles::PARAGRAPH;
return roles::TEXT_CONTAINER; // In ATK this works
}
uint64_t
HyperTextAccessible::NativeState()
{
uint64_t states = AccessibleWrap::NativeState();
nsCOMPtr<nsIEditor> editor = GetEditor();
if (editor) {
states |= states::EDITABLE;
} else if (mContent->Tag() == nsGkAtoms::article) {
// We want <article> to behave like a document in terms of readonly state.
states |= states::READONLY;
}
if (HasChildren())
states |= states::SELECTABLE_TEXT;
return states;
}
// Substring must be entirely within the same text node
nsIntRect
HyperTextAccessible::GetBoundsForString(nsIFrame* aFrame, uint32_t aStartRenderedOffset,
uint32_t aEndRenderedOffset)
{
nsPresContext* presContext = mDoc->PresContext();
if (aFrame->GetType() != nsGkAtoms::textFrame) {
// XXX fallback for non-text frames, happens for bullets right now
// but in the future bullets will have proper text frames
return aFrame->GetScreenRectInAppUnits().
ToNearestPixels(presContext->AppUnitsPerDevPixel());
}
int32_t startContentOffset, endContentOffset;
nsresult rv = RenderedToContentOffset(aFrame, aStartRenderedOffset, &startContentOffset);
NS_ENSURE_SUCCESS(rv, nsIntRect());
rv = RenderedToContentOffset(aFrame, aEndRenderedOffset, &endContentOffset);
NS_ENSURE_SUCCESS(rv, nsIntRect());
nsIFrame *frame;
int32_t startContentOffsetInFrame;
// Get the right frame continuation -- not really a child, but a sibling of
// the primary frame passed in
rv = aFrame->GetChildFrameContainingOffset(startContentOffset, false,
&startContentOffsetInFrame, &frame);
NS_ENSURE_SUCCESS(rv, nsIntRect());
nsRect screenRect;
while (frame && startContentOffset < endContentOffset) {
// Start with this frame's screen rect, which we will
// shrink based on the substring we care about within it.
// We will then add that frame to the total screenRect we
// are returning.
nsRect frameScreenRect = frame->GetScreenRectInAppUnits();
// Get the length of the substring in this frame that we want the bounds for
int32_t startFrameTextOffset, endFrameTextOffset;
frame->GetOffsets(startFrameTextOffset, endFrameTextOffset);
int32_t frameTotalTextLength = endFrameTextOffset - startFrameTextOffset;
int32_t seekLength = endContentOffset - startContentOffset;
int32_t frameSubStringLength = std::min(frameTotalTextLength - startContentOffsetInFrame, seekLength);
// Add the point where the string starts to the frameScreenRect
nsPoint frameTextStartPoint;
rv = frame->GetPointFromOffset(startContentOffset, &frameTextStartPoint);
NS_ENSURE_SUCCESS(rv, nsIntRect());
frameScreenRect.x += frameTextStartPoint.x;
// Use the point for the end offset to calculate the width
nsPoint frameTextEndPoint;
rv = frame->GetPointFromOffset(startContentOffset + frameSubStringLength, &frameTextEndPoint);
NS_ENSURE_SUCCESS(rv, nsIntRect());
frameScreenRect.width = frameTextEndPoint.x - frameTextStartPoint.x;
screenRect.UnionRect(frameScreenRect, screenRect);
// Get ready to loop back for next frame continuation
startContentOffset += frameSubStringLength;
startContentOffsetInFrame = 0;
frame = frame->GetNextContinuation();
}
return screenRect.ToNearestPixels(presContext->AppUnitsPerDevPixel());
}
/*
* Gets the specified text.
*/
nsIFrame*
HyperTextAccessible::GetPosAndText(int32_t& aStartOffset, int32_t& aEndOffset,
nsAString* aText, nsIFrame** aEndFrame,
nsIntRect* aBoundsRect,
Accessible** aStartAcc,
Accessible** aEndAcc)
{
aStartOffset = ConvertMagicOffset(aStartOffset);
aEndOffset = ConvertMagicOffset(aEndOffset);
int32_t startOffset = aStartOffset;
int32_t endOffset = aEndOffset;
// XXX this prevents text interface usage on <input type="password">
bool isPassword = (Role() == roles::PASSWORD_TEXT);
// Clear out parameters and set up loop
if (aText) {
aText->Truncate();
}
if (endOffset < 0) {
const int32_t kMaxTextLength = 32767;
endOffset = kMaxTextLength; // Max end offset
}
else if (startOffset > endOffset) {
return nullptr;
}
nsIFrame *startFrame = nullptr;
nsIFrame* endFrame = nullptr;
if (aEndFrame) {
*aEndFrame = nullptr;
}
if (aBoundsRect) {
aBoundsRect->SetEmpty();
}
if (aStartAcc)
*aStartAcc = nullptr;
if (aEndAcc)
*aEndAcc = nullptr;
nsIntRect unionRect;
Accessible* lastAccessible = nullptr;
gfxSkipChars skipChars;
gfxSkipCharsIterator iter;
// Loop through children and collect valid offsets, text and bounds
// depending on what we need for out parameters.
uint32_t childCount = ChildCount();
for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) {
Accessible* childAcc = mChildren[childIdx];
lastAccessible = childAcc;
nsIFrame *frame = childAcc->GetFrame();
if (!frame) {
continue;
}
nsIFrame *primaryFrame = frame;
endFrame = frame;
if (!nsAccUtils::IsEmbeddedObject(childAcc)) {
// We only need info up to rendered offset -- that is what we're
// converting to content offset
int32_t substringEndOffset = -1;
uint32_t ourRenderedStart = 0;
int32_t ourContentStart = 0;
if (frame->GetType() == nsGkAtoms::textFrame) {
nsresult rv = frame->GetRenderedText(nullptr, &skipChars, &iter);
if (NS_SUCCEEDED(rv)) {
ourRenderedStart = iter.GetSkippedOffset();
ourContentStart = iter.GetOriginalOffset();
substringEndOffset =
iter.ConvertOriginalToSkipped(skipChars.GetOriginalCharCount() +
ourContentStart) - ourRenderedStart;
}
}
if (substringEndOffset < 0) {
// XXX for non-textframe text like list bullets,
// should go away after list bullet rewrite
substringEndOffset = nsAccUtils::TextLength(childAcc);
}
if (startOffset < substringEndOffset ||
(startOffset == substringEndOffset && (childIdx == childCount - 1))) {
// Our start is within this substring
if (startOffset > 0 || endOffset < substringEndOffset) {
// We don't want the whole string for this accessible
// Get out the continuing text frame with this offset
int32_t outStartLineUnused;
int32_t contentOffset;
if (frame->GetType() == nsGkAtoms::textFrame) {
contentOffset = iter.ConvertSkippedToOriginal(startOffset) +
ourRenderedStart - ourContentStart;
}
else {
contentOffset = startOffset;
}
frame->GetChildFrameContainingOffset(contentOffset, true,
&outStartLineUnused, &frame);
if (aEndFrame) {
*aEndFrame = frame; // We ended in the current frame
if (aEndAcc)
NS_ADDREF(*aEndAcc = childAcc);
}
if (substringEndOffset > endOffset) {
// Need to stop before the end of the available text
substringEndOffset = endOffset;
}
aEndOffset = endOffset;
}
if (aText) {
if (isPassword) {
for (int32_t count = startOffset; count < substringEndOffset; count ++)
*aText += '*'; // Show *'s only for password text
}
else {
childAcc->AppendTextTo(*aText, startOffset,
substringEndOffset - startOffset);
}
}
if (aBoundsRect) { // Caller wants the bounds of the text
aBoundsRect->UnionRect(*aBoundsRect,
GetBoundsForString(primaryFrame, startOffset,
substringEndOffset));
}
if (!startFrame) {
startFrame = frame;
aStartOffset = startOffset;
if (aStartAcc)
NS_ADDREF(*aStartAcc = childAcc);
}
// We already started copying in this accessible's string,
// for the next accessible we'll start at offset 0
startOffset = 0;
}
else {
// We have not found the start position yet, get the new startOffset
// that is relative to next accessible
startOffset -= substringEndOffset;
}
// The endOffset needs to be relative to the new startOffset
endOffset -= substringEndOffset;
}
else {
// Embedded object, append marker
// XXX Append \n for <br>'s
if (startOffset >= 1) {
-- startOffset;
}
else {
if (endOffset > 0) {
if (aText) {
// XXX: should use nsIAccessible::AppendTextTo.
if (frame->GetType() == nsGkAtoms::brFrame) {
*aText += kForcedNewLineChar;
} else if (nsAccUtils::MustPrune(this)) {
*aText += kImaginaryEmbeddedObjectChar;
// Expose imaginary embedded object character if the accessible
// hans't children.
} else {
*aText += kEmbeddedObjectChar;
}
}
if (aBoundsRect) {
nsIntRect frameScreenRect = frame->GetScreenRectInAppUnits().
ToNearestPixels(frame->PresContext()->AppUnitsPerDevPixel());
aBoundsRect->UnionRect(*aBoundsRect, frameScreenRect);
}
}
if (!startFrame) {
startFrame = frame;
aStartOffset = 0;
if (aStartAcc)
NS_ADDREF(*aStartAcc = childAcc);
}
}
-- endOffset;
}
if (endOffset <= 0 && startFrame) {
break; // If we don't have startFrame yet, get that in next loop iteration
}
}
if (aStartAcc && !*aStartAcc) {
NS_IF_ADDREF(*aStartAcc = lastAccessible);
}
if (aEndFrame && !*aEndFrame) {
*aEndFrame = endFrame;
if (aEndAcc && !*aEndAcc)
NS_IF_ADDREF(*aEndAcc = lastAccessible);
}
return startFrame;
}
NS_IMETHODIMP
HyperTextAccessible::GetText(int32_t aStartOffset, int32_t aEndOffset,
nsAString& aText)
{
aText.Truncate();
if (IsDefunct())
return NS_ERROR_FAILURE;
int32_t startOffset = ConvertMagicOffset(aStartOffset);
int32_t endOffset = ConvertMagicOffset(aEndOffset);
int32_t startChildIdx = GetChildIndexAtOffset(startOffset);
if (startChildIdx == -1) {
// 0 offsets are considered valid for empty text.
return (startOffset == 0 && endOffset == 0) ? NS_OK : NS_ERROR_INVALID_ARG;
}
int32_t endChildIdx = GetChildIndexAtOffset(endOffset);
if (endChildIdx == -1)
return NS_ERROR_INVALID_ARG;
if (startChildIdx == endChildIdx) {
int32_t childOffset = GetChildOffset(startChildIdx);
NS_ENSURE_STATE(childOffset != -1);
Accessible* child = GetChildAt(startChildIdx);
child->AppendTextTo(aText, startOffset - childOffset,
endOffset - startOffset);
return NS_OK;
}
int32_t startChildOffset = GetChildOffset(startChildIdx);
NS_ENSURE_STATE(startChildOffset != -1);
Accessible* startChild = GetChildAt(startChildIdx);
startChild->AppendTextTo(aText, startOffset - startChildOffset);
for (int32_t childIdx = startChildIdx + 1; childIdx < endChildIdx; childIdx++) {
Accessible* child = GetChildAt(childIdx);
child->AppendTextTo(aText);
}
int32_t endChildOffset = GetChildOffset(endChildIdx);
NS_ENSURE_STATE(endChildOffset != -1);
Accessible* endChild = GetChildAt(endChildIdx);
endChild->AppendTextTo(aText, 0, endOffset - endChildOffset);
return NS_OK;
}
/*
* Gets the character count.
*/
NS_IMETHODIMP
HyperTextAccessible::GetCharacterCount(int32_t* aCharacterCount)
{
NS_ENSURE_ARG_POINTER(aCharacterCount);
*aCharacterCount = 0;
if (IsDefunct())
return NS_ERROR_FAILURE;
*aCharacterCount = CharacterCount();
return NS_OK;
}
/*
* Gets the specified character.
*/
NS_IMETHODIMP
HyperTextAccessible::GetCharacterAtOffset(int32_t aOffset, PRUnichar* aCharacter)
{
NS_ENSURE_ARG_POINTER(aCharacter);
*aCharacter = L'\0';
if (IsDefunct())
return NS_ERROR_FAILURE;
nsAutoString character;
if (GetCharAt(aOffset, eGetAt, character)) {
*aCharacter = character.First();
return NS_OK;
}
return NS_ERROR_INVALID_ARG;
}
Accessible*
HyperTextAccessible::DOMPointToHypertextOffset(nsINode* aNode,
int32_t aNodeOffset,
int32_t* aHyperTextOffset,
bool aIsEndOffset)
{
if (!aHyperTextOffset)
return nullptr;
*aHyperTextOffset = 0;
if (!aNode)
return nullptr;
uint32_t addTextOffset = 0;
nsINode* findNode = nullptr;
if (aNodeOffset == -1) {
findNode = aNode;
} else if (aNode->IsNodeOfType(nsINode::eTEXT)) {
// For text nodes, aNodeOffset comes in as a character offset
// Text offset will be added at the end, if we find the offset in this hypertext
// We want the "skipped" offset into the text (rendered text without the extra whitespace)
nsIFrame *frame = aNode->AsContent()->GetPrimaryFrame();
NS_ENSURE_TRUE(frame, nullptr);
nsresult rv = ContentToRenderedOffset(frame, aNodeOffset, &addTextOffset);
NS_ENSURE_SUCCESS(rv, nullptr);
// Get the child node and
findNode = aNode;
} else {
// findNode could be null if aNodeOffset == # of child nodes, which means
// one of two things:
// 1) there are no children, and the passed-in node is not mContent -- use
// parentContent for the node to find
// 2) there are no children and the passed-in node is mContent, which means
// we're an empty nsIAccessibleText
// 3) there are children and we're at the end of the children
findNode = aNode->GetChildAt(aNodeOffset);
if (!findNode) {
if (aNodeOffset == 0) {
if (aNode == GetNode()) {
// Case #1: this accessible has no children and thus has empty text,
// we can only be at hypertext offset 0.
*aHyperTextOffset = 0;
return nullptr;
}
// Case #2: there are no children, we're at this node.
findNode = aNode;
} else if (aNodeOffset == aNode->GetChildCount()) {
// Case #3: we're after the last child, get next node to this one.
for (nsINode* tmpNode = aNode;
!findNode && tmpNode && tmpNode != mContent;
tmpNode = tmpNode->GetParent()) {
findNode = tmpNode->GetNextSibling();
}
}
}
}
// Get accessible for this findNode, or if that node isn't accessible, use the
// accessible for the next DOM node which has one (based on forward depth first search)
Accessible* descendantAcc = nullptr;
if (findNode) {
nsCOMPtr<nsIContent> findContent(do_QueryInterface(findNode));
if (findContent && findContent->IsHTML() &&
findContent->NodeInfo()->Equals(nsGkAtoms::br) &&
findContent->AttrValueIs(kNameSpaceID_None,
nsGkAtoms::mozeditorbogusnode,
nsGkAtoms::_true,
eIgnoreCase)) {
// This <br> is the hacky "bogus node" used when there is no text in a control
*aHyperTextOffset = 0;
return nullptr;
}
descendantAcc = GetFirstAvailableAccessible(findNode);
}
// From the descendant, go up and get the immediate child of this hypertext
Accessible* childAccAtOffset = nullptr;
while (descendantAcc) {
Accessible* parentAcc = descendantAcc->Parent();
if (parentAcc == this) {
childAccAtOffset = descendantAcc;
break;
}
// This offset no longer applies because the passed-in text object is not a child
// of the hypertext. This happens when there are nested hypertexts, e.g.
// <div>abc<h1>def</h1>ghi</div>
// If the passed-in DOM point was not on a direct child of the hypertext, we will
// return the offset for that entire hypertext
if (aIsEndOffset) {
// Not inclusive, the indicated char comes at index before this offset
// If the end offset is after the first character of the passed in object, use 1 for
// addTextOffset, to put us after the embedded object char. We'll only treat the offset as
// before the embedded object char if we end at the very beginning of the child.
addTextOffset = addTextOffset > 0;
}
else {
// Start offset, inclusive
// Make sure the offset lands on the embedded object character in order to indicate
// the true inner offset is inside the subtree for that link
addTextOffset =
(nsAccUtils::TextLength(descendantAcc) == addTextOffset) ? 1 : 0;
}
descendantAcc = parentAcc;
}
// Loop through, adding offsets until we reach childAccessible
// If childAccessible is null we will end up adding up the entire length of
// the hypertext, which is good -- it just means our offset node
// came after the last accessible child's node
uint32_t childCount = ChildCount();
uint32_t childIdx = 0;
Accessible* childAcc = nullptr;
for (; childIdx < childCount; childIdx++) {
childAcc = mChildren[childIdx];
if (childAcc == childAccAtOffset)
break;
*aHyperTextOffset += nsAccUtils::TextLength(childAcc);
}
if (childIdx < childCount) {
*aHyperTextOffset += addTextOffset;
NS_ASSERTION(childAcc == childAccAtOffset,
"These should be equal whenever we exit loop and childAcc != nullptr");
if (childIdx < childCount - 1 ||
addTextOffset < nsAccUtils::TextLength(childAccAtOffset)) {
// If not at end of last text node, we will return the accessible we were in
return childAccAtOffset;
}
}
return nullptr;
}
nsresult
HyperTextAccessible::HypertextOffsetsToDOMRange(int32_t aStartHTOffset,
int32_t aEndHTOffset,
nsRange* aRange)
{
// If the given offsets are 0 and associated editor is empty then return
// collapsed range with editor root element as range container.
if (aStartHTOffset == 0 && aEndHTOffset == 0) {
nsCOMPtr<nsIEditor> editor = GetEditor();
if (editor) {
bool isEmpty = false;
editor->GetDocumentIsEmpty(&isEmpty);
if (isEmpty) {
nsCOMPtr<nsIDOMElement> editorRootElm;
editor->GetRootElement(getter_AddRefs(editorRootElm));
nsCOMPtr<nsINode> editorRoot(do_QueryInterface(editorRootElm));
if (editorRoot) {
aRange->SetStart(editorRoot, 0);
aRange->SetEnd(editorRoot, 0);
return NS_OK;
}
}
}
}
nsRefPtr<Accessible> startAcc, endAcc;
int32_t startOffset = aStartHTOffset, endOffset = aEndHTOffset;
nsIFrame *startFrame = nullptr, *endFrame = nullptr;
startFrame = GetPosAndText(startOffset, endOffset, nullptr, &endFrame, nullptr,
getter_AddRefs(startAcc), getter_AddRefs(endAcc));
if (!startAcc || !endAcc)
return NS_ERROR_FAILURE;
DOMPoint startPoint, endPoint;
nsresult rv = GetDOMPointByFrameOffset(startFrame, startOffset, startAcc,
&startPoint);
NS_ENSURE_SUCCESS(rv, rv);
rv = aRange->SetStart(startPoint.node, startPoint.idx);
NS_ENSURE_SUCCESS(rv, rv);
if (aStartHTOffset == aEndHTOffset)
return aRange->SetEnd(startPoint.node, startPoint.idx);
rv = GetDOMPointByFrameOffset(endFrame, endOffset, endAcc, &endPoint);
NS_ENSURE_SUCCESS(rv, rv);
return aRange->SetEnd(endPoint.node, endPoint.idx);
}
int32_t
HyperTextAccessible::GetRelativeOffset(nsIPresShell* aPresShell,
nsIFrame* aFromFrame,
int32_t aFromOffset,
Accessible* aFromAccessible,
nsSelectionAmount aAmount,
nsDirection aDirection,
bool aNeedsStart,
EWordMovementType aWordMovementType)
{
const bool kIsJumpLinesOk = true; // okay to jump lines
const bool kIsScrollViewAStop = false; // do not stop at scroll views
const bool kIsKeyboardSelect = true; // is keyboard selection
const bool kIsVisualBidi = false; // use visual order for bidi text
// Ask layout for the new node and offset, after moving the appropriate amount
nsresult rv;
int32_t contentOffset = aFromOffset;
nsIFrame *frame = aFromAccessible->GetFrame();
NS_ENSURE_TRUE(frame, -1);
if (frame->GetType() == nsGkAtoms::textFrame) {
rv = RenderedToContentOffset(frame, aFromOffset, &contentOffset);
NS_ENSURE_SUCCESS(rv, -1);
}
nsPeekOffsetStruct pos(aAmount, aDirection, contentOffset,
0, kIsJumpLinesOk, kIsScrollViewAStop, kIsKeyboardSelect, kIsVisualBidi,
aWordMovementType);
rv = aFromFrame->PeekOffset(&pos);
if (NS_FAILED(rv)) {
if (aDirection == eDirPrevious) {
// Use passed-in frame as starting point in failure case for now,
// this is a hack to deal with starting on a list bullet frame,
// which fails in PeekOffset() because the line iterator doesn't see it.
// XXX Need to look at our overall handling of list bullets, which are an odd case
pos.mResultContent = aFromFrame->GetContent();
int32_t endOffsetUnused;
aFromFrame->GetOffsets(pos.mContentOffset, endOffsetUnused);
}
else {
return -1;
}
}
// Turn the resulting node and offset into a hyperTextOffset
int32_t hyperTextOffset;
if (!pos.mResultContent)
return -1;
// If finalAccessible is nullptr, then DOMPointToHypertextOffset() searched
// through the hypertext children without finding the node/offset position.
Accessible* finalAccessible =
DOMPointToHypertextOffset(pos.mResultContent, pos.mContentOffset,
&hyperTextOffset, aDirection == eDirNext);
if (!finalAccessible && aDirection == eDirPrevious) {
// If we reached the end during search, this means we didn't find the DOM point
// and we're actually at the start of the paragraph
hyperTextOffset = 0;
}
else if (aAmount == eSelectBeginLine) {
Accessible* firstChild = mChildren.SafeElementAt(0, nullptr);
// For line selection with needsStart, set start of line exactly to line break
if (pos.mContentOffset == 0 && firstChild &&
firstChild->Role() == roles::STATICTEXT &&
static_cast<int32_t>(nsAccUtils::TextLength(firstChild)) == hyperTextOffset) {
// XXX Bullet hack -- we should remove this once list bullets use anonymous content
hyperTextOffset = 0;
}
if (!aNeedsStart && hyperTextOffset > 0) {
-- hyperTextOffset;
}
}
else if (aAmount == eSelectEndLine && finalAccessible) {
// If not at very end of hypertext, we may need change the end of line offset by 1,
// to make sure we are in the right place relative to the line ending
if (finalAccessible->Role() == roles::WHITESPACE) { // Landed on <br> hard line break
// if aNeedsStart, set end of line exactly 1 character past line break
// XXX It would be cleaner if we did not have to have the hard line break check,
// and just got the correct results from PeekOffset() for the <br> case -- the returned offset should
// come after the new line, as it does in other cases.
++ hyperTextOffset; // Get past hard line break
}
// We are now 1 character past the line break
if (!aNeedsStart) {
-- hyperTextOffset;
}
}
return hyperTextOffset;
}
int32_t
HyperTextAccessible::FindWordBoundary(int32_t aOffset, nsDirection aDirection,
EWordMovementType aWordMovementType)
{
// Convert hypertext offset to frame-relative offset.
int32_t offsetInFrame = aOffset, notUsedOffset = aOffset;
nsRefPtr<Accessible> accAtOffset;
nsIFrame* frameAtOffset =
GetPosAndText(offsetInFrame, notUsedOffset, nullptr, nullptr,
nullptr, getter_AddRefs(accAtOffset));
if (!frameAtOffset) {
if (aOffset == CharacterCount()) {
// Asking for start of line, while on last character.
if (accAtOffset)
frameAtOffset = accAtOffset->GetFrame();
}
NS_ASSERTION(frameAtOffset, "No start frame for text getting!");
if (!frameAtOffset)
return -1;
// We're on the last continuation since we're on the last character.
frameAtOffset = frameAtOffset->GetLastContinuation();
}
// Return hypertext offset of the boundary of the found word.
return GetRelativeOffset(mDoc->PresShell(), frameAtOffset, offsetInFrame,
accAtOffset, eSelectWord, aDirection,
(aWordMovementType == eStartWord),
aWordMovementType);
}
/*
Gets the specified text relative to aBoundaryType, which means:
BOUNDARY_CHAR The character before/at/after the offset is returned.
BOUNDARY_WORD_START From the word start before/at/after the offset to the next word start.
BOUNDARY_WORD_END From the word end before/at/after the offset to the next work end.
BOUNDARY_LINE_START From the line start before/at/after the offset to the next line start.
BOUNDARY_LINE_END From the line end before/at/after the offset to the next line start.
*/
nsresult
HyperTextAccessible::GetTextHelper(EGetTextType aType,
AccessibleTextBoundary aBoundaryType,
int32_t aOffset,
int32_t* aStartOffset, int32_t* aEndOffset,
nsAString& aText)
{
aText.Truncate();
NS_ENSURE_ARG_POINTER(aStartOffset);
NS_ENSURE_ARG_POINTER(aEndOffset);
*aStartOffset = *aEndOffset = 0;
int32_t offset = ConvertMagicOffset(aOffset);
if (offset < 0)
return NS_ERROR_INVALID_ARG;
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET && offset > 0 &&
(aBoundaryType == BOUNDARY_LINE_START ||
aBoundaryType == BOUNDARY_LINE_END)) {
// It is the same character offset when the caret is visually at
// the very end of a line or the start of a new line. Getting text at
// the line should provide the line with the visual caret,
// otherwise screen readers will announce the wrong line as the user
// presses up or down arrow and land at the end of a line.
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
if (frameSelection &&
frameSelection->GetHint() == nsFrameSelection::HINTLEFT) {
-- offset; // We are at the start of a line
}
}
nsSelectionAmount amount;
bool needsStart = false;
switch (aBoundaryType) {
case BOUNDARY_WORD_START:
needsStart = true;
amount = eSelectWord;
break;
case BOUNDARY_WORD_END:
amount = eSelectWord;
break;
case BOUNDARY_LINE_START:
// Newlines are considered at the end of a line. Since getting
// the BOUNDARY_LINE_START gets the text from the line-start to the next
// line-start, the newline is included at the end of the string.
needsStart = true;
amount = eSelectLine;
break;
case BOUNDARY_LINE_END:
// Newlines are considered at the end of a line. Since getting
// the BOUNDARY_END_START gets the text from the line-end to the next
//line-end, the newline is included at the beginning of the string.
amount = eSelectLine;
break;
case BOUNDARY_ATTRIBUTE_RANGE:
{
nsresult rv = GetTextAttributes(false, offset,
aStartOffset, aEndOffset, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
return GetText(*aStartOffset, *aEndOffset, aText);
}
default: // Note, sentence support is deprecated and falls through to here
return NS_ERROR_INVALID_ARG;
}
int32_t startOffset = offset + (aBoundaryType == BOUNDARY_LINE_END); // Avoid getting the previous line
int32_t endOffset = startOffset;
// Convert offsets to frame-relative
nsRefPtr<Accessible> startAcc;
nsIFrame *startFrame = GetPosAndText(startOffset, endOffset, nullptr, nullptr,
nullptr, getter_AddRefs(startAcc));
if (!startFrame) {
int32_t textLength = CharacterCount();
if (aBoundaryType == BOUNDARY_LINE_START && offset > 0 && offset == textLength) {
// Asking for start of line, while on last character
if (startAcc)
startFrame = startAcc->GetFrame();
}
if (!startFrame) {
return offset > textLength ? NS_ERROR_FAILURE : NS_OK;
}
else {
// We're on the last continuation since we're on the last character
startFrame = startFrame->GetLastContinuation();
}
}
int32_t finalStartOffset = 0, finalEndOffset = 0;
EWordMovementType wordMovementType = needsStart ? eStartWord : eEndWord;
nsIPresShell* presShell = mDoc->PresShell();
// If aType == eGetAt we'll change both the start and end offset from
// the original offset
if (aType == eGetAfter) {
finalStartOffset = offset;
}
else {
finalStartOffset = GetRelativeOffset(presShell, startFrame, startOffset,
startAcc,
(amount == eSelectLine ? eSelectBeginLine : amount),
eDirPrevious, needsStart,
wordMovementType);
NS_ENSURE_TRUE(finalStartOffset >= 0, NS_ERROR_FAILURE);
}
if (aType == eGetBefore) {
finalEndOffset = offset;
}
else {
// Start moving forward from the start so that we don't get
// 2 words/lines if the offset occurred on whitespace boundary
// Careful, startOffset and endOffset are passed by reference to GetPosAndText() and changed
// For BOUNDARY_LINE_END, make sure we start of this line
startOffset = endOffset = finalStartOffset + (aBoundaryType == BOUNDARY_LINE_END);
nsRefPtr<Accessible> endAcc;
nsIFrame *endFrame = GetPosAndText(startOffset, endOffset, nullptr, nullptr,
nullptr, getter_AddRefs(endAcc));
if (endAcc && endAcc->Role() == roles::STATICTEXT) {
// Static text like list bullets will ruin our forward calculation,
// since the caret cannot be in the static text. Start just after the static text.
startOffset = endOffset = finalStartOffset +
(aBoundaryType == BOUNDARY_LINE_END) +
nsAccUtils::TextLength(endAcc);
endFrame = GetPosAndText(startOffset, endOffset, nullptr, nullptr,
nullptr, getter_AddRefs(endAcc));
}
if (!endFrame) {
return NS_ERROR_FAILURE;
}
finalEndOffset = GetRelativeOffset(presShell, endFrame, endOffset, endAcc,
(amount == eSelectLine ? eSelectEndLine : amount),
eDirNext, needsStart, wordMovementType);
NS_ENSURE_TRUE(endOffset >= 0, NS_ERROR_FAILURE);
if (finalEndOffset == offset) {
if (aType == eGetAt && amount == eSelectWord) {
// Fix word error for the first character in word: PeekOffset() will return the previous word when
// offset points to the first character of the word, but accessibility APIs want the current word
// that the first character is in
return GetTextHelper(eGetAfter, aBoundaryType, offset,
aStartOffset, aEndOffset, aText);
}
int32_t textLength = CharacterCount();
if (finalEndOffset < textLength) {
// This happens sometimes when current character at finalStartOffset
// is an embedded object character representing another hypertext, that
// the AT really needs to dig into separately
++ finalEndOffset;
}
}
}
*aStartOffset = finalStartOffset;
*aEndOffset = finalEndOffset;
NS_ASSERTION((finalStartOffset < offset && finalEndOffset >= offset) || aType != eGetBefore, "Incorrect results for GetTextHelper");
NS_ASSERTION((finalStartOffset <= offset && finalEndOffset > offset) || aType == eGetBefore, "Incorrect results for GetTextHelper");
GetPosAndText(finalStartOffset, finalEndOffset, &aText);
return NS_OK;
}
/**
* nsIAccessibleText impl.
*/
NS_IMETHODIMP
HyperTextAccessible::GetTextBeforeOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset, nsAString& aText)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
int32_t offset = ConvertMagicOffset(aOffset);
if (offset < 0)
return NS_ERROR_INVALID_ARG;
switch (aBoundaryType) {
case BOUNDARY_CHAR:
GetCharAt(offset, eGetBefore, aText, aStartOffset, aEndOffset);
return NS_OK;
case BOUNDARY_WORD_START: {
if (offset == 0) { // no word before 0 offset
*aStartOffset = *aEndOffset = 0;
return NS_OK;
}
// If the offset is a word start then move backward to find start offset
// (end offset is the given offset). Otherwise move backward twice to find
// both start and end offsets.
int32_t midOffset = FindWordBoundary(offset, eDirPrevious, eStartWord);
*aEndOffset = FindWordBoundary(midOffset, eDirNext, eStartWord);
if (*aEndOffset == offset) {
*aStartOffset = midOffset;
return GetText(*aStartOffset, *aEndOffset, aText);
}
*aStartOffset = FindWordBoundary(midOffset, eDirPrevious, eStartWord);
*aEndOffset = midOffset;
return GetText(*aStartOffset, *aEndOffset, aText);
}
case BOUNDARY_WORD_END: {
if (offset == 0) { // no word before 0 offset
*aStartOffset = *aEndOffset = 0;
return NS_OK;
}
// Move word backward twice to find start and end offsets.
*aEndOffset = FindWordBoundary(offset, eDirPrevious, eEndWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eEndWord);
return GetText(*aStartOffset, *aEndOffset, aText);
}
case BOUNDARY_LINE_START:
case BOUNDARY_LINE_END:
case BOUNDARY_ATTRIBUTE_RANGE:
return GetTextHelper(eGetBefore, aBoundaryType, aOffset,
aStartOffset, aEndOffset, aText);
default:
return NS_ERROR_INVALID_ARG;
}
}
NS_IMETHODIMP
HyperTextAccessible::GetTextAtOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset, nsAString& aText)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
int32_t offset = ConvertMagicOffset(aOffset);
if (offset < 0)
return NS_ERROR_INVALID_ARG;
EWordMovementType wordMovementType = eDefaultBehavior;
bool moveForwardThenBack = true;
switch (aBoundaryType) {
case BOUNDARY_CHAR:
return GetCharAt(aOffset, eGetAt, aText, aStartOffset, aEndOffset) ?
NS_OK : NS_ERROR_INVALID_ARG;
case BOUNDARY_WORD_START:
*aEndOffset = FindWordBoundary(offset, eDirNext, eStartWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eStartWord);
return GetText(*aStartOffset, *aEndOffset, aText);
case BOUNDARY_WORD_END:
// Ignore the spec and follow what WebKitGtk does because Orca expects it,
// i.e. return a next word at word end offset of the current word
// (WebKitGtk behavior) instead the current word (AKT spec).
*aEndOffset = FindWordBoundary(offset, eDirNext, eEndWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eEndWord);
return GetText(*aStartOffset, *aEndOffset, aText);
case BOUNDARY_LINE_START:
case BOUNDARY_LINE_END:
case BOUNDARY_ATTRIBUTE_RANGE:
return GetTextHelper(eGetAt, aBoundaryType, aOffset,
aStartOffset, aEndOffset, aText);
default:
return NS_ERROR_INVALID_ARG;
}
}
NS_IMETHODIMP
HyperTextAccessible::GetTextAfterOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset, nsAString& aText)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
if (aBoundaryType == BOUNDARY_CHAR) {
GetCharAt(aOffset, eGetAfter, aText, aStartOffset, aEndOffset);
return NS_OK;
}
return GetTextHelper(eGetAfter, aBoundaryType, aOffset,
aStartOffset, aEndOffset, aText);
}
// nsIPersistentProperties
// nsIAccessibleText::getTextAttributes(in boolean includeDefAttrs,
// in long offset,
// out long rangeStartOffset,
// out long rangeEndOffset);
NS_IMETHODIMP
HyperTextAccessible::GetTextAttributes(bool aIncludeDefAttrs,
int32_t aOffset,
int32_t* aStartOffset,
int32_t* aEndOffset,
nsIPersistentProperties** aAttributes)
{
// 1. Get each attribute and its ranges one after another.
// 2. As we get each new attribute, we pass the current start and end offsets
// as in/out parameters. In other words, as attributes are collected,
// the attribute range itself can only stay the same or get smaller.
NS_ENSURE_ARG_POINTER(aStartOffset);
*aStartOffset = 0;
NS_ENSURE_ARG_POINTER(aEndOffset);
*aEndOffset = 0;
if (IsDefunct())
return NS_ERROR_FAILURE;
int32_t offset = ConvertMagicOffset(aOffset);
if (aAttributes) {
*aAttributes = nullptr;
nsCOMPtr<nsIPersistentProperties> attributes =
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID);
NS_ENSURE_TRUE(attributes, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(*aAttributes = attributes);
}
Accessible* accAtOffset = GetChildAtOffset(offset);
if (!accAtOffset) {
// Offset 0 is correct offset when accessible has empty text. Include
// default attributes if they were requested, otherwise return empty set.
if (offset == 0) {
if (aIncludeDefAttrs) {
TextAttrsMgr textAttrsMgr(this);
textAttrsMgr.GetAttributes(*aAttributes);
}
return NS_OK;
}
return NS_ERROR_INVALID_ARG;
}
int32_t accAtOffsetIdx = accAtOffset->IndexInParent();
int32_t startOffset = GetChildOffset(accAtOffsetIdx);
int32_t endOffset = GetChildOffset(accAtOffsetIdx + 1);
int32_t offsetInAcc = offset - startOffset;
TextAttrsMgr textAttrsMgr(this, aIncludeDefAttrs, accAtOffset,
accAtOffsetIdx);
textAttrsMgr.GetAttributes(*aAttributes, &startOffset, &endOffset);
// Compute spelling attributes on text accessible only.
nsIFrame *offsetFrame = accAtOffset->GetFrame();
if (offsetFrame && offsetFrame->GetType() == nsGkAtoms::textFrame) {
int32_t nodeOffset = 0;
nsresult rv = RenderedToContentOffset(offsetFrame, offsetInAcc,
&nodeOffset);
NS_ENSURE_SUCCESS(rv, rv);
// Set 'misspelled' text attribute.
rv = GetSpellTextAttribute(accAtOffset->GetNode(), nodeOffset,
&startOffset, &endOffset,
aAttributes ? *aAttributes : nullptr);
NS_ENSURE_SUCCESS(rv, rv);
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return NS_OK;
}
// nsIPersistentProperties
// nsIAccessibleText::defaultTextAttributes
NS_IMETHODIMP
HyperTextAccessible::GetDefaultTextAttributes(nsIPersistentProperties** aAttributes)
{
NS_ENSURE_ARG_POINTER(aAttributes);
*aAttributes = nullptr;
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIPersistentProperties> attributes =
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID);
NS_ENSURE_TRUE(attributes, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(*aAttributes = attributes);
TextAttrsMgr textAttrsMgr(this);
textAttrsMgr.GetAttributes(*aAttributes);
return NS_OK;
}
int32_t
HyperTextAccessible::GetLevelInternal()
{
nsIAtom *tag = mContent->Tag();
if (tag == nsGkAtoms::h1)
return 1;
if (tag == nsGkAtoms::h2)
return 2;
if (tag == nsGkAtoms::h3)
return 3;
if (tag == nsGkAtoms::h4)
return 4;
if (tag == nsGkAtoms::h5)
return 5;
if (tag == nsGkAtoms::h6)
return 6;
return AccessibleWrap::GetLevelInternal();
}
already_AddRefed<nsIPersistentProperties>
HyperTextAccessible::NativeAttributes()
{
nsCOMPtr<nsIPersistentProperties> attributes =
AccessibleWrap::NativeAttributes();
// 'formatting' attribute is deprecated, 'display' attribute should be
// instead.
nsIFrame *frame = GetFrame();
if (frame && frame->GetType() == nsGkAtoms::blockFrame) {
nsAutoString unused;
attributes->SetStringProperty(NS_LITERAL_CSTRING("formatting"),
NS_LITERAL_STRING("block"), unused);
}
if (FocusMgr()->IsFocused(this)) {
int32_t lineNumber = CaretLineNumber();
if (lineNumber >= 1) {
nsAutoString strLineNumber;
strLineNumber.AppendInt(lineNumber);
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::lineNumber, strLineNumber);
}
}
if (!HasOwnContent())
return attributes.forget();
// For the html landmark elements we expose them like we do aria landmarks to
// make AT navigation schemes "just work".
nsIAtom* tag = mContent->Tag();
if (tag == nsGkAtoms::nav) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("navigation"));
} else if (tag == nsGkAtoms::section) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("region"));
} else if (tag == nsGkAtoms::header || tag == nsGkAtoms::footer) {
// Only map header and footer if they are not descendants
// of an article or section tag.
nsIContent* parent = mContent->GetParent();
while (parent) {
if (parent->Tag() == nsGkAtoms::article ||
parent->Tag() == nsGkAtoms::section)
break;
parent = parent->GetParent();
}
// No article or section elements found.
if (!parent) {
if (tag == nsGkAtoms::header) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("banner"));
} else if (tag == nsGkAtoms::footer) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("contentinfo"));
}
}
} else if (tag == nsGkAtoms::footer) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("contentinfo"));
} else if (tag == nsGkAtoms::aside) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("complementary"));
} else if (tag == nsGkAtoms::article) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("article"));
} else if (tag == nsGkAtoms::main) {
nsAccUtils::SetAccAttr(attributes, nsGkAtoms::xmlroles,
NS_LITERAL_STRING("main"));
}
return attributes.forget();
}
/*
* Given an offset, the x, y, width, and height values are filled appropriately.
*/
NS_IMETHODIMP
HyperTextAccessible::GetCharacterExtents(int32_t aOffset, int32_t* aX, int32_t* aY,
int32_t* aWidth, int32_t* aHeight,
uint32_t aCoordType)
{
return GetRangeExtents(aOffset, aOffset + 1, aX, aY, aWidth, aHeight, aCoordType);
}
/*
* Given a start & end offset, the x, y, width, and height values are filled appropriately.
*/
NS_IMETHODIMP
HyperTextAccessible::GetRangeExtents(int32_t aStartOffset, int32_t aEndOffset,
int32_t* aX, int32_t* aY,
int32_t* aWidth, int32_t* aHeight,
uint32_t aCoordType)
{
nsIntRect boundsRect;
nsIFrame *endFrameUnused;
if (!GetPosAndText(aStartOffset, aEndOffset, nullptr, &endFrameUnused, &boundsRect) ||
boundsRect.IsEmpty()) {
return NS_ERROR_FAILURE;
}
*aX = boundsRect.x;
*aY = boundsRect.y;
*aWidth = boundsRect.width;
*aHeight = boundsRect.height;
nsAccUtils::ConvertScreenCoordsTo(aX, aY, aCoordType, this);
return NS_OK;
}
/*
* Gets the offset of the character located at coordinates x and y. x and y are interpreted as being relative to
* the screen or this widget's window depending on coords.
*/
NS_IMETHODIMP
HyperTextAccessible::GetOffsetAtPoint(int32_t aX, int32_t aY,
uint32_t aCoordType, int32_t* aOffset)
{
*aOffset = -1;
if (IsDefunct())
return NS_ERROR_FAILURE;
nsIFrame *hyperFrame = GetFrame();
if (!hyperFrame) {
return NS_ERROR_FAILURE;
}
nsIntPoint coords = nsAccUtils::ConvertToScreenCoords(aX, aY, aCoordType,
this);
nsPresContext* presContext = mDoc->PresContext();
nsPoint coordsInAppUnits =
coords.ToAppUnits(presContext->AppUnitsPerDevPixel());
nsRect frameScreenRect = hyperFrame->GetScreenRectInAppUnits();
if (!frameScreenRect.Contains(coordsInAppUnits.x, coordsInAppUnits.y))
return NS_OK; // Not found, will return -1
nsPoint pointInHyperText(coordsInAppUnits.x - frameScreenRect.x,
coordsInAppUnits.y - frameScreenRect.y);
// Go through the frames to check if each one has the point.
// When one does, add up the character offsets until we have a match
// We have an point in an accessible child of this, now we need to add up the
// offsets before it to what we already have
int32_t offset = 0;
uint32_t childCount = ChildCount();
for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) {
Accessible* childAcc = mChildren[childIdx];
nsIFrame *primaryFrame = childAcc->GetFrame();
NS_ENSURE_TRUE(primaryFrame, NS_ERROR_FAILURE);
nsIFrame *frame = primaryFrame;
while (frame) {
nsIContent *content = frame->GetContent();
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
nsPoint pointInFrame = pointInHyperText - frame->GetOffsetTo(hyperFrame);
nsSize frameSize = frame->GetSize();
if (pointInFrame.x < frameSize.width && pointInFrame.y < frameSize.height) {
// Finished
if (frame->GetType() == nsGkAtoms::textFrame) {
nsIFrame::ContentOffsets contentOffsets =
frame->GetContentOffsetsFromPointExternal(pointInFrame, nsIFrame::IGNORE_SELECTION_STYLE);
if (contentOffsets.IsNull() || contentOffsets.content != content) {
return NS_OK; // Not found, will return -1
}
uint32_t addToOffset;
nsresult rv = ContentToRenderedOffset(primaryFrame,
contentOffsets.offset,
&addToOffset);
NS_ENSURE_SUCCESS(rv, rv);
offset += addToOffset;
}
*aOffset = offset;
return NS_OK;
}
frame = frame->GetNextContinuation();
}
offset += nsAccUtils::TextLength(childAcc);
}
return NS_OK; // Not found, will return -1
}
////////////////////////////////////////////////////////////////////////////////
// nsIAccessibleHyperText
NS_IMETHODIMP
HyperTextAccessible::GetLinkCount(int32_t* aLinkCount)
{
NS_ENSURE_ARG_POINTER(aLinkCount);
*aLinkCount = 0;
if (IsDefunct())
return NS_ERROR_FAILURE;
*aLinkCount = GetLinkCount();
return NS_OK;
}
NS_IMETHODIMP
HyperTextAccessible::GetLinkAt(int32_t aIndex, nsIAccessibleHyperLink** aLink)
{
NS_ENSURE_ARG_POINTER(aLink);
*aLink = nullptr;
if (IsDefunct())
return NS_ERROR_FAILURE;
Accessible* link = GetLinkAt(aIndex);
if (link)
CallQueryInterface(link, aLink);
return NS_OK;
}
NS_IMETHODIMP
HyperTextAccessible::GetLinkIndex(nsIAccessibleHyperLink* aLink,
int32_t* aIndex)
{
NS_ENSURE_ARG_POINTER(aLink);
if (IsDefunct())
return NS_ERROR_FAILURE;
nsRefPtr<Accessible> link(do_QueryObject(aLink));
*aIndex = GetLinkIndex(link);
return NS_OK;
}
NS_IMETHODIMP
HyperTextAccessible::GetLinkIndexAtOffset(int32_t aOffset, int32_t* aLinkIndex)
{
NS_ENSURE_ARG_POINTER(aLinkIndex);
*aLinkIndex = -1; // API says this magic value means 'not found'
if (IsDefunct())
return NS_ERROR_FAILURE;
*aLinkIndex = GetLinkIndexAtOffset(aOffset);
return NS_OK;
}
/**
* nsIAccessibleEditableText impl.
*/
NS_IMETHODIMP
HyperTextAccessible::SetAttributes(int32_t aStartPos, int32_t aEndPos,
nsISupports* aAttributes)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
HyperTextAccessible::SetTextContents(const nsAString& aText)
{
int32_t numChars = CharacterCount();
if (numChars == 0 || NS_SUCCEEDED(DeleteText(0, numChars))) {
return InsertText(aText, 0);
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIEditor> editor = GetEditor();
nsCOMPtr<nsIPlaintextEditor> peditor(do_QueryInterface(editor));
NS_ENSURE_STATE(peditor);
nsresult rv = SetSelectionRange(aPosition, aPosition);
NS_ENSURE_SUCCESS(rv, rv);
return peditor->InsertText(aText);
}
NS_IMETHODIMP
HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIEditor> editor = GetEditor();
NS_ENSURE_STATE(editor);
nsresult rv = SetSelectionRange(aStartPos, aEndPos);
NS_ENSURE_SUCCESS(rv, rv);
return editor->Copy();
}
NS_IMETHODIMP
HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIEditor> editor = GetEditor();
NS_ENSURE_STATE(editor);
nsresult rv = SetSelectionRange(aStartPos, aEndPos);
NS_ENSURE_SUCCESS(rv, rv);
return editor->Cut();
}
NS_IMETHODIMP
HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIEditor> editor = GetEditor();
NS_ENSURE_STATE(editor);
nsresult rv = SetSelectionRange(aStartPos, aEndPos);
NS_ENSURE_SUCCESS(rv, rv);
return editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
}
NS_IMETHODIMP
HyperTextAccessible::PasteText(int32_t aPosition)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsIEditor> editor = GetEditor();
NS_ENSURE_STATE(editor);
nsresult rv = SetSelectionRange(aPosition, aPosition);
NS_ENSURE_SUCCESS(rv, rv);
return editor->Paste(nsIClipboard::kGlobalClipboard);
}
already_AddRefed<nsIEditor>
HyperTextAccessible::GetEditor() const
{
if (!mContent->HasFlag(NODE_IS_EDITABLE)) {
// If we're inside an editable container, then return that container's editor
Accessible* ancestor = Parent();
while (ancestor) {
HyperTextAccessible* hyperText = ancestor->AsHyperText();
if (hyperText) {
// Recursion will stop at container doc because it has its own impl
// of GetEditor()
return hyperText->GetEditor();
}
ancestor = ancestor->Parent();
}
return nullptr;
}
nsCOMPtr<nsIDocShell> docShell = nsCoreUtils::GetDocShellFor(mContent);
nsCOMPtr<nsIEditingSession> editingSession(do_GetInterface(docShell));
if (!editingSession)
return nullptr; // No editing session interface
nsCOMPtr<nsIEditor> editor;
nsIDocument* docNode = mDoc->DocumentNode();
editingSession->GetEditorForWindow(docNode->GetWindow(),
getter_AddRefs(editor));
return editor.forget();
}
/**
* =================== Caret & Selection ======================
*/
nsresult
HyperTextAccessible::SetSelectionRange(int32_t aStartPos, int32_t aEndPos)
{
// Before setting the selection range, we need to ensure that the editor
// is initialized. (See bug 804927.)
// Otherwise, it's possible that lazy editor initialization will override
// the selection we set here and leave the caret at the end of the text.
// By calling GetEditor here, we ensure that editor initialization is
// completed before we set the selection.
nsCOMPtr<nsIEditor> editor = GetEditor();
bool isFocusable = InteractiveState() & states::FOCUSABLE;
// If accessible is focusable then focus it before setting the selection to
// neglect control's selection changes on focus if any (for example, inputs
// that do select all on focus).
// some input controls
if (isFocusable)
TakeFocus();
// Set the selection
SetSelectionBounds(0, aStartPos, aEndPos);
// If range 0 was successfully set, clear any additional selection
// ranges remaining from previous selection
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
NS_ENSURE_STATE(frameSelection);
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_STATE(domSel);
for (int32_t idx = domSel->GetRangeCount() - 1; idx > 0; idx--)
domSel->RemoveRange(domSel->GetRangeAt(idx));
// When selection is done, move the focus to the selection if accessible is
// not focusable. That happens when selection is set within hypertext
// accessible.
if (isFocusable)
return NS_OK;
nsFocusManager* DOMFocusManager = nsFocusManager::GetFocusManager();
if (DOMFocusManager) {
NS_ENSURE_TRUE(mDoc, NS_ERROR_FAILURE);
nsIDocument* docNode = mDoc->DocumentNode();
NS_ENSURE_TRUE(docNode, NS_ERROR_FAILURE);
nsCOMPtr<nsPIDOMWindow> window = docNode->GetWindow();
nsCOMPtr<nsIDOMElement> result;
DOMFocusManager->MoveFocus(window, nullptr, nsIFocusManager::MOVEFOCUS_CARET,
nsIFocusManager::FLAG_BYMOVEFOCUS, getter_AddRefs(result));
}
return NS_OK;
}
NS_IMETHODIMP
HyperTextAccessible::SetCaretOffset(int32_t aCaretOffset)
{
return SetSelectionRange(aCaretOffset, aCaretOffset);
}
/*
* Gets the offset position of the caret (cursor).
*/
NS_IMETHODIMP
HyperTextAccessible::GetCaretOffset(int32_t* aCaretOffset)
{
NS_ENSURE_ARG_POINTER(aCaretOffset);
*aCaretOffset = -1;
if (IsDefunct())
return NS_ERROR_FAILURE;
// Not focused focusable accessible except document accessible doesn't have
// a caret.
if (!IsDoc() && !FocusMgr()->IsFocused(this) &&
(InteractiveState() & states::FOCUSABLE)) {
return NS_OK;
}
// No caret if the focused node is not inside this DOM node and this DOM node
// is not inside of focused node.
FocusManager::FocusDisposition focusDisp =
FocusMgr()->IsInOrContainsFocus(this);
if (focusDisp == FocusManager::eNone)
return NS_OK;
// Turn the focus node and offset of the selection into caret hypretext
// offset.
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
NS_ENSURE_STATE(frameSelection);
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_STATE(domSel);
nsINode* focusNode = domSel->GetFocusNode();
int32_t focusOffset = domSel->GetFocusOffset();
// No caret if this DOM node is inside of focused node but the selection's
// focus point is not inside of this DOM node.
if (focusDisp == FocusManager::eContainedByFocus) {
nsINode *resultNode =
nsCoreUtils::GetDOMNodeFromDOMPoint(focusNode, focusOffset);
nsINode* thisNode = GetNode();
if (resultNode != thisNode &&
!nsCoreUtils::IsAncestorOf(thisNode, resultNode))
return NS_OK;
}
DOMPointToHypertextOffset(focusNode, focusOffset, aCaretOffset);
return NS_OK;
}
int32_t
HyperTextAccessible::CaretLineNumber()
{
// Provide the line number for the caret, relative to the
// currently focused node. Use a 1-based index
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
if (!frameSelection)
return -1;
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
if (!domSel)
return - 1;
nsINode* caretNode = domSel->GetFocusNode();
if (!caretNode || !caretNode->IsContent())
return -1;
nsIContent* caretContent = caretNode->AsContent();
if (!nsCoreUtils::IsAncestorOf(GetNode(), caretContent))
return -1;
int32_t returnOffsetUnused;
int32_t caretOffset = domSel->GetFocusOffset();
nsFrameSelection::HINT hint = frameSelection->GetHint();
nsIFrame *caretFrame = frameSelection->GetFrameForNodeOffset(caretContent, caretOffset,
hint, &returnOffsetUnused);
NS_ENSURE_TRUE(caretFrame, -1);
int32_t lineNumber = 1;
nsAutoLineIterator lineIterForCaret;
nsIContent *hyperTextContent = IsContent() ? mContent.get() : nullptr;
while (caretFrame) {
if (hyperTextContent == caretFrame->GetContent()) {
return lineNumber; // Must be in a single line hyper text, there is no line iterator
}
nsIFrame *parentFrame = caretFrame->GetParent();
if (!parentFrame)
break;
// Add lines for the sibling frames before the caret
nsIFrame *sibling = parentFrame->GetFirstPrincipalChild();
while (sibling && sibling != caretFrame) {
nsAutoLineIterator lineIterForSibling = sibling->GetLineIterator();
if (lineIterForSibling) {
// For the frames before that grab all the lines
int32_t addLines = lineIterForSibling->GetNumLines();
lineNumber += addLines;
}
sibling = sibling->GetNextSibling();
}
// Get the line number relative to the container with lines
if (!lineIterForCaret) { // Add the caret line just once
lineIterForCaret = parentFrame->GetLineIterator();
if (lineIterForCaret) {
// Ancestor of caret
int32_t addLines = lineIterForCaret->FindLineContaining(caretFrame);
lineNumber += addLines;
}
}
caretFrame = parentFrame;
}
NS_NOTREACHED("DOM ancestry had this hypertext but frame ancestry didn't");
return lineNumber;
}
already_AddRefed<nsFrameSelection>
HyperTextAccessible::FrameSelection()
{
nsIFrame* frame = GetFrame();
return frame ? frame->GetFrameSelection() : nullptr;
}
void
HyperTextAccessible::GetSelectionDOMRanges(int16_t aType,
nsTArray<nsRange*>* aRanges)
{
// Ignore selection if it is not visible.
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
if (!frameSelection ||
frameSelection->GetDisplaySelection() <= nsISelectionController::SELECTION_HIDDEN)
return;
Selection* domSel = frameSelection->GetSelection(aType);
if (!domSel)
return;
nsCOMPtr<nsINode> startNode = GetNode();
nsCOMPtr<nsIEditor> editor = GetEditor();
if (editor) {
nsCOMPtr<nsIDOMElement> editorRoot;
editor->GetRootElement(getter_AddRefs(editorRoot));
startNode = do_QueryInterface(editorRoot);
}
if (!startNode)
return;
uint32_t childCount = startNode->GetChildCount();
nsresult rv = domSel->
GetRangesForIntervalArray(startNode, 0, startNode, childCount, true, aRanges);
NS_ENSURE_SUCCESS_VOID(rv);
// Remove collapsed ranges
uint32_t numRanges = aRanges->Length();
for (uint32_t idx = 0; idx < numRanges; idx ++) {
if ((*aRanges)[idx]->Collapsed()) {
aRanges->RemoveElementAt(idx);
--numRanges;
--idx;
}
}
}
/*
* Gets the number of selected regions.
*/
NS_IMETHODIMP
HyperTextAccessible::GetSelectionCount(int32_t* aSelectionCount)
{
NS_ENSURE_ARG_POINTER(aSelectionCount);
*aSelectionCount = 0;
nsTArray<nsRange*> ranges;
GetSelectionDOMRanges(nsISelectionController::SELECTION_NORMAL, &ranges);
*aSelectionCount = int32_t(ranges.Length());
return NS_OK;
}
/*
* Gets the start and end offset of the specified selection.
*/
NS_IMETHODIMP
HyperTextAccessible::GetSelectionBounds(int32_t aSelectionNum,
int32_t* aStartOffset,
int32_t* aEndOffset)
{
NS_ENSURE_ARG_POINTER(aStartOffset);
NS_ENSURE_ARG_POINTER(aEndOffset);
*aStartOffset = *aEndOffset = 0;
nsTArray<nsRange*> ranges;
GetSelectionDOMRanges(nsISelectionController::SELECTION_NORMAL, &ranges);
uint32_t rangeCount = ranges.Length();
if (aSelectionNum < 0 || aSelectionNum >= rangeCount)
return NS_ERROR_INVALID_ARG;
nsRange* range = ranges[aSelectionNum];
// Get start and end points.
nsINode* startNode = range->GetStartParent();
nsINode* endNode = range->GetEndParent();
int32_t startOffset = range->StartOffset(), endOffset = range->EndOffset();
// Make sure start is before end, by swapping DOM points. This occurs when
// the user selects backwards in the text.
int32_t rangeCompare = nsContentUtils::ComparePoints(endNode, endOffset,
startNode, startOffset);
if (rangeCompare < 0) {
nsINode* tempNode = startNode;
startNode = endNode;
endNode = tempNode;
int32_t tempOffset = startOffset;
startOffset = endOffset;
endOffset = tempOffset;
}
Accessible* startAccessible =
DOMPointToHypertextOffset(startNode, startOffset, aStartOffset);
if (!startAccessible) {
*aStartOffset = 0; // Could not find start point within this hypertext, so starts before
}
DOMPointToHypertextOffset(endNode, endOffset, aEndOffset, true);
return NS_OK;
}
/*
* Changes the start and end offset of the specified selection.
*/
NS_IMETHODIMP
HyperTextAccessible::SetSelectionBounds(int32_t aSelectionNum,
int32_t aStartOffset,
int32_t aEndOffset)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
if (aSelectionNum < 0)
return NS_ERROR_INVALID_ARG;
int32_t startOffset = ConvertMagicOffset(aStartOffset);
int32_t endOffset = ConvertMagicOffset(aEndOffset);
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
NS_ENSURE_STATE(frameSelection);
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_STATE(domSel);
uint32_t rangeCount = domSel->GetRangeCount();
if (rangeCount < static_cast<uint32_t>(aSelectionNum))
return NS_ERROR_INVALID_ARG;
nsRefPtr<nsRange> range;
if (aSelectionNum == rangeCount)
range = new nsRange(mContent);
else
range = domSel->GetRangeAt(aSelectionNum);
nsresult rv = HypertextOffsetsToDOMRange(startOffset, endOffset, range);
NS_ENSURE_SUCCESS(rv, rv);
// If new range was created then add it, otherwise notify selection listeners
// that existing selection range was changed.
if (aSelectionNum == rangeCount)
return domSel->AddRange(range);
domSel->RemoveRange(range);
domSel->AddRange(range);
return NS_OK;
}
/*
* Adds a selection bounded by the specified offsets.
*/
NS_IMETHODIMP
HyperTextAccessible::AddSelection(int32_t aStartOffset, int32_t aEndOffset)
{
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
NS_ENSURE_STATE(frameSelection);
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_STATE(domSel);
return SetSelectionBounds(domSel->GetRangeCount(), aStartOffset, aEndOffset);
}
/*
* Removes the specified selection.
*/
NS_IMETHODIMP
HyperTextAccessible::RemoveSelection(int32_t aSelectionNum)
{
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
NS_ENSURE_STATE(frameSelection);
Selection* domSel =
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
NS_ENSURE_STATE(domSel);
if (aSelectionNum < 0 || aSelectionNum >= domSel->GetRangeCount())
return NS_ERROR_INVALID_ARG;
return domSel->RemoveRange(domSel->GetRangeAt(aSelectionNum));
}
// void nsIAccessibleText::
// scrollSubstringTo(in long startIndex, in long endIndex,
// in unsigned long scrollType);
NS_IMETHODIMP
HyperTextAccessible::ScrollSubstringTo(int32_t aStartIndex, int32_t aEndIndex,
uint32_t aScrollType)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsRefPtr<nsRange> range = new nsRange(mContent);
nsresult rv = HypertextOffsetsToDOMRange(aStartIndex, aEndIndex, range);
NS_ENSURE_SUCCESS(rv, rv);
return nsCoreUtils::ScrollSubstringTo(GetFrame(), range, aScrollType);
}
// void nsIAccessibleText::
// scrollSubstringToPoint(in long startIndex, in long endIndex,
// in unsigned long coordinateType,
// in long x, in long y);
NS_IMETHODIMP
HyperTextAccessible::ScrollSubstringToPoint(int32_t aStartIndex,
int32_t aEndIndex,
uint32_t aCoordinateType,
int32_t aX, int32_t aY)
{
nsIFrame *frame = GetFrame();
if (!frame)
return NS_ERROR_FAILURE;
nsIntPoint coords = nsAccUtils::ConvertToScreenCoords(aX, aY, aCoordinateType,
this);
nsRefPtr<nsRange> range = new nsRange(mContent);
nsresult rv = HypertextOffsetsToDOMRange(aStartIndex, aEndIndex, range);
NS_ENSURE_SUCCESS(rv, rv);
nsPresContext* presContext = frame->PresContext();
nsPoint coordsInAppUnits =
coords.ToAppUnits(presContext->AppUnitsPerDevPixel());
bool initialScrolled = false;
nsIFrame *parentFrame = frame;
while ((parentFrame = parentFrame->GetParent())) {
nsIScrollableFrame *scrollableFrame = do_QueryFrame(parentFrame);
if (scrollableFrame) {
if (!initialScrolled) {
// Scroll substring to the given point. Turn the point into percents
// relative scrollable area to use nsCoreUtils::ScrollSubstringTo.
nsRect frameRect = parentFrame->GetScreenRectInAppUnits();
nscoord offsetPointX = coordsInAppUnits.x - frameRect.x;
nscoord offsetPointY = coordsInAppUnits.y - frameRect.y;
nsSize size(parentFrame->GetSize());
// avoid divide by zero
size.width = size.width ? size.width : 1;
size.height = size.height ? size.height : 1;
int16_t hPercent = offsetPointX * 100 / size.width;
int16_t vPercent = offsetPointY * 100 / size.height;
rv = nsCoreUtils::ScrollSubstringTo(frame, range, vPercent, hPercent);
NS_ENSURE_SUCCESS(rv, rv);
initialScrolled = true;
} else {
// Substring was scrolled to the given point already inside its closest
// scrollable area. If there are nested scrollable areas then make
// sure we scroll lower areas to the given point inside currently
// traversed scrollable area.
nsCoreUtils::ScrollFrameToPoint(parentFrame, frame, coords);
}
}
frame = parentFrame;
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// Accessible public
// Accessible protected
ENameValueFlag
HyperTextAccessible::NativeName(nsString& aName)
{
ENameValueFlag nameFlag = AccessibleWrap::NativeName(aName);
if (!aName.IsEmpty())
return nameFlag;
// Get name from title attribute for HTML abbr and acronym elements making it
// a valid name from markup. Otherwise their name isn't picked up by recursive
// name computation algorithm. See NS_OK_NAME_FROM_TOOLTIP.
if (IsAbbreviation() &&
mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::title, aName))
aName.CompressWhitespace();
return eNameOK;
}
void
HyperTextAccessible::InvalidateChildren()
{
mOffsets.Clear();
AccessibleWrap::InvalidateChildren();
}
bool
HyperTextAccessible::RemoveChild(Accessible* aAccessible)
{
int32_t childIndex = aAccessible->IndexInParent();
int32_t count = mOffsets.Length() - childIndex;
if (count > 0)
mOffsets.RemoveElementsAt(childIndex, count);
return Accessible::RemoveChild(aAccessible);
}
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible public static
nsresult
HyperTextAccessible::ContentToRenderedOffset(nsIFrame* aFrame, int32_t aContentOffset,
uint32_t* aRenderedOffset)
{
if (!aFrame) {
// Current frame not rendered -- this can happen if text is set on
// something with display: none
*aRenderedOffset = 0;
return NS_OK;
}
if (IsTextField()) {
*aRenderedOffset = aContentOffset;
return NS_OK;
}
NS_ASSERTION(aFrame->GetType() == nsGkAtoms::textFrame,
"Need text frame for offset conversion");
NS_ASSERTION(aFrame->GetPrevContinuation() == nullptr,
"Call on primary frame only");
gfxSkipChars skipChars;
gfxSkipCharsIterator iter;
// Only get info up to original offset, we know that will be larger than skipped offset
nsresult rv = aFrame->GetRenderedText(nullptr, &skipChars, &iter, 0, aContentOffset);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t ourRenderedStart = iter.GetSkippedOffset();
int32_t ourContentStart = iter.GetOriginalOffset();
*aRenderedOffset = iter.ConvertOriginalToSkipped(aContentOffset + ourContentStart) -
ourRenderedStart;
return NS_OK;
}
nsresult
HyperTextAccessible::RenderedToContentOffset(nsIFrame* aFrame, uint32_t aRenderedOffset,
int32_t* aContentOffset)
{
if (IsTextField()) {
*aContentOffset = aRenderedOffset;
return NS_OK;
}
*aContentOffset = 0;
NS_ENSURE_TRUE(aFrame, NS_ERROR_FAILURE);
NS_ASSERTION(aFrame->GetType() == nsGkAtoms::textFrame,
"Need text frame for offset conversion");
NS_ASSERTION(aFrame->GetPrevContinuation() == nullptr,
"Call on primary frame only");
gfxSkipChars skipChars;
gfxSkipCharsIterator iter;
// We only need info up to skipped offset -- that is what we're converting to original offset
nsresult rv = aFrame->GetRenderedText(nullptr, &skipChars, &iter, 0, aRenderedOffset);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t ourRenderedStart = iter.GetSkippedOffset();
int32_t ourContentStart = iter.GetOriginalOffset();
*aContentOffset = iter.ConvertSkippedToOriginal(aRenderedOffset + ourRenderedStart) - ourContentStart;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible public
bool
HyperTextAccessible::GetCharAt(int32_t aOffset, EGetTextType aShift,
nsAString& aChar, int32_t* aStartOffset,
int32_t* aEndOffset)
{
aChar.Truncate();
int32_t offset = ConvertMagicOffset(aOffset) + static_cast<int32_t>(aShift);
int32_t childIdx = GetChildIndexAtOffset(offset);
if (childIdx == -1)
return false;
Accessible* child = GetChildAt(childIdx);
child->AppendTextTo(aChar, offset - GetChildOffset(childIdx), 1);
if (aStartOffset)
*aStartOffset = offset;
if (aEndOffset)
*aEndOffset = aChar.IsEmpty() ? offset : offset + 1;
return true;
}
int32_t
HyperTextAccessible::GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter)
{
if (aChildIndex == 0) {
if (aInvalidateAfter)
mOffsets.Clear();
return aChildIndex;
}
int32_t count = mOffsets.Length() - aChildIndex;
if (count > 0) {
if (aInvalidateAfter)
mOffsets.RemoveElementsAt(aChildIndex, count);
return mOffsets[aChildIndex - 1];
}
uint32_t lastOffset = mOffsets.IsEmpty() ?
0 : mOffsets[mOffsets.Length() - 1];
while (mOffsets.Length() < aChildIndex) {
Accessible* child = mChildren[mOffsets.Length()];
lastOffset += nsAccUtils::TextLength(child);
mOffsets.AppendElement(lastOffset);
}
return mOffsets[aChildIndex - 1];
}
int32_t
HyperTextAccessible::GetChildIndexAtOffset(uint32_t aOffset)
{
uint32_t lastOffset = 0;
uint32_t offsetCount = mOffsets.Length();
if (offsetCount > 0) {
lastOffset = mOffsets[offsetCount - 1];
if (aOffset < lastOffset) {
uint32_t low = 0, high = offsetCount;
while (high > low) {
uint32_t mid = (high + low) >> 1;
if (mOffsets[mid] == aOffset)
return mid < offsetCount - 1 ? mid + 1 : mid;
if (mOffsets[mid] < aOffset)
low = mid + 1;
else
high = mid;
}
if (high == offsetCount)
return -1;
return low;
}
}
uint32_t childCount = ChildCount();
while (mOffsets.Length() < childCount) {
Accessible* child = GetChildAt(mOffsets.Length());
lastOffset += nsAccUtils::TextLength(child);
mOffsets.AppendElement(lastOffset);
if (aOffset < lastOffset)
return mOffsets.Length() - 1;
}
if (aOffset == lastOffset)
return mOffsets.Length() - 1;
return -1;
}
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible protected
nsresult
HyperTextAccessible::GetDOMPointByFrameOffset(nsIFrame* aFrame, int32_t aOffset,
Accessible* aAccessible,
DOMPoint* aPoint)
{
NS_ENSURE_ARG(aAccessible);
if (!aFrame) {
// If the given frame is null then set offset after the DOM node of the
// given accessible.
NS_ASSERTION(!aAccessible->IsDoc(),
"Shouldn't be called on document accessible!");
nsIContent* content = aAccessible->GetContent();
NS_ASSERTION(content, "Shouldn't operate on defunct accessible!");
nsIContent* parent = content->GetParent();
aPoint->idx = parent->IndexOf(content) + 1;
aPoint->node = parent;
} else if (aFrame->GetType() == nsGkAtoms::textFrame) {
nsIContent* content = aFrame->GetContent();
NS_ENSURE_STATE(content);
nsIFrame *primaryFrame = content->GetPrimaryFrame();
nsresult rv = RenderedToContentOffset(primaryFrame, aOffset, &(aPoint->idx));
NS_ENSURE_SUCCESS(rv, rv);
aPoint->node = content;
} else {
nsIContent* content = aFrame->GetContent();
NS_ENSURE_STATE(content);
nsIContent* parent = content->GetParent();
NS_ENSURE_STATE(parent);
aPoint->idx = parent->IndexOf(content);
aPoint->node = parent;
}
return NS_OK;
}
// HyperTextAccessible
nsresult
HyperTextAccessible::RangeBoundToHypertextOffset(nsRange* aRange,
bool aIsStartBound,
bool aIsStartHTOffset,
int32_t* aHTOffset)
{
nsINode* node = nullptr;
int32_t nodeOffset = 0;
if (aIsStartBound) {
node = aRange->GetStartParent();
nodeOffset = aRange->StartOffset();
} else {
node = aRange->GetEndParent();
nodeOffset = aRange->EndOffset();
}
Accessible* startAcc =
DOMPointToHypertextOffset(node, nodeOffset, aHTOffset);
if (aIsStartHTOffset && !startAcc)
*aHTOffset = 0;
return NS_OK;
}
// HyperTextAccessible
nsresult
HyperTextAccessible::GetSpellTextAttribute(nsINode* aNode,
int32_t aNodeOffset,
int32_t* aHTStartOffset,
int32_t* aHTEndOffset,
nsIPersistentProperties* aAttributes)
{
nsRefPtr<nsFrameSelection> fs = FrameSelection();
if (!fs)
return NS_OK;
Selection* domSel = fs->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
if (!domSel)
return NS_OK;
int32_t rangeCount = domSel->GetRangeCount();
if (rangeCount <= 0)
return NS_OK;
int32_t startHTOffset = 0, endHTOffset = 0;
nsresult rv = NS_OK;
for (int32_t idx = 0; idx < rangeCount; idx++) {
nsRange* range = domSel->GetRangeAt(idx);
if (range->Collapsed())
continue;
// See if the point comes after the range in which case we must continue in
// case there is another range after this one.
nsINode* endNode = range->GetEndParent();
int32_t endOffset = range->EndOffset();
if (nsContentUtils::ComparePoints(aNode, aNodeOffset, endNode, endOffset) >= 0)
continue;
// At this point our point is either in this range or before it but after
// the previous range. So we check to see if the range starts before the
// point in which case the point is in the missspelled range, otherwise it
// must be before the range and after the previous one if any.
nsINode* startNode = range->GetStartParent();
int32_t startOffset = range->StartOffset();
if (nsContentUtils::ComparePoints(startNode, startOffset, aNode,
aNodeOffset) <= 0) {
rv = RangeBoundToHypertextOffset(range, true, true, &startHTOffset);
NS_ENSURE_SUCCESS(rv, rv);
rv = RangeBoundToHypertextOffset(range, false, false, &endHTOffset);
NS_ENSURE_SUCCESS(rv, rv);
if (startHTOffset > *aHTStartOffset)
*aHTStartOffset = startHTOffset;
if (endHTOffset < *aHTEndOffset)
*aHTEndOffset = endHTOffset;
if (aAttributes) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid,
NS_LITERAL_STRING("spelling"));
}
return NS_OK;
}
// This range came after the point.
rv = RangeBoundToHypertextOffset(range, true, false, &endHTOffset);
NS_ENSURE_SUCCESS(rv, rv);
if (idx > 0) {
rv = RangeBoundToHypertextOffset(domSel->GetRangeAt(idx - 1), false,
true, &startHTOffset);
NS_ENSURE_SUCCESS(rv, rv);
}
if (startHTOffset > *aHTStartOffset)
*aHTStartOffset = startHTOffset;
if (endHTOffset < *aHTEndOffset)
*aHTEndOffset = endHTOffset;
return NS_OK;
}
// We never found a range that ended after the point, therefore we know that
// the point is not in a range, that we do not need to compute an end offset,
// and that we should use the end offset of the last range to compute the
// start offset of the text attribute range.
rv = RangeBoundToHypertextOffset(domSel->GetRangeAt(rangeCount - 1), false,
true, &startHTOffset);
NS_ENSURE_SUCCESS(rv, rv);
if (startHTOffset > *aHTStartOffset)
*aHTStartOffset = startHTOffset;
return NS_OK;
}
bool
HyperTextAccessible::IsTextRole()
{
if (mRoleMapEntry &&
(mRoleMapEntry->role == roles::GRAPHIC ||
mRoleMapEntry->role == roles::IMAGE_MAP ||
mRoleMapEntry->role == roles::SLIDER ||
mRoleMapEntry->role == roles::PROGRESSBAR ||
mRoleMapEntry->role == roles::SEPARATOR))
return false;
return true;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks