Bug 1947107 - Rename WhiteSpaceVisibilityKeeper::ReplaceText r=m_kato

Despite the name, it does not replace text of the specified the range.

The range is not collapsed only when updating composition string and even in
the case, `EditorBase::InsertTextWithTransaction` automatically creates
a `CompositionTransaction` and the transaction will delete the existing
composition string before inserting the new string.

For making the job of the method clearer, this patch renames it as
`InsertOrUpdateCompositionString` and it's actually implemented by
`InsertTextOrInsertOrUpdateCompositionString`.

Differential Revision: https://phabricator.services.mozilla.com/D237525
This commit is contained in:
Masayuki Nakano
2025-02-13 04:29:46 +00:00
parent 8f725d7683
commit 6b49882e22
3 changed files with 60 additions and 23 deletions

View File

@@ -1234,15 +1234,14 @@ Result<EditActionResult, nsresult> HTMLEditor::HandleInsertText(
return EditActionResult::HandledResult(); return EditActionResult::HandledResult();
} }
auto compositionEndPoint = GetLastIMESelectionEndPoint<EditorDOMPoint>(); const auto compositionEndPoint =
if (!compositionEndPoint.IsSet()) { GetLastIMESelectionEndPoint<EditorDOMPoint>();
compositionEndPoint = pointToInsert;
}
Result<InsertTextResult, nsresult> replaceTextResult = Result<InsertTextResult, nsresult> replaceTextResult =
WhiteSpaceVisibilityKeeper::ReplaceText( WhiteSpaceVisibilityKeeper::InsertOrUpdateCompositionString(
*this, aInsertionString, *this, aInsertionString,
EditorDOMRange(pointToInsert, compositionEndPoint), compositionEndPoint.IsSet()
InsertTextTo::ExistingTextNodeIfAvailable); ? EditorDOMRange(pointToInsert, compositionEndPoint)
: EditorDOMRange(pointToInsert));
if (MOZ_UNLIKELY(replaceTextResult.isErr())) { if (MOZ_UNLIKELY(replaceTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::ReplaceText() failed"); NS_WARNING("WhiteSpaceVisibilityKeeper::ReplaceText() failed");
return replaceTextResult.propagateErr(); return replaceTextResult.propagateErr();

View File

@@ -1034,9 +1034,15 @@ WhiteSpaceVisibilityKeeper::InsertLineBreak(
} }
// static // static
Result<InsertTextResult, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText( Result<InsertTextResult, nsresult>
WhiteSpaceVisibilityKeeper::InsertTextOrInsertOrUpdateCompositionString(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert, HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced, InsertTextTo aInsertTextTo) { const EditorDOMRange& aRangeToBeReplaced, InsertTextTo aInsertTextTo,
TextIsCompositionString aTextIsCompositionString) {
MOZ_ASSERT(aRangeToBeReplaced.StartRef().IsInContentNode());
MOZ_ASSERT_IF(aTextIsCompositionString == TextIsCompositionString::No,
aRangeToBeReplaced.Collapsed());
// MOOSE: for now, we always assume non-PRE formatting. Fix this later. // MOOSE: for now, we always assume non-PRE formatting. Fix this later.
// meanwhile, the pre case is handled in HandleInsertText() in // meanwhile, the pre case is handled in HandleInsertText() in
// HTMLEditSubActionHandler.cpp // HTMLEditSubActionHandler.cpp

View File

@@ -240,26 +240,34 @@ class WhiteSpaceVisibilityKeeper final {
InsertText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert, InsertText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMPointType& aPointToInsert, const EditorDOMPointType& aPointToInsert,
InsertTextTo aInsertTextTo) { InsertTextTo aInsertTextTo) {
return WhiteSpaceVisibilityKeeper::ReplaceText( return WhiteSpaceVisibilityKeeper::
aHTMLEditor, aStringToInsert, EditorDOMRange(aPointToInsert), InsertTextOrInsertOrUpdateCompositionString(
aInsertTextTo); aHTMLEditor, aStringToInsert, EditorDOMRange(aPointToInsert),
aInsertTextTo, TextIsCompositionString::No);
} }
/** /**
* Replace aRangeToReplace with aStringToInsert and makes any needed * Insert aCompositionString to the start boundary of aCompositionStringRange
* adjustments to white-spaces around both start of the range and end of the * or update existing composition string with aCompositionString.
* range. * If inserting composition string, this may normalize white-spaces around
* there. However, if updating composition string, this will skip it to
* avoid CompositionTransaction work.
* *
* @param aStringToInsert The string to insert. * @param aCompositionString The new composition string.
* @param aRangeToBeReplaced The range to be replaced. * @param aCompositionStringRange
* @param aInsertTextTo Whether forcibly creates a new `Text` node in * If there is old composition string, this should
* specific condition or use existing `Text` if * cover all of it. Otherwise, this should be
* available. * collapsed and indicate the insertion point.
*/ */
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<InsertTextResult, nsresult> [[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<InsertTextResult, nsresult>
ReplaceText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert, InsertOrUpdateCompositionString(
const EditorDOMRange& aRangeToBeReplaced, HTMLEditor& aHTMLEditor, const nsAString& aCompositionString,
InsertTextTo aInsertTextTo); const EditorDOMRange& aCompositionStringRange) {
return InsertTextOrInsertOrUpdateCompositionString(
aHTMLEditor, aCompositionString, aCompositionStringRange,
HTMLEditor::InsertTextTo::ExistingTextNodeIfAvailable,
TextIsCompositionString::Yes);
}
/** /**
* Delete previous white-space of aPoint. This automatically keeps visibility * Delete previous white-space of aPoint. This automatically keeps visibility
@@ -335,6 +343,30 @@ class WhiteSpaceVisibilityKeeper final {
ReplaceTextAndRemoveEmptyTextNodes( ReplaceTextAndRemoveEmptyTextNodes(
HTMLEditor& aHTMLEditor, const EditorDOMRangeInTexts& aRangeToReplace, HTMLEditor& aHTMLEditor, const EditorDOMRangeInTexts& aRangeToReplace,
const nsAString& aReplaceString); const nsAString& aReplaceString);
enum class TextIsCompositionString : bool { No, Yes };
/**
* Insert aStringToInsert to aRangeToBeReplaced.StartRef() with normalizing
* white-spaces around there.
*
* @param aStringToInsert The string to insert.
* @param aRangeToBeReplaced If you insert non-composing text, this MUST be
* collapsed to the insertion point.
* If you update composition string, this may be
* not collapsed. The range is required to
* normalizing the new composition string.
* Therefore, this should match the range of the
* latest composition string.
* @param aInsertTextTo Whether forcibly creates a new `Text` node in
* specific condition or use existing `Text` if
* available.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<InsertTextResult, nsresult>
InsertTextOrInsertOrUpdateCompositionString(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced, InsertTextTo aInsertTextTo,
TextIsCompositionString aTextIsCompositionString);
}; };
} // namespace mozilla } // namespace mozilla