avm1: Implement TextField.restrict

This commit is contained in:
Kamil Jarosz 2024-01-11 13:37:36 +01:00 committed by Nathan Adams
parent 43d4c26b77
commit 1c15cd55b9
6 changed files with 1433 additions and 0 deletions

View File

@ -78,6 +78,7 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"maxChars" => property(tf_getter!(max_chars), tf_setter!(set_max_chars)); "maxChars" => property(tf_getter!(max_chars), tf_setter!(set_max_chars));
"multiline" => property(tf_getter!(multiline), tf_setter!(set_multiline)); "multiline" => property(tf_getter!(multiline), tf_setter!(set_multiline));
"password" => property(tf_getter!(password), tf_setter!(set_password)); "password" => property(tf_getter!(password), tf_setter!(set_password));
"restrict" => property(tf_getter!(restrict), tf_setter!(set_restrict));
"scroll" => property(tf_getter!(scroll), tf_setter!(set_scroll)); "scroll" => property(tf_getter!(scroll), tf_setter!(set_scroll));
"selectable" => property(tf_getter!(selectable), tf_setter!(set_selectable)); "selectable" => property(tf_getter!(selectable), tf_setter!(set_selectable));
"text" => property(tf_getter!(text), tf_setter!(set_text)); "text" => property(tf_getter!(text), tf_setter!(set_text));
@ -859,3 +860,36 @@ fn set_filters<'gc>(
this.set_filters(activation.context.gc_context, filters); this.set_filters(activation.context.gc_context, filters);
Ok(()) Ok(())
} }
fn restrict<'gc>(
this: EditText<'gc>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
match this.restrict() {
Some(value) => Ok(AvmString::new(activation.context.gc_context, value).into()),
None => Ok(Value::Null),
}
}
fn set_restrict<'gc>(
this: EditText<'gc>,
activation: &mut Activation<'_, 'gc>,
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
match value {
Value::Undefined | Value::Null => {
this.set_restrict(None, &mut activation.context);
}
_ => {
let text = value.coerce_to_string(activation)?;
if text.is_empty() {
// According to docs, an empty string means that you cannot enter any character,
// but according to reality, an empty string is equivalent to null in AVM1.
this.set_restrict(None, &mut activation.context);
} else {
this.set_restrict(Some(&text), &mut activation.context);
}
}
};
Ok(())
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,191 @@
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: 'undefined'
Restrict get: 'null'
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: 'null'
Restrict get: 'null'
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: ''
Restrict get: 'null'
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: 'false'
Restrict get: 'false'
Text: 'aa'
====================
Restrict set: '1'
Restrict get: '1'
Text: '1'
====================
Restrict set: 'true'
Restrict get: 'true'
Text: ''
====================
Restrict set: '0.1'
Restrict get: '0.1'
Text: '01'
====================
Restrict set: 'NaN'
Restrict get: 'NaN'
Text: 'aa'
====================
Restrict set: '[object Object]'
Restrict get: '[object Object]'
Text: 'bcbc '
====================
Restrict set: 'aB*Δ'
Restrict get: 'aB*Δ'
Text: 'aBaB*Δ'
====================
Restrict set: 'aa'
Restrict get: 'aa'
Text: 'aa'
====================
Restrict set: 'a-z'
Restrict get: 'a-z'
Text: 'abcabc'
====================
Restrict set: 'A-Z'
Restrict get: 'A-Z'
Text: 'ABCABC'
====================
Restrict set: 'a-bA'
Restrict get: 'a-bA'
Text: 'abAb'
====================
Restrict set: 'a-'
Restrict get: 'a-'
Text: 'aa'
====================
Restrict set: '-b'
Restrict get: '-b'
Text: 'abCABC012^\-* &'
====================
Restrict set: 'b-a'
Restrict get: 'b-a'
Text: 'bb'
====================
Restrict set: 'A-z'
Restrict get: 'A-z'
Text: 'abcABC^\'
====================
Restrict set: '-'
Restrict get: '-'
Text: ''
====================
Restrict set: '--'
Restrict get: '--'
Text: ''
====================
Restrict set: '---'
Restrict get: '---'
Text: ''
====================
Restrict set: '----'
Restrict get: '----'
Text: ''
====================
Restrict set: '-----'
Restrict get: '-----'
Text: ''
====================
Restrict set: '-----b'
Restrict get: '-----b'
Text: 'abCABC012^\-* &'
====================
Restrict set: 'b-----'
Restrict get: 'b-----'
Text: 'bb'
====================
Restrict set: 'a-b-c'
Restrict get: 'a-b-c'
Text: 'abcABC012^\-* &'
====================
Restrict set: 'a-b-A'
Restrict get: 'a-b-A'
Text: 'abAb012-* &'
====================
Restrict set: 'a-a-b'
Restrict get: 'a-a-b'
Text: 'abCABC012^\-* &'
====================
Restrict set: '\\-\^'
Restrict get: '\\-\^'
Text: '^\'
====================
Restrict set: '\^-\\'
Restrict get: '\^-\\'
Text: '^'
====================
Restrict set: '^'
Restrict get: '^'
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: '^^'
Restrict get: '^^'
Text: 'abcABC012^\-* &ąδłĄΔŁß'
====================
Restrict set: '\^a'
Restrict get: '\^a'
Text: 'aa^'
====================
Restrict set: '^\^'
Restrict get: '^\^'
Text: 'abcABC012\-* &ąδłĄΔŁß'
====================
Restrict set: '^aą'
Restrict get: '^aą'
Text: 'AbcABC012^\-* &δłĄΔŁß'
====================
Restrict set: 'a^b^c'
Restrict get: 'a^b^c'
Text: 'acac'
====================
Restrict set: 'a^b^c^A^B'
Restrict get: 'a^b^c^A^B'
Text: 'aBcaBc'
====================
Restrict set: 'a-zA-Z^bC'
Restrict get: 'a-zA-Z^bC'
Text: 'aBcABc'
====================
Restrict set: 'a-zA-Z^'
Restrict get: 'a-zA-Z^'
Text: 'abcABC'
====================
Restrict set: '\-'
Restrict get: '\-'
Text: '-'
====================
Restrict set: 'a\-z'
Restrict get: 'a\-z'
Text: 'aa-'
====================
Restrict set: '\\'
Restrict get: '\\'
Text: '\'
====================
Restrict set: '\^'
Restrict get: '\^'
Text: '^'
====================
Restrict set: '\ab'
Restrict get: '\ab'
Text: 'abab'
====================
Restrict set: 'a\'
Restrict get: 'a\'
Text: 'aa'
====================
Restrict set: ' -~'
Restrict get: ' -~'
Text: 'abcABC012^\-* &'
====================
Restrict set: 'α-ω'
Restrict get: 'α-ω'
Text: 'δ'
====================
No more restricts

