DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
/* -*- 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/. */

#ifndef mozilla_EditorCommands_h
#define mozilla_EditorCommands_h

#include "mozilla/EventForwards.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/TypedEnumBits.h"
#include "nsGkAtoms.h"
#include "nsIControllerCommand.h"
#include "nsIPrincipal.h"
#include "nsISupportsImpl.h"
#include "nsRefPtrHashtable.h"
#include "nsStringFwd.h"

class nsAtom;
class nsCommandParams;
class nsICommandParams;
class nsIEditingSession;
class nsITransferable;
class nsStaticAtom;

namespace mozilla {

class HTMLEditor;
class TextEditor;

/**
 * EditorCommandParamType tells you that EditorCommand subclasses refer
 * which type in nsCommandParams (e.g., bool or nsString) or do not refer.
 * If they refer some types, also set where is in nsCommandParams, e.g.,
 * whether "state_attribute" or "state_data".
 */
enum class EditorCommandParamType : uint16_t {
  // The command does not take params (even if specified, always ignored).
  None = 0,
  // The command refers nsCommandParams::GetBool() result.
  Bool = 1 << 0,
  // The command refers nsCommandParams::GetString() result.
  // This may be specified with CString.  In such case,
  // nsCommandParams::GetCString() is preferred.
  String = 1 << 1,
  // The command refers nsCommandParams::GetCString() result.
  CString = 1 << 2,
  // The command refers nsCommandParams::GetISupports("transferable") result.
  Transferable = 1 << 3,

  // The command refres "state_attribute" of nsCommandParams when calling
  // GetBool()/GetString()/GetCString().  This must not be set when the
  // type is None or Transferable.
  StateAttribute = 1 << 14,
  // The command refers "state_data" of nsCommandParams when calling
  // GetBool()/GetString()/GetCString().  This must not be set when the
  // type is None or Transferable.
  StateData = 1 << 15,
};

MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(EditorCommandParamType)

/**
 * This is a base class for commands registered with the editor controller.
 * Note that such commands are designed as singleton classes.  So, MUST be
 * stateless. Any state must be stored via the refCon (an nsIEditor).
 */

class EditorCommand : public nsIControllerCommand {
 public:
  NS_DECL_ISUPPORTS

