Skip to content

Commit

Permalink
core: Support TextField.restrict when pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarosh authored and Dinnerbone committed Jan 11, 2024
1 parent 1db3499 commit 7048646
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,8 @@ impl<'gc> EditText<'gc> {
}
}
TextControlCode::Paste => {
let mut text = context.ui.clipboard_content();
let text = context.ui.clipboard_content();
let mut text = self.0.read().restrict.filter_allowed(&text);

if text.len() > self.available_chars() && self.available_chars() > 0 {
text = text[0..self.available_chars()].to_owned();
Expand Down Expand Up @@ -2532,4 +2533,14 @@ impl EditTextRestrict {
None
}
}

pub fn filter_allowed(&self, text: &str) -> String {
let mut filtered = String::with_capacity(text.len());
for c in text.chars() {
if let Some(c) = self.to_allowed(c) {
filtered.push(c);
}
}
filtered
}
}
17 changes: 17 additions & 0 deletions tests/tests/swfs/avm1/edittext_restrict_paste/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{ "type": "SetClipboardText", "text": "abc" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "SetClipboardText", "text": "aaa" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "SetClipboardText", "text": "aAbBcCdD" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "SetClipboardText", "text": "aaaBBCC" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "SetClipboardText", "text": "abcabc" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 }
]
5 changes: 5 additions & 0 deletions tests/tests/swfs/avm1/edittext_restrict_paste/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Text: 'bc'
Text: ''
Text: 'bbc'
Text: 'bbc'
Text: 'bcb'
13 changes: 13 additions & 0 deletions tests/tests/swfs/avm1/edittext_restrict_paste/test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var listener = new Object();
listener.onKeyDown = function() {
if (Key.getCode() == 27) {
trace("Text: '" + text.text + "'");
text.text = "";
}
};
Key.addListener(listener);

text.restrict = "bc";
text.maxChars = 3;

Selection.setFocus(text);
Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm1/edittext_restrict_paste/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_frames = 1

0 comments on commit 7048646

Please sign in to comment.