Clip UTF-16 offsets in text for range (#20968)
When launching the Pinyin keyboard, macOS will sometimes try to peek one character back in the string. This caused a panic if the preceding character was an emoji. The docs say "don't assume the range is valid", so now we don't. Release Notes: - (macOS) Fixed a panic when using the Pinyin keyboard with emojis
This commit is contained in:
parent
7285cdb955
commit
ebaa270baf
6 changed files with 70 additions and 16 deletions
|
@ -38,6 +38,7 @@ use std::{
|
|||
cell::Cell,
|
||||
ffi::{c_void, CStr},
|
||||
mem,
|
||||
ops::Range,
|
||||
path::PathBuf,
|
||||
ptr::{self, NonNull},
|
||||
rc::Rc,
|
||||
|
@ -1754,15 +1755,21 @@ extern "C" fn attributed_substring_for_proposed_range(
|
|||
this: &Object,
|
||||
_: Sel,
|
||||
range: NSRange,
|
||||
_actual_range: *mut c_void,
|
||||
actual_range: *mut c_void,
|
||||
) -> id {
|
||||
with_input_handler(this, |input_handler| {
|
||||
let range = range.to_range()?;
|
||||
if range.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut adjusted: Option<Range<usize>> = None;
|
||||
|
||||
let selected_text = input_handler.text_for_range(range.clone())?;
|
||||
let selected_text = input_handler.text_for_range(range.clone(), &mut adjusted)?;
|
||||
if let Some(adjusted) = adjusted {
|
||||
if adjusted != range {
|
||||
unsafe { (actual_range as *mut NSRange).write(NSRange::from(adjusted)) };
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
let string: id = msg_send![class!(NSAttributedString), alloc];
|
||||
let string: id = msg_send![string, initWithString: ns_string(&selected_text)];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue