Bug 1337698 - Use UniquePtr instead of nsAutoPtr in editor

* PlaceholderTransaction should use UniquePtr
* HTMLEditor should use UniquePtr
* TypeInState should use UniquePtr

Tag #1375
This commit is contained in:
Matt A. Tobin 2020-04-17 05:43:18 -04:00 • committed by Roy Tam
commit 32e4ad01e3
10 changed files with 32 additions and 44 deletions

View file

@ -688,13 +688,10 @@ EditorBase::DoTransaction(nsITransaction* aTxn)
{ {
if (mPlaceHolderBatch && !mPlaceHolderTxn) { if (mPlaceHolderBatch && !mPlaceHolderTxn) {
nsCOMPtr<nsIAbsorbingTransaction> placeholderTransaction = nsCOMPtr<nsIAbsorbingTransaction> placeholderTransaction =
new PlaceholderTransaction(); new PlaceholderTransaction(*this, mPlaceHolderName, Move(mSelState));
// Save off weak reference to placeholder transaction // Save off weak reference to placeholder transaction
mPlaceHolderTxn = do_GetWeakReference(placeholderTransaction); mPlaceHolderTxn = do_GetWeakReference(placeholderTransaction);
placeholderTransaction->Init(mPlaceHolderName, mSelState, this);
// placeholder txn took ownership of this pointer
mSelState = nullptr;
// QI to an nsITransaction since that's what DoTransaction() expects // QI to an nsITransaction since that's what DoTransaction() expects
nsCOMPtr<nsITransaction> transaction = nsCOMPtr<nsITransaction> transaction =
@ -944,7 +941,7 @@ EditorBase::BeginPlaceHolderTransaction(nsIAtom* aName)
mPlaceHolderName = aName; mPlaceHolderName = aName;
RefPtr<Selection> selection = GetSelection(); RefPtr<Selection> selection = GetSelection();
if (selection) { if (selection) {
mSelState = new SelectionState(); mSelState = MakeUnique<SelectionState>();
mSelState->SaveSelection(selection); mSelState->SaveSelection(selection);
// Composition transaction can modify multiple nodes and it merges text // Composition transaction can modify multiple nodes and it merges text
// node for ime into single text node. // node for ime into single text node.
@ -1008,7 +1005,6 @@ EditorBase::EndPlaceHolderTransaction()
if (mPlaceHolderName == nsGkAtoms::IMETxnName) { if (mPlaceHolderName == nsGkAtoms::IMETxnName) {
mRangeUpdater.DropSelectionState(*mSelState); mRangeUpdater.DropSelectionState(*mSelState);
} }
delete mSelState;
mSelState = nullptr; mSelState = nullptr;
} }
// We might have never made a placeholder if no action took place. // We might have never made a placeholder if no action took place.

View file

@ -11,6 +11,7 @@
#include "mozilla/OwningNonNull.h" // for OwningNonNull #include "mozilla/OwningNonNull.h" // for OwningNonNull
#include "mozilla/SelectionState.h" // for RangeUpdater, etc. #include "mozilla/SelectionState.h" // for RangeUpdater, etc.
#include "mozilla/StyleSheet.h" // for StyleSheet #include "mozilla/StyleSheet.h" // for StyleSheet
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Text.h" #include "mozilla/dom/Text.h"
#include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr #include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr
#include "nsCycleCollectionParticipant.h" #include "nsCycleCollectionParticipant.h"
@ -980,7 +981,7 @@ protected:
// Name of placeholder transaction. // Name of placeholder transaction.
nsIAtom* mPlaceHolderName; nsIAtom* mPlaceHolderName;
// Saved selection state for placeholder transaction batching. // Saved selection state for placeholder transaction batching.
SelectionState* mSelState; mozilla::UniquePtr<SelectionState> mSelState;
nsString* mPhonetic; nsString* mPhonetic;
// IME composition this is not null between compositionstart and // IME composition this is not null between compositionstart and
// compositionend. // compositionend.

View file

@ -16,12 +16,13 @@
#include "mozilla/EditorUtils.h" #include "mozilla/EditorUtils.h"
#include "mozilla/HTMLEditor.h" #include "mozilla/HTMLEditor.h"
#include "mozilla/MathAlgorithms.h" #include "mozilla/MathAlgorithms.h"
#include "mozilla/Move.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Selection.h" #include "mozilla/dom/Selection.h"
#include "mozilla/dom/Element.h" #include "mozilla/dom/Element.h"
#include "mozilla/OwningNonNull.h" #include "mozilla/OwningNonNull.h"
#include "mozilla/mozalloc.h" #include "mozilla/mozalloc.h"
#include "nsAutoPtr.h"
#include "nsAString.h" #include "nsAString.h"
#include "nsAlgorithm.h" #include "nsAlgorithm.h"
#include "nsCRT.h" #include "nsCRT.h"
@ -4410,20 +4411,21 @@ HTMLEditRules::CreateStyleForInsertText(Selection& aSelection,
NS_ENSURE_STATE(rootElement); NS_ENSURE_STATE(rootElement);
// process clearing any styles first // process clearing any styles first
nsAutoPtr<PropItem> item(mHTMLEditor->mTypeInState->TakeClearProperty()); UniquePtr<PropItem> item =
Move(mHTMLEditor->mTypeInState->TakeClearProperty());
while (item && node != rootElement) { while (item && node != rootElement) {
NS_ENSURE_STATE(mHTMLEditor); NS_ENSURE_STATE(mHTMLEditor);
nsresult rv = nsresult rv =
mHTMLEditor->ClearStyle(address_of(node), &offset, mHTMLEditor->ClearStyle(address_of(node), &offset,
item->tag, &item->attr); item->tag, &item->attr);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
item = mHTMLEditor->mTypeInState->TakeClearProperty(); item = Move(mHTMLEditor->mTypeInState->TakeClearProperty());
weDidSomething = true; weDidSomething = true;
} }
// then process setting any styles // then process setting any styles
int32_t relFontSize = mHTMLEditor->mTypeInState->TakeRelativeFontSize(); int32_t relFontSize = mHTMLEditor->mTypeInState->TakeRelativeFontSize();
item = mHTMLEditor->mTypeInState->TakeSetProperty(); item = Move(mHTMLEditor->mTypeInState->TakeSetProperty());
if (item || relFontSize) { if (item || relFontSize) {
// we have at least one style to add; make a new text node to insert style // we have at least one style to add; make a new text node to insert style

View file

@ -287,7 +287,7 @@ HTMLEditor::Init(nsIDOMDocument* aDoc,
} }
// Init the HTML-CSS utils // Init the HTML-CSS utils
mCSSEditUtils = new CSSEditUtils(this); mCSSEditUtils = MakeUnique<CSSEditUtils>(this);
// disable links // disable links
nsCOMPtr<nsIPresShell> presShell = GetPresShell(); nsCOMPtr<nsIPresShell> presShell = GetPresShell();

View file

@ -10,11 +10,11 @@
#include "mozilla/CSSEditUtils.h" #include "mozilla/CSSEditUtils.h"
#include "mozilla/StyleSheet.h" #include "mozilla/StyleSheet.h"
#include "mozilla/TextEditor.h" #include "mozilla/TextEditor.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Element.h" #include "mozilla/dom/Element.h"
#include "mozilla/dom/File.h" #include "mozilla/dom/File.h"
#include "nsAttrName.h" #include "nsAttrName.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsIContentFilter.h" #include "nsIContentFilter.h"
#include "nsICSSLoaderObserver.h" #include "nsICSSLoaderObserver.h"
@ -896,7 +896,7 @@ protected:
bool mCRInParagraphCreatesParagraph; bool mCRInParagraphCreatesParagraph;
bool mCSSAware; bool mCSSAware;
nsAutoPtr<CSSEditUtils> mCSSEditUtils; UniquePtr<CSSEditUtils> mCSSEditUtils;
// Used by GetFirstSelectedCell and GetNextSelectedCell // Used by GetFirstSelectedCell and GetNextSelectedCell
int32_t mSelectedCellIndex; int32_t mSelectedCellIndex;

View file

@ -8,6 +8,7 @@
#include "CompositionTransaction.h" #include "CompositionTransaction.h"
#include "mozilla/EditorBase.h" #include "mozilla/EditorBase.h"
#include "mozilla/dom/Selection.h" #include "mozilla/dom/Selection.h"
#include "mozilla/Move.h"
#include "nsGkAtoms.h" #include "nsGkAtoms.h"
#include "nsQueryObject.h" #include "nsQueryObject.h"
@ -15,13 +16,18 @@ namespace mozilla {
using namespace dom; using namespace dom;
PlaceholderTransaction::PlaceholderTransaction() PlaceholderTransaction::PlaceholderTransaction(
EditorBase& aEditorBase,
nsIAtom* aName,
UniquePtr<SelectionState> aSelState)
: mAbsorb(true) : mAbsorb(true)
, mForwarding(nullptr) , mForwarding(nullptr)
, mCompositionTransaction(nullptr) , mCompositionTransaction(nullptr)
, mCommitted(false) , mCommitted(false)
, mEditorBase(nullptr) , mStartSel(Move(aSelState))
, mEditorBase(&aEditorBase)
{ {
mName = aName;
} }
PlaceholderTransaction::~PlaceholderTransaction() PlaceholderTransaction::~PlaceholderTransaction()
@ -56,19 +62,6 @@ NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
NS_IMPL_ADDREF_INHERITED(PlaceholderTransaction, EditAggregateTransaction) NS_IMPL_ADDREF_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
NS_IMPL_RELEASE_INHERITED(PlaceholderTransaction, EditAggregateTransaction) NS_IMPL_RELEASE_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
NS_IMETHODIMP
PlaceholderTransaction::Init(nsIAtom* aName,
SelectionState* aSelState,
EditorBase* aEditorBase)
{
NS_ENSURE_TRUE(aEditorBase && aSelState, NS_ERROR_NULL_POINTER);
mName = aName;
mStartSel = aSelState;
mEditorBase = aEditorBase;
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
PlaceholderTransaction::DoTransaction() PlaceholderTransaction::DoTransaction()
{ {

View file

@ -8,12 +8,12 @@
#include "EditAggregateTransaction.h" #include "EditAggregateTransaction.h"
#include "mozilla/EditorUtils.h" #include "mozilla/EditorUtils.h"
#include "mozilla/UniquePtr.h"
#include "nsIAbsorbingTransaction.h" #include "nsIAbsorbingTransaction.h"
#include "nsIDOMNode.h" #include "nsIDOMNode.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsWeakPtr.h" #include "nsWeakPtr.h"
#include "nsWeakReference.h" #include "nsWeakReference.h"
#include "nsAutoPtr.h"
namespace mozilla { namespace mozilla {
@ -33,7 +33,8 @@ class PlaceholderTransaction final : public EditAggregateTransaction,
public: public:
NS_DECL_ISUPPORTS_INHERITED NS_DECL_ISUPPORTS_INHERITED
PlaceholderTransaction(); PlaceholderTransaction(EditorBase& aEditorBase, nsIAtom* aName,
UniquePtr<SelectionState> aSelState);
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PlaceholderTransaction, NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PlaceholderTransaction,
EditAggregateTransaction) EditAggregateTransaction)
@ -46,9 +47,6 @@ public:
// ------------ nsIAbsorbingTransaction ----------------------- // ------------ nsIAbsorbingTransaction -----------------------
NS_IMETHOD Init(nsIAtom* aName, SelectionState* aSelState,
EditorBase* aEditorBase) override;
NS_IMETHOD GetTxnName(nsIAtom** aName) override; NS_IMETHOD GetTxnName(nsIAtom** aName) override;
NS_IMETHOD StartSelectionEquals(SelectionState* aSelState, NS_IMETHOD StartSelectionEquals(SelectionState* aSelState,
@ -80,7 +78,7 @@ protected:
// restore the selection properly. // restore the selection properly.
// Use a pointer because this is constructed before we exist. // Use a pointer because this is constructed before we exist.
nsAutoPtr<SelectionState> mStartSel; UniquePtr<SelectionState> mStartSel;
SelectionState mEndSel; SelectionState mEndSel;
// The editor for this transaction. // The editor for this transaction.

View file

@ -193,7 +193,7 @@ TypeInState::ClearProp(nsIAtom* aProp,
* TakeClearProperty() hands back next property item on the clear list. * TakeClearProperty() hands back next property item on the clear list.
* Caller assumes ownership of PropItem and must delete it. * Caller assumes ownership of PropItem and must delete it.
*/ */
PropItem* UniquePtr<PropItem>
TypeInState::TakeClearProperty() TypeInState::TakeClearProperty()
{ {
size_t count = mClearedArray.Length(); size_t count = mClearedArray.Length();
@ -204,14 +204,14 @@ TypeInState::TakeClearProperty()
--count; // indices are zero based --count; // indices are zero based
PropItem* propItem = mClearedArray[count]; PropItem* propItem = mClearedArray[count];
mClearedArray.RemoveElementAt(count); mClearedArray.RemoveElementAt(count);
return propItem; return UniquePtr<PropItem>(propItem);
} }
/** /**
* TakeSetProperty() hands back next poroperty item on the set list. * TakeSetProperty() hands back next poroperty item on the set list.
* Caller assumes ownership of PropItem and must delete it. * Caller assumes ownership of PropItem and must delete it.
*/ */
PropItem* UniquePtr<PropItem>
TypeInState::TakeSetProperty() TypeInState::TakeSetProperty()
{ {
size_t count = mSetArray.Length(); size_t count = mSetArray.Length();
@ -221,7 +221,7 @@ TypeInState::TakeSetProperty()
count--; // indices are zero based count--; // indices are zero based
PropItem* propItem = mSetArray[count]; PropItem* propItem = mSetArray[count];
mSetArray.RemoveElementAt(count); mSetArray.RemoveElementAt(count);
return propItem; return UniquePtr<PropItem>(propItem);
} }
/** /**

View file

@ -6,6 +6,7 @@
#ifndef TypeInState_h #ifndef TypeInState_h
#define TypeInState_h #define TypeInState_h
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h" #include "nsCycleCollectionParticipant.h"
#include "nsISelectionListener.h" #include "nsISelectionListener.h"
@ -63,13 +64,13 @@ public:
* TakeClearProperty() hands back next property item on the clear list. * TakeClearProperty() hands back next property item on the clear list.
* Caller assumes ownership of PropItem and must delete it. * Caller assumes ownership of PropItem and must delete it.
*/ */
PropItem* TakeClearProperty(); UniquePtr<PropItem> TakeClearProperty();
/** /**
* TakeSetProperty() hands back next property item on the set list. * TakeSetProperty() hands back next property item on the set list.
* Caller assumes ownership of PropItem and must delete it. * Caller assumes ownership of PropItem and must delete it.
*/ */
PropItem* TakeSetProperty(); UniquePtr<PropItem> TakeSetProperty();
/** /**
* TakeRelativeFontSize() hands back relative font value, which is then * TakeRelativeFontSize() hands back relative font value, which is then

View file

@ -35,9 +35,6 @@ public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IABSORBINGTRANSACTION_IID) NS_DECLARE_STATIC_IID_ACCESSOR(NS_IABSORBINGTRANSACTION_IID)
NS_IMETHOD Init(nsIAtom* aName, mozilla::SelectionState* aSelState,
mozilla::EditorBase* aEditorBase) = 0;
NS_IMETHOD EndPlaceHolderBatch()=0; NS_IMETHOD EndPlaceHolderBatch()=0;
NS_IMETHOD GetTxnName(nsIAtom **aName)=0; NS_IMETHOD GetTxnName(nsIAtom **aName)=0;