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
|
/* -*- 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 "mozilla/EditorCommands.h"
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/EditorBase.h" // for EditorBase
#include "mozilla/ErrorResult.h"
#include "mozilla/HTMLEditor.h" // for HTMLEditor
#include "mozilla/dom/Element.h"
#include "nsAString.h"
#include "nsAtom.h" // for nsAtom, nsStaticAtom, etc
#include "nsCommandParams.h" // for nsCommandParams, etc
#include "nsComponentManagerUtils.h" // for do_CreateInstance
#include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::font, etc
#include "nsIClipboard.h" // for nsIClipboard, etc
#include "nsIEditingSession.h"
#include "nsIPrincipal.h" // for nsIPrincipal
#include "nsLiteralString.h" // for NS_LITERAL_STRING
#include "nsReadableUtils.h" // for EmptyString
#include "nsString.h" // for nsAutoString, nsString, etc
#include "nsStringFwd.h" // for nsString
class nsISupports;
namespace mozilla {
using dom::Element;
// prototype
static nsresult GetListState(HTMLEditor* aHTMLEditor, bool* aMixed,
nsAString& aLocalName);
// defines
#define STATE_ENABLED "state_enabled"
#define STATE_ALL "state_all"
#define STATE_ANY "state_any"
#define STATE_MIXED "state_mixed"
#define STATE_BEGIN "state_begin"
#define STATE_END "state_end"
#define STATE_ATTRIBUTE "state_attribute"
#define STATE_DATA "state_data"
/*****************************************************************************
* mozilla::StateUpdatingCommandBase
*****************************************************************************/
bool StateUpdatingCommandBase::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
if (!htmlEditor->IsModifiable() || !htmlEditor->IsSelectionEditable()) {
return false;
}
if (aCommand == Command::FormatAbsolutePosition) {
return htmlEditor->IsAbsolutePositionEditorEnabled();
}
return true;
}
nsresult StateUpdatingCommandBase::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsStaticAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED;
}
nsresult rv = ToggleState(MOZ_KnownLive(*tagName), MOZ_KnownLive(*htmlEditor),
aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"StateUpdatingCommandBase::ToggleState() failed");
return rv;
}
nsresult StateUpdatingCommandBase::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
if (!aEditorBase) {
return NS_OK;
}
HTMLEditor* htmlEditor = aEditorBase->GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsStaticAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED;
}
// MOZ_KnownLive(htmlEditor) because the lifetime of aEditorBase is guaranteed
// by the callers.
// MOZ_KnownLive(tagName) because nsStaticAtom instances are alive until
// shutting down.
nsresult rv = GetCurrentState(MOZ_KnownLive(*tagName),
MOZ_KnownLive(*htmlEditor), aParams);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"StateUpdatingCommandBase::GetCurrentState() failed");
return rv;
}
/*****************************************************************************
* mozilla::PasteNoFormattingCommand
*****************************************************************************/
StaticRefPtr<PasteNoFormattingCommand> PasteNoFormattingCommand::sInstance;
bool PasteNoFormattingCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
return htmlEditor->CanPaste(nsIClipboard::kGlobalClipboard);
}
nsresult PasteNoFormattingCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
// Known live because we hold a ref above in "editor"
nsresult rv = MOZ_KnownLive(htmlEditor)
->PasteNoFormattingAsAction(
nsIClipboard::kGlobalClipboard,
EditorBase::DispatchPasteEvent::Yes, aPrincipal);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::PasteNoFormattingAsAction(DispatchPasteEvent::Yes) failed");
return rv;
}
nsresult PasteNoFormattingCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::StyleUpdatingCommand
*****************************************************************************/
StaticRefPtr<StyleUpdatingCommand> StyleUpdatingCommand::sInstance;
nsresult StyleUpdatingCommand::GetCurrentState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsCommandParams& aParams) const {
bool firstOfSelectionHasProp = false;
bool anyOfSelectionHasProp = false;
bool allOfSelectionHasProp = false;
nsresult rv = aHTMLEditor.GetInlineProperty(
aTagName, nullptr, u""_ns, &firstOfSelectionHasProp,
&anyOfSelectionHasProp, &allOfSelectionHasProp);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::GetInlineProperty() failed");
aParams.SetBool(STATE_ENABLED, NS_SUCCEEDED(rv));
aParams.SetBool(STATE_ALL, allOfSelectionHasProp);
aParams.SetBool(STATE_ANY, anyOfSelectionHasProp);
aParams.SetBool(STATE_MIXED, anyOfSelectionHasProp && !allOfSelectionHasProp);
aParams.SetBool(STATE_BEGIN, firstOfSelectionHasProp);
aParams.SetBool(STATE_END, allOfSelectionHasProp); // not completely accurate
return NS_OK;
}
nsresult StyleUpdatingCommand::ToggleState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsIPrincipal* aPrincipal) const {
RefPtr<nsCommandParams> params = new nsCommandParams();
// tags "href" and "name" are special cases in the core editor
// they are used to remove named anchor/link and shouldn't be used for
// insertion
bool doTagRemoval;
if (&aTagName == nsGkAtoms::href || &aTagName == nsGkAtoms::name) {
doTagRemoval = true;
} else {
// check current selection; set doTagRemoval if formatting should be removed
nsresult rv = GetCurrentState(aTagName, aHTMLEditor, *params);
if (NS_FAILED(rv)) {
NS_WARNING("StyleUpdatingCommand::GetCurrentState() failed");
return rv;
}
ErrorResult error;
doTagRemoval = params->GetBool(STATE_ALL, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
}
if (doTagRemoval) {
nsresult rv =
aHTMLEditor.RemoveInlinePropertyAsAction(aTagName, nullptr, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyAsAction() failed");
return rv;
}
nsresult rv = aHTMLEditor.SetInlinePropertyAsAction(aTagName, nullptr, u""_ns,
aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::ListCommand
*****************************************************************************/
StaticRefPtr<ListCommand> ListCommand::sInstance;
nsresult ListCommand::GetCurrentState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsCommandParams& aParams) const {
bool bMixed;
nsAutoString localName;
nsresult rv = GetListState(&aHTMLEditor, &bMixed, localName);
if (NS_FAILED(rv)) {
NS_WARNING("GetListState() failed");
return rv;
}
bool inList = aTagName.Equals(localName);
aParams.SetBool(STATE_ALL, !bMixed && inList);
aParams.SetBool(STATE_MIXED, bMixed);
aParams.SetBool(STATE_ENABLED, true);
return NS_OK;
}
nsresult ListCommand::ToggleState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsIPrincipal* aPrincipal) const {
RefPtr<nsCommandParams> params = new nsCommandParams();
nsresult rv = GetCurrentState(aTagName, aHTMLEditor, *params);
if (NS_FAILED(rv)) {
NS_WARNING("ListCommand::GetCurrentState() failed");
return rv;
}
ErrorResult error;
bool inList = params->GetBool(STATE_ALL, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
nsDependentAtomString listType(&aTagName);
if (inList) {
nsresult rv = aHTMLEditor.RemoveListAsAction(listType, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveListAsAction() failed");
return rv;
}
rv = aHTMLEditor.MakeOrChangeListAsAction(
aTagName, u""_ns, HTMLEditor::SelectAllOfCurrentList::No, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::MakeOrChangeListAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::ListItemCommand
*****************************************************************************/
StaticRefPtr<ListItemCommand> ListItemCommand::sInstance;
nsresult ListItemCommand::GetCurrentState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsCommandParams& aParams) const {
ErrorResult error;
ListItemElementSelectionState state(aHTMLEditor, error);
if (error.Failed()) {
NS_WARNING("ListItemElementSelectionState failed");
return error.StealNSResult();
}
if (state.IsNotOneTypeDefinitionListItemElementSelected()) {
aParams.SetBool(STATE_ALL, false);
aParams.SetBool(STATE_MIXED, true);
return NS_OK;
}
nsStaticAtom* selectedListItemTagName = nullptr;
if (state.IsLIElementSelected()) {
selectedListItemTagName = nsGkAtoms::li;
} else if (state.IsDTElementSelected()) {
selectedListItemTagName = nsGkAtoms::dt;
} else if (state.IsDDElementSelected()) {
selectedListItemTagName = nsGkAtoms::dd;
}
aParams.SetBool(STATE_ALL, &aTagName == selectedListItemTagName);
aParams.SetBool(STATE_MIXED, false);
return NS_OK;
}
nsresult ListItemCommand::ToggleState(nsStaticAtom& aTagName,
HTMLEditor& aHTMLEditor,
nsIPrincipal* aPrincipal) const {
// Need to use aTagName????
RefPtr<nsCommandParams> params = new nsCommandParams();
GetCurrentState(aTagName, aHTMLEditor, *params);
ErrorResult error;
bool inList = params->GetBool(STATE_ALL, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
if (inList) {
// To remove a list, first get what kind of list we're in
bool bMixed;
nsAutoString localName;
nsresult rv = GetListState(&aHTMLEditor, &bMixed, localName);
if (NS_FAILED(rv)) {
NS_WARNING("GetListState() failed");
return rv;
}
if (localName.IsEmpty() || bMixed) {
return NS_OK;
}
rv = aHTMLEditor.RemoveListAsAction(localName, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveListAsAction() failed");
return rv;
}
// Set to the requested paragraph type
// XXX Note: This actually doesn't work for "LI",
// but we currently don't use this for non DL lists anyway.
// Problem: won't this replace any current block paragraph style?
nsresult rv = aHTMLEditor.SetParagraphStateAsAction(
nsDependentAtomString(&aTagName), aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetParagraphFormatAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::RemoveListCommand
*****************************************************************************/
StaticRefPtr<RemoveListCommand> RemoveListCommand::sInstance;
bool RemoveListCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
if (!htmlEditor->IsModifiable() || !htmlEditor->IsSelectionEditable()) {
return false;
}
// It is enabled if we are in any list type
bool bMixed;
nsAutoString localName;
nsresult rv = GetListState(MOZ_KnownLive(htmlEditor), &bMixed, localName);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "GetListState() failed");
return NS_SUCCEEDED(rv) && (bMixed || !localName.IsEmpty());
}
nsresult RemoveListCommand::DoCommand(Command aCommand, EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
// This removes any list type
nsresult rv =
MOZ_KnownLive(htmlEditor)->RemoveListAsAction(u""_ns, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveListAsAction() failed");
return rv;
}
nsresult RemoveListCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::IndentCommand
*****************************************************************************/
StaticRefPtr<IndentCommand> IndentCommand::sInstance;
bool IndentCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult IndentCommand::DoCommand(Command aCommand, EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->IndentAsAction(aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "HTMLEditor::IndentAsAction() failed");
return rv;
}
nsresult IndentCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::OutdentCommand
*****************************************************************************/
StaticRefPtr<OutdentCommand> OutdentCommand::sInstance;
bool OutdentCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult OutdentCommand::DoCommand(Command aCommand, EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->OutdentAsAction(aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::OutdentAsAction() failed");
return rv;
}
nsresult OutdentCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::MultiStateCommandBase
*****************************************************************************/
bool MultiStateCommandBase::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
// should be disabled sometimes, like if the current selection is an image
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult MultiStateCommandBase::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
NS_WARNING(
"who is calling MultiStateCommandBase::DoCommand (no implementation)?");
return NS_OK;
}
nsresult MultiStateCommandBase::DoCommandParam(Command aCommand,
const nsAString& aStringParam,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
NS_WARNING_ASSERTION(aCommand != Command::FormatJustify,
"Command::FormatJustify should be used only for "
"IsCommandEnabled() and GetCommandStateParams()");
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = SetState(MOZ_KnownLive(htmlEditor), aStringParam, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"MultiStateCommandBase::SetState() failed");
return rv;
}
nsresult MultiStateCommandBase::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
if (!aEditorBase) {
return NS_OK;
}
HTMLEditor* htmlEditor = aEditorBase->GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = GetCurrentState(MOZ_KnownLive(htmlEditor), aParams);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"MultiStateCommandBase::GetCurrentState() failed");
return rv;
}
/*****************************************************************************
* mozilla::FormatBlockStateCommand
*****************************************************************************/
StaticRefPtr<FormatBlockStateCommand> FormatBlockStateCommand::sInstance;
nsresult FormatBlockStateCommand::GetCurrentState(
HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
ParagraphStateAtSelection state(
*aHTMLEditor,
ParagraphStateAtSelection::FormatBlockMode::HTMLFormatBlockCommand,
error);
if (error.Failed()) {
NS_WARNING("ParagraphStateAtSelection failed");
return error.StealNSResult();
}
aParams.SetBool(STATE_MIXED, state.IsMixed());
if (NS_WARN_IF(!state.GetFirstParagraphStateAtSelection())) {
aParams.SetCString(STATE_ATTRIBUTE, ""_ns);
} else {
nsCString paragraphState; // Don't use `nsAutoCString` for avoiding copy.
state.GetFirstParagraphStateAtSelection()->ToUTF8String(paragraphState);
aParams.SetCString(STATE_ATTRIBUTE, paragraphState);
}
return NS_OK;
}
nsresult FormatBlockStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor) || NS_WARN_IF(aNewState.IsEmpty())) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = aHTMLEditor->FormatBlockAsAction(aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::FormatBlockAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::ParagraphStateCommand
*****************************************************************************/
StaticRefPtr<ParagraphStateCommand> ParagraphStateCommand::sInstance;
nsresult ParagraphStateCommand::GetCurrentState(
HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
ParagraphStateAtSelection state(
*aHTMLEditor,
ParagraphStateAtSelection::FormatBlockMode::XULParagraphStateCommand,
error);
if (error.Failed()) {
NS_WARNING("ParagraphStateAtSelection failed");
return error.StealNSResult();
}
aParams.SetBool(STATE_MIXED, state.IsMixed());
if (NS_WARN_IF(!state.GetFirstParagraphStateAtSelection())) {
// XXX This is odd behavior, we should fix this later.
aParams.SetCString(STATE_ATTRIBUTE, "x"_ns);
} else {
nsCString paragraphState; // Don't use `nsAutoCString` for avoiding copy.
state.GetFirstParagraphStateAtSelection()->ToUTF8String(paragraphState);
aParams.SetCString(STATE_ATTRIBUTE, paragraphState);
}
return NS_OK;
}
nsresult ParagraphStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = aHTMLEditor->SetParagraphStateAsAction(aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetParagraphStateAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::FontFaceStateCommand
*****************************************************************************/
StaticRefPtr<FontFaceStateCommand> FontFaceStateCommand::sInstance;
nsresult FontFaceStateCommand::GetCurrentState(HTMLEditor* aHTMLEditor,
nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsAutoString outStateString;
bool outMixed;
nsresult rv = aHTMLEditor->GetFontFaceState(&outMixed, outStateString);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::GetFontFaceState() failed");
return rv;
}
aParams.SetBool(STATE_MIXED, outMixed);
aParams.SetCString(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString));
return NS_OK;
}
nsresult FontFaceStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
if (aNewState.IsEmpty() || aNewState.EqualsLiteral("normal")) {
nsresult rv = aHTMLEditor->RemoveInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::face, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyAsAction(nsGkAtoms::"
"font, nsGkAtoms::face) failed");
return rv;
}
nsresult rv = aHTMLEditor->SetInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::face, aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyAsAction(nsGkAtoms::font, "
"nsGkAtoms::face) failed");
return rv;
}
/*****************************************************************************
* mozilla::FontSizeStateCommand
*****************************************************************************/
StaticRefPtr<FontSizeStateCommand> FontSizeStateCommand::sInstance;
nsresult FontSizeStateCommand::GetCurrentState(HTMLEditor* aHTMLEditor,
nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsAutoString outStateString;
bool firstHas, anyHas, allHas;
nsresult rv = aHTMLEditor->GetInlinePropertyWithAttrValue(
*nsGkAtoms::font, nsGkAtoms::size, u""_ns, &firstHas, &anyHas, &allHas,
outStateString);
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::GetInlinePropertyWithAttrValue(nsGkAtoms::font, "
"nsGkAtoms::size) failed");
return rv;
}
nsAutoCString tOutStateString;
LossyCopyUTF16toASCII(outStateString, tOutStateString);
aParams.SetBool(STATE_MIXED, anyHas && !allHas);
aParams.SetCString(STATE_ATTRIBUTE, tOutStateString);
aParams.SetBool(STATE_ENABLED, true);
return NS_OK;
}
// acceptable values for "aNewState" are:
// -2
// -1
// 0
// +1
// +2
// +3
// medium
// normal
nsresult FontSizeStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
if (!aNewState.IsEmpty() && !aNewState.EqualsLiteral("normal") &&
!aNewState.EqualsLiteral("medium")) {
nsresult rv = aHTMLEditor->SetInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::size, aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyAsAction(nsGkAtoms::"
"font, nsGkAtoms::size) failed");
return rv;
}
// remove any existing font size, big or small
nsresult rv = aHTMLEditor->RemoveInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::size, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyAsAction(nsGkAtoms::"
"font, nsGkAtoms::size) failed");
return rv;
}
/*****************************************************************************
* mozilla::FontColorStateCommand
*****************************************************************************/
StaticRefPtr<FontColorStateCommand> FontColorStateCommand::sInstance;
nsresult FontColorStateCommand::GetCurrentState(
HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
bool outMixed;
nsAutoString outStateString;
nsresult rv = aHTMLEditor->GetFontColorState(&outMixed, outStateString);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::GetFontColorState() failed");
return rv;
}
nsAutoCString tOutStateString;
LossyCopyUTF16toASCII(outStateString, tOutStateString);
aParams.SetBool(STATE_MIXED, outMixed);
aParams.SetCString(STATE_ATTRIBUTE, tOutStateString);
return NS_OK;
}
nsresult FontColorStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
if (aNewState.IsEmpty() || aNewState.EqualsLiteral("normal")) {
nsresult rv = aHTMLEditor->RemoveInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::color, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyAsAction(nsGkAtoms::"
"font, nsGkAtoms::color) failed");
return rv;
}
nsresult rv = aHTMLEditor->SetInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::color, aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyAsAction(nsGkAtoms::font, "
"nsGkAtoms::color) failed");
return rv;
}
/*****************************************************************************
* mozilla::HighlightColorStateCommand
*****************************************************************************/
StaticRefPtr<HighlightColorStateCommand> HighlightColorStateCommand::sInstance;
nsresult HighlightColorStateCommand::GetCurrentState(
HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
bool outMixed;
nsAutoString outStateString;
nsresult rv = aHTMLEditor->GetHighlightColorState(&outMixed, outStateString);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::GetHighlightColorState() failed");
return rv;
}
nsAutoCString tOutStateString;
LossyCopyUTF16toASCII(outStateString, tOutStateString);
aParams.SetBool(STATE_MIXED, outMixed);
aParams.SetCString(STATE_ATTRIBUTE, tOutStateString);
return NS_OK;
}
nsresult HighlightColorStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
if (aNewState.IsEmpty() || aNewState.EqualsLiteral("normal")) {
nsresult rv = aHTMLEditor->RemoveInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::bgcolor, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveInlinePropertyAsAction(nsGkAtoms::"
"font, nsGkAtoms::bgcolor) failed");
return rv;
}
nsresult rv = aHTMLEditor->SetInlinePropertyAsAction(
*nsGkAtoms::font, nsGkAtoms::bgcolor, aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetInlinePropertyAsAction(nsGkAtoms::font, "
"nsGkAtoms::bgcolor) failed");
return rv;
}
/*****************************************************************************
* mozilla::BackgroundColorStateCommand
*****************************************************************************/
StaticRefPtr<BackgroundColorStateCommand>
BackgroundColorStateCommand::sInstance;
nsresult BackgroundColorStateCommand::GetCurrentState(
HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
bool outMixed;
nsAutoString outStateString;
nsresult rv = aHTMLEditor->GetBackgroundColorState(&outMixed, outStateString);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::GetBackgroundColorState() failed");
return rv;
}
nsAutoCString tOutStateString;
LossyCopyUTF16toASCII(outStateString, tOutStateString);
aParams.SetBool(STATE_MIXED, outMixed);
aParams.SetCString(STATE_ATTRIBUTE, tOutStateString);
return NS_OK;
}
nsresult BackgroundColorStateCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = aHTMLEditor->SetBackgroundColorAsAction(aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SetBackgroundColorAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::AlignCommand
*****************************************************************************/
StaticRefPtr<AlignCommand> AlignCommand::sInstance;
nsresult AlignCommand::GetCurrentState(HTMLEditor* aHTMLEditor,
nsCommandParams& aParams) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
AlignStateAtSelection state(*aHTMLEditor, error);
if (error.Failed()) {
if (!state.IsSelectionRangesFound()) {
// If there was no selection ranges, we shouldn't throw exception for
// compatibility with the other browsers, but I have no better idea
// than returning empty string in this case. Oddly, Blink/WebKit returns
// "true" or "false", but it's different from us and the value does not
// make sense. Additionally, WPT loves our behavior.
error.SuppressException();
aParams.SetBool(STATE_MIXED, false);
aParams.SetCString(STATE_ATTRIBUTE, ""_ns);
return NS_OK;
}
NS_WARNING("AlignStateAtSelection failed");
return error.StealNSResult();
}
nsCString alignment; // Don't use `nsAutoCString` to avoid copying string.
switch (state.AlignmentAtSelectionStart()) {
default:
case nsIHTMLEditor::eLeft:
alignment.AssignLiteral("left");
break;
case nsIHTMLEditor::eCenter:
alignment.AssignLiteral("center");
break;
case nsIHTMLEditor::eRight:
alignment.AssignLiteral("right");
break;
case nsIHTMLEditor::eJustify:
alignment.AssignLiteral("justify");
break;
}
aParams.SetBool(STATE_MIXED, false);
aParams.SetCString(STATE_ATTRIBUTE, alignment);
return NS_OK;
}
nsresult AlignCommand::SetState(HTMLEditor* aHTMLEditor,
const nsAString& aNewState,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(!aHTMLEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = aHTMLEditor->AlignAsAction(aNewState, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "HTMLEditor::AlignAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::AbsolutePositioningCommand
*****************************************************************************/
StaticRefPtr<AbsolutePositioningCommand> AbsolutePositioningCommand::sInstance;
nsresult AbsolutePositioningCommand::GetCurrentState(
nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor,
nsCommandParams& aParams) const {
if (!aHTMLEditor.IsAbsolutePositionEditorEnabled()) {
aParams.SetBool(STATE_MIXED, false);
aParams.SetCString(STATE_ATTRIBUTE, ""_ns);
return NS_OK;
}
RefPtr<Element> container =
aHTMLEditor.GetAbsolutelyPositionedSelectionContainer();
aParams.SetBool(STATE_MIXED, false);
aParams.SetCString(STATE_ATTRIBUTE, container ? "absolute"_ns : ""_ns);
return NS_OK;
}
nsresult AbsolutePositioningCommand::ToggleState(
nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor,
nsIPrincipal* aPrincipal) const {
RefPtr<Element> container =
aHTMLEditor.GetAbsolutelyPositionedSelectionContainer();
nsresult rv = aHTMLEditor.SetSelectionToAbsoluteOrStaticAsAction(!container,
aPrincipal);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::SetSelectionToAbsoluteOrStaticAsAction() failed");
return rv;
}
/*****************************************************************************
* mozilla::DecreaseZIndexCommand
*****************************************************************************/
StaticRefPtr<DecreaseZIndexCommand> DecreaseZIndexCommand::sInstance;
bool DecreaseZIndexCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
RefPtr<HTMLEditor> htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
if (!htmlEditor->IsAbsolutePositionEditorEnabled()) {
return false;
}
RefPtr<Element> positionedElement = htmlEditor->GetPositionedElement();
if (!positionedElement) {
return false;
}
return htmlEditor->GetZIndex(*positionedElement) > 0;
}
nsresult DecreaseZIndexCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(-1, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::AddZIndexAsAction(-1) failed");
return rv;
}
nsresult DecreaseZIndexCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::IncreaseZIndexCommand
*****************************************************************************/
StaticRefPtr<IncreaseZIndexCommand> IncreaseZIndexCommand::sInstance;
bool IncreaseZIndexCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
if (!htmlEditor->IsAbsolutePositionEditorEnabled()) {
return false;
}
return !!htmlEditor->GetPositionedElement();
}
nsresult IncreaseZIndexCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(1, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::AddZIndexAsAction(1) failed");
return rv;
}
nsresult IncreaseZIndexCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::RemoveStylesCommand
*****************************************************************************/
StaticRefPtr<RemoveStylesCommand> RemoveStylesCommand::sInstance;
bool RemoveStylesCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
// test if we have any styles?
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult RemoveStylesCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
nsresult rv =
MOZ_KnownLive(htmlEditor)->RemoveAllInlinePropertiesAsAction(aPrincipal);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::RemoveAllInlinePropertiesAsAction() failed");
return rv;
}
nsresult RemoveStylesCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::IncreaseFontSizeCommand
*****************************************************************************/
StaticRefPtr<IncreaseFontSizeCommand> IncreaseFontSizeCommand::sInstance;
bool IncreaseFontSizeCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
// test if we are at max size?
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult IncreaseFontSizeCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->IncreaseFontSizeAsAction(aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::IncreaseFontSizeAsAction() failed");
return rv;
}
nsresult IncreaseFontSizeCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::DecreaseFontSizeCommand
*****************************************************************************/
StaticRefPtr<DecreaseFontSizeCommand> DecreaseFontSizeCommand::sInstance;
bool DecreaseFontSizeCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
// test if we are at min size?
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult DecreaseFontSizeCommand::DoCommand(Command aCommand,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_OK;
}
nsresult rv = MOZ_KnownLive(htmlEditor)->DecreaseFontSizeAsAction(aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::DecreaseFontSizeAsAction() failed");
return rv;
}
nsresult DecreaseFontSizeCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::InsertHTMLCommand
*****************************************************************************/
StaticRefPtr<InsertHTMLCommand> InsertHTMLCommand::sInstance;
bool InsertHTMLCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
nsresult InsertHTMLCommand::DoCommand(Command aCommand, EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
// If InsertHTMLCommand is called with no parameters, it was probably called
// with an empty string parameter ''. In this case, it should act the same as
// the delete command
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv =
MOZ_KnownLive(htmlEditor)->InsertHTMLAsAction(u""_ns, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::InsertHTMLAsAction() failed");
return rv;
}
nsresult InsertHTMLCommand::DoCommandParam(Command aCommand,
const nsAString& aStringParam,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
if (NS_WARN_IF(aStringParam.IsVoid())) {
return NS_ERROR_INVALID_ARG;
}
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv =
MOZ_KnownLive(htmlEditor)->InsertHTMLAsAction(aStringParam, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::InsertHTMLAsAction() failed");
return rv;
}
nsresult InsertHTMLCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* mozilla::InsertTagCommand
*****************************************************************************/
StaticRefPtr<InsertTagCommand> InsertTagCommand::sInstance;
bool InsertTagCommand::IsCommandEnabled(Command aCommand,
EditorBase* aEditorBase) const {
HTMLEditor* htmlEditor = HTMLEditor::GetFrom(aEditorBase);
if (!htmlEditor) {
return false;
}
return htmlEditor->IsModifiable() && htmlEditor->IsSelectionEditable();
}
// corresponding STATE_ATTRIBUTE is: src (img) and href (a)
nsresult InsertTagCommand::DoCommand(Command aCommand, EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(tagName != nsGkAtoms::hr)) {
return NS_ERROR_NOT_IMPLEMENTED;
}
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
RefPtr<Element> newElement =
MOZ_KnownLive(htmlEditor)
->CreateElementWithDefaults(MOZ_KnownLive(*tagName));
if (NS_WARN_IF(!newElement)) {
return NS_ERROR_FAILURE;
}
nsresult rv =
MOZ_KnownLive(htmlEditor)
->InsertElementAtSelectionAsAction(newElement, true, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::InsertElementAtSelectionAsAction() failed");
return rv;
}
nsresult InsertTagCommand::DoCommandParam(Command aCommand,
const nsAString& aStringParam,
EditorBase& aEditorBase,
nsIPrincipal* aPrincipal) const {
MOZ_ASSERT(aCommand != Command::InsertHorizontalRule);
if (NS_WARN_IF(aStringParam.IsEmpty())) {
return NS_ERROR_INVALID_ARG;
}
nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED;
}
HTMLEditor* htmlEditor = aEditorBase.GetAsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
// filter out tags we don't know how to insert
nsAtom* attribute = nullptr;
if (tagName == nsGkAtoms::a) {
attribute = nsGkAtoms::href;
} else if (tagName == nsGkAtoms::img) {
attribute = nsGkAtoms::src;
} else {
return NS_ERROR_NOT_IMPLEMENTED;
}
RefPtr<Element> newElement =
MOZ_KnownLive(htmlEditor)
->CreateElementWithDefaults(MOZ_KnownLive(*tagName));
if (!newElement) {
NS_WARNING("HTMLEditor::CreateElementWithDefaults() failed");
return NS_ERROR_FAILURE;
}
ErrorResult error;
newElement->SetAttr(attribute, aStringParam, error);
if (error.Failed()) {
NS_WARNING("Element::SetAttr() failed");
return error.StealNSResult();
}
// do actual insertion
if (tagName == nsGkAtoms::a) {
nsresult rv =
MOZ_KnownLive(htmlEditor)
->InsertLinkAroundSelectionAsAction(newElement, aPrincipal);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::InsertLinkAroundSelectionAsAction() failed");
return rv;
}
nsresult rv =
MOZ_KnownLive(htmlEditor)
->InsertElementAtSelectionAsAction(newElement, true, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::InsertElementAtSelectionAsAction() failed");
return rv;
}
nsresult InsertTagCommand::GetCommandStateParams(
Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase,
nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommand, aEditorBase));
}
/*****************************************************************************
* Helper methods
*****************************************************************************/
static nsresult GetListState(HTMLEditor* aHTMLEditor, bool* aMixed,
nsAString& aLocalName) {
MOZ_ASSERT(aHTMLEditor);
MOZ_ASSERT(aMixed);
*aMixed = false;
aLocalName.Truncate();
ErrorResult error;
ListElementSelectionState state(*aHTMLEditor, error);
if (error.Failed()) {
NS_WARNING("ListElementSelectionState failed");
return error.StealNSResult();
}
if (state.IsNotOneTypeListElementSelected()) {
*aMixed = true;
return NS_OK;
}
if (state.IsOLElementSelected()) {
aLocalName.AssignLiteral("ol");
} else if (state.IsULElementSelected()) {
aLocalName.AssignLiteral("ul");
} else if (state.IsDLElementSelected()) {
aLocalName.AssignLiteral("dl");
}
return NS_OK;
}
} // namespace mozilla
|