EditorCommands.h (36426B)
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 #ifndef mozilla_EditorCommands_h 7 #define mozilla_EditorCommands_h 8 9 #include "mozilla/EditorForwards.h" 10 #include "mozilla/EventForwards.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/StaticPtr.h" 13 #include "mozilla/TypedEnumBits.h" 14 #include "nsGkAtoms.h" 15 #include "mozilla/ControllerCommand.h" 16 #include "nsIPrincipal.h" 17 #include "nsISupportsImpl.h" 18 #include "nsRefPtrHashtable.h" 19 #include "nsStringFwd.h" 20 21 class nsAtom; 22 class nsCommandParams; 23 class nsICommandParams; 24 class nsIEditingSession; 25 class nsITransferable; 26 class nsStaticAtom; 27 28 namespace mozilla { 29 30 /** 31 * EditorCommandParamType tells you that EditorCommand subclasses refer 32 * which type in nsCommandParams (e.g., bool or nsString) or do not refer. 33 * If they refer some types, also set where is in nsCommandParams, e.g., 34 * whether "state_attribute" or "state_data". 35 */ 36 enum class EditorCommandParamType : uint16_t { 37 // The command does not take params (even if specified, always ignored). 38 None = 0, 39 // The command refers nsCommandParams::GetBool() result. 40 Bool = 1 << 0, 41 // The command refers nsCommandParams::GetString() result. 42 // This may be specified with CString. In such case, 43 // nsCommandParams::GetCString() is preferred. 44 String = 1 << 1, 45 // The command refers nsCommandParams::GetCString() result. 46 CString = 1 << 2, 47 // The command refers nsCommandParams::GetISupports("transferable") result. 48 Transferable = 1 << 3, 49 50 // The command refres "state_attribute" of nsCommandParams when calling 51 // GetBool()/GetString()/GetCString(). This must not be set when the 52 // type is None or Transferable. 53 StateAttribute = 1 << 14, 54 // The command refers "state_data" of nsCommandParams when calling 55 // GetBool()/GetString()/GetCString(). This must not be set when the 56 // type is None or Transferable. 57 StateData = 1 << 15, 58 }; 59 60 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(EditorCommandParamType) 61 62 /** 63 * This is a base class for commands registered with the editor controller. 64 * Note that such commands are designed as singleton classes. So, MUST be 65 * stateless. Any state must be stored via the refCon (an nsIEditor). 66 */ 67 68 class EditorCommand : public ControllerCommand { 69 public: 70 static EditorCommandParamType GetParamType(Command aCommand) { 71 // Keep same order of registration in EditorController.cpp and 72 // HTMLEditorController.cpp. 73 switch (aCommand) { 74 // UndoCommand 75 case Command::HistoryUndo: 76 return EditorCommandParamType::None; 77 // RedoCommand 78 case Command::HistoryRedo: 79 return EditorCommandParamType::None; 80 // CutCommand 81 case Command::Cut: 82 return EditorCommandParamType::None; 83 // CutOrDeleteCommand 84 case Command::CutOrDelete: 85 return EditorCommandParamType::None; 86 // CopyCommand 87 case Command::Copy: 88 return EditorCommandParamType::None; 89 // CopyOrDeleteCommand 90 case Command::CopyOrDelete: 91 return EditorCommandParamType::None; 92 // SelectAllCommand 93 case Command::SelectAll: 94 return EditorCommandParamType::None; 95 // PasteCommand 96 case Command::Paste: 97 return EditorCommandParamType::None; 98 case Command::PasteTransferable: 99 return EditorCommandParamType::Transferable; 100 // SwitchTextDirectionCommand 101 case Command::FormatSetBlockTextDirection: 102 return EditorCommandParamType::None; 103 // DeleteCommand 104 case Command::Delete: 105 case Command::DeleteCharBackward: 106 case Command::DeleteCharForward: 107 case Command::DeleteWordBackward: 108 case Command::DeleteWordForward: 109 case Command::DeleteToBeginningOfLine: 110 case Command::DeleteToEndOfLine: 111 return EditorCommandParamType::None; 112 // InsertPlaintextCommand 113 case Command::InsertText: 114 return EditorCommandParamType::String | 115 EditorCommandParamType::StateData; 116 // InsertParagraphCommand 117 case Command::InsertParagraph: 118 return EditorCommandParamType::None; 119 // InsertLineBreakCommand 120 case Command::InsertLineBreak: 121 return EditorCommandParamType::None; 122 // PasteQuotationCommand 123 case Command::PasteAsQuotation: 124 return EditorCommandParamType::None; 125 126 // SelectionMoveCommand 127 case Command::ScrollTop: 128 case Command::ScrollBottom: 129 case Command::MoveTop: 130 case Command::MoveBottom: 131 case Command::SelectTop: 132 case Command::SelectBottom: 133 case Command::LineNext: 134 case Command::LinePrevious: 135 case Command::SelectLineNext: 136 case Command::SelectLinePrevious: 137 case Command::CharPrevious: 138 case Command::CharNext: 139 case Command::SelectCharPrevious: 140 case Command::SelectCharNext: 141 case Command::BeginLine: 142 case Command::EndLine: 143 case Command::SelectBeginLine: 144 case Command::SelectEndLine: 145 case Command::WordPrevious: 146 case Command::WordNext: 147 case Command::SelectWordPrevious: 148 case Command::SelectWordNext: 149 case Command::ScrollPageUp: 150 case Command::ScrollPageDown: 151 case Command::ScrollLineUp: 152 case Command::ScrollLineDown: 153 case Command::MovePageUp: 154 case Command::MovePageDown: 155 case Command::SelectPageUp: 156 case Command::SelectPageDown: 157 case Command::MoveLeft: 158 case Command::MoveRight: 159 case Command::MoveUp: 160 case Command::MoveDown: 161 case Command::MoveLeft2: 162 case Command::MoveRight2: 163 case Command::MoveUp2: 164 case Command::MoveDown2: 165 case Command::SelectLeft: 166 case Command::SelectRight: 167 case Command::SelectUp: 168 case Command::SelectDown: 169 case Command::SelectLeft2: 170 case Command::SelectRight2: 171 case Command::SelectUp2: 172 case Command::SelectDown2: 173 return EditorCommandParamType::None; 174 // PasteNoFormattingCommand 175 case Command::PasteWithoutFormat: 176 return EditorCommandParamType::None; 177 178 // DocumentStateCommand 179 case Command::EditorObserverDocumentCreated: 180 case Command::EditorObserverDocumentLocationChanged: 181 case Command::EditorObserverDocumentWillBeDestroyed: 182 return EditorCommandParamType::None; 183 // SetDocumentStateCommand 184 case Command::SetDocumentModified: 185 case Command::SetDocumentUseCSS: 186 case Command::SetDocumentReadOnly: 187 case Command::SetDocumentInsertBROnEnterKeyPress: 188 return EditorCommandParamType::Bool | 189 EditorCommandParamType::StateAttribute; 190 case Command::SetDocumentDefaultParagraphSeparator: 191 return EditorCommandParamType::CString | 192 EditorCommandParamType::StateAttribute; 193 case Command::ToggleObjectResizers: 194 case Command::ToggleInlineTableEditor: 195 case Command::ToggleAbsolutePositionEditor: 196 case Command::EnableCompatibleJoinSplitNodeDirection: 197 return EditorCommandParamType::Bool | 198 EditorCommandParamType::StateAttribute; 199 200 // IndentCommand 201 case Command::FormatIndent: 202 return EditorCommandParamType::None; 203 // OutdentCommand 204 case Command::FormatOutdent: 205 return EditorCommandParamType::None; 206 // StyleUpdatingCommand 207 case Command::FormatBold: 208 case Command::FormatItalic: 209 case Command::FormatUnderline: 210 case Command::FormatTeletypeText: 211 case Command::FormatStrikeThrough: 212 case Command::FormatSuperscript: 213 case Command::FormatSubscript: 214 case Command::FormatNoBreak: 215 case Command::FormatEmphasis: 216 case Command::FormatStrong: 217 case Command::FormatCitation: 218 case Command::FormatAbbreviation: 219 case Command::FormatAcronym: 220 case Command::FormatCode: 221 case Command::FormatSample: 222 case Command::FormatVariable: 223 case Command::FormatRemoveLink: 224 return EditorCommandParamType::None; 225 // ListCommand 226 case Command::InsertOrderedList: 227 case Command::InsertUnorderedList: 228 return EditorCommandParamType::None; 229 // ListItemCommand 230 case Command::InsertDefinitionTerm: 231 case Command::InsertDefinitionDetails: 232 return EditorCommandParamType::None; 233 // RemoveListCommand 234 case Command::FormatRemoveList: 235 return EditorCommandParamType::None; 236 // ParagraphStateCommand 237 case Command::FormatBlock: 238 case Command::ParagraphState: 239 return EditorCommandParamType::CString | 240 EditorCommandParamType::String | 241 EditorCommandParamType::StateAttribute; 242 // FontFaceStateCommand 243 case Command::FormatFontName: 244 return EditorCommandParamType::CString | 245 EditorCommandParamType::String | 246 EditorCommandParamType::StateAttribute; 247 // FontSizeStateCommand 248 case Command::FormatFontSize: 249 return EditorCommandParamType::CString | 250 EditorCommandParamType::String | 251 EditorCommandParamType::StateAttribute; 252 // FontColorStateCommand 253 case Command::FormatFontColor: 254 return EditorCommandParamType::CString | 255 EditorCommandParamType::String | 256 EditorCommandParamType::StateAttribute; 257 // BackgroundColorStateCommand 258 case Command::FormatDocumentBackgroundColor: 259 return EditorCommandParamType::CString | 260 EditorCommandParamType::String | 261 EditorCommandParamType::StateAttribute; 262 // HighlightColorStateCommand 263 case Command::FormatBackColor: 264 return EditorCommandParamType::CString | 265 EditorCommandParamType::String | 266 EditorCommandParamType::StateAttribute; 267 // AlignCommand: 268 case Command::FormatJustifyLeft: 269 case Command::FormatJustifyRight: 270 case Command::FormatJustifyCenter: 271 case Command::FormatJustifyFull: 272 case Command::FormatJustifyNone: 273 return EditorCommandParamType::CString | 274 EditorCommandParamType::String | 275 EditorCommandParamType::StateAttribute; 276 // RemoveStylesCommand 277 case Command::FormatRemove: 278 return EditorCommandParamType::None; 279 // IncreaseFontSizeCommand 280 case Command::FormatIncreaseFontSize: 281 return EditorCommandParamType::None; 282 // DecreaseFontSizeCommand 283 case Command::FormatDecreaseFontSize: 284 return EditorCommandParamType::None; 285 // InsertHTMLCommand 286 case Command::InsertHTML: 287 return EditorCommandParamType::String | 288 EditorCommandParamType::StateData; 289 // InsertTagCommand 290 case Command::InsertLink: 291 case Command::InsertImage: 292 return EditorCommandParamType::String | 293 EditorCommandParamType::StateAttribute; 294 case Command::InsertHorizontalRule: 295 return EditorCommandParamType::None; 296 // AbsolutePositioningCommand 297 case Command::FormatAbsolutePosition: 298 return EditorCommandParamType::None; 299 // DecreaseZIndexCommand 300 case Command::FormatDecreaseZIndex: 301 return EditorCommandParamType::None; 302 // IncreaseZIndexCommand 303 case Command::FormatIncreaseZIndex: 304 return EditorCommandParamType::None; 305 306 default: 307 MOZ_ASSERT_UNREACHABLE("Unknown Command"); 308 return EditorCommandParamType::None; 309 } 310 } 311 312 // ControllerCommand methods. Use EditorCommand specific methods instead 313 // for internal use. 314 MOZ_CAN_RUN_SCRIPT_BOUNDARY bool IsCommandEnabled( 315 const nsACString& aCommandName, nsISupports* aCommandRefCon) final; 316 MOZ_CAN_RUN_SCRIPT nsresult DoCommand(const nsACString& aCommandName, 317 nsICommandParams* aParams, 318 nsISupports* aCommandRefCon) final; 319 MOZ_CAN_RUN_SCRIPT_BOUNDARY void GetCommandStateParams( 320 const nsACString& aCommandName, nsICommandParams* aParams, 321 nsISupports* aCommandRefCon) final; 322 323 MOZ_CAN_RUN_SCRIPT virtual bool IsCommandEnabled( 324 Command aCommand, EditorBase* aEditorBase) const = 0; 325 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommand( 326 Command aCommand, EditorBase& aEditorBase, 327 nsIPrincipal* aPrincipal) const = 0; 328 329 /** 330 * @param aEditorBase If the context is an editor, should be set to 331 * it. Otherwise, nullptr. 332 * @param aEditingSession If the context is an editing session, should be 333 * set to it. This usually occurs if editor has 334 * not been created yet during initialization. 335 * Otherwise, nullptr. 336 */ 337 MOZ_CAN_RUN_SCRIPT virtual nsresult GetCommandStateParams( 338 Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase, 339 nsIEditingSession* aEditingSession) const = 0; 340 341 /** 342 * Called only when the result of EditorCommand::GetParamType(aCommand) is 343 * EditorCommandParamType::None. 344 */ 345 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam( 346 Command aCommand, EditorBase& aEditorBase, 347 nsIPrincipal* aPrincipal) const { 348 MOZ_ASSERT_UNREACHABLE("Wrong overload is called"); 349 return NS_ERROR_NOT_IMPLEMENTED; 350 } 351 352 /** 353 * Called only when the result of EditorCommand::GetParamType(aCommand) 354 * includes EditorCommandParamType::Bool. If aBoolParam is Nothing, it 355 * means that given param was nullptr. 356 */ 357 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam( 358 Command aCommand, const Maybe<bool>& aBoolParam, EditorBase& aEditorBase, 359 nsIPrincipal* aPrincipal) const { 360 MOZ_ASSERT_UNREACHABLE("Wrong overload is called"); 361 return NS_ERROR_NOT_IMPLEMENTED; 362 } 363 364 /** 365 * Called only when the result of EditorCommand::GetParamType(aCommand) 366 * includes EditorCommandParamType::CString. If aCStringParam is void, it 367 * means that given param was nullptr. 368 */ 369 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam( 370 Command aCommand, const nsACString& aCStringParam, 371 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const { 372 MOZ_ASSERT_UNREACHABLE("Wrong overload is called"); 373 return NS_ERROR_NOT_IMPLEMENTED; 374 } 375 376 /** 377 * Called only when the result of EditorCommand::GetParamType(aCommand) 378 * includes EditorCommandParamType::String. If aStringParam is void, it 379 * means that given param was nullptr. 380 */ 381 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam( 382 Command aCommand, const nsAString& aStringParam, EditorBase& aEditorBase, 383 nsIPrincipal* aPrincipal) const { 384 MOZ_ASSERT_UNREACHABLE("Wrong overload is called"); 385 return NS_ERROR_NOT_IMPLEMENTED; 386 } 387 388 /** 389 * Called only when the result of EditorCommand::GetParamType(aCommand) is 390 * EditorCommandParamType::Transferable. If aTransferableParam may be 391 * nullptr. 392 */ 393 MOZ_CAN_RUN_SCRIPT virtual nsresult DoCommandParam( 394 Command aCommand, nsITransferable* aTransferableParam, 395 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const { 396 MOZ_ASSERT_UNREACHABLE("Wrong overload is called"); 397 return NS_ERROR_NOT_IMPLEMENTED; 398 } 399 400 protected: 401 EditorCommand() = default; 402 virtual ~EditorCommand() = default; 403 }; 404 405 #define NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 406 public: \ 407 MOZ_CAN_RUN_SCRIPT bool IsCommandEnabled( \ 408 Command aCommand, EditorBase* aEditorBase) const final; \ 409 using EditorCommand::IsCommandEnabled; \ 410 MOZ_CAN_RUN_SCRIPT nsresult DoCommand( \ 411 Command aCommand, EditorBase& aEditorBase, nsIPrincipal* aPrincipal) \ 412 const final; \ 413 using EditorCommand::DoCommand; \ 414 MOZ_CAN_RUN_SCRIPT nsresult GetCommandStateParams( \ 415 Command aCommand, nsCommandParams& aParams, EditorBase* aEditorBase, \ 416 nsIEditingSession* aEditingSession) const final; \ 417 using EditorCommand::GetCommandStateParams; \ 418 using EditorCommand::DoCommandParam; 419 420 #define NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND \ 421 public: \ 422 MOZ_CAN_RUN_SCRIPT nsresult DoCommandParam( \ 423 Command aCommand, EditorBase& aEditorBase, nsIPrincipal* aPrincipal) \ 424 const final { \ 425 return DoCommand(aCommand, aEditorBase, aPrincipal); \ 426 } 427 428 #define NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM \ 429 public: \ 430 MOZ_CAN_RUN_SCRIPT nsresult DoCommandParam( \ 431 Command aCommand, const Maybe<bool>& aBoolParam, \ 432 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const final; 433 434 #define NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM \ 435 public: \ 436 MOZ_CAN_RUN_SCRIPT nsresult DoCommandParam( \ 437 Command aCommand, const nsACString& aCStringParam, \ 438 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const final; 439 440 #define NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM \ 441 public: \ 442 MOZ_CAN_RUN_SCRIPT nsresult DoCommandParam( \ 443 Command aCommand, const nsAString& aStringParam, \ 444 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const final; 445 446 #define NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM \ 447 public: \ 448 MOZ_CAN_RUN_SCRIPT nsresult DoCommandParam( \ 449 Command aCommand, nsITransferable* aTransferableParam, \ 450 EditorBase& aEditorBase, nsIPrincipal* aPrincipal) const final; 451 452 #define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 453 public: \ 454 static EditorCommand* GetInstance() { \ 455 if (!sInstance) { \ 456 sInstance = new _cmd(); \ 457 } \ 458 return sInstance; \ 459 } \ 460 \ 461 static void Shutdown() { sInstance = nullptr; } \ 462 \ 463 private: \ 464 static StaticRefPtr<_cmd> sInstance; 465 466 #define NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(_cmd) \ 467 class _cmd final : public EditorCommand { \ 468 NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 469 NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND \ 470 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 471 \ 472 protected: \ 473 _cmd() = default; \ 474 virtual ~_cmd() = default; \ 475 }; 476 477 #define NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM(_cmd) \ 478 class _cmd final : public EditorCommand { \ 479 NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 480 NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM \ 481 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 482 \ 483 protected: \ 484 _cmd() = default; \ 485 virtual ~_cmd() = default; \ 486 }; 487 488 #define NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM(_cmd) \ 489 class _cmd final : public EditorCommand { \ 490 NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 491 NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM \ 492 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 493 \ 494 protected: \ 495 _cmd() = default; \ 496 virtual ~_cmd() = default; \ 497 }; 498 499 #define NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(_cmd) \ 500 class _cmd final : public EditorCommand { \ 501 NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 502 NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM \ 503 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 504 \ 505 protected: \ 506 _cmd() = default; \ 507 virtual ~_cmd() = default; \ 508 }; 509 510 #define NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(_cmd) \ 511 class _cmd final : public EditorCommand { \ 512 NS_DECL_EDITOR_COMMAND_COMMON_METHODS \ 513 NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM \ 514 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ 515 \ 516 protected: \ 517 _cmd() = default; \ 518 virtual ~_cmd() = default; \ 519 }; 520 521 // basic editor commands 522 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(UndoCommand) 523 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RedoCommand) 524 525 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutCommand) 526 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutOrDeleteCommand) 527 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyCommand) 528 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyOrDeleteCommand) 529 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteCommand) 530 NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(PasteTransferableCommand) 531 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SwitchTextDirectionCommand) 532 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DeleteCommand) 533 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectAllCommand) 534 535 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectionMoveCommands) 536 537 // Insert content commands 538 NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertPlaintextCommand) 539 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(InsertParagraphCommand) 540 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(InsertLineBreakCommand) 541 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteQuotationCommand) 542 543 /****************************************************************************** 544 * Commands for HTML editor 545 ******************************************************************************/ 546 547 // virtual base class for commands that need to save and update Boolean state 548 // (like styles etc) 549 class StateUpdatingCommandBase : public EditorCommand { 550 public: 551 NS_DECL_EDITOR_COMMAND_COMMON_METHODS 552 NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND 553 554 protected: 555 StateUpdatingCommandBase() = default; 556 virtual ~StateUpdatingCommandBase() = default; 557 558 // get the current state (on or off) for this style or block format 559 MOZ_CAN_RUN_SCRIPT virtual nsresult GetCurrentState( 560 nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 561 nsCommandParams& aParams) const = 0; 562 563 // add/remove the style 564 MOZ_CAN_RUN_SCRIPT virtual nsresult ToggleState( 565 nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 566 nsIPrincipal* aPrincipal) const = 0; 567 568 static nsStaticAtom* GetTagName(Command aCommand) { 569 switch (aCommand) { 570 case Command::FormatBold: 571 return nsGkAtoms::b; 572 case Command::FormatItalic: 573 return nsGkAtoms::i; 574 case Command::FormatUnderline: 575 return nsGkAtoms::u; 576 case Command::FormatTeletypeText: 577 return nsGkAtoms::tt; 578 case Command::FormatStrikeThrough: 579 return nsGkAtoms::strike; 580 case Command::FormatSuperscript: 581 return nsGkAtoms::sup; 582 case Command::FormatSubscript: 583 return nsGkAtoms::sub; 584 case Command::FormatNoBreak: 585 return nsGkAtoms::nobr; 586 case Command::FormatEmphasis: 587 return nsGkAtoms::em; 588 case Command::FormatStrong: 589 return nsGkAtoms::strong; 590 case Command::FormatCitation: 591 return nsGkAtoms::cite; 592 case Command::FormatAbbreviation: 593 return nsGkAtoms::abbr; 594 case Command::FormatAcronym: 595 return nsGkAtoms::acronym; 596 case Command::FormatCode: 597 return nsGkAtoms::code; 598 case Command::FormatSample: 599 return nsGkAtoms::samp; 600 case Command::FormatVariable: 601 return nsGkAtoms::var; 602 case Command::FormatRemoveLink: 603 return nsGkAtoms::href; 604 case Command::InsertOrderedList: 605 return nsGkAtoms::ol; 606 case Command::InsertUnorderedList: 607 return nsGkAtoms::ul; 608 case Command::InsertDefinitionTerm: 609 return nsGkAtoms::dt; 610 case Command::InsertDefinitionDetails: 611 return nsGkAtoms::dd; 612 case Command::FormatAbsolutePosition: 613 return nsGkAtoms::_empty; 614 default: 615 return nullptr; 616 } 617 } 618 friend class InsertTagCommand; // for allowing it to access GetTagName() 619 }; 620 621 // Shared class for the various style updating commands like bold, italics etc. 622 // Suitable for commands whose state is either 'on' or 'off'. 623 class StyleUpdatingCommand final : public StateUpdatingCommandBase { 624 public: 625 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(StyleUpdatingCommand) 626 627 protected: 628 StyleUpdatingCommand() = default; 629 virtual ~StyleUpdatingCommand() = default; 630 631 // get the current state (on or off) for this style or block format 632 MOZ_CAN_RUN_SCRIPT nsresult 633 GetCurrentState(nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 634 nsCommandParams& aParams) const final; 635 636 // add/remove the style 637 MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName, 638 HTMLEditor& aHTMLEditor, 639 nsIPrincipal* aPrincipal) const final; 640 }; 641 642 class InsertTagCommand final : public EditorCommand { 643 public: 644 NS_DECL_EDITOR_COMMAND_COMMON_METHODS 645 NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND 646 NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM 647 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(InsertTagCommand) 648 649 protected: 650 InsertTagCommand() = default; 651 virtual ~InsertTagCommand() = default; 652 653 static nsAtom* GetTagName(Command aCommand) { 654 switch (aCommand) { 655 case Command::InsertLink: 656 return nsGkAtoms::a; 657 case Command::InsertImage: 658 return nsGkAtoms::img; 659 case Command::InsertHorizontalRule: 660 return nsGkAtoms::hr; 661 default: 662 return StateUpdatingCommandBase::GetTagName(aCommand); 663 } 664 } 665 }; 666 667 class ListCommand final : public StateUpdatingCommandBase { 668 public: 669 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ListCommand) 670 671 protected: 672 ListCommand() = default; 673 virtual ~ListCommand() = default; 674 675 // get the current state (on or off) for this style or block format 676 MOZ_CAN_RUN_SCRIPT nsresult 677 GetCurrentState(nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 678 nsCommandParams& aParams) const final; 679 680 // add/remove the style 681 MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName, 682 HTMLEditor& aHTMLEditor, 683 nsIPrincipal* aPrincipal) const final; 684 }; 685 686 class ListItemCommand final : public StateUpdatingCommandBase { 687 public: 688 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ListItemCommand) 689 690 protected: 691 ListItemCommand() = default; 692 virtual ~ListItemCommand() = default; 693 694 // get the current state (on or off) for this style or block format 695 MOZ_CAN_RUN_SCRIPT nsresult 696 GetCurrentState(nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 697 nsCommandParams& aParams) const final; 698 699 // add/remove the style 700 MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName, 701 HTMLEditor& aHTMLEditor, 702 nsIPrincipal* aPrincipal) const final; 703 }; 704 705 // Base class for commands whose state consists of a string (e.g. para format) 706 class MultiStateCommandBase : public EditorCommand { 707 public: 708 NS_DECL_EDITOR_COMMAND_COMMON_METHODS 709 NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM 710 711 protected: 712 MultiStateCommandBase() = default; 713 virtual ~MultiStateCommandBase() = default; 714 715 MOZ_CAN_RUN_SCRIPT virtual nsresult GetCurrentState( 716 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const = 0; 717 MOZ_CAN_RUN_SCRIPT virtual nsresult SetState( 718 HTMLEditor* aHTMLEditor, const nsAString& aNewState, 719 nsIPrincipal* aPrincipal) const = 0; 720 }; 721 722 /** 723 * The command class for Document.execCommand("formatBlock"), 724 * Document.queryCommandValue("formatBlock") etc. 725 */ 726 class FormatBlockStateCommand final : public MultiStateCommandBase { 727 public: 728 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FormatBlockStateCommand) 729 730 protected: 731 FormatBlockStateCommand() = default; 732 virtual ~FormatBlockStateCommand() = default; 733 734 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 735 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 736 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 737 const nsAString& aNewState, 738 nsIPrincipal* aPrincipal) const final; 739 }; 740 741 /** 742 * The command class for the legacy XUL edit command, cmd_paragraphState. 743 * This command treats only <p>, <pre>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, 744 * <address> as a format node. 745 */ 746 class ParagraphStateCommand final : public MultiStateCommandBase { 747 public: 748 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(ParagraphStateCommand) 749 750 protected: 751 ParagraphStateCommand() = default; 752 virtual ~ParagraphStateCommand() = default; 753 754 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 755 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 756 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 757 const nsAString& aNewState, 758 nsIPrincipal* aPrincipal) const final; 759 }; 760 761 class FontFaceStateCommand final : public MultiStateCommandBase { 762 public: 763 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontFaceStateCommand) 764 765 protected: 766 FontFaceStateCommand() = default; 767 virtual ~FontFaceStateCommand() = default; 768 769 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 770 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 771 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 772 const nsAString& aNewState, 773 nsIPrincipal* aPrincipal) const final; 774 }; 775 776 class FontSizeStateCommand final : public MultiStateCommandBase { 777 public: 778 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontSizeStateCommand) 779 780 protected: 781 FontSizeStateCommand() = default; 782 virtual ~FontSizeStateCommand() = default; 783 784 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 785 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 786 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 787 const nsAString& aNewState, 788 nsIPrincipal* aPrincipal) const final; 789 }; 790 791 class HighlightColorStateCommand final : public MultiStateCommandBase { 792 public: 793 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(HighlightColorStateCommand) 794 795 protected: 796 HighlightColorStateCommand() = default; 797 virtual ~HighlightColorStateCommand() = default; 798 799 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 800 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 801 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 802 const nsAString& aNewState, 803 nsIPrincipal* aPrincipal) const final; 804 }; 805 806 class FontColorStateCommand final : public MultiStateCommandBase { 807 public: 808 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(FontColorStateCommand) 809 810 protected: 811 FontColorStateCommand() = default; 812 virtual ~FontColorStateCommand() = default; 813 814 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 815 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 816 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 817 const nsAString& aNewState, 818 nsIPrincipal* aPrincipal) const final; 819 }; 820 821 class AlignCommand final : public MultiStateCommandBase { 822 public: 823 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(AlignCommand) 824 825 protected: 826 AlignCommand() = default; 827 virtual ~AlignCommand() = default; 828 829 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 830 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 831 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 832 const nsAString& aNewState, 833 nsIPrincipal* aPrincipal) const final; 834 }; 835 836 class BackgroundColorStateCommand final : public MultiStateCommandBase { 837 public: 838 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(BackgroundColorStateCommand) 839 840 protected: 841 BackgroundColorStateCommand() = default; 842 virtual ~BackgroundColorStateCommand() = default; 843 844 MOZ_CAN_RUN_SCRIPT nsresult GetCurrentState( 845 HTMLEditor* aHTMLEditor, nsCommandParams& aParams) const final; 846 MOZ_CAN_RUN_SCRIPT nsresult SetState(HTMLEditor* aHTMLEditor, 847 const nsAString& aNewState, 848 nsIPrincipal* aPrincipal) const final; 849 }; 850 851 class AbsolutePositioningCommand final : public StateUpdatingCommandBase { 852 public: 853 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(AbsolutePositioningCommand) 854 855 protected: 856 AbsolutePositioningCommand() = default; 857 virtual ~AbsolutePositioningCommand() = default; 858 859 MOZ_CAN_RUN_SCRIPT nsresult 860 GetCurrentState(nsStaticAtom& aTagName, HTMLEditor& aHTMLEditor, 861 nsCommandParams& aParams) const final; 862 MOZ_CAN_RUN_SCRIPT nsresult ToggleState(nsStaticAtom& aTagName, 863 HTMLEditor& aHTMLEditor, 864 nsIPrincipal* aPrincipal) const final; 865 }; 866 867 // composer commands 868 869 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DocumentStateCommand) 870 871 class SetDocumentStateCommand final : public EditorCommand { 872 public: 873 NS_DECL_EDITOR_COMMAND_COMMON_METHODS 874 NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM 875 NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM 876 NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(SetDocumentStateCommand) 877 878 private: 879 SetDocumentStateCommand() = default; 880 virtual ~SetDocumentStateCommand() = default; 881 }; 882 883 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseZIndexCommand) 884 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IncreaseZIndexCommand) 885 886 // Generic commands 887 888 // Edit menu 889 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteNoFormattingCommand) 890 891 // Block transformations 892 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IndentCommand) 893 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(OutdentCommand) 894 895 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveListCommand) 896 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveStylesCommand) 897 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IncreaseFontSizeCommand) 898 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseFontSizeCommand) 899 900 // Insert content commands 901 NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertHTMLCommand) 902 903 #undef NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE 904 #undef NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM 905 #undef NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM 906 #undef NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM 907 #undef NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM 908 #undef NS_DECL_EDITOR_COMMAND_COMMON_METHODS 909 #undef NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND 910 #undef NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM 911 #undef NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM 912 #undef NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM 913 #undef NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM 914 #undef NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON 915 916 } // namespace mozilla 917 918 #endif // #ifndef mozilla_EditorCommands_h