  static EditorCommandParamType GetParamType(Command aCommand) {
    // Keep same order of registration in EditorController.cpp and
    // HTMLEditorController.cpp.
    switch (aCommand) {
      // UndoCommand
      case Command::HistoryUndo:
        return EditorCommandParamType::None;
      // RedoCommand
      case Command::HistoryRedo:
        return EditorCommandParamType::None;
      // CutCommand
      case Command::Cut:
        return EditorCommandParamType::None;
      // CutOrDeleteCommand
      case Command::CutOrDelete:
        return EditorCommandParamType::None;
      // CopyCommand
      case Command::Copy:
        return EditorCommandParamType::None;
      // CopyOrDeleteCommand
      case Command::CopyOrDelete:
        return EditorCommandParamType::None;
      // SelectAllCommand
      case Command::SelectAll:
        return EditorCommandParamType::None;
      // PasteCommand
      case Command::Paste:
        return EditorCommandParamType::None;
      case Command::PasteTransferable:
        return EditorCommandParamType::Transferable;
      // SwitchTextDirectionCommand
      case Command::FormatSetBlockTextDirection:
        return EditorCommandParamType::None;
      // DeleteCommand
      case Command::Delete:
      case Command::DeleteCharBackward:
      case Command::DeleteCharForward:
      case Command::DeleteWordBackward:
      case Command::DeleteWordForward:
      case Command::DeleteToBeginningOfLine:
      case Command::DeleteToEndOfLine:
        return EditorCommandParamType::None;
      // InsertPlaintextCommand
      case Command::InsertText:
        return EditorCommandParamType::String |
               EditorCommandParamType::StateData;
      // InsertParagraphCommand
      case Command::InsertParagraph:
        return EditorCommandParamType::None;
      // InsertLineBreakCommand
      case Command::InsertLineBreak:
        return EditorCommandParamType::None;
      // PasteQuotationCommand
      case Command::PasteAsQuotation:
        return EditorCommandParamType::None;

      // SelectionMoveCommand
      case Command::ScrollTop:
      case Command::ScrollBottom:
      case Command::MoveTop:
      case Command::MoveBottom:
      case Command::SelectTop:
      case Command::SelectBottom:
      case Command::LineNext:
      case Command::LinePrevious:
      case Command::SelectLineNext:
      case Command::SelectLinePrevious:
      case Command::CharPrevious:
      case Command::CharNext:
      case Command::SelectCharPrevious:
      case Command::SelectCharNext:
      case Command::BeginLine:
      case Command::EndLine:
      case Command::SelectBeginLine:
      case Command::SelectEndLine:
      case Command::WordPrevious:
      case Command::WordNext:
      case Command::SelectWordPrevious:
      case Command::SelectWordNext:
      case Command::ScrollPageUp:
      case Command::ScrollPageDown:
      case Command::ScrollLineUp:
      case Command::ScrollLineDown:
      case Command::MovePageUp:
      case Command::MovePageDown:
      case Command::SelectPageUp:
      case Command::SelectPageDown:
      case Command::MoveLeft:
      case Command::MoveRight:
      case Command::MoveUp:
      case Command::MoveDown:
      case Command::MoveLeft2:
      case Command::MoveRight2:
      case Command::MoveUp2:
      case Command::MoveDown2:
      case Command::SelectLeft:
      case Command::SelectRight:
      case Command::SelectUp:
      case Command::SelectDown:
      case Command::SelectLeft2:
      case Command::SelectRight2:
      case Command::SelectUp2:
      case Command::SelectDown2:
        return EditorCommandParamType::None;
      // PasteNoFormattingCommand
      case Command::PasteWithoutFormat:
        return EditorCommandParamType::None;

      // DocumentStateCommand
      case Command::EditorObserverDocumentCreated:
      case Command::EditorObserverDocumentLocationChanged:
      case Command::EditorObserverDocumentWillBeDestroyed:
        return EditorCommandParamType::None;
      // SetDocumentStateCommand
      case Command::SetDocumentModified:
      case Command::SetDocumentUseCSS:
      case Command::SetDocumentReadOnly:
      case Command::SetDocumentInsertBROnEnterKeyPress:
        return EditorCommandParamType::Bool |
               EditorCommandParamType::StateAttribute;
      case Command::SetDocumentDefaultParagraphSeparator:
        return EditorCommandParamType::CString |
               EditorCommandParamType::StateAttribute;
      case Command::ToggleObjectResizers:
      case Command::ToggleInlineTableEditor:
      case Command::ToggleAbsolutePositionEditor:
        return EditorCommandParamType::Bool |
               EditorCommandParamType::StateAttribute;

      // IndentCommand
      case Command::FormatIndent:
        return EditorCommandParamType::None;
      // OutdentCommand
      case Command::FormatOutdent:
        return EditorCommandParamType::None;
      // StyleUpdatingCommand
      case Command::FormatBold:
      case Command::FormatItalic:
      case Command::FormatUnderline:
      case Command::FormatTeletypeText:
      case Command::FormatStrikeThrough:
      case Command::FormatSuperscript:
      case Command::FormatSubscript:
      case Command::FormatNoBreak:
      case Command::FormatEmphasis:
      case Command::FormatStrong:
      case Command::FormatCitation:
      case Command::FormatAbbreviation:
      case Command::FormatAcronym:
      case Command::FormatCode:
      case Command::FormatSample:
      case Command::FormatVariable:
      case Command::FormatRemoveLink:
        return EditorCommandParamType::None;
      // ListCommand
      case Command::InsertOrderedList:
      case Command::InsertUnorderedList:
        return EditorCommandParamType::None;
      // ListItemCommand
      case Command::InsertDefinitionTerm:
      case Command::InsertDefinitionDetails:
        return EditorCommandParamType::None;
      // RemoveListCommand
      case Command::FormatRemoveList:
        return EditorCommandParamType::None;
      // ParagraphStateCommand
      case Command::FormatBlock:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // FontFaceStateCommand
      case Command::FormatFontName:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // FontSizeStateCommand
      case Command::FormatFontSize:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // FontColorStateCommand
      case Command::FormatFontColor:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // BackgroundColorStateCommand
      case Command::FormatDocumentBackgroundColor:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // HighlightColorStateCommand
      case Command::FormatBackColor:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // AlignCommand:
      case Command::FormatJustifyLeft:
      case Command::FormatJustifyRight:
      case Command::FormatJustifyCenter:
      case Command::FormatJustifyFull:
      case Command::FormatJustifyNone:
        return EditorCommandParamType::CString |
               EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      // RemoveStylesCommand
      case Command::FormatRemove:
        return EditorCommandParamType::None;
      // IncreaseFontSizeCommand
      case Command::FormatIncreaseFontSize:
        return EditorCommandParamType::None;
      // DecreaseFontSizeCommand
      case Command::FormatDecreaseFontSize:
        return EditorCommandParamType::None;
      // InsertHTMLCommand
      case Command::InsertHTML:
        return EditorCommandParamType::String |
               EditorCommandParamType::StateData;
      // InsertTagCommand
      case Command::InsertLink:
      case Command::InsertImage:
        return EditorCommandParamType::String |
               EditorCommandParamType::StateAttribute;
      case Command::InsertHorizontalRule:
        return EditorCommandParamType::None;
      // AbsolutePositioningCommand
      case Command::FormatAbsolutePosition:
        return EditorCommandParamType::None;
      // DecreaseZIndexCommand
      case Command::FormatDecreaseZIndex:
        return EditorCommandParamType::None;
      // IncreaseZIndexCommand
      case Command::FormatIncreaseZIndex:
        return EditorCommandParamType::None;

      // nsClipboardGetContentsCommand
      // XXX nsClipboardGetContentsCommand is implemented by
      //     nsGlobalWindowCommands.cpp but cmd_getContents command is not
      //     used internally, but it's accessible from JS with
      //     queryCommandValue(), etc.  So, this class is out of scope of
      //     editor module for now but we should return None for making
      //     Document code simpler.  We should reimplement the command class
      //     in editor later for making Document's related methods possible
      //     to access directly.  Anyway, it does not support `DoCommand()`
      //     nor `DoCommandParams()` so that let's return `None` here.
      case Command::GetHTML:
        return EditorCommandParamType::None;

      default:
        MOZ_ASSERT_UNREACHABLE("Unknown Command");
        return EditorCommandParamType::None;
    }
  }

