Bug 1815383 - part 2: Make InsertText and ReplaceText of WhiteSpaceVisibilityKeeper return InsertTextResult r=m_kato

Depends on D169744

Differential Revision: https://phabricator.services.mozilla.com/D169745
This commit is contained in:
Masayuki Nakano 2023-02-17 08:48:13 +00:00
parent b1b8ae7fb7
commit e75ea6a30f
3 changed files with 44 additions and 41 deletions

View File

@ -1173,7 +1173,7 @@ Result<EditActionResult, nsresult> HTMLEditor::HandleInsertText(
if (!compositionEndPoint.IsSet()) {
compositionEndPoint = compositionStartPoint;
}
Result<EditorDOMPoint, nsresult> replaceTextResult =
Result<InsertTextResult, nsresult> replaceTextResult =
WhiteSpaceVisibilityKeeper::ReplaceText(
*this, aInsertionString,
EditorDOMRange(compositionStartPoint, compositionEndPoint));
@ -1181,6 +1181,9 @@ Result<EditActionResult, nsresult> HTMLEditor::HandleInsertText(
NS_WARNING("WhiteSpaceVisibilityKeeper::ReplaceText() failed");
return replaceTextResult.propagateErr();
}
MOZ_ASSERT(
!replaceTextResult.inspect().HasCaretPointSuggestion(),
"Assuming CompositionTransaction set caret by itself, but it didn't?");
compositionStartPoint = GetFirstIMESelectionStartPoint<EditorDOMPoint>();
compositionEndPoint = GetLastIMESelectionEndPoint<EditorDOMPoint>();
@ -1327,17 +1330,26 @@ Result<EditActionResult, nsresult> HTMLEditor::HandleInsertText(
// is it a tab?
if (subStr.Equals(tabStr)) {
Result<EditorDOMPoint, nsresult> insertTextResult =
Result<InsertTextResult, nsresult> insertTextResult =
WhiteSpaceVisibilityKeeper::InsertText(*this, spacesStr,
currentPoint);
if (MOZ_UNLIKELY(insertTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::InsertText() failed");
return insertTextResult.propagateErr();
}
// Ignore the caret suggestion because of `dontChangeMySelection`
// above.
insertTextResult.inspect().IgnoreCaretPointSuggestion();
pos++;
MOZ_ASSERT(insertTextResult.inspect().IsSet());
currentPoint = insertTextResult.inspect();
pointToInsert = insertTextResult.unwrap();
if (insertTextResult.inspect().Handled()) {
pointToInsert = currentPoint = insertTextResult.unwrap()
.EndOfInsertedTextRef()
.To<EditorDOMPoint>();
MOZ_ASSERT(pointToInsert.IsSet());
} else {
pointToInsert = currentPoint;
MOZ_ASSERT(pointToInsert.IsSet());
}
}
// is it a return?
else if (subStr.Equals(newlineStr)) {
@ -1384,16 +1396,25 @@ Result<EditActionResult, nsresult> HTMLEditor::HandleInsertText(
currentPoint == pointToInsert,
"Perhaps, newBRElement has been moved or removed unexpectedly");
} else {
Result<EditorDOMPoint, nsresult> insertTextResult =
Result<InsertTextResult, nsresult> insertTextResult =
WhiteSpaceVisibilityKeeper::InsertText(*this, subStr,
currentPoint);
if (MOZ_UNLIKELY(insertTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::InsertText() failed");
return insertTextResult.propagateErr();
}
MOZ_ASSERT(insertTextResult.inspect().IsSet());
currentPoint = insertTextResult.inspect();
pointToInsert = insertTextResult.unwrap();
// Ignore the caret suggestion because of `dontChangeMySelection`
// above.
insertTextResult.inspect().IgnoreCaretPointSuggestion();
if (insertTextResult.inspect().Handled()) {
pointToInsert = currentPoint = insertTextResult.unwrap()
.EndOfInsertedTextRef()
.To<EditorDOMPoint>();
MOZ_ASSERT(pointToInsert.IsSet());
} else {
pointToInsert = currentPoint;
MOZ_ASSERT(pointToInsert.IsSet());
}
}
}
}

View File

@ -965,7 +965,7 @@ WhiteSpaceVisibilityKeeper::InsertBRElement(
}
// static
Result<EditorDOMPoint, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText(
Result<InsertTextResult, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced) {
// MOOSE: for now, we always assume non-PRE formatting. Fix this later.
@ -978,7 +978,7 @@ Result<EditorDOMPoint, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText(
if (aStringToInsert.IsEmpty()) {
MOZ_ASSERT(aRangeToBeReplaced.Collapsed());
return EditorDOMPoint(aRangeToBeReplaced.StartRef());
return InsertTextResult();
}
RefPtr<Element> editingHost = aHTMLEditor.ComputeEditingHost();
@ -1318,20 +1318,10 @@ Result<EditorDOMPoint, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText(
Result<InsertTextResult, nsresult> insertTextResult =
aHTMLEditor.InsertTextWithTransaction(document, theString, pointToInsert);
if (MOZ_UNLIKELY(insertTextResult.isErr())) {
if (MOZ_UNLIKELY(insertTextResult.inspectErr() ==
NS_ERROR_EDITOR_DESTROYED)) {
NS_WARNING("HTMLEditor::InsertTextWithTransaction() failed");
return Err(NS_ERROR_EDITOR_DESTROYED);
}
NS_WARNING("HTMLEditor::InsertTextWithTransaction() failed, but ignored");
// XXX Temporarily, set new insertion point to the original point.
return pointToInsert;
NS_WARNING("HTMLEditor::InsertTextWithTransaction() failed");
return insertTextResult.propagateErr();
}
insertTextResult.inspect().IgnoreCaretPointSuggestion();
return insertTextResult.inspect().Handled() ? insertTextResult.inspect()
.EndOfInsertedTextRef()
.To<EditorDOMPoint>()
: pointToInsert;
return insertTextResult;
}
// static

View File

@ -1478,18 +1478,14 @@ class WhiteSpaceVisibilityKeeper final {
const Element& aEditingHost);
/**
* InsertText() inserts aStringToInsert to aPointToInsert and makes any needed
* adjustments to white-spaces around the insertion point.
* Insert aStringToInsert to aPointToInsert and makes any needed adjustments
* to white-spaces around the insertion point.
*
* @param aStringToInsert The string to insert.
* @param aRangeToBeReplaced The range to be deleted.
* @return If succeeded, returns the point after inserted
* aStringToInsert. So, when this method actually
* inserts string, returns a point in the text
* node. Otherwise, may return mScanStartPoint.
* @param aRangeToBeReplaced The range to be replaced.
*/
template <typename EditorDOMPointType>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<EditorDOMPoint, nsresult>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<InsertTextResult, nsresult>
InsertText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMPointType& aPointToInsert) {
return WhiteSpaceVisibilityKeeper::ReplaceText(
@ -1497,18 +1493,14 @@ class WhiteSpaceVisibilityKeeper final {
}
/**
* ReplaceText() repaces aRangeToReplace with aStringToInsert and makes any
* needed adjustments to white-spaces around both start of the range and
* end of the range.
* Replace aRangeToReplace with aStringToInsert and makes any needed
* adjustments to white-spaces around both start of the range and end of the
* range.
*
* @param aStringToInsert The string to insert.
* @param aRangeToBeReplaced The range to be deleted.
* @return If succeeded, returns the point after inserted
* aStringToInsert. So, when this method actually
* inserts string, returns a point in the text
* node. Otherwise, may return mScanStartPoint.
* @param aRangeToBeReplaced The range to be replaced.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<EditorDOMPoint, nsresult>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<InsertTextResult, nsresult>
ReplaceText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced);