Scripts: a ScriptUI dialog's OK and Cancel found by name or text skip buttons that cannot be clicked

Without defaultElement or cancelElement, the dialog's default and cancel
buttons are the first enabled and visible buttons named or titled OK and
Cancel, so Enter and Escape click a button the dialog takes a click on.
This commit is contained in:
dotCooCoo committed 2026-11-25 15:44:37 -08:00
1 parent 6aae27aa2e
commit 5473a2217b
3 files changed
+15 -3

No files matched your search

+4 -2
View File
@@ -1611,9 +1611,11 @@ fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult<Option<Described>> {
("name".into(), Data::String(name.clone())),
("text".into(), Data::String(text.clone())),
];
names.push((kind.clone(), name, text));
let enabled = get(i, o, "enabled")?.truthy();
let visible = get(i, o, "visible")?.truthy();
// Enter and Escape click a button found by its name or text only when it can be clicked.
let clickable = kind == "button" && enabled && visible;
names.push((clickable, name, text));
d.push(("enabled".into(), Data::Bool(enabled)));
d.push(("visible".into(), Data::Bool(visible)));
// A value a getter gives is kept from collection while its own properties are read.
@@ -1694,7 +1696,7 @@ fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult<Option<Described>> {
if !matches!(given, Value::Undefined | Value::Null) {
return Ok(-1.0);
}
let button = |by_name: bool| names.iter().position(|(k, n, t)| k == "button" && (if by_name { n } else { t }).to_lowercase() == word);
let button = |by_name: bool| names.iter().position(|(c, n, t)| *c && (if by_name { n } else { t }).to_lowercase() == word);
Ok(button(true).or_else(|| button(false)).map_or(-1.0, |n| n as f64))
};
let def = target(i, "defaultElement", "ok")?;
+9
View File
@@ -403,6 +403,15 @@ fn a_close_that_on_close_asks_for_is_part_of_the_close_it_refuses() {
assert_eq!(r.run(source).unwrap(), JsValue::String("1,2".into()), "the refused close waits for the click on OK");
}
#[test]
fn a_hidden_or_disabled_ok_button_is_not_the_default_found_by_its_text() {
let (mut r, seen, _) = realm(vec![close()]);
let source = "var w = new Window('dialog'); w.add('button', undefined, 'OK').enabled = false; w.add('button', undefined, 'OK').visible = false; \
w.add('button', undefined, 'OK'); w.show()";
assert_eq!(r.run(source).unwrap(), JsValue::Number(0.0));
assert_eq!(field(&seen.borrow().descriptions[0], "defaultElement"), &Data::Number(3.0), "the OK button that can be clicked is the default");
}
#[test]
fn an_edit_field_that_hides_its_text_is_refused_when_shown() {
let (mut r, seen, _) = realm(Vec::new());
+2 -1
View File
@@ -3052,7 +3052,8 @@ close box. The default or cancel button closes the dialog only when it has no `o
its own and no `capture` handler on a container stopped the click; `click` handlers on the containers that do not
capture do not keep it from closing (VectorCraft's own rule). `onClose` runs before the dialog closes, and
returning `false` keeps it open. The default button is `defaultElement`, else a button named or titled OK, and the
cancel button is `cancelElement`, else a button named or titled Cancel, in any case. A `show()` inside a handler,
cancel button is `cancelElement`, else a button named or titled Cancel, in any case; a button found by its name or
title counts only while it is enabled and visible. A `show()` inside a handler,
or of a dialog that is showing, throws an `Error`, and so does one inside a callback of a built-in function. In
the app, `alert`, `confirm`, `prompt` and the file dialogs called from a handler also throw an `Error`; in a run with
nobody to ask, `alert`, `confirm` and `prompt` in a handler take their answers from `answers`. A shown dialog can be shown again after it closes.