diff --git a/src/observers/form_link_click_observer.ts b/src/observers/form_link_click_observer.ts index 8ea08597e..ddd69ae94 100644 --- a/src/observers/form_link_click_observer.ts +++ b/src/observers/form_link_click_observer.ts @@ -26,7 +26,7 @@ export class FormLinkClickObserver implements LinkClickObserverDelegate { willFollowLinkToLocation(link: Element, location: URL, originalEvent: MouseEvent): boolean { return ( this.delegate.willSubmitFormLinkToLocation(link, location, originalEvent) && - link.hasAttribute("data-turbo-method") + (link.hasAttribute("data-turbo-method") || link.hasAttribute("data-turbo-confirm")) ) } diff --git a/src/tests/functional/visit_tests.ts b/src/tests/functional/visit_tests.ts index 146bcda58..d8b082444 100644 --- a/src/tests/functional/visit_tests.ts +++ b/src/tests/functional/visit_tests.ts @@ -223,3 +223,28 @@ test("test Visit with network error", async ({ page }) => { async function visitLocation(page: Page, location: string) { return page.evaluate((location) => window.Turbo.visit(location), location) } + +test("test data-turbo-confirm on anchor element without data-turbo-method", async ({ page }) => { + let confirmed = false + + page.on("dialog", (alert) => { + assert.equal(alert.message(), "Are you sure?") + alert.accept() + confirmed = true + }) + + await page.evaluate(() => { + const link = document.querySelector("#same-origin-link") + + if (link) link.dataset.turboConfirm = "Are you sure?" + }) + + assert.equal(await page.locator("#same-origin-link[data-turbo-confirm]:not([data-turbo-method])").count(), 1) + assert.equal(pathname(page.url()), "/src/tests/fixtures/visit.html") + + await page.click("#same-origin-link") + await nextEventNamed(page, "turbo:load") + + assert.isTrue(confirmed) + assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html") +})