Skip to content

Commit f3682bd

Browse files
committed
Fix data-turbo-confirm on <a> without data-turbo-method
1 parent 89be8e4 commit f3682bd

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

src/core/confirmation.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Confirmation {
2+
static confirmMethod(message, _element, _submitter) {
3+
return Promise.resolve(confirm(message))
4+
}
5+
}

src/core/drive/form_submission.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Confirmation } from "../confirmation"
12
import { FetchRequest, FetchMethod, fetchMethodFromString, fetchEnctypeFromString, isSafe } from "../../http/fetch_request"
23
import { expandURL } from "../url"
34
import { clearBusyState, dispatch, getAttribute, getMetaContent, hasAttribute, markAsBusy } from "../../util"
@@ -22,10 +23,6 @@ export const FormEnctype = {
2223
export class FormSubmission {
2324
state = FormSubmissionState.initialized
2425

25-
static confirmMethod(message, _element, _submitter) {
26-
return Promise.resolve(confirm(message))
27-
}
28-
2926
constructor(delegate, formElement, submitter, mustRedirect = false) {
3027
const method = getMethod(formElement, submitter)
3128
const action = getAction(getFormAction(formElement, submitter), method)
@@ -78,7 +75,7 @@ export class FormSubmission {
7875
const confirmationMessage = getAttribute("data-turbo-confirm", this.submitter, this.formElement)
7976

8077
if (typeof confirmationMessage === "string") {
81-
const answer = await FormSubmission.confirmMethod(confirmationMessage, this.formElement, this.submitter)
78+
const answer = await Confirmation.confirmMethod(confirmationMessage, this.formElement, this.submitter)
8279
if (!answer) {
8380
return
8481
}

src/core/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Session } from "./session"
22
import { PageRenderer } from "./drive/page_renderer"
33
import { PageSnapshot } from "./drive/page_snapshot"
44
import { FrameRenderer } from "./frames/frame_renderer"
5-
import { FormSubmission } from "./drive/form_submission"
5+
import { Confirmation } from "./confirmation"
66
import { fetch, recentRequests } from "../http/fetch"
77

88
const session = new Session(recentRequests)
@@ -101,7 +101,7 @@ export function setProgressBarDelay(delay) {
101101
}
102102

103103
export function setConfirmMethod(confirmMethod) {
104-
FormSubmission.confirmMethod = confirmMethod
104+
Confirmation.confirmMethod = confirmMethod
105105
}
106106

107107
export function setFormMode(mode) {

src/core/session.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PageView } from "./drive/page_view"
1818
import { FrameElement } from "../elements/frame_element"
1919
import { Preloader } from "./drive/preloader"
2020
import { Cache } from "./cache"
21+
import { Confirmation } from "./confirmation"
2122

2223
export class Session {
2324
navigator = new Navigator(this)
@@ -223,9 +224,17 @@ export class Session {
223224
)
224225
}
225226

226-
followedLinkToLocation(link, location) {
227+
async followedLinkToLocation(link, location) {
227228
const action = this.getActionForLink(link)
228229
const acceptsStreamResponse = link.hasAttribute("data-turbo-stream")
230+
const confirmationMessage = link.getAttribute("data-turbo-confirm")
231+
232+
if (typeof confirmationMessage === "string") {
233+
const answer = await Confirmation.confirmMethod(confirmationMessage, link, link)
234+
if (!answer) {
235+
return
236+
}
237+
}
229238

230239
this.visit(location.href, { action, acceptsStreamResponse })
231240
}

src/tests/functional/visit_tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ test("Turbo history state after a reload", async ({ page }) => {
267267
)
268268
})
269269

270+
test("test data-turbo-confirm on anchor element without data-turbo-method", async ({ page }) => {
271+
let confirmed = false
272+
273+
page.on("dialog", (alert) => {
274+
assert.equal(alert.message(), "Are you sure?")
275+
alert.accept()
276+
confirmed = true
277+
})
278+
279+
await page.evaluate(() => {
280+
const link = document.querySelector("#same-origin-link")
281+
282+
if (link) link.dataset.turboConfirm = "Are you sure?"
283+
})
284+
285+
assert.equal(await page.locator("#same-origin-link[data-turbo-confirm]:not([data-turbo-method])").count(), 1)
286+
assert.equal(pathname(page.url()), "/src/tests/fixtures/visit.html")
287+
288+
await page.click("#same-origin-link")
289+
await nextEventNamed(page, "turbo:load")
290+
291+
assert.isTrue(confirmed)
292+
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
293+
})
294+
270295
async function visitLocation(page, location) {
271296
return page.evaluate((location) => window.Turbo.visit(location), location)
272297
}

0 commit comments

Comments
 (0)