1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "HTMLEditUtils.h"
#include "AutoRangeArray.h" // for AutoRangeArray
#include "CSSEditUtils.h" // for CSSEditUtils
#include "EditAction.h" // for EditAction
#include "EditorBase.h" // for EditorBase, EditorType
#include "EditorDOMPoint.h" // for EditorDOMPoint, etc.
#include "EditorForwards.h" // for CollectChildrenOptions
#include "EditorUtils.h" // for EditorUtils
#include "HTMLEditHelpers.h" // for EditorInlineStyle
#include "WSRunObject.h" // for WSRunScanner
#include "mozilla/ArrayUtils.h" // for ArrayLength
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc.
#include "mozilla/Attributes.h"
#include "mozilla/StaticPrefs_editor.h" // for StaticPrefs::editor_
#include "mozilla/RangeUtils.h" // for RangeUtils
#include "mozilla/dom/DocumentInlines.h" // for GetBodyElement()
#include "mozilla/dom/Element.h" // for Element, nsINode
#include "mozilla/dom/HTMLAnchorElement.h"
#include "mozilla/dom/HTMLBodyElement.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/ServoCSSParser.h" // for ServoCSSParser
#include "mozilla/dom/StaticRange.h"
#include "mozilla/dom/Text.h" // for Text
#include "nsAString.h" // for nsAString::IsEmpty
#include "nsAtom.h" // for nsAtom
#include "nsAttrValue.h" // nsAttrValue
#include "nsCaseTreatment.h"
#include "nsCOMPtr.h" // for nsCOMPtr, operator==, etc.
#include "nsComputedDOMStyle.h" // for nsComputedDOMStyle
#include "nsDebug.h" // for NS_ASSERTION, etc.
#include "nsElementTable.h" // for nsHTMLElement
#include "nsError.h" // for NS_SUCCEEDED
#include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::a, etc.
#include "nsHTMLTags.h"
#include "nsIFrameInlines.h" // for nsIFrame::IsFlexOrGridItem()
#include "nsLiteralString.h" // for NS_LITERAL_STRING
#include "nsNameSpaceManager.h" // for kNameSpaceID_None
#include "nsPrintfCString.h" // nsPringfCString
#include "nsString.h" // for nsAutoString
#include "nsStyledElement.h"
#include "nsStyleStruct.h" // for StyleDisplay
#include "nsStyleUtil.h" // for nsStyleUtil
#include "nsTextFragment.h" // for nsTextFragment
#include "nsTextFrame.h" // for nsTextFrame
namespace mozilla {
using namespace dom;
using EditorType = EditorBase::EditorType;
template nsIContent* HTMLEditUtils::GetPreviousContent(
const EditorDOMPoint& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetPreviousContent(
const EditorRawDOMPoint& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetPreviousContent(
const EditorDOMPointInText& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetPreviousContent(
const EditorRawDOMPointInText& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetNextContent(
const EditorDOMPoint& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetNextContent(
const EditorRawDOMPoint& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetNextContent(
const EditorDOMPointInText& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template nsIContent* HTMLEditUtils::GetNextContent(
const EditorRawDOMPointInText& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck, const Element* aAncestorLimiter);
template EditorDOMPoint HTMLEditUtils::GetPreviousEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary);
template EditorRawDOMPoint HTMLEditUtils::GetPreviousEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary);
template EditorDOMPoint HTMLEditUtils::GetNextEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary);
template EditorRawDOMPoint HTMLEditUtils::GetNextEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary);
template nsIContent* HTMLEditUtils::GetContentToPreserveInlineStyles(
const EditorDOMPoint& aPoint, const Element& aEditingHost);
template nsIContent* HTMLEditUtils::GetContentToPreserveInlineStyles(
const EditorRawDOMPoint& aPoint, const Element& aEditingHost);
template EditorDOMPoint HTMLEditUtils::GetBetterInsertionPointFor(
const nsIContent& aContentToInsert, const EditorDOMPoint& aPointToInsert,
const Element& aEditingHost);
template EditorRawDOMPoint HTMLEditUtils::GetBetterInsertionPointFor(
const nsIContent& aContentToInsert, const EditorRawDOMPoint& aPointToInsert,
const Element& aEditingHost);
template EditorDOMPoint HTMLEditUtils::GetBetterInsertionPointFor(
const nsIContent& aContentToInsert, const EditorRawDOMPoint& aPointToInsert,
const Element& aEditingHost);
template EditorRawDOMPoint HTMLEditUtils::GetBetterInsertionPointFor(
const nsIContent& aContentToInsert, const EditorDOMPoint& aPointToInsert,
const Element& aEditingHost);
template EditorDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText(
const EditorDOMPoint& aPoint, const Element& aEditingHost);
template EditorDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText(
const EditorRawDOMPoint& aPoint, const Element& aEditingHost);
template EditorRawDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText(
const EditorDOMPoint& aPoint, const Element& aEditingHost);
template EditorRawDOMPoint HTMLEditUtils::GetBetterCaretPositionToInsertText(
const EditorRawDOMPoint& aPoint, const Element& aEditingHost);
template Result<EditorDOMPoint, nsresult>
HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
const Element& aElement, const EditorDOMPoint& aCurrentPoint);
template Result<EditorRawDOMPoint, nsresult>
HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
const Element& aElement, const EditorDOMPoint& aCurrentPoint);
template Result<EditorDOMPoint, nsresult>
HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
const Element& aElement, const EditorRawDOMPoint& aCurrentPoint);
template Result<EditorRawDOMPoint, nsresult>
HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
const Element& aElement, const EditorRawDOMPoint& aCurrentPoint);
template bool HTMLEditUtils::IsSameCSSColorValue(const nsAString& aColorA,
const nsAString& aColorB);
template bool HTMLEditUtils::IsSameCSSColorValue(const nsACString& aColorA,
const nsACString& aColorB);
bool HTMLEditUtils::CanContentsBeJoined(const nsIContent& aLeftContent,
const nsIContent& aRightContent) {
if (aLeftContent.NodeInfo()->NameAtom() !=
aRightContent.NodeInfo()->NameAtom()) {
return false;
}
if (!aLeftContent.IsElement()) {
return true; // can join text nodes, etc
}
MOZ_ASSERT(aRightContent.IsElement());
if (aLeftContent.NodeInfo()->NameAtom() == nsGkAtoms::font) {
const nsAttrValue* const leftSize =
aLeftContent.AsElement()->GetParsedAttr(nsGkAtoms::size);
const nsAttrValue* const rightSize =
aRightContent.AsElement()->GetParsedAttr(nsGkAtoms::size);
if (!leftSize ^ !rightSize || (leftSize && !leftSize->Equals(*rightSize))) {
return false;
}
const nsAttrValue* const leftColor =
aLeftContent.AsElement()->GetParsedAttr(nsGkAtoms::color);
const nsAttrValue* const rightColor =
aRightContent.AsElement()->GetParsedAttr(nsGkAtoms::color);
if (!leftColor ^ !rightColor ||
(leftColor && !leftColor->Equals(*rightColor))) {
return false;
}
const nsAttrValue* const leftFace =
aLeftContent.AsElement()->GetParsedAttr(nsGkAtoms::face);
const nsAttrValue* const rightFace =
aRightContent.AsElement()->GetParsedAttr(nsGkAtoms::face);
if (!leftFace ^ !rightFace || (leftFace && !leftFace->Equals(*rightFace))) {
return false;
}
}
nsStyledElement* leftStyledElement =
nsStyledElement::FromNode(const_cast<nsIContent*>(&aLeftContent));
if (!leftStyledElement) {
return false;
}
nsStyledElement* rightStyledElement =
nsStyledElement::FromNode(const_cast<nsIContent*>(&aRightContent));
if (!rightStyledElement) {
return false;
}
return CSSEditUtils::DoStyledElementsHaveSameStyle(*leftStyledElement,
*rightStyledElement);
}
static bool IsHTMLBlockElementByDefault(const nsIContent& aContent) {
if (!aContent.IsHTMLElement()) {
return false;
}
if (aContent.IsHTMLElement(nsGkAtoms::br)) { // shortcut for TextEditor
MOZ_ASSERT(!nsHTMLElement::IsBlock(
nsHTMLTags::CaseSensitiveAtomTagToId(nsGkAtoms::br)));
return false;
}
// We want to treat these as block nodes even though nsHTMLElement says
// they're not.
if (aContent.IsAnyOfHTMLElements(
nsGkAtoms::body, nsGkAtoms::head, nsGkAtoms::tbody, nsGkAtoms::thead,
nsGkAtoms::tfoot, nsGkAtoms::tr, nsGkAtoms::th, nsGkAtoms::td,
nsGkAtoms::dt, nsGkAtoms::dd)) {
return true;
}
return nsHTMLElement::IsBlock(
nsHTMLTags::CaseSensitiveAtomTagToId(aContent.NodeInfo()->NameAtom()));
}
bool HTMLEditUtils::IsBlockElement(const nsIContent& aContent,
BlockInlineCheck aBlockInlineCheck) {
MOZ_ASSERT(aBlockInlineCheck != BlockInlineCheck::Unused);
if (MOZ_UNLIKELY(!aContent.IsElement())) {
return false;
}
// If it's a <br>, we should always treat it as an inline element because
// its preceding collapse white-spaces and another <br> works same as usual
// even if you set its style to `display:block`.
if (aContent.IsHTMLElement(nsGkAtoms::br)) {
return false;
}
if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
aBlockInlineCheck == BlockInlineCheck::UseHTMLDefaultStyle) {
return IsHTMLBlockElementByDefault(aContent);
}
// Let's treat the document element and the body element is a block to avoid
// complicated things which may be detected by fuzzing.
if (aContent.OwnerDoc()->GetDocumentElement() == &aContent ||
(aContent.IsHTMLElement(nsGkAtoms::body) &&
aContent.OwnerDoc()->GetBodyElement() == &aContent)) {
return true;
}
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(aContent.AsElement());
if (MOZ_UNLIKELY(!elementStyle)) { // If aContent is not in the composed tree
return IsHTMLBlockElementByDefault(aContent);
}
const nsStyleDisplay* styleDisplay = elementStyle->StyleDisplay();
if (MOZ_UNLIKELY(styleDisplay->mDisplay == StyleDisplay::None)) {
// Typically, we should not keep handling editing in invisible nodes, but if
// we reach here, let's fallback to the default style for protecting the
// structure as far as possible.
return IsHTMLBlockElementByDefault(aContent);
}
// Both Blink and WebKit treat ruby style as a block, see IsEnclosingBlock()
// in Chromium or isBlock() in WebKit.
if (styleDisplay->IsRubyDisplayType()) {
return true;
}
// If the outside is not inline, treat it as block.
if (!styleDisplay->IsInlineOutsideStyle()) {
return true;
}
// If we're checking display-inside, inline-block, etc should be a block too.
return aBlockInlineCheck == BlockInlineCheck::UseComputedDisplayStyle &&
styleDisplay->DisplayInside() == StyleDisplayInside::FlowRoot &&
// Treat widgets as inline since they won't hide collapsible
// white-spaces around them.
styleDisplay->EffectiveAppearance() == StyleAppearance::None;
}
bool HTMLEditUtils::IsInlineContent(const nsIContent& aContent,
BlockInlineCheck aBlockInlineCheck) {
MOZ_ASSERT(aBlockInlineCheck != BlockInlineCheck::Unused);
if (!aContent.IsElement()) {
return true;
}
// If it's a <br>, we should always treat it as an inline element because
// its preceding collapse white-spaces and another <br> works same as usual
// even if you set its style to `display:block`.
if (aContent.IsHTMLElement(nsGkAtoms::br)) {
return true;
}
if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
aBlockInlineCheck == BlockInlineCheck::UseHTMLDefaultStyle) {
return !IsHTMLBlockElementByDefault(aContent);
}
// Let's treat the document element and the body element is a block to avoid
// complicated things which may be detected by fuzzing.
if (aContent.OwnerDoc()->GetDocumentElement() == &aContent ||
(aContent.IsHTMLElement(nsGkAtoms::body) &&
aContent.OwnerDoc()->GetBodyElement() == &aContent)) {
return false;
}
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(aContent.AsElement());
if (MOZ_UNLIKELY(!elementStyle)) { // If aContent is not in the composed tree
return !IsHTMLBlockElementByDefault(aContent);
}
const nsStyleDisplay* styleDisplay = elementStyle->StyleDisplay();
if (MOZ_UNLIKELY(styleDisplay->mDisplay == StyleDisplay::None)) {
// Similar to IsBlockElement, let's fallback to refer the default style.
// Note that if you change here, you may need to check the parent element
// style if aContent.
return !IsHTMLBlockElementByDefault(aContent);
}
// Different block IsBlockElement, when the display-outside is inline, it's
// simply an inline element.
return styleDisplay->IsInlineOutsideStyle() ||
styleDisplay->IsRubyDisplayType();
}
bool HTMLEditUtils::IsFlexOrGridItem(const Element& aElement) {
nsIFrame* frame = aElement.GetPrimaryFrame();
return frame && frame->IsFlexOrGridItem();
}
bool HTMLEditUtils::IsInclusiveAncestorCSSDisplayNone(
const nsIContent& aContent) {
if (NS_WARN_IF(!aContent.IsInComposedDoc())) {
return true;
}
for (const Element* element :
aContent.InclusiveFlatTreeAncestorsOfType<Element>()) {
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(element);
if (NS_WARN_IF(!elementStyle)) {
continue;
}
const nsStyleDisplay* styleDisplay = elementStyle->StyleDisplay();
if (MOZ_UNLIKELY(styleDisplay->mDisplay == StyleDisplay::None)) {
return true;
}
}
return false;
}
bool HTMLEditUtils::IsVisibleElementEvenIfLeafNode(const nsIContent& aContent) {
if (!aContent.IsElement()) {
return false;
}
// Assume non-HTML element is visible.
if (!aContent.IsHTMLElement()) {
return true;
}
// XXX Should we return false if the element is display:none?
if (HTMLEditUtils::IsBlockElement(
aContent, BlockInlineCheck::UseComputedDisplayStyle)) {
return true;
}
if (aContent.IsAnyOfHTMLElements(nsGkAtoms::applet, nsGkAtoms::iframe,
nsGkAtoms::img, nsGkAtoms::meter,
nsGkAtoms::progress, nsGkAtoms::select,
nsGkAtoms::textarea)) {
return true;
}
if (const HTMLInputElement* inputElement =
HTMLInputElement::FromNode(&aContent)) {
return inputElement->ControlType() != FormControlType::InputHidden;
}
// Maybe, empty inline element such as <span>.
return false;
}
/**
* IsInlineStyle() returns true if aNode is an inline style.
*/
bool HTMLEditUtils::IsInlineStyle(nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(
nsGkAtoms::b, nsGkAtoms::i, nsGkAtoms::u, nsGkAtoms::tt, nsGkAtoms::s,
nsGkAtoms::strike, nsGkAtoms::big, nsGkAtoms::small, nsGkAtoms::sub,
nsGkAtoms::sup, nsGkAtoms::font);
}
bool HTMLEditUtils::IsDisplayOutsideInline(const Element& aElement) {
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(&aElement);
if (!elementStyle) {
return false;
}
return elementStyle->StyleDisplay()->DisplayOutside() ==
StyleDisplayOutside::Inline;
}
bool HTMLEditUtils::IsDisplayInsideFlowRoot(const Element& aElement) {
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(&aElement);
if (!elementStyle) {
return false;
}
return elementStyle->StyleDisplay()->DisplayInside() ==
StyleDisplayInside::FlowRoot;
}
bool HTMLEditUtils::IsRemovableInlineStyleElement(Element& aElement) {
if (!aElement.IsHTMLElement()) {
return false;
}
// https://w3c.github.io/editing/execCommand.html#removeformat-candidate
if (aElement.IsAnyOfHTMLElements(
nsGkAtoms::abbr, // Chrome ignores, but does not make sense.
nsGkAtoms::acronym, nsGkAtoms::b,
nsGkAtoms::bdi, // Chrome ignores, but does not make sense.
nsGkAtoms::bdo, nsGkAtoms::big, nsGkAtoms::cite, nsGkAtoms::code,
// nsGkAtoms::del, Chrome ignores, but does not make sense but
// execCommand unofficial draft excludes this. Spec issue:
// https://github.com/w3c/editing/issues/192
nsGkAtoms::dfn, nsGkAtoms::em, nsGkAtoms::font, nsGkAtoms::i,
nsGkAtoms::ins, nsGkAtoms::kbd,
nsGkAtoms::mark, // Chrome ignores, but does not make sense.
nsGkAtoms::nobr, nsGkAtoms::q, nsGkAtoms::s, nsGkAtoms::samp,
nsGkAtoms::small, nsGkAtoms::span, nsGkAtoms::strike,
nsGkAtoms::strong, nsGkAtoms::sub, nsGkAtoms::sup, nsGkAtoms::tt,
nsGkAtoms::u, nsGkAtoms::var)) {
return true;
}
// If it's a <blink> element, we can remove it.
nsAutoString tagName;
aElement.GetTagName(tagName);
return tagName.LowerCaseEqualsASCII("blink");
}
/**
* IsNodeThatCanOutdent() returns true if aNode is a list, list item or
* blockquote.
*/
bool HTMLEditUtils::IsNodeThatCanOutdent(nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::ul, nsGkAtoms::ol, nsGkAtoms::dl,
nsGkAtoms::li, nsGkAtoms::dd, nsGkAtoms::dt,
nsGkAtoms::blockquote);
}
/**
* IsHeader() returns true if aNode is an html header.
*/
bool HTMLEditUtils::IsHeader(nsINode& aNode) {
return aNode.IsAnyOfHTMLElements(nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3,
nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6);
}
/**
* IsListItem() returns true if aNode is an html list item.
*/
bool HTMLEditUtils::IsListItem(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dd,
nsGkAtoms::dt);
}
/**
* IsAnyTableElement() returns true if aNode is an html table, td, tr, ...
*/
bool HTMLEditUtils::IsAnyTableElement(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(
nsGkAtoms::table, nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
nsGkAtoms::thead, nsGkAtoms::tfoot, nsGkAtoms::tbody, nsGkAtoms::caption);
}
/**
* IsAnyTableElementButNotTable() returns true if aNode is an html td, tr, ...
* (doesn't include table)
*/
bool HTMLEditUtils::IsAnyTableElementButNotTable(nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
nsGkAtoms::thead, nsGkAtoms::tfoot,
nsGkAtoms::tbody, nsGkAtoms::caption);
}
/**
* IsTable() returns true if aNode is an html table.
*/
bool HTMLEditUtils::IsTable(nsINode* aNode) {
return aNode && aNode->IsHTMLElement(nsGkAtoms::table);
}
/**
* IsTableRow() returns true if aNode is an html tr.
*/
bool HTMLEditUtils::IsTableRow(nsINode* aNode) {
return aNode && aNode->IsHTMLElement(nsGkAtoms::tr);
}
/**
* IsTableCell() returns true if aNode is an html td or th.
*/
bool HTMLEditUtils::IsTableCell(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th);
}
/**
* IsTableCellOrCaption() returns true if aNode is an html td or th or caption.
*/
bool HTMLEditUtils::IsTableCellOrCaption(nsINode& aNode) {
return aNode.IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th,
nsGkAtoms::caption);
}
/**
* IsAnyListElement() returns true if aNode is an html list.
*/
bool HTMLEditUtils::IsAnyListElement(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::ul, nsGkAtoms::ol,
nsGkAtoms::dl);
}
/**
* IsPre() returns true if aNode is an html pre node.
*/
bool HTMLEditUtils::IsPre(const nsINode* aNode) {
return aNode && aNode->IsHTMLElement(nsGkAtoms::pre);
}
/**
* IsImage() returns true if aNode is an html image node.
*/
bool HTMLEditUtils::IsImage(nsINode* aNode) {
return aNode && aNode->IsHTMLElement(nsGkAtoms::img);
}
bool HTMLEditUtils::IsLink(const nsINode* aNode) {
MOZ_ASSERT(aNode);
if (!aNode->IsContent()) {
return false;
}
RefPtr<const dom::HTMLAnchorElement> anchor =
dom::HTMLAnchorElement::FromNodeOrNull(aNode->AsContent());
if (!anchor) {
return false;
}
nsAutoCString tmpText;
anchor->GetHref(tmpText);
return !tmpText.IsEmpty();
}
bool HTMLEditUtils::IsNamedAnchor(const nsINode* aNode) {
MOZ_ASSERT(aNode);
if (!aNode->IsHTMLElement(nsGkAtoms::a)) {
return false;
}
nsAutoString text;
return aNode->AsElement()->GetAttr(nsGkAtoms::name, text) && !text.IsEmpty();
}
/**
* IsMozDiv() returns true if aNode is an html div node with |type = _moz|.
*/
bool HTMLEditUtils::IsMozDiv(nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsHTMLElement(nsGkAtoms::div) &&
aNode->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
u"_moz"_ns, eIgnoreCase);
}
/**
* IsMailCite() returns true if aNode is an html blockquote with |type=cite|.
*/
bool HTMLEditUtils::IsMailCite(const Element& aElement) {
// don't ask me why, but our html mailcites are id'd by "type=cite"...
if (aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, u"cite"_ns,
eIgnoreCase)) {
return true;
}
// ... but our plaintext mailcites by "_moz_quote=true". go figure.
if (aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::mozquote, u"true"_ns,
eIgnoreCase)) {
return true;
}
return false;
}
/**
* IsFormWidget() returns true if aNode is a form widget of some kind.
*/
bool HTMLEditUtils::IsFormWidget(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->IsAnyOfHTMLElements(nsGkAtoms::textarea, nsGkAtoms::select,
nsGkAtoms::button, nsGkAtoms::output,
nsGkAtoms::progress, nsGkAtoms::meter,
nsGkAtoms::input);
}
bool HTMLEditUtils::SupportsAlignAttr(nsINode& aNode) {
return aNode.IsAnyOfHTMLElements(
nsGkAtoms::hr, nsGkAtoms::table, nsGkAtoms::tbody, nsGkAtoms::tfoot,
nsGkAtoms::thead, nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
nsGkAtoms::div, nsGkAtoms::p, nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3,
nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6);
}
bool HTMLEditUtils::IsVisibleTextNode(const Text& aText) {
if (!aText.TextDataLength()) {
return false;
}
Maybe<uint32_t> visibleCharOffset =
HTMLEditUtils::GetInclusiveNextNonCollapsibleCharOffset(
EditorDOMPointInText(&aText, 0));
if (visibleCharOffset.isSome()) {
return true;
}
// Now, all characters in aText is collapsible white-spaces. The node is
// invisible if next to block boundary.
return !HTMLEditUtils::GetElementOfImmediateBlockBoundary(
aText, WalkTreeDirection::Forward) &&
!HTMLEditUtils::GetElementOfImmediateBlockBoundary(
aText, WalkTreeDirection::Backward);
}
bool HTMLEditUtils::IsInVisibleTextFrames(nsPresContext* aPresContext,
const Text& aText) {
// TODO(dholbert): aPresContext is now unused; maybe we can remove it, here
// and in IsEmptyNode? We do use it as a signal (implicitly here,
// more-explicitly in IsEmptyNode) that we are in a "SafeToAskLayout" case...
// If/when we remove it, we should be sure we're not losing that signal of
// strictness, since this function here does absolutely need to query layout.
MOZ_ASSERT(aPresContext);
if (!aText.TextDataLength()) {
return false;
}
nsTextFrame* textFrame = do_QueryFrame(aText.GetPrimaryFrame());
if (!textFrame) {
return false;
}
return textFrame->HasVisibleText();
}
Element* HTMLEditUtils::GetElementOfImmediateBlockBoundary(
const nsIContent& aContent, const WalkTreeDirection aDirection) {
MOZ_ASSERT(aContent.IsHTMLElement(nsGkAtoms::br) || aContent.IsText());
// First, we get a block container. This is not designed for reaching
// no block boundaries in the tree.
Element* maybeNonEditableAncestorBlock = HTMLEditUtils::GetAncestorElement(
aContent, HTMLEditUtils::ClosestBlockElement,
BlockInlineCheck::UseComputedDisplayStyle);
if (NS_WARN_IF(!maybeNonEditableAncestorBlock)) {
return nullptr;
}
auto getNextContent = [&aDirection, &maybeNonEditableAncestorBlock](
const nsIContent& aContent) -> nsIContent* {
return aDirection == WalkTreeDirection::Forward
? HTMLEditUtils::GetNextContent(
aContent,
{WalkTreeOption::IgnoreDataNodeExceptText,
WalkTreeOption::StopAtBlockBoundary},
BlockInlineCheck::UseComputedDisplayStyle,
maybeNonEditableAncestorBlock)
: HTMLEditUtils::GetPreviousContent(
aContent,
{WalkTreeOption::IgnoreDataNodeExceptText,
WalkTreeOption::StopAtBlockBoundary},
BlockInlineCheck::UseComputedDisplayStyle,
maybeNonEditableAncestorBlock);
};
// Then, scan block element boundary while we don't see visible things.
const bool isBRElement = aContent.IsHTMLElement(nsGkAtoms::br);
for (nsIContent* nextContent = getNextContent(aContent); nextContent;
nextContent = getNextContent(*nextContent)) {
if (nextContent->IsElement()) {
// Break is right before a child block, it's not visible
if (HTMLEditUtils::IsBlockElement(
*nextContent, BlockInlineCheck::UseComputedDisplayStyle)) {
return nextContent->AsElement();
}
// XXX How about other non-HTML elements? Assume they are styled as
// blocks for now.
if (!nextContent->IsHTMLElement()) {
return nextContent->AsElement();
}
// If there is a visible content which generates something visible,
// stop scanning.
if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*nextContent)) {
return nullptr;
}
if (nextContent->IsHTMLElement(nsGkAtoms::br)) {
// If aContent is a <br> element, another <br> element prevents the
// block boundary special handling.
if (isBRElement) {
return nullptr;
}
MOZ_ASSERT(aContent.IsText());
// Following <br> element always hides its following block boundary.
// I.e., white-spaces is at end of the text node is visible.
if (aDirection == WalkTreeDirection::Forward) {
return nullptr;
}
// Otherwise, if text node follows <br> element, its white-spaces at
// start of the text node are invisible. In this case, we return
// the found <br> element.
return nextContent->AsElement();
}
continue;
}
switch (nextContent->NodeType()) {
case nsINode::TEXT_NODE:
case nsINode::CDATA_SECTION_NODE:
break;
default:
continue;
}
Text* textNode = Text::FromNode(nextContent);
MOZ_ASSERT(textNode);
if (!textNode->TextLength()) {
continue; // empty invisible text node, keep scanning next one.
}
if (HTMLEditUtils::IsInclusiveAncestorCSSDisplayNone(*textNode)) {
continue; // Styled as invisible.
}
if (!textNode->TextIsOnlyWhitespace()) {
return nullptr; // found a visible text node.
}
const nsTextFragment& textFragment = textNode->TextFragment();
const bool isWhiteSpacePreformatted =
EditorUtils::IsWhiteSpacePreformatted(*textNode);
const bool isNewLinePreformatted =
EditorUtils::IsNewLinePreformatted(*textNode);
if (!isWhiteSpacePreformatted && !isNewLinePreformatted) {
// if the white-space only text node is not preformatted, ignore it.
continue;
}
for (uint32_t i = 0; i < textFragment.GetLength(); i++) {
if (textFragment.CharAt(i) == HTMLEditUtils::kNewLine) {
if (isNewLinePreformatted) {
return nullptr; // found a visible text node.
}
continue;
}
if (isWhiteSpacePreformatted) {
return nullptr; // found a visible text node.
}
}
// All white-spaces in the text node is invisible, keep scanning next one.
}
// There is no visible content and reached current block boundary. Then,
// the <br> element is the last content in the block and invisible.
// XXX Should we treat it visible if it's the only child of a block?
return maybeNonEditableAncestorBlock;
}
nsIContent* HTMLEditUtils::GetUnnecessaryLineBreakContent(
const Element& aBlockElement, ScanLineBreak aScanLineBreak) {
auto* lastLineBreakContent = [&]() -> nsIContent* {
const LeafNodeTypes leafNodeOrNonEditableNode{
LeafNodeType::LeafNodeOrNonEditableNode};
const WalkTreeOptions onlyPrecedingLine{
WalkTreeOption::StopAtBlockBoundary};
for (nsIContent* content =
aScanLineBreak == ScanLineBreak::AtEndOfBlock
? HTMLEditUtils::GetLastLeafContent(aBlockElement,
leafNodeOrNonEditableNode)
: HTMLEditUtils::GetPreviousContent(
aBlockElement, onlyPrecedingLine,
BlockInlineCheck::UseComputedDisplayStyle,
aBlockElement.GetParentElement());
content;
content =
aScanLineBreak == ScanLineBreak::AtEndOfBlock
? HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
*content, aBlockElement, leafNodeOrNonEditableNode,
BlockInlineCheck::UseComputedDisplayStyle)
: HTMLEditUtils::GetPreviousContent(
*content, onlyPrecedingLine,
BlockInlineCheck::UseComputedDisplayStyle,
aBlockElement.GetParentElement())) {
// If we're scanning preceding <br> element of aBlockElement, we don't
// need to look for a line break in another block because the caller
// needs to handle only preceding <br> element of aBlockElement.
if (aScanLineBreak == ScanLineBreak::BeforeBlock &&
HTMLEditUtils::IsBlockElement(
*content, BlockInlineCheck::UseComputedDisplayStyle)) {
return nullptr;
}
if (Text* textNode = Text::FromNode(content)) {
if (!textNode->TextLength()) {
continue; // ignore empty text node
}
const nsTextFragment& textFragment = textNode->TextFragment();
if (EditorUtils::IsNewLinePreformatted(*textNode) &&
textFragment.CharAt(textFragment.GetLength() - 1u) ==
HTMLEditUtils::kNewLine) {
// If the text node ends with a preserved line break, it's unnecessary
// unless it follows another preformatted line break.
if (textFragment.GetLength() == 1u) {
return textNode; // Need to scan previous leaf.
}
return textFragment.CharAt(textFragment.GetLength() - 2u) ==
HTMLEditUtils::kNewLine
? nullptr
: textNode;
}
if (HTMLEditUtils::IsVisibleTextNode(*textNode)) {
return nullptr;
}
continue;
}
if (content->IsCharacterData()) {
continue; // ignore hidden character data nodes like comment
}
if (content->IsHTMLElement(nsGkAtoms::br)) {
return content;
}
// If found element is empty block or visible element, there is no
// unnecessary line break.
if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*content)) {
return nullptr;
}
// Otherwise, e.g., empty <b>, we should keep scanning.
}
return nullptr;
}();
if (!lastLineBreakContent) {
return nullptr;
}
// If the found node is a text node and contains only one preformatted new
// line break, we need to keep scanning previous one, but if it has 2 or more
// characters, we know it has redundant line break.
Text* lastLineBreakText = Text::FromNode(lastLineBreakContent);
if (lastLineBreakText && lastLineBreakText->TextDataLength() != 1u) {
return lastLineBreakText;
}
// Scan previous leaf content, but now, we can stop at child block boundary.
const LeafNodeTypes leafNodeOrNonEditableNodeOrChildBlock{
LeafNodeType::LeafNodeOrNonEditableNode,
LeafNodeType::LeafNodeOrChildBlock};
const Element* blockElement = HTMLEditUtils::GetAncestorElement(
*lastLineBreakContent, HTMLEditUtils::ClosestBlockElement,
BlockInlineCheck::UseComputedDisplayStyle);
for (nsIContent* content =
HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
*lastLineBreakContent, *blockElement,
leafNodeOrNonEditableNodeOrChildBlock,
BlockInlineCheck::UseComputedDisplayStyle);
content;
content = HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
*content, *blockElement, leafNodeOrNonEditableNodeOrChildBlock,
BlockInlineCheck::UseComputedDisplayStyle)) {
if (HTMLEditUtils::IsBlockElement(
*content, BlockInlineCheck::UseComputedDisplayStyle) ||
(content->IsElement() && !content->IsHTMLElement())) {
// Now, must found <div>...<div>...</div><br></div>
// ^^^^
// In this case, the <br> element is necessary to make a following empty
// line of the inner <div> visible.
return nullptr;
}
if (Text* textNode = Text::FromNode(content)) {
if (!textNode->TextLength()) {
continue; // ignore empty text node
}
const nsTextFragment& textFragment = textNode->TextFragment();
if (EditorUtils::IsNewLinePreformatted(*textNode) &&
textFragment.CharAt(textFragment.GetLength() - 1u) ==
HTMLEditUtils::kNewLine) {
// So, we are here because the preformatted line break is followed by
// lastLineBreakContent which is <br> or a text node containing only
// one. In this case, even if their parents are different,
// lastLineBreakContent is necessary to make the last line visible.
return nullptr;
}
if (!HTMLEditUtils::IsVisibleTextNode(*textNode)) {
continue;
}
if (EditorUtils::IsWhiteSpacePreformatted(*textNode)) {
// If the white-space is preserved, neither following <br> nor a
// preformatted line break is not necessary.
return lastLineBreakContent;
}
// Otherwise, only if the last character is a collapsible white-space,
// we need lastLineBreakContent to make the trailing white-space visible.
switch (textFragment.CharAt(textFragment.GetLength() - 1u)) {
case HTMLEditUtils::kSpace:
case HTMLEditUtils::kCarriageReturn:
case HTMLEditUtils::kTab:
case HTMLEditUtils::kNBSP:
return nullptr;
default:
return lastLineBreakContent;
}
}
if (content->IsCharacterData()) {
continue; // ignore hidden character data nodes like comment
}
// If lastLineBreakContent follows a <br> element in same block, it's
// necessary to make the empty last line visible.
if (content->IsHTMLElement(nsGkAtoms::br)) {
return nullptr;
}
// If it follows a visible element, it's unnecessary line break.
if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*content)) {
return lastLineBreakContent;
}
// Otherwise, ignore empty inline elements such as <b>.
}
// If the block is empty except invisible data nodes and lastLineBreakContent,
// lastLineBreakContent is necessary to make the block visible.
return nullptr;
}
bool HTMLEditUtils::IsEmptyNode(nsPresContext* aPresContext,
const nsINode& aNode,
const EmptyCheckOptions& aOptions /* = {} */,
bool* aSeenBR /* = nullptr */) {
MOZ_ASSERT_IF(aOptions.contains(EmptyCheckOption::SafeToAskLayout),
aPresContext);
if (aSeenBR) {
*aSeenBR = false;
}
if (const Text* text = Text::FromNode(&aNode)) {
return aOptions.contains(EmptyCheckOption::SafeToAskLayout)
? !IsInVisibleTextFrames(aPresContext, *text)
: !IsVisibleTextNode(*text);
}
if (!aNode.IsElement()) {
return false;
}
if (
// If it's not a container such as an <hr> or <br>, etc, it should be
// treated as not empty.
!IsContainerNode(*aNode.AsContent()) ||
// If it's a named anchor, we shouldn't treat it as empty because it
// has special meaning even if invisible.
IsNamedAnchor(&aNode) ||
// Form widgets should be treated as not empty because they have special
// meaning even if invisible.
IsFormWidget(&aNode)) {
return false;
}
const auto [isListItem, isTableCell, hasAppearance] =
[&]() MOZ_NEVER_INLINE_DEBUG -> std::tuple<bool, bool, bool> {
if (!StaticPrefs::editor_block_inline_check_use_computed_style()) {
return {IsListItem(&aNode), IsTableCell(&aNode), false};
}
// Let's stop treating the document element and the <body> as a list item
// nor a table cell to avoid tricky cases.
if (aNode.OwnerDoc()->GetDocumentElement() == &aNode ||
(aNode.IsHTMLElement(nsGkAtoms::body) &&
aNode.OwnerDoc()->GetBodyElement() == &aNode)) {
return {false, false, false};
}
RefPtr<const ComputedStyle> elementStyle =
nsComputedDOMStyle::GetComputedStyleNoFlush(aNode.AsElement());
// If there is no style information like in a document fragment, let's refer
// the default style.
if (MOZ_UNLIKELY(!elementStyle)) {
return {IsListItem(&aNode), IsTableCell(&aNode), false};
}
const nsStyleDisplay* styleDisplay = elementStyle->StyleDisplay();
if (NS_WARN_IF(!styleDisplay)) {
return {IsListItem(&aNode), IsTableCell(&aNode), false};
}
if (styleDisplay->mDisplay != StyleDisplay::None &&
styleDisplay->HasAppearance()) {
return {false, false, true};
}
if (styleDisplay->IsListItem()) {
return {true, false, false};
}
if (styleDisplay->mDisplay == StyleDisplay::TableCell) {
return {false, true, false};
}
// The default display of <dt> and <dd> is block. Therefore, we need
// special handling for them.
return {styleDisplay->mDisplay == StyleDisplay::Block &&
aNode.IsAnyOfHTMLElements(nsGkAtoms::dd, nsGkAtoms::dt),
false, false};
}();
// The web author created native widget without form control elements. Let's
// treat it as visible.
if (hasAppearance) {
return false;
}
if (isListItem &&
aOptions.contains(EmptyCheckOption::TreatListItemAsVisible)) {
return false;
}
if (isTableCell &&
aOptions.contains(EmptyCheckOption::TreatTableCellAsVisible)) {
return false;
}
bool seenBR = aSeenBR && *aSeenBR;
for (nsIContent* childContent = aNode.GetFirstChild(); childContent;
childContent = childContent->GetNextSibling()) {
// Is the child editable and non-empty? if so, return false
if (aOptions.contains(
EmptyCheckOption::TreatNonEditableContentAsInvisible) &&
!EditorUtils::IsEditableContent(*childContent, EditorType::HTML)) {
continue;
}
if (Text* text = Text::FromNode(childContent)) {
// break out if we find we aren't empty
if (aOptions.contains(EmptyCheckOption::SafeToAskLayout)
? IsInVisibleTextFrames(aPresContext, *text)
: IsVisibleTextNode(*text)) {
return false;
}
continue;
}
MOZ_ASSERT(childContent != &aNode);
if (!aOptions.contains(EmptyCheckOption::TreatSingleBRElementAsVisible) &&
!seenBR && childContent->IsHTMLElement(nsGkAtoms::br)) {
// Ignore first <br> element in it if caller wants so because it's
// typically a padding <br> element of for a parent block.
seenBR = true;
if (aSeenBR) {
*aSeenBR = true;
}
continue;
}
// Note: list items or table cells are not considered empty
// if they contain other lists or tables
EmptyCheckOptions options(aOptions);
if (childContent->IsElement() && (isListItem || isTableCell)) {
options += {EmptyCheckOption::TreatListItemAsVisible,
EmptyCheckOption::TreatTableCellAsVisible};
}
if (!IsEmptyNode(aPresContext, *childContent, options, &seenBR)) {
if (aSeenBR) {
*aSeenBR = seenBR;
}
return false;
}
}
if (aSeenBR) {
*aSeenBR = seenBR;
}
return true;
}
bool HTMLEditUtils::ShouldInsertLinefeedCharacter(
const EditorDOMPoint& aPointToInsert, const Element& aEditingHost) {
MOZ_ASSERT(aPointToInsert.IsSetAndValid());
if (!aPointToInsert.IsInContentNode()) {
return false;
}
// closestEditableBlockElement can be nullptr if aEditingHost is an inline
// element.
Element* closestEditableBlockElement =
HTMLEditUtils::GetInclusiveAncestorElement(
*aPointToInsert.ContainerAs<nsIContent>(),
HTMLEditUtils::ClosestEditableBlockElement,
BlockInlineCheck::UseComputedDisplayOutsideStyle);
// If and only if the nearest block is the editing host or its parent,
// and new line character is preformatted, we should insert a linefeed.
return (!closestEditableBlockElement ||
closestEditableBlockElement == &aEditingHost) &&
EditorUtils::IsNewLinePreformatted(
*aPointToInsert.ContainerAs<nsIContent>());
}
// We use bitmasks to test containment of elements. Elements are marked to be
// in certain groups by setting the mGroup member of the `ElementInfo` struct
// to the corresponding GROUP_ values (OR'ed together). Similarly, elements are
// marked to allow containment of certain groups by setting the
// mCanContainGroups member of the `ElementInfo` struct to the corresponding
// GROUP_ values (OR'ed together).
// Testing containment then simply consists of checking whether the
// mCanContainGroups bitmask of an element and the mGroup bitmask of a
// potential child overlap.
#define GROUP_NONE 0
// body, head, html
#define GROUP_TOPLEVEL (1 << 1)
// base, link, meta, script, style, title
#define GROUP_HEAD_CONTENT (1 << 2)
// b, big, i, s, small, strike, tt, u
#define GROUP_FONTSTYLE (1 << 3)
// abbr, acronym, cite, code, datalist, del, dfn, em, ins, kbd, mark, rb, rp
// rt, rtc, ruby, samp, strong, var
#define GROUP_PHRASE (1 << 4)
// a, applet, basefont, bdi, bdo, br, font, iframe, img, map, meter, object,
// output, picture, progress, q, script, span, sub, sup
#define GROUP_SPECIAL (1 << 5)
// button, form, input, label, select, textarea
#define GROUP_FORMCONTROL (1 << 6)
// address, applet, article, aside, blockquote, button, center, del, details,
// dialog, dir, div, dl, fieldset, figure, footer, form, h1, h2, h3, h4, h5,
// h6, header, hgroup, hr, iframe, ins, main, map, menu, nav, noframes,
// noscript, object, ol, p, pre, table, search, section, summary, ul
#define GROUP_BLOCK (1 << 7)
// frame, frameset
#define GROUP_FRAME (1 << 8)
// col, tbody
#define GROUP_TABLE_CONTENT (1 << 9)
// tr
#define GROUP_TBODY_CONTENT (1 << 10)
// td, th
#define GROUP_TR_CONTENT (1 << 11)
// col
#define GROUP_COLGROUP_CONTENT (1 << 12)
// param
#define GROUP_OBJECT_CONTENT (1 << 13)
// li
#define GROUP_LI (1 << 14)
// area
#define GROUP_MAP_CONTENT (1 << 15)
// optgroup, option
#define GROUP_SELECT_CONTENT (1 << 16)
// option
#define GROUP_OPTIONS (1 << 17)
// dd, dt
#define GROUP_DL_CONTENT (1 << 18)
// p
#define GROUP_P (1 << 19)
// text, white-space, newline, comment
#define GROUP_LEAF (1 << 20)
// XXX This is because the editor does sublists illegally.
// ol, ul
#define GROUP_OL_UL (1 << 21)
// h1, h2, h3, h4, h5, h6
#define GROUP_HEADING (1 << 22)
// figcaption
#define GROUP_FIGCAPTION (1 << 23)
// picture members (img, source)
#define GROUP_PICTURE_CONTENT (1 << 24)
#define GROUP_INLINE_ELEMENT \
(GROUP_FONTSTYLE | GROUP_PHRASE | GROUP_SPECIAL | GROUP_FORMCONTROL | \
GROUP_LEAF)
#define GROUP_FLOW_ELEMENT (GROUP_INLINE_ELEMENT | GROUP_BLOCK)
struct ElementInfo final {
#ifdef DEBUG
nsHTMLTag mTag;
#endif
// See `GROUP_NONE`'s comment.
uint32_t mGroup;
// See `GROUP_NONE`'s comment.
uint32_t mCanContainGroups;
bool mIsContainer;
bool mCanContainSelf;
};
#ifdef DEBUG
# define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
{ \
eHTMLTag_##_tag, _group, _canContainGroups, _isContainer, \
_canContainSelf \
}
#else
# define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
{ _group, _canContainGroups, _isContainer, _canContainSelf }
#endif
static const ElementInfo kElements[eHTMLTag_userdefined] = {
ELEM(a, true, false, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(abbr, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(acronym, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(address, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT | GROUP_P),
// While applet is no longer a valid tag, removing it here breaks the editor
// (compiles, but causes many tests to fail in odd ways). This list is
// tracked against the main HTML Tag list, so any changes will require more
// than just removing entries.
ELEM(applet, true, true, GROUP_SPECIAL | GROUP_BLOCK,
GROUP_FLOW_ELEMENT | GROUP_OBJECT_CONTENT),
ELEM(area, false, false, GROUP_MAP_CONTENT, GROUP_NONE),
ELEM(article, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(aside, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(audio, false, false, GROUP_NONE, GROUP_NONE),
ELEM(b, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(base, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
ELEM(basefont, false, false, GROUP_SPECIAL, GROUP_NONE),
ELEM(bdi, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(bdo, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(bgsound, false, false, GROUP_NONE, GROUP_NONE),
ELEM(big, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(blockquote, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(body, true, true, GROUP_TOPLEVEL, GROUP_FLOW_ELEMENT),
ELEM(br, false, false, GROUP_SPECIAL, GROUP_NONE),
ELEM(button, true, true, GROUP_FORMCONTROL | GROUP_BLOCK,
GROUP_FLOW_ELEMENT),
ELEM(canvas, false, false, GROUP_NONE, GROUP_NONE),
ELEM(caption, true, true, GROUP_NONE, GROUP_INLINE_ELEMENT),
ELEM(center, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(cite, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(code, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(col, false, false, GROUP_TABLE_CONTENT | GROUP_COLGROUP_CONTENT,
GROUP_NONE),
ELEM(colgroup, true, false, GROUP_NONE, GROUP_COLGROUP_CONTENT),
ELEM(data, true, false, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(datalist, true, false, GROUP_PHRASE,
GROUP_OPTIONS | GROUP_INLINE_ELEMENT),
ELEM(dd, true, false, GROUP_DL_CONTENT, GROUP_FLOW_ELEMENT),
ELEM(del, true, true, GROUP_PHRASE | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(details, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(dfn, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(dialog, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(dir, true, false, GROUP_BLOCK, GROUP_LI),
ELEM(div, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(dl, true, false, GROUP_BLOCK, GROUP_DL_CONTENT),
ELEM(dt, true, true, GROUP_DL_CONTENT, GROUP_INLINE_ELEMENT),
ELEM(em, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(embed, false, false, GROUP_NONE, GROUP_NONE),
ELEM(fieldset, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(figcaption, true, false, GROUP_FIGCAPTION, GROUP_FLOW_ELEMENT),
ELEM(figure, true, true, GROUP_BLOCK,
GROUP_FLOW_ELEMENT | GROUP_FIGCAPTION),
ELEM(font, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(footer, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(form, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(frame, false, false, GROUP_FRAME, GROUP_NONE),
ELEM(frameset, true, true, GROUP_FRAME, GROUP_FRAME),
ELEM(h1, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(h2, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(h3, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(h4, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(h5, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(h6, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
ELEM(head, true, false, GROUP_TOPLEVEL, GROUP_HEAD_CONTENT),
ELEM(header, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(hgroup, true, false, GROUP_BLOCK, GROUP_HEADING),
ELEM(hr, false, false, GROUP_BLOCK, GROUP_NONE),
ELEM(html, true, false, GROUP_TOPLEVEL, GROUP_TOPLEVEL),
ELEM(i, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(iframe, true, true, GROUP_SPECIAL | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(image, false, false, GROUP_NONE, GROUP_NONE),
ELEM(img, false, false, GROUP_SPECIAL | GROUP_PICTURE_CONTENT, GROUP_NONE),
ELEM(input, false, false, GROUP_FORMCONTROL, GROUP_NONE),
ELEM(ins, true, true, GROUP_PHRASE | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(kbd, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(keygen, false, false, GROUP_NONE, GROUP_NONE),
ELEM(label, true, false, GROUP_FORMCONTROL, GROUP_INLINE_ELEMENT),
ELEM(legend, true, true, GROUP_NONE, GROUP_INLINE_ELEMENT),
ELEM(li, true, false, GROUP_LI, GROUP_FLOW_ELEMENT),
ELEM(link, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
ELEM(listing, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT),
ELEM(main, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(map, true, true, GROUP_SPECIAL, GROUP_BLOCK | GROUP_MAP_CONTENT),
ELEM(mark, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(marquee, true, false, GROUP_NONE, GROUP_NONE),
ELEM(menu, true, true, GROUP_BLOCK, GROUP_LI | GROUP_FLOW_ELEMENT),
ELEM(meta, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
ELEM(meter, true, false, GROUP_SPECIAL, GROUP_FLOW_ELEMENT),
ELEM(multicol, false, false, GROUP_NONE, GROUP_NONE),
ELEM(nav, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(nobr, true, false, GROUP_NONE, GROUP_NONE),
ELEM(noembed, false, false, GROUP_NONE, GROUP_NONE),
ELEM(noframes, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(noscript, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(object, true, true, GROUP_SPECIAL | GROUP_BLOCK,
GROUP_FLOW_ELEMENT | GROUP_OBJECT_CONTENT),
// XXX Can contain self and ul because editor does sublists illegally.
ELEM(ol, true, true, GROUP_BLOCK | GROUP_OL_UL, GROUP_LI | GROUP_OL_UL),
ELEM(optgroup, true, false, GROUP_SELECT_CONTENT, GROUP_OPTIONS),
ELEM(option, true, false, GROUP_SELECT_CONTENT | GROUP_OPTIONS, GROUP_LEAF),
ELEM(output, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(p, true, false, GROUP_BLOCK | GROUP_P, GROUP_INLINE_ELEMENT),
ELEM(param, false, false, GROUP_OBJECT_CONTENT, GROUP_NONE),
ELEM(picture, true, false, GROUP_SPECIAL, GROUP_PICTURE_CONTENT),
ELEM(plaintext, false, false, GROUP_NONE, GROUP_NONE),
ELEM(pre, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT),
ELEM(progress, true, false, GROUP_SPECIAL, GROUP_FLOW_ELEMENT),
ELEM(q, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(rb, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(rp, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(rt, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(rtc, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(ruby, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(s, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(samp, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(script, true, false, GROUP_HEAD_CONTENT | GROUP_SPECIAL, GROUP_LEAF),
ELEM(search, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(section, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(select, true, false, GROUP_FORMCONTROL, GROUP_SELECT_CONTENT),
ELEM(small, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(slot, true, false, GROUP_NONE, GROUP_FLOW_ELEMENT),
ELEM(source, false, false, GROUP_PICTURE_CONTENT, GROUP_NONE),
ELEM(span, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(strike, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(strong, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(style, true, false, GROUP_HEAD_CONTENT, GROUP_LEAF),
ELEM(sub, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(summary, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
ELEM(sup, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
ELEM(table, true, false, GROUP_BLOCK, GROUP_TABLE_CONTENT),
ELEM(tbody, true, false, GROUP_TABLE_CONTENT, GROUP_TBODY_CONTENT),
ELEM(td, true, false, GROUP_TR_CONTENT, GROUP_FLOW_ELEMENT),
ELEM(textarea, true, false, GROUP_FORMCONTROL, GROUP_LEAF),
ELEM(tfoot, true, false, GROUP_NONE, GROUP_TBODY_CONTENT),
ELEM(th, true, false, GROUP_TR_CONTENT, GROUP_FLOW_ELEMENT),
ELEM(thead, true, false, GROUP_NONE, GROUP_TBODY_CONTENT),
ELEM(template, false, false, GROUP_NONE, GROUP_NONE),
ELEM(time, true, false, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(title, true, false, GROUP_HEAD_CONTENT, GROUP_LEAF),
ELEM(tr, true, false, GROUP_TBODY_CONTENT, GROUP_TR_CONTENT),
ELEM(track, false, false, GROUP_NONE, GROUP_NONE),
ELEM(tt, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
ELEM(u, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
// XXX Can contain self and ol because editor does sublists illegally.
ELEM(ul, true, true, GROUP_BLOCK | GROUP_OL_UL, GROUP_LI | GROUP_OL_UL),
ELEM(var, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
ELEM(video, false, false, GROUP_NONE, GROUP_NONE),
ELEM(wbr, false, false, GROUP_NONE, GROUP_NONE),
ELEM(xmp, true, false, GROUP_BLOCK, GROUP_NONE),
// These aren't elements.
ELEM(text, false, false, GROUP_LEAF, GROUP_NONE),
ELEM(whitespace, false, false, GROUP_LEAF, GROUP_NONE),
ELEM(newline, false, false, GROUP_LEAF, GROUP_NONE),
ELEM(comment, false, false, GROUP_LEAF, GROUP_NONE),
ELEM(entity, false, false, GROUP_NONE, GROUP_NONE),
ELEM(doctypeDecl, false, false, GROUP_NONE, GROUP_NONE),
ELEM(markupDecl, false, false, GROUP_NONE, GROUP_NONE),
ELEM(instruction, false, false, GROUP_NONE, GROUP_NONE),
ELEM(userdefined, true, false, GROUP_NONE, GROUP_FLOW_ELEMENT)};
bool HTMLEditUtils::CanNodeContain(nsHTMLTag aParentTagId,
nsHTMLTag aChildTagId) {
NS_ASSERTION(
aParentTagId > eHTMLTag_unknown && aParentTagId <= eHTMLTag_userdefined,
"aParentTagId out of range!");
NS_ASSERTION(
aChildTagId > eHTMLTag_unknown && aChildTagId <= eHTMLTag_userdefined,
"aChildTagId out of range!");
#ifdef DEBUG
static bool checked = false;
if (!checked) {
checked = true;
int32_t i;
for (i = 1; i <= eHTMLTag_userdefined; ++i) {
NS_ASSERTION(kElements[i - 1].mTag == i,
"You need to update kElements (missing tags).");
}
}
#endif
// Special-case button.
if (aParentTagId == eHTMLTag_button) {
static const nsHTMLTag kButtonExcludeKids[] = {
eHTMLTag_a, eHTMLTag_fieldset, eHTMLTag_form, eHTMLTag_iframe,
eHTMLTag_input, eHTMLTag_select, eHTMLTag_textarea};
uint32_t j;
for (j = 0; j < ArrayLength(kButtonExcludeKids); ++j) {
if (kButtonExcludeKids[j] == aChildTagId) {
return false;
}
}
}
// Deprecated elements.
if (aChildTagId == eHTMLTag_bgsound) {
return false;
}
// Bug #67007, dont strip userdefined tags.
if (aChildTagId == eHTMLTag_userdefined) {
return true;
}
const ElementInfo& parent = kElements[aParentTagId - 1];
if (aParentTagId == aChildTagId) {
return parent.mCanContainSelf;
}
const ElementInfo& child = kElements[aChildTagId - 1];
return !!(parent.mCanContainGroups & child.mGroup);
}
bool HTMLEditUtils::ContentIsInert(const nsIContent& aContent) {
for (nsIContent* content :
aContent.InclusiveFlatTreeAncestorsOfType<nsIContent>()) {
if (nsIFrame* frame = content->GetPrimaryFrame()) {
return frame->StyleUI()->IsInert();
}
// If it doesn't have primary frame, we need to check its ancestors.
// This may occur if it's an invisible text node or element nodes whose
// display is an invisible value.
if (!content->IsElement()) {
continue;
}
if (content->AsElement()->State().HasState(dom::ElementState::INERT)) {
return true;
}
}
return false;
}
bool HTMLEditUtils::IsContainerNode(nsHTMLTag aTagId) {
NS_ASSERTION(aTagId > eHTMLTag_unknown && aTagId <= eHTMLTag_userdefined,
"aTagId out of range!");
return kElements[aTagId - 1].mIsContainer;
}
bool HTMLEditUtils::IsNonListSingleLineContainer(const nsINode& aNode) {
return aNode.IsAnyOfHTMLElements(
nsGkAtoms::address, nsGkAtoms::div, nsGkAtoms::h1, nsGkAtoms::h2,
nsGkAtoms::h3, nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6,
nsGkAtoms::listing, nsGkAtoms::p, nsGkAtoms::pre, nsGkAtoms::xmp);
}
bool HTMLEditUtils::IsSingleLineContainer(const nsINode& aNode) {
return IsNonListSingleLineContainer(aNode) ||
aNode.IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dt, nsGkAtoms::dd);
}
// static
template <typename PT, typename CT>
nsIContent* HTMLEditUtils::GetPreviousContent(
const EditorDOMPointBase<PT, CT>& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
MOZ_ASSERT(aPoint.IsSetAndValid());
NS_WARNING_ASSERTION(
!aPoint.IsInDataNode() || aPoint.IsInTextNode(),
"GetPreviousContent() doesn't assume that the start point is a "
"data node except text node");
// If we are at the beginning of the node, or it is a text node, then just
// look before it.
if (aPoint.IsStartOfContainer() || aPoint.IsInTextNode()) {
if (aOptions.contains(WalkTreeOption::StopAtBlockBoundary) &&
aPoint.IsInContentNode() &&
HTMLEditUtils::IsBlockElement(
*aPoint.template ContainerAs<nsIContent>(), aBlockInlineCheck)) {
// If we aren't allowed to cross blocks, don't look before this block.
return nullptr;
}
return HTMLEditUtils::GetPreviousContent(
*aPoint.GetContainer(), aOptions, aBlockInlineCheck, aAncestorLimiter);
}
// else look before the child at 'aOffset'
if (aPoint.GetChild()) {
return HTMLEditUtils::GetPreviousContent(
*aPoint.GetChild(), aOptions, aBlockInlineCheck, aAncestorLimiter);
}
// unless there isn't one, in which case we are at the end of the node
// and want the deep-right child.
nsIContent* lastLeafContent = HTMLEditUtils::GetLastLeafContent(
*aPoint.GetContainer(),
{aOptions.contains(WalkTreeOption::StopAtBlockBoundary)
? LeafNodeType::LeafNodeOrChildBlock
: LeafNodeType::OnlyLeafNode},
aBlockInlineCheck);
if (!lastLeafContent) {
return nullptr;
}
if (!HTMLEditUtils::IsContentIgnored(*lastLeafContent, aOptions)) {
return lastLeafContent;
}
// restart the search from the non-editable node we just found
return HTMLEditUtils::GetPreviousContent(*lastLeafContent, aOptions,
aBlockInlineCheck, aAncestorLimiter);
}
// static
template <typename PT, typename CT>
nsIContent* HTMLEditUtils::GetNextContent(
const EditorDOMPointBase<PT, CT>& aPoint, const WalkTreeOptions& aOptions,
BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
MOZ_ASSERT(aPoint.IsSetAndValid());
NS_WARNING_ASSERTION(
!aPoint.IsInDataNode() || aPoint.IsInTextNode(),
"GetNextContent() doesn't assume that the start point is a "
"data node except text node");
auto point = aPoint.template To<EditorRawDOMPoint>();
// if the container is a text node, use its location instead
if (point.IsInTextNode()) {
point.SetAfter(point.GetContainer());
if (NS_WARN_IF(!point.IsSet())) {
return nullptr;
}
}
if (point.GetChild()) {
if (aOptions.contains(WalkTreeOption::StopAtBlockBoundary) &&
HTMLEditUtils::IsBlockElement(*point.GetChild(), aBlockInlineCheck)) {
return point.GetChild();
}
nsIContent* firstLeafContent = HTMLEditUtils::GetFirstLeafContent(
*point.GetChild(),
{aOptions.contains(WalkTreeOption::StopAtBlockBoundary)
? LeafNodeType::LeafNodeOrChildBlock
: LeafNodeType::OnlyLeafNode},
aBlockInlineCheck);
if (!firstLeafContent) {
return point.GetChild();
}
// XXX Why do we need to do this check? The leaf node must be a descendant
// of `point.GetChild()`.
if (aAncestorLimiter &&
(firstLeafContent == aAncestorLimiter ||
!firstLeafContent->IsInclusiveDescendantOf(aAncestorLimiter))) {
return nullptr;
}
if (!HTMLEditUtils::IsContentIgnored(*firstLeafContent, aOptions)) {
return firstLeafContent;
}
// restart the search from the non-editable node we just found
return HTMLEditUtils::GetNextContent(*firstLeafContent, aOptions,
aBlockInlineCheck, aAncestorLimiter);
}
// unless there isn't one, in which case we are at the end of the node
// and want the next one.
if (aOptions.contains(WalkTreeOption::StopAtBlockBoundary) &&
point.IsInContentNode() &&
HTMLEditUtils::IsBlockElement(*point.template ContainerAs<nsIContent>(),
aBlockInlineCheck)) {
// don't cross out of parent block
return nullptr;
}
return HTMLEditUtils::GetNextContent(*point.GetContainer(), aOptions,
aBlockInlineCheck, aAncestorLimiter);
}
// static
nsIContent* HTMLEditUtils::GetAdjacentLeafContent(
const nsINode& aNode, WalkTreeDirection aWalkTreeDirection,
const WalkTreeOptions& aOptions, BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
// called only by GetPriorNode so we don't need to check params.
MOZ_ASSERT(&aNode != aAncestorLimiter);
MOZ_ASSERT_IF(aAncestorLimiter,
aAncestorLimiter->IsInclusiveDescendantOf(aAncestorLimiter));
const nsINode* node = &aNode;
for (;;) {
// if aNode has a sibling in the right direction, return
// that sibling's closest child (or itself if it has no children)
nsIContent* sibling = aWalkTreeDirection == WalkTreeDirection::Forward
? node->GetNextSibling()
: node->GetPreviousSibling();
if (sibling) {
// XXX If `sibling` belongs to siblings of inclusive ancestors of aNode,
// perhaps, we need to use
// IgnoreInsideBlockBoundary(aBlockInlineCheck) here.
if (aOptions.contains(WalkTreeOption::StopAtBlockBoundary) &&
HTMLEditUtils::IsBlockElement(*sibling, aBlockInlineCheck)) {
// don't look inside previous sibling, since it is a block
return sibling;
}
const LeafNodeTypes leafNodeTypes = {
aOptions.contains(WalkTreeOption::StopAtBlockBoundary)
? LeafNodeType::LeafNodeOrChildBlock
: LeafNodeType::OnlyLeafNode};
nsIContent* leafContent =
aWalkTreeDirection == WalkTreeDirection::Forward
? HTMLEditUtils::GetFirstLeafContent(*sibling, leafNodeTypes,
aBlockInlineCheck)
: HTMLEditUtils::GetLastLeafContent(*sibling, leafNodeTypes,
aBlockInlineCheck);
return leafContent ? leafContent : sibling;
}
nsIContent* parent = node->GetParent();
if (!parent) {
return nullptr;
}
if (parent == aAncestorLimiter ||
(aOptions.contains(WalkTreeOption::StopAtBlockBoundary) &&
HTMLEditUtils::IsBlockElement(*parent, aBlockInlineCheck))) {
return nullptr;
}
node = parent;
}
MOZ_ASSERT_UNREACHABLE("What part of for(;;) do you not understand?");
return nullptr;
}
// static
nsIContent* HTMLEditUtils::GetAdjacentContent(
const nsINode& aNode, WalkTreeDirection aWalkTreeDirection,
const WalkTreeOptions& aOptions, BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
if (&aNode == aAncestorLimiter) {
// Don't allow traversal above the root node! This helps
// prevent us from accidentally editing browser content
// when the editor is in a text widget.
return nullptr;
}
nsIContent* leafContent = HTMLEditUtils::GetAdjacentLeafContent(
aNode, aWalkTreeDirection, aOptions, aBlockInlineCheck, aAncestorLimiter);
if (!leafContent) {
return nullptr;
}
if (!HTMLEditUtils::IsContentIgnored(*leafContent, aOptions)) {
return leafContent;
}
return HTMLEditUtils::GetAdjacentContent(*leafContent, aWalkTreeDirection,
aOptions, aBlockInlineCheck,
aAncestorLimiter);
}
// static
template <typename EditorDOMPointType>
EditorDOMPointType HTMLEditUtils::GetPreviousEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary) {
MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent));
NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent) ||
HTMLEditUtils::IsTableCellOrCaption(aContent),
"HTMLEditUtils::GetPreviousEditablePoint() may return a point "
"between table structure elements");
if (&aContent == aAncestorLimiter) {
return EditorDOMPointType();
}
// First, look for previous content.
nsIContent* previousContent = aContent.GetPreviousSibling();
if (!previousContent) {
if (!aContent.GetParentElement()) {
return EditorDOMPointType();
}
nsIContent* inclusiveAncestor = &aContent;
for (Element* parentElement : aContent.AncestorsOfType<Element>()) {
if (parentElement == aAncestorLimiter ||
!HTMLEditUtils::IsSimplyEditableNode(*parentElement) ||
!HTMLEditUtils::CanCrossContentBoundary(*parentElement,
aHowToTreatTableBoundary)) {
// If cannot cross the parent element boundary, return the point of
// last inclusive ancestor point.
return EditorDOMPointType(inclusiveAncestor);
}
// Start of the parent element is a next editable point if it's an
// element which is not a table structure element.
if (!HTMLEditUtils::IsAnyTableElement(parentElement) ||
HTMLEditUtils::IsTableCellOrCaption(*parentElement)) {
inclusiveAncestor = parentElement;
}
previousContent = parentElement->GetPreviousSibling();
if (!previousContent) {
continue; // Keep looking for previous sibling of an ancestor.
}
// XXX Should we ignore data node like CDATA, Comment, etc?
// If previous content is not editable, let's return the point after it.
if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent)) {
return EditorDOMPointType::After(*previousContent);
}
// If cannot cross previous content boundary, return start of last
// inclusive ancestor.
if (!HTMLEditUtils::CanCrossContentBoundary(*previousContent,
aHowToTreatTableBoundary)) {
return inclusiveAncestor == &aContent
? EditorDOMPointType(inclusiveAncestor)
: EditorDOMPointType(inclusiveAncestor, 0);
}
break;
}
if (!previousContent) {
return EditorDOMPointType(inclusiveAncestor);
}
} else if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent)) {
return EditorDOMPointType::After(*previousContent);
} else if (!HTMLEditUtils::CanCrossContentBoundary(
*previousContent, aHowToTreatTableBoundary)) {
return EditorDOMPointType(&aContent);
}
// Next, look for end of the previous content.
nsIContent* leafContent = previousContent;
if (previousContent->GetChildCount() &&
HTMLEditUtils::IsContainerNode(*previousContent)) {
for (nsIContent* maybeLeafContent = previousContent->GetLastChild();
maybeLeafContent;
maybeLeafContent = maybeLeafContent->GetLastChild()) {
// If it's not an editable content or cannot cross the boundary,
// return the point after the content. Note that in this case,
// the content must not be any table elements except `<table>`
// because we've climbed down the tree.
if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent) ||
!HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent,
aHowToTreatTableBoundary)) {
return EditorDOMPointType::After(*maybeLeafContent);
}
leafContent = maybeLeafContent;
if (!HTMLEditUtils::IsContainerNode(*leafContent)) {
break;
}
}
}
if (leafContent->IsText()) {
Text* textNode = leafContent->AsText();
if (aInvisibleWhiteSpaces == InvisibleWhiteSpaces::Preserve) {
return EditorDOMPointType::AtEndOf(*textNode);
}
// There may be invisible trailing white-spaces which should be
// ignored. Let's scan its start.
return WSRunScanner::GetAfterLastVisiblePoint<EditorDOMPointType>(
*textNode, aAncestorLimiter);
}
// If it's a container element, return end of it. Otherwise, return
// the point after the non-container element.
return HTMLEditUtils::IsContainerNode(*leafContent)
? EditorDOMPointType::AtEndOf(*leafContent)
: EditorDOMPointType::After(*leafContent);
}
// static
template <typename EditorDOMPointType>
EditorDOMPointType HTMLEditUtils::GetNextEditablePoint(
nsIContent& aContent, const Element* aAncestorLimiter,
InvisibleWhiteSpaces aInvisibleWhiteSpaces,
TableBoundary aHowToTreatTableBoundary) {
MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent));
NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent) ||
HTMLEditUtils::IsTableCellOrCaption(aContent),
"HTMLEditUtils::GetPreviousEditablePoint() may return a point "
"between table structure elements");
if (&aContent == aAncestorLimiter) {
return EditorDOMPointType();
}
// First, look for next content.
nsIContent* nextContent = aContent.GetNextSibling();
if (!nextContent) {
if (!aContent.GetParentElement()) {
return EditorDOMPointType();
}
nsIContent* inclusiveAncestor = &aContent;
for (Element* parentElement : aContent.AncestorsOfType<Element>()) {
if (parentElement == aAncestorLimiter ||
!HTMLEditUtils::IsSimplyEditableNode(*parentElement) ||
!HTMLEditUtils::CanCrossContentBoundary(*parentElement,
aHowToTreatTableBoundary)) {
// If cannot cross the parent element boundary, return the point of
// last inclusive ancestor point.
return EditorDOMPointType(inclusiveAncestor);
}
// End of the parent element is a next editable point if it's an
// element which is not a table structure element.
if (!HTMLEditUtils::IsAnyTableElement(parentElement) ||
HTMLEditUtils::IsTableCellOrCaption(*parentElement)) {
inclusiveAncestor = parentElement;
}
nextContent = parentElement->GetNextSibling();
if (!nextContent) {
continue; // Keep looking for next sibling of an ancestor.
}
// XXX Should we ignore data node like CDATA, Comment, etc?
// If next content is not editable, let's return the point after
// the last inclusive ancestor.
if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent)) {
return EditorDOMPointType::After(*parentElement);
}
// If cannot cross next content boundary, return after the last
// inclusive ancestor.
if (!HTMLEditUtils::CanCrossContentBoundary(*nextContent,
aHowToTreatTableBoundary)) {
return EditorDOMPointType::After(*inclusiveAncestor);
}
break;
}
if (!nextContent) {
return EditorDOMPointType::After(*inclusiveAncestor);
}
} else if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent)) {
return EditorDOMPointType::After(aContent);
} else if (!HTMLEditUtils::CanCrossContentBoundary(
*nextContent, aHowToTreatTableBoundary)) {
return EditorDOMPointType::After(aContent);
}
// Next, look for start of the next content.
nsIContent* leafContent = nextContent;
if (nextContent->GetChildCount() &&
HTMLEditUtils::IsContainerNode(*nextContent)) {
for (nsIContent* maybeLeafContent = nextContent->GetFirstChild();
maybeLeafContent;
maybeLeafContent = maybeLeafContent->GetFirstChild()) {
// If it's not an editable content or cannot cross the boundary,
// return the point at the content (i.e., start of its parent). Note
// that in this case, the content must not be any table elements except
// `<table>` because we've climbed down the tree.
if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent) ||
!HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent,
aHowToTreatTableBoundary)) {
return EditorDOMPointType(maybeLeafContent);
}
leafContent = maybeLeafContent;
if (!HTMLEditUtils::IsContainerNode(*leafContent)) {
break;
}
}
}
if (leafContent->IsText()) {
Text* textNode = leafContent->AsText();
if (aInvisibleWhiteSpaces == InvisibleWhiteSpaces::Preserve) {
return EditorDOMPointType(textNode, 0);
}
// There may be invisible leading white-spaces which should be
// ignored. Let's scan its start.
return WSRunScanner::GetFirstVisiblePoint<EditorDOMPointType>(
*textNode, aAncestorLimiter);
}
// If it's a container element, return start of it. Otherwise, return
// the point at the non-container element (i.e., start of its parent).
return HTMLEditUtils::IsContainerNode(*leafContent)
? EditorDOMPointType(leafContent, 0)
: EditorDOMPointType(leafContent);
}
// static
Element* HTMLEditUtils::GetAncestorElement(
const nsIContent& aContent, const AncestorTypes& aAncestorTypes,
BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
MOZ_ASSERT(
aAncestorTypes.contains(AncestorType::ClosestBlockElement) ||
aAncestorTypes.contains(AncestorType::MostDistantInlineElementInBlock) ||
aAncestorTypes.contains(AncestorType::ButtonElement));
if (&aContent == aAncestorLimiter) {
return nullptr;
}
const Element* theBodyElement = aContent.OwnerDoc()->GetBody();
const Element* theDocumentElement = aContent.OwnerDoc()->GetDocumentElement();
Element* lastAncestorElement = nullptr;
const bool editableElementOnly =
aAncestorTypes.contains(AncestorType::EditableElement);
const bool lookingForClosestBlockElement =
aAncestorTypes.contains(AncestorType::ClosestBlockElement);
const bool lookingForMostDistantInlineElementInBlock =
aAncestorTypes.contains(AncestorType::MostDistantInlineElementInBlock);
const bool ignoreHRElement =
aAncestorTypes.contains(AncestorType::IgnoreHRElement);
const bool lookingForButtonElement =
aAncestorTypes.contains(AncestorType::ButtonElement);
auto IsSearchingElementType = [&](const nsIContent& aContent) -> bool {
if (!aContent.IsElement() ||
(ignoreHRElement && aContent.IsHTMLElement(nsGkAtoms::hr))) {
return false;
}
if (editableElementOnly &&
!EditorUtils::IsEditableContent(aContent, EditorType::HTML)) {
return false;
}
return (lookingForClosestBlockElement &&
HTMLEditUtils::IsBlockElement(aContent, aBlockInlineCheck)) ||
(lookingForMostDistantInlineElementInBlock &&
HTMLEditUtils::IsInlineContent(aContent, aBlockInlineCheck)) ||
(lookingForButtonElement &&
aContent.IsHTMLElement(nsGkAtoms::button));
};
for (Element* element : aContent.AncestorsOfType<Element>()) {
if (editableElementOnly &&
!EditorUtils::IsEditableContent(*element, EditorType::HTML)) {
return lastAncestorElement && IsSearchingElementType(*lastAncestorElement)
? lastAncestorElement // editing host (can be inline element)
: nullptr;
}
if (ignoreHRElement && element->IsHTMLElement(nsGkAtoms::hr)) {
if (element == aAncestorLimiter) {
break;
}
continue;
}
if (lookingForButtonElement && element->IsHTMLElement(nsGkAtoms::button)) {
return element; // closest button element
}
if (HTMLEditUtils::IsBlockElement(*element, aBlockInlineCheck)) {
if (lookingForClosestBlockElement) {
return element; // closest block element
}
MOZ_ASSERT_IF(lastAncestorElement,
HTMLEditUtils::IsInlineContent(*lastAncestorElement,
aBlockInlineCheck));
return lastAncestorElement; // the last inline element which we found
}
if (element == aAncestorLimiter || element == theBodyElement ||
element == theDocumentElement) {
break;
}
lastAncestorElement = element;
}
return lastAncestorElement && IsSearchingElementType(*lastAncestorElement)
? lastAncestorElement
: nullptr;
}
// static
Element* HTMLEditUtils::GetInclusiveAncestorElement(
const nsIContent& aContent, const AncestorTypes& aAncestorTypes,
BlockInlineCheck aBlockInlineCheck,
const Element* aAncestorLimiter /* = nullptr */) {
MOZ_ASSERT(
aAncestorTypes.contains(AncestorType::ClosestBlockElement) ||
aAncestorTypes.contains(AncestorType::MostDistantInlineElementInBlock) ||
aAncestorTypes.contains(AncestorType::ButtonElement));
const Element* theBodyElement = aContent.OwnerDoc()->GetBody();
const Element* theDocumentElement = aContent.OwnerDoc()->GetDocumentElement();
const bool editableElementOnly =
aAncestorTypes.contains(AncestorType::EditableElement);
const bool lookingForClosestBlockElement =
aAncestorTypes.contains(AncestorType::ClosestBlockElement);
const bool lookingForMostDistantInlineElementInBlock =
aAncestorTypes.contains(AncestorType::MostDistantInlineElementInBlock);
const bool lookingForButtonElement =
aAncestorTypes.contains(AncestorType::ButtonElement);
const bool ignoreHRElement =
aAncestorTypes.contains(AncestorType::IgnoreHRElement);
auto IsSearchingElementType = [&](const nsIContent& aContent) -> bool {
if (!aContent.IsElement() ||
(ignoreHRElement && aContent.IsHTMLElement(nsGkAtoms::hr))) {
return false;
}
if (editableElementOnly &&
!EditorUtils::IsEditableContent(aContent, EditorType::HTML)) {
return false;
}
return (lookingForClosestBlockElement &&
HTMLEditUtils::IsBlockElement(aContent, aBlockInlineCheck)) ||
(lookingForMostDistantInlineElementInBlock &&
HTMLEditUtils::IsInlineContent(aContent, aBlockInlineCheck)) ||
(lookingForButtonElement &&
aContent.IsHTMLElement(nsGkAtoms::button));
};
// If aContent is the body element or the document element, we shouldn't climb
// up to its parent.
if (editableElementOnly &&
(&aContent == theBodyElement || &aContent == theDocumentElement)) {
return IsSearchingElementType(aContent)
? const_cast<Element*>(aContent.AsElement())
: nullptr;
}
if (lookingForButtonElement && aContent.IsHTMLElement(nsGkAtoms::button)) {
return const_cast<Element*>(aContent.AsElement());
}
// If aContent is a block element, we don't need to climb up the tree.
// Consider the result right now.
if ((lookingForClosestBlockElement ||
lookingForMostDistantInlineElementInBlock) &&
HTMLEditUtils::IsBlockElement(aContent, aBlockInlineCheck) &&
!(ignoreHRElement && aContent.IsHTMLElement(nsGkAtoms::hr))) {
return IsSearchingElementType(aContent)
? const_cast<Element*>(aContent.AsElement())
: nullptr;
}
// If aContent is the last element to search range because of the parent
// element type, consider the result before calling GetAncestorElement()
// because it won't return aContent.
if (!aContent.GetParent() ||
(editableElementOnly && !EditorUtils::IsEditableContent(
*aContent.GetParent(), EditorType::HTML)) ||
(!lookingForClosestBlockElement &&
HTMLEditUtils::IsBlockElement(*aContent.GetParent(),
aBlockInlineCheck) &&
!(ignoreHRElement &&
aContent.GetParent()->IsHTMLElement(nsGkAtoms::hr)))) {
return IsSearchingElementType(aContent)
? const_cast<Element*>(aContent.AsElement())
: nullptr;
}
if (&aContent == aAncestorLimiter) {
return nullptr;
}
return HTMLEditUtils::GetAncestorElement(aContent, aAncestorTypes,
aBlockInlineCheck, aAncestorLimiter);
}
// static
Element* HTMLEditUtils::GetClosestAncestorAnyListElement(
const nsIContent& aContent) {
for (Element* element : aContent.AncestorsOfType<Element>()) {
if (HTMLEditUtils::IsAnyListElement(element)) {
return element;
}
}
return nullptr;
}
// static
Element* HTMLEditUtils::GetClosestInclusiveAncestorAnyListElement(
const nsIContent& aContent) {
for (Element* element : aContent.InclusiveAncestorsOfType<Element>()) {
if (HTMLEditUtils::IsAnyListElement(element)) {
return element;
}
}
return nullptr;
}
EditAction HTMLEditUtils::GetEditActionForInsert(const nsAtom& aTagName) {
// This method may be in a hot path. So, return only necessary
// EditAction::eInsert*Element.
if (&aTagName == nsGkAtoms::ul) {
// For InputEvent.inputType, "insertUnorderedList".
return EditAction::eInsertUnorderedListElement;
}
if (&aTagName == nsGkAtoms::ol) {
// For InputEvent.inputType, "insertOrderedList".
return EditAction::eInsertOrderedListElement;
}
if (&aTagName == nsGkAtoms::hr) {
// For InputEvent.inputType, "insertHorizontalRule".
return EditAction::eInsertHorizontalRuleElement;
}
return EditAction::eInsertNode;
}
EditAction HTMLEditUtils::GetEditActionForRemoveList(const nsAtom& aTagName) {
// This method may be in a hot path. So, return only necessary
// EditAction::eRemove*Element.
if (&aTagName == nsGkAtoms::ul) {
// For InputEvent.inputType, "insertUnorderedList".
return EditAction::eRemoveUnorderedListElement;
}
if (&aTagName == nsGkAtoms::ol) {
// For InputEvent.inputType, "insertOrderedList".
return EditAction::eRemoveOrderedListElement;
}
return EditAction::eRemoveListElement;
}
EditAction HTMLEditUtils::GetEditActionForInsert(const Element& aElement) {
return GetEditActionForInsert(*aElement.NodeInfo()->NameAtom());
}
EditAction HTMLEditUtils::GetEditActionForFormatText(const nsAtom& aProperty,
const nsAtom* aAttribute,
bool aToSetStyle) {
// This method may be in a hot path. So, return only necessary
// EditAction::eSet*Property or EditAction::eRemove*Property.
if (&aProperty == nsGkAtoms::b) {
return aToSetStyle ? EditAction::eSetFontWeightProperty
: EditAction::eRemoveFontWeightProperty;
}
if (&aProperty == nsGkAtoms::i) {
return aToSetStyle ? EditAction::eSetTextStyleProperty
: EditAction::eRemoveTextStyleProperty;
}
if (&aProperty == nsGkAtoms::u) {
return aToSetStyle ? EditAction::eSetTextDecorationPropertyUnderline
: EditAction::eRemoveTextDecorationPropertyUnderline;
}
if (&aProperty == nsGkAtoms::strike) {
return aToSetStyle ? EditAction::eSetTextDecorationPropertyLineThrough
: EditAction::eRemoveTextDecorationPropertyLineThrough;
}
if (&aProperty == nsGkAtoms::sup) {
return aToSetStyle ? EditAction::eSetVerticalAlignPropertySuper
: EditAction::eRemoveVerticalAlignPropertySuper;
}
if (&aProperty == nsGkAtoms::sub) {
return aToSetStyle ? EditAction::eSetVerticalAlignPropertySub
: EditAction::eRemoveVerticalAlignPropertySub;
}
if (&aProperty == nsGkAtoms::font) {
if (aAttribute == nsGkAtoms::face) {
return aToSetStyle ? EditAction::eSetFontFamilyProperty
: EditAction::eRemoveFontFamilyProperty;
}
if (aAttribute == nsGkAtoms::color) {
return aToSetStyle ? EditAction::eSetColorProperty
: EditAction::eRemoveColorProperty;
}
if (aAttribute == nsGkAtoms::bgcolor) {
return aToSetStyle ? EditAction::eSetBackgroundColorPropertyInline
: EditAction::eRemoveBackgroundColorPropertyInline;
}
}
return aToSetStyle ? EditAction::eSetInlineStyleProperty
: EditAction::eRemoveInlineStyleProperty;
}
EditAction HTMLEditUtils::GetEditActionForAlignment(
const nsAString& aAlignType) {
// This method may be in a hot path. So, return only necessary
// EditAction::eAlign*.
if (aAlignType.EqualsLiteral("left")) {
return EditAction::eAlignLeft;
}
if (aAlignType.EqualsLiteral("right")) {
return EditAction::eAlignRight;
}
if (aAlignType.EqualsLiteral("center")) {
return EditAction::eAlignCenter;
}
if (aAlignType.EqualsLiteral("justify")) {
return EditAction::eJustify;
}
return EditAction::eSetAlignment;
}
// static
template <typename EditorDOMPointType>
nsIContent* HTMLEditUtils::GetContentToPreserveInlineStyles(
const EditorDOMPointType& aPoint, const Element& aEditingHost) {
MOZ_ASSERT(aPoint.IsSetAndValid());
if (MOZ_UNLIKELY(!aPoint.IsInContentNode())) {
return nullptr;
}
// If it points middle of a text node, use it. Otherwise, scan next visible
// thing and use the style of following text node if there is.
if (aPoint.IsInTextNode() && !aPoint.IsEndOfContainer()) {
return aPoint.template ContainerAs<nsIContent>();
}
for (auto point = aPoint.template To<EditorRawDOMPoint>(); point.IsSet();) {
const WSScanResult nextVisibleThing =
WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary(
&aEditingHost, point,
BlockInlineCheck::UseComputedDisplayOutsideStyle);
if (nextVisibleThing.InVisibleOrCollapsibleCharacters()) {
return nextVisibleThing.TextPtr();
}
// Ignore empty inline container elements because it's not visible for
// users so that using the style will appear suddenly from point of
// view of users.
if (nextVisibleThing.ReachedSpecialContent() &&
nextVisibleThing.IsContentEditable() &&
nextVisibleThing.ContentIsElement() &&
!nextVisibleThing.ElementPtr()->HasChildNodes() &&
HTMLEditUtils::IsContainerNode(*nextVisibleThing.ElementPtr())) {
point.SetAfter(nextVisibleThing.ElementPtr());
continue;
}
// Otherwise, we should use style of the container of the start point.
break;
}
return aPoint.template ContainerAs<nsIContent>();
}
template <typename EditorDOMPointType, typename EditorDOMPointTypeInput>
EditorDOMPointType HTMLEditUtils::GetBetterInsertionPointFor(
const nsIContent& aContentToInsert,
const EditorDOMPointTypeInput& aPointToInsert,
const Element& aEditingHost) {
if (NS_WARN_IF(!aPointToInsert.IsSet())) {
return EditorDOMPointType();
}
auto pointToInsert =
aPointToInsert.template GetNonAnonymousSubtreePoint<EditorDOMPointType>();
if (MOZ_UNLIKELY(
NS_WARN_IF(!pointToInsert.IsSet()) ||
NS_WARN_IF(!pointToInsert.GetContainer()->IsInclusiveDescendantOf(
&aEditingHost)))) {
// Cannot insert aContentToInsert into this DOM tree.
return EditorDOMPointType();
}
// If the node to insert is not a block level element, we can insert it
// at any point.
if (!HTMLEditUtils::IsBlockElement(
aContentToInsert, BlockInlineCheck::UseComputedDisplayStyle)) {
return pointToInsert;
}
WSRunScanner wsScannerForPointToInsert(
const_cast<Element*>(&aEditingHost), pointToInsert,
BlockInlineCheck::UseComputedDisplayStyle);
// If the insertion position is after the last visible item in a line,
// i.e., the insertion position is just before a visible line break <br>,
// we want to skip to the position just after the line break (see bug 68767).
const WSScanResult forwardScanFromPointToInsertResult =
wsScannerForPointToInsert.ScanInclusiveNextVisibleNodeOrBlockBoundaryFrom(
pointToInsert);
// So, if the next visible node isn't a <br> element, we can insert the block
// level element to the point.
if (!forwardScanFromPointToInsertResult.ReachedBRElement()) {
return pointToInsert;
}
// However, we must not skip next <br> element when the caret appears to be
// positioned at the beginning of a block, in that case skipping the <br>
// would not insert the <br> at the caret position, but after the current
// empty line.
const WSScanResult backwardScanFromPointToInsertResult =
wsScannerForPointToInsert.ScanPreviousVisibleNodeOrBlockBoundaryFrom(
pointToInsert);
// So, if there is no previous visible node,
// or, if both nodes of the insertion point is <br> elements,
// or, if the previous visible node is different block,
// we need to skip the following <br>. So, otherwise, we can insert the
// block at the insertion point.
if (NS_WARN_IF(backwardScanFromPointToInsertResult.Failed()) ||
backwardScanFromPointToInsertResult.ReachedInlineEditingHostBoundary() ||
backwardScanFromPointToInsertResult.ReachedBRElement() ||
backwardScanFromPointToInsertResult.ReachedCurrentBlockBoundary()) {
return pointToInsert;
}
return forwardScanFromPointToInsertResult
.template PointAfterReachedContent<EditorDOMPointType>();
}
// static
template <typename EditorDOMPointType, typename EditorDOMPointTypeInput>
EditorDOMPointType HTMLEditUtils::GetBetterCaretPositionToInsertText(
const EditorDOMPointTypeInput& aPoint, const Element& aEditingHost) {
MOZ_ASSERT(aPoint.IsSetAndValid());
MOZ_ASSERT(
aPoint.GetContainer()->IsInclusiveFlatTreeDescendantOf(&aEditingHost));
if (aPoint.IsInTextNode()) {
return aPoint.template To<EditorDOMPointType>();
}
if (!aPoint.IsEndOfContainer() && aPoint.GetChild() &&
aPoint.GetChild()->IsText()) {
return EditorDOMPointType(aPoint.GetChild(), 0u);
}
if (aPoint.IsEndOfContainer()) {
WSRunScanner scanner(&aEditingHost, aPoint,
BlockInlineCheck::UseComputedDisplayStyle);
const WSScanResult previousThing =
scanner.ScanPreviousVisibleNodeOrBlockBoundaryFrom(aPoint);
if (previousThing.InVisibleOrCollapsibleCharacters()) {
return EditorDOMPointType::AtEndOf(*previousThing.TextPtr());
}
}
if (HTMLEditUtils::CanNodeContain(*aPoint.GetContainer(),
*nsGkAtoms::textTagName)) {
return aPoint.template To<EditorDOMPointType>();
}
if (MOZ_UNLIKELY(aPoint.GetContainer() == &aEditingHost ||
!aPoint.template GetContainerParentAs<nsIContent>() ||
!HTMLEditUtils::CanNodeContain(
*aPoint.template ContainerParentAs<nsIContent>(),
*nsGkAtoms::textTagName))) {
return EditorDOMPointType();
}
return aPoint.ParentPoint().template To<EditorDOMPointType>();
}
// static
template <typename EditorDOMPointType, typename EditorDOMPointTypeInput>
Result<EditorDOMPointType, nsresult>
HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
const Element& aElement, const EditorDOMPointTypeInput& aCurrentPoint) {
MOZ_ASSERT(aCurrentPoint.IsSet());
// FYI: This was moved from
// https://searchfox.org/mozilla-central/rev/d3c2f51d89c3ca008ff0cb5a057e77ccd973443e/editor/libeditor/HTMLEditSubActionHandler.cpp#9193
// Use range boundaries and RangeUtils::CompareNodeToRange() to compare
// selection start to new block.
bool nodeBefore, nodeAfter;
nsresult rv = RangeUtils::CompareNodeToRangeBoundaries(
const_cast<Element*>(&aElement), aCurrentPoint.ToRawRangeBoundary(),
aCurrentPoint.ToRawRangeBoundary(), &nodeBefore, &nodeAfter);
if (NS_FAILED(rv)) {
NS_WARNING("RangeUtils::CompareNodeToRange() failed");
return Err(rv);
}
if (nodeBefore && nodeAfter) {
return EditorDOMPointType(); // aCurrentPoint is in aElement
}
if (nodeBefore) {
// selection is after block. put at end of block.
const nsIContent* lastEditableContent = HTMLEditUtils::GetLastChild(
aElement, {WalkTreeOption::IgnoreNonEditableNode});
if (!lastEditableContent) {
lastEditableContent = &aElement;
}
if (lastEditableContent->IsText() ||
HTMLEditUtils::IsContainerNode(*lastEditableContent)) {
return EditorDOMPointType::AtEndOf(*lastEditableContent);
}
MOZ_ASSERT(lastEditableContent->GetParentNode());
return EditorDOMPointType::After(*lastEditableContent);
}
// selection is before block. put at start of block.
const nsIContent* firstEditableContent = HTMLEditUtils::GetFirstChild(
aElement, {WalkTreeOption::IgnoreNonEditableNode});
if (!firstEditableContent) {
firstEditableContent = &aElement;
}
if (firstEditableContent->IsText() ||
HTMLEditUtils::IsContainerNode(*firstEditableContent)) {
MOZ_ASSERT(firstEditableContent->GetParentNode());
// XXX Shouldn't this be EditorDOMPointType(firstEditableContent, 0u)?
return EditorDOMPointType(firstEditableContent);
}
// XXX And shouldn't this be EditorDOMPointType(firstEditableContent)?
return EditorDOMPointType(firstEditableContent, 0u);
}
// static
bool HTMLEditUtils::IsInlineStyleSetByElement(
const nsIContent& aContent, const EditorInlineStyle& aStyle,
const nsAString* aValue, nsAString* aOutValue /* = nullptr */) {
for (Element* element : aContent.InclusiveAncestorsOfType<Element>()) {
if (aStyle.mHTMLProperty != element->NodeInfo()->NameAtom()) {
continue;
}
if (!aStyle.mAttribute) {
return true;
}
nsAutoString value;
element->GetAttr(aStyle.mAttribute, value);
if (aOutValue) {
*aOutValue = value;
}
if (!value.IsEmpty()) {
if (!aValue) {
return true;
}
if (aValue->Equals(value, nsCaseInsensitiveStringComparator)) {
return true;
}
// We found the prop with the attribute, but the value doesn't match.
return false;
}
}
return false;
}
// static
size_t HTMLEditUtils::CollectChildren(
const nsINode& aNode,
nsTArray<OwningNonNull<nsIContent>>& aOutArrayOfContents,
size_t aIndexToInsertChildren, const CollectChildrenOptions& aOptions) {
// FYI: This was moved from
// https://searchfox.org/mozilla-central/rev/4bce7d85ba4796dd03c5dcc7cfe8eee0e4c07b3b/editor/libeditor/HTMLEditSubActionHandler.cpp#6261
size_t numberOfFoundChildren = 0;
for (nsIContent* content =
GetFirstChild(aNode, {WalkTreeOption::IgnoreNonEditableNode});
content; content = content->GetNextSibling()) {
if ((aOptions.contains(CollectChildrenOption::CollectListChildren) &&
(HTMLEditUtils::IsAnyListElement(content) ||
HTMLEditUtils::IsListItem(content))) ||
(aOptions.contains(CollectChildrenOption::CollectTableChildren) &&
HTMLEditUtils::IsAnyTableElement(content))) {
numberOfFoundChildren += HTMLEditUtils::CollectChildren(
*content, aOutArrayOfContents,
aIndexToInsertChildren + numberOfFoundChildren, aOptions);
continue;
}
if (aOptions.contains(CollectChildrenOption::IgnoreNonEditableChildren) &&
!EditorUtils::IsEditableContent(*content, EditorType::HTML)) {
continue;
}
if (aOptions.contains(CollectChildrenOption::IgnoreInvisibleTextNodes) &&
content->IsText() &&
!HTMLEditUtils::IsVisibleTextNode(*content->AsText())) {
continue;
}
aOutArrayOfContents.InsertElementAt(
aIndexToInsertChildren + numberOfFoundChildren++, *content);
}
return numberOfFoundChildren;
}
// static
size_t HTMLEditUtils::CollectEmptyInlineContainerDescendants(
const nsINode& aNode,
nsTArray<OwningNonNull<nsIContent>>& aOutArrayOfContents,
const EmptyCheckOptions& aOptions, BlockInlineCheck aBlockInlineCheck) {
size_t numberOfFoundElements = 0;
for (Element* element = aNode.GetFirstElementChild(); element;) {
if (HTMLEditUtils::IsEmptyInlineContainer(*element, aOptions,
aBlockInlineCheck)) {
aOutArrayOfContents.AppendElement(*element);
numberOfFoundElements++;
nsIContent* nextContent = element->GetNextNonChildNode(&aNode);
element = nullptr;
for (; nextContent; nextContent = nextContent->GetNextNode(&aNode)) {
if (nextContent->IsElement()) {
element = nextContent->AsElement();
break;
}
}
continue;
}
nsIContent* nextContent = element->GetNextNode(&aNode);
element = nullptr;
for (; nextContent; nextContent = nextContent->GetNextNode(&aNode)) {
if (nextContent->IsElement()) {
element = nextContent->AsElement();
break;
}
}
}
return numberOfFoundElements;
}
// static
bool HTMLEditUtils::ElementHasAttributeExcept(const Element& aElement,
const nsAtom& aAttribute1,
const nsAtom& aAttribute2,
const nsAtom& aAttribute3) {
// FYI: This was moved from
// https://searchfox.org/mozilla-central/rev/0b1543e85d13c30a13c57e959ce9815a3f0fa1d3/editor/libeditor/HTMLStyleEditor.cpp#1626
for (auto i : IntegerRange<uint32_t>(aElement.GetAttrCount())) {
const nsAttrName* name = aElement.GetAttrNameAt(i);
if (!name->NamespaceEquals(kNameSpaceID_None)) {
return true;
}
if (name->LocalName() == &aAttribute1 ||
name->LocalName() == &aAttribute2 ||
name->LocalName() == &aAttribute3) {
continue; // Ignore the given attribute
}
// Ignore empty style, class and id attributes because those attributes are
// not meaningful with empty value.
if (name->LocalName() == nsGkAtoms::style ||
name->LocalName() == nsGkAtoms::_class ||
name->LocalName() == nsGkAtoms::id) {
if (aElement.HasNonEmptyAttr(name->LocalName())) {
return true;
}
continue;
}
// Ignore special _moz attributes
nsAutoString attrString;
name->LocalName()->ToString(attrString);
if (!StringBeginsWith(attrString, u"_moz"_ns)) {
return true;
}
}
// if we made it through all of them without finding a real attribute
// other than aAttribute, then return true
return false;
}
bool HTMLEditUtils::GetNormalizedHTMLColorValue(const nsAString& aColorValue,
nsAString& aNormalizedValue) {
nsAttrValue value;
if (!value.ParseColor(aColorValue)) {
aNormalizedValue = aColorValue;
return false;
}
nscolor color = NS_RGB(0, 0, 0);
MOZ_ALWAYS_TRUE(value.GetColorValue(color));
aNormalizedValue = NS_ConvertASCIItoUTF16(nsPrintfCString(
"#%02x%02x%02x", NS_GET_R(color), NS_GET_G(color), NS_GET_B(color)));
return true;
}
bool HTMLEditUtils::IsSameHTMLColorValue(
const nsAString& aColorA, const nsAString& aColorB,
TransparentKeyword aTransparentKeyword) {
if (aTransparentKeyword == TransparentKeyword::Allowed) {
const bool isATransparent = aColorA.LowerCaseEqualsLiteral("transparent");
const bool isBTransparent = aColorB.LowerCaseEqualsLiteral("transparent");
if (isATransparent || isBTransparent) {
return isATransparent && isBTransparent;
}
}
nsAttrValue valueA, valueB;
if (!valueA.ParseColor(aColorA) || !valueB.ParseColor(aColorB)) {
return false;
}
nscolor colorA = NS_RGB(0, 0, 0), colorB = NS_RGB(0, 0, 0);
MOZ_ALWAYS_TRUE(valueA.GetColorValue(colorA));
MOZ_ALWAYS_TRUE(valueB.GetColorValue(colorB));
return colorA == colorB;
}
bool HTMLEditUtils::MaybeCSSSpecificColorValue(const nsAString& aColorValue) {
if (aColorValue.IsEmpty() || aColorValue.First() == '#') {
return false; // Quick return for the most cases.
}
nsAutoString colorValue(aColorValue);
colorValue.CompressWhitespace(true, true);
if (colorValue.LowerCaseEqualsASCII("transparent")) {
return true;
}
nscolor color = NS_RGB(0, 0, 0);
if (colorValue.IsEmpty() || colorValue.First() == '#') {
return false;
}
const NS_ConvertUTF16toUTF8 colorU8(colorValue);
if (Servo_ColorNameToRgb(&colorU8, &color)) {
return false;
}
if (colorValue.LowerCaseEqualsASCII("initial") ||
colorValue.LowerCaseEqualsASCII("inherit") ||
colorValue.LowerCaseEqualsASCII("unset") ||
colorValue.LowerCaseEqualsASCII("revert") ||
colorValue.LowerCaseEqualsASCII("currentcolor")) {
return true;
}
return ServoCSSParser::IsValidCSSColor(colorU8);
}
static bool ComputeColor(const nsAString& aColorValue, nscolor* aColor,
bool* aIsCurrentColor) {
return ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0),
NS_ConvertUTF16toUTF8(aColorValue),
aColor, aIsCurrentColor);
}
static bool ComputeColor(const nsACString& aColorValue, nscolor* aColor,
bool* aIsCurrentColor) {
return ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), aColorValue,
aColor, aIsCurrentColor);
}
bool HTMLEditUtils::CanConvertToHTMLColorValue(const nsAString& aColorValue) {
bool isCurrentColor = false;
nscolor color = NS_RGB(0, 0, 0);
return ComputeColor(aColorValue, &color, &isCurrentColor) &&
!isCurrentColor && NS_GET_A(color) == 0xFF;
}
bool HTMLEditUtils::ConvertToNormalizedHTMLColorValue(
const nsAString& aColorValue, nsAString& aNormalizedValue) {
bool isCurrentColor = false;
nscolor color = NS_RGB(0, 0, 0);
if (!ComputeColor(aColorValue, &color, &isCurrentColor) || isCurrentColor ||
NS_GET_A(color) != 0xFF) {
aNormalizedValue = aColorValue;
return false;
}
aNormalizedValue.Truncate();
aNormalizedValue.AppendPrintf("#%02x%02x%02x", NS_GET_R(color),
NS_GET_G(color), NS_GET_B(color));
return true;
}
bool HTMLEditUtils::GetNormalizedCSSColorValue(const nsAString& aColorValue,
ZeroAlphaColor aZeroAlphaColor,
nsAString& aNormalizedValue) {
bool isCurrentColor = false;
nscolor color = NS_RGB(0, 0, 0);
if (!ComputeColor(aColorValue, &color, &isCurrentColor)) {
aNormalizedValue = aColorValue;
return false;
}
// If it's currentcolor, let's return it as-is since we cannot resolve it
// without ancestors.
if (isCurrentColor) {
aNormalizedValue = aColorValue;
return true;
}
if (aZeroAlphaColor == ZeroAlphaColor::TransparentKeyword &&
NS_GET_A(color) == 0) {
aNormalizedValue.AssignLiteral("transparent");
return true;
}
// Get serialized color value (i.e., "rgb()" or "rgba()").
aNormalizedValue.Truncate();
nsStyleUtil::GetSerializedColorValue(color, aNormalizedValue);
return true;
}
template <typename CharType>
bool HTMLEditUtils::IsSameCSSColorValue(const nsTSubstring<CharType>& aColorA,
const nsTSubstring<CharType>& aColorB) {
bool isACurrentColor = false;
nscolor colorA = NS_RGB(0, 0, 0);
if (!ComputeColor(aColorA, &colorA, &isACurrentColor)) {
return false;
}
bool isBCurrentColor = false;
nscolor colorB = NS_RGB(0, 0, 0);
if (!ComputeColor(aColorB, &colorB, &isBCurrentColor)) {
return false;
}
if (isACurrentColor || isBCurrentColor) {
return isACurrentColor && isBCurrentColor;
}
return colorA == colorB;
}
/******************************************************************************
* SelectedTableCellScanner
******************************************************************************/
SelectedTableCellScanner::SelectedTableCellScanner(
const AutoRangeArray& aRanges) {
if (aRanges.Ranges().IsEmpty()) {
return;
}
Element* firstSelectedCellElement =
HTMLEditUtils::GetTableCellElementIfOnlyOneSelected(
aRanges.FirstRangeRef());
if (!firstSelectedCellElement) {
return; // We're not in table cell selection mode.
}
mSelectedCellElements.SetCapacity(aRanges.Ranges().Length());
mSelectedCellElements.AppendElement(*firstSelectedCellElement);
for (uint32_t i = 1; i < aRanges.Ranges().Length(); i++) {
nsRange* range = aRanges.Ranges()[i];
if (NS_WARN_IF(!range) || NS_WARN_IF(!range->IsPositioned())) {
continue; // Shouldn't occur in normal conditions.
}
// Just ignore selection ranges which do not select only one table
// cell element. This is possible case if web apps sets multiple
// selections and first range selects a table cell element.
if (Element* selectedCellElement =
HTMLEditUtils::GetTableCellElementIfOnlyOneSelected(*range)) {
mSelectedCellElements.AppendElement(*selectedCellElement);
}
}
}
} // namespace mozilla
|