Skip to content

Commit 84b94c0

Browse files
Merge pull request #2528 from johanrd/review/no-action-on-submit-button
Skip reporting for <form method="dialog"> submit buttons
2 parents 12f743a + e85f7c6 commit 84b94c0

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/rules/template-no-action-on-submit-button.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ function isInsideForm(node) {
22
let current = node.parent;
33
while (current) {
44
if (current.type === 'GlimmerElementNode' && current.tag === 'form') {
5+
// <form method="dialog"> closes the dialog on submit without navigating,
6+
// so click handlers on submit buttons are intentional and correct.
7+
// See https://github.com/ember-template-lint/ember-template-lint/issues/2989
8+
const methodAttr = current.attributes?.find((a) => a.name === 'method');
9+
if (
10+
methodAttr &&
11+
methodAttr.value?.type === 'GlimmerTextNode' &&
12+
methodAttr.value.chars.toLowerCase() === 'dialog'
13+
) {
14+
return false;
15+
}
516
return true;
617
}
718
current = current.parent;

tests/lib/rules/template-no-action-on-submit-button.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ ruleTester.run('template-no-action-on-submit-button', rule, {
2828
'<template><form><div type="submit"></div></form></template>',
2929
'<template><form><div type="submit" {{action this.handleClick}}></div></form></template>',
3030
'<template><form><div type="submit" {{on "click" this.handleClick}}></div></form></template>',
31+
// <form method="dialog"> — click handlers are intentional (closes dialog)
32+
// https://github.com/ember-template-lint/ember-template-lint/issues/2989
33+
'<template><form method="dialog"><button type="submit" {{on "click" this.handleClick}} /></form></template>',
34+
'<template><form method="dialog"><button {{action this.handleClick}} /></form></template>',
35+
'<template><form method="DIALOG"><button type="submit" {{on "click" this.handleClick}} /></form></template>',
3136
// Outside a form — valid
3237
'<template><button {{action this.handleClick}} /></template>',
3338
'<template><button {{action this.handleClick on="click"}}/></template>',
@@ -110,6 +115,9 @@ hbsRuleTester.run('template-no-action-on-submit-button (hbs)', rule, {
110115
'<form><div type="submit"></div></form>',
111116
'<form><div type="submit" {{action this.handleClick}}></div></form>',
112117
'<form><div type="submit" {{on "click" this.handleClick}}></div></form>',
118+
// <form method="dialog"> — click handlers are intentional
119+
'<form method="dialog"><button type="submit" {{on "click" this.handleClick}} /></form>',
120+
'<form method="dialog"><button {{action this.handleClick}} /></form>',
113121
// Outside a form — valid
114122
'<button {{action this.handleClick}} />',
115123
'<button {{action this.handleClick on="click"}}/>',

0 commit comments

Comments
 (0)