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
|
/* -*- 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 "HTMLEditor.h"
#include "HTMLEditUtils.h"
#include "TypeInState.h"
#include "mozilla/Assertions.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/EditAction.h"
#include "mozilla/EditorUtils.h"
#include "mozilla/SelectionState.h"
#include "mozilla/mozalloc.h"
#include "mozilla/dom/AncestorIterator.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLBRElement.h"
#include "mozilla/dom/Selection.h"
#include "nsAString.h"
#include "nsAttrName.h"
#include "nsCOMPtr.h"
#include "nsCaseTreatment.h"
#include "nsComponentManagerUtils.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsAtom.h"
#include "nsIContent.h"
#include "nsNameSpaceManager.h"
#include "nsINode.h"
#include "nsIPrincipal.h"
#include "nsISupportsImpl.h"
#include "nsLiteralString.h"
#include "nsRange.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsStringFwd.h"
#include "nsStyledElement.h"
#include "nsTArray.h"
#include "nsUnicharUtils.h"
#include "nscore.h"
class nsISupports;
namespace mozilla {
using namespace dom;
using ChildBlockBoundary = HTMLEditUtils::ChildBlockBoundary;
nsresult HTMLEditor::SetInlinePropertyAsAction(nsAtom& aProperty,
nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(
*this,
HTMLEditUtils::GetEditActionForFormatText(aProperty, aAttribute, true),
aPrincipal);
switch (editActionData.GetEditAction()) {
case EditAction::eSetFontFamilyProperty:
MOZ_ASSERT(!aValue.IsVoid());
// XXX Should we trim unnecessary white-spaces?
editActionData.SetData(aValue);
break;
case EditAction::eSetColorProperty:
case EditAction::eSetBackgroundColorPropertyInline:
editActionData.SetColorData(aValue);
break;
default:
break;
}
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
// XXX Due to bug 1659276 and bug 1659924, we should not scroll selection
// into view after setting the new style.
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::No);
nsAtom* property = &aProperty;
nsAtom* attribute = aAttribute;
nsAutoString value(aValue);
if (&aProperty == nsGkAtoms::sup) {
// Superscript and Subscript styles are mutually exclusive.
nsresult rv = RemoveInlinePropertyInternal(nsGkAtoms::sub, nullptr,
RemoveRelatedElements::No);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::RemoveInlinePropertyInternal(nsGkAtoms::sub, "
"RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
} else if (&aProperty == nsGkAtoms::sub) {
// Superscript and Subscript styles are mutually exclusive.
nsresult rv = RemoveInlinePropertyInternal(nsGkAtoms::sup, nullptr,
RemoveRelatedElements::No);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::RemoveInlinePropertyInternal(nsGkAtoms::sup, "
"RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
}
// Handling `<tt>` element code was implemented for composer (bug 115922).
// This shouldn't work with `Document.execCommand()`. Currently, aPrincipal
// is set only when the root caller is Document::ExecCommand() so that
// we should handle `<tt>` element only when aPrincipal is nullptr that
// must be only when XUL command is executed on composer.
else if (!aPrincipal) {
if (&aProperty == nsGkAtoms::tt) {
nsresult rv = RemoveInlinePropertyInternal(
nsGkAtoms::font, nsGkAtoms::face, RemoveRelatedElements::No);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::RemoveInlinePropertyInternal(nsGkAtoms::font, "
"nsGkAtoms::face, RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
} else if (&aProperty == nsGkAtoms::font && aAttribute == nsGkAtoms::face) {
if (!value.LowerCaseEqualsASCII("tt")) {
nsresult rv = RemoveInlinePropertyInternal(nsGkAtoms::tt, nullptr,
RemoveRelatedElements::No);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::RemoveInlinePropertyInternal(nsGkAtoms::tt, "
"RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
} else {
nsresult rv = RemoveInlinePropertyInternal(
nsGkAtoms::font, nsGkAtoms::face, RemoveRelatedElements::No);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::RemoveInlinePropertyInternal(nsGkAtoms::font, "
"nsGkAtoms::face, RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
// Override property, attribute and value if the new font face value is
// "tt".
property = nsGkAtoms::tt;
attribute = nullptr;
value.Truncate();
}
}
}
rv = SetInlinePropertyInternal(MOZ_KnownLive(*property),
MOZ_KnownLive(attribute), value);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyInternal() failed");
return EditorBase::ToGenericNSResult(rv);
}
NS_IMETHODIMP HTMLEditor::SetInlineProperty(const nsAString& aProperty,
const nsAString& aAttribute,
const nsAString& aValue) {
RefPtr<nsAtom> property = NS_Atomize(aProperty);
if (NS_WARN_IF(!property)) {
return NS_ERROR_INVALID_ARG;
}
nsStaticAtom* attribute = EditorUtils::GetAttributeAtom(aAttribute);
AutoEditActionDataSetter editActionData(
*this,
HTMLEditUtils::GetEditActionForFormatText(*property, attribute, true));
switch (editActionData.GetEditAction()) {
case EditAction::eSetFontFamilyProperty:
MOZ_ASSERT(!aValue.IsVoid());
// XXX Should we trim unnecessary white-spaces?
editActionData.SetData(aValue);
break;
case EditAction::eSetColorProperty:
case EditAction::eSetBackgroundColorPropertyInline:
editActionData.SetColorData(aValue);
break;
default:
break;
}
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
rv = SetInlinePropertyInternal(*property, MOZ_KnownLive(attribute), aValue);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyInternal() failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::SetInlinePropertyInternal(
nsAtom& aProperty, nsAtom* aAttribute, const nsAString& aAttributeValue) {
MOZ_ASSERT(IsEditActionDataAvailable());
if (NS_WARN_IF(!mInitSucceeded)) {
return NS_ERROR_NOT_INITIALIZED;
}
DebugOnly<nsresult> rvIgnored = CommitComposition();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"EditorBase::CommitComposition() failed, but ignored");
if (SelectionRefPtr()->IsCollapsed()) {
// Manipulating text attributes on a collapsed selection only sets state
// for the next text insertion
mTypeInState->SetProp(&aProperty, aAttribute, aAttributeValue);
return NS_OK;
}
// XXX Shouldn't we return before calling `CommitComposition()`?
if (IsPlaintextEditor()) {
return NS_OK;
}
EditActionResult result = CanHandleHTMLEditSubAction();
if (result.Failed() || result.Canceled()) {
NS_WARNING_ASSERTION(result.Succeeded(),
"HTMLEditor::CanHandleHTMLEditSubAction() failed");
return result.Rv();
}
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::Yes);
IgnoredErrorResult ignoredError;
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eInsertElement, nsIEditor::eNext, ignoredError);
if (NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
return ignoredError.StealNSResult();
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
"HTMLEditor::OnStartToHandleTopLevelEditSubAction() failed, but ignored");
{
AutoSelectionRestorer restoreSelectionLater(*this);
AutoTransactionsConserveSelection dontChangeMySelection(*this);
// Loop through the ranges in the selection
// XXX This is different from `SetCSSBackgroundColorWithTransaction()`.
// It refers `Selection::GetRangeAt()` in each time. The result may
// be different if mutation event listener changes the `Selection`.
AutoSelectionRangeArray arrayOfRanges(SelectionRefPtr());
for (auto& range : arrayOfRanges.mRanges) {
// Adjust range to include any ancestors whose children are entirely
// selected
nsresult rv = PromoteInlineRange(*range);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::PromoteInlineRange() failed");
return rv;
}
// XXX Shouldn't we skip the range if it's been collapsed by mutation
// event listener?
EditorDOMPoint startOfRange(range->StartRef());
EditorDOMPoint endOfRange(range->EndRef());
if (NS_WARN_IF(!startOfRange.IsSet()) ||
NS_WARN_IF(!endOfRange.IsSet())) {
continue;
}
// If range is in a text node, apply new style simply.
if (startOfRange.GetContainer() == endOfRange.GetContainer() &&
startOfRange.IsInTextNode()) {
nsresult rv = SetInlinePropertyOnTextNode(
MOZ_KnownLive(*startOfRange.GetContainerAsText()),
startOfRange.Offset(), endOfRange.Offset(), aProperty, aAttribute,
aAttributeValue);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnTextNode() failed");
return rv;
}
continue;
}
// Collect editable nodes which are entirely contained in the range.
AutoTArray<OwningNonNull<nsIContent>, 64> arrayOfContents;
ContentSubtreeIterator subtreeIter;
// If there is no node which is entirely in the range,
// `ContentSubtreeIterator::Init()` fails, but this is possible case,
// don't warn it.
if (NS_SUCCEEDED(subtreeIter.Init(range))) {
for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
nsINode* node = subtreeIter.GetCurrentNode();
if (NS_WARN_IF(!node)) {
return NS_ERROR_FAILURE;
}
if (node->IsContent() && EditorUtils::IsEditableContent(
*node->AsContent(), EditorType::HTML)) {
arrayOfContents.AppendElement(*node->AsContent());
}
}
}
// If start node is a text node, apply new style to a part of it.
if (startOfRange.IsInTextNode() &&
EditorUtils::IsEditableContent(*startOfRange.ContainerAsText(),
EditorType::HTML)) {
nsresult rv = SetInlinePropertyOnTextNode(
MOZ_KnownLive(*startOfRange.GetContainerAsText()),
startOfRange.Offset(), startOfRange.GetContainer()->Length(),
aProperty, aAttribute, aAttributeValue);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnTextNode() failed");
return rv;
}
}
// Then, apply new style to all nodes in the range entirely.
for (auto& content : arrayOfContents) {
// MOZ_KnownLive because 'arrayOfContents' is guaranteed to
// keep it alive.
nsresult rv = SetInlinePropertyOnNode(
MOZ_KnownLive(*content), aProperty, aAttribute, aAttributeValue);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnNode() failed");
return rv;
}
}
// Finally, if end node is a text node, apply new style to a part of it.
if (endOfRange.IsInTextNode() &&
EditorUtils::IsEditableContent(*endOfRange.GetContainerAsText(),
EditorType::HTML)) {
nsresult rv = SetInlinePropertyOnTextNode(
MOZ_KnownLive(*endOfRange.GetContainerAsText()), 0,
endOfRange.Offset(), aProperty, aAttribute, aAttributeValue);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnNode() failed");
return rv;
}
}
}
}
// Restoring `Selection` may have destroyed us.
return NS_WARN_IF(Destroyed()) ? NS_ERROR_EDITOR_DESTROYED : NS_OK;
}
Result<bool, nsresult> HTMLEditor::ElementIsGoodContainerForTheStyle(
Element& aElement, nsAtom* aProperty, nsAtom* aAttribute,
const nsAString* aValue) {
// aContent can be null, in which case we'll return false in a few lines
MOZ_ASSERT(aProperty);
MOZ_ASSERT_IF(aAttribute, aValue);
// First check for <b>, <i>, etc.
if (aElement.IsHTMLElement(aProperty) && !aElement.GetAttrCount() &&
!aAttribute) {
return true;
}
// Special cases for various equivalencies: <strong>, <em>, <s>
if (!aElement.GetAttrCount() &&
((aProperty == nsGkAtoms::b &&
aElement.IsHTMLElement(nsGkAtoms::strong)) ||
(aProperty == nsGkAtoms::i && aElement.IsHTMLElement(nsGkAtoms::em)) ||
(aProperty == nsGkAtoms::strike &&
aElement.IsHTMLElement(nsGkAtoms::s)))) {
return true;
}
// Now look for things like <font>
if (aAttribute) {
nsString attrValue;
if (aElement.IsHTMLElement(aProperty) &&
IsOnlyAttribute(&aElement, aAttribute) &&
aElement.GetAttr(kNameSpaceID_None, aAttribute, attrValue) &&
attrValue.Equals(*aValue, nsCaseInsensitiveStringComparator)) {
// This is not quite correct, because it excludes cases like
// <font face=000> being the same as <font face=#000000>.
// Property-specific handling is needed (bug 760211).
return true;
}
}
// No luck so far. Now we check for a <span> with a single style=""
// attribute that sets only the style we're looking for, if this type of
// style supports it
if (!CSSEditUtils::IsCSSEditableProperty(&aElement, aProperty, aAttribute) ||
!aElement.IsHTMLElement(nsGkAtoms::span) ||
aElement.GetAttrCount() != 1 ||
!aElement.HasAttr(kNameSpaceID_None, nsGkAtoms::style)) {
return false;
}
// Some CSS styles are not so simple. For instance, underline is
// "text-decoration: underline", which decomposes into four different text-*
// properties. So for now, we just create a span, add the desired style, and
// see if it matches.
RefPtr<Element> newSpanElement = CreateHTMLContent(nsGkAtoms::span);
if (!newSpanElement) {
NS_WARNING("EditorBase::CreateHTMLContent(nsGkAtoms::span) failed");
return false;
}
nsStyledElement* styledNewSpanElement =
nsStyledElement::FromNode(newSpanElement);
if (!styledNewSpanElement) {
return false;
}
// MOZ_KnownLive(*styledNewSpanElement): It's newSpanElement whose type is
// RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithoutTransaction(
MOZ_KnownLive(*styledNewSpanElement), aProperty, aAttribute, aValue);
if (result.isErr()) {
// The call shouldn't return destroyed error because it must be
// impossible to run script with modifying the new orphan node.
MOZ_ASSERT_UNREACHABLE("How did you destroy this editor?");
if (NS_WARN_IF(result.inspectErr() == NS_ERROR_EDITOR_DESTROYED)) {
return Err(NS_ERROR_EDITOR_DESTROYED);
}
return false;
}
nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement);
if (!styledElement) {
return false;
}
return CSSEditUtils::DoStyledElementsHaveSameStyle(*styledNewSpanElement,
*styledElement);
}
nsresult HTMLEditor::SetInlinePropertyOnTextNode(
Text& aText, uint32_t aStartOffset, uint32_t aEndOffset, nsAtom& aProperty,
nsAtom* aAttribute, const nsAString& aValue) {
if (!aText.GetParentNode() ||
!HTMLEditUtils::CanNodeContain(*aText.GetParentNode(), aProperty)) {
return NS_OK;
}
// Don't need to do anything if no characters actually selected
if (aStartOffset == aEndOffset) {
return NS_OK;
}
// Don't need to do anything if property already set on node
if (CSSEditUtils::IsCSSEditableProperty(&aText, &aProperty, aAttribute)) {
// The HTML styles defined by aProperty/aAttribute have a CSS equivalence
// for node; let's check if it carries those CSS styles
nsAutoString value(aValue);
if (CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
aText, &aProperty, aAttribute, value)) {
return NS_OK;
}
} else if (IsTextPropertySetByContent(&aText, &aProperty, aAttribute,
&aValue)) {
return NS_OK;
}
// Make the range an independent node.
nsCOMPtr<nsIContent> textNodeForTheRange = &aText;
// Split at the end of the range.
EditorDOMPoint atEnd(textNodeForTheRange, aEndOffset);
if (!atEnd.IsEndOfContainer()) {
// We need to split off back of text node
ErrorResult error;
textNodeForTheRange = SplitNodeWithTransaction(atEnd, error);
if (NS_WARN_IF(Destroyed())) {
error = NS_ERROR_EDITOR_DESTROYED;
}
if (error.Failed()) {
NS_WARNING_ASSERTION(error.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED),
"HTMLEditor::SplitNodeWithTransaction() failed");
return error.StealNSResult();
}
}
// Split at the start of the range.
EditorDOMPoint atStart(textNodeForTheRange, aStartOffset);
if (!atStart.IsStartOfContainer()) {
// We need to split off front of text node
ErrorResult error;
nsCOMPtr<nsIContent> newLeftNode = SplitNodeWithTransaction(atStart, error);
if (NS_WARN_IF(Destroyed())) {
error = NS_ERROR_EDITOR_DESTROYED;
}
if (error.Failed()) {
NS_WARNING_ASSERTION(error.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED),
"HTMLEditor::SplitNodeWithTransaction() failed");
return error.StealNSResult();
}
Unused << newLeftNode;
}
if (aAttribute) {
// Look for siblings that are correct type of node
nsIContent* sibling = GetPriorHTMLSibling(textNodeForTheRange);
if (sibling && sibling->IsElement()) {
OwningNonNull<Element> element(*sibling->AsElement());
Result<bool, nsresult> result = ElementIsGoodContainerForTheStyle(
element, &aProperty, aAttribute, &aValue);
if (result.isErr()) {
NS_WARNING("HTMLEditor::ElementIsGoodContainerForTheStyle() failed");
return result.unwrapErr();
}
if (result.inspect()) {
// Previous sib is already right kind of inline node; slide this over
nsresult rv =
MoveNodeToEndWithTransaction(*textNodeForTheRange, element);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeToEndWithTransaction() failed");
return rv;
}
}
sibling = GetNextHTMLSibling(textNodeForTheRange);
if (sibling && sibling->IsElement()) {
OwningNonNull<Element> element(*sibling->AsElement());
Result<bool, nsresult> result = ElementIsGoodContainerForTheStyle(
element, &aProperty, aAttribute, &aValue);
if (result.isErr()) {
NS_WARNING("HTMLEditor::ElementIsGoodContainerForTheStyle() failed");
return result.unwrapErr();
}
if (result.inspect()) {
// Following sib is already right kind of inline node; slide this over
nsresult rv = MoveNodeWithTransaction(*textNodeForTheRange,
EditorDOMPoint(sibling, 0));
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeWithTransaction() failed");
return rv;
}
}
}
// Reparent the node inside inline node with appropriate {attribute,value}
nsresult rv = SetInlinePropertyOnNode(*textNodeForTheRange, aProperty,
aAttribute, aValue);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyOnNode() failed");
return rv;
}
nsresult HTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aContent,
nsAtom& aProperty,
nsAtom* aAttribute,
const nsAString& aValue) {
// If this is an element that can't be contained in a span, we have to
// recurse to its children.
if (!HTMLEditUtils::CanNodeContain(*nsGkAtoms::span, aContent)) {
if (aContent.HasChildren()) {
nsTArray<OwningNonNull<nsIContent>> arrayOfNodes;
// Populate the list.
for (nsCOMPtr<nsIContent> child = aContent.GetFirstChild(); child;
child = child->GetNextSibling()) {
if (EditorUtils::IsEditableContent(*child, EditorType::HTML) &&
!IsEmptyTextNode(*child)) {
arrayOfNodes.AppendElement(*child);
}
}
// Then loop through the list, set the property on each node.
for (auto& node : arrayOfNodes) {
// MOZ_KnownLive because 'arrayOfNodes' is guaranteed to
// keep it alive.
nsresult rv = SetInlinePropertyOnNode(MOZ_KnownLive(node), aProperty,
aAttribute, aValue);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnNode() failed");
return rv;
}
}
}
return NS_OK;
}
// First check if there's an adjacent sibling we can put our node into.
nsCOMPtr<nsIContent> previousSibling = GetPriorHTMLSibling(&aContent);
nsCOMPtr<nsIContent> nextSibling = GetNextHTMLSibling(&aContent);
if (previousSibling && previousSibling->IsElement()) {
OwningNonNull<Element> previousElement(*previousSibling->AsElement());
Result<bool, nsresult> canMoveIntoPreviousSibling =
ElementIsGoodContainerForTheStyle(previousElement, &aProperty,
aAttribute, &aValue);
if (canMoveIntoPreviousSibling.isErr()) {
NS_WARNING("HTMLEditor::ElementIsGoodContainerForTheStyle() failed");
return canMoveIntoPreviousSibling.unwrapErr();
}
if (canMoveIntoPreviousSibling.inspect()) {
nsresult rv = MoveNodeToEndWithTransaction(aContent, *previousSibling);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::MoveNodeToEndWithTransaction() failed");
return rv;
}
if (!nextSibling || !nextSibling->IsElement()) {
return NS_OK;
}
OwningNonNull<Element> nextElement(*nextSibling->AsElement());
Result<bool, nsresult> canMoveIntoNextSibling =
ElementIsGoodContainerForTheStyle(nextElement, &aProperty, aAttribute,
&aValue);
if (canMoveIntoNextSibling.isErr()) {
NS_WARNING("HTMLEditor::ElementIsGoodContainerForTheStyle() failed");
return canMoveIntoNextSibling.unwrapErr();
}
if (!canMoveIntoNextSibling.inspect()) {
return NS_OK;
}
rv = JoinNodesWithTransaction(*previousSibling, *nextSibling);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::JoinNodesWithTransaction() failed");
return rv;
}
}
if (nextSibling && nextSibling->IsElement()) {
OwningNonNull<Element> nextElement(*nextSibling->AsElement());
Result<bool, nsresult> canMoveIntoNextSibling =
ElementIsGoodContainerForTheStyle(nextElement, &aProperty, aAttribute,
&aValue);
if (canMoveIntoNextSibling.isErr()) {
NS_WARNING("HTMLEditor::ElementIsGoodContainerForTheStyle() failed");
return canMoveIntoNextSibling.unwrapErr();
}
if (canMoveIntoNextSibling.inspect()) {
nsresult rv =
MoveNodeWithTransaction(aContent, EditorDOMPoint(nextElement, 0));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeWithTransaction() failed");
return rv;
}
}
// Don't need to do anything if property already set on node
if (CSSEditUtils::IsCSSEditableProperty(&aContent, &aProperty, aAttribute)) {
nsAutoString value(aValue);
if (CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
aContent, &aProperty, aAttribute, value)) {
return NS_OK;
}
} else if (IsTextPropertySetByContent(&aContent, &aProperty, aAttribute,
&aValue)) {
return NS_OK;
}
bool useCSS = (IsCSSEnabled() && CSSEditUtils::IsCSSEditableProperty(
&aContent, &aProperty, aAttribute)) ||
// bgcolor is always done using CSS
aAttribute == nsGkAtoms::bgcolor ||
// called for removing parent style, we should use CSS with
// `<span>` element.
aValue.EqualsLiteral("-moz-editor-invert-value");
if (useCSS) {
RefPtr<Element> spanElement;
// We only add style="" to <span>s with no attributes (bug 746515). If we
// don't have one, we need to make one.
if (aContent.IsHTMLElement(nsGkAtoms::span) &&
!aContent.AsElement()->GetAttrCount()) {
spanElement = aContent.AsElement();
} else {
spanElement = InsertContainerWithTransaction(aContent, *nsGkAtoms::span);
if (!spanElement) {
NS_WARNING(
"HTMLEditor::InsertContainerWithTransaction(nsGkAtoms::span) "
"failed");
return NS_ERROR_FAILURE;
}
}
// Add the CSS styles corresponding to the HTML style request
if (nsStyledElement* spanStyledElement =
nsStyledElement::FromNode(spanElement)) {
// MOZ_KnownLive(*spanStyledElement): It's spanElement whose type is
// RefPtr.
Result<int32_t, nsresult> result =
mCSSEditUtils->SetCSSEquivalentToHTMLStyleWithTransaction(
MOZ_KnownLive(*spanStyledElement), &aProperty, aAttribute,
&aValue);
if (result.isErr()) {
if (result.inspectErr() == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::SetCSSEquivalentToHTMLStyleWithTransaction() "
"destroyed the editor");
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING(
"CSSEditUtils::SetCSSEquivalentToHTMLStyleWithTransaction() "
"failed, "
"but ignored");
}
}
return NS_OK;
}
// is it already the right kind of node, but with wrong attribute?
if (aContent.IsHTMLElement(&aProperty)) {
if (NS_WARN_IF(!aAttribute)) {
return NS_ERROR_INVALID_ARG;
}
// Just set the attribute on it.
nsresult rv = SetAttributeWithTransaction(
MOZ_KnownLive(*aContent.AsElement()), *aAttribute, aValue);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::SetAttributeWithTransaction() failed");
return rv;
}
// ok, chuck it in its very own container
RefPtr<Element> newContainerElement = InsertContainerWithTransaction(
aContent, aProperty, aAttribute ? *aAttribute : *nsGkAtoms::_empty,
aValue);
NS_WARNING_ASSERTION(newContainerElement,
"HTMLEditor::InsertContainerWithTransaction() failed");
return newContainerElement ? NS_OK : NS_ERROR_FAILURE;
}
nsresult HTMLEditor::SetInlinePropertyOnNode(nsIContent& aNode,
nsAtom& aProperty,
nsAtom* aAttribute,
const nsAString& aValue) {
nsCOMPtr<nsIContent> previousSibling = aNode.GetPreviousSibling(),
nextSibling = aNode.GetNextSibling();
if (NS_WARN_IF(!aNode.GetParentNode())) {
return NS_ERROR_INVALID_ARG;
}
OwningNonNull<nsINode> parent = *aNode.GetParentNode();
if (aNode.IsElement()) {
nsresult rv = RemoveStyleInside(MOZ_KnownLive(*aNode.AsElement()),
&aProperty, aAttribute);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveStyleInside() failed");
return rv;
}
}
if (aNode.GetParentNode()) {
// The node is still where it was
nsresult rv =
SetInlinePropertyOnNodeImpl(aNode, aProperty, aAttribute, aValue);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyOnNodeImpl() failed");
return rv;
}
// It's vanished. Use the old siblings for reference to construct a
// list. But first, verify that the previous/next siblings are still
// where we expect them; otherwise we have to give up.
if (NS_WARN_IF(previousSibling &&
previousSibling->GetParentNode() != parent) ||
NS_WARN_IF(nextSibling && nextSibling->GetParentNode() != parent)) {
return NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE;
}
AutoTArray<OwningNonNull<nsIContent>, 24> nodesToSet;
for (nsIContent* content = previousSibling ? previousSibling->GetNextSibling()
: parent->GetFirstChild();
content && content != nextSibling; content = content->GetNextSibling()) {
if (EditorUtils::IsEditableContent(*content, EditorType::HTML)) {
nodesToSet.AppendElement(*content);
}
}
for (OwningNonNull<nsIContent>& content : nodesToSet) {
// MOZ_KnownLive because 'nodesToSet' is guaranteed to
// keep it alive.
nsresult rv = SetInlinePropertyOnNodeImpl(MOZ_KnownLive(content), aProperty,
aAttribute, aValue);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::SetInlinePropertyOnNodeImpl() failed");
return rv;
}
}
return NS_OK;
}
SplitRangeOffResult HTMLEditor::SplitAncestorStyledInlineElementsAtRangeEdges(
const EditorDOMPoint& aStartOfRange, const EditorDOMPoint& aEndOfRange,
nsAtom* aProperty, nsAtom* aAttribute) {
MOZ_ASSERT(IsEditActionDataAvailable());
if (NS_WARN_IF(!aStartOfRange.IsSet()) || NS_WARN_IF(!aEndOfRange.IsSet())) {
return SplitRangeOffResult(NS_ERROR_INVALID_ARG);
}
EditorDOMPoint startOfRange(aStartOfRange);
EditorDOMPoint endOfRange(aEndOfRange);
// split any matching style nodes above the start of range
SplitNodeResult resultAtStart(NS_ERROR_NOT_INITIALIZED);
{
AutoTrackDOMPoint tracker(RangeUpdaterRef(), &endOfRange);
resultAtStart = SplitAncestorStyledInlineElementsAt(startOfRange, aProperty,
aAttribute);
if (resultAtStart.Failed()) {
NS_WARNING("HTMLEditor::SplitAncestorStyledInlineElementsAt() failed");
return SplitRangeOffResult(resultAtStart.Rv());
}
if (resultAtStart.Handled()) {
startOfRange = resultAtStart.SplitPoint();
if (!startOfRange.IsSet()) {
NS_WARNING(
"HTMLEditor::SplitAncestorStyledInlineElementsAt() didn't return "
"split point");
return SplitRangeOffResult(NS_ERROR_FAILURE);
}
}
}
// second verse, same as the first...
SplitNodeResult resultAtEnd(NS_ERROR_NOT_INITIALIZED);
{
AutoTrackDOMPoint tracker(RangeUpdaterRef(), &startOfRange);
resultAtEnd =
SplitAncestorStyledInlineElementsAt(endOfRange, aProperty, aAttribute);
if (resultAtEnd.Failed()) {
NS_WARNING("HTMLEditor::SplitAncestorStyledInlineElementsAt() failed");
return SplitRangeOffResult(resultAtEnd.Rv());
}
if (resultAtEnd.Handled()) {
endOfRange = resultAtEnd.SplitPoint();
if (!endOfRange.IsSet()) {
NS_WARNING(
"HTMLEditor::SplitAncestorStyledInlineElementsAt() didn't return "
"split point");
return SplitRangeOffResult(NS_ERROR_FAILURE);
}
}
}
return SplitRangeOffResult(startOfRange, resultAtStart, endOfRange,
resultAtEnd);
}
SplitNodeResult HTMLEditor::SplitAncestorStyledInlineElementsAt(
const EditorDOMPoint& aPointToSplit, nsAtom* aProperty,
nsAtom* aAttribute) {
if (NS_WARN_IF(!aPointToSplit.IsSet()) ||
NS_WARN_IF(!aPointToSplit.GetContainerAsContent())) {
return SplitNodeResult(NS_ERROR_INVALID_ARG);
}
// We assume that this method is called only when we're removing style(s).
// Even if we're in HTML mode and there is no presentation element in the
// block, we may need to overwrite the block's style with `<span>` element
// and CSS. For example, `<h1>` element has `font-weight: bold;` as its
// default style. If `Document.execCommand("bold")` is called for its
// text, we should make it unbold. Therefore, we shouldn't check
// IsCSSEnabled() in most cases. However, there is an exception.
// FontFaceStateCommand::SetState() calls RemoveInlinePropertyAsAction()
// with nsGkAtoms::tt before calling SetInlinePropertyAsAction() if we
// are handling a XUL command. Only in that case, we need to check
// IsCSSEnabled().
bool useCSS = aProperty != nsGkAtoms::tt || IsCSSEnabled();
AutoTArray<OwningNonNull<nsIContent>, 24> arrayOfParents;
for (nsIContent* content :
aPointToSplit.GetContainer()->InclusiveAncestorsOfType<nsIContent>()) {
if (HTMLEditUtils::IsBlockElement(*content) || !content->GetParent() ||
!EditorUtils::IsEditableContent(*content->GetParent(),
EditorType::HTML)) {
break;
}
arrayOfParents.AppendElement(*content);
}
// Split any matching style nodes above the point.
SplitNodeResult result(aPointToSplit);
MOZ_ASSERT(!result.Handled());
for (OwningNonNull<nsIContent>& content : arrayOfParents) {
bool isSetByCSS = false;
if (useCSS &&
CSSEditUtils::IsCSSEditableProperty(content, aProperty, aAttribute)) {
// The HTML style defined by aProperty/aAttribute has a CSS equivalence
// in this implementation for the node; let's check if it carries those
// CSS styles
nsAutoString firstValue;
isSetByCSS = CSSEditUtils::IsSpecifiedCSSEquivalentToHTMLInlineStyleSet(
*content, aProperty, aAttribute, firstValue);
}
if (!isSetByCSS) {
if (!content->IsElement()) {
continue;
}
// If aProperty is set, we need to split only elements which applies the
// given style.
if (aProperty) {
// If the content is an inline element represents aProperty or
// the content is a link element and aProperty is `href`, we should
// split the content.
if (!content->IsHTMLElement(aProperty) &&
!(aProperty == nsGkAtoms::href && HTMLEditUtils::IsLink(content))) {
continue;
}
}
// If aProperty is nullptr, we need to split any style.
else if (!EditorUtils::IsEditableContent(content, EditorType::HTML) ||
!HTMLEditUtils::IsRemovableInlineStyleElement(
*content->AsElement())) {
continue;
}
}
// Found a style node we need to split.
// XXX If first content is a text node and CSS is enabled, we call this
// with text node but in such case, this does nothing, but returns
// as handled with setting only previous or next node. If its parent
// is a block, we do nothing but return as handled.
SplitNodeResult splitNodeResult = SplitNodeDeepWithTransaction(
MOZ_KnownLive(content), result.SplitPoint(),
SplitAtEdges::eAllowToCreateEmptyContainer);
if (splitNodeResult.Failed()) {
NS_WARNING("HTMLEditor::SplitNodeDeepWithTransaction() failed");
return splitNodeResult;
}
MOZ_ASSERT(splitNodeResult.Handled());
// Mark the final result as handled forcibly.
result = SplitNodeResult(splitNodeResult.GetPreviousNode(),
splitNodeResult.GetNextNode());
MOZ_ASSERT(result.Handled());
}
return result;
}
EditResult HTMLEditor::ClearStyleAt(const EditorDOMPoint& aPoint,
nsAtom* aProperty, nsAtom* aAttribute) {
MOZ_ASSERT(IsEditActionDataAvailable());
if (NS_WARN_IF(!aPoint.IsSet())) {
return EditResult(NS_ERROR_INVALID_ARG);
}
// First, split inline elements at the point.
// E.g., if aProperty is nsGkAtoms::b and `<p><b><i>a[]bc</i></b></p>`,
// we want to make it as `<p><b><i>a</i></b><b><i>bc</i></b></p>`.
SplitNodeResult splitResult =
SplitAncestorStyledInlineElementsAt(aPoint, aProperty, aAttribute);
if (splitResult.Failed()) {
NS_WARNING("HTMLEditor::SplitAncestorStyledInlineElementsAt() failed");
return EditResult(splitResult.Rv());
}
// If there is no styled inline elements of aProperty/aAttribute, we just
// return the given point.
// E.g., `<p><i>a[]bc</i></p>` for nsGkAtoms::b.
if (!splitResult.Handled()) {
return EditResult(aPoint);
}
// If it did split nodes, but topmost ancestor inline element is split
// at start of it, we don't need the empty inline element. Let's remove
// it now.
if (splitResult.GetPreviousNode() &&
IsEmptyNode(*splitResult.GetPreviousNode(), false, true)) {
// Delete previous node if it's empty.
nsresult rv = DeleteNodeWithTransaction(
MOZ_KnownLive(*splitResult.GetPreviousNode()));
if (NS_WARN_IF(Destroyed())) {
return EditResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteNodeWithTransaction() failed");
return EditResult(rv);
}
}
// If we reached block from end of a text node, we can do nothing here.
// E.g., `<p style="font-weight: bold;">a[]bc</p>` for nsGkAtoms::b and
// we're in CSS mode.
// XXX Chrome resets block style and creates `<span>` elements for each
// line in this case.
if (!splitResult.GetNextNode()) {
MOZ_ASSERT(IsCSSEnabled());
return EditResult(aPoint);
}
// Otherwise, the next node is topmost ancestor inline element which has
// the style. We want to put caret between the split nodes, but we need
// to keep other styles. Therefore, next, we need to split at start of
// the next node. The first example should become
// `<p><b><i>a</i></b><b><i></i></b><b><i>bc</i></b></p>`.
// ^^^^^^^^^^^^^^
nsIContent* firstLeafChildOfNextNode = HTMLEditUtils::GetFirstLeafChild(
*splitResult.GetNextNode(), ChildBlockBoundary::Ignore);
EditorDOMPoint atStartOfNextNode(firstLeafChildOfNextNode
? firstLeafChildOfNextNode
: splitResult.GetNextNode(),
0);
RefPtr<HTMLBRElement> brElement;
// But don't try to split non-containers like `<br>`, `<hr>` and `<img>`
// element.
if (!atStartOfNextNode.IsInContentNode() ||
!HTMLEditUtils::IsContainerNode(
*atStartOfNextNode.ContainerAsContent())) {
// If it's a `<br>` element, let's move it into new node later.
brElement = HTMLBRElement::FromNode(atStartOfNextNode.GetContainer());
if (!atStartOfNextNode.GetContainerParentAsContent()) {
NS_WARNING("atStartOfNextNode was in an orphan node");
return EditResult(NS_ERROR_FAILURE);
}
atStartOfNextNode.Set(atStartOfNextNode.GetContainerParent(), 0);
}
SplitNodeResult splitResultAtStartOfNextNode =
SplitAncestorStyledInlineElementsAt(atStartOfNextNode, aProperty,
aAttribute);
if (splitResultAtStartOfNextNode.Failed()) {
NS_WARNING("HTMLEditor::SplitAncestorStyledInlineElementsAt() failed");
return EditResult(splitResultAtStartOfNextNode.Rv());
}
// Let's remove the next node if it becomes empty by splitting it.
// XXX Is this possible case without mutation event listener?
if (splitResultAtStartOfNextNode.Handled() &&
splitResultAtStartOfNextNode.GetNextNode() &&
IsEmptyNode(*splitResultAtStartOfNextNode.GetNextNode(), false, true)) {
// Delete next node if it's empty.
nsresult rv = DeleteNodeWithTransaction(
MOZ_KnownLive(*splitResultAtStartOfNextNode.GetNextNode()));
if (NS_WARN_IF(Destroyed())) {
return EditResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteNodeWithTransaction() failed");
return EditResult(rv);
}
}
// If there is no content, we should return here.
// XXX Is this possible case without mutation event listener?
if (NS_WARN_IF(!splitResultAtStartOfNextNode.Handled()) ||
!splitResultAtStartOfNextNode.GetPreviousNode()) {
// XXX This is really odd, but we retrun this value...
return EditResult(
EditorDOMPoint(splitResult.SplitPoint().GetContainer(),
splitResultAtStartOfNextNode.SplitPoint().Offset()));
}
// Now, we want to put `<br>` element into the empty split node if
// it was in next node of the first split.
// E.g., `<p><b><i>a</i></b><b><i><br></i></b><b><i>bc</i></b></p>`
nsIContent* firstLeafChildOfPreviousNode = HTMLEditUtils::GetFirstLeafChild(
*splitResultAtStartOfNextNode.GetPreviousNode(),
ChildBlockBoundary::Ignore);
EditorDOMPoint pointToPutCaret(
firstLeafChildOfPreviousNode
? firstLeafChildOfPreviousNode
: splitResultAtStartOfNextNode.GetPreviousNode(),
0);
// If the right node starts with a `<br>`, suck it out of right node and into
// the left node left node. This is so we you don't revert back to the
// previous style if you happen to click at the end of a line.
if (brElement) {
nsresult rv = MoveNodeWithTransaction(*brElement, pointToPutCaret);
if (NS_WARN_IF(Destroyed())) {
return EditResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::MoveNodeWithTransaction() failed");
return EditResult(rv);
}
// Update the child.
pointToPutCaret.Set(pointToPutCaret.GetContainer(), 0);
}
// Finally, remove the specified style in the previous node at the
// second split and tells good insertion point to the caller. I.e., we
// want to make the first example as:
// `<p><b><i>a</i></b><i>[]</i><b><i>bc</i></b></p>`
// ^^^^^^^^^
if (splitResultAtStartOfNextNode.GetPreviousNode()->IsElement()) {
// Track the point at the new hierarchy. This is so we can know where
// to put the selection after we call RemoveStyleInside().
// RemoveStyleInside() could remove any and all of those nodes, so I
// have to use the range tracking system to find the right spot to put
// selection.
AutoTrackDOMPoint tracker(RangeUpdaterRef(), &pointToPutCaret);
nsresult rv = RemoveStyleInside(
MOZ_KnownLive(
*splitResultAtStartOfNextNode.GetPreviousNode()->AsElement()),
aProperty, aAttribute);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveStyleInside() failed");
return EditResult(rv);
}
}
return EditResult(pointToPutCaret);
}
nsresult HTMLEditor::RemoveStyleInside(Element& aElement, nsAtom* aProperty,
nsAtom* aAttribute) {
// First, handle all descendants.
RefPtr<nsIContent> child = aElement.GetFirstChild();
while (child) {
// cache next sibling since we might remove child
// XXX Well, the next sibling is moved from `aElement`, shouldn't we skip
// it here?
nsCOMPtr<nsIContent> nextSibling = child->GetNextSibling();
if (child->IsElement()) {
nsresult rv = RemoveStyleInside(MOZ_KnownLive(*child->AsElement()),
aProperty, aAttribute);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveStyleInside() failed");
return rv;
}
}
child = ToRefPtr(std::move(nextSibling));
}
// Next, remove the element or its attribute.
bool removeHTMLStyle = false;
if (aProperty) {
removeHTMLStyle =
// If the element is a presentation element of aProperty
aElement.NodeInfo()->NameAtom() == aProperty ||
// or an `<a>` element with `href` attribute
(aProperty == nsGkAtoms::href && HTMLEditUtils::IsLink(&aElement)) ||
// or an `<a>` element with `name` attribute
(aProperty == nsGkAtoms::name &&
HTMLEditUtils::IsNamedAnchor(&aElement));
}
// XXX Why do we check if aElement is editable only when aProperty is
// nullptr?
else if (EditorUtils::IsEditableContent(aElement, EditorType::HTML)) {
// or removing all styles and the element is a presentation element.
removeHTMLStyle = HTMLEditUtils::IsRemovableInlineStyleElement(aElement);
}
if (removeHTMLStyle) {
// If aAttribute is nullptr, we want to remove any matching inline styles
// entirely.
if (!aAttribute) {
// If some style rules are specified to aElement, we need to keep them
// as far as possible.
// XXX Why don't we clone `id` attribute?
if (aProperty &&
(aElement.HasAttr(kNameSpaceID_None, nsGkAtoms::style) ||
aElement.HasAttr(kNameSpaceID_None, nsGkAtoms::_class))) {
// Move `style` attribute and `class` element to span element before
// removing aElement from the tree.
RefPtr<Element> spanElement =
InsertContainerWithTransaction(aElement, *nsGkAtoms::span);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (!spanElement) {
NS_WARNING(
"HTMLEditor::InsertContainerWithTransaction(nsGkAtoms::span) "
"failed");
return NS_ERROR_FAILURE;
}
nsresult rv = CloneAttributeWithTransaction(*nsGkAtoms::style,
*spanElement, aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING(
"EditorBase::CloneAttributeWithTransaction(nsGkAtoms::style) "
"failed");
return rv;
}
rv = CloneAttributeWithTransaction(*nsGkAtoms::_class, *spanElement,
aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING(
"EditorBase::CloneAttributeWithTransaction(nsGkAtoms::_class) "
"failed");
return rv;
}
}
nsresult rv = RemoveContainerWithTransaction(aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveContainerWithTransaction() failed");
return rv;
}
}
// If aAttribute is specified, we want to remove only the attribute
// unless it's the last attribute of aElement.
else if (aElement.HasAttr(kNameSpaceID_None, aAttribute)) {
if (IsOnlyAttribute(&aElement, aAttribute)) {
nsresult rv = RemoveContainerWithTransaction(aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveContainerWithTransaction() failed");
return rv;
}
} else {
nsresult rv = RemoveAttributeWithTransaction(aElement, *aAttribute);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::RemoveAttributeWithTransaction() failed");
return rv;
}
}
}
}
// Then, remove CSS style if specified.
// XXX aElement may have already been removed from the DOM tree. Why
// do we keep handling aElement here??
if (CSSEditUtils::IsCSSEditableProperty(&aElement, aProperty, aAttribute) &&
CSSEditUtils::HaveSpecifiedCSSEquivalentStyles(aElement, aProperty,
aAttribute)) {
if (nsStyledElement* styledElement = nsStyledElement::FromNode(&aElement)) {
// If aElement has CSS declaration of the given style, remove it.
// MOZ_KnownLive(*styledElement): It's aElement and its lifetime must be
// guaranteed by the caller because of MOZ_CAN_RUN_SCRIPT method.
nsresult rv =
mCSSEditUtils->RemoveCSSEquivalentToHTMLStyleWithTransaction(
MOZ_KnownLive(*styledElement), aProperty, aAttribute, nullptr);
if (rv == NS_ERROR_EDITOR_DESTROYED) {
NS_WARNING(
"CSSEditUtils::RemoveCSSEquivalentToHTMLStyleWithTransaction() "
"destroyed the editor");
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"CSSEditUtils::RemoveCSSEquivalentToHTMLStyleWithTransaction() "
"failed, but ignored");
}
// Additionally, remove aElement itself if it's a `<span>` or `<font>`
// and it does not have non-empty `style`, `id` nor `class` attribute.
if (aElement.IsAnyOfHTMLElements(nsGkAtoms::span, nsGkAtoms::font) &&
!HTMLEditor::HasStyleOrIdOrClassAttribute(aElement)) {
DebugOnly<nsresult> rvIgnored = RemoveContainerWithTransaction(aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"HTMLEditor::RemoveContainerWithTransaction() failed, but ignored");
}
}
// Finally, remove aElement if it's a `<big>` or `<small>` element and
// we're removing `<font size>`.
if (aProperty == nsGkAtoms::font && aAttribute == nsGkAtoms::size &&
aElement.IsAnyOfHTMLElements(nsGkAtoms::big, nsGkAtoms::small)) {
nsresult rv = RemoveContainerWithTransaction(aElement);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveContainerWithTransaction() failed");
return rv;
}
return NS_OK;
}
bool HTMLEditor::IsOnlyAttribute(const Element* aElement, nsAtom* aAttribute) {
MOZ_ASSERT(aElement);
uint32_t attrCount = aElement->GetAttrCount();
for (uint32_t i = 0; i < attrCount; ++i) {
const nsAttrName* name = aElement->GetAttrNameAt(i);
if (!name->NamespaceEquals(kNameSpaceID_None)) {
return false;
}
// if it's the attribute we know about, or a special _moz attribute,
// keep looking
if (name->LocalName() != aAttribute) {
nsAutoString attrString;
name->LocalName()->ToString(attrString);
if (!StringBeginsWith(attrString, u"_moz"_ns)) {
return false;
}
}
}
// if we made it through all of them without finding a real attribute
// other than aAttribute, then return true
return true;
}
nsresult HTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange& aRange) {
// We assume that <a> is not nested.
// XXX Shouldn't ignore the editing host.
if (NS_WARN_IF(!aRange.GetStartContainer()) ||
NS_WARN_IF(!aRange.GetEndContainer())) {
return NS_ERROR_INVALID_ARG;
}
EditorRawDOMPoint newRangeStart(aRange.StartRef());
for (Element* element :
aRange.GetStartContainer()->InclusiveAncestorsOfType<Element>()) {
if (element->IsHTMLElement(nsGkAtoms::body)) {
break;
}
if (!HTMLEditUtils::IsNamedAnchor(element)) {
continue;
}
newRangeStart.Set(element);
break;
}
if (!newRangeStart.GetContainerAsContent()) {
NS_WARNING(
"HTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor() reached root "
"element from start container");
return NS_ERROR_FAILURE;
}
EditorRawDOMPoint newRangeEnd(aRange.EndRef());
for (Element* element :
aRange.GetEndContainer()->InclusiveAncestorsOfType<Element>()) {
if (element->IsHTMLElement(nsGkAtoms::body)) {
break;
}
if (!HTMLEditUtils::IsNamedAnchor(element)) {
continue;
}
newRangeEnd.SetAfter(element);
break;
}
if (!newRangeEnd.GetContainerAsContent()) {
NS_WARNING(
"HTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor() reached root "
"element from end container");
return NS_ERROR_FAILURE;
}
if (newRangeStart == aRange.StartRef() && newRangeEnd == aRange.EndRef()) {
return NS_OK;
}
nsresult rv = aRange.SetStartAndEnd(newRangeStart.ToRawRangeBoundary(),
newRangeEnd.ToRawRangeBoundary());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "nsRange::SetStartAndEnd() failed");
return rv;
}
nsresult HTMLEditor::PromoteInlineRange(nsRange& aRange) {
if (NS_WARN_IF(!aRange.GetStartContainer()) ||
NS_WARN_IF(!aRange.GetEndContainer())) {
return NS_ERROR_INVALID_ARG;
}
EditorRawDOMPoint newRangeStart(aRange.StartRef());
for (nsIContent* content :
aRange.GetStartContainer()->InclusiveAncestorsOfType<nsIContent>()) {
MOZ_ASSERT(newRangeStart.GetContainer() == content);
if (content->IsHTMLElement(nsGkAtoms::body) ||
!EditorUtils::IsEditableContent(*content, EditorType::HTML) ||
!IsStartOfContainerOrBeforeFirstEditableChild(newRangeStart)) {
break;
}
newRangeStart.Set(content);
}
if (!newRangeStart.GetContainerAsContent()) {
NS_WARNING(
"HTMLEditor::PromoteInlineRange() reached root element from start "
"container");
return NS_ERROR_FAILURE;
}
EditorRawDOMPoint newRangeEnd(aRange.EndRef());
for (nsIContent* content :
aRange.GetEndContainer()->InclusiveAncestorsOfType<nsIContent>()) {
MOZ_ASSERT(newRangeEnd.GetContainer() == content);
if (content->IsHTMLElement(nsGkAtoms::body) ||
!EditorUtils::IsEditableContent(*content, EditorType::HTML) ||
!IsEndOfContainerOrEqualsOrAfterLastEditableChild(newRangeEnd)) {
break;
}
newRangeEnd.SetAfter(content);
}
if (!newRangeEnd.GetContainerAsContent()) {
NS_WARNING(
"HTMLEditor::PromoteInlineRange() reached root element from end "
"container");
return NS_ERROR_FAILURE;
}
if (newRangeStart == aRange.StartRef() && newRangeEnd == aRange.EndRef()) {
return NS_OK;
}
nsresult rv = aRange.SetStartAndEnd(newRangeStart.ToRawRangeBoundary(),
newRangeEnd.ToRawRangeBoundary());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "nsRange::SetStartAndEnd() failed");
return rv;
}
bool HTMLEditor::IsStartOfContainerOrBeforeFirstEditableChild(
const EditorRawDOMPoint& aPoint) const {
MOZ_ASSERT(aPoint.IsSet());
if (aPoint.IsStartOfContainer()) {
return true;
}
if (aPoint.IsInTextNode()) {
return false;
}
nsIContent* firstEditableChild =
GetFirstEditableChild(*aPoint.GetContainer());
if (!firstEditableChild) {
return true;
}
return EditorRawDOMPoint(firstEditableChild).Offset() >= aPoint.Offset();
}
bool HTMLEditor::IsEndOfContainerOrEqualsOrAfterLastEditableChild(
const EditorRawDOMPoint& aPoint) const {
MOZ_ASSERT(aPoint.IsSet());
if (aPoint.IsEndOfContainer()) {
return true;
}
if (aPoint.IsInTextNode()) {
return false;
}
nsIContent* lastEditableChild = GetLastEditableChild(*aPoint.GetContainer());
if (!lastEditableChild) {
return true;
}
return EditorRawDOMPoint(lastEditableChild).Offset() < aPoint.Offset();
}
nsresult HTMLEditor::GetInlinePropertyBase(nsAtom& aHTMLProperty,
nsAtom* aAttribute,
const nsAString* aValue,
bool* aFirst, bool* aAny, bool* aAll,
nsAString* outValue) const {
MOZ_ASSERT(IsEditActionDataAvailable());
*aAny = false;
*aAll = true;
*aFirst = false;
bool first = true;
bool isCollapsed = SelectionRefPtr()->IsCollapsed();
RefPtr<nsRange> range = SelectionRefPtr()->GetRangeAt(0);
// XXX: Should be a while loop, to get each separate range
// XXX: ERROR_HANDLING can currentItem be null?
if (range) {
// For each range, set a flag
bool firstNodeInRange = true;
if (isCollapsed) {
nsCOMPtr<nsINode> collapsedNode = range->GetStartContainer();
if (NS_WARN_IF(!collapsedNode)) {
return NS_ERROR_FAILURE;
}
bool isSet, theSetting;
nsString tOutString;
if (aAttribute) {
mTypeInState->GetTypingState(isSet, theSetting, &aHTMLProperty,
aAttribute, &tOutString);
if (outValue) {
outValue->Assign(tOutString);
}
} else {
mTypeInState->GetTypingState(isSet, theSetting, &aHTMLProperty);
}
if (isSet) {
*aFirst = *aAny = *aAll = theSetting;
return NS_OK;
}
if (collapsedNode->IsContent() &&
CSSEditUtils::IsCSSEditableProperty(collapsedNode, &aHTMLProperty,
aAttribute)) {
if (aValue) {
tOutString.Assign(*aValue);
}
*aFirst = *aAny = *aAll =
CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
MOZ_KnownLive(*collapsedNode->AsContent()), &aHTMLProperty,
aAttribute, tOutString);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (outValue) {
outValue->Assign(tOutString);
}
return NS_OK;
}
isSet = IsTextPropertySetByContent(collapsedNode, &aHTMLProperty,
aAttribute, aValue, outValue);
*aFirst = *aAny = *aAll = isSet;
return NS_OK;
}
// Non-collapsed selection
nsAutoString firstValue, theValue;
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
int32_t endOffset = range->EndOffset();
PostContentIterator postOrderIter;
DebugOnly<nsresult> rvIgnored = postOrderIter.Init(range);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"Failed to initialize post-order content iterator");
for (; !postOrderIter.IsDone(); postOrderIter.Next()) {
if (!postOrderIter.GetCurrentNode()->IsContent()) {
continue;
}
nsCOMPtr<nsIContent> content =
postOrderIter.GetCurrentNode()->AsContent();
if (content->IsHTMLElement(nsGkAtoms::body)) {
break;
}
// just ignore any non-editable nodes
if (content->IsText() &&
(!EditorUtils::IsEditableContent(*content, EditorType::HTML) ||
IsEmptyTextNode(*content))) {
continue;
}
if (content->GetAsText()) {
if (!isCollapsed && first && firstNodeInRange) {
firstNodeInRange = false;
if (range->StartOffset() == content->Length()) {
continue;
}
} else if (content == endNode && !endOffset) {
continue;
}
} else if (content->IsElement()) {
// handle non-text leaf nodes here
continue;
}
bool isSet = false;
bool useTextDecoration =
&aHTMLProperty == nsGkAtoms::u || &aHTMLProperty == nsGkAtoms::strike;
if (first) {
if (CSSEditUtils::IsCSSEditableProperty(content, &aHTMLProperty,
aAttribute)) {
// The HTML styles defined by aHTMLProperty/aAttribute have a CSS
// equivalence in this implementation for node; let's check if it
// carries those CSS styles
if (aValue) {
firstValue.Assign(*aValue);
}
isSet = CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
*content, &aHTMLProperty, aAttribute, firstValue);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
} else {
isSet = IsTextPropertySetByContent(content, &aHTMLProperty,
aAttribute, aValue, &firstValue);
}
*aFirst = isSet;
first = false;
if (outValue) {
*outValue = firstValue;
}
} else {
if (CSSEditUtils::IsCSSEditableProperty(content, &aHTMLProperty,
aAttribute)) {
// The HTML styles defined by aHTMLProperty/aAttribute have a CSS
// equivalence in this implementation for node; let's check if it
// carries those CSS styles
if (aValue) {
theValue.Assign(*aValue);
}
isSet = CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
*content, &aHTMLProperty, aAttribute, theValue);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
} else {
isSet = IsTextPropertySetByContent(content, &aHTMLProperty,
aAttribute, aValue, &theValue);
}
if (firstValue != theValue &&
// For text-decoration related HTML properties, i.e. <u> and
// <strike>, we have to also check |isSet| because text-decoration
// is a shorthand property, and it may contains other unrelated
// longhand components, e.g. text-decoration-color, so we have to do
// an extra check before setting |*aAll| to false.
// e.g.
// firstValue: "underline rgb(0, 0, 0)"
// theValue: "underline rgb(0, 0, 238)" // <a> uses blue color
// These two values should be the same if we are checking `<u>`.
// That's why we need to check |*aFirst| and |isSet|.
//
// This is a work-around for text-decoration.
// The spec issue: https://github.com/w3c/editing/issues/241.
// Once this spec issue is resolved, we could drop this work-around
// check.
(!useTextDecoration || *aFirst != isSet)) {
*aAll = false;
}
}
if (isSet) {
*aAny = true;
} else {
*aAll = false;
}
}
}
if (!*aAny) {
// make sure that if none of the selection is set, we don't report all is
// set
*aAll = false;
}
return NS_OK;
}
NS_IMETHODIMP HTMLEditor::GetInlineProperty(const nsAString& aHTMLProperty,
const nsAString& aAttribute,
const nsAString& aValue,
bool* aFirst, bool* aAny,
bool* aAll) {
RefPtr<nsAtom> property = NS_Atomize(aHTMLProperty);
nsStaticAtom* attribute = EditorUtils::GetAttributeAtom(aAttribute);
nsresult rv = GetInlineProperty(property, MOZ_KnownLive(attribute), aValue,
aFirst, aAny, aAll);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::GetInlineProperty() failed");
return rv;
}
nsresult HTMLEditor::GetInlineProperty(nsAtom* aHTMLProperty,
nsAtom* aAttribute,
const nsAString& aValue, bool* aFirst,
bool* aAny, bool* aAll) const {
if (NS_WARN_IF(!aHTMLProperty) || NS_WARN_IF(!aFirst) || NS_WARN_IF(!aAny) ||
NS_WARN_IF(!aAll)) {
return NS_ERROR_INVALID_ARG;
}
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return NS_ERROR_NOT_INITIALIZED;
}
const nsAString* val = !aValue.IsEmpty() ? &aValue : nullptr;
nsresult rv = GetInlinePropertyBase(*aHTMLProperty, aAttribute, val, aFirst,
aAny, aAll, nullptr);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::GetInlinePropertyBase() failed");
return EditorBase::ToGenericNSResult(rv);
}
NS_IMETHODIMP HTMLEditor::GetInlinePropertyWithAttrValue(
const nsAString& aHTMLProperty, const nsAString& aAttribute,
const nsAString& aValue, bool* aFirst, bool* aAny, bool* aAll,
nsAString& outValue) {
RefPtr<nsAtom> property = NS_Atomize(aHTMLProperty);
nsStaticAtom* attribute = EditorUtils::GetAttributeAtom(aAttribute);
nsresult rv = GetInlinePropertyWithAttrValue(
property, MOZ_KnownLive(attribute), aValue, aFirst, aAny, aAll, outValue);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::GetInlinePropertyWithAttrValue() failed");
return rv;
}
nsresult HTMLEditor::GetInlinePropertyWithAttrValue(
nsAtom* aHTMLProperty, nsAtom* aAttribute, const nsAString& aValue,
bool* aFirst, bool* aAny, bool* aAll, nsAString& outValue) {
if (NS_WARN_IF(!aHTMLProperty) || NS_WARN_IF(!aFirst) || NS_WARN_IF(!aAny) ||
NS_WARN_IF(!aAll)) {
return NS_ERROR_INVALID_ARG;
}
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return NS_ERROR_NOT_INITIALIZED;
}
const nsAString* val = !aValue.IsEmpty() ? &aValue : nullptr;
nsresult rv = GetInlinePropertyBase(*aHTMLProperty, aAttribute, val, aFirst,
aAny, aAll, &outValue);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::GetInlinePropertyBase() failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::RemoveAllInlinePropertiesAsAction(
nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(
*this, EditAction::eRemoveAllInlineStyleProperties, aPrincipal);
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::Yes);
IgnoredErrorResult ignoredError;
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eRemoveAllTextProperties, nsIEditor::eNext,
ignoredError);
if (NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
return EditorBase::ToGenericNSResult(ignoredError.StealNSResult());
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
"HTMLEditor::OnStartToHandleTopLevelEditSubAction() failed, but ignored");
rv =
RemoveInlinePropertyInternal(nullptr, nullptr, RemoveRelatedElements::No);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyInternal(nullptr, "
"nullptr, RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::RemoveInlinePropertyAsAction(nsStaticAtom& aHTMLProperty,
nsStaticAtom* aAttribute,
nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(
*this,
HTMLEditUtils::GetEditActionForFormatText(aHTMLProperty, aAttribute,
false),
aPrincipal);
switch (editActionData.GetEditAction()) {
case EditAction::eRemoveFontFamilyProperty:
MOZ_ASSERT(!u""_ns.IsVoid());
editActionData.SetData(u""_ns);
break;
case EditAction::eRemoveColorProperty:
case EditAction::eRemoveBackgroundColorPropertyInline:
editActionData.SetColorData(u""_ns);
break;
default:
break;
}
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
rv = RemoveInlinePropertyInternal(&aHTMLProperty, aAttribute,
RemoveRelatedElements::Yes);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyInternal("
"RemoveRelatedElements::Yes) failed");
return EditorBase::ToGenericNSResult(rv);
}
NS_IMETHODIMP HTMLEditor::RemoveInlineProperty(const nsAString& aProperty,
const nsAString& aAttribute) {
nsStaticAtom* property = NS_GetStaticAtom(aProperty);
nsStaticAtom* attribute = EditorUtils::GetAttributeAtom(aAttribute);
AutoEditActionDataSetter editActionData(
*this,
HTMLEditUtils::GetEditActionForFormatText(*property, attribute, false));
switch (editActionData.GetEditAction()) {
case EditAction::eRemoveFontFamilyProperty:
MOZ_ASSERT(!u""_ns.IsVoid());
editActionData.SetData(u""_ns);
break;
case EditAction::eRemoveColorProperty:
case EditAction::eRemoveBackgroundColorPropertyInline:
editActionData.SetColorData(u""_ns);
break;
default:
break;
}
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
rv = RemoveInlinePropertyInternal(MOZ_KnownLive(property),
MOZ_KnownLive(attribute),
RemoveRelatedElements::No);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyInternal("
"RemoveRelatedElements::No) failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::RemoveInlinePropertyInternal(
nsStaticAtom* aProperty, nsStaticAtom* aAttribute,
RemoveRelatedElements aRemoveRelatedElements) {
MOZ_ASSERT(IsEditActionDataAvailable());
MOZ_ASSERT(aAttribute != nsGkAtoms::_empty);
if (NS_WARN_IF(!mInitSucceeded)) {
return NS_ERROR_NOT_INITIALIZED;
}
DebugOnly<nsresult> rvIgnored = CommitComposition();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"EditorBase::CommitComposition() failed, but ignored");
// Also remove equivalent properties (bug 317093)
struct HTMLStyle final {
// HTML tag name or nsGkAtoms::href or nsGkAtoms::name.
nsStaticAtom* mProperty = nullptr;
// HTML attribute like nsGkAtom::color for nsGkAtoms::font.
nsStaticAtom* mAttribute = nullptr;
explicit HTMLStyle(nsStaticAtom* aProperty,
nsStaticAtom* aAttribute = nullptr)
: mProperty(aProperty), mAttribute(aAttribute) {}
};
AutoTArray<HTMLStyle, 3> removeStyles;
if (aRemoveRelatedElements == RemoveRelatedElements::Yes) {
if (aProperty == nsGkAtoms::b) {
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::strong));
} else if (aProperty == nsGkAtoms::i) {
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::em));
} else if (aProperty == nsGkAtoms::strike) {
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::s));
} else if (aProperty == nsGkAtoms::font) {
if (aAttribute == nsGkAtoms::size) {
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::big));
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::small));
}
// Handling `<tt>` element code was implemented for composer (bug 115922).
// This shouldn't work with `Document.execCommand()` for compatibility
// with the other browsers. Currently, edit action principal is set only
// when the root caller is Document::ExecCommand() so that we should
// handle `<tt>` element only when the principal is nullptr that must be
// only when XUL command is executed on composer.
else if (aAttribute == nsGkAtoms::face && !GetEditActionPrincipal()) {
removeStyles.AppendElement(HTMLStyle(nsGkAtoms::tt));
}
}
}
removeStyles.AppendElement(HTMLStyle(aProperty, aAttribute));
if (SelectionRefPtr()->IsCollapsed()) {
// Manipulating text attributes on a collapsed selection only sets state
// for the next text insertion
if (removeStyles[0].mProperty) {
for (HTMLStyle& style : removeStyles) {
MOZ_ASSERT(style.mProperty);
if (style.mProperty == nsGkAtoms::href ||
style.mProperty == nsGkAtoms::name) {
mTypeInState->ClearProp(nsGkAtoms::a, nullptr);
} else {
mTypeInState->ClearProp(style.mProperty, style.mAttribute);
}
}
} else {
mTypeInState->ClearAllProps();
}
return NS_OK;
}
// XXX Shouldn't we quit before calling `CommitComposition()`?
if (IsPlaintextEditor()) {
return NS_OK;
}
EditActionResult result = CanHandleHTMLEditSubAction();
if (result.Failed() || result.Canceled()) {
NS_WARNING_ASSERTION(result.Succeeded(),
"HTMLEditor::CanHandleHTMLEditSubAction() failed");
return result.Rv();
}
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::Yes);
IgnoredErrorResult ignoredError;
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eRemoveTextProperty, nsIEditor::eNext,
ignoredError);
if (NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
return ignoredError.StealNSResult();
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
"HTMLEditor::OnStartToHandleTopLevelEditSubAction() failed, but ignored");
{
AutoSelectionRestorer restoreSelectionLater(*this);
AutoTransactionsConserveSelection dontChangeMySelection(*this);
for (HTMLStyle& style : removeStyles) {
// Loop through the ranges in the selection.
// XXX Although `Selection` will be restored by AutoSelectionRestorer,
// AutoSelectionRangeArray just grabs the ranges in `Selection`.
// Therefore, modifying each range may notify selection listener. So
// perhaps, we should clone each range here instead.
AutoSelectionRangeArray arrayOfRanges(SelectionRefPtr());
for (auto& range : arrayOfRanges.mRanges) {
if (style.mProperty == nsGkAtoms::name) {
// Promote range if it starts or end in a named anchor and we want to
// remove named anchors
nsresult rv = PromoteRangeIfStartsOrEndsInNamedAnchor(*range);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor() failed");
return rv;
}
} else {
// Adjust range to include any ancestors whose children are entirely
// selected
nsresult rv = PromoteInlineRange(*range);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::PromoteInlineRange() failed");
return rv;
}
}
// Remove this style from ancestors of our range endpoints, splitting
// them as appropriate
SplitRangeOffResult splitRangeOffResult =
SplitAncestorStyledInlineElementsAtRangeEdges(
EditorDOMPoint(range->StartRef()),
EditorDOMPoint(range->EndRef()), MOZ_KnownLive(style.mProperty),
MOZ_KnownLive(style.mAttribute));
if (splitRangeOffResult.Failed()) {
NS_WARNING(
"HTMLEditor::SplitAncestorStyledInlineElementsAtRangeEdges() "
"failed");
return splitRangeOffResult.Rv();
}
// XXX Modifying `range` means that we may modify ranges in `Selection`.
// Is this intentional? Note that the range may be not in
// `Selection` too. It seems that at least one of them is not
// an unexpected case.
const EditorDOMPoint& startOfRange(
splitRangeOffResult.SplitPointAtStart());
const EditorDOMPoint& endOfRange(splitRangeOffResult.SplitPointAtEnd());
if (NS_WARN_IF(!startOfRange.IsSet()) ||
NS_WARN_IF(!endOfRange.IsSet())) {
continue;
}
nsresult rv = range->SetStartAndEnd(startOfRange.ToRawRangeBoundary(),
endOfRange.ToRawRangeBoundary());
// Note that modifying a range in `Selection` may run script so that
// we might have been destroyed here.
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("nsRange::SetStartAndEnd() failed");
return rv;
}
// Collect editable nodes which are entirely contained in the range.
AutoTArray<OwningNonNull<nsIContent>, 64> arrayOfContents;
if (startOfRange.GetContainer() == endOfRange.GetContainer() &&
startOfRange.IsInTextNode()) {
if (!EditorUtils::IsEditableContent(*startOfRange.ContainerAsText(),
EditorType::HTML)) {
continue;
}
arrayOfContents.AppendElement(*startOfRange.ContainerAsText());
} else if (startOfRange.IsInTextNode() && endOfRange.IsInTextNode() &&
startOfRange.GetContainer()->GetNextSibling() ==
endOfRange.GetContainer()) {
if (EditorUtils::IsEditableContent(*startOfRange.ContainerAsText(),
EditorType::HTML)) {
arrayOfContents.AppendElement(*startOfRange.ContainerAsText());
}
if (EditorUtils::IsEditableContent(*endOfRange.ContainerAsText(),
EditorType::HTML)) {
arrayOfContents.AppendElement(*endOfRange.ContainerAsText());
}
if (arrayOfContents.IsEmpty()) {
continue;
}
} else {
// Append first node if it's a text node but selected not entirely.
if (startOfRange.IsInTextNode() &&
!startOfRange.IsStartOfContainer() &&
EditorUtils::IsEditableContent(*startOfRange.ContainerAsText(),
EditorType::HTML)) {
arrayOfContents.AppendElement(*startOfRange.ContainerAsText());
}
// Append all entirely selected nodes.
ContentSubtreeIterator subtreeIter;
if (NS_SUCCEEDED(subtreeIter.Init(range))) {
for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
nsCOMPtr<nsINode> node = subtreeIter.GetCurrentNode();
if (NS_WARN_IF(!node)) {
return NS_ERROR_FAILURE;
}
if (node->IsContent() &&
EditorUtils::IsEditableContent(*node->AsContent(),
EditorType::HTML)) {
arrayOfContents.AppendElement(*node->AsContent());
}
}
}
// Append last node if it's a text node but selected not entirely.
if (startOfRange.GetContainer() != endOfRange.GetContainer() &&
endOfRange.IsInTextNode() && !endOfRange.IsEndOfContainer() &&
EditorUtils::IsEditableContent(*endOfRange.ContainerAsText(),
EditorType::HTML)) {
arrayOfContents.AppendElement(*endOfRange.ContainerAsText());
}
}
for (OwningNonNull<nsIContent>& content : arrayOfContents) {
if (content->IsElement()) {
nsresult rv =
RemoveStyleInside(MOZ_KnownLive(*content->AsElement()),
MOZ_KnownLive(style.mProperty),
MOZ_KnownLive(style.mAttribute));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveStyleInside() failed");
return rv;
}
}
bool isRemovable = IsRemovableParentStyleWithNewSpanElement(
MOZ_KnownLive(content), MOZ_KnownLive(style.mProperty),
MOZ_KnownLive(style.mAttribute));
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (!isRemovable) {
continue;
}
if (!content->IsText()) {
// XXX Do we need to call this even when data node or something? If
// so, for what?
// MOZ_KnownLive because 'arrayOfContents' is guaranteed to
// keep it alive.
DebugOnly<nsresult> rvIgnored = SetInlinePropertyOnNode(
MOZ_KnownLive(content), MOZ_KnownLive(*style.mProperty),
MOZ_KnownLive(style.mAttribute),
u"-moz-editor-invert-value"_ns);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"HTMLEditor::SetInlinePropertyOnNode(-moz-editor-invert-value) "
"failed, but ignored");
continue;
}
// If current node is a text node, we need to create `<span>` element
// for it to overwrite parent style. Unfortunately, all browsers
// don't join text nodes when removing a style. Therefore, there
// may be multiple text nodes as adjacent siblings. That's the
// reason why we need to handle text nodes in this loop.
uint32_t startOffset = content == startOfRange.GetContainer()
? startOfRange.Offset()
: 0;
uint32_t endOffset = content == endOfRange.GetContainer()
? endOfRange.Offset()
: content->Length();
nsresult rv = SetInlinePropertyOnTextNode(
MOZ_KnownLive(*content->AsText()), startOffset, endOffset,
MOZ_KnownLive(*style.mProperty), MOZ_KnownLive(style.mAttribute),
u"-moz-editor-invert-value"_ns);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::SetInlinePropertyOnTextNode(-moz-editor-invert-"
"value) failed");
return rv;
}
}
// For avoiding unnecessary loop cost, check whether the style is
// invertible first.
if (style.mProperty &&
CSSEditUtils::IsCSSInvertible(*style.mProperty, style.mAttribute)) {
// Finally, we should remove the style from all leaf text nodes if
// they still have the style.
AutoTArray<OwningNonNull<Text>, 32> leafTextNodes;
for (OwningNonNull<nsIContent>& content : arrayOfContents) {
if (content->IsElement()) {
CollectEditableLeafTextNodes(*content->AsElement(),
leafTextNodes);
}
}
for (OwningNonNull<Text>& textNode : leafTextNodes) {
bool isRemovable = IsRemovableParentStyleWithNewSpanElement(
MOZ_KnownLive(textNode), MOZ_KnownLive(style.mProperty),
MOZ_KnownLive(style.mAttribute));
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (!isRemovable) {
continue;
}
// MOZ_KnownLive because 'leafTextNodes' is guaranteed to
// keep it alive.
nsresult rv = SetInlinePropertyOnTextNode(
MOZ_KnownLive(textNode), 0, textNode->TextLength(),
MOZ_KnownLive(*style.mProperty),
MOZ_KnownLive(style.mAttribute),
u"-moz-editor-invert-value"_ns);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::SetInlinePropertyOnTextNode(-moz-editor-invert-"
"value) failed");
return rv;
}
}
}
} // for-loop of selection ranges
} // for-loop of styles
} // AutoSelectionRestorer and AutoTransactionsConserveSelection
// Restoring `Selection` may cause destroying us.
return NS_WARN_IF(Destroyed()) ? NS_ERROR_EDITOR_DESTROYED : NS_OK;
}
bool HTMLEditor::IsRemovableParentStyleWithNewSpanElement(
nsIContent& aContent, nsAtom* aHTMLProperty, nsAtom* aAttribute) const {
// We don't support to remove all inline styles with this path.
if (!aHTMLProperty) {
return false;
}
// First check whether the style is invertible since this is the fastest
// check.
if (!CSSEditUtils::IsCSSInvertible(*aHTMLProperty, aAttribute)) {
return false;
}
// If parent block has the removing style, we should create `<span>`
// element to remove the style even in HTML mode since Chrome does it.
if (!CSSEditUtils::IsCSSEditableProperty(&aContent, aHTMLProperty,
aAttribute)) {
return false;
}
// aContent's computed style indicates the CSS equivalence to
// the HTML style to remove is applied; but we found no element
// in the ancestors of aContent carrying specified styles;
// assume it comes from a rule and let's try to insert a span
// "inverting" the style
nsAutoString emptyString;
bool isSet = CSSEditUtils::IsComputedCSSEquivalentToHTMLInlineStyleSet(
aContent, aHTMLProperty, aAttribute, emptyString);
return NS_WARN_IF(Destroyed()) ? false : isSet;
}
void HTMLEditor::CollectEditableLeafTextNodes(
Element& aElement, nsTArray<OwningNonNull<Text>>& aLeafTextNodes) const {
for (nsIContent* child = aElement.GetFirstChild(); child;
child = child->GetNextSibling()) {
if (child->IsElement()) {
CollectEditableLeafTextNodes(*child->AsElement(), aLeafTextNodes);
continue;
}
if (child->IsText()) {
aLeafTextNodes.AppendElement(*child->AsText());
}
}
}
nsresult HTMLEditor::IncreaseFontSizeAsAction(nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(*this, EditAction::eIncrementFontSize,
aPrincipal);
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
rv = RelativeFontChange(FontSize::incr);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RelativeFontChange(FontSize::incr) failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::DecreaseFontSizeAsAction(nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(*this, EditAction::eDecrementFontSize,
aPrincipal);
nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent();
if (NS_FAILED(rv)) {
NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED,
"CanHandleAndMaybeDispatchBeforeInputEvent(), failed");
return EditorBase::ToGenericNSResult(rv);
}
rv = RelativeFontChange(FontSize::decr);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RelativeFontChange(FontSize::decr) failed");
return EditorBase::ToGenericNSResult(rv);
}
nsresult HTMLEditor::RelativeFontChange(FontSize aDir) {
MOZ_ASSERT(IsEditActionDataAvailable());
DebugOnly<nsresult> rvIgnored = CommitComposition();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"EditorBase::CommitComposition() failed, but ignored");
// If selection is collapsed, set typing state
if (SelectionRefPtr()->IsCollapsed()) {
nsAtom& atom = aDir == FontSize::incr ? *nsGkAtoms::big : *nsGkAtoms::small;
// Let's see in what kind of element the selection is
if (NS_WARN_IF(!SelectionRefPtr()->RangeCount())) {
return NS_OK;
}
RefPtr<const nsRange> firstRange = SelectionRefPtr()->GetRangeAt(0);
if (NS_WARN_IF(!firstRange) ||
NS_WARN_IF(!firstRange->GetStartContainer())) {
return NS_OK;
}
OwningNonNull<nsINode> selectedNode = *firstRange->GetStartContainer();
if (selectedNode->IsText()) {
if (NS_WARN_IF(!selectedNode->GetParentNode())) {
return NS_OK;
}
selectedNode = *selectedNode->GetParentNode();
}
if (!HTMLEditUtils::CanNodeContain(selectedNode, atom)) {
return NS_OK;
}
// Manipulating text attributes on a collapsed selection only sets state
// for the next text insertion
mTypeInState->SetProp(&atom, nullptr, u""_ns);
return NS_OK;
}
// Wrap with txn batching, rules sniffing, and selection preservation code
AutoPlaceholderBatch treatAsOneTransaction(*this,
ScrollSelectionIntoView::Yes);
IgnoredErrorResult ignoredError;
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eSetTextProperty, nsIEditor::eNext, ignoredError);
if (NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
return ignoredError.StealNSResult();
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
"HTMLEditor::OnStartToHandleTopLevelEditSubAction() failed, but ignored");
AutoSelectionRestorer restoreSelectionLater(*this);
AutoTransactionsConserveSelection dontChangeMySelection(*this);
// Loop through the ranges in the selection
AutoSelectionRangeArray arrayOfRanges(SelectionRefPtr());
for (auto& range : arrayOfRanges.mRanges) {
// Adjust range to include any ancestors with entirely selected children
nsresult rv = PromoteInlineRange(*range);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::PromoteInlineRange() failed");
return rv;
}
// Check for easy case: both range endpoints in same text node
nsCOMPtr<nsINode> startNode = range->GetStartContainer();
nsCOMPtr<nsINode> endNode = range->GetEndContainer();
MOZ_ASSERT(startNode);
MOZ_ASSERT(endNode);
if (startNode == endNode && startNode->IsText()) {
nsresult rv = RelativeFontChangeOnTextNode(
aDir, MOZ_KnownLive(*startNode->GetAsText()), range->StartOffset(),
range->EndOffset());
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnTextNode() failed");
return rv;
}
} else {
// Not the easy case. Range not contained in single text node. There
// are up to three phases here. There are all the nodes reported by the
// subtree iterator to be processed. And there are potentially a
// starting textnode and an ending textnode which are only partially
// contained by the range.
// Let's handle the nodes reported by the iterator. These nodes are
// entirely contained in the selection range. We build up a list of them
// (since doing operations on the document during iteration would perturb
// the iterator).
// Iterate range and build up array
ContentSubtreeIterator subtreeIter;
if (NS_SUCCEEDED(subtreeIter.Init(range))) {
nsTArray<OwningNonNull<nsIContent>> arrayOfContents;
for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
if (NS_WARN_IF(!subtreeIter.GetCurrentNode()->IsContent())) {
return NS_ERROR_FAILURE;
}
OwningNonNull<nsIContent> content =
*subtreeIter.GetCurrentNode()->AsContent();
if (EditorUtils::IsEditableContent(content, EditorType::HTML)) {
arrayOfContents.AppendElement(content);
}
}
// Now that we have the list, do the font size change on each node
for (OwningNonNull<nsIContent>& content : arrayOfContents) {
// MOZ_KnownLive because 'arrayOfContents' is guaranteed to keep it
// alive.
nsresult rv = RelativeFontChangeOnNode(
aDir == FontSize::incr ? +1 : -1, MOZ_KnownLive(content));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnNode() failed");
return rv;
}
}
}
// Now check the start and end parents of the range to see if they need
// to be separately handled (they do if they are text nodes, due to how
// the subtree iterator works - it will not have reported them).
if (startNode->IsText() && EditorUtils::IsEditableContent(
*startNode->AsText(), EditorType::HTML)) {
nsresult rv = RelativeFontChangeOnTextNode(
aDir, MOZ_KnownLive(*startNode->AsText()), range->StartOffset(),
startNode->Length());
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnTextNode() failed");
return rv;
}
}
if (endNode->IsText() && EditorUtils::IsEditableContent(
*endNode->AsText(), EditorType::HTML)) {
nsresult rv = RelativeFontChangeOnTextNode(
aDir, MOZ_KnownLive(*endNode->AsText()), 0, range->EndOffset());
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnTextNode() failed");
return rv;
}
}
}
}
return NS_OK;
}
nsresult HTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
Text& aTextNode,
int32_t aStartOffset,
int32_t aEndOffset) {
// Don't need to do anything if no characters actually selected
if (aStartOffset == aEndOffset) {
return NS_OK;
}
if (!aTextNode.GetParentNode() ||
!HTMLEditUtils::CanNodeContain(*aTextNode.GetParentNode(),
*nsGkAtoms::big)) {
return NS_OK;
}
// -1 is a magic value meaning to the end of node
if (aEndOffset == -1) {
aEndOffset = aTextNode.Length();
}
// Make the range an independent node.
nsCOMPtr<nsIContent> textNodeForTheRange = &aTextNode;
// Split at the end of the range.
EditorDOMPoint atEnd(textNodeForTheRange, aEndOffset);
if (!atEnd.IsEndOfContainer()) {
// We need to split off back of text node
ErrorResult error;
textNodeForTheRange = SplitNodeWithTransaction(atEnd, error);
if (error.Failed()) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return error.StealNSResult();
}
}
// Split at the start of the range.
EditorDOMPoint atStart(textNodeForTheRange, aStartOffset);
if (!atStart.IsStartOfContainer()) {
// We need to split off front of text node
ErrorResult error;
nsCOMPtr<nsIContent> newLeftNode = SplitNodeWithTransaction(atStart, error);
if (error.Failed()) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return error.StealNSResult();
}
Unused << newLeftNode;
}
// Look for siblings that are correct type of node
nsAtom* nodeType = aDir == FontSize::incr ? nsGkAtoms::big : nsGkAtoms::small;
nsCOMPtr<nsIContent> sibling = GetPriorHTMLSibling(textNodeForTheRange);
if (sibling && sibling->IsHTMLElement(nodeType)) {
// Previous sib is already right kind of inline node; slide this over
nsresult rv = MoveNodeToEndWithTransaction(*textNodeForTheRange, *sibling);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeToEndWithTransaction() failed");
return rv;
}
sibling = GetNextHTMLSibling(textNodeForTheRange);
if (sibling && sibling->IsHTMLElement(nodeType)) {
// Following sib is already right kind of inline node; slide this over
nsresult rv = MoveNodeWithTransaction(*textNodeForTheRange,
EditorDOMPoint(sibling, 0));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeWithTransaction() failed");
return rv;
}
// Else reparent the node inside font node with appropriate relative size
RefPtr<Element> newElement = InsertContainerWithTransaction(
*textNodeForTheRange, MOZ_KnownLive(*nodeType));
NS_WARNING_ASSERTION(newElement,
"HTMLEditor::InsertContainerWithTransaction() failed");
return newElement ? NS_OK : NS_ERROR_FAILURE;
}
nsresult HTMLEditor::RelativeFontChangeHelper(int32_t aSizeChange,
nsINode* aNode) {
MOZ_ASSERT(aNode);
/* This routine looks for all the font nodes in the tree rooted by aNode,
including aNode itself, looking for font nodes that have the size attr
set. Any such nodes need to have big or small put inside them, since
they override any big/small that are above them.
*/
// Can only change font size by + or - 1
if (aSizeChange != 1 && aSizeChange != -1) {
return NS_ERROR_ILLEGAL_VALUE;
}
// If this is a font node with size, put big/small inside it.
if (aNode->IsHTMLElement(nsGkAtoms::font) &&
aNode->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::size)) {
// Cycle through children and adjust relative font size.
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
for (nsIContent* child = aNode->GetFirstChild(); child;
child = child->GetNextSibling()) {
childList.AppendElement(child);
}
for (const auto& child : childList) {
// MOZ_KnownLive because 'childList' is guaranteed to
// keep it alive.
nsresult rv = RelativeFontChangeOnNode(aSizeChange, MOZ_KnownLive(child));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnNode() failed");
return rv;
}
}
// RelativeFontChangeOnNode already calls us recursively,
// so we don't need to check our children again.
return NS_OK;
}
// Otherwise cycle through the children.
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
for (nsIContent* child = aNode->GetFirstChild(); child;
child = child->GetNextSibling()) {
childList.AppendElement(child);
}
for (const auto& child : childList) {
// MOZ_KnownLive because 'childList' is guaranteed to
// keep it alive.
nsresult rv = RelativeFontChangeHelper(aSizeChange, MOZ_KnownLive(child));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeHelper() failed");
return rv;
}
}
return NS_OK;
}
nsresult HTMLEditor::RelativeFontChangeOnNode(int32_t aSizeChange,
nsIContent* aNode) {
MOZ_ASSERT(aNode);
// Can only change font size by + or - 1
if (aSizeChange != 1 && aSizeChange != -1) {
return NS_ERROR_ILLEGAL_VALUE;
}
nsAtom* atom;
if (aSizeChange == 1) {
atom = nsGkAtoms::big;
} else {
atom = nsGkAtoms::small;
}
// Is it the opposite of what we want?
if ((aSizeChange == 1 && aNode->IsHTMLElement(nsGkAtoms::small)) ||
(aSizeChange == -1 && aNode->IsHTMLElement(nsGkAtoms::big))) {
// first populate any nested font tags that have the size attr set
nsresult rv = RelativeFontChangeHelper(aSizeChange, aNode);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeHelper() failed");
return rv;
}
// in that case, just remove this node and pull up the children
rv = RemoveContainerWithTransaction(MOZ_KnownLive(*aNode->AsElement()));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveContainerWithTransaction() failed");
return rv;
}
// can it be put inside a "big" or "small"?
if (HTMLEditUtils::CanNodeContain(*atom, *aNode)) {
// first populate any nested font tags that have the size attr set
nsresult rv = RelativeFontChangeHelper(aSizeChange, aNode);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeHelper() failed");
return rv;
}
// ok, chuck it in.
// first look at siblings of aNode for matching bigs or smalls.
// if we find one, move aNode into it.
nsCOMPtr<nsIContent> sibling = GetPriorHTMLSibling(aNode);
if (sibling && sibling->IsHTMLElement(atom)) {
// previous sib is already right kind of inline node; slide this over into
// it
nsresult rv = MoveNodeToEndWithTransaction(*aNode, *sibling);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodeToEndWithTransaction() failed");
return rv;
}
sibling = GetNextHTMLSibling(aNode);
if (sibling && sibling->IsHTMLElement(atom)) {
// following sib is already right kind of inline node; slide this over
// into it
return MoveNodeWithTransaction(*aNode, EditorDOMPoint(sibling, 0));
}
// else insert it above aNode
RefPtr<Element> newElement =
InsertContainerWithTransaction(*aNode, MOZ_KnownLive(*atom));
NS_WARNING_ASSERTION(newElement,
"HTMLEditor::InsertContainerWithTransaction() failed");
return newElement ? NS_OK : NS_ERROR_FAILURE;
}
// none of the above? then cycle through the children.
// MOOSE: we should group the children together if possible
// into a single "big" or "small". For the moment they are
// each getting their own.
AutoTArray<nsCOMPtr<nsIContent>, 10> childList;
for (nsIContent* child = aNode->GetFirstChild(); child;
child = child->GetNextSibling()) {
childList.AppendElement(child);
}
for (const auto& child : childList) {
// MOZ_KnownLive because 'childList' is guaranteed to
// keep it alive.
nsresult rv = RelativeFontChangeOnNode(aSizeChange, MOZ_KnownLive(child));
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RelativeFontChangeOnNode() failed");
return rv;
}
}
return NS_OK;
}
NS_IMETHODIMP HTMLEditor::GetFontFaceState(bool* aMixed, nsAString& outFace) {
if (NS_WARN_IF(!aMixed)) {
return NS_ERROR_INVALID_ARG;
}
*aMixed = true;
outFace.Truncate();
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return NS_ERROR_NOT_INITIALIZED;
}
bool first, any, all;
nsresult rv = GetInlinePropertyBase(*nsGkAtoms::font, nsGkAtoms::face,
nullptr, &first, &any, &all, &outFace);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::GetInlinePropertyBase(nsGkAtoms::font, nsGkAtoms::face) "
"failed");
return EditorBase::ToGenericNSResult(rv);
}
if (any && !all) {
return NS_OK; // mixed
}
if (all) {
*aMixed = false;
return NS_OK;
}
// if there is no font face, check for tt
rv = GetInlinePropertyBase(*nsGkAtoms::tt, nullptr, nullptr, &first, &any,
&all, nullptr);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::GetInlinePropertyBase(nsGkAtoms::tt) failed");
return EditorBase::ToGenericNSResult(rv);
}
if (any && !all) {
return NS_OK; // mixed
}
if (all) {
*aMixed = false;
outFace.AssignLiteral("tt");
}
if (!any) {
// there was no font face attrs of any kind. We are in normal font.
outFace.Truncate();
*aMixed = false;
}
return NS_OK;
}
nsresult HTMLEditor::GetFontColorState(bool* aMixed, nsAString& aOutColor) {
if (NS_WARN_IF(!aMixed)) {
return NS_ERROR_INVALID_ARG;
}
*aMixed = true;
aOutColor.Truncate();
AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return NS_ERROR_NOT_INITIALIZED;
}
bool first, any, all;
nsresult rv = GetInlinePropertyBase(*nsGkAtoms::font, nsGkAtoms::color,
nullptr, &first, &any, &all, &aOutColor);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::GetInlinePropertyBase(nsGkAtoms::font, nsGkAtoms::color) "
"failed");
return EditorBase::ToGenericNSResult(rv);
}
if (any && !all) {
return NS_OK; // mixed
}
if (all) {
*aMixed = false;
return NS_OK;
}
if (!any) {
// there was no font color attrs of any kind..
aOutColor.Truncate();
*aMixed = false;
}
return NS_OK;
}
// the return value is true only if the instance of the HTML editor we created
// can handle CSS styles (for instance, Composer can, Messenger can't) and if
// the CSS preference is checked
NS_IMETHODIMP HTMLEditor::GetIsCSSEnabled(bool* aIsCSSEnabled) {
*aIsCSSEnabled = IsCSSEnabled();
return NS_OK;
}
bool HTMLEditor::HasStyleOrIdOrClassAttribute(Element& aElement) {
return aElement.HasNonEmptyAttr(nsGkAtoms::style) ||
aElement.HasNonEmptyAttr(nsGkAtoms::_class) ||
aElement.HasNonEmptyAttr(nsGkAtoms::id);
}
} // namespace mozilla
|