Bug 1765018 - part 5: Make `InsertText()` and `ReplaceText()` of `WhiteSpaceVisibilityKeeper` return `Result<EditorDOMPoint, nsresult>` r=m_kato

Aligning to `EditorBase::InsertTextWithTransaction`, these methods should return
`Result<EditorDOMPoint, nsresult>` rather than taking an out param.

Differential Revision: https://phabricator.services.mozilla.com/D143882
This commit is contained in:
Masayuki Nakano 2022-04-20 23:52:09 +00:00
Родитель 9968200d7c
Коммит 9cd4ac5480
3 изменённых файлов: 58 добавлений и 80 удалений

Просмотреть файл

@ -1155,12 +1155,13 @@ EditActionResult HTMLEditor::HandleInsertText(
if (!compositionEndPoint.IsSet()) {
compositionEndPoint = compositionStartPoint;
}
nsresult rv = WhiteSpaceVisibilityKeeper::ReplaceText(
*this, aInsertionString,
EditorDOMRange(compositionStartPoint, compositionEndPoint));
if (NS_FAILED(rv)) {
Result<EditorDOMPoint, nsresult> replaceTextResult =
WhiteSpaceVisibilityKeeper::ReplaceText(
*this, aInsertionString,
EditorDOMRange(compositionStartPoint, compositionEndPoint));
if (MOZ_UNLIKELY(replaceTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::ReplaceText() failed");
return EditActionHandled(rv);
return EditActionHandled(replaceTextResult.unwrapErr());
}
compositionStartPoint = GetFirstIMESelectionStartPoint<EditorDOMPoint>();
@ -1170,7 +1171,7 @@ EditActionResult HTMLEditor::HandleInsertText(
// Mutation event listener has changed the DOM tree...
return EditActionHandled();
}
rv = TopLevelEditSubActionDataRef().mChangedRange->SetStartAndEnd(
nsresult rv = TopLevelEditSubActionDataRef().mChangedRange->SetStartAndEnd(
compositionStartPoint.ToRawRangeBoundary(),
compositionEndPoint.ToRawRangeBoundary());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "nsRange::SetStartAndEnd() failed");
@ -1292,17 +1293,17 @@ EditActionResult HTMLEditor::HandleInsertText(
// is it a tab?
if (subStr.Equals(tabStr)) {
EditorRawDOMPoint pointAfterInsertedSpaces;
nsresult rv = WhiteSpaceVisibilityKeeper::InsertText(
*this, spacesStr, currentPoint, &pointAfterInsertedSpaces);
if (NS_FAILED(rv)) {
Result<EditorDOMPoint, nsresult> insertTextResult =
WhiteSpaceVisibilityKeeper::InsertText(*this, spacesStr,
currentPoint);
if (MOZ_UNLIKELY(insertTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::InsertText() failed");
return EditActionHandled(rv);
return EditActionHandled(insertTextResult.unwrapErr());
}
pos++;
MOZ_ASSERT(pointAfterInsertedSpaces.IsSet());
currentPoint = pointAfterInsertedSpaces.To<EditorDOMPoint>();
pointToInsert = pointAfterInsertedSpaces.To<EditorDOMPoint>();
MOZ_ASSERT(insertTextResult.inspect().IsSet());
currentPoint = insertTextResult.inspect();
pointToInsert = insertTextResult.unwrap();
}
// is it a return?
else if (subStr.Equals(newlineStr)) {
@ -1331,16 +1332,16 @@ EditActionResult HTMLEditor::HandleInsertText(
currentPoint == pointToInsert,
"Perhaps, newBRElement has been moved or removed unexpectedly");
} else {
EditorRawDOMPoint pointAfterInsertedString;
nsresult rv = WhiteSpaceVisibilityKeeper::InsertText(
*this, subStr, currentPoint, &pointAfterInsertedString);
if (NS_FAILED(rv)) {
Result<EditorDOMPoint, nsresult> insertTextResult =
WhiteSpaceVisibilityKeeper::InsertText(*this, subStr,
currentPoint);
if (MOZ_UNLIKELY(insertTextResult.isErr())) {
NS_WARNING("WhiteSpaceVisibilityKeeper::InsertText() failed");
return EditActionHandled(rv);
return EditActionHandled(insertTextResult.unwrapErr());
}
MOZ_ASSERT(pointAfterInsertedString.IsSet());
currentPoint = pointAfterInsertedString.To<EditorDOMPoint>();
pointToInsert = pointAfterInsertedString.To<EditorDOMPoint>();
MOZ_ASSERT(insertTextResult.inspect().IsSet());
currentPoint = insertTextResult.inspect();
pointToInsert = insertTextResult.unwrap();
}
}
}

Просмотреть файл

@ -854,10 +854,9 @@ Result<RefPtr<Element>, nsresult> WhiteSpaceVisibilityKeeper::InsertBRElement(
}
// static
nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
Result<EditorDOMPoint, nsresult> WhiteSpaceVisibilityKeeper::ReplaceText(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced,
EditorRawDOMPoint* aPointAfterInsertedString /* = nullptr */) {
const EditorDOMRange& aRangeToBeReplaced) {
// MOOSE: for now, we always assume non-PRE formatting. Fix this later.
// meanwhile, the pre case is handled in HandleInsertText() in
// HTMLEditSubActionHandler.cpp
@ -868,18 +867,14 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
if (aStringToInsert.IsEmpty()) {
MOZ_ASSERT(aRangeToBeReplaced.Collapsed());
if (aPointAfterInsertedString) {
*aPointAfterInsertedString =
aRangeToBeReplaced.StartRef().To<EditorRawDOMPoint>();
}
return NS_OK;
return EditorDOMPoint(aRangeToBeReplaced.StartRef());
}
RefPtr<Element> editingHost = aHTMLEditor.GetActiveEditingHost();
TextFragmentData textFragmentDataAtStart(aRangeToBeReplaced.StartRef(),
editingHost);
if (NS_WARN_IF(!textFragmentDataAtStart.IsInitialized())) {
return NS_ERROR_FAILURE;
if (MOZ_UNLIKELY(NS_WARN_IF(!textFragmentDataAtStart.IsInitialized()))) {
return Err(NS_ERROR_FAILURE);
}
const bool isInsertionPointEqualsOrIsBeforeStartOfText =
aRangeToBeReplaced.StartRef().EqualsOrIsBefore(
@ -888,8 +883,8 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
aRangeToBeReplaced.Collapsed()
? textFragmentDataAtStart
: TextFragmentData(aRangeToBeReplaced.EndRef(), editingHost);
if (NS_WARN_IF(!textFragmentDataAtEnd.IsInitialized())) {
return NS_ERROR_FAILURE;
if (MOZ_UNLIKELY(NS_WARN_IF(!textFragmentDataAtEnd.IsInitialized()))) {
return Err(NS_ERROR_FAILURE);
}
const bool isInsertionPointEqualsOrAfterEndOfText =
textFragmentDataAtEnd.EndRef().EqualsOrIsBefore(
@ -961,10 +956,10 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
invisibleTrailingWhiteSpaceRangeAtEnd.StartRef(),
invisibleTrailingWhiteSpaceRangeAtEnd.EndRef(),
HTMLEditor::TreatEmptyTextNodes::KeepIfContainerOfRangeBoundaries);
if (NS_FAILED(rv)) {
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
NS_WARNING(
"HTMLEditor::DeleteTextAndTextNodesWithTransaction() failed");
return rv;
return Err(rv);
}
}
}
@ -997,9 +992,9 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
nsresult rv = aHTMLEditor.ReplaceTextWithTransaction(
MOZ_KnownLive(*atNBSPReplacedWithASCIIWhiteSpace.ContainerAsText()),
atNBSPReplacedWithASCIIWhiteSpace.Offset(), 1, u" "_ns);
if (NS_FAILED(rv)) {
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
NS_WARNING("HTMLEditor::ReplaceTextWithTransaction() failed");
return rv;
return Err(rv);
}
}
}
@ -1018,10 +1013,10 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
invisibleLeadingWhiteSpaceRangeAtStart.StartRef(),
invisibleLeadingWhiteSpaceRangeAtStart.EndRef(),
HTMLEditor::TreatEmptyTextNodes::KeepIfContainerOfRangeBoundaries);
if (NS_FAILED(rv)) {
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
NS_WARNING(
"HTMLEditor::DeleteTextAndTextNodesWithTransaction() failed");
return rv;
return Err(rv);
}
// Don't refer the following variables anymore unless tracking the
// change.
@ -1049,9 +1044,9 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
nsresult rv = aHTMLEditor.ReplaceTextWithTransaction(
MOZ_KnownLive(*atNBSPReplacedWithASCIIWhiteSpace.ContainerAsText()),
atNBSPReplacedWithASCIIWhiteSpace.Offset(), 1, u" "_ns);
if (NS_FAILED(rv)) {
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
NS_WARNING("HTMLEditor::ReplaceTextWithTransaction() failed failed");
return rv;
return Err(rv);
}
// Don't refer the following variables anymore unless tracking the
// change.
@ -1197,10 +1192,10 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
// runtime cost. So, perhaps, we should return error code which couldn't
// modify it and make each caller of this method decide whether it should
// keep or stop handling the edit action.
if (!aHTMLEditor.GetDocument()) {
if (MOZ_UNLIKELY(!aHTMLEditor.GetDocument())) {
NS_WARNING(
"WhiteSpaceVisibilityKeeper::ReplaceText() lost proper document");
return NS_ERROR_UNEXPECTED;
return Err(NS_ERROR_UNEXPECTED);
}
OwningNonNull<Document> document = *aHTMLEditor.GetDocument();
Result<EditorDOMPoint, nsresult> insertTextResult =
@ -1209,23 +1204,16 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
NS_ERROR_EDITOR_DESTROYED)) {
NS_WARNING(
"HTMLEditor::InsertTextWithTransaction() caused destroying the editor");
return NS_ERROR_EDITOR_DESTROYED;
return Err(NS_ERROR_EDITOR_DESTROYED);
}
if (insertTextResult.isOk()) {
if (aPointAfterInsertedString) {
*aPointAfterInsertedString =
insertTextResult.unwrap().To<EditorRawDOMPoint>();
}
return NS_OK;
return insertTextResult.unwrap();
}
NS_WARNING("HTMLEditor::InsertTextWithTransaction() failed, but ignored");
// XXX Temporarily, set new insertion point to the original point.
if (aPointAfterInsertedString) {
*aPointAfterInsertedString = pointToInsert.To<EditorRawDOMPoint>();
}
return NS_OK;
return pointToInsert;
}
// static

Просмотреть файл

@ -1468,23 +1468,17 @@ class WhiteSpaceVisibilityKeeper final {
*
* @param aStringToInsert The string to insert.
* @param aRangeToBeReplaced The range to be deleted.
* @param aPointAfterInsertedString
* The point after inserted aStringToInsert.
* So, when this method actually inserts string,
* this is set to a point in the text node.
* Otherwise, this may be set to mScanStartPoint.
* @return When this succeeds to insert the string or
* does nothing during composition, returns NS_OK.
* Otherwise, an error code.
* @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.
*/
template <typename EditorDOMPointType>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static nsresult InsertText(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMPointType& aPointToInsert,
EditorRawDOMPoint* aPointAfterInsertedString = nullptr) {
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<EditorDOMPoint, nsresult>
InsertText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMPointType& aPointToInsert) {
return WhiteSpaceVisibilityKeeper::ReplaceText(
aHTMLEditor, aStringToInsert, EditorDOMRange(aPointToInsert),
aPointAfterInsertedString);
aHTMLEditor, aStringToInsert, EditorDOMRange(aPointToInsert));
}
/**
@ -1494,19 +1488,14 @@ class WhiteSpaceVisibilityKeeper final {
*
* @param aStringToInsert The string to insert.
* @param aRangeToBeReplaced The range to be deleted.
* @param aPointAfterInsertedString
* The point after inserted aStringToInsert.
* So, when this method actually inserts string,
* this is set to a point in the text node.
* Otherwise, this may be set to mScanStartPoint.
* @return When this succeeds to insert the string or
* does nothing during composition, returns NS_OK.
* Otherwise, an error code.
* @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.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static nsresult ReplaceText(
HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced,
EditorRawDOMPoint* aPointAfterInsertedString = nullptr);
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static Result<EditorDOMPoint, nsresult>
ReplaceText(HTMLEditor& aHTMLEditor, const nsAString& aStringToInsert,
const EditorDOMRange& aRangeToBeReplaced);
/**
* DeletePreviousWhiteSpace() deletes previous white-space of aPoint.