Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/editor/libeditor/EditorCommands.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "EditorCommands.h"
7
8
#include "mozilla/ArrayUtils.h"
9
#include "mozilla/Assertions.h"
10
#include "mozilla/FlushType.h"
11
#include "mozilla/TextEditor.h"
12
#include "mozilla/dom/Selection.h"
13
#include "nsCommandParams.h"
14
#include "nsCOMPtr.h"
15
#include "nsCRT.h"
16
#include "nsDebug.h"
17
#include "nsError.h"
18
#include "nsIClipboard.h"
19
#include "nsID.h"
20
#include "nsIDocument.h"
21
#include "nsIEditor.h"
22
#include "nsISelectionController.h"
23
#include "nsITransferable.h"
24
#include "nsString.h"
25
#include "nsAString.h"
26
27
class nsISupports;
28
29
0
#define STATE_ENABLED "state_enabled"
30
0
#define STATE_DATA "state_data"
31
32
namespace mozilla {
33
34
/******************************************************************************
35
 * mozilla::EditorCommandBase
36
 ******************************************************************************/
37
38
EditorCommandBase::EditorCommandBase()
39
0
{
40
0
}
41
42
NS_IMPL_ISUPPORTS(EditorCommandBase, nsIControllerCommand)
43
44
/******************************************************************************
45
 * mozilla::UndoCommand
46
 ******************************************************************************/
47
48
NS_IMETHODIMP
49
UndoCommand::IsCommandEnabled(const char* aCommandName,
50
                              nsISupports* aCommandRefCon,
51
                              bool* aIsEnabled)
52
0
{
53
0
  if (NS_WARN_IF(!aIsEnabled)) {
54
0
    return NS_ERROR_INVALID_ARG;
55
0
  }
56
0
57
0
  *aIsEnabled = false;
58
0
59
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
60
0
  if (!editor) {
61
0
    return NS_OK;
62
0
  }
63
0
  TextEditor* textEditor = editor->AsTextEditor();
64
0
  MOZ_ASSERT(textEditor);
65
0
  if (!textEditor->IsSelectionEditable()) {
66
0
    return NS_OK;
67
0
  }
68
0
  *aIsEnabled = textEditor->CanUndo();
69
0
  return NS_OK;
70
0
}
71
72
NS_IMETHODIMP
73
UndoCommand::DoCommand(const char* aCommandName,
74
                       nsISupports* aCommandRefCon)
75
0
{
76
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
77
0
  if (!editor) {
78
0
    return NS_ERROR_FAILURE;
79
0
  }
80
0
  TextEditor* textEditor = editor->AsTextEditor();
81
0
  MOZ_ASSERT(textEditor);
82
0
  return textEditor->Undo(1);
83
0
}
84
85
NS_IMETHODIMP
86
UndoCommand::DoCommandParams(const char* aCommandName,
87
                             nsICommandParams* aParams,
88
                             nsISupports* aCommandRefCon)
89
0
{
90
0
  return DoCommand(aCommandName, aCommandRefCon);
91
0
}
92
93
NS_IMETHODIMP
94
UndoCommand::GetCommandStateParams(const char* aCommandName,
95
                                   nsICommandParams* aParams,
96
                                   nsISupports* aCommandRefCon)
97
0
{
98
0
  bool canUndo;
99
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
100
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
101
0
}
102
103
/******************************************************************************
104
 * mozilla::RedoCommand
105
 ******************************************************************************/
106
107
NS_IMETHODIMP
108
RedoCommand::IsCommandEnabled(const char* aCommandName,
109
                              nsISupports* aCommandRefCon,
110
                              bool* aIsEnabled)
111
0
{
112
0
  if (NS_WARN_IF(!aIsEnabled)) {
113
0
    return NS_ERROR_INVALID_ARG;
114
0
  }
115
0
116
0
  *aIsEnabled = false;
117
0
118
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
119
0
  if (!editor) {
120
0
    return NS_OK;
121
0
  }
122
0
  TextEditor* textEditor = editor->AsTextEditor();
123
0
  MOZ_ASSERT(textEditor);
124
0
  if (!textEditor->IsSelectionEditable()) {
125
0
    return NS_OK;
126
0
  }
127
0
  *aIsEnabled = textEditor->CanRedo();
128
0
  return NS_OK;
129
0
}
130
131
NS_IMETHODIMP
132
RedoCommand::DoCommand(const char* aCommandName,
133
                       nsISupports* aCommandRefCon)
134
0
{
135
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
136
0
  if (!editor) {
137
0
    return NS_ERROR_FAILURE;
138
0
  }
139
0
  TextEditor* textEditor = editor->AsTextEditor();
140
0
  MOZ_ASSERT(textEditor);
141
0
  return textEditor->Redo(1);
142
0
}
143
144
NS_IMETHODIMP
145
RedoCommand::DoCommandParams(const char* aCommandName,
146
                             nsICommandParams* aParams,
147
                             nsISupports* aCommandRefCon)
148
0
{
149
0
  return DoCommand(aCommandName, aCommandRefCon);
150
0
}
151
152
NS_IMETHODIMP
153
RedoCommand::GetCommandStateParams(const char* aCommandName,
154
                                   nsICommandParams* aParams,
155
                                   nsISupports* aCommandRefCon)
156
0
{
157
0
  bool canUndo;
158
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
159
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
160
0
}
161
162
/******************************************************************************
163
 * mozilla::ClearUndoCommand
164
 ******************************************************************************/
165
166
NS_IMETHODIMP
167
ClearUndoCommand::IsCommandEnabled(const char* aCommandName,
168
                                   nsISupports* aCommandRefCon,
169
                                   bool* aIsEnabled)
170
0
{
171
0
  if (NS_WARN_IF(!aIsEnabled)) {
172
0
    return NS_ERROR_INVALID_ARG;
173
0
  }
174
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
175
0
  if (!editor) {
176
0
    *aIsEnabled = false;
177
0
    return NS_OK;
178
0
  }
179
0
  TextEditor* textEditor = editor->AsTextEditor();
180
0
  MOZ_ASSERT(textEditor);
181
0
  *aIsEnabled = textEditor->IsSelectionEditable();
182
0
  return NS_OK;
183
0
}
184
185
NS_IMETHODIMP
186
ClearUndoCommand::DoCommand(const char* aCommandName,
187
                            nsISupports* aCommandRefCon)
188
0
{
189
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
190
0
  if (!editor) {
191
0
    return NS_ERROR_FAILURE;
192
0
  }
193
0
  TextEditor* textEditor = editor->AsTextEditor();
194
0
  MOZ_ASSERT(textEditor);
195
0
  // XXX Should we return NS_ERROR_FAILURE if ClearUndoRedo() returns false?
196
0
  DebugOnly<bool> clearedUndoRedo = textEditor->ClearUndoRedo();
197
0
  NS_WARNING_ASSERTION(clearedUndoRedo,
198
0
    "Failed to clear undo/redo transactions");
199
0
  return NS_OK;
200
0
}
201
202
NS_IMETHODIMP
203
ClearUndoCommand::DoCommandParams(const char* aCommandName,
204
                                  nsICommandParams* aParams,
205
                                  nsISupports* aCommandRefCon)
206
0
{
207
0
  return DoCommand(aCommandName, aCommandRefCon);
208
0
}
209
210
NS_IMETHODIMP
211
ClearUndoCommand::GetCommandStateParams(const char* aCommandName,
212
                                        nsICommandParams* aParams,
213
                                        nsISupports* aCommandRefCon)
214
0
{
215
0
  NS_ENSURE_ARG_POINTER(aParams);
216
0
217
0
  bool enabled;
218
0
  nsresult rv = IsCommandEnabled(aCommandName, aCommandRefCon, &enabled);
219
0
  NS_ENSURE_SUCCESS(rv, rv);
220
0
221
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, enabled);
222
0
}
223
224
/******************************************************************************
225
 * mozilla::CutCommand
226
 ******************************************************************************/
