mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-23 02:05:42 +00:00
ca49cc9d2d
In the next patch, we need to update unmasked range when anonymous text node in `<input type="password">` is modified. This patch makes editor manage it easier to update the range. Note that the next patch also handles text node creation and destruction. Differential Revision: https://phabricator.services.mozilla.com/D38003 --HG-- extra : moz-landing-system : lando
133 lines
4.6 KiB
C++
133 lines
4.6 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* 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/. */
|
|
|
|
#include "InsertTextTransaction.h"
|
|
|
|
#include "mozilla/EditorBase.h" // mEditorBase
|
|
#include "mozilla/SelectionState.h" // RangeUpdater
|
|
#include "mozilla/dom/Selection.h" // Selection local var
|
|
#include "mozilla/dom/Text.h" // mTextNode
|
|
#include "nsAString.h" // nsAString parameter
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc.
|
|
#include "nsError.h" // for NS_OK, etc.
|
|
#include "nsQueryObject.h" // for do_QueryObject
|
|
|
|
namespace mozilla {
|
|
|
|
using namespace dom;
|
|
|
|
// 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,
|
|
Text& aTextNode, uint32_t aOffset)
|
|
: mTextNode(&aTextNode),
|
|
mOffset(aOffset),
|
|
mStringToInsert(aStringToInsert),
|
|
mEditorBase(&aEditorBase) {}
|
|
|
|
InsertTextTransaction::~InsertTextTransaction() {}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertTextTransaction, EditTransactionBase,
|
|
mEditorBase, mTextNode)
|
|
|
|
NS_IMPL_ADDREF_INHERITED(InsertTextTransaction, EditTransactionBase)
|
|
NS_IMPL_RELEASE_INHERITED(InsertTextTransaction, EditTransactionBase)
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTransaction)
|
|
NS_INTERFACE_MAP_ENTRY_CONCRETE(InsertTextTransaction)
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
|
NS_IMETHODIMP
|
|
InsertTextTransaction::DoTransaction() {
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) {
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
}
|
|
|
|
RefPtr<EditorBase> editorBase = mEditorBase;
|
|
RefPtr<Text> textNode = mTextNode;
|
|
|
|
ErrorResult rv;
|
|
editorBase->DoInsertText(*textNode, mOffset, mStringToInsert, rv);
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
return rv.StealNSResult();
|
|
}
|
|
|
|
// Only set selection to insertion point if editor gives permission
|
|
if (editorBase->AllowsTransactionsToChangeSelection()) {
|
|
RefPtr<Selection> selection = editorBase->GetSelection();
|
|
if (NS_WARN_IF(!selection)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
DebugOnly<nsresult> rv =
|
|
selection->Collapse(textNode, mOffset + mStringToInsert.Length());
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
"Selection could not be collapsed after insert");
|
|
} else {
|
|
// Do nothing - DOM Range gravity will adjust selection
|
|
}
|
|
// XXX Other transactions do not do this but its callers do.
|
|
// Why do this transaction do this by itself?
|
|
editorBase->RangeUpdaterRef().SelAdjInsertText(*textNode, mOffset,
|
|
mStringToInsert);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
|
NS_IMETHODIMP
|
|
InsertTextTransaction::UndoTransaction() {
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) {
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
}
|
|
RefPtr<EditorBase> editorBase = mEditorBase;
|
|
RefPtr<Text> textNode = mTextNode;
|
|
ErrorResult error;
|
|
editorBase->DoDeleteText(*textNode, mOffset, mStringToInsert.Length(), error);
|
|
return error.StealNSResult();
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
InsertTextTransaction::Merge(nsITransaction* aTransaction, bool* aDidMerge) {
|
|
if (!aTransaction || !aDidMerge) {
|
|
return NS_OK;
|
|
}
|
|
// Set out param default value
|
|
*aDidMerge = false;
|
|
|
|
// If aTransaction is a InsertTextTransaction, and if the selection hasn't
|
|
// changed, then absorb it.
|
|
RefPtr<InsertTextTransaction> otherTransaction = do_QueryObject(aTransaction);
|
|
if (otherTransaction && IsSequentialInsert(*otherTransaction)) {
|
|
nsAutoString otherData;
|
|
otherTransaction->GetData(otherData);
|
|
mStringToInsert += otherData;
|
|
*aDidMerge = true;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
/* ============ private methods ================== */
|
|
|
|
void InsertTextTransaction::GetData(nsString& aResult) {
|
|
aResult = mStringToInsert;
|
|
}
|
|
|
|
bool InsertTextTransaction::IsSequentialInsert(
|
|
InsertTextTransaction& aOtherTransaction) {
|
|
return aOtherTransaction.mTextNode == mTextNode &&
|
|
aOtherTransaction.mOffset == mOffset + mStringToInsert.Length();
|
|
}
|
|
|
|
} // namespace mozilla
|