Fix illegal invocation for FormData proxy (#2997)

Fix illegal invocation for a FormData proxy returning a function looked-up via a symbol

Co-authored-by: shartley <scrhartley@github.com>
This commit is contained in:
Simon Hartley 2024-11-07 17:15:07 +00:00 committed by GitHub
parent 816fe6d161
commit 62969122f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -4039,7 +4039,15 @@ var htmx = (function() {
get: function(target, name) {
if (typeof name === 'symbol') {
// Forward symbol calls to the FormData itself directly
return Reflect.get(target, name)
const result = Reflect.get(target, name)
// Wrap in function with apply to correctly bind the FormData context, as a direct call would result in an illegal invocation error
if (typeof result === 'function') {
return function() {
return result.apply(formData, arguments)
}
} else {
return result
}
}
if (name === 'toJSON') {
// Support JSON.stringify call on proxy

View File

@ -288,6 +288,14 @@ describe('Core htmx Parameter Handling', function() {
vals.foo.should.equal('null')
})
it('formdata can be used to construct a URLSearchParams instance', function() {
var form = make('<input name="foo" value="bar"/>')
var vals = htmx._('getInputValues')(form, 'get').values
function makeSearchParams() { return new URLSearchParams(vals).toString() }
makeSearchParams.should.not.throw()
makeSearchParams().should.equal('foo=bar')
})
it('order of parameters follows order of input elements', function() {
this.server.respondWith('GET', '/test?foo=bar&bar=foo&foo=bar&foo2=bar2', function(xhr) {
xhr.respond(200, {}, 'Clicked!')