  // nsIControllerCommand methods.  Use EditorCommand specific methods instead
  // for internal use.
  MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD
  IsCommandEnabled(const char* aCommandName, nsISupports* aCommandRefCon,
                   bool* aIsEnabled) final;
  MOZ_CAN_RUN_SCRIPT NS_IMETHOD DoCommand(const char* aCommandName,
                                          nsISupports* aCommandRefCon) final;
  MOZ_CAN_RUN_SCRIPT NS_IMETHOD
  DoCommandParams(const char* aCommandName, nsICommandParams* aParams,
                  nsISupports* aCommandRefCon) final;
  MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD
  GetCommandStateParams(const char* aCommandName, nsICommandParams* aParams,
                        nsISupports* aCommandRefCon) final;

  MOZ_CAN_RUN_SCRIPT virtual bool IsCommandEnabled(
      Command aCommand, TextEditor* aTextEditor) const = 0;
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommand(
      Command aCommand, TextEditor& aTextEditor,
      nsIPrincipal* aPrincipal) const = 0;

  /**
   * @param aTextEditor         If the context is an editor, should be set to
   *                            it.  Otherwise, nullptr.
   * @param aEditingSession     If the context is an editing session, should be
   *                            set to it.  This usually occurs if editor has
   *                            not been created yet during initialization.
   *                            Otherwise, nullptr.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult GetCommandStateParams(
      Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
      nsIEditingSession* aEditingSession) const = 0;

  /**
   * Called only when the result of EditorCommand::GetParamType(aCommand) is
   * EditorCommandParamType::None.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(
      Command aCommand, TextEditor& aTextEditor,
      nsIPrincipal* aPrincipal) const {
    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  /**
   * Called only when the result of EditorCommand::GetParamType(aCommand)
   * includes EditorCommandParamType::Bool.  If aBoolParam is Nothing, it
   * means that given param was nullptr.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(
      Command aCommand, const Maybe<bool>& aBoolParam, TextEditor& aTextEditor,
      nsIPrincipal* aPrincipal) const {
    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  /**
   * Called only when the result of EditorCommand::GetParamType(aCommand)
   * includes EditorCommandParamType::CString.  If aCStringParam is void, it
   * means that given param was nullptr.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(
      Command aCommand, const nsACString& aCStringParam,
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const {
    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  /**
   * Called only when the result of EditorCommand::GetParamType(aCommand)
   * includes EditorCommandParamType::String.  If aStringParam is void, it
   * means that given param was nullptr.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(
      Command aCommand, const nsAString& aStringParam, TextEditor& aTextEditor,
      nsIPrincipal* aPrincipal) const {
    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  /**
   * Called only when the result of EditorCommand::GetParamType(aCommand) is
   * EditorCommandParamType::Transferable.  If aTransferableParam may be
   * nullptr.
   */
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(
      Command aCommand, nsITransferable* aTransferableParam,
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const {
    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

 protected:
  EditorCommand() = default;
  virtual ~EditorCommand() = default;
};

#define NS_DECL_EDITOR_COMMAND_COMMON_METHODS                              \
 public:                                                                   \
  MOZ_CAN_RUN_SCRIPT virtual bool IsCommandEnabled(                        \
      Command aCommand, TextEditor* aTextEditor) const final;              \
  using EditorCommand::IsCommandEnabled;                                   \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommand(                           \
      Command aCommand, TextEditor& aTextEditor, nsIPrincipal* aPrincipal) \
      const final;                                                         \
  using EditorCommand::DoCommand;                                          \
  MOZ_CAN_RUN_SCRIPT virtual nsresult GetCommandStateParams(               \
      Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor, \
      nsIEditingSession* aEditingSession) const final;                     \
  using EditorCommand::GetCommandStateParams;                              \
  using EditorCommand::DoCommandParam;

#define NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND                    \
 public:                                                                   \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(                      \
      Command aCommand, TextEditor& aTextEditor, nsIPrincipal* aPrincipal) \
      const final {                                                        \
    return DoCommand(aCommand, aTextEditor, aPrincipal);                   \
  }

#define NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM        \
 public:                                               \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(  \
      Command aCommand, const Maybe<bool>& aBoolParam, \
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const final;

#define NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM       \
 public:                                                 \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(    \
      Command aCommand, const nsACString& aCStringParam, \
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const final;

#define NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM      \
 public:                                               \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(  \
      Command aCommand, const nsAString& aStringParam, \
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const final;

#define NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM      \
 public:                                                     \
  MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam(        \
      Command aCommand, nsITransferable* aTransferableParam, \
      TextEditor& aTextEditor, nsIPrincipal* aPrincipal) const final;

#define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
 public:                                                   \
  static EditorCommand* GetInstance() {                    \
    if (!sInstance) {                                      \
      sInstance = new _cmd();                              \
    }                                                      \
    return sInstance;                                      \
  }                                                        \
                                                           \
  static void Shutdown() { sInstance = nullptr; }          \
                                                           \
 private:                                                  \
  static StaticRefPtr<_cmd> sInstance;

#define NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(_cmd) \
  class _cmd final : public EditorCommand {                     \
    NS_DECL_EDITOR_COMMAND_COMMON_METHODS                       \
    NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND             \
    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd)          \
                                                                \
   protected:                                                   \
    _cmd() = default;                                           \
    virtual ~_cmd() = default;                                  \
  };

#define NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM(_cmd)    \
  class _cmd final : public EditorCommand {            \
    NS_DECL_EDITOR_COMMAND_COMMON_METHODS              \
    NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM            \
    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
                                                       \
   protected:                                          \
    _cmd() = default;                                  \
    virtual ~_cmd() = default;                         \
  };

#define NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM(_cmd) \
  class _cmd final : public EditorCommand {            \
    NS_DECL_EDITOR_COMMAND_COMMON_METHODS              \
    NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM         \
    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
                                                       \
   protected:                                          \
    _cmd() = default;                                  \
    virtual ~_cmd() = default;                         \
  };

#define NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(_cmd)  \
  class _cmd final : public EditorCommand {            \
    NS_DECL_EDITOR_COMMAND_COMMON_METHODS              \
    NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM          \
    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
                                                       \
   protected:                                          \
    _cmd() = default;                                  \
    virtual ~_cmd() = default;                         \
  };

#define NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(_cmd) \
  class _cmd final : public EditorCommand {                 \
    NS_DECL_EDITOR_COMMAND_COMMON_METHODS                   \
    NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM         \
    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd)      \
                                                            \
   protected:                                               \
    _cmd() = default;                                       \
    virtual ~_cmd() = default;                              \
  };

// basic editor commands
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(UndoCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RedoCommand)

NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutOrDeleteCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyOrDeleteCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteCommand)
NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(PasteTransferableCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SwitchTextDirectionCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DeleteCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectAllCommand)

NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectionMoveCommands)

// Insert content commands
NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertPlaintextCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(InsertParagraphCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(InsertLineBreakCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteQuotationCommand)

/******************************************************************************
 * Commands for HTML editor
 ******************************************************************************/

// virtual base class for commands that need to save and update Boolean state
// (like styles etc)
class StateUpdatingCommandBase : public EditorCommand {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(StateUpdatingCommandBase, EditorCommand)

  NS_DECL_EDITOR_COMMAND_COMMON_METHODS
  NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND

 protected:
  StateUpdatingCommandBase() = default;
  virtual ~StateUpdatingCommandBase() = default;

  // get the current state (on or off) for this style or block format
  MOZ_CAN_RUN_SCRIPT virtual nsresult GetCurrentState(
      nsAtom* aTagName, HTMLEditor* aHTMLEditor,
      nsCommandParams& aParams) const = 0;

  // add/remove the style
  MOZ_CAN_RUN_SCRIPT virtual nsresult ToggleState(
      nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor,
      nsIPrincipal* aPrincipal) const = 0;

  static nsStaticAtom* GetTagName(Command aCommand) {
    switch (aCommand) {
      case Command::FormatBold:
        return nsGkAtoms::b;
      case Command::FormatItalic:
        return nsGkAtoms::i;
      case Command::FormatUnderline:
        return nsGkAtoms::u;
      case Command::FormatTeletypeText:
        return nsGkAtoms::tt;
      case Command::FormatStrikeThrough:
        return nsGkAtoms::strike;
      case Command::FormatSuperscript:
        return nsGkAtoms::sup;
      case Command::FormatSubscript:
        return nsGkAtoms::sub;
      case Command::FormatNoBreak:
        return nsGkAtoms::nobr;
      case Command::FormatEmphasis:
        return nsGkAtoms::em;
      case Command::FormatStrong:
        return nsGkAtoms::strong;
      case Command::FormatCitation:
        return nsGkAtoms::cite;
      case Command::FormatAbbreviation:
        return nsGkAtoms::abbr;
      case Command::FormatAcronym:
        return nsGkAtoms::acronym;
      case Command::FormatCode:
        return nsGkAtoms::code;
      case Command::FormatSample:
        return nsGkAtoms::samp;
      case Command::FormatVariable:
        return nsGkAtoms::var;
      case Command::FormatRemoveLink:
        return nsGkAtoms::href;
      case Command::InsertOrderedList:
        return nsGkAtoms::ol;
      case Command::InsertUnorderedList:
        return nsGkAtoms::ul;
      case Command::InsertDefinitionTerm:
        return nsGkAtoms::dt;
      case Command::InsertDefinitionDetails:
        return nsGkAtoms::dd;
      case Command::FormatAbsolutePosition:
        return nsGkAtoms::_empty;
      default:
        return nullptr;
    }
  }
  friend class InsertTagCommand;  // for allowing it to access GetTagName()
};

// Shared class for the various style updating commands like bold, italics etc.
// Suitable for commands whose state is either 'on' or 'off'.
class StyleUpdatingCommand final : public StateUpdatingCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(StyleUpdatingCommand)

 protected:
  StyleUpdatingCommand() = default;
  virtual ~StyleUpdatingCommand() = default;

  // get the current state (on or off) for this style or block format
  MOZ_CAN_RUN_SCRIPT nsresult
  GetCurrentState(nsAtom* aTagName, HTMLEditor* aHTMLEditor,
                  nsCommandParams& aParams) const final;

  // add/remove the style
  MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName,
                                          HTMLEditor& aHTMLEditor,
                                          nsIPrincipal* aPrincipal) const final;
};

class InsertTagCommand final : public EditorCommand {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(InsertTagCommand, EditorCommand)

