From ea72316093a1e3aaf86a7cda5f83ca8f6a20d2da Mon Sep 17 00:00:00 2001
From: jgraham <james@hoppipolla.co.uk>
Date: Wed, 20 Nov 2024 01:46:01 +0000
Subject: [PATCH] Support automatic user prompt handing in WebDriver BiDi

Read the return value from the "WebDriver BiDi user prompt opened" algorithm and use it to decide whether to show the prompt, or skip showing it but act as if it had been accepted or canceled.

At present the prompt opened algorithm can return three values: "none", which causes the prompt to be displayed, "accept", which acts as if the prompt was accepted, and "dismiss" which acts as if the prompt was rejected. The WebDriver side is responsible for ensuring that only valid responses are sent for each prompt type (e.g. for an alert there isn't a "dismiss" option).
---
 source | 103 +++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 71 insertions(+), 32 deletions(-)

diff --git a/source b/source
index 0d819eccb96..7bca5bea4a4 100644
--- a/source
+++ b/source
@@ -101046,24 +101046,33 @@ location.href = '#foo';</code></pre>
     <ol>
      <li><p>Set <var>unloadPromptShown</var> to true.</p></li>
 
-     <li><p>Invoke <span>WebDriver BiDi user prompt opened</span> with <var>document</var>'s
-     <span>relevant global object</span>, "<code data-x="">beforeunload</code>", and "".</p></li>
+     <li><p>Let <var>userPromptHandler</var> be the result of <span>WebDriver BiDi user prompt
+     opened</span> with <var>document</var>'s <span>relevant global object</span>,
+     "<code data-x="">beforeunload</code>", and "".</p></li>
+
+     <li><p>If <var>userPromptHandler</var> is "<code data-x="">dismiss</code>", then set
+     <var>unloadPromptCanceled</var> to true.</p></li>
 
      <li>
-      <p>Ask the user to confirm that they wish to unload the document, and <span>pause</span> while
-      waiting for the user's response.</p>
+      <p>If <var>userPromptHandler</var> is "<code data-x="">none</code>", then:</p>
 
-      <p class="note">The message shown to the user is not customizable, but instead determined by
-      the user agent. In particular, the actual value of the <code
-      data-x="dom-BeforeUnloadEvent-returnValue">returnValue</code> attribute is ignored.</p>
-     </li>
+      <ul>
+       <li>
+        <p>Ask the user to confirm that they wish to unload the document, and <span>pause</span>
+        while waiting for the user's response.</p>
 
-     <li><p>If the user did not confirm the page navigation, set <var>unloadPromptCanceled</var> to
-     true.</p></li>
+        <p class="note">The message shown to the user is not customizable, but instead determined by
+        the user agent. In particular, the actual value of the <code
+        data-x="dom-BeforeUnloadEvent-returnValue">returnValue</code> attribute is ignored.</p>
+       </li>
+
+       <li><p>If the user did not confirm the page navigation, then set
+       <var>unloadPromptCanceled</var> to true.</p></li>
+     </ul>
 
      <li><p>Invoke <span>WebDriver BiDi user prompt closed</span> with <var>document</var>'s
-     <span>relevant global object</span> and true if <var>unloadPromptCanceled</var> is false or
-     false otherwise.</p></li>
+     <span>relevant global object</span>, "<code data-x="">beforeunload</code>", and true
+     if <var>unloadPromptCanceled</var> is false or false otherwise.</p></li>
     </ol>
    </li>
 
@@ -115602,16 +115611,22 @@ function sendData(data) {
    <li><p>Set <var>message</var> to the result of <span data-x="optionally truncate a simple dialog
    string">optionally truncating</span> <var>message</var>.</p></li>
 
-   <li><p>Show <var>message</var> to the user, treating U+000A LF as a line break.</p></li>
+   <li><p>Let <var>userPromptHandler</var> be <span>WebDriver BiDi user prompt opened</span> with
+   <span>this</span>, "<code data-x="">alert</code>", and <var>message</var>.</p></li>
 
-   <li><p>Invoke <span>WebDriver BiDi user prompt opened</span> with <span>this</span>,
-   "<code data-x="">alert</code>", and <var>message</var>.</p></li>
+   <li>
+    <p>If <var>userPromptHandler</var> is "<code data-x="">none</code>", then:</p>
+
+    <ol>
+     <li><p>Show <var>message</var> to the user, treating U+000A LF as a line break.</p></li>
 
-   <li><p>Optionally, <span>pause</span> while waiting for the user to acknowledge the
-   message.</p></li>
+     <li><p>Optionally, <span>pause</span> while waiting for the user to acknowledge the
+     message.</p></li>
+    </ol>
+   </li>
 
-   <li><p>Invoke <span>WebDriver BiDi user prompt closed</span> with <span>this</span>
-   and true.</p></li>
+   <li><p>Invoke <span>WebDriver BiDi user prompt closed</span> with <span>this</span>,
+   "<code data-x="">alert</code>", and true.</p></li>
   </ol>
 
   <p class="note">This method is defined using two overloads, instead of using an
@@ -115636,16 +115651,28 @@ function sendData(data) {
    <li><p>Show <var>message</var> to the user, treating U+000A LF as a line break, and ask the user
    to respond with a positive or negative response.</p></li>
 
-   <li><p>Invoke <span>WebDriver BiDi user prompt opened</span> with <span>this</span>,
-   "<code data-x="">confirm</code>", and <var>message</var>.</p></li>
+   <li><p>Let <var>userPromptHandler</var> be <span>WebDriver BiDi user prompt opened</span> with
+   <span>this</span>, "<code data-x="">confirm</code>", and <var>message</var>.</p></li>
+
+   <li><p>Let <var>accepted</var> be false.</p></li>
+
+   <li>
+    <p>If <var>userPromptHandler</var> is "<code data-x="">none</code>", then:</p>
+
+    <ol>
+     <li><p><span>Pause</span> until the user responds either positively or negatively.</p></li>
+
+     <li><p>If the user responded positively, then set <var>accepted</var> to true.</p></li>
+    </ol>
+   </li>
 
-   <li><p><span>Pause</span> until the user responds either positively or negatively.</p></li>
+   <li><p>If <var>userPromptHandler</var> is "<code data-x="">accept</code>", then set
+   <var>accepted</var> to true.</p></li>
 
    <li><p>Invoke <span>WebDriver BiDi user prompt closed</span> with <span>this</span>,
-   and true if the user responded positively or false otherwise.</p></li>
+   "<code data-x="">confirm</code>", and <var>accepted</var>.</p></li>
 
-   <li><p>If the user responded positively, return true; otherwise, the user responded negatively:
-   return false.</p></li>
+   <li><p>Return <var>accepted</var>.</p></li>
   </ol>
 
   <p>The <dfn method for="Window"><code data-x="dom-prompt">prompt(<var>message</var>,
@@ -115668,18 +115695,30 @@ function sendData(data) {
    to either respond with a string value or abort. The response must be defaulted to the value given
    by <var>default</var>.</p></li>
 
-   <li><p>Invoke <span>WebDriver BiDi user prompt opened</span> with <span>this</span>,
-   "<code data-x="">prompt</code>", <var>message</var>, and <var>default</var>.</p></li>
+   <li><p>Let <var>userPromptHandler</var> be <span>WebDriver BiDi user prompt opened</span> with
+   <span>this</span>, "<code data-x="">prompt</code>", and <var>message</var>.</p></li>
+
+   <li><p>Let <var>result</var> be null.</p></li>
+
+   <li>
+    <p>If <var>userPromptHandler</var> is "<code data-x="">none</code>", then:</p>
+
+    <ol>
+     <li><p><span>Pause</span> while waiting for the user's response.</p></li>
 
-   <li><p><span>Pause</span> while waiting for the user's response.</p></li>
+     <li><p>If the user did not abort, then set <var>result</var> to the string that the user
+     responded with.</p></li>
+    </ol>
+   </li>
 
-   <li><p>Let <var>result</var> be null if the user aborts, or otherwise the string that the user
-   responded with.</p></li>
+   <li><p>Otherwise, if <var>userPromptHandler</var> is "<code data-x="">accept</code>", then set
+   <var>result</var> to the empty string.</p></li>
 
    <li><p>Invoke <span>WebDriver BiDi user prompt closed</span> with <span>this</span>,
-   false if <var>result</var> is null or true otherwise, and <var>result</var>.</p></li>
+   "<code data-x="">prompt</code>", false if <var>result</var> is null or true otherwise,
+   and <var>result</var>.</p></li>
 
-   <li><p>Return <var>result</var>.</li>
+   <li><p>Return <var>result</var>.</p></li>
   </ol>
 
   <p>To <dfn>optionally truncate a simple dialog string</dfn> <var>s</var>, return either