227
228
NS_IMETHODIMP
229
CutCommand::IsCommandEnabled(const char* aCommandName,
230
                             nsISupports* aCommandRefCon,
231
                             bool* aIsEnabled)
232
0
{
233
0
  if (NS_WARN_IF(!aIsEnabled)) {
234
0
    return NS_ERROR_INVALID_ARG;
235
0
  }
236
0
237
0
  *aIsEnabled = false;
238
0
239
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
240
0
  if (!editor) {
241
0
    return NS_OK;
242
0
  }
243
0
  TextEditor* textEditor = editor->AsTextEditor();
244
0
  MOZ_ASSERT(textEditor);
245
0
  if (!textEditor->IsSelectionEditable()) {
246
0
    return NS_OK;
247
0
  }
248
0
  return editor->CanCut(aIsEnabled);
249
0
}
250
251
NS_IMETHODIMP
252
CutCommand::DoCommand(const char* aCommandName,
253
                      nsISupports* aCommandRefCon)
254
0
{
255
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
256
0
  if (!editor) {
257
0
    return NS_ERROR_FAILURE;
258
0
  }
259
0
  TextEditor* textEditor = editor->AsTextEditor();
260
0
  MOZ_ASSERT(textEditor);
261
0
  return textEditor->Cut();
262
0
}
263
264
NS_IMETHODIMP
265
CutCommand::DoCommandParams(const char* aCommandName,
266
                            nsICommandParams* aParams,
267
                            nsISupports* aCommandRefCon)
268
0
{
269
0
  return DoCommand(aCommandName, aCommandRefCon);
270
0
}
271
272
NS_IMETHODIMP
273
CutCommand::GetCommandStateParams(const char* aCommandName,
274
                                  nsICommandParams* aParams,
275
                                  nsISupports* aCommandRefCon)
276
0
{
277
0
  bool canUndo;
278
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
279
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
280
0
}
281
282
/******************************************************************************
283
 * mozilla::CutOrDeleteCommand
284
 ******************************************************************************/
285
286
NS_IMETHODIMP
287
CutOrDeleteCommand::IsCommandEnabled(const char* aCommandName,
288
                                     nsISupports* aCommandRefCon,
289
                                     bool* aIsEnabled)
290
0
{
291
0
  if (NS_WARN_IF(!aIsEnabled)) {
292
0
    return NS_ERROR_INVALID_ARG;
293
0
  }
294
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
295
0
  if (!editor) {
296
0
    *aIsEnabled = false;
297
0
    return NS_OK;
298
0
  }
299
0
  TextEditor* textEditor = editor->AsTextEditor();
300
0
  MOZ_ASSERT(textEditor);
301
0
  *aIsEnabled = textEditor->IsSelectionEditable();
302
0
  return NS_OK;
303
0
}
304
305
NS_IMETHODIMP
306
CutOrDeleteCommand::DoCommand(const char* aCommandName,
307
                              nsISupports* aCommandRefCon)
308
0
{
309
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
310
0
  if (!editor) {
311
0
    return NS_ERROR_FAILURE;
312
0
  }
313
0
  TextEditor* textEditor = editor->AsTextEditor();
314
0
  MOZ_ASSERT(textEditor);
315
0
  dom::Selection* selection = textEditor->GetSelection();
316
0
  if (selection && selection->IsCollapsed()) {
317
0
    nsresult rv =
318
0
      textEditor->DeleteSelectionAsAction(nsIEditor::eNext,
319
0
                                          nsIEditor::eStrip);
320
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
321
0
      return rv;
322
0
    }
323
0
    return NS_OK;
324
0
  }
325
0
  return textEditor->Cut();
326
0
}
327
328
NS_IMETHODIMP
329
CutOrDeleteCommand::DoCommandParams(const char* aCommandName,
330
                                    nsICommandParams* aParams,
331
                                    nsISupports* aCommandRefCon)
332
0
{
333
0
  return DoCommand(aCommandName, aCommandRefCon);
334
0
}
335
336
NS_IMETHODIMP
337
CutOrDeleteCommand::GetCommandStateParams(const char* aCommandName,
338
                                          nsICommandParams* aParams,
339
                                          nsISupports* aCommandRefCon)
340
0
{
341
0
  bool canUndo;
342
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
343
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
344
0
}
345
346
/******************************************************************************
347
 * mozilla::CopyCommand
348
 ******************************************************************************/
349
350
NS_IMETHODIMP
351
CopyCommand::IsCommandEnabled(const char* aCommandName,
352
                              nsISupports* aCommandRefCon,
353
                              bool* aIsEnabled)
354
0
{
355
0
  if (NS_WARN_IF(!aIsEnabled)) {
356
0
    return NS_ERROR_INVALID_ARG;
357
0
  }
358
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
359
0
  if (!editor) {
360
0
    *aIsEnabled = false;
361
0
    return NS_OK;
362
0
  }
363
0
  TextEditor* textEditor = editor->AsTextEditor();
364
0
  MOZ_ASSERT(textEditor);
365
0
  return textEditor->CanCopy(aIsEnabled);
366
0
}
367
368
NS_IMETHODIMP
369
CopyCommand::DoCommand(const char* aCommandName,
370
                       nsISupports* aCommandRefCon)
