Skip to main content

ferridriver_script/bindings/
dialog.rs

1//! `DialogJs` — QuickJS binding for [`ferridriver::dialog::Dialog`].
2//!
3//! Mirrors Playwright's client-side `Dialog` class from
4//! `/tmp/playwright/packages/playwright-core/src/client/dialog.ts`:
5//! read-only accessors (`type()`, `message()`, `defaultValue()`) +
6//! async `accept(promptText?)` / `dismiss()`.
7
8use ferridriver::dialog::Dialog as CoreDialog;
9use rquickjs::JsLifetime;
10use rquickjs::class::Trace;
11use rquickjs::function::Opt;
12
13use crate::bindings::convert::FerriResultExt;
14
15#[derive(JsLifetime, Trace)]
16#[rquickjs::class(rename = "Dialog")]
17pub struct DialogJs {
18  #[qjs(skip_trace)]
19  inner: CoreDialog,
20}
21
22impl DialogJs {
23  #[must_use]
24  pub fn new(inner: CoreDialog) -> Self {
25    Self { inner }
26  }
27}
28
29#[rquickjs::methods]
30impl DialogJs {
31  /// Playwright: `dialog.type(): string` — `"alert"` / `"beforeunload"`
32  /// / `"confirm"` / `"prompt"`.
33  #[qjs(rename = "type")]
34  pub fn dialog_type(&self) -> String {
35    self.inner.dialog_type().as_str().to_string()
36  }
37
38  /// Playwright: `dialog.message(): string`.
39  #[qjs(rename = "message")]
40  pub fn message(&self) -> String {
41    self.inner.message().to_string()
42  }
43
44  /// Playwright: `dialog.defaultValue(): string`. Empty for non-prompt
45  /// dialogs.
46  #[qjs(rename = "defaultValue")]
47  pub fn default_value(&self) -> String {
48    self.inner.default_value().to_string()
49  }
50
51  /// Playwright: `dialog.page(): Page | null`. `null` when the dialog
52  /// opened before its page was available (early page initialization)
53  /// or the owning page has been closed.
54  #[qjs(rename = "page")]
55  pub fn page(&self) -> Option<crate::bindings::page::PageJs> {
56    self.inner.page().map(crate::bindings::page::PageJs::new)
57  }
58
59  /// Playwright: `dialog.accept(promptText?): Promise<void>`.
60  /// `promptText` is applied to `prompt` dialogs only; other types
61  /// ignore it. Double-accept / accept-after-dismiss rejects with the
62  /// Playwright-exact message `"Cannot accept dialog which is already
63  /// handled!"`.
64  #[qjs(rename = "accept")]
65  pub async fn accept(&self, prompt_text: Opt<String>) -> rquickjs::Result<()> {
66    self.inner.accept(prompt_text.0).await.into_js()
67  }
68
69  /// Playwright: `dialog.dismiss(): Promise<void>`.
70  #[qjs(rename = "dismiss")]
71  pub async fn dismiss(&self) -> rquickjs::Result<()> {
72    self.inner.dismiss().await.into_js()
73  }
74}