2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
#ifndef DeleteRangeTransaction_h
|
|
|
|
#define DeleteRangeTransaction_h
|
1999-01-14 21:02:45 +03:00
|
|
|
|
Bug 1797247 - part 1: Add delete transaction classes to use build time type checks r=m_kato
First, `EditorBase::CreateTransactionForDeleteSelection` returns an instance of
`EditAggregateTransaction`. It's a base class of `PlaceholderTransaction` and
`DeleteRangeTransaction` but it's also a concrete class. However, it's too
generic. Therefore, this patch creates `DeleteMultipleRangesTransaction` which
is a simple sub-class of it, and makes `EditAggregateTransaction` be an abstract
class. Then, add `AddChild` methods to each concrete class to restrict the
type of child transactions.
Next, `DeleteRangeTransaction` contains only `DeleteNodeTransaction` and
`DeleteTextTransaction`. Therefore, once they have a common base class,
we can check the type easier. Therefore, this patch also adds
`DeleteContentTransactionBase` and
`EditorBase::CreateTransactionForCollapsedRange` becomes clearer what it
returns.
With these changes, `DeleteRangeTransaction` obviously contains only
`DeleteContentTransactionBase`, `DeleteMultipleRangesTransaction` contains only
`DeleteRangeTransaction`, `DeleteNodeTransaction` and `DeleteTextTransaction`.
And they are guaranteed at build time (at least from outside the classes).
***
fix
Differential Revision: https://phabricator.services.mozilla.com/D169038
2023-02-16 01:17:17 +03:00
|
|
|
#include "DeleteContentTransactionBase.h"
|
2016-07-07 11:11:30 +03:00
|
|
|
#include "EditAggregateTransaction.h"
|
2022-04-20 17:46:16 +03:00
|
|
|
|
2022-10-28 14:07:37 +03:00
|
|
|
#include "EditorBase.h"
|
2023-02-16 01:23:34 +03:00
|
|
|
#include "EditorDOMPoint.h"
|
2022-04-20 17:46:16 +03:00
|
|
|
#include "EditorForwards.h"
|
|
|
|
|
2022-10-28 14:07:37 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2012-06-25 14:22:49 +04:00
|
|
|
#include "nsRange.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nscore.h"
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
class nsINode;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
1999-01-14 21:02:45 +03:00
|
|
|
/**
|
|
|
|
* A transaction that deletes an entire range in the content tree
|
|
|
|
*/
|
2016-07-07 11:11:30 +03:00
|
|
|
class DeleteRangeTransaction final : public EditAggregateTransaction {
|
2017-12-18 12:08:43 +03:00
|
|
|
protected:
|
2020-05-03 02:44:46 +03:00
|
|
|
DeleteRangeTransaction(EditorBase& aEditorBase,
|
|
|
|
const nsRange& aRangeToDelete);
|
2017-12-18 12:08:43 +03:00
|
|
|
|
1999-01-14 21:02:45 +03:00
|
|
|
public:
|
2016-07-07 08:56:53 +03:00
|
|
|
/**
|
2017-12-18 12:08:43 +03:00
|
|
|
* Creates a delete range transaction. This never returns nullptr.
|
|
|
|
*
|
2017-03-09 05:19:39 +03:00
|
|
|
* @param aEditorBase The object providing basic editing operations.
|
|
|
|
* @param aRangeToDelete The range to delete.
|
2016-07-07 08:56:53 +03:00
|
|
|
*/
|
2017-12-18 12:08:43 +03:00
|
|
|
static already_AddRefed<DeleteRangeTransaction> Create(
|
2020-05-03 02:44:46 +03:00
|
|
|
EditorBase& aEditorBase, const nsRange& aRangeToDelete) {
|
2017-12-18 12:08:43 +03:00
|
|
|
RefPtr<DeleteRangeTransaction> transaction =
|
|
|
|
new DeleteRangeTransaction(aEditorBase, aRangeToDelete);
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteRangeTransaction,
|
2016-07-07 11:11:30 +03:00
|
|
|
EditAggregateTransaction)
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_DECL_EDITTRANSACTIONBASE
|
2020-04-23 08:46:43 +03:00
|
|
|
NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(DeleteRangeTransaction)
|
1999-01-14 21:02:45 +03:00
|
|
|
|
Bug 1797247 - part 1: Add delete transaction classes to use build time type checks r=m_kato
First, `EditorBase::CreateTransactionForDeleteSelection` returns an instance of
`EditAggregateTransaction`. It's a base class of `PlaceholderTransaction` and
`DeleteRangeTransaction` but it's also a concrete class. However, it's too
generic. Therefore, this patch creates `DeleteMultipleRangesTransaction` which
is a simple sub-class of it, and makes `EditAggregateTransaction` be an abstract
class. Then, add `AddChild` methods to each concrete class to restrict the
type of child transactions.
Next, `DeleteRangeTransaction` contains only `DeleteNodeTransaction` and
`DeleteTextTransaction`. Therefore, once they have a common base class,
we can check the type easier. Therefore, this patch also adds
`DeleteContentTransactionBase` and
`EditorBase::CreateTransactionForCollapsedRange` becomes clearer what it
returns.
With these changes, `DeleteRangeTransaction` obviously contains only
`DeleteContentTransactionBase`, `DeleteMultipleRangesTransaction` contains only
`DeleteRangeTransaction`, `DeleteNodeTransaction` and `DeleteTextTransaction`.
And they are guaranteed at build time (at least from outside the classes).
***
fix
Differential Revision: https://phabricator.services.mozilla.com/D169038
2023-02-16 01:17:17 +03:00
|
|
|
void AppendChild(DeleteContentTransactionBase& aTransaction);
|
|
|
|
|
2020-04-03 11:32:01 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
|
1999-09-22 02:31:27 +04:00
|
|
|
|
2023-02-16 01:23:34 +03:00
|
|
|
/**
|
|
|
|
* Return a good point to put caret after calling DoTransaction().
|
|
|
|
*/
|
|
|
|
EditorDOMPoint SuggestPointToPutCaret() const {
|
|
|
|
return mPointToPutCaret.IsSetAndValid() ? mPointToPutCaret
|
|
|
|
: EditorDOMPoint();
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
protected:
|
2022-10-25 10:00:26 +03:00
|
|
|
/**
|
|
|
|
* Extend the range by adding a surrounding whitespace character to the range
|
|
|
|
* that is about to be deleted. This method depends on the pref
|
|
|
|
* `layout.word_select.delete_space_after_doubleclick_selection`.
|
|
|
|
*
|
|
|
|
* Considered cases:
|
|
|
|
* "one [two] three" -> "one [two ]three" -> "one three"
|
|
|
|
* "[one] two" -> "[one ]two" -> "two"
|
|
|
|
* "one [two]" -> "one[ two]" -> "one"
|
|
|
|
* "one [two], three" -> "one[ two], three" -> "one, three"
|
|
|
|
* "one [two]" -> "one [ two]" -> "one "
|
|
|
|
*
|
|
|
|
* @param aRange [inout] The range that is about to be deleted.
|
|
|
|
* @return NS_OK, unless nsRange::SetStart / ::SetEnd fails.
|
|
|
|
*/
|
|
|
|
nsresult MaybeExtendDeletingRangeWithSurroundingWhitespace(
|
|
|
|
nsRange& aRange) const;
|
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
/**
|
2022-10-28 14:07:37 +03:00
|
|
|
* AppendTransactionsToDeleteIn() creates a DeleteTextTransaction or some
|
2017-11-08 09:01:23 +03:00
|
|
|
* DeleteNodeTransactions to remove text or nodes between aStart and aEnd
|
|
|
|
* and appends the created transactions to the array.
|
|
|
|
*
|
2022-10-28 14:07:37 +03:00
|
|
|
* @param aRangeToDelete Must be positioned, valid and in same container.
|
2017-11-08 09:01:23 +03:00
|
|
|
* @return Returns NS_OK in most cases.
|
|
|
|
* When the arguments are invalid, returns
|
|
|
|
* NS_ERROR_INVALID_ARG.
|
|
|
|
* When mEditorBase isn't available, returns
|
2022-10-28 14:07:37 +03:00
|
|
|
* NS_ERROR_NOT_AVAILABLE.
|
2017-11-08 09:01:23 +03:00
|
|
|
* When created DeleteTextTransaction cannot do its
|
|
|
|
* transaction, returns NS_ERROR_FAILURE.
|
|
|
|
* Note that even if one of created DeleteNodeTransaction
|
|
|
|
* cannot do its transaction, this returns NS_OK.
|
|
|
|
*/
|
2022-10-28 14:07:37 +03:00
|
|
|
nsresult AppendTransactionsToDeleteIn(
|
|
|
|
const EditorRawDOMRange& aRangeToDelete);
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2022-10-28 14:07:37 +03:00
|
|
|
/**
|
|
|
|
* AppendTransactionsToDeleteNodesWhoseEndBoundaryIn() creates
|
|
|
|
* DeleteNodeTransaction instances to remove nodes whose end is in the range
|
|
|
|
* (in other words, its end tag is in the range if it's an element) and append
|
|
|
|
* them to the array.
|
|
|
|
*
|
|
|
|
* @param aRangeToDelete Must be positioned and valid.
|
|
|
|
*/
|
|
|
|
nsresult AppendTransactionsToDeleteNodesWhoseEndBoundaryIn(
|
|
|
|
const EditorRawDOMRange& aRangeToDelete);
|
2012-06-25 14:22:49 +04:00
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
/**
|
2022-10-28 14:07:37 +03:00
|
|
|
* AppendTransactionToDeleteText() creates a DeleteTextTransaction to delete
|
2017-12-07 12:45:52 +03:00
|
|
|
* text between start of aPoint.GetContainer() and aPoint or aPoint and end of
|
|
|
|
* aPoint.GetContainer() and appends the created transaction to the array.
|
2017-11-08 09:01:23 +03:00
|
|
|
*
|
2022-10-28 14:07:37 +03:00
|
|
|
* @param aMaybePointInText Must be set and valid. If the point is not
|
|
|
|
* in a text node, this method does nothing.
|
2017-11-08 09:01:23 +03:00
|
|
|
* @param aAction If nsIEditor::eNext, this method creates a transaction
|
|
|
|
* to delete text from aPoint to the end of the data node.
|
|
|
|
* Otherwise, this method creates a transaction to delete
|
|
|
|
* text from start of the data node to aPoint.
|
|
|
|
* @return Returns NS_OK in most cases.
|
|
|
|
* When the arguments are invalid, returns
|
|
|
|
* NS_ERROR_INVALID_ARG.
|
|
|
|
* When mEditorBase isn't available, returns
|
2022-10-28 14:07:37 +03:00
|
|
|
* NS_ERROR_NOT_AVAILABLE.
|
2017-11-08 09:01:23 +03:00
|
|
|
* When created DeleteTextTransaction cannot do its
|
|
|
|
* transaction, returns NS_ERROR_FAILURE.
|
|
|
|
* Note that even if no character will be deleted,
|
|
|
|
* this returns NS_OK.
|
|
|
|
*/
|
2022-10-28 14:07:37 +03:00
|
|
|
nsresult AppendTransactionToDeleteText(
|
|
|
|
const EditorRawDOMPoint& aMaybePointInText,
|
|
|
|
nsIEditor::EDirection aAction);
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
// The editor for this transaction.
|
2017-03-21 13:00:36 +03:00
|
|
|
RefPtr<EditorBase> mEditorBase;
|
2017-03-09 05:19:39 +03:00
|
|
|
|
2017-03-24 23:07:11 +03:00
|
|
|
// P1 in the range. This is only non-null until DoTransaction is called and
|
|
|
|
// we convert it into child transactions.
|
2017-03-09 05:19:39 +03:00
|
|
|
RefPtr<nsRange> mRangeToDelete;
|
2023-02-16 01:23:34 +03:00
|
|
|
|
|
|
|
EditorDOMPoint mPointToPutCaret;
|
1999-01-14 21:02:45 +03:00
|
|
|
};
|
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // #ifndef DeleteRangeTransaction_h
|