371
0
{
372
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
373
0
  if (!editor) {
374
0
    return NS_ERROR_FAILURE;
375
0
  }
376
0
  TextEditor* textEditor = editor->AsTextEditor();
377
0
  MOZ_ASSERT(textEditor);
378
0
  return textEditor->Copy();
379
0
}
380
381
NS_IMETHODIMP
382
CopyCommand::DoCommandParams(const char* aCommandName,
383
                             nsICommandParams* aParams,
384
                             nsISupports* aCommandRefCon)
385
0
{
386
0
  return DoCommand(aCommandName, aCommandRefCon);
387
0
}
388
389
NS_IMETHODIMP
390
CopyCommand::GetCommandStateParams(const char* aCommandName,
391
                                   nsICommandParams* aParams,
392
                                   nsISupports* aCommandRefCon)
393
0
{
394
0
  bool canUndo;
395
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
396
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
397
0
}
398
399
/******************************************************************************
400
 * mozilla::CopyOrDeleteCommand
401
 ******************************************************************************/
402
403
NS_IMETHODIMP
404
CopyOrDeleteCommand::IsCommandEnabled(const char* aCommandName,
405
                                      nsISupports* aCommandRefCon,
406
                                      bool* aIsEnabled)
407
0
{
408
0
  if (NS_WARN_IF(!aIsEnabled)) {
409
0
    return NS_ERROR_INVALID_ARG;
410
0
  }
411
0
412
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
413
0
  if (!editor) {
414
0
    *aIsEnabled = false;
415
0
    return NS_OK;
416
0
  }
417
0
  TextEditor* textEditor = editor->AsTextEditor();
418
0
  MOZ_ASSERT(textEditor);
419
0
  *aIsEnabled = textEditor->IsSelectionEditable();
420
0
  return NS_OK;
421
0
}
422
423
NS_IMETHODIMP
424
CopyOrDeleteCommand::DoCommand(const char* aCommandName,
425
                               nsISupports* aCommandRefCon)
426
0
{
427
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
428
0
  if (!editor) {
429
0
    return NS_ERROR_FAILURE;
430
0
  }
431
0
  TextEditor* textEditor = editor->AsTextEditor();
432
0
  MOZ_ASSERT(textEditor);
433
0
  dom::Selection* selection = textEditor->GetSelection();
434
0
  if (selection && selection->IsCollapsed()) {
435
0
    nsresult rv =
436
0
      textEditor->DeleteSelectionAsAction(nsIEditor::eNextWord,
437
0
                                          nsIEditor::eStrip);
438
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
439
0
      return rv;
440
0
    }
441
0
    return NS_OK;
442
0
  }
443
0
  return textEditor->Copy();
444
0
}
445
446
NS_IMETHODIMP
447
CopyOrDeleteCommand::DoCommandParams(const char* aCommandName,
448
                                     nsICommandParams* aParams,
449
                                     nsISupports* aCommandRefCon)
450
0
{
451
0
  return DoCommand(aCommandName, aCommandRefCon);
452
0
}
453
454
NS_IMETHODIMP
455
CopyOrDeleteCommand::GetCommandStateParams(const char* aCommandName,
456
                                           nsICommandParams* aParams,
457
                                           nsISupports* aCommandRefCon)
458
0
{
459
0
  bool canUndo;
460
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
461
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
462
0
}
463
464
/******************************************************************************
465
 * mozilla::CopyAndCollapseToEndCommand
466
 ******************************************************************************/
467
468
NS_IMETHODIMP
469
CopyAndCollapseToEndCommand::IsCommandEnabled(const char* aCommandName,
470
                                              nsISupports* aCommandRefCon,
471
                                              bool* aIsEnabled)
472
0
{
473
0
  if (NS_WARN_IF(!aIsEnabled)) {
474
0
    return NS_ERROR_INVALID_ARG;
475
0
  }
476
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
477
0
  if (!editor) {
478
0
    *aIsEnabled = false;
479
0
    return NS_OK;
480
0
  }
481
0
  TextEditor* textEditor = editor->AsTextEditor();
482
0
  MOZ_ASSERT(textEditor);
483
0
  return textEditor->CanCopy(aIsEnabled);
484
0
}
485
486
NS_IMETHODIMP
487
CopyAndCollapseToEndCommand::DoCommand(const char* aCommandName,
488
                                       nsISupports* aCommandRefCon)
489
0
{
490
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
491
0
  if (!editor) {
492
0
    return NS_ERROR_FAILURE;
493
0
  }
494
0
  TextEditor* textEditor = editor->AsTextEditor();
495
0
  MOZ_ASSERT(textEditor);
496
0
  nsresult rv = textEditor->Copy();
497
0
  if (NS_FAILED(rv)) {
498
0
    return rv;
499
0
  }
500
0
  RefPtr<dom::Selection> selection = textEditor->GetSelection();
501
0
  if (selection) {
502
0
    selection->CollapseToEnd(IgnoreErrors());
503
0
  }
504
0
  return NS_OK;
505
0
}
506
507
NS_IMETHODIMP
508
CopyAndCollapseToEndCommand::DoCommandParams(const char* aCommandName,
509
                                             nsICommandParams* aParams,
510
                                             nsISupports* aCommandRefCon)
511
0
{
512
0
  return DoCommand(aCommandName, aCommandRefCon);
513
0
}
514
515
NS_IMETHODIMP
516
CopyAndCollapseToEndCommand::GetCommandStateParams(const char* aCommandName,
517
                                                   nsICommandParams* aParams,
518
                                                   nsISupports* aCommandRefCon)
519
0
{
520
0
  bool canUndo;
521
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
522
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
523
0
}
524
525
/******************************************************************************
526
 * mozilla::PasteCommand
527
 ******************************************************************************/
528
529
NS_IMETHODIMP
530
PasteCommand::IsCommandEnabled(const char* aCommandName,
531
                               nsISupports* aCommandRefCon,
532
                               bool* aIsEnabled)
533
0
{
534
0
  if (NS_WARN_IF(!aIsEnabled)) {
535
0
    return NS_ERROR_INVALID_ARG;
536
0
  }
537
0
538
0
  *aIsEnabled = false;
539
0
540
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
541
0
  if (!editor) {
542
0
    return NS_OK;
543
0
  }
544
0
  TextEditor* textEditor = editor->AsTextEditor();
545
0
  MOZ_ASSERT(textEditor);
546
0
  if (!textEditor->IsSelectionEditable()) {
547
0
    return NS_OK;
548
0
  }
549
0
  return textEditor->CanPaste(nsIClipboard::kGlobalClipboard, aIsEnabled);
550
0
}
551
552
NS_IMETHODIMP
553
PasteCommand::DoCommand(const char* aCommandName,
554
                        nsISupports* aCommandRefCon)