  NS_DECL_EDITOR_COMMAND_COMMON_METHODS
  NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND
  NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(InsertTagCommand)

 protected:
  InsertTagCommand() = default;
  virtual ~InsertTagCommand() = default;

  static nsAtom* GetTagName(Command aCommand) {
    switch (aCommand) {
      case Command::InsertLink:
        return nsGkAtoms::a;
      case Command::InsertImage:
        return nsGkAtoms::img;
      case Command::InsertHorizontalRule:
        return nsGkAtoms::hr;
      default:
        return StateUpdatingCommandBase::GetTagName(aCommand);
    }
  }
};

class ListCommand final : public StateUpdatingCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ListCommand)

 protected:
  ListCommand() = default;
  virtual ~ListCommand() = default;

  // get the current state (on or off) for this style or block format
  MOZ_CAN_RUN_SCRIPT nsresult
  GetCurrentState(nsAtom* aTagName, HTMLEditor* aHTMLEditor,
                  nsCommandParams& aParams) const final;

  // add/remove the style
  MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName,
                                          HTMLEditor& aHTMLEditor,
                                          nsIPrincipal* aPrincipal) const final;
};

class ListItemCommand final : public StateUpdatingCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ListItemCommand)

 protected:
  ListItemCommand() = default;
  virtual ~ListItemCommand() = default;

  // get the current state (on or off) for this style or block format
  MOZ_CAN_RUN_SCRIPT nsresult
  GetCurrentState(nsAtom* aTagName, HTMLEditor* aHTMLEditor,
                  nsCommandParams& aParams) const final;

  // add/remove the style
  MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName,
                                          HTMLEditor& aHTMLEditor,
                                          nsIPrincipal* aPrincipal) const final;
};

