Skip to content

Commit

Permalink
[Unified Text Replacement] Add support for ephemeral sessions
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271433
rdar://125038208

Reviewed by Abrar Rahman Protyasha.

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

An ephemeral session is identified by an invalid UUID, no state is persisted.

(WebKit::UnifiedTextReplacementController::textReplacementSessionPerformEditActionForPlainText):

Drive-by address post-landing comments on 276490@main.

Canonical link: https://commits.webkit.org/276521@main
  • Loading branch information
pxlcoder committed Mar 22, 2024
1 parent c614496 commit 1b69ad8
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,28 @@ static void replaceContentsInRange(WebCore::Document& document, const WebCore::S
return;
}

RefPtr frame = corePage->checkedFocusController()->focusedOrMainFrame();
if (!frame) {
ASSERT_NOT_REACHED();
completionHandler({ });
return;
}

auto contextRange = m_webPage->autocorrectionContextRange();
if (!contextRange) {
RELEASE_LOG(UnifiedTextReplacement, "UnifiedTextReplacementController::willBeginTextReplacementSession (%s) => no context range", uuid.toString().utf8().data());
completionHandler({ });
return;
}

auto liveRange = createLiveRange(*contextRange);
// If the UUID is invalid, the session is ephemeral.
if (uuid.isValid()) {
auto liveRange = createLiveRange(*contextRange);

ASSERT(!m_contextRanges.contains(uuid));
m_contextRanges.set(uuid, liveRange);
ASSERT(!m_contextRanges.contains(uuid));
m_contextRanges.set(uuid, liveRange);

m_replacementTypes.set(uuid, type);

RefPtr frame = corePage->checkedFocusController()->focusedOrMainFrame();
if (!frame) {
ASSERT_NOT_REACHED();
completionHandler({ });
return;
m_replacementTypes.set(uuid, type);
}

auto selectedTextRange = frame->selection().selection().firstRange();
Expand Down Expand Up @@ -502,7 +505,7 @@ static void replaceContentsInRange(WebCore::Document& document, const WebCore::S

document.markers().removeMarkers(node, offsetRange, { WebCore::DocumentMarker::Type::UnifiedTextReplacement });

auto newState = [&]() -> WebCore::DocumentMarker::UnifiedTextReplacementData::State {
auto newState = [&] {
switch (action) {
case WebTextReplacementData::EditAction::Undo:
return WebCore::DocumentMarker::UnifiedTextReplacementData::State::Reverted;
Expand All @@ -519,7 +522,7 @@ static void replaceContentsInRange(WebCore::Document& document, const WebCore::S
replaceTextInRange(document, rangeToReplace, previousText);

auto newData = WebCore::DocumentMarker::UnifiedTextReplacementData { currentText, oldData.uuid, newState };
auto newOffsetRange = WebCore::OffsetRange { offsetRange.start, offsetRange.end - currentText.length() + previousText.length() };
auto newOffsetRange = WebCore::OffsetRange { offsetRange.start, offsetRange.end + previousText.length() - currentText.length() };

document.markers().addMarker(node, WebCore::DocumentMarker { WebCore::DocumentMarker::Type::UnifiedTextReplacement, newOffsetRange, WTFMove(newData) });
}
Expand Down

0 comments on commit 1b69ad8

Please sign in to comment.