555
0
{
556
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
557
0
  if (NS_WARN_IF(!editor)) {
558
0
    return NS_ERROR_FAILURE;
559
0
  }
560
0
  TextEditor* textEditor = editor->AsTextEditor();
561
0
  MOZ_ASSERT(textEditor);
562
0
  return textEditor->PasteAsAction(nsIClipboard::kGlobalClipboard);
563
0
}
564
565
NS_IMETHODIMP
566
PasteCommand::DoCommandParams(const char* aCommandName,
567
                              nsICommandParams* aParams,
568
                              nsISupports* aCommandRefCon)
569
0
{
570
0
  return DoCommand(aCommandName, aCommandRefCon);
571
0
}
572
573
NS_IMETHODIMP
574
PasteCommand::GetCommandStateParams(const char* aCommandName,
575
                                    nsICommandParams* aParams,
576
                                    nsISupports* aCommandRefCon)
577
0
{
578
0
  bool canUndo;
579
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
580
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
581
0
}
582
583
/******************************************************************************
584
 * mozilla::PasteTransferableCommand
585
 ******************************************************************************/
586
587
NS_IMETHODIMP
588
PasteTransferableCommand::IsCommandEnabled(const char* aCommandName,
589
                                           nsISupports* aCommandRefCon,
590
                                           bool* aIsEnabled)
591
0
{
592
0
  if (NS_WARN_IF(!aIsEnabled)) {
593
0
    return NS_ERROR_INVALID_ARG;
594
0
  }
595
0
596
0
  *aIsEnabled = false;
597
0
598
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
599
0
  if (!editor) {
600
0
    return NS_OK;
601
0
  }
602
0
  TextEditor* textEditor = editor->AsTextEditor();
603
0
  MOZ_ASSERT(textEditor);
604
0
  if (!textEditor->IsSelectionEditable()) {
605
0
    return NS_OK;
606
0
  }
607
0
  *aIsEnabled = textEditor->CanPasteTransferable(nullptr);
608
0
  return NS_OK;
609
0
}
610
611
NS_IMETHODIMP
612
PasteTransferableCommand::DoCommand(const char* aCommandName,
613
                                    nsISupports* aCommandRefCon)
614
0
{
615
0
  return NS_ERROR_FAILURE;
616
0
}
617
618
NS_IMETHODIMP
619
PasteTransferableCommand::DoCommandParams(const char* aCommandName,
620
                                          nsICommandParams* aParams,
621
                                          nsISupports* aCommandRefCon)
622
0
{
623
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
624
0
  if (NS_WARN_IF(!editor)) {
625
0
    return NS_ERROR_FAILURE;
626
0
  }
627
0
628
0
  nsCOMPtr<nsISupports> supports =
629
0
    aParams->AsCommandParams()->GetISupports("transferable");
630
0
  if (NS_WARN_IF(!supports)) {
631
0
    return NS_ERROR_FAILURE;
632
0
  }
633
0
634
0
  nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
635
0
  if (NS_WARN_IF(!trans)) {
636
0
    return NS_ERROR_FAILURE;
637
0
  }
638
0
639
0
  TextEditor* textEditor = editor->AsTextEditor();
640
0
  MOZ_ASSERT(textEditor);
641
0
  return textEditor->PasteTransferable(trans);
642
0
}
643
644
NS_IMETHODIMP
645
PasteTransferableCommand::GetCommandStateParams(const char* aCommandName,
646
                                                nsICommandParams* aParams,
647
                                                nsISupports* aCommandRefCon)
648
0
{
649
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
650
0
  if (NS_WARN_IF(!editor)) {
651
0
    return NS_ERROR_FAILURE;
652
0
  }
653
0
654
0
  nsCOMPtr<nsISupports> supports =
655
0
    aParams->AsCommandParams()->GetISupports("transferable");
656
0
  if (NS_WARN_IF(!supports)) {
657
0
    return NS_ERROR_FAILURE;
658
0
  }
659
0
660
0
  nsCOMPtr<nsITransferable> trans;
661
0
  trans = do_QueryInterface(supports);
662
0
  if (NS_WARN_IF(!trans)) {
663
0
    return NS_ERROR_FAILURE;
664
0
  }
665
0
666
0
  TextEditor* textEditor = editor->AsTextEditor();
667
0
  MOZ_ASSERT(textEditor);
668
0
669
0
  return aParams->AsCommandParams()->SetBool(
670
0
           STATE_ENABLED, textEditor->CanPasteTransferable(trans));
671
0
}
672
673
/******************************************************************************
674
 * mozilla::SwitchTextDirectionCommand
675
 ******************************************************************************/
676
677
NS_IMETHODIMP
678
SwitchTextDirectionCommand::IsCommandEnabled(const char* aCommandName,
679
                                             nsISupports* aCommandRefCon,
680
                                             bool* aIsEnabled)
681
0
{
682
0
  if (NS_WARN_IF(!aIsEnabled)) {
683
0
    return NS_ERROR_INVALID_ARG;
684
0
  }
685
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
686
0
  if (!editor) {
687
0
    *aIsEnabled = false;
688
0
    return NS_OK;
689
0
  }
690
0
  TextEditor* textEditor = editor->AsTextEditor();
691
0
  MOZ_ASSERT(textEditor);
692
0
  *aIsEnabled = textEditor->IsSelectionEditable();
693
0
  return NS_OK;
694
0
}
695
696
NS_IMETHODIMP
697
SwitchTextDirectionCommand::DoCommand(const char* aCommandName,
698
                                      nsISupports* aCommandRefCon)
699
0
{
700
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
701
0
  if (NS_WARN_IF(!editor)) {
702
0
    return NS_ERROR_FAILURE;
703
0
  }
704
0
  TextEditor* textEditor = editor->AsTextEditor();
705
0
  MOZ_ASSERT(textEditor);
706
0
  return textEditor->ToggleTextDirection();
707
0
}
708
709
NS_IMETHODIMP
710
SwitchTextDirectionCommand::DoCommandParams(const char* aCommandName,
711
                                            nsICommandParams* aParams,
712
                                            nsISupports* aCommandRefCon)
713
0
{
714
0
  return DoCommand(aCommandName, aCommandRefCon);
715
0
}
716
717
NS_IMETHODIMP
718
SwitchTextDirectionCommand::GetCommandStateParams(const char* aCommandName,
719
                                                  nsICommandParams* aParams,
720
                                                  nsISupports* aCommandRefCon)
721
0
{
722
0
  bool canSwitchTextDirection = true;
723
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
724
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED,
725
0
                                             canSwitchTextDirection);
726
0
}
727
728
/******************************************************************************
729
 * mozilla::DeleteCommand
730
 ******************************************************************************/