// Base class for commands whose state consists of a string (e.g. para format)
class MultiStateCommandBase : public EditorCommand {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(MultiStateCommandBase, EditorCommand)

  NS_DECL_EDITOR_COMMAND_COMMON_METHODS
  NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM

 protected:
  MultiStateCommandBase() = default;
  virtual ~MultiStateCommandBase() = default;

  MOZ_CAN_RUN_SCRIPT virtual nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const = 0;
  MOZ_CAN_RUN_SCRIPT virtual nsresult SetState(
      HTMLEditor* aHTMLEditor, const nsAString& aNewState,
      nsIPrincipal* aPrincipal) const = 0;
};

class ParagraphStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ParagraphStateCommand)

 protected:
  ParagraphStateCommand() = default;
  virtual ~ParagraphStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class FontFaceStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontFaceStateCommand)

 protected:
  FontFaceStateCommand() = default;
  virtual ~FontFaceStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class FontSizeStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontSizeStateCommand)

 protected:
  FontSizeStateCommand() = default;
  virtual ~FontSizeStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class HighlightColorStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(HighlightColorStateCommand)

 protected:
  HighlightColorStateCommand() = default;
  virtual ~HighlightColorStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class FontColorStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontColorStateCommand)

 protected:
  FontColorStateCommand() = default;
  virtual ~FontColorStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class AlignCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(AlignCommand)

 protected:
  AlignCommand() = default;
  virtual ~AlignCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class BackgroundColorStateCommand final : public MultiStateCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(BackgroundColorStateCommand)

 protected:
  BackgroundColorStateCommand() = default;
  virtual ~BackgroundColorStateCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState(
      HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor,
                                       const nsAString& aNewState,
                                       nsIPrincipal* aPrincipal) const final;
};

