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

Make hx-header and hx-vals JS eval more consistent with expectations #2673

Open
wants to merge 4 commits into
base: master
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
3 changes: 2 additions & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,8 @@ var htmx = (function() {
str = str.substr(3)
evaluateValue = true
}
if (str.indexOf('{') !== 0) {
var needsBraces = ((!evaluateValue || attr === 'hx-vars') && str.indexOf('{') !== 0)
if (needsBraces) {
str = '{' + str + '}'
}
let varsValues
Expand Down
10 changes: 5 additions & 5 deletions test/attributes/hx-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('hx-headers attribute', function() {
xhr.requestHeaders.i1.should.equal('test')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-post="/vars" hx-headers="javascript:i1:\'test\'"></div>')
var div = make('<div hx-post="/vars" hx-headers="javascript:(function(){return {i1: \'test\'}})()"></div>')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked!')
Expand All @@ -106,7 +106,7 @@ describe('hx-headers attribute', function() {
xhr.requestHeaders.v2.should.equal('42')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-post="/vars" hx-headers="javascript:v1:\'test\', v2:42"></div>')
var div = make('<div hx-post="/vars" hx-headers="javascript:{v1: (function(){return \'test\'})(), v2: (function(){return 42})()}"></div>')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked!')
Expand All @@ -117,7 +117,7 @@ describe('hx-headers attribute', function() {
xhr.requestHeaders.i1.should.equal('test')
xhr.respond(200, {}, 'Clicked!')
})
make('<div hx-headers="javascript:i1:\'test\'"><div id="d1" hx-post="/vars"></div></div>')
make('<div hx-headers="javascript:{i1:\'test\'}"><div id="d1" hx-post="/vars"></div></div>')
var div = byId('d1')
div.click()
this.server.respond()
Expand All @@ -129,7 +129,7 @@ describe('hx-headers attribute', function() {
xhr.requestHeaders.i1.should.equal('best')
xhr.respond(200, {}, 'Clicked!')
})
make('<div hx-headers="javascript:i1:\'test\'"><div id="d1" hx-headers="javascript:i1:\'best\'" hx-post="/vars"></div></div>')
make('<div hx-headers="javascript:{i1:\'test\'}"><div id="d1" hx-headers="javascript:{i1:\'best\'}" hx-post="/vars"></div></div>')
var div = byId('d1')
div.click()
this.server.respond()
Expand All @@ -141,7 +141,7 @@ describe('hx-headers attribute', function() {
xhr.requestHeaders.i1.should.equal('best')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-target="this"><input hx-post="/include" hx-headers="javascript:i1:\'best\'" hx-trigger="click" id="i1" name="i1" value="test"/></div>')
var div = make('<div hx-target="this"><input hx-post="/include" hx-headers="javascript:{i1:\'best\'}" hx-trigger="click" id="i1" name="i1" value="test"/></div>')
var input = byId('i1')
input.click()
this.server.respond()
Expand Down
10 changes: 5 additions & 5 deletions test/attributes/hx-vals.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('hx-vals attribute', function() {
params.i1.should.equal('test')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-post="/vars" hx-vals="javascript:i1:\'test\'"></div>')
var div = make('<div hx-post="/vars" hx-vals="javascript:(function(){return {i1: \'test\'}})()"></div>')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked!')
Expand All @@ -127,7 +127,7 @@ describe('hx-vals attribute', function() {
params.v2.should.equal('42')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-post="/vars" hx-vals="javascript:v1:\'test\', v2:42"></div>')
var div = make('<div hx-post="/vars" hx-vals="javascript:{v1: (function(){return \'test\'})(), v2: (function(){return 42})()}"></div>')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked!')
Expand All @@ -139,7 +139,7 @@ describe('hx-vals attribute', function() {
params.i1.should.equal('test')
xhr.respond(200, {}, 'Clicked!')
})
make('<div hx-vals="javascript:i1:\'test\'"><div id="d1" hx-post="/vars"></div></div>')
make('<div hx-vals="javascript:{i1:\'test\'}"><div id="d1" hx-post="/vars"></div></div>')
var div = byId('d1')
div.click()
this.server.respond()
Expand All @@ -152,7 +152,7 @@ describe('hx-vals attribute', function() {
params.i1.should.equal('best')
xhr.respond(200, {}, 'Clicked!')
})
make('<div hx-vals="javascript:i1:\'test\'"><div id="d1" hx-vals="javascript:i1:\'best\'" hx-post="/vars"></div></div>')
make('<div hx-vals="javascript:{i1:\'test\'}"><div id="d1" hx-vals="javascript:{i1:\'best\'}" hx-post="/vars"></div></div>')
var div = byId('d1')
div.click()
this.server.respond()
Expand All @@ -165,7 +165,7 @@ describe('hx-vals attribute', function() {
params.i1.should.equal('best')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-target="this"><input hx-post="/include" hx-vals="javascript:i1:\'best\'" hx-trigger="click" id="i1" name="i1" value="test"/></div>')
var div = make('<div hx-target="this"><input hx-post="/include" hx-vals="javascript:{i1:\'best\'}" hx-trigger="click" id="i1" name="i1" value="test"/></div>')
var input = byId('i1')
input.click()
this.server.respond()
Expand Down
2 changes: 2 additions & 0 deletions www/content/attributes/hx-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ If you wish for `hx-headers` to *evaluate* the values given, you can prefix the

```html
<div hx-get="/example" hx-headers='{"myHeader": "My Value"}'>Get Some HTML, Including A Custom Header in the Request</div>

<div hx-get="/example" hx-vals='js:{myVal: calculateValue()}'>Get Some HTML, Including a Dynamic Header from Javascript in the Request</div>
```

## Security Considerations
Expand Down