731
732
NS_IMETHODIMP
733
DeleteCommand::IsCommandEnabled(const char* aCommandName,
734
                                nsISupports* aCommandRefCon,
735
                                bool* aIsEnabled)
736
0
{
737
0
  if (NS_WARN_IF(!aIsEnabled)) {
738
0
    return NS_ERROR_INVALID_ARG;
739
0
  }
740
0
741
0
  *aIsEnabled = false;
742
0
743
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
744
0
  if (!editor) {
745
0
    return NS_OK;
746
0
  }
747
0
748
0
  TextEditor* textEditor = editor->AsTextEditor();
749
0
  MOZ_ASSERT(textEditor);
750
0
751
0
  // We can generally delete whenever the selection is editable.  However,
752
0
  // cmd_delete doesn't make sense if the selection is collapsed because it's
753
0
  // directionless, which is the same condition under which we can't cut.
754
0
  *aIsEnabled = textEditor->IsSelectionEditable();
755
0
756
0
  if (!nsCRT::strcmp("cmd_delete", aCommandName) && *aIsEnabled) {
757
0
    nsresult rv = textEditor->CanDelete(aIsEnabled);
758
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
759
0
      return rv;
760
0
    }
761
0
  }
762
0
  return NS_OK;
763
0
}
764
765
NS_IMETHODIMP
766
DeleteCommand::DoCommand(const char* aCommandName,
767
                         nsISupports* aCommandRefCon)
768
0
{
769
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
770
0
  if (NS_WARN_IF(!editor)) {
771
0
    return NS_ERROR_FAILURE;
772
0
  }
773
0
774
0
  nsIEditor::EDirection deleteDir = nsIEditor::eNone;
775
0
776
0
  if (!nsCRT::strcmp("cmd_delete", aCommandName)) {
777
0
    // Really this should probably be eNone, but it only makes a difference if
778
0
    // the selection is collapsed, and then this command is disabled.  So let's
779
0
    // keep it as it always was to avoid breaking things.
780
0
    deleteDir = nsIEditor::ePrevious;
781
0
  } else if (!nsCRT::strcmp("cmd_deleteCharForward", aCommandName)) {
782
0
    deleteDir = nsIEditor::eNext;
783
0
  } else if (!nsCRT::strcmp("cmd_deleteCharBackward", aCommandName)) {
784
0
    deleteDir = nsIEditor::ePrevious;
785
0
  } else if (!nsCRT::strcmp("cmd_deleteWordBackward", aCommandName)) {
786
0
    deleteDir = nsIEditor::ePreviousWord;
787
0
  } else if (!nsCRT::strcmp("cmd_deleteWordForward", aCommandName)) {
788
0
    deleteDir = nsIEditor::eNextWord;
789
0
  } else if (!nsCRT::strcmp("cmd_deleteToBeginningOfLine", aCommandName)) {
790
0
    deleteDir = nsIEditor::eToBeginningOfLine;
791
0
  } else if (!nsCRT::strcmp("cmd_deleteToEndOfLine", aCommandName)) {
792
0
    deleteDir = nsIEditor::eToEndOfLine;
793
0
  } else {
794
0
    MOZ_CRASH("Unrecognized nsDeleteCommand");
795
0
  }
796
0
797
0
  TextEditor* textEditor = editor->AsTextEditor();
798
0
  MOZ_ASSERT(textEditor);
799
0
  nsresult rv =
800
0
    textEditor->DeleteSelectionAsAction(deleteDir, nsIEditor::eStrip);
801
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
802
0
    return rv;
803
0
  }
804
0
  return NS_OK;
805
0
}
806
807
NS_IMETHODIMP
808
DeleteCommand::DoCommandParams(const char* aCommandName,
809
                               nsICommandParams* aParams,
810
                               nsISupports* aCommandRefCon)
811
0
{
812
0
  return DoCommand(aCommandName, aCommandRefCon);
813
0
}
814
815
NS_IMETHODIMP
816
DeleteCommand::GetCommandStateParams(const char* aCommandName,
817
                                     nsICommandParams* aParams,
818
                                     nsISupports* aCommandRefCon)
819
0
{
820
0
  bool canUndo;
821
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
822
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
823
0
}
824
825
/******************************************************************************
826
 * mozilla::SelectAllCommand
827
 ******************************************************************************/
828
829
NS_IMETHODIMP
830
SelectAllCommand::IsCommandEnabled(const char* aCommandName,
831
                                   nsISupports* aCommandRefCon,
832
                                   bool* aIsEnabled)
833
0
{
834
0
  NS_ENSURE_ARG_POINTER(aIsEnabled);
835
0
836
0
  nsresult rv = NS_OK;
837
0
  // You can always select all, unless the selection is editable,
838
0
  // and the editable region is empty!
839
0
  *aIsEnabled = true;
840
0
841
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
842
0
  if (!editor) {
843
0
    return NS_OK;
844
0
  }
845
0
846
0
  // You can select all if there is an editor which is non-empty
847
0
  TextEditor* textEditor = editor->AsTextEditor();
848
0
  MOZ_ASSERT(textEditor);
849
0
  bool isEmpty = false;
850
0
  rv = textEditor->IsEmpty(&isEmpty);
851
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
852
0
    return rv;
853
0
  }
854
0
  *aIsEnabled = !isEmpty;
855
0
  return NS_OK;
856
0
}
857
858
NS_IMETHODIMP
859
SelectAllCommand::DoCommand(const char* aCommandName,
860
                            nsISupports* aCommandRefCon)
861
0
{
862
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
863
0
  if (!editor) {
864
0
    return NS_ERROR_FAILURE;
865
0
  }
866
0
  TextEditor* textEditor = editor->AsTextEditor();
867
0
  MOZ_ASSERT(textEditor);
868
0
  return textEditor->SelectAll();
869
0
}
870
871
NS_IMETHODIMP
872
SelectAllCommand::DoCommandParams(const char* aCommandName,
873
                                  nsICommandParams* aParams,
874
                                  nsISupports* aCommandRefCon)
875
0
{
876
0
  return DoCommand(aCommandName, aCommandRefCon);
877
0
}
878
879
NS_IMETHODIMP
880
SelectAllCommand::GetCommandStateParams(const char* aCommandName,
881
                                        nsICommandParams* aParams,
882
                                        nsISupports* aCommandRefCon)
883
0
{
884
0
  bool canUndo;
885
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
886
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
887
0
}
888
889
/******************************************************************************
890
 * mozilla::SelectionMoveCommands
891
 ******************************************************************************/
892
893
NS_IMETHODIMP
894
SelectionMoveCommands::IsCommandEnabled(const char* aCommandName,
895
                                        nsISupports* aCommandRefCon,
896
                                        bool* aIsEnabled)