View File

@ -0,0 +1,101 @@
// https://open-flash.github.io/mirrors/as2-language-reference/TextField.html#restrict
// There is one TextField named "text", which receives the text input.
// We are changing its "restrict" property with various values (variable "restricts").
// When we receive "KeyDown" with the given code, we print
// the actual text and switch the restrict to the next one.
// For each restrict we are testing the following values:
// a,b,c lowercase ASCII letters
// A,B,C uppercase ASCII letters
// 0,1,2 digits
// ^,\,- special characters with a meaning
// *, ,& other special characters
// ą,δ,ł lowercase non-ASCII letters
// Ą,Δ,Ł uppercase non-ASCII letters
// ß a German letter with a controversial uppercase form
// When checking the test manually, one can copy the text, and keep pressing
// Ctrl-V, ESC, Ctrl-V, ESC, ... until all restricts are tested.
// Copy-pastable text: "abcABC012^\-* &ąδłĄΔŁß"
var restricts = [
// different empty values
undefined,
null,
"",
false,
// non-empty non-string values
1,
true,
0.1,
NaN,
new Object(),
// only selected chars
"aB*Δ",
"aa",
// ASCII ranges
"a-z",
"A-Z",
"a-bA",
// non-standard ranges
"a-",
"-b",
"b-a",
"A-z",
"-",
"--",
"---",
"----",
"-----",
"-----b",
"b-----",
"a-b-c",
"a-b-A",
"a-a-b",
"\\\\-\\^",
"\\^-\\\\",
// various behaviors with caret ^
"^",
"^^",
"\\^a",
"^\\^",
"^aą",
"a^b^c",
"a^b^c^A^B",
"a-zA-Z^bC",
"a-zA-Z^",
// escapes
"\\-",
"a\\-z",
"\\\\",
"\\^",
"\\ab",
"a\\",
"\u0020-\u007E",
// unicode range
"α-ω"
];
var currentRestrict = -1;
function nextRestrict() {
trace("Text: '" + text.text + "'");
trace("====================");
text.text = "";
currentRestrict += 1;
if (restricts.length <= currentRestrict) {
trace("No more restricts");
return;
}
text.restrict = restricts[currentRestrict];
trace("Restrict set: '" + restricts[currentRestrict] + "'");
trace("Restrict get: '" + text.restrict + "'");
}
var listener = new Object();
listener.onKeyDown = function() {
if (Key.getCode() == 27) {
nextRestrict();
}
};
Key.addListener(listener);
Selection.setFocus(text);

Binary file not shown.

View File

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