editor/libeditor/base/DeleteTextTxn.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 #include "DeleteTextTxn.h"
     7 #include "mozilla/Assertions.h"
     8 #include "mozilla/dom/Selection.h"
     9 #include "nsAutoPtr.h"
    10 #include "nsDebug.h"
    11 #include "nsEditor.h"
    12 #include "nsError.h"
    13 #include "nsIEditor.h"
    14 #include "nsISelection.h"
    15 #include "nsISupportsImpl.h"
    16 #include "nsSelectionState.h"
    17 #include "nsAString.h"
    19 using namespace mozilla;
    20 using namespace mozilla::dom;
    22 DeleteTextTxn::DeleteTextTxn() :
    23   EditTxn(),
    24   mEditor(nullptr),
    25   mCharData(),
    26   mOffset(0),
    27   mNumCharsToDelete(0),
    28   mRangeUpdater(nullptr)
    29 {
    30 }
    32 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTxn, EditTxn,
    33                                    mCharData)
    35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTxn)
    36 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
    38 NS_IMETHODIMP
    39 DeleteTextTxn::Init(nsEditor* aEditor,
    40                     nsIDOMCharacterData* aCharData,
    41                     uint32_t aOffset,
    42                     uint32_t aNumCharsToDelete,
    43                     nsRangeUpdater* aRangeUpdater)
    44 {
    45   MOZ_ASSERT(aEditor && aCharData);
    47   mEditor = aEditor;
    48   mCharData = aCharData;
    50   // do nothing if the node is read-only
    51   if (!mEditor->IsModifiableNode(mCharData)) {
    52     return NS_ERROR_FAILURE;
    53   }
    55   mOffset = aOffset;
    56   mNumCharsToDelete = aNumCharsToDelete;
    57 #ifdef DEBUG
    58   uint32_t length;
    59   mCharData->GetLength(&length);
    60   NS_ASSERTION(length >= aOffset + aNumCharsToDelete,
    61                "Trying to delete more characters than in node");
    62 #endif
    63   mDeletedText.Truncate();
    64   mRangeUpdater = aRangeUpdater;
    65   return NS_OK;
    66 }
    68 NS_IMETHODIMP
    69 DeleteTextTxn::DoTransaction()
    70 {
    71   MOZ_ASSERT(mEditor && mCharData);
    73   // get the text that we're about to delete
    74   nsresult res = mCharData->SubstringData(mOffset, mNumCharsToDelete,
    75                                           mDeletedText);
    76   MOZ_ASSERT(NS_SUCCEEDED(res));
    77   res = mCharData->DeleteData(mOffset, mNumCharsToDelete);
    78   NS_ENSURE_SUCCESS(res, res);
    80   if (mRangeUpdater) {
    81     mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
    82   }
    84   // only set selection to deletion point if editor gives permission
    85   bool bAdjustSelection;
    86   mEditor->ShouldTxnSetSelection(&bAdjustSelection);
    87   if (bAdjustSelection) {
    88     nsRefPtr<Selection> selection = mEditor->GetSelection();
    89     NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
    90     res = selection->Collapse(mCharData, mOffset);
    91     NS_ASSERTION(NS_SUCCEEDED(res),
    92                  "selection could not be collapsed after undo of deletetext.");
    93     NS_ENSURE_SUCCESS(res, res);
    94   }
    95   // else do nothing - dom range gravity will adjust selection
    96   return NS_OK;
    97 }
    99 //XXX: we may want to store the selection state and restore it properly
   100 //     was it an insertion point or an extended selection?
   101 NS_IMETHODIMP
   102 DeleteTextTxn::UndoTransaction()
   103 {
   104   MOZ_ASSERT(mEditor && mCharData);
   106   return mCharData->InsertData(mOffset, mDeletedText);
   107 }
   109 NS_IMETHODIMP
   110 DeleteTextTxn::GetTxnDescription(nsAString& aString)
   111 {
   112   aString.AssignLiteral("DeleteTextTxn: ");
   113   aString += mDeletedText;
   114   return NS_OK;
   115 }

mercurial