897
0
{
898
0
  NS_ENSURE_ARG_POINTER(aIsEnabled);
899
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
900
0
  if (!editor) {
901
0
    *aIsEnabled = false;
902
0
    return NS_OK;
903
0
  }
904
0
  TextEditor* textEditor = editor->AsTextEditor();
905
0
  MOZ_ASSERT(textEditor);
906
0
  *aIsEnabled = textEditor->IsSelectionEditable();
907
0
  return NS_OK;
908
0
}
909
910
static const struct ScrollCommand {
911
  const char *reverseScroll;
912
  const char *forwardScroll;
913
  nsresult (NS_STDCALL nsISelectionController::*scroll)(bool);
914
} scrollCommands[] = {
915
  { "cmd_scrollTop", "cmd_scrollBottom",
916
    &nsISelectionController::CompleteScroll },
917
  { "cmd_scrollPageUp", "cmd_scrollPageDown",
918
    &nsISelectionController::ScrollPage },
919
  { "cmd_scrollLineUp", "cmd_scrollLineDown",
920
    &nsISelectionController::ScrollLine }
921
};
922
923
static const struct MoveCommand {
924
  const char *reverseMove;
925
  const char *forwardMove;
926
  const char *reverseSelect;
927
  const char *forwardSelect;
928
  nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool);
929
} moveCommands[] = {
930
  { "cmd_charPrevious", "cmd_charNext",
931
    "cmd_selectCharPrevious", "cmd_selectCharNext",
932
    &nsISelectionController::CharacterMove },
933
  { "cmd_linePrevious", "cmd_lineNext",
934
    "cmd_selectLinePrevious", "cmd_selectLineNext",
935
    &nsISelectionController::LineMove },
936
  { "cmd_wordPrevious", "cmd_wordNext",
937
    "cmd_selectWordPrevious", "cmd_selectWordNext",
938
    &nsISelectionController::WordMove },
939
  { "cmd_beginLine", "cmd_endLine",
940
    "cmd_selectBeginLine", "cmd_selectEndLine",
941
    &nsISelectionController::IntraLineMove },
942
  { "cmd_movePageUp", "cmd_movePageDown",
943
    "cmd_selectPageUp", "cmd_selectPageDown",
944
    &nsISelectionController::PageMove },
945
  { "cmd_moveTop", "cmd_moveBottom",
946
    "cmd_selectTop", "cmd_selectBottom",
947
    &nsISelectionController::CompleteMove }
948
};
949
950
static const struct PhysicalCommand {
951
  const char *move;
952
  const char *select;
953
  int16_t direction;
954
  int16_t amount;
955
} physicalCommands[] = {
956
  { "cmd_moveLeft", "cmd_selectLeft",
957
    nsISelectionController::MOVE_LEFT, 0 },
958
  { "cmd_moveRight", "cmd_selectRight",
959
    nsISelectionController::MOVE_RIGHT, 0 },
960
  { "cmd_moveUp", "cmd_selectUp",
961
    nsISelectionController::MOVE_UP, 0 },
962
  { "cmd_moveDown", "cmd_selectDown",
963
    nsISelectionController::MOVE_DOWN, 0 },
964
  { "cmd_moveLeft2", "cmd_selectLeft2",
965
    nsISelectionController::MOVE_LEFT, 1 },
966
  { "cmd_moveRight2", "cmd_selectRight2",
967
    nsISelectionController::MOVE_RIGHT, 1 },
968
  { "cmd_moveUp2", "cmd_selectUp2",
969
    nsISelectionController::MOVE_UP, 1 },
970
  { "cmd_moveDown2", "cmd_selectDown2",
971
    nsISelectionController::MOVE_DOWN, 1 }
972
};
973
974
NS_IMETHODIMP
975
SelectionMoveCommands::DoCommand(const char* aCommandName,
976
                                 nsISupports* aCommandRefCon)
977
0
{
978
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
979
0
  if (NS_WARN_IF(!editor)) {
980
0
    return NS_ERROR_FAILURE;
981
0
  }
982
0
983
0
  TextEditor* textEditor = editor->AsTextEditor();
984
0
  MOZ_ASSERT(textEditor);
985
0
  nsCOMPtr<nsIDocument> doc = textEditor->GetDocument();
986
0
  if (doc) {
987
0
    // Most of the commands below (possibly all of them) need layout to
988
0
    // be up to date.
989
0
    doc->FlushPendingNotifications(FlushType::Layout);
990
0
  }
991
0
992
0
  nsCOMPtr<nsISelectionController> selectionController =
993
0
    textEditor->GetSelectionController();
994
0
  if (NS_WARN_IF(!selectionController)) {
995
0
    return NS_ERROR_FAILURE;
996
0
  }
997
0
998
0
  // scroll commands
999
0
  for (size_t i = 0; i < mozilla::ArrayLength(scrollCommands); i++) {
1000
0
    const ScrollCommand &cmd = scrollCommands[i];
1001
0
    if (!nsCRT::strcmp(aCommandName, cmd.reverseScroll)) {
1002
0
      return (selectionController->*(cmd.scroll))(false);
1003
0
    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardScroll)) {
1004
0
      return (selectionController->*(cmd.scroll))(true);
1005
0
    }
1006
0
  }
1007
0
1008
0
  // caret movement/selection commands
1009
0
  for (size_t i = 0; i < mozilla::ArrayLength(moveCommands); i++) {
1010
0
    const MoveCommand &cmd = moveCommands[i];
1011
0
    if (!nsCRT::strcmp(aCommandName, cmd.reverseMove)) {
1012
0
      return (selectionController->*(cmd.move))(false, false);
1013
0
    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardMove)) {
1014
0
      return (selectionController->*(cmd.move))(true, false);
1015
0
    } else if (!nsCRT::strcmp(aCommandName, cmd.reverseSelect)) {
1016
0
      return (selectionController->*(cmd.move))(false, true);
1017
0
    } else if (!nsCRT::strcmp(aCommandName, cmd.forwardSelect)) {
1018
0
      return (selectionController->*(cmd.move))(true, true);
1019
0
    }
1020
0
  }
1021
0
1022
0
  // physical-direction movement/selection
1023
0
  for (size_t i = 0; i < mozilla::ArrayLength(physicalCommands); i++) {
1024
0
    const PhysicalCommand &cmd = physicalCommands[i];
1025
0
    if (!nsCRT::strcmp(aCommandName, cmd.move)) {
1026
0
      return selectionController->PhysicalMove(cmd.direction, cmd.amount,
1027
0
                                               false);
1028
0
    } else if (!nsCRT::strcmp(aCommandName, cmd.select)) {
1029
0
      return selectionController->PhysicalMove(cmd.direction, cmd.amount,
1030
0
                                               true);
1031
0
    }
1032
0
  }
