Windows text input fixes (flutter/engine#17768)

Fixes a few issues with Windows text input:
- Filters out ASCII control characters
- Filters out lead surrogates, which aren't valid UTF-16 on their own so will cause assertion failures if sent to Flutter
- Adds a bandaid fix for a crash due to mismatches in indexing in the C++ and Dart text models. (A better fix would be to use UTF-16 and add surrogate pair handling to deletion and forward/back; that will be a later PR since it has a larger scope.)

Fixes https://github.com/flutter/flutter/issues/54879
This commit is contained in:
stuartmorgan
2020-04-20 14:05:41 -07:00
committed by GitHub
parent eb29da8831
commit f166d4c159
2 changed files with 24 additions and 18 deletions

View File

@@ -4,6 +4,7 @@
#include "flutter/shell/platform/common/cpp/text_input_model.h"
#include <algorithm>
#include <codecvt>
#include <iostream>
#include <locale>
@@ -73,8 +74,13 @@ bool TextInputModel::SetEditingState(size_t selection_base,
}
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
text_ = utf32conv.from_bytes(text);
selection_base_ = text_.begin() + selection_base;
selection_extent_ = text_.begin() + selection_extent;
// selection_base and selection_extent are relative to UTF-16, so don't
// necessarily line up. The full fix would be to use UTF-16 here so
// the models align, but for now just make sure it doesn't crash;
// longer term the goal is to eliminate having two sources of truth.
size_t length = text_.length();
selection_base_ = text_.begin() + std::min(selection_base, length);
selection_extent_ = text_.begin() + std::min(selection_extent, length);
return true;
}

View File

@@ -196,29 +196,29 @@ Win32Window::MessageHandler(HWND hwnd,
case WM_CHAR:
case WM_SYSCHAR: {
char32_t code_point = static_cast<char32_t>(wparam);
static char32_t lead_surrogate = 0;
static char32_t s_pending_lead_surrogate = 0;
// If code_point is LeadSurrogate, save to combine to potentially form
// a complex Unicode character.
if ((code_point & 0xFFFFFC00) == 0xD800) {
lead_surrogate = code_point;
} else if (lead_surrogate != 0 && (code_point & 0xFFFFFC00) == 0xDC00) {
s_pending_lead_surrogate = code_point;
} else if (s_pending_lead_surrogate != 0 &&
(code_point & 0xFFFFFC00) == 0xDC00) {
// Merge TrailSurrogate and LeadSurrogate.
code_point = 0x10000 + ((lead_surrogate & 0x000003FF) << 10) +
code_point = 0x10000 +
((s_pending_lead_surrogate & 0x000003FF) << 10) +
(code_point & 0x3FF);
lead_surrogate = 0;
s_pending_lead_surrogate = 0;
}
// In an ENG-INTL keyboard, pressing "'" + "e" produces é. In this case,
// the "'" key is a dead char, and shouldn't be sent to window->OnChar
// for text input. However, the key event should still be sent to
// Flutter. The result would be:
// * Key event - key code: 222 (quote) - key label: '
// * Key event - key code: 69 (e) - key label: é
//
// As for text input, only the second key press will display a
// character.
if (wparam != VK_BACK && message != WM_DEADCHAR &&
message != WM_SYSDEADCHAR) {
// Of the messages handled here, only WM_CHAR should be treated as
// characters. WM_SYS*CHAR are not part of text input, and WM_DEADCHAR
// will be incorporated into a later WM_CHAR with the full character.
// Also filter out:
// - Lead surrogates, which like dead keys will be send once combined.
// - ASCII control characters, which are sent as WM_CHAR events for all
// control key shortcuts.
if (message == WM_CHAR && code_point >= U' ' &&
s_pending_lead_surrogate == 0) {
window->OnChar(code_point);
}