class AbsolutePositioningCommand final : public StateUpdatingCommandBase {
 public:
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(AbsolutePositioningCommand)

 protected:
  AbsolutePositioningCommand() = default;
  virtual ~AbsolutePositioningCommand() = default;

  MOZ_CAN_RUN_SCRIPT nsresult
  GetCurrentState(nsAtom* aTagName, HTMLEditor* aHTMLEditor,
                  nsCommandParams& aParams) const final;
  MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName,
                                          HTMLEditor& aHTMLEditor,
                                          nsIPrincipal* aPrincipal) const final;
};

// composer commands

NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DocumentStateCommand)

class SetDocumentStateCommand final : public EditorCommand {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(SetDocumentStateCommand, EditorCommand)

  NS_DECL_EDITOR_COMMAND_COMMON_METHODS
  NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM
  NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM
  NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(SetDocumentStateCommand)

 private:
  SetDocumentStateCommand() = default;
  virtual ~SetDocumentStateCommand() = default;
};

NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseZIndexCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IncreaseZIndexCommand)

// Generic commands

// Edit menu
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteNoFormattingCommand)

// Block transformations
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IndentCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(OutdentCommand)

NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveListCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveStylesCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IncreaseFontSizeCommand)
NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseFontSizeCommand)

// Insert content commands
NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertHTMLCommand)

#undef NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE
#undef NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM
#undef NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM
#undef NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM
#undef NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM
#undef NS_DECL_EDITOR_COMMAND_COMMON_METHODS
#undef NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND
#undef NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM
#undef NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM
#undef NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM
#undef NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM
#undef NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON

}  // namespace mozilla

#endif  // #ifndef mozilla_EditorCommands_h