moveBefore: keep current modal dialog when performing an atomic move.

We still update the a11y tree when we've performed that move as the
inert tree is updated.

Bug: 353441314
Change-Id: I369c6825394c89f2b01bd26a8f1c025cd1f98474
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5712819
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Noam Rosenthal <nrosenthal@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1344723}
diff --git a/third_party/blink/renderer/core/html/html_dialog_element.cc b/third_party/blink/renderer/core/html/html_dialog_element.cc
index 8c5398ae..0a97765e 100644
--- a/third_party/blink/renderer/core/html/html_dialog_element.cc
+++ b/third_party/blink/renderer/core/html/html_dialog_element.cc
@@ -400,6 +400,11 @@
   HTMLDialogElement* old_modal_dialog = document.ActiveModalDialog();
   HTMLElement::RemovedFrom(insertion_point);
   InertSubtreesChanged(document, old_modal_dialog);
+
+  if (GetDocument().StatePreservingAtomicMoveInProgress()) {
+    return;
+  }
+
   SetIsModal(false);
 
   if (close_watcher_) {
diff --git a/third_party/blink/web_tests/external/wpt/dom/nodes/moveBefore/tentative/modal-dialog.html b/third_party/blink/web_tests/external/wpt/dom/nodes/moveBefore/tentative/modal-dialog.html
new file mode 100644
index 0000000..fa9eeae8
--- /dev/null
+++ b/third_party/blink/web_tests/external/wpt/dom/nodes/moveBefore/tentative/modal-dialog.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<title>moveBefore should not close a modal dialog</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<body>
+<section id="old_parent">
+    <dialog id="dialog">
+    </dialog>
+</section>
+<section id="new_parent">
+</section>
+<script>
+promise_test(async t => {
+    const dialog = document.querySelector("#dialog");
+    dialog.showModal();
+    document.querySelector("#new_parent").moveBefore(dialog, null);
+    assert_equals(document.elementFromPoint(0, 0), dialog);
+}, "when reparenting a modal dialog, the dialog should stay modal");
+</script>