Avoid eval on web (#5313)

This commit is contained in:
Moritz Hedtke authored and GitHub committed 2026-02-16 08:39:43 -06:00
1 parent eeb60f7173
commit d312ef84d2
1 file changed
+22 -13
+22 -13
View File
@@ -213,6 +213,23 @@ impl Display for ToastLevel {
}
}
#[wasm_bindgen(inline_js = r#"
export function js_show_toast(header_text, message, level, as_ms) {
if (typeof showDXToast !== "undefined") {{
window.showDXToast(header_text, message, level, as_ms);
}}
}
export function js_schedule_toast(header_text, message, level, as_ms) {
if (typeof scheduleDXToast !== "undefined") {{
window.scheduleDXToast(header_text, message, level, as_ms);
}}
}
"#)]
extern "C" {
fn js_schedule_toast(header_text: &str, message: &str, level: String, as_ms: u32);
fn js_show_toast(header_text: &str, message: &str, level: String, as_ms: u32);
}
/// Displays a toast to the developer.
pub(crate) fn show_toast(
header_text: &str,
@@ -221,20 +238,12 @@ pub(crate) fn show_toast(
duration: Duration,
after_reload: bool,
) {
let as_ms = duration.as_millis();
let as_ms: u32 = duration.as_millis().try_into().unwrap();
let js_fn_name = match after_reload {
true => "scheduleDXToast",
false => "showDXToast",
};
_ = js_sys::eval(&format!(
r#"
if (typeof {js_fn_name} !== "undefined") {{
window.{js_fn_name}(`{header_text}`, `{message}`, `{level}`, {as_ms});
}}
"#,
));
match after_reload {
true => js_schedule_toast(header_text, message, level.to_string(), as_ms),
false => js_show_toast(header_text, message, level.to_string(), as_ms),
}
}
/// Force a hotreload of the assets on this page by walking them and changing their URLs to include