1033
0
1034
0
  return NS_ERROR_FAILURE;
1035
0
}
1036
1037
NS_IMETHODIMP
1038
SelectionMoveCommands::DoCommandParams(const char* aCommandName,
1039
                                       nsICommandParams* aParams,
1040
                                       nsISupports* aCommandRefCon)
1041
0
{
1042
0
  return DoCommand(aCommandName, aCommandRefCon);
1043
0
}
1044
1045
NS_IMETHODIMP
1046
SelectionMoveCommands::GetCommandStateParams(const char* aCommandName,
1047
                                             nsICommandParams* aParams,
1048
                                             nsISupports* aCommandRefCon)
1049
0
{
1050
0
  bool canUndo;
1051
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
1052
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
1053
0
}
1054
1055
/******************************************************************************
1056
 * mozilla::InsertPlaintextCommand
1057
 ******************************************************************************/
1058
1059
NS_IMETHODIMP
1060
InsertPlaintextCommand::IsCommandEnabled(const char* aCommandName,
1061
                                         nsISupports* aCommandRefCon,
1062
                                         bool* aIsEnabled)
1063
0
{
1064
0
  if (NS_WARN_IF(!aIsEnabled)) {
1065
0
    return NS_ERROR_INVALID_ARG;
1066
0
  }
1067
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1068
0
  if (NS_WARN_IF(!editor)) {
1069
0
    *aIsEnabled = false;
1070
0
    return NS_ERROR_FAILURE;
1071
0
  }
1072
0
  TextEditor* textEditor = editor->AsTextEditor();
1073
0
  MOZ_ASSERT(textEditor);
1074
0
  *aIsEnabled = textEditor->IsSelectionEditable();
1075
0
  return NS_OK;
1076
0
}
1077
1078
NS_IMETHODIMP
1079
InsertPlaintextCommand::DoCommand(const char* aCommandName,
1080
                                  nsISupports* aCommandRefCon)
1081
0
{
1082
0
  // No value is equivalent to empty string
1083
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1084
0
  if (NS_WARN_IF(!editor)) {
1085
0
    return NS_ERROR_FAILURE;
1086
0
  }
1087
0
  TextEditor* textEditor = editor->AsTextEditor();
1088
0
  MOZ_ASSERT(textEditor);
1089
0
  // XXX InsertTextAsAction() is not same as OnInputText().  However, other
1090
0
  //     commands to insert line break or paragraph separator use OnInput*().
1091
0
  //     According to the semantics of those methods, using *AsAction() is
1092
0
  //     better, however, this may not cause two or more placeholder
1093
0
  //     transactions to the top transaction since its name may not be
1094
0
  //     nsGkAtoms::TypingTxnName.
1095
0
  DebugOnly<nsresult> rv = textEditor->InsertTextAsAction(EmptyString());
1096
0
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert empty string");
1097
0
  return NS_OK;
1098
0
}
1099
1100
NS_IMETHODIMP
1101
InsertPlaintextCommand::DoCommandParams(const char* aCommandName,
1102
                                        nsICommandParams* aParams,
1103
                                        nsISupports* aCommandRefCon)
1104
0
{
1105
0
  if (NS_WARN_IF(!aParams)) {
1106
0
    return NS_ERROR_INVALID_ARG;
1107
0
  }
1108
0
1109
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1110
0
  if (NS_WARN_IF(!editor)) {
1111
0
    return NS_ERROR_FAILURE;
1112
0
  }
1113
0
1114
0
  // Get text to insert from command params
1115
0
  nsAutoString text;
1116
0
  nsresult rv = aParams->AsCommandParams()->GetString(STATE_DATA, text);
1117
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1118
0
    return rv;
1119
0
  }
1120
0
1121
0
  TextEditor* textEditor = editor->AsTextEditor();
1122
0
  MOZ_ASSERT(textEditor);
1123
0
  // XXX InsertTextAsAction() is not same as OnInputText().  However, other
1124
0
  //     commands to insert line break or paragraph separator use OnInput*().
1125
0
  //     According to the semantics of those methods, using *AsAction() is
1126
0
  //     better, however, this may not cause two or more placeholder
1127
0
  //     transactions to the top transaction since its name may not be
1128
0
  //     nsGkAtoms::TypingTxnName.
1129
0
  rv = textEditor->InsertTextAsAction(text);
1130
0
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert the text");
1131
0
  return NS_OK;
1132
0
}
1133
1134
NS_IMETHODIMP
1135
InsertPlaintextCommand::GetCommandStateParams(const char* aCommandName,
1136
                                              nsICommandParams* aParams,
1137
                                              nsISupports* aCommandRefCon)
1138
0
{
1139
0
  if (NS_WARN_IF(!aParams)) {
1140
0
    return NS_ERROR_INVALID_ARG;
1141
0
  }
1142
0
1143
0
  bool aIsEnabled = false;
1144
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
1145
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
1146
0
}
1147
1148
/******************************************************************************
1149
 * mozilla::InsertParagraphCommand
1150
 ******************************************************************************/
1151
1152
NS_IMETHODIMP
1153
InsertParagraphCommand::IsCommandEnabled(const char* aCommandName,
1154
                                         nsISupports* aCommandRefCon,
1155
                                         bool* aIsEnabled)
1156
0
{
1157
0
  if (NS_WARN_IF(!aIsEnabled)) {
1158
0
    return NS_ERROR_INVALID_ARG;
1159
0
  }
1160
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1161
0
  if (NS_WARN_IF(!editor)) {
1162
0
    *aIsEnabled = false;
1163
0
    return NS_ERROR_FAILURE;
1164
0
  }
1165
0
1166
0
  TextEditor* textEditor = editor->AsTextEditor();
1167
0
  MOZ_ASSERT(textEditor);
1168
0
  *aIsEnabled = textEditor->IsSelectionEditable();
1169
0
  return NS_OK;
1170
0
}
1171
1172
NS_IMETHODIMP
1173
InsertParagraphCommand::DoCommand(const char* aCommandName,
1174
                                  nsISupports* aCommandRefCon)
1175
0
{
1176
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1177
0
  if (NS_WARN_IF(!editor)) {
1178
0
    return NS_ERROR_FAILURE;
1179
0
  }
1180
0
1181
0
  TextEditor* textEditor = editor->AsTextEditor();
1182
0
  MOZ_ASSERT(textEditor);
1183
0
  // XXX OnInputParagraphSeparator() is a handler of user input.  So, this
1184
0
  //     call may not be expected.
1185
0
  return textEditor->OnInputParagraphSeparator();
1186
0
}
1187
1188
NS_IMETHODIMP
1189
InsertParagraphCommand::DoCommandParams(const char* aCommandName,
1190
                                        nsICommandParams* aParams,
1191
                                        nsISupports* aCommandRefCon)
