editor/libeditor/base/DeleteTextTxn.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "DeleteTextTxn.h"
michael@0 7 #include "mozilla/Assertions.h"
michael@0 8 #include "mozilla/dom/Selection.h"
michael@0 9 #include "nsAutoPtr.h"
michael@0 10 #include "nsDebug.h"
michael@0 11 #include "nsEditor.h"
michael@0 12 #include "nsError.h"
michael@0 13 #include "nsIEditor.h"
michael@0 14 #include "nsISelection.h"
michael@0 15 #include "nsISupportsImpl.h"
michael@0 16 #include "nsSelectionState.h"
michael@0 17 #include "nsAString.h"
michael@0 18
michael@0 19 using namespace mozilla;
michael@0 20 using namespace mozilla::dom;
michael@0 21
michael@0 22 DeleteTextTxn::DeleteTextTxn() :
michael@0 23 EditTxn(),
michael@0 24 mEditor(nullptr),
michael@0 25 mCharData(),
michael@0 26 mOffset(0),
michael@0 27 mNumCharsToDelete(0),
michael@0 28 mRangeUpdater(nullptr)
michael@0 29 {
michael@0 30 }
michael@0 31
michael@0 32 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTxn, EditTxn,
michael@0 33 mCharData)
michael@0 34
michael@0 35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTxn)
michael@0 36 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 37
michael@0 38 NS_IMETHODIMP
michael@0 39 DeleteTextTxn::Init(nsEditor* aEditor,
michael@0 40 nsIDOMCharacterData* aCharData,
michael@0 41 uint32_t aOffset,
michael@0 42 uint32_t aNumCharsToDelete,
michael@0 43 nsRangeUpdater* aRangeUpdater)
michael@0 44 {
michael@0 45 MOZ_ASSERT(aEditor && aCharData);
michael@0 46
michael@0 47 mEditor = aEditor;
michael@0 48 mCharData = aCharData;
michael@0 49
michael@0 50 // do nothing if the node is read-only
michael@0 51 if (!mEditor->IsModifiableNode(mCharData)) {
michael@0 52 return NS_ERROR_FAILURE;
michael@0 53 }
michael@0 54
michael@0 55 mOffset = aOffset;
michael@0 56 mNumCharsToDelete = aNumCharsToDelete;
michael@0 57 #ifdef DEBUG
michael@0 58 uint32_t length;
michael@0 59 mCharData->GetLength(&length);
michael@0 60 NS_ASSERTION(length >= aOffset + aNumCharsToDelete,
michael@0 61 "Trying to delete more characters than in node");
michael@0 62 #endif
michael@0 63 mDeletedText.Truncate();
michael@0 64 mRangeUpdater = aRangeUpdater;
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 NS_IMETHODIMP
michael@0 69 DeleteTextTxn::DoTransaction()
michael@0 70 {
michael@0 71 MOZ_ASSERT(mEditor && mCharData);
michael@0 72
michael@0 73 // get the text that we're about to delete
michael@0 74 nsresult res = mCharData->SubstringData(mOffset, mNumCharsToDelete,
michael@0 75 mDeletedText);
michael@0 76 MOZ_ASSERT(NS_SUCCEEDED(res));
michael@0 77 res = mCharData->DeleteData(mOffset, mNumCharsToDelete);
michael@0 78 NS_ENSURE_SUCCESS(res, res);
michael@0 79
michael@0 80 if (mRangeUpdater) {
michael@0 81 mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
michael@0 82 }
michael@0 83
michael@0 84 // only set selection to deletion point if editor gives permission
michael@0 85 bool bAdjustSelection;
michael@0 86 mEditor->ShouldTxnSetSelection(&bAdjustSelection);
michael@0 87 if (bAdjustSelection) {
michael@0 88 nsRefPtr<Selection> selection = mEditor->GetSelection();
michael@0 89 NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
michael@0 90 res = selection->Collapse(mCharData, mOffset);
michael@0 91 NS_ASSERTION(NS_SUCCEEDED(res),
michael@0 92 "selection could not be collapsed after undo of deletetext.");
michael@0 93 NS_ENSURE_SUCCESS(res, res);
michael@0 94 }
michael@0 95 // else do nothing - dom range gravity will adjust selection
michael@0 96 return NS_OK;
michael@0 97 }
michael@0 98
michael@0 99 //XXX: we may want to store the selection state and restore it properly
michael@0 100 // was it an insertion point or an extended selection?
michael@0 101 NS_IMETHODIMP
michael@0 102 DeleteTextTxn::UndoTransaction()
michael@0 103 {
michael@0 104 MOZ_ASSERT(mEditor && mCharData);
michael@0 105
michael@0 106 return mCharData->InsertData(mOffset, mDeletedText);
michael@0 107 }
michael@0 108
michael@0 109 NS_IMETHODIMP
michael@0 110 DeleteTextTxn::GetTxnDescription(nsAString& aString)
michael@0 111 {
michael@0 112 aString.AssignLiteral("DeleteTextTxn: ");
michael@0 113 aString += mDeletedText;
michael@0 114 return NS_OK;
michael@0 115 }

mercurial