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
|
/* -*- 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-inl.h"
#include "Accessible-inl.h"
#include "nsAccessibilityService.h"
#include "nsAccessiblePivot.h"
#include "nsIAccessibleTypes.h"
#include "DocAccessible.h"
#include "HTMLListAccessible.h"
#include "Pivot.h"
#include "Relation.h"
#include "Role.h"
#include "States.h"
#include "TextAttrs.h"
#include "TextRange.h"
#include "TreeWalker.h"
#include "nsCaret.h"
#include "nsContentUtils.h"
#include "nsDebug.h"
#include "nsFocusManager.h"
#include "nsIEditingSession.h"
#include "nsContainerFrame.h"
#include "nsFrameSelection.h"
#include "nsILineIterator.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsPersistentProperties.h"
#include "nsIScrollableFrame.h"
#include "nsIMathMLFrame.h"
#include "nsRange.h"
#include "nsTextFragment.h"
#include "mozilla/Assertions.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/EventStates.h"
#include "mozilla/HTMLEditor.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/TextEditor.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLBRElement.h"
#include "mozilla/dom/HTMLHeadingElement.h"
#include "mozilla/dom/Selection.h"
#include "gfxSkipChars.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::a11y;
/**
* This class is used in HyperTextAccessible to search for paragraph
* boundaries.
*/
class ParagraphBoundaryRule : public PivotRule {
public:
explicit ParagraphBoundaryRule(Accessible* aAnchor,
uint32_t aAnchorTextoffset,
nsDirection aDirection,
bool aSkipAnchorSubtree = false)
: mAnchor(aAnchor),
mAnchorTextOffset(aAnchorTextoffset),
mDirection(aDirection),
mSkipAnchorSubtree(aSkipAnchorSubtree),
mLastMatchTextOffset(0) {}
virtual uint16_t Match(const AccessibleOrProxy& aAccOrProxy) override {
MOZ_ASSERT(aAccOrProxy.IsAccessible());
Accessible* acc = aAccOrProxy.AsAccessible();
if (acc->IsOuterDoc()) {
// The child document might be remote and we can't (and don't want to)
// handle remote documents. Also, iframes are inline anyway and thus
// can't be paragraph boundaries. Therefore, skip this unconditionally.
return nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
}
uint16_t result = nsIAccessibleTraversalRule::FILTER_IGNORE;
if (mSkipAnchorSubtree && acc == mAnchor) {
result |= nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
}
// First, deal with the case that we encountered a line break, for example,
// a br in a paragraph.
if (acc->Role() == roles::WHITESPACE) {
result |= nsIAccessibleTraversalRule::FILTER_MATCH;
return result;
}
// Now, deal with the case that we encounter a new block level accessible.
// This also means a new paragraph boundary start.
nsIFrame* frame = acc->GetFrame();
if (frame && frame->IsBlockFrame()) {
result |= nsIAccessibleTraversalRule::FILTER_MATCH;
return result;
}
// A text leaf can contain a line break if it's pre-formatted text.
if (acc->IsTextLeaf()) {
nsAutoString name;
acc->Name(name);
int32_t offset;
if (mDirection == eDirPrevious) {
if (acc == mAnchor && mAnchorTextOffset == 0) {
// We're already at the start of this node, so there can be no line
// break before.
return result;
}
// If we began on a line break, we don't want to match it, so search
// from 1 before our anchor offset.
offset =
name.RFindChar('\n', acc == mAnchor ? mAnchorTextOffset - 1 : -1);
} else {
offset = name.FindChar('\n', acc == mAnchor ? mAnchorTextOffset : 0);
}
if (offset != -1) {
// Line ebreak!
mLastMatchTextOffset = offset;
result |= nsIAccessibleTraversalRule::FILTER_MATCH;
}
}
return result;
}
// This is only valid if the last match was a text leaf. It returns the
// offset of the line break character in that text leaf.
uint32_t GetLastMatchTextOffset() { return mLastMatchTextOffset; }
private:
Accessible* mAnchor;
uint32_t mAnchorTextOffset;
nsDirection mDirection;
bool mSkipAnchorSubtree;
uint32_t mLastMatchTextOffset;
};
/**
* This class is used in HyperTextAccessible::FindParagraphStartOffset to
* search forward exactly one step from a match found by the above.
* It should only be initialized with a boundary, and it will skip that
* boundary's sub tree if it is a block element boundary.
*/
class SkipParagraphBoundaryRule : public PivotRule {
public:
explicit SkipParagraphBoundaryRule(AccessibleOrProxy& aBoundary)
: mBoundary(aBoundary) {}
virtual uint16_t Match(const AccessibleOrProxy& aAccOrProxy) override {
MOZ_ASSERT(aAccOrProxy.IsAccessible());
// If matching the boundary, skip its sub tree.
if (aAccOrProxy == mBoundary) {
return nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
}
return nsIAccessibleTraversalRule::FILTER_MATCH;
}
private:
AccessibleOrProxy& mBoundary;
};
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible
////////////////////////////////////////////////////////////////////////////////
HyperTextAccessible::HyperTextAccessible(nsIContent* aNode, DocAccessible* aDoc)
: AccessibleWrap(aNode, aDoc) {
mType = eHyperTextType;
mGenericTypes |= eHyperText;
}
role HyperTextAccessible::NativeRole() const {
a11y::role r = GetAccService()->MarkupRole(mContent);
if (r != roles::NOTHING) return r;
nsIFrame* frame = GetFrame();
if (frame && frame->IsInlineFrame()) return roles::TEXT;
return roles::TEXT_CONTAINER;
}
uint64_t HyperTextAccessible::NativeState() const {
uint64_t states = AccessibleWrap::NativeState();
if (mContent->AsElement()->State().HasState(NS_EVENT_STATE_READWRITE)) {
states |= states::EDITABLE;
} else if (mContent->IsHTMLElement(nsGkAtoms::article)) {
// We want <article> to behave like a document in terms of readonly state.
states |= states::READONLY;
}
nsIFrame* frame = GetFrame();
if ((states & states::EDITABLE) || (frame && frame->IsSelectable(nullptr))) {
// If the accessible is editable the layout selectable state only disables
// mouse selection, but keyboard (shift+arrow) selection is still possible.
states |= states::SELECTABLE_TEXT;
}
return states;
}
nsIntRect HyperTextAccessible::GetBoundsInFrame(nsIFrame* aFrame,
uint32_t aStartRenderedOffset,
uint32_t aEndRenderedOffset) {
nsPresContext* presContext = mDoc->PresContext();
if (!aFrame->IsTextFrame()) {
return aFrame->GetScreenRectInAppUnits().ToNearestPixels(
presContext->AppUnitsPerDevPixel());
}
// Substring must be entirely within the same text node.
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());
// 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.SetRectX(
frameScreenRect.X() +
std::min(frameTextStartPoint.x, frameTextEndPoint.x),
mozilla::Abs(frameTextStartPoint.x - frameTextEndPoint.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());
}
void HyperTextAccessible::TextSubstring(int32_t aStartOffset,
int32_t aEndOffset, nsAString& aText) {
aText.Truncate();
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
startOffset > endOffset || endOffset > CharacterCount()) {
NS_ERROR("Wrong in offset");
return;
}
int32_t startChildIdx = GetChildIndexAtOffset(startOffset);
if (startChildIdx == -1) return;
int32_t endChildIdx = GetChildIndexAtOffset(endOffset);
if (endChildIdx == -1) return;
if (startChildIdx == endChildIdx) {
int32_t childOffset = GetChildOffset(startChildIdx);
if (childOffset == -1) return;
Accessible* child = GetChildAt(startChildIdx);
child->AppendTextTo(aText, startOffset - childOffset,
endOffset - startOffset);
return;
}
int32_t startChildOffset = GetChildOffset(startChildIdx);
if (startChildOffset == -1) return;
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);
if (endChildOffset == -1) return;
Accessible* endChild = GetChildAt(endChildIdx);
endChild->AppendTextTo(aText, 0, endOffset - endChildOffset);
}
uint32_t HyperTextAccessible::DOMPointToOffset(nsINode* aNode,
int32_t aNodeOffset,
bool aIsEndOffset) const {
if (!aNode) return 0;
uint32_t offset = 0;
nsINode* findNode = nullptr;
if (aNodeOffset == -1) {
findNode = aNode;
} else if (aNode->IsText()) {
// 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, 0);
nsresult rv = ContentToRenderedOffset(frame, aNodeOffset, &offset);
NS_ENSURE_SUCCESS(rv, 0);
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_Deprecated(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.
return 0;
}
// Case #2: there are no children, we're at this node.
findNode = aNode;
} else if (aNodeOffset == static_cast<int32_t>(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* descendant = nullptr;
if (findNode) {
dom::HTMLBRElement* brElement = dom::HTMLBRElement::FromNode(findNode);
if (brElement && brElement->IsPaddingForEmptyEditor()) {
// This <br> is the hacky "padding <br> element" used when there is no
// text in the editor.
return 0;
}
descendant = mDoc->GetAccessible(findNode);
if (!descendant && findNode->IsContent()) {
Accessible* container = mDoc->GetContainerAccessible(findNode);
if (container) {
TreeWalker walker(container, findNode->AsContent(),
TreeWalker::eWalkContextTree);
descendant = walker.Next();
if (!descendant) descendant = container;
}
}
}
return TransformOffset(descendant, offset, aIsEndOffset);
}
uint32_t HyperTextAccessible::TransformOffset(Accessible* aDescendant,
uint32_t aOffset,
bool aIsEndOffset) const {
// From the descendant, go up and get the immediate child of this hypertext.
uint32_t offset = aOffset;
Accessible* descendant = aDescendant;
while (descendant) {
Accessible* parent = descendant->Parent();
if (parent == this) return GetChildOffset(descendant) + offset;
// 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>. Thus we need to adjust the offset
// to make it relative the hypertext.
// If the end offset is not supposed to be inclusive and the original point
// is not at 0 offset then the returned offset should be after an embedded
// character the original point belongs to.
if (aIsEndOffset) {
// Similar to our special casing in FindOffset, we add handling for
// bulleted lists here because PeekOffset returns the inner text node
// for a list when it should return the list bullet.
// We manually set the offset so the error doesn't propagate up.
if (offset == 0 && parent && parent->IsHTMLListItem() &&
descendant->PrevSibling() && descendant->PrevSibling()->GetFrame() &&
descendant->PrevSibling()->GetFrame()->IsBulletFrame()) {
offset = 0;
} else {
offset = (offset > 0 || descendant->IndexInParent() > 0) ? 1 : 0;
}
} else {
offset = 0;
}
descendant = parent;
}
// If the given a11y point cannot be mapped into offset relative this
// hypertext offset then return length as fallback value.
return CharacterCount();
}
DOMPoint HyperTextAccessible::OffsetToDOMPoint(int32_t aOffset) const {
// 0 offset is valid even if no children. In this case the associated editor
// is empty so return a DOM point for editor root element.
if (aOffset == 0) {
RefPtr<TextEditor> textEditor = GetEditor();
if (textEditor) {
if (textEditor->IsEmpty()) {
return DOMPoint(textEditor->GetRoot(), 0);
}
}
}
int32_t childIdx = GetChildIndexAtOffset(aOffset);
if (childIdx == -1) return DOMPoint();
Accessible* child = GetChildAt(childIdx);
int32_t innerOffset = aOffset - GetChildOffset(childIdx);
// A text leaf case.
if (child->IsTextLeaf()) {
// The point is inside the text node. This is always true for any text leaf
// except a last child one. See assertion below.
if (aOffset < GetChildOffset(childIdx + 1)) {
nsIContent* content = child->GetContent();
int32_t idx = 0;
if (NS_FAILED(RenderedToContentOffset(content->GetPrimaryFrame(),
innerOffset, &idx)))
return DOMPoint();
return DOMPoint(content, idx);
}
// Set the DOM point right after the text node.
MOZ_ASSERT(static_cast<uint32_t>(aOffset) == CharacterCount());
innerOffset = 1;
}
// Case of embedded object. The point is either before or after the element.
NS_ASSERTION(innerOffset == 0 || innerOffset == 1, "A wrong inner offset!");
nsINode* node = child->GetNode();
nsINode* parentNode = node->GetParentNode();
return parentNode ? DOMPoint(parentNode,
parentNode->ComputeIndexOf(node) + innerOffset)
: DOMPoint();
}
uint32_t HyperTextAccessible::FindOffset(uint32_t aOffset,
nsDirection aDirection,
nsSelectionAmount aAmount,
EWordMovementType aWordMovementType) {
NS_ASSERTION(aDirection == eDirPrevious || aAmount != eSelectBeginLine,
"eSelectBeginLine should only be used with eDirPrevious");
// Find a leaf accessible frame to start with. PeekOffset wants this.
HyperTextAccessible* text = this;
Accessible* child = nullptr;
int32_t innerOffset = aOffset;
do {
int32_t childIdx = text->GetChildIndexAtOffset(innerOffset);
// We can have an empty text leaf as our only child. Since empty text
// leaves are not accessible we then have no children, but 0 is a valid
// innerOffset.
if (childIdx == -1) {
NS_ASSERTION(innerOffset == 0 && !text->ChildCount(), "No childIdx?");
return DOMPointToOffset(text->GetNode(), 0, aDirection == eDirNext);
}
child = text->GetChildAt(childIdx);
// HTML list items may need special processing because PeekOffset doesn't
// work with list bullets.
if (text->IsHTMLListItem()) {
HTMLLIAccessible* li = text->AsHTMLListItem();
if (child == li->Bullet()) {
// XXX: the logic is broken for multichar bullets in moving by
// char/cluster/word cases.
if (text != this) {
return aDirection == eDirPrevious ? TransformOffset(text, 0, false)
: TransformOffset(text, 1, true);
}
if (aDirection == eDirPrevious) return 0;
uint32_t nextOffset = GetChildOffset(1);
if (nextOffset == 0) return 0;
switch (aAmount) {
case eSelectLine:
case eSelectEndLine:
// Ask a text leaf next (if not empty) to the bullet for an offset
// since list item may be multiline.
return nextOffset < CharacterCount()
? FindOffset(nextOffset, aDirection, aAmount,
aWordMovementType)
: nextOffset;
default:
return nextOffset;
}
}
}
innerOffset -= text->GetChildOffset(childIdx);
text = child->AsHyperText();
} while (text);
nsIFrame* childFrame = child->GetFrame();
if (!childFrame) {
NS_ERROR("No child frame");
return 0;
}
int32_t innerContentOffset = innerOffset;
if (child->IsTextLeaf()) {
NS_ASSERTION(childFrame->IsTextFrame(), "Wrong frame!");
RenderedToContentOffset(childFrame, innerOffset, &innerContentOffset);
}
nsIFrame* frameAtOffset = childFrame;
int32_t unusedOffsetInFrame = 0;
childFrame->GetChildFrameContainingOffset(
innerContentOffset, true, &unusedOffsetInFrame, &frameAtOffset);
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
nsPeekOffsetStruct pos(
aAmount, aDirection, innerContentOffset, nsPoint(0, 0), kIsJumpLinesOk,
kIsScrollViewAStop, kIsKeyboardSelect, kIsVisualBidi, false,
nsPeekOffsetStruct::ForceEditableRegion::No, aWordMovementType, false);
nsresult rv = frameAtOffset->PeekOffset(&pos);
// PeekOffset fails on last/first lines of the text in certain cases.
bool fallBackToSelectEndLine = false;
if (NS_FAILED(rv) && aAmount == eSelectLine) {
fallBackToSelectEndLine = aDirection == eDirNext;
pos.mAmount = fallBackToSelectEndLine ? eSelectEndLine : eSelectBeginLine;
frameAtOffset->PeekOffset(&pos);
}
if (!pos.mResultContent) {
NS_ERROR("No result content!");
return 0;
}
// Turn the resulting DOM point into an offset.
uint32_t hyperTextOffset = DOMPointToOffset(
pos.mResultContent, pos.mContentOffset, aDirection == eDirNext);
if (fallBackToSelectEndLine && IsLineEndCharAt(hyperTextOffset)) {
// We used eSelectEndLine, but the caller requested eSelectLine.
// If there's a '\n' at the end of the line, eSelectEndLine will stop on
// it rather than after it. This is not what we want, since the caller
// wants the next line, not the same line.
++hyperTextOffset;
}
if (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
if (hyperTextOffset == CharacterCount()) return 0;
// PeekOffset stops right before bullet so return 0 to workaround it.
if (IsHTMLListItem() && aAmount == eSelectBeginLine &&
hyperTextOffset > 0) {
Accessible* prevOffsetChild = GetChildAtOffset(hyperTextOffset - 1);
if (prevOffsetChild == AsHTMLListItem()->Bullet()) return 0;
}
}
return hyperTextOffset;
}
uint32_t HyperTextAccessible::FindWordBoundary(
uint32_t aOffset, nsDirection aDirection,
EWordMovementType aWordMovementType) {
uint32_t orig =
FindOffset(aOffset, aDirection, eSelectWord, aWordMovementType);
if (aWordMovementType != eStartWord) {
return orig;
}
if (aDirection == eDirPrevious) {
// When layout.word_select.stop_at_punctuation is true (the default),
// for a word beginning with punctuation, layout treats the punctuation
// as the start of the word when moving next. However, when moving
// previous, layout stops *after* the punctuation. We want to be
// consistent regardless of movement direction and always treat punctuation
// as the start of a word.
if (!StaticPrefs::layout_word_select_stop_at_punctuation()) {
return orig;
}
// Case 1: Example: "a @"
// If aOffset is 2 or 3, orig will be 0, but it should be 2. That is,
// previous word moved back too far.
Accessible* child = GetChildAtOffset(orig);
if (child && child->IsHyperText()) {
// For a multi-word embedded object, previous word correctly goes back
// to the start of the word (the embedded object). Next word (below)
// incorrectly stops after the embedded object in this case, so return
// the already correct result.
// Example: "a x y b", where "x y" is an embedded link
// If aOffset is 4, orig will be 2, which is correct.
// If we get the next word (below), we'll end up returning 3 instead.
return orig;
}
uint32_t next = FindOffset(orig, eDirNext, eSelectWord, eStartWord);
if (next < aOffset) {
// Next word stopped on punctuation.
return next;
}
// case 2: example: "a @@b"
// If aOffset is 2, 3 or 4, orig will be 4, but it should be 2. That is,
// previous word didn't go back far enough.
if (orig == 0) {
return orig;
}
// Walk backwards by offset, getting the next word.
// In the loop, o is unsigned, so o >= 0 will always be true and won't
// prevent us from decrementing at 0. Instead, we check that o doesn't
// wrap around.
for (uint32_t o = orig - 1; o < orig; --o) {
next = FindOffset(o, eDirNext, eSelectWord, eStartWord);
if (next == orig) {
// Next word and previous word were consistent. This
// punctuation problem isn't applicable here.
break;
}
if (next < orig) {
// Next word stopped on punctuation.
return next;
}
}
} else {
// When layout.word_select.stop_at_punctuation is true (the default),
// when positioned on punctuation in the middle of a word, next word skips
// the rest of the word. However, when positioned before the punctuation,
// next word moves just after the punctuation. We want to be consistent
// regardless of starting position and always stop just after the
// punctuation.
// Next word can move too far when positioned on white space too.
// Example: "a b@c"
// If aOffset is 3, orig will be 5, but it should be 4. That is, next word
// moved too far.
if (aOffset == 0) {
return orig;
}
uint32_t prev = FindOffset(orig, eDirPrevious, eSelectWord, eStartWord);
if (prev <= aOffset) {
// orig definitely isn't too far forward.
return orig;
}
// Walk backwards by offset, getting the next word.
// In the loop, o is unsigned, so o >= 0 will always be true and won't
// prevent us from decrementing at 0. Instead, we check that o doesn't
// wrap around.
for (uint32_t o = aOffset - 1; o < aOffset; --o) {
uint32_t next = FindOffset(o, eDirNext, eSelectWord, eStartWord);
if (next > aOffset && next < orig) {
return next;
}
if (next <= aOffset) {
break;
}
}
}
return orig;
}
uint32_t HyperTextAccessible::FindLineBoundary(
uint32_t aOffset, EWhichLineBoundary aWhichLineBoundary) {
// Note: empty last line doesn't have own frame (a previous line contains '\n'
// character instead) thus when it makes a difference we need to process this
// case separately (otherwise operations are performed on previous line).
switch (aWhichLineBoundary) {
case ePrevLineBegin: {
// Fetch a previous line and move to its start (as arrow up and home keys
// were pressed).
if (IsEmptyLastLineOffset(aOffset))
return FindOffset(aOffset, eDirPrevious, eSelectBeginLine);
uint32_t tmpOffset = FindOffset(aOffset, eDirPrevious, eSelectLine);
return FindOffset(tmpOffset, eDirPrevious, eSelectBeginLine);
}
case ePrevLineEnd: {
if (IsEmptyLastLineOffset(aOffset)) return aOffset - 1;
// If offset is at first line then return 0 (first line start).
uint32_t tmpOffset = FindOffset(aOffset, eDirPrevious, eSelectBeginLine);
if (tmpOffset == 0) return 0;
// Otherwise move to end of previous line (as arrow up and end keys were
// pressed).
tmpOffset = FindOffset(aOffset, eDirPrevious, eSelectLine);
return FindOffset(tmpOffset, eDirNext, eSelectEndLine);
}
case eThisLineBegin: {
if (IsEmptyLastLineOffset(aOffset)) return aOffset;
// Move to begin of the current line (as home key was pressed).
uint32_t thisLineBeginOffset =
FindOffset(aOffset, eDirPrevious, eSelectBeginLine);
if (IsCharAt(thisLineBeginOffset, kEmbeddedObjectChar)) {
// We landed on an embedded character, don't mess with possible embedded
// line breaks, and assume the offset is correct.
return thisLineBeginOffset;
}
// Sometimes, there is the possibility layout returned an
// offset smaller than it should. Sanity-check by moving to the end of the
// previous line and see if that has a greater offset.
uint32_t tmpOffset = FindOffset(aOffset, eDirPrevious, eSelectLine);
tmpOffset = FindOffset(tmpOffset, eDirNext, eSelectEndLine);
if (tmpOffset > thisLineBeginOffset && tmpOffset < aOffset) {
// We found a previous line offset. Return the next character after it
// as our start offset if it points to a line end char.
return IsLineEndCharAt(tmpOffset) ? tmpOffset + 1 : tmpOffset;
}
return thisLineBeginOffset;
}
case eThisLineEnd:
if (IsEmptyLastLineOffset(aOffset)) return aOffset;
// Move to end of the current line (as end key was pressed).
return FindOffset(aOffset, eDirNext, eSelectEndLine);
case eNextLineBegin: {
if (IsEmptyLastLineOffset(aOffset)) return aOffset;
// Move to begin of the next line if any (arrow down and home keys),
// otherwise end of the current line (arrow down only).
uint32_t tmpOffset = FindOffset(aOffset, eDirNext, eSelectLine);
uint32_t characterCount = CharacterCount();
if (tmpOffset == characterCount) {
return tmpOffset;
}
// Now, simulate the Home key on the next line to get its real offset.
uint32_t nextLineBeginOffset =
FindOffset(tmpOffset, eDirPrevious, eSelectBeginLine);
// Sometimes, there are line breaks inside embedded characters. If this
// is the case, the cursor is after the line break, but the offset will
// be that of the embedded character, which points to before the line
// break. We definitely want the line break included.
if (IsCharAt(nextLineBeginOffset, kEmbeddedObjectChar)) {
// We can determine if there is a line break by pressing End from
// the queried offset. If there is a line break, the offset will be 1
// greater, since this line ends with the embed. If there is not, the
// value will be different even if a line break follows right after the
// embed.
uint32_t thisLineEndOffset =
FindOffset(aOffset, eDirNext, eSelectEndLine);
if (thisLineEndOffset == nextLineBeginOffset + 1) {
// If we're querying the offset of the embedded character, we want
// the end offset of the parent line instead. Press End
// once more from the current position, which is after the embed.
if (nextLineBeginOffset == aOffset) {
uint32_t thisLineEndOffset2 =
FindOffset(thisLineEndOffset, eDirNext, eSelectEndLine);
// The above returns an offset exclusive the final line break, so we
// need to add 1 to it to return an inclusive end offset. Make sure
// we don't overshoot if we've started from another embedded
// character that has a line break, or landed on another embedded
// character, or if the result is the very end.
return (thisLineEndOffset2 == characterCount ||
(IsCharAt(thisLineEndOffset, kEmbeddedObjectChar) &&
thisLineEndOffset2 == thisLineEndOffset + 1) ||
IsCharAt(thisLineEndOffset2, kEmbeddedObjectChar))
? thisLineEndOffset2
: thisLineEndOffset2 + 1;
}
return thisLineEndOffset;
}
return nextLineBeginOffset;
}
// If the resulting offset is not greater than the offset we started from,
// layout could not find the offset for us. This can happen with certain
// inline-block elements.
if (nextLineBeginOffset <= aOffset) {
// Walk forward from the offset we started from up to tmpOffset,
// stopping after a line end character.
nextLineBeginOffset = aOffset;
while (nextLineBeginOffset < tmpOffset) {
if (IsLineEndCharAt(nextLineBeginOffset)) {
return nextLineBeginOffset + 1;
}
nextLineBeginOffset++;
}
}
return nextLineBeginOffset;
}
case eNextLineEnd: {
if (IsEmptyLastLineOffset(aOffset)) return aOffset;
// Move to next line end (as down arrow and end key were pressed).
uint32_t tmpOffset = FindOffset(aOffset, eDirNext, eSelectLine);
if (tmpOffset == CharacterCount()) return tmpOffset;
return FindOffset(tmpOffset, eDirNext, eSelectEndLine);
}
}
return 0;
}
int32_t HyperTextAccessible::FindParagraphStartOffset(uint32_t aOffset) {
// Because layout often gives us offsets that are incompatible with
// accessibility API requirements, for example when a paragraph contains
// presentational line breaks as found in Google Docs, use the accessibility
// tree to find the start offset instead.
Accessible* child = GetChildAtOffset(aOffset);
if (!child) {
return -1; // Invalid offset
}
// Use the pivot class to search for the start offset.
Pivot p = Pivot(this);
ParagraphBoundaryRule boundaryRule = ParagraphBoundaryRule(
child, child->IsTextLeaf() ? aOffset - GetChildOffset(child) : 0,
eDirPrevious);
AccessibleOrProxy wrappedChild = AccessibleOrProxy(child);
AccessibleOrProxy match = p.Prev(wrappedChild, boundaryRule, true);
if (match.IsNull() || match.AsAccessible() == this) {
// Found nothing, or pivot found the root of the search, startOffset is 0.
// This will include all relevant text nodes.
return 0;
}
if (match == wrappedChild) {
// We started out on a boundary.
if (match.Role() == roles::WHITESPACE) {
// We are on a line break boundary, so force pivot to find the previous
// boundary. What we want is any text before this, if any.
match = p.Prev(match, boundaryRule);
if (match.IsNull() || match.AsAccessible() == this) {
// Same as before, we landed on the root, so offset is definitely 0.
return 0;
}
} else if (!match.AsAccessible()->IsTextLeaf()) {
// The match is a block element, which is always a starting point, so
// just return its offset.
return TransformOffset(match.AsAccessible(), 0, false);
}
}
if (match.AsAccessible()->IsTextLeaf()) {
// ParagraphBoundaryRule only returns a text leaf if it contains a line
// break. We want to stop after that.
return TransformOffset(match.AsAccessible(),
boundaryRule.GetLastMatchTextOffset() + 1, false);
}
// This is a previous boundary, we don't want to include it itself.
// So, walk forward one accessible, excluding the descendants of this
// boundary if it is a block element. The below call to Next should always be
// initialized with a boundary.
SkipParagraphBoundaryRule goForwardOneRule = SkipParagraphBoundaryRule(match);
match = p.Next(match, goForwardOneRule);
// We already know that the search skipped over at least one accessible,
// so match can't be null. Get its transformed offset.
MOZ_ASSERT(!match.IsNull());
return TransformOffset(match.AsAccessible(), 0, false);
}
int32_t HyperTextAccessible::FindParagraphEndOffset(uint32_t aOffset) {
// Because layout often gives us offsets that are incompatible with
// accessibility API requirements, for example when a paragraph contains
// presentational line breaks as found in Google Docs, use the accessibility
// tree to find the end offset instead.
Accessible* child = GetChildAtOffset(aOffset);
if (!child) {
return -1; // invalid offset
}
// Use the pivot class to search for the end offset.
Pivot p = Pivot(this);
AccessibleOrProxy wrappedChild = AccessibleOrProxy(child);
ParagraphBoundaryRule boundaryRule = ParagraphBoundaryRule(
child, child->IsTextLeaf() ? aOffset - GetChildOffset(child) : 0,
eDirNext,
// In order to encompass all paragraphs inside embedded objects, not just
// the first, we want to skip the anchor's subtree.
/* aSkipAnchorSubtree */ true);
// Search forward for the end offset, including wrappedChild. We don't want
// to go beyond this point if this offset indicates a paragraph boundary.
AccessibleOrProxy match = p.Next(wrappedChild, boundaryRule, true);
if (!match.IsNull()) {
// Found something of relevance, adjust end offset.
Accessible* matchAcc = match.AsAccessible();
uint32_t matchOffset;
if (matchAcc->IsTextLeaf()) {
// ParagraphBoundaryRule only returns a text leaf if it contains a line
// break.
matchOffset = boundaryRule.GetLastMatchTextOffset() + 1;
} else if (matchAcc->Role() != roles::WHITESPACE && matchAcc != child) {
// We found a block boundary that wasn't our origin. We want to stop
// right on it, not after it, since we don't want to include the content
// of the block.
matchOffset = 0;
} else {
matchOffset = nsAccUtils::TextLength(matchAcc);
}
return TransformOffset(matchAcc, matchOffset, true);
}
// Didn't find anything, end offset is character count.
return CharacterCount();
}
void HyperTextAccessible::TextBeforeOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset,
nsAString& aText) {
*aStartOffset = *aEndOffset = 0;
aText.Truncate();
if (aBoundaryType == nsIAccessibleText::BOUNDARY_PARAGRAPH) {
// Not supported, bail out with empty text.
return;
}
index_t convertedOffset = ConvertMagicOffset(aOffset);
if (!convertedOffset.IsValid() || convertedOffset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return;
}
uint32_t adjustedOffset = convertedOffset;
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
adjustedOffset = AdjustCaretOffset(adjustedOffset);
switch (aBoundaryType) {
case nsIAccessibleText::BOUNDARY_CHAR:
if (convertedOffset != 0)
CharAt(convertedOffset - 1, aText, aStartOffset, aEndOffset);
break;
case nsIAccessibleText::BOUNDARY_WORD_START: {
// If the offset is a word start (except text length offset) then move
// backward to find a start offset (end offset is the given offset).
// Otherwise move backward twice to find both start and end offsets.
if (adjustedOffset == CharacterCount()) {
*aEndOffset =
FindWordBoundary(adjustedOffset, eDirPrevious, eStartWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eStartWord);
} else {
*aStartOffset =
FindWordBoundary(adjustedOffset, eDirPrevious, eStartWord);
*aEndOffset = FindWordBoundary(*aStartOffset, eDirNext, eStartWord);
if (*aEndOffset != static_cast<int32_t>(adjustedOffset)) {
*aEndOffset = *aStartOffset;
*aStartOffset =
FindWordBoundary(*aEndOffset, eDirPrevious, eStartWord);
}
}
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
}
case nsIAccessibleText::BOUNDARY_WORD_END: {
// Move word backward twice to find start and end offsets.
*aEndOffset = FindWordBoundary(convertedOffset, eDirPrevious, eEndWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eEndWord);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
}
case nsIAccessibleText::BOUNDARY_LINE_START:
*aStartOffset = FindLineBoundary(adjustedOffset, ePrevLineBegin);
*aEndOffset = FindLineBoundary(adjustedOffset, eThisLineBegin);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_LINE_END: {
*aEndOffset = FindLineBoundary(adjustedOffset, ePrevLineEnd);
int32_t tmpOffset = *aEndOffset;
// Adjust offset if line is wrapped.
if (*aEndOffset != 0 && !IsLineEndCharAt(*aEndOffset)) tmpOffset--;
*aStartOffset = FindLineBoundary(tmpOffset, ePrevLineEnd);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
}
}
}
void HyperTextAccessible::TextAtOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset, nsAString& aText) {
*aStartOffset = *aEndOffset = 0;
aText.Truncate();
uint32_t adjustedOffset = ConvertMagicOffset(aOffset);
if (adjustedOffset == std::numeric_limits<uint32_t>::max()) {
NS_ERROR("Wrong given offset!");
return;
}
switch (aBoundaryType) {
case nsIAccessibleText::BOUNDARY_CHAR:
// Return no char if caret is at the end of wrapped line (case of no line
// end character). Returning a next line char is confusing for AT.
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET &&
IsCaretAtEndOfLine())
*aStartOffset = *aEndOffset = adjustedOffset;
else
CharAt(adjustedOffset, aText, aStartOffset, aEndOffset);
break;
case nsIAccessibleText::BOUNDARY_WORD_START:
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
adjustedOffset = AdjustCaretOffset(adjustedOffset);
*aEndOffset = FindWordBoundary(adjustedOffset, eDirNext, eStartWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eStartWord);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::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(adjustedOffset, eDirNext, eEndWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eEndWord);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_LINE_START:
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
adjustedOffset = AdjustCaretOffset(adjustedOffset);
*aStartOffset = FindLineBoundary(adjustedOffset, eThisLineBegin);
*aEndOffset = FindLineBoundary(adjustedOffset, eNextLineBegin);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_LINE_END:
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
adjustedOffset = AdjustCaretOffset(adjustedOffset);
// In contrast to word end boundary we follow the spec here.
*aStartOffset = FindLineBoundary(adjustedOffset, ePrevLineEnd);
*aEndOffset = FindLineBoundary(adjustedOffset, eThisLineEnd);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_PARAGRAPH: {
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET) {
adjustedOffset = AdjustCaretOffset(adjustedOffset);
}
if (IsEmptyLastLineOffset(adjustedOffset)) {
// We are on the last line of a paragraph where there is no text.
// For example, in a textarea where a new line has just been inserted.
// In this case, return offsets for an empty line without text content.
*aStartOffset = *aEndOffset = adjustedOffset;
break;
}
*aStartOffset = FindParagraphStartOffset(adjustedOffset);
*aEndOffset = FindParagraphEndOffset(adjustedOffset);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
}
}
}
void HyperTextAccessible::TextAfterOffset(int32_t aOffset,
AccessibleTextBoundary aBoundaryType,
int32_t* aStartOffset,
int32_t* aEndOffset,
nsAString& aText) {
*aStartOffset = *aEndOffset = 0;
aText.Truncate();
if (aBoundaryType == nsIAccessibleText::BOUNDARY_PARAGRAPH) {
// Not supported, bail out with empty text.
return;
}
index_t convertedOffset = ConvertMagicOffset(aOffset);
if (!convertedOffset.IsValid() || convertedOffset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return;
}
uint32_t adjustedOffset = convertedOffset;
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
adjustedOffset = AdjustCaretOffset(adjustedOffset);
switch (aBoundaryType) {
case nsIAccessibleText::BOUNDARY_CHAR:
// If caret is at the end of wrapped line (case of no line end character)
// then char after the offset is a first char at next line.
if (adjustedOffset >= CharacterCount())
*aStartOffset = *aEndOffset = CharacterCount();
else
CharAt(adjustedOffset + 1, aText, aStartOffset, aEndOffset);
break;
case nsIAccessibleText::BOUNDARY_WORD_START:
// Move word forward twice to find start and end offsets.
*aStartOffset = FindWordBoundary(adjustedOffset, eDirNext, eStartWord);
*aEndOffset = FindWordBoundary(*aStartOffset, eDirNext, eStartWord);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_WORD_END:
// If the offset is a word end (except 0 offset) then move forward to find
// end offset (start offset is the given offset). Otherwise move forward
// twice to find both start and end offsets.
if (convertedOffset == 0) {
*aStartOffset = FindWordBoundary(convertedOffset, eDirNext, eEndWord);
*aEndOffset = FindWordBoundary(*aStartOffset, eDirNext, eEndWord);
} else {
*aEndOffset = FindWordBoundary(convertedOffset, eDirNext, eEndWord);
*aStartOffset = FindWordBoundary(*aEndOffset, eDirPrevious, eEndWord);
if (*aStartOffset != static_cast<int32_t>(convertedOffset)) {
*aStartOffset = *aEndOffset;
*aEndOffset = FindWordBoundary(*aStartOffset, eDirNext, eEndWord);
}
}
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_LINE_START:
*aStartOffset = FindLineBoundary(adjustedOffset, eNextLineBegin);
*aEndOffset = FindLineBoundary(*aStartOffset, eNextLineBegin);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
case nsIAccessibleText::BOUNDARY_LINE_END:
*aStartOffset = FindLineBoundary(adjustedOffset, eThisLineEnd);
*aEndOffset = FindLineBoundary(adjustedOffset, eNextLineEnd);
TextSubstring(*aStartOffset, *aEndOffset, aText);
break;
}
}
already_AddRefed<nsIPersistentProperties> HyperTextAccessible::TextAttributes(
bool aIncludeDefAttrs, int32_t aOffset, int32_t* aStartOffset,
int32_t* aEndOffset) {
// 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.
*aStartOffset = *aEndOffset = 0;
index_t offset = ConvertMagicOffset(aOffset);
if (!offset.IsValid() || offset > CharacterCount()) {
NS_ERROR("Wrong in offset!");
return nullptr;
}
RefPtr<nsPersistentProperties> attributes = new nsPersistentProperties();
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(attributes);
}
return attributes.forget();
}
return nullptr;
}
int32_t accAtOffsetIdx = accAtOffset->IndexInParent();
uint32_t startOffset = GetChildOffset(accAtOffsetIdx);
uint32_t endOffset = GetChildOffset(accAtOffsetIdx + 1);
int32_t offsetInAcc = offset - startOffset;
TextAttrsMgr textAttrsMgr(this, aIncludeDefAttrs, accAtOffset,
accAtOffsetIdx);
textAttrsMgr.GetAttributes(attributes, &startOffset, &endOffset);
// Compute spelling attributes on text accessible only.
nsIFrame* offsetFrame = accAtOffset->GetFrame();
if (offsetFrame && offsetFrame->IsTextFrame()) {
int32_t nodeOffset = 0;
RenderedToContentOffset(offsetFrame, offsetInAcc, &nodeOffset);
// Set 'misspelled' text attribute.
GetSpellTextAttr(accAtOffset->GetNode(), nodeOffset, &startOffset,
&endOffset, attributes);
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return attributes.forget();
}
already_AddRefed<nsIPersistentProperties>
HyperTextAccessible::DefaultTextAttributes() {
RefPtr<nsPersistentProperties> attributes = new nsPersistentProperties();
TextAttrsMgr textAttrsMgr(this);
textAttrsMgr.GetAttributes(attributes);
return attributes.forget();
}
int32_t HyperTextAccessible::GetLevelInternal() {
if (auto* heading = dom::HTMLHeadingElement::FromNode(mContent)) {
return heading->AccessibilityLevel();
}
return AccessibleWrap::GetLevelInternal();
}
void HyperTextAccessible::SetMathMLXMLRoles(
nsIPersistentProperties* aAttributes) {
// Add MathML xmlroles based on the position inside the parent.
Accessible* parent = Parent();
if (parent) {
switch (parent->Role()) {
case roles::MATHML_CELL:
case roles::MATHML_ENCLOSED:
case roles::MATHML_ERROR:
case roles::MATHML_MATH:
case roles::MATHML_ROW:
case roles::MATHML_SQUARE_ROOT:
case roles::MATHML_STYLE:
if (Role() == roles::MATHML_OPERATOR) {
// This is an operator inside an <mrow> (or an inferred <mrow>).
// See http://www.w3.org/TR/MathML3/chapter3.html#presm.inferredmrow
// XXX We should probably do something similar for MATHML_FENCED, but
// operators do not appear in the accessible tree. See bug 1175747.
nsIMathMLFrame* mathMLFrame = do_QueryFrame(GetFrame());
if (mathMLFrame) {
nsEmbellishData embellishData;
mathMLFrame->GetEmbellishData(embellishData);
if (NS_MATHML_EMBELLISH_IS_FENCE(embellishData.flags)) {
if (!PrevSibling()) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
nsGkAtoms::open_fence);
} else if (!NextSibling()) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
nsGkAtoms::close_fence);
}
}
if (NS_MATHML_EMBELLISH_IS_SEPARATOR(embellishData.flags)) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
nsGkAtoms::separator_);
}
}
}
break;
case roles::MATHML_FRACTION:
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::numerator
: nsGkAtoms::denominator);
break;
case roles::MATHML_ROOT:
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::base : nsGkAtoms::root_index);
break;
case roles::MATHML_SUB:
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::base : nsGkAtoms::subscript);
break;
case roles::MATHML_SUP:
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::base : nsGkAtoms::superscript);
break;
case roles::MATHML_SUB_SUP: {
int32_t index = IndexInParent();
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
index == 0
? nsGkAtoms::base
: (index == 1 ? nsGkAtoms::subscript : nsGkAtoms::superscript));
} break;
case roles::MATHML_UNDER:
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::base : nsGkAtoms::underscript);
break;
case roles::MATHML_OVER:
nsAccUtils::SetAccAttr(
aAttributes, nsGkAtoms::xmlroles,
IndexInParent() == 0 ? nsGkAtoms::base : nsGkAtoms::overscript);
break;
case roles::MATHML_UNDER_OVER: {
int32_t index = IndexInParent();
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
index == 0
? nsGkAtoms::base
: (index == 1 ? nsGkAtoms::underscript
: nsGkAtoms::overscript));
} break;
case roles::MATHML_MULTISCRIPTS: {
// Get the <multiscripts> base.
nsIContent* child;
bool baseFound = false;
for (child = parent->GetContent()->GetFirstChild(); child;
child = child->GetNextSibling()) {
if (child->IsMathMLElement()) {
baseFound = true;
break;
}
}
if (baseFound) {
nsIContent* content = GetContent();
if (child == content) {
// We are the base.
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
nsGkAtoms::base);
} else {
// Browse the list of scripts to find us and determine our type.
bool postscript = true;
bool subscript = true;
for (child = child->GetNextSibling(); child;
child = child->GetNextSibling()) {
if (!child->IsMathMLElement()) continue;
if (child->IsMathMLElement(nsGkAtoms::mprescripts_)) {
postscript = false;
subscript = true;
continue;
}
if (child == content) {
if (postscript) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
subscript ? nsGkAtoms::subscript
: nsGkAtoms::superscript);
} else {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::xmlroles,
subscript ? nsGkAtoms::presubscript
: nsGkAtoms::presuperscript);
}
break;
}
subscript = !subscript;
}
}
}
} break;
default:
break;
}
}
}
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->IsBlockFrame()) {
nsAutoString unused;
attributes->SetStringProperty("formatting"_ns, u"block"_ns, 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()) {
GetAccService()->MarkupAttributes(mContent, attributes);
if (mContent->IsMathMLElement()) SetMathMLXMLRoles(attributes);
}
return attributes.forget();
}
nsAtom* HyperTextAccessible::LandmarkRole() const {
if (!HasOwnContent()) return nullptr;
// For the html landmark elements we expose them like we do ARIA landmarks to
// make AT navigation schemes "just work".
if (mContent->IsHTMLElement(nsGkAtoms::nav)) {
return nsGkAtoms::navigation;
}
if (mContent->IsHTMLElement(nsGkAtoms::aside)) {
return nsGkAtoms::complementary;
}
if (mContent->IsHTMLElement(nsGkAtoms::main)) {
return nsGkAtoms::main;
}
return AccessibleWrap::LandmarkRole();
}
int32_t HyperTextAccessible::OffsetAtPoint(int32_t aX, int32_t aY,
uint32_t aCoordType) {
nsIFrame* hyperFrame = GetFrame();
if (!hyperFrame) return -1;
nsIntPoint coords =
nsAccUtils::ConvertToScreenCoords(aX, aY, aCoordType, this);
nsPresContext* presContext = mDoc->PresContext();
nsPoint coordsInAppUnits =
ToAppUnits(coords, presContext->AppUnitsPerDevPixel());
nsRect frameScreenRect = hyperFrame->GetScreenRectInAppUnits();
if (!frameScreenRect.Contains(coordsInAppUnits.x, coordsInAppUnits.y))
return -1; // Not found
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, -1);
nsIFrame* frame = primaryFrame;
while (frame) {
nsIContent* content = frame->GetContent();
NS_ENSURE_TRUE(content, -1);
nsPoint pointInFrame = pointInHyperText - frame->GetOffsetTo(hyperFrame);
nsSize frameSize = frame->GetSize();
if (pointInFrame.x < frameSize.width &&
pointInFrame.y < frameSize.height) {
// Finished
if (frame->IsTextFrame()) {
nsIFrame::ContentOffsets contentOffsets =
frame->GetContentOffsetsFromPointExternal(
pointInFrame, nsIFrame::IGNORE_SELECTION_STYLE);
if (contentOffsets.IsNull() || contentOffsets.content != content) {
return -1; // Not found
}
uint32_t addToOffset;
nsresult rv = ContentToRenderedOffset(
primaryFrame, contentOffsets.offset, &addToOffset);
NS_ENSURE_SUCCESS(rv, -1);
offset += addToOffset;
}
return offset;
}
frame = frame->GetNextContinuation();
}
offset += nsAccUtils::TextLength(childAcc);
}
return -1; // Not found
}
nsIntRect HyperTextAccessible::TextBounds(int32_t aStartOffset,
int32_t aEndOffset,
uint32_t aCoordType) {
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
startOffset > endOffset || endOffset > CharacterCount()) {
NS_ERROR("Wrong in offset");
return nsIntRect();
}
if (CharacterCount() == 0) {
nsPresContext* presContext = mDoc->PresContext();
// Empty content, use our own bound to at least get x,y coordinates
nsIFrame* frame = GetFrame();
if (!frame) {
return nsIntRect();
}
return frame->GetScreenRectInAppUnits().ToNearestPixels(
presContext->AppUnitsPerDevPixel());
}
int32_t childIdx = GetChildIndexAtOffset(startOffset);
if (childIdx == -1) return nsIntRect();
nsIntRect bounds;
int32_t prevOffset = GetChildOffset(childIdx);
int32_t offset1 = startOffset - prevOffset;
while (childIdx < static_cast<int32_t>(ChildCount())) {
nsIFrame* frame = GetChildAt(childIdx++)->GetFrame();
if (!frame) {
MOZ_ASSERT_UNREACHABLE("No frame for a child!");
continue;
}
int32_t nextOffset = GetChildOffset(childIdx);
if (nextOffset >= static_cast<int32_t>(endOffset)) {
bounds.UnionRect(
bounds, GetBoundsInFrame(frame, offset1, endOffset - prevOffset));
break;
}
bounds.UnionRect(bounds,
GetBoundsInFrame(frame, offset1, nextOffset - prevOffset));
prevOffset = nextOffset;
offset1 = 0;
}
// This document may have a resolution set, we will need to multiply
// the document-relative coordinates by that value and re-apply the doc's
// screen coordinates.
nsPresContext* presContext = mDoc->PresContext();
nsIFrame* rootFrame = presContext->PresShell()->GetRootFrame();
nsIntRect orgRectPixels =
rootFrame->GetScreenRectInAppUnits().ToNearestPixels(
presContext->AppUnitsPerDevPixel());
bounds.MoveBy(-orgRectPixels.X(), -orgRectPixels.Y());
bounds.ScaleRoundOut(presContext->PresShell()->GetResolution());
bounds.MoveBy(orgRectPixels.X(), orgRectPixels.Y());
auto boundsX = bounds.X();
auto boundsY = bounds.Y();
nsAccUtils::ConvertScreenCoordsTo(&boundsX, &boundsY, aCoordType, this);
bounds.MoveTo(boundsX, boundsY);
return bounds;
}
already_AddRefed<TextEditor> 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;
docShell->GetEditingSession(getter_AddRefs(editingSession));
if (!editingSession) return nullptr; // No editing session interface
dom::Document* docNode = mDoc->DocumentNode();
RefPtr<HTMLEditor> htmlEditor =
editingSession->GetHTMLEditorForWindow(docNode->GetWindow());
return htmlEditor.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.
RefPtr<TextEditor> textEditor = 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();
RefPtr<dom::Selection> domSel = DOMSelection();
NS_ENSURE_STATE(domSel);
// Set up the selection.
for (int32_t idx = domSel->RangeCount() - 1; idx > 0; idx--) {
RefPtr<nsRange> range{domSel->GetRangeAt(idx)};
domSel->RemoveRangeAndUnselectFramesAndNotifyListeners(*range,
IgnoreErrors());
}
SetSelectionBoundsAt(0, aStartPos, aEndPos);
// Make sure it is visible
domSel->ScrollIntoView(nsISelectionController::SELECTION_FOCUS_REGION,
ScrollAxis(), ScrollAxis(),
dom::Selection::SCROLL_FOR_CARET_MOVE |
dom::Selection::SCROLL_OVERFLOW_HIDDEN);
// 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);
dom::Document* docNode = mDoc->DocumentNode();
NS_ENSURE_TRUE(docNode, NS_ERROR_FAILURE);
nsCOMPtr<nsPIDOMWindowOuter> window = docNode->GetWindow();
RefPtr<dom::Element> result;
DOMFocusManager->MoveFocus(
window, nullptr, nsIFocusManager::MOVEFOCUS_CARET,
nsIFocusManager::FLAG_BYMOVEFOCUS, getter_AddRefs(result));
}
return NS_OK;
}
int32_t HyperTextAccessible::CaretOffset() const {
// Not focused focusable accessible except document accessible doesn't have
// a caret.
if (!IsDoc() && !FocusMgr()->IsFocused(this) &&
(InteractiveState() & states::FOCUSABLE)) {
return -1;
}
// Check cached value.
int32_t caretOffset = -1;
HyperTextAccessible* text = SelectionMgr()->AccessibleWithCaret(&caretOffset);
// Use cached value if it corresponds to this accessible.
if (caretOffset != -1) {
if (text == this) return caretOffset;
nsINode* textNode = text->GetNode();
// Ignore offset if cached accessible isn't a text leaf.
if (nsCoreUtils::IsAncestorOf(GetNode(), textNode))
return TransformOffset(text, textNode->IsText() ? caretOffset : 0, false);
}
// 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 -1;
// Turn the focus node and offset of the selection into caret hypretext
// offset.
dom::Selection* domSel = DOMSelection();
NS_ENSURE_TRUE(domSel, -1);
nsINode* focusNode = domSel->GetFocusNode();
uint32_t focusOffset = domSel->FocusOffset();
// 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 -1;
}
return DOMPointToOffset(focusNode, focusOffset);
}
int32_t HyperTextAccessible::CaretLineNumber() {
// Provide the line number for the caret, relative to the
// currently focused node. Use a 1-based index
RefPtr<nsFrameSelection> frameSelection = FrameSelection();
if (!frameSelection) return -1;
dom::Selection* domSel = frameSelection->GetSelection(SelectionType::eNormal);
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;
uint32_t caretOffset = domSel->FocusOffset();
CaretAssociationHint 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
}
nsContainerFrame* parentFrame = caretFrame->GetParent();
if (!parentFrame) break;
// Add lines for the sibling frames before the caret
nsIFrame* sibling = parentFrame->PrincipalChildList().FirstChild();
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;
}
MOZ_ASSERT_UNREACHABLE(
"DOM ancestry had this hypertext but frame ancestry didn't");
return lineNumber;
}
LayoutDeviceIntRect HyperTextAccessible::GetCaretRect(nsIWidget** aWidget) {
*aWidget = nullptr;
RefPtr<nsCaret> caret = mDoc->PresShellPtr()->GetCaret();
NS_ENSURE_TRUE(caret, LayoutDeviceIntRect());
bool isVisible = caret->IsVisible();
if (!isVisible) return LayoutDeviceIntRect();
nsRect rect;
nsIFrame* frame = caret->GetGeometry(&rect);
if (!frame || rect.IsEmpty()) return LayoutDeviceIntRect();
nsPoint offset;
// Offset from widget origin to the frame origin, which includes chrome
// on the widget.
*aWidget = frame->GetNearestWidget(offset);
NS_ENSURE_TRUE(*aWidget, LayoutDeviceIntRect());
rect.MoveBy(offset);
LayoutDeviceIntRect caretRect = LayoutDeviceIntRect::FromUnknownRect(
rect.ToOutsidePixels(frame->PresContext()->AppUnitsPerDevPixel()));
// clang-format off
// ((content screen origin) - (content offset in the widget)) = widget origin on the screen
// clang-format on
caretRect.MoveBy((*aWidget)->WidgetToScreenOffset() -
(*aWidget)->GetClientOffset());
// Correct for character size, so that caret always matches the size of
// the character. This is important for font size transitions, and is
// necessary because the Gecko caret uses the previous character's size as
// the user moves forward in the text by character.
nsIntRect charRect = CharBounds(
CaretOffset(), nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE);
if (!charRect.IsEmpty()) {
caretRect.SetTopEdge(charRect.Y());
}
return caretRect;
}
void HyperTextAccessible::GetSelectionDOMRanges(SelectionType aSelectionType,
nsTArray<nsRange*>* aRanges) {
// Ignore selection if it is not visible.
RefPtr<nsFrameSelection> frameSelection = FrameSelection();
if (!frameSelection || frameSelection->GetDisplaySelection() <=
nsISelectionController::SELECTION_HIDDEN)
return;
dom::Selection* domSel = frameSelection->GetSelection(aSelectionType);
if (!domSel) return;
nsINode* startNode = GetNode();
RefPtr<TextEditor> textEditor = GetEditor();
if (textEditor) {
startNode = textEditor->GetRoot();
}
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
aRanges->RemoveElementsBy(
[](const auto& range) { return range->Collapsed(); });
}
int32_t HyperTextAccessible::SelectionCount() {
nsTArray<nsRange*> ranges;
GetSelectionDOMRanges(SelectionType::eNormal, &ranges);
return ranges.Length();
}
bool HyperTextAccessible::SelectionBoundsAt(int32_t aSelectionNum,
int32_t* aStartOffset,
int32_t* aEndOffset) {
*aStartOffset = *aEndOffset = 0;
nsTArray<nsRange*> ranges;
GetSelectionDOMRanges(SelectionType::eNormal, &ranges);
uint32_t rangeCount = ranges.Length();
if (aSelectionNum < 0 || aSelectionNum >= static_cast<int32_t>(rangeCount))
return false;
nsRange* range = ranges[aSelectionNum];
// Get start and end points.
nsINode* startNode = range->GetStartContainer();
nsINode* endNode = range->GetEndContainer();
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.
const Maybe<int32_t> order =
nsContentUtils::ComparePoints(endNode, endOffset, startNode, startOffset);
if (!order) {
MOZ_ASSERT_UNREACHABLE();
return false;
}
if (*order < 0) {
nsINode* tempNode = startNode;
startNode = endNode;
endNode = tempNode;
int32_t tempOffset = startOffset;
startOffset = endOffset;
endOffset = tempOffset;
}
if (!startNode->IsInclusiveDescendantOf(mContent))
*aStartOffset = 0;
else
*aStartOffset = DOMPointToOffset(startNode, startOffset);
if (!endNode->IsInclusiveDescendantOf(mContent))
*aEndOffset = CharacterCount();
else
*aEndOffset = DOMPointToOffset(endNode, endOffset, true);
return true;
}
bool HyperTextAccessible::SetSelectionBoundsAt(int32_t aSelectionNum,
int32_t aStartOffset,
int32_t aEndOffset) {
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || !endOffset.IsValid() ||
std::max(startOffset, endOffset) > CharacterCount()) {
NS_ERROR("Wrong in offset");
return false;
}
TextRange range(this, this, startOffset, this, endOffset);
return range.SetSelectionAt(aSelectionNum);
}
bool HyperTextAccessible::RemoveFromSelection(int32_t aSelectionNum) {
RefPtr<dom::Selection> domSel = DOMSelection();
if (!domSel) return false;
if (aSelectionNum < 0 ||
aSelectionNum >= static_cast<int32_t>(domSel->RangeCount()))
return false;
const RefPtr<nsRange> range{domSel->GetRangeAt(aSelectionNum)};
domSel->RemoveRangeAndUnselectFramesAndNotifyListeners(*range,
IgnoreErrors());
return true;
}
void HyperTextAccessible::ScrollSubstringTo(int32_t aStartOffset,
int32_t aEndOffset,
uint32_t aScrollType) {
TextRange range(this, this, aStartOffset, this, aEndOffset);
range.ScrollIntoView(aScrollType);
}
void HyperTextAccessible::ScrollSubstringToPoint(int32_t aStartOffset,
int32_t aEndOffset,
uint32_t aCoordinateType,
int32_t aX, int32_t aY) {
nsIFrame* frame = GetFrame();
if (!frame) return;
nsIntPoint coords =
nsAccUtils::ConvertToScreenCoords(aX, aY, aCoordinateType, this);
RefPtr<nsRange> domRange = nsRange::Create(mContent);
TextRange range(this, this, aStartOffset, this, aEndOffset);
if (!range.AssignDOMRange(domRange)) {
return;
}
nsPresContext* presContext = frame->PresContext();
nsPoint coordsInAppUnits =
ToAppUnits(coords, 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;
nsresult rv = nsCoreUtils::ScrollSubstringTo(
frame, domRange, ScrollAxis(vPercent, WhenToScroll::Always),
ScrollAxis(hPercent, WhenToScroll::Always));
if (NS_FAILED(rv)) return;
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;
}
}
void HyperTextAccessible::EnclosingRange(a11y::TextRange& aRange) const {
if (IsTextField()) {
aRange.Set(mDoc, const_cast<HyperTextAccessible*>(this), 0,
const_cast<HyperTextAccessible*>(this), CharacterCount());
} else {
aRange.Set(mDoc, mDoc, 0, mDoc, mDoc->CharacterCount());
}
}
void HyperTextAccessible::SelectionRanges(
nsTArray<a11y::TextRange>* aRanges) const {
dom::Selection* sel = DOMSelection();
if (!sel) {
return;
}
TextRange::TextRangesFromSelection(sel, aRanges);
}
void HyperTextAccessible::VisibleRanges(
nsTArray<a11y::TextRange>* aRanges) const {}
void HyperTextAccessible::RangeByChild(Accessible* aChild,
a11y::TextRange& aRange) const {
HyperTextAccessible* ht = aChild->AsHyperText();
if (ht) {
aRange.Set(mDoc, ht, 0, ht, ht->CharacterCount());
return;
}
Accessible* child = aChild;
Accessible* parent = nullptr;
while ((parent = child->Parent()) && !(ht = parent->AsHyperText()))
child = parent;
// If no text then return collapsed text range, otherwise return a range
// containing the text enclosed by the given child.
if (ht) {
int32_t childIdx = child->IndexInParent();
int32_t startOffset = ht->GetChildOffset(childIdx);
int32_t endOffset =
child->IsTextLeaf() ? ht->GetChildOffset(childIdx + 1) : startOffset;
aRange.Set(mDoc, ht, startOffset, ht, endOffset);
}
}
void HyperTextAccessible::RangeAtPoint(int32_t aX, int32_t aY,
a11y::TextRange& aRange) const {
Accessible* child = mDoc->ChildAtPoint(aX, aY, eDeepestChild);
if (!child) return;
Accessible* parent = nullptr;
while ((parent = child->Parent()) && !parent->IsHyperText()) child = parent;
// Return collapsed text range for the point.
if (parent) {
HyperTextAccessible* ht = parent->AsHyperText();
int32_t offset = ht->GetChildOffset(child);
aRange.Set(mDoc, ht, offset, ht, offset);
}
}
////////////////////////////////////////////////////////////////////////////////
// Accessible public
// Accessible protected
ENameValueFlag HyperTextAccessible::NativeName(nsString& aName) const {
// Check @alt attribute for invalid img elements.
bool hasImgAlt = false;
if (mContent->IsHTMLElement(nsGkAtoms::img)) {
hasImgAlt = mContent->AsElement()->GetAttr(kNameSpaceID_None,
nsGkAtoms::alt, aName);
if (!aName.IsEmpty()) return eNameOK;
}
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->AsElement()->GetAttr(
kNameSpaceID_None, nsGkAtoms::title, aName))
aName.CompressWhitespace();
return hasImgAlt ? eNoNameOnPurpose : eNameOK;
}
void HyperTextAccessible::Shutdown() {
mOffsets.Clear();
AccessibleWrap::Shutdown();
}
bool HyperTextAccessible::RemoveChild(Accessible* aAccessible) {
const int32_t childIndex = aAccessible->IndexInParent();
if (childIndex < static_cast<int64_t>(mOffsets.Length())) {
mOffsets.RemoveLastElements(mOffsets.Length() -
aAccessible->IndexInParent());
}
return AccessibleWrap::RemoveChild(aAccessible);
}
bool HyperTextAccessible::InsertChildAt(uint32_t aIndex, Accessible* aChild) {
if (aIndex < mOffsets.Length()) {
mOffsets.RemoveLastElements(mOffsets.Length() - aIndex);
}
return AccessibleWrap::InsertChildAt(aIndex, aChild);
}
Relation HyperTextAccessible::RelationByType(RelationType aType) const {
Relation rel = Accessible::RelationByType(aType);
switch (aType) {
case RelationType::NODE_CHILD_OF:
if (HasOwnContent() && mContent->IsMathMLElement()) {
Accessible* parent = Parent();
if (parent) {
nsIContent* parentContent = parent->GetContent();
if (parentContent &&
parentContent->IsMathMLElement(nsGkAtoms::mroot_)) {
// Add a relation pointing to the parent <mroot>.
rel.AppendTarget(parent);
}
}
}
break;
case RelationType::NODE_PARENT_OF:
if (HasOwnContent() && mContent->IsMathMLElement(nsGkAtoms::mroot_)) {
Accessible* base = GetChildAt(0);
Accessible* index = GetChildAt(1);
if (base && index) {
// Append the <mroot> children in the order index, base.
rel.AppendTarget(index);
rel.AppendTarget(base);
}
}
break;
default:
break;
}
return rel;
}
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible public static
nsresult HyperTextAccessible::ContentToRenderedOffset(
nsIFrame* aFrame, int32_t aContentOffset, uint32_t* aRenderedOffset) const {
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->IsTextFrame(), "Need text frame for offset conversion");
NS_ASSERTION(aFrame->GetPrevContinuation() == nullptr,
"Call on primary frame only");
nsIFrame::RenderedText text =
aFrame->GetRenderedText(aContentOffset, aContentOffset + 1,
nsIFrame::TextOffsetType::OffsetsInContentText,
nsIFrame::TrailingWhitespace::DontTrim);
*aRenderedOffset = text.mOffsetWithinNodeRenderedText;
return NS_OK;
}
nsresult HyperTextAccessible::RenderedToContentOffset(
nsIFrame* aFrame, uint32_t aRenderedOffset, int32_t* aContentOffset) const {
if (IsTextField()) {
*aContentOffset = aRenderedOffset;
return NS_OK;
}
*aContentOffset = 0;
NS_ENSURE_TRUE(aFrame, NS_ERROR_FAILURE);
NS_ASSERTION(aFrame->IsTextFrame(), "Need text frame for offset conversion");
NS_ASSERTION(aFrame->GetPrevContinuation() == nullptr,
"Call on primary frame only");
nsIFrame::RenderedText text =
aFrame->GetRenderedText(aRenderedOffset, aRenderedOffset + 1,
nsIFrame::TextOffsetType::OffsetsInRenderedText,
nsIFrame::TrailingWhitespace::DontTrim);
*aContentOffset = text.mOffsetWithinNodeText;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// HyperTextAccessible public
int32_t HyperTextAccessible::GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter) const {
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) const {
uint32_t lastOffset = 0;
const uint32_t offsetCount = mOffsets.Length();
if (offsetCount > 0) {
lastOffset = mOffsets[offsetCount - 1];
if (aOffset < lastOffset) {
size_t index;
if (BinarySearch(mOffsets, 0, offsetCount, aOffset, &index)) {
return (index < (offsetCount - 1)) ? index + 1 : index;
}
return (index == offsetCount) ? -1 : index;
}
}
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->ComputeIndexOf(content) + 1;
aPoint->node = parent;
} else if (aFrame->IsTextFrame()) {
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->ComputeIndexOf(content);
aPoint->node = parent;
}
return NS_OK;
}
// HyperTextAccessible
void HyperTextAccessible::GetSpellTextAttr(
nsINode* aNode, int32_t aNodeOffset, uint32_t* aStartOffset,
uint32_t* aEndOffset, nsIPersistentProperties* aAttributes) {
RefPtr<nsFrameSelection> fs = FrameSelection();
if (!fs) return;
dom::Selection* domSel = fs->GetSelection(SelectionType::eSpellCheck);
if (!domSel) return;
int32_t rangeCount = domSel->RangeCount();
if (rangeCount <= 0) return;
uint32_t startOffset = 0, endOffset = 0;
for (int32_t idx = 0; idx < rangeCount; idx++) {
const 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->GetEndContainer();
int32_t endNodeOffset = range->EndOffset();
Maybe<int32_t> order = nsContentUtils::ComparePoints(
aNode, aNodeOffset, endNode, endNodeOffset);
if (NS_WARN_IF(!order)) {
continue;
}
if (*order >= 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->GetStartContainer();
int32_t startNodeOffset = range->StartOffset();
order = nsContentUtils::ComparePoints(startNode, startNodeOffset, aNode,
aNodeOffset);
if (!order) {
// As (`aNode`, `aNodeOffset`) is comparable to the end of the range, it
// should also be comparable to the range's start. Returning here
// prevents crashes in release builds.
MOZ_ASSERT_UNREACHABLE();
return;
}
if (*order <= 0) {
startOffset = DOMPointToOffset(startNode, startNodeOffset);
endOffset = DOMPointToOffset(endNode, endNodeOffset);
if (startOffset > *aStartOffset) *aStartOffset = startOffset;
if (endOffset < *aEndOffset) *aEndOffset = endOffset;
if (aAttributes) {
nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::invalid, u"spelling"_ns);
}
return;
}
// This range came after the point.
endOffset = DOMPointToOffset(startNode, startNodeOffset);
if (idx > 0) {
const nsRange* prevRange = domSel->GetRangeAt(idx - 1);
startOffset = DOMPointToOffset(prevRange->GetEndContainer(),
prevRange->EndOffset());
}
// The previous range might not be within this accessible. In that case,
// DOMPointToOffset returns length as a fallback. We don't want to use
// that offset if so, hence the startOffset < *aEndOffset check.
if (startOffset > *aStartOffset && startOffset < *aEndOffset)
*aStartOffset = startOffset;
if (endOffset < *aEndOffset) *aEndOffset = endOffset;
return;
}
// 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.
const nsRange* prevRange = domSel->GetRangeAt(rangeCount - 1);
startOffset =
DOMPointToOffset(prevRange->GetEndContainer(), prevRange->EndOffset());
// The previous range might not be within this accessible. In that case,
// DOMPointToOffset returns length as a fallback. We don't want to use
// that offset if so, hence the startOffset < *aEndOffset check.
if (startOffset > *aStartOffset && startOffset < *aEndOffset)
*aStartOffset = startOffset;
}
bool HyperTextAccessible::IsTextRole() {
const nsRoleMapEntry* roleMapEntry = ARIARoleMap();
if (roleMapEntry && (roleMapEntry->role == roles::GRAPHIC ||
roleMapEntry->role == roles::IMAGE_MAP ||
roleMapEntry->role == roles::SLIDER ||
roleMapEntry->role == roles::PROGRESSBAR ||
roleMapEntry->role == roles::SEPARATOR))
return false;
return true;
}
|