diff --git a/crates/js/src/scriptui.rs b/crates/js/src/scriptui.rs index 6f45d87c7c..52440bcdce 100644 --- a/crates/js/src/scriptui.rs +++ b/crates/js/src/scriptui.rs @@ -1611,9 +1611,11 @@ fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult> { ("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> { 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")?; diff --git a/crates/js/src/scriptui_tests.rs b/crates/js/src/scriptui_tests.rs index eb80e515ab..78b491b021 100644 --- a/crates/js/src/scriptui_tests.rs +++ b/crates/js/src/scriptui_tests.rs @@ -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()); diff --git a/docs/mcp.md b/docs/mcp.md index 33049dfe14..10be342154 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -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.