1192
0
{
1193
0
  return DoCommand(aCommandName, aCommandRefCon);
1194
0
}
1195
1196
NS_IMETHODIMP
1197
InsertParagraphCommand::GetCommandStateParams(const char* aCommandName,
1198
                                              nsICommandParams* aParams,
1199
                                              nsISupports* aCommandRefCon)
1200
0
{
1201
0
  if (NS_WARN_IF(!aParams)) {
1202
0
    return NS_ERROR_INVALID_ARG;
1203
0
  }
1204
0
1205
0
  bool aIsEnabled = false;
1206
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
1207
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
1208
0
}
1209
1210
/******************************************************************************
1211
 * mozilla::InsertLineBreakCommand
1212
 ******************************************************************************/
1213
1214
NS_IMETHODIMP
1215
InsertLineBreakCommand::IsCommandEnabled(const char* aCommandName,
1216
                                         nsISupports* aCommandRefCon,
1217
                                         bool* aIsEnabled)
1218
0
{
1219
0
  if (NS_WARN_IF(!aIsEnabled)) {
1220
0
    return NS_ERROR_INVALID_ARG;
1221
0
  }
1222
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1223
0
  if (NS_WARN_IF(!editor)) {
1224
0
    *aIsEnabled = false;
1225
0
    return NS_ERROR_FAILURE;
1226
0
  }
1227
0
1228
0
  TextEditor* textEditor = editor->AsTextEditor();
1229
0
  MOZ_ASSERT(textEditor);
1230
0
  *aIsEnabled = textEditor->IsSelectionEditable();
1231
0
  return NS_OK;
1232
0
}
1233
1234
NS_IMETHODIMP
1235
InsertLineBreakCommand::DoCommand(const char* aCommandName,
1236
                                  nsISupports* aCommandRefCon)
1237
0
{
1238
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1239
0
  if (NS_WARN_IF(!editor)) {
1240
0
    return NS_ERROR_FAILURE;
1241
0
  }
1242
0
1243
0
  HTMLEditor* htmlEditor = editor->AsHTMLEditor();
1244
0
  if (!htmlEditor) {
1245
0
    return NS_ERROR_FAILURE;
1246
0
  }
1247
0
  // XXX OnInputLineBreak() is a handler of user input.  So, this call may not
1248
0
  //     be expected.
1249
0
  return htmlEditor->OnInputLineBreak();
1250
0
}
1251
1252
NS_IMETHODIMP
1253
InsertLineBreakCommand::DoCommandParams(const char* aCommandName,
1254
                                        nsICommandParams* aParams,
1255
                                        nsISupports* aCommandRefCon)
1256
0
{
1257
0
  return DoCommand(aCommandName, aCommandRefCon);
1258
0
}
1259
1260
NS_IMETHODIMP
1261
InsertLineBreakCommand::GetCommandStateParams(const char* aCommandName,
1262
                                              nsICommandParams* aParams,
1263
                                              nsISupports* aCommandRefCon)
1264
0
{
1265
0
  if (NS_WARN_IF(!aParams)) {
1266
0
    return NS_ERROR_INVALID_ARG;
1267
0
  }
1268
0
1269
0
  bool aIsEnabled = false;
1270
0
  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
1271
0
  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
1272
0
}
1273
1274
/******************************************************************************
1275
 * mozilla::PasteQuotationCommand
1276
 ******************************************************************************/
1277
1278
NS_IMETHODIMP
1279
PasteQuotationCommand::IsCommandEnabled(const char* aCommandName,
1280
                                        nsISupports* aCommandRefCon,
1281
                                        bool* aIsEnabled)
1282
0
{
1283
0
  if (NS_WARN_IF(!aIsEnabled)) {
1284
0
    return NS_ERROR_INVALID_ARG;
1285
0
  }
1286
0
1287
0
  *aIsEnabled = false;
1288
0
1289
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1290
0
  if (!editor) {
1291
0
    return NS_OK;
1292
0
  }
1293
0
  TextEditor* textEditor = editor->AsTextEditor();
1294
0
  MOZ_ASSERT(textEditor);
1295
0
  if (textEditor->IsSingleLineEditor()) {
1296
0
    return NS_OK;
1297
0
  }
1298
0
  return textEditor->CanPaste(nsIClipboard::kGlobalClipboard, aIsEnabled);
1299
0
}
1300
1301
NS_IMETHODIMP
1302
PasteQuotationCommand::DoCommand(const char* aCommandName,
1303
                                 nsISupports* aCommandRefCon)
1304
0
{
1305
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1306
0
  if (NS_WARN_IF(!editor)) {
1307
0
    return NS_ERROR_FAILURE;
1308
0
  }
1309
0
  TextEditor* textEditor = editor->AsTextEditor();
1310
0
  MOZ_ASSERT(textEditor);
1311
0
  return textEditor->PasteAsQuotationAsAction(nsIClipboard::kGlobalClipboard);
1312
0
}
1313
1314
NS_IMETHODIMP
1315
PasteQuotationCommand::DoCommandParams(const char* aCommandName,
1316
                                       nsICommandParams* aParams,
1317
                                       nsISupports* aCommandRefCon)
1318
0
{
1319
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1320
0
  if (!editor) {
1321
0
    return NS_ERROR_FAILURE;
1322
0
  }
1323
0
  TextEditor* textEditor = editor->AsTextEditor();
1324
0
  MOZ_ASSERT(textEditor);
1325
0
  return textEditor->PasteAsQuotationAsAction(nsIClipboard::kGlobalClipboard);
1326
0
}
1327
1328
NS_IMETHODIMP
1329
PasteQuotationCommand::GetCommandStateParams(const char* aCommandName,
1330
                                             nsICommandParams* aParams,
1331
                                             nsISupports* aCommandRefCon)
1332
0
{
1333
0
  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
1334
0
  if (!editor) {
1335
0
    return NS_OK;
1336
0
  }
1337
0
  TextEditor* textEditor = editor->AsTextEditor();
1338
0
  MOZ_ASSERT(textEditor);
1339
0
  bool enabled = false;
1340
0
  textEditor->CanPaste(nsIClipboard::kGlobalClipboard, &enabled);
1341
0
  aParams->AsCommandParams()->SetBool(STATE_ENABLED, enabled);
1342
0
  return NS_OK;
1343
0
}
1344
1345
} // namespace mozilla