Skip to content
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: 5 additions & 1 deletion src/json-enc/json-enc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
})

const vals = api.getExpressionVars(elt)
Object.keys(object).forEach(function(key) {
// A hx-vals/hx-vars key whose value serializes to nothing in FormData (e.g. an
// empty array) has no entry in `object` yet, so the key set to restore over must
// be the union of both, not just what FormData produced.
const keys = new Set(Object.keys(object).concat(Object.keys(vals)))
keys.forEach(function(key) {
// FormData encodes values as strings, restore hx-vals/hx-vars with their initial types
object[key] = Object.hasOwn(vals, key) ? vals[key] : object[key]
})
Expand Down
19 changes: 19 additions & 0 deletions src/json-enc/test/ext/json-enc.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ describe('json-enc extension', function() {
form.innerHTML.should.equal('OK')
})

it('keeps hx-vals keys whose value serializes to nothing in FormData', function() {
var values = {}
this.server.respondWith('POST', '/test', function(xhr) {
values = JSON.parse(xhr.requestBody)
xhr.respond(200, {}, 'clicked')
})

make(`<form hx-ext="json-enc" hx-post="/test" hx-vals="js:{'firstLevel': [], 'secondLevel': {'values': []}}">
<button id="btn" type="submit">Submit</button>
</form>`)
var btn = byId('btn')
btn.click()
this.server.respond()

values.should.have.keys('firstLevel', 'secondLevel')
chai.assert.deepEqual(values.firstLevel, [])
chai.assert.deepEqual(values.secondLevel, { values: [] })
})

it('handles multiple select', function() {
this.server.respondWith('POST', '/test', function(xhr) {
var values = JSON.parse(xhr.requestBody)
Expand Down