mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 13:51:41 +00:00
Bug 1425412 - part 1: Create InsertTextTransaction::Create() and remove EditorBase::CreateTxnForInsertText() r=m_kato
EditorBase::CreateTxnForInsertText() just hides what it exactly does. Rewriting it with a static factory method, InsertTextTransaction::Create() should be clearer for its caller. MozReview-Commit-ID: Er7Zlhtbnb0 --HG-- extra : rebase_source : 9dc71b3baab839f61153b96806fac5baae5d46cb
This commit is contained in:
parent
e65ab1463e
commit
ab059453f1
@ -2795,7 +2795,8 @@ EditorBase::InsertTextIntoTextNodeImpl(const nsAString& aStringToInsert,
|
||||
insertedTextNode = mComposition->GetContainerTextNode();
|
||||
insertedOffset = mComposition->XPOffsetInTextNode();
|
||||
} else {
|
||||
transaction = CreateTxnForInsertText(aStringToInsert, aTextNode, aOffset);
|
||||
transaction =
|
||||
InsertTextTransaction::Create(*this, aStringToInsert, aTextNode, aOffset);
|
||||
}
|
||||
|
||||
// Let listeners know what's up
|
||||
@ -2998,17 +2999,6 @@ EditorBase::SetTextImpl(Selection& aSelection, const nsAString& aString,
|
||||
return rv;
|
||||
}
|
||||
|
||||
already_AddRefed<InsertTextTransaction>
|
||||
EditorBase::CreateTxnForInsertText(const nsAString& aStringToInsert,
|
||||
Text& aTextNode,
|
||||
int32_t aOffset)
|
||||
{
|
||||
RefPtr<InsertTextTransaction> transaction =
|
||||
new InsertTextTransaction(aTextNode, aOffset, aStringToInsert, *this,
|
||||
&mRangeUpdater);
|
||||
return transaction.forget();
|
||||
}
|
||||
|
||||
nsresult
|
||||
EditorBase::DeleteText(nsGenericDOMDataNode& aCharData,
|
||||
uint32_t aOffset,
|
||||
|
@ -497,6 +497,8 @@ public:
|
||||
|
||||
void SwitchTextDirectionTo(uint32_t aDirection);
|
||||
|
||||
RangeUpdater& RangeUpdaterRef() { return mRangeUpdater; }
|
||||
|
||||
protected:
|
||||
nsresult DetermineCurrentDirection();
|
||||
void FireInputEvent();
|
||||
@ -613,14 +615,6 @@ protected:
|
||||
int32_t* aOffset,
|
||||
int32_t* aLength);
|
||||
|
||||
/**
|
||||
* Create a transaction for inserting aStringToInsert into aTextNode. Never
|
||||
* returns null.
|
||||
*/
|
||||
already_AddRefed<mozilla::InsertTextTransaction>
|
||||
CreateTxnForInsertText(const nsAString& aStringToInsert, Text& aTextNode,
|
||||
int32_t aOffset);
|
||||
|
||||
/**
|
||||
* Never returns null.
|
||||
*/
|
||||
|
@ -18,16 +18,26 @@ namespace mozilla {
|
||||
|
||||
using namespace dom;
|
||||
|
||||
InsertTextTransaction::InsertTextTransaction(Text& aTextNode,
|
||||
uint32_t aOffset,
|
||||
// static
|
||||
already_AddRefed<InsertTextTransaction>
|
||||
InsertTextTransaction::Create(EditorBase& aEditorBase,
|
||||
const nsAString& aStringToInsert,
|
||||
Text& aTextNode,
|
||||
uint32_t aOffset)
|
||||
{
|
||||
RefPtr<InsertTextTransaction> transaction =
|
||||
new InsertTextTransaction(aEditorBase, aStringToInsert, aTextNode, aOffset);
|
||||
return transaction.forget();
|
||||
}
|
||||
|
||||
InsertTextTransaction::InsertTextTransaction(EditorBase& aEditorBase,
|
||||
const nsAString& aStringToInsert,
|
||||
EditorBase& aEditorBase,
|
||||
RangeUpdater* aRangeUpdater)
|
||||
Text& aTextNode,
|
||||
uint32_t aOffset)
|
||||
: mTextNode(&aTextNode)
|
||||
, mOffset(aOffset)
|
||||
, mStringToInsert(aStringToInsert)
|
||||
, mEditorBase(&aEditorBase)
|
||||
, mRangeUpdater(aRangeUpdater)
|
||||
{
|
||||
}
|
||||
|
||||
@ -56,12 +66,16 @@ InsertTextTransaction::DoTransaction()
|
||||
}
|
||||
|
||||
nsresult rv = mTextNode->InsertData(mOffset, mStringToInsert);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// Only set selection to insertion point if editor gives permission
|
||||
if (mEditorBase->GetShouldTxnSetSelection()) {
|
||||
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
||||
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
||||
if (NS_WARN_IF(!selection)) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
DebugOnly<nsresult> rv =
|
||||
selection->Collapse(mTextNode, mOffset + mStringToInsert.Length());
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv),
|
||||
@ -69,7 +83,8 @@ InsertTextTransaction::DoTransaction()
|
||||
} else {
|
||||
// Do nothing - DOM Range gravity will adjust selection
|
||||
}
|
||||
mRangeUpdater->SelAdjInsertText(*mTextNode, mOffset, mStringToInsert);
|
||||
mEditorBase->RangeUpdaterRef().
|
||||
SelAdjInsertText(*mTextNode, mOffset, mStringToInsert);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ class nsITransaction;
|
||||
namespace mozilla {
|
||||
|
||||
class EditorBase;
|
||||
class RangeUpdater;
|
||||
|
||||
namespace dom {
|
||||
class Text;
|
||||
@ -33,19 +32,29 @@ class Text;
|
||||
*/
|
||||
class InsertTextTransaction final : public EditTransactionBase
|
||||
{
|
||||
public:
|
||||
NS_DECLARE_STATIC_IID_ACCESSOR(NS_INSERTTEXTTXN_IID)
|
||||
protected:
|
||||
InsertTextTransaction(EditorBase& aEditorBase,
|
||||
const nsAString& aStringToInsert,
|
||||
dom::Text& aTextNode,
|
||||
uint32_t aOffset);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param aElement The text content node.
|
||||
* @param aOffset The location in aElement to do the insertion.
|
||||
* @param aString The new text to insert.
|
||||
* @param aPresShell Used to get and set the selection.
|
||||
* @param aRangeUpdater The range updater
|
||||
* Creates new InsertTextTransaction instance. This never returns nullptr.
|
||||
*
|
||||
* @param aEditorBase The editor which manages the transaction.
|
||||
* @param aTextNode The text content node to be inserted
|
||||
* aStringToInsert.
|
||||
* @param aOffset The offset in aTextNode to do the insertion.
|
||||
* @param aStringToInsert The new string to insert.
|
||||
*/
|
||||
InsertTextTransaction(dom::Text& aTextNode, uint32_t aOffset,
|
||||
const nsAString& aString, EditorBase& aEditorBase,
|
||||
RangeUpdater* aRangeUpdater);
|
||||
static already_AddRefed<InsertTextTransaction>
|
||||
Create(EditorBase& aEditorBase,
|
||||
const nsAString& aStringToInsert,
|
||||
dom::Text& aTextNode,
|
||||
uint32_t aOffset);
|
||||
|
||||
NS_DECLARE_STATIC_IID_ACCESSOR(NS_INSERTTEXTTXN_IID)
|
||||
|
||||
NS_DECL_ISUPPORTS_INHERITED
|
||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertTextTransaction,
|
||||
@ -77,8 +86,6 @@ private:
|
||||
|
||||
// The editor, which we'll need to get the selection.
|
||||
RefPtr<EditorBase> mEditorBase;
|
||||
|
||||
RangeUpdater* mRangeUpdater;
|
||||
};
|
||||
|
||||
NS_DEFINE_STATIC_IID_ACCESSOR(InsertTextTransaction, NS_INSERTTEXTTXN_IID)
|
||||
|
Loading…
Reference in New Issue
Block a user