Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce no-op on submit buttons with formmethod=dialog. #3075

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4201,9 +4201,11 @@ var htmx = (function() {

const buttonVerb = getRawAttribute(submitter, 'formmethod')
if (buttonVerb != null) {
// ignore buttons with formmethod="dialog"
if (buttonVerb.toLowerCase() !== 'dialog') {
if (VERBS.includes(buttonVerb.toLowerCase())) {
verb = (/** @type HttpVerb */(buttonVerb))
} else {
maybeCall(resolve)
return promise
}
}
}
Expand Down
26 changes: 6 additions & 20 deletions test/core/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,32 +1206,18 @@ describe('Core htmx AJAX Tests', function() {
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'], s1: 'selectValue' })
})

it('handles form post with button formmethod dialog properly', function() {
var values
it('properly handles buttons with formmethod=dialog', function() {
var request = false
this.server.respondWith('POST', '/test', function(xhr) {
values = getParameters(xhr)
xhr.respond(200, {}, '')
})

make('<dialog><form hx-post="/test"><button id="submit" formmethod="dialog" name="foo" value="bar">submit</button></form></dialog>')

byId('submit').click()
this.server.respond()
values.should.deep.equal({ foo: 'bar' })
})

it('handles form get with button formmethod dialog properly', function() {
var responded = false
this.server.respondWith('GET', '/test', function(xhr) {
responded = true
xhr.respond(200, {}, '')
request = true
xhr.respond(200, {}, '<button>Bar</button>')
})

make('<dialog><form hx-get="/test"><button id="submit" formmethod="dialog">submit</button></form></dialog>')
make('<dialog><form hx-post="/test"><button id="submit" formmethod="dialog" name="foo" value="bar">Submit</button></form></dialog>')

byId('submit').click()
this.server.respond()
responded.should.equal(true)
request.should.equal(false)
})

it('can associate submit buttons from outside a form with the current version of the form after swap', function() {
Expand Down
26 changes: 6 additions & 20 deletions test/core/shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,32 +1254,18 @@ describe('Core htmx Shadow DOM Tests', function() {
values.should.deep.equal({ t1: 'textValue', b1: ['inputValue', 'buttonValue'], s1: 'selectValue' })
})

it('handles form post with button formmethod dialog properly', function() {
var values
it('properly handles buttons with formmethod=dialog', function() {
var request = false
this.server.respondWith('POST', '/test', function(xhr) {
values = getParameters(xhr)
xhr.respond(200, {}, '')
})

make('<dialog><form hx-post="/test"><button id="submit" formmethod="dialog" name="foo" value="bar">submit</button></form></dialog>')

byId('submit').click()
this.server.respond()
values.should.deep.equal({ foo: 'bar' })
})

it('handles form get with button formmethod dialog properly', function() {
var responded = false
this.server.respondWith('GET', '/test', function(xhr) {
responded = true
xhr.respond(200, {}, '')
request = true
xhr.respond(200, {}, '<button>Bar</button>')
})

make('<dialog><form hx-get="/test"><button id="submit" formmethod="dialog">submit</button></form></dialog>')
make('<dialog><form hx-post="/test"><button id="submit" formmethod="dialog" name="foo" value="bar">Submit</button></form></dialog>')

byId('submit').click()
this.server.respond()
responded.should.equal(true)
request.should.equal(false)
})

it('can associate submit buttons from outside a form with the current version of the form after swap', function() {
Expand Down