editor/libeditor/base/DeleteTextTxn.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/base/DeleteTextTxn.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "DeleteTextTxn.h"
    1.10 +#include "mozilla/Assertions.h"
    1.11 +#include "mozilla/dom/Selection.h"
    1.12 +#include "nsAutoPtr.h"
    1.13 +#include "nsDebug.h"
    1.14 +#include "nsEditor.h"
    1.15 +#include "nsError.h"
    1.16 +#include "nsIEditor.h"
    1.17 +#include "nsISelection.h"
    1.18 +#include "nsISupportsImpl.h"
    1.19 +#include "nsSelectionState.h"
    1.20 +#include "nsAString.h"
    1.21 +
    1.22 +using namespace mozilla;
    1.23 +using namespace mozilla::dom;
    1.24 +
    1.25 +DeleteTextTxn::DeleteTextTxn() :
    1.26 +  EditTxn(),
    1.27 +  mEditor(nullptr),
    1.28 +  mCharData(),
    1.29 +  mOffset(0),
    1.30 +  mNumCharsToDelete(0),
    1.31 +  mRangeUpdater(nullptr)
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTxn, EditTxn,
    1.36 +                                   mCharData)
    1.37 +
    1.38 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTxn)
    1.39 +NS_INTERFACE_MAP_END_INHERITING(EditTxn)
    1.40 +
    1.41 +NS_IMETHODIMP
    1.42 +DeleteTextTxn::Init(nsEditor* aEditor,
    1.43 +                    nsIDOMCharacterData* aCharData,
    1.44 +                    uint32_t aOffset,
    1.45 +                    uint32_t aNumCharsToDelete,
    1.46 +                    nsRangeUpdater* aRangeUpdater)
    1.47 +{
    1.48 +  MOZ_ASSERT(aEditor && aCharData);
    1.49 +
    1.50 +  mEditor = aEditor;
    1.51 +  mCharData = aCharData;
    1.52 +
    1.53 +  // do nothing if the node is read-only
    1.54 +  if (!mEditor->IsModifiableNode(mCharData)) {
    1.55 +    return NS_ERROR_FAILURE;
    1.56 +  }
    1.57 +
    1.58 +  mOffset = aOffset;
    1.59 +  mNumCharsToDelete = aNumCharsToDelete;
    1.60 +#ifdef DEBUG
    1.61 +  uint32_t length;
    1.62 +  mCharData->GetLength(&length);
    1.63 +  NS_ASSERTION(length >= aOffset + aNumCharsToDelete,
    1.64 +               "Trying to delete more characters than in node");
    1.65 +#endif
    1.66 +  mDeletedText.Truncate();
    1.67 +  mRangeUpdater = aRangeUpdater;
    1.68 +  return NS_OK;
    1.69 +}
    1.70 +
    1.71 +NS_IMETHODIMP
    1.72 +DeleteTextTxn::DoTransaction()
    1.73 +{
    1.74 +  MOZ_ASSERT(mEditor && mCharData);
    1.75 +
    1.76 +  // get the text that we're about to delete
    1.77 +  nsresult res = mCharData->SubstringData(mOffset, mNumCharsToDelete,
    1.78 +                                          mDeletedText);
    1.79 +  MOZ_ASSERT(NS_SUCCEEDED(res));
    1.80 +  res = mCharData->DeleteData(mOffset, mNumCharsToDelete);
    1.81 +  NS_ENSURE_SUCCESS(res, res);
    1.82 +
    1.83 +  if (mRangeUpdater) {
    1.84 +    mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
    1.85 +  }
    1.86 +
    1.87 +  // only set selection to deletion point if editor gives permission
    1.88 +  bool bAdjustSelection;
    1.89 +  mEditor->ShouldTxnSetSelection(&bAdjustSelection);
    1.90 +  if (bAdjustSelection) {
    1.91 +    nsRefPtr<Selection> selection = mEditor->GetSelection();
    1.92 +    NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
    1.93 +    res = selection->Collapse(mCharData, mOffset);
    1.94 +    NS_ASSERTION(NS_SUCCEEDED(res),
    1.95 +                 "selection could not be collapsed after undo of deletetext.");
    1.96 +    NS_ENSURE_SUCCESS(res, res);
    1.97 +  }
    1.98 +  // else do nothing - dom range gravity will adjust selection
    1.99 +  return NS_OK;
   1.100 +}
   1.101 +
   1.102 +//XXX: we may want to store the selection state and restore it properly
   1.103 +//     was it an insertion point or an extended selection?
   1.104 +NS_IMETHODIMP
   1.105 +DeleteTextTxn::UndoTransaction()
   1.106 +{
   1.107 +  MOZ_ASSERT(mEditor && mCharData);
   1.108 +
   1.109 +  return mCharData->InsertData(mOffset, mDeletedText);
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +DeleteTextTxn::GetTxnDescription(nsAString& aString)
   1.114 +{
   1.115 +  aString.AssignLiteral("DeleteTextTxn: ");
   1.116 +  aString += mDeletedText;
   1.117 +  return NS_OK;
   1.118 +}

mercurial