Skip to content

Commit

Permalink
[Unified Text Replacement] When receiving replacements, the document …
Browse files Browse the repository at this point in the history
…markers may be added to incorrect ranges

https://bugs.webkit.org/show_bug.cgi?id=274435
rdar://128083075

Reviewed by Aditya Keerthi.

When receiving replacements, after replacing the text and before adding the document marker, the
session range must be re-computed so that it is up-to-date so that the marker range is correct.

* Source/WebKit/WebProcess/WebPage/Cocoa/UnifiedTextReplacementController.mm:
(WebKit::UnifiedTextReplacementController::textReplacementSessionDidReceiveReplacements):

Canonical link: https://commits.webkit.org/279098@main
  • Loading branch information
rr-codes committed May 22, 2024
1 parent 24db6e6 commit 8b67f03
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@

document->selection().clear();

auto sessionRange = contextRangeForSessionWithUUID(session.uuid);
if (!sessionRange) {
ASSERT_NOT_REACHED();
return;
}

// The tracking of the additional replacement location offset needs to be scoped to a particular instance
// of this class, instead of just this function, because the function may need to be called multiple times.
// This ensures that subsequent calls of this function should effectively be treated as just more iterations
Expand All @@ -156,18 +162,18 @@
auto& additionalOffset = m_replacementLocationOffsets.add(session.uuid, 0).iterator->value;

for (const auto& replacementData : replacements) {
auto sessionRange = contextRangeForSessionWithUUID(session.uuid);
if (!sessionRange) {
ASSERT_NOT_REACHED();
return;
}

auto locationWithOffset = replacementData.originalRange.location + additionalOffset;

auto resolvedRange = UnifiedTextReplacementController::resolveCharacterRange(*sessionRange, { locationWithOffset, replacementData.originalRange.length });

replaceContentsOfRangeInSession(session.uuid, resolvedRange, replacementData.replacement);

sessionRange = contextRangeForSessionWithUUID(session.uuid);
if (!sessionRange) {
ASSERT_NOT_REACHED();
return;
}

auto newRangeWithOffset = WebCore::CharacterRange { locationWithOffset, replacementData.replacement.length() };
auto newResolvedRange = UnifiedTextReplacementController::resolveCharacterRange(*sessionRange, newRangeWithOffset);

Expand All @@ -179,15 +185,8 @@
additionalOffset += static_cast<int>(replacementData.replacement.length()) - static_cast<int>(replacementData.originalRange.length);
}

if (finished) {
auto sessionRange = contextRangeForSessionWithUUID(session.uuid);
if (!sessionRange) {
ASSERT_NOT_REACHED();
return;
}

if (finished)
document->selection().setSelection({ *sessionRange });
}
}

void UnifiedTextReplacementController::textReplacementSessionDidUpdateStateForReplacement(const WebUnifiedTextReplacementSessionData& session, WebTextReplacementData::State state, const WebTextReplacementData& replacement, const WebUnifiedTextReplacementContextData& context)
Expand Down

0 comments on commit 8b67f03

Please sign in to comment.