core: Fix non-ASCII characters input in EditText

Casting the character to u8 and back to char caused some non-ASCII
non-control characters to be treated as control characters.
For instance the letter "ą" (U+0105) after casting to u8 and back
became ENQ (U+0005) which is a control character.
Some other letters worked, for instance the letter "ł" (U+0142)
became "B" (U+0042) and was not classified as a control character.

The test edittext_input was added to verify this behavior.
This commit is contained in:
Kamil Jarosz 2024-01-05 12:37:45 +01:00 committed by Lord-McSweeney
parent 217585daa8
commit 47deaf50a2
8 changed files with 175 additions and 34 deletions

View File

@ -1475,43 +1475,38 @@ impl<'gc> EditText<'gc> {
if let Some(selection) = self.selection() { if let Some(selection) = self.selection() {
let mut changed = false; let mut changed = false;
let mut cancelled = false; let mut cancelled = false;
match character as u8 { if !character.is_control() && self.available_chars() > 0 {
code if !(code as char).is_control() => { if let Avm2Value::Object(target) = self.object2() {
if self.available_chars() > 0 { let character_string =
if let Avm2Value::Object(target) = self.object2() { AvmString::new_utf8(context.gc_context, character.to_string());
let character_string =
AvmString::new_utf8(context.gc_context, character.to_string());
let mut activation = Avm2Activation::from_nothing(context.reborrow()); let mut activation = Avm2Activation::from_nothing(context.reborrow());
let text_evt = Avm2EventObject::text_event( let text_evt = Avm2EventObject::text_event(
&mut activation, &mut activation,
"textInput", "textInput",
character_string, character_string,
true, true,
true, true,
); );
Avm2::dispatch_event(&mut activation.context, text_evt, target); Avm2::dispatch_event(&mut activation.context, text_evt, target);
cancelled = text_evt.as_event().unwrap().is_cancelled(); cancelled = text_evt.as_event().unwrap().is_cancelled();
} }
if !cancelled { if !cancelled {
self.replace_text( self.replace_text(
selection.start(), selection.start(),
selection.end(), selection.end(),
&WString::from_char(character), &WString::from_char(character),
context, context,
); );
let new_pos = selection.start() + character.len_utf8(); let new_pos = selection.start() + character.len_utf8();
self.set_selection( self.set_selection(
Some(TextSelection::for_position(new_pos)), Some(TextSelection::for_position(new_pos)),
context.gc_context, context.gc_context,
); );
changed = true; changed = true;
}
}
} }
_ => {}
} }
if changed { if changed {

View File

@ -0,0 +1,116 @@
[
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextInput", "codepoint": "b" },
{ "type": "TextInput", "codepoint": "c" },
{ "type": "TextInput", "codepoint": "A" },
{ "type": "TextInput", "codepoint": "B" },
{ "type": "TextInput", "codepoint": "C" },
{ "type": "TextInput", "codepoint": "~" },
{ "type": "TextInput", "codepoint": "!" },
{ "type": "TextInput", "codepoint": "@" },
{ "type": "TextInput", "codepoint": "#" },
{ "type": "TextInput", "codepoint": "$" },
{ "type": "TextInput", "codepoint": "%" },
{ "type": "TextInput", "codepoint": "^" },
{ "type": "TextInput", "codepoint": "&" },
{ "type": "TextInput", "codepoint": "*" },
{ "type": "TextInput", "codepoint": "(" },
{ "type": "TextInput", "codepoint": ")" },
{ "type": "TextInput", "codepoint": "`" },
{ "type": "TextInput", "codepoint": "-" },
{ "type": "TextInput", "codepoint": "=" },
{ "type": "TextInput", "codepoint": "_" },
{ "type": "TextInput", "codepoint": "+" },
{ "type": "TextInput", "codepoint": "/" },
{ "type": "TextInput", "codepoint": "?" },
{ "type": "TextInput", "codepoint": "|" },
{ "type": "TextInput", "codepoint": "ą" },
{ "type": "TextInput", "codepoint": "ę" },
{ "type": "TextInput", "codepoint": "ć" },
{ "type": "TextInput", "codepoint": "ł" },
{ "type": "TextInput", "codepoint": "þ" },
{ "type": "TextInput", "codepoint": "ó" },
{ "type": "TextInput", "codepoint": "→" },
{ "type": "TextInput", "codepoint": "↓" },
{ "type": "TextInput", "codepoint": "ß" },
{ "type": "TextInput", "codepoint": "©" },
{ "type": "TextInput", "codepoint": "ę" },
{ "type": "TextInput", "codepoint": "œ" },
{ "type": "TextInput", "codepoint": "π" },
{ "type": "TextInput", "codepoint": "ą" },
{ "type": "TextInput", "codepoint": "ś" },
{ "type": "TextInput", "codepoint": "ð" },
{ "type": "TextInput", "codepoint": "æ" },
{ "type": "TextInput", "codepoint": "ŋ" },
{ "type": "TextInput", "codepoint": "" },
{ "type": "TextInput", "codepoint": "ə" },
{ "type": "TextInput", "codepoint": "…" },
{ "type": "TextInput", "codepoint": "ł" },
{ "type": "TextInput", "codepoint": "ź" },
{ "type": "TextInput", "codepoint": "ć" },
{ "type": "TextInput", "codepoint": "ż" },
{ "type": "TextInput", "codepoint": "„" },
{ "type": "TextInput", "codepoint": "”" },
{ "type": "TextInput", "codepoint": "ń" },
{ "type": "TextInput", "codepoint": "µ" },
{ "type": "TextInput", "codepoint": "≤" },
{ "type": "TextInput", "codepoint": "≥" },
{ "type": "TextInput", "codepoint": "≠" },
{ "type": "TextInput", "codepoint": "²" },
{ "type": "TextInput", "codepoint": "³" },
{ "type": "TextInput", "codepoint": "¢" },
{ "type": "TextInput", "codepoint": "€" },
{ "type": "TextInput", "codepoint": "½" },
{ "type": "TextInput", "codepoint": "§" },
{ "type": "TextInput", "codepoint": "·" },
{ "type": "TextInput", "codepoint": "«" },
{ "type": "TextInput", "codepoint": "»" },
{ "type": "TextInput", "codepoint": "" },
{ "type": "TextInput", "codepoint": "—" },
{ "type": "TextInput", "codepoint": "°" },
{ "type": "TextInput", "codepoint": "±" },
{ "type": "TextInput", "codepoint": "¾" },
{ "type": "TextInput", "codepoint": "≈" },
{ "type": "TextInput", "codepoint": "∧" },
{ "type": "TextInput", "codepoint": "‰" },
{ "type": "TextInput", "codepoint": "¼" },
{ "type": "TextInput", "codepoint": "£" },
{ "type": "TextInput", "codepoint": "¿" },
{ "type": "TextInput", "codepoint": "¡" },
{ "type": "TextInput", "codepoint": "Ƈ" },
{ "type": "TextInput", "codepoint": "ƌ" },
{ "type": "TextInput", "codepoint": "ƣ" },
{ "type": "TextInput", "codepoint": "Ʊ" },
{ "type": "TextInput", "codepoint": "Lj" },
{ "type": "TextInput", "codepoint": "Ǎ" },
{ "type": "TextInput", "codepoint": "Ǜ" },
{ "type": "TextInput", "codepoint": "ǭ" },
{ "type": "TextInput", "codepoint": "Ǽ" },
{ "type": "TextInput", "codepoint": "ȉ" },
{ "type": "TextInput", "codepoint": "ȥ" },
{ "type": "TextInput", "codepoint": "ȹ" },
{ "type": "TextInput", "codepoint": "Ɍ" },
{ "type": "TextInput", "codepoint": "ɏ" },
{ "type": "TextInput", "codepoint": "Ḑ" },
{ "type": "TextInput", "codepoint": "ṁ" },
{ "type": "TextInput", "codepoint": "Ṥ" },
{ "type": "TextInput", "codepoint": "Ẉ" },
{ "type": "TextInput", "codepoint": "ẟ" },
{ "type": "TextInput", "codepoint": "Ắ" },
{ "type": "TextInput", "codepoint": "ố" },
{ "type": "TextInput", "codepoint": "ỳ" },
{ "type": "TextInput", "codepoint": "ỿ" },
{ "type": "TextInput", "codepoint": "Ⱨ" },
{ "type": "TextInput", "codepoint": "Ȿ" },
{ "type": "TextInput", "codepoint": "Ꞧ" },
{ "type": "TextInput", "codepoint": "Ꞁ" },
{ "type": "TextInput", "codepoint": "Ꞿ" },
{ "type": "TextInput", "codepoint": "ꟿ" },
{ "type": "TextInput", "codepoint": "ꭧ" },
{ "type": "TextInput", "codepoint": "𐞔" },
{ "type": "TextInput", "codepoint": "𐞮" },
{ "type": "TextInput", "codepoint": "𝼙" },
{ "type": "TextInput", "codepoint": "𝼞" },
{ "type": "TextInput", "codepoint": "စ" },
{ "type": "KeyDown", "key_code": 27 }
]

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
import sys
from os import path
sys.stdout.reconfigure(encoding='utf-8')
input_file = path.join(path.dirname(__file__), 'input.txt')
with open(input_file, 'r', encoding='utf-8') as file:
characters = file.read().replace(' ', '').replace('\n', '')
print('[')
for ch in characters:
print(f' {{ "type": "TextInput", "codepoint": "{ch}" }},')
print(f' {{ "type": "KeyDown", "key_code": 27 }}')
print(']')

View File

@ -0,0 +1 @@
abcABC~!@#$%^&*()`-=_+/?|ąęćłþó→↓ß©ęœπąśðæŋ’ə…łźćż„”ńµ≤≥≠²³¢€½§·«»–—°±¾≈∧‰¼£¿¡ƇƌƣƱLjǍǛǭǼȉȥȹɌɏḐṁṤẈẟẮốỳỿⱧⱾꞦꞀꞾꟿꭧ𐞔𐞮𝼙𝼞စ

View File

@ -0,0 +1 @@
abcABC~!@#$%^&*()`-=_+/?|ąęćłþó→↓ß©ęœπąśðæŋ’ə…łźćż„”ńµ≤≥≠²³¢€½§·«»–—°±¾≈∧‰¼£¿¡ƇƌƣƱLjǍǛǭǼȉȥȹɌɏḐṁṤẈẟẮốỳỿⱧⱾꞦꞀꞾꟿꭧ𐞔𐞮𝼙𝼞စ

View File

@ -0,0 +1,9 @@
var listener = new Object();
listener.onKeyDown = function() {
if (Key.getCode() == 27) {
trace(text.text);
}
};
Key.addListener(listener);
Selection.setFocus(text);

Binary file not shown.

View File

@ -0,0 +1 @@
num_frames = 1