DeleteTextTransaction.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "DeleteTextTransaction.h"
  6. #include "mozilla/Assertions.h"
  7. #include "mozilla/EditorBase.h"
  8. #include "mozilla/SelectionState.h"
  9. #include "mozilla/dom/Selection.h"
  10. #include "nsDebug.h"
  11. #include "nsError.h"
  12. #include "nsIEditor.h"
  13. #include "nsISupportsImpl.h"
  14. #include "nsAString.h"
  15. namespace mozilla {
  16. using namespace dom;
  17. DeleteTextTransaction::DeleteTextTransaction(
  18. EditorBase& aEditorBase,
  19. nsGenericDOMDataNode& aCharData,
  20. uint32_t aOffset,
  21. uint32_t aNumCharsToDelete,
  22. RangeUpdater* aRangeUpdater)
  23. : mEditorBase(&aEditorBase)
  24. , mCharData(&aCharData)
  25. , mOffset(aOffset)
  26. , mNumCharsToDelete(aNumCharsToDelete)
  27. , mRangeUpdater(aRangeUpdater)
  28. {
  29. NS_ASSERTION(mCharData->Length() >= aOffset + aNumCharsToDelete,
  30. "Trying to delete more characters than in node");
  31. }
  32. NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTransaction, EditTransactionBase,
  33. mEditorBase,
  34. mCharData)
  35. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTransaction)
  36. NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
  37. nsresult
  38. DeleteTextTransaction::Init()
  39. {
  40. // Do nothing if the node is read-only
  41. if (NS_WARN_IF(!mEditorBase) || !mEditorBase->IsModifiableNode(mCharData)) {
  42. return NS_ERROR_FAILURE;
  43. }
  44. return NS_OK;
  45. }
  46. NS_IMETHODIMP
  47. DeleteTextTransaction::DoTransaction()
  48. {
  49. if (NS_WARN_IF(!mCharData) || NS_WARN_IF(!mEditorBase)) {
  50. return NS_ERROR_NOT_INITIALIZED;
  51. }
  52. // Get the text that we're about to delete
  53. nsresult rv = mCharData->SubstringData(mOffset, mNumCharsToDelete,
  54. mDeletedText);
  55. MOZ_ASSERT(NS_SUCCEEDED(rv));
  56. rv = mCharData->DeleteData(mOffset, mNumCharsToDelete);
  57. NS_ENSURE_SUCCESS(rv, rv);
  58. if (mRangeUpdater) {
  59. mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
  60. }
  61. // Only set selection to deletion point if editor gives permission
  62. if (mEditorBase->GetShouldTxnSetSelection()) {
  63. RefPtr<Selection> selection = mEditorBase->GetSelection();
  64. NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
  65. rv = selection->Collapse(mCharData, mOffset);
  66. NS_ASSERTION(NS_SUCCEEDED(rv),
  67. "Selection could not be collapsed after undo of deletetext");
  68. NS_ENSURE_SUCCESS(rv, rv);
  69. }
  70. // Else do nothing - DOM Range gravity will adjust selection
  71. return NS_OK;
  72. }
  73. //XXX: We may want to store the selection state and restore it properly. Was
  74. // it an insertion point or an extended selection?
  75. NS_IMETHODIMP
  76. DeleteTextTransaction::UndoTransaction()
  77. {
  78. if (NS_WARN_IF(!mCharData)) {
  79. return NS_ERROR_NOT_INITIALIZED;
  80. }
  81. return mCharData->InsertData(mOffset, mDeletedText);
  82. }
  83. NS_IMETHODIMP
  84. DeleteTextTransaction::GetTxnDescription(nsAString& aString)
  85. {
  86. aString.AssignLiteral("DeleteTextTransaction: ");
  87. aString += mDeletedText;
  88. return NS_OK;
  89. }
  90. } // namespace mozilla