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
2 changes: 2 additions & 0 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ var htmx = (() => {

__parseTriggerSpecs(spec) {
return HCON.split(spec).flatMap(s => {
// the filter of `every 1s [cond]` follows the interval, move it onto the name
s = s.replace(/^(\s*every)(\s+[^\s\[]+)\s*(\[[^\]]*\])/, '$1$3$2');
let [,name,rest] = s.match(/^\s*(\S+\[[^\]]*\]|\S+)\s*(.*?)\s*$/) ?? [];
if (!name) return []; // skip empty/whitespace-only tokens
if (/\[[^\]]*$/.test(name)) throw "unterminated:" + name; // e.g. click[ctrlKey
Expand Down
11 changes: 11 additions & 0 deletions test/tests/attributes/hx-trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ describe('hx-trigger attribute', function() {
await forRequest()
})

it('filters polling with a filter after the every interval', async function () {
mockResponse('GET', '/test', 'Called!')
let div = createProcessedHTML('<div hx-get="/test" hx-trigger="every 20ms [this.dataset.go]"></div>' +
'<div hx-get="/test" hx-trigger="every 20ms[this.dataset.go]"></div>')
await htmx.timeout(100)
fetchMock.calls.length.should.equal(0)
div.dataset.go = 'true'
await forRequest()
delete div.dataset.go
})

it('once modifier works', function () {
mockResponse('GET', '/test', "Response")
createProcessedHTML('<button hx-trigger="click once" hx-get="/test"/></button>')
Expand Down
9 changes: 9 additions & 0 deletions test/tests/unit/__parseTriggerSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ describe('__parseTriggerSpecs unit tests', function() {
assert.equal(specs[0]['1s'], true)
})

it('parses every trigger with a filter after the interval', function () {
for (let spec of ['every 1s [foo === 1]', 'every 1s[foo === 1]']) {
let specs = htmx.__parseTriggerSpecs(spec)
assert.equal(specs.length, 1)
assert.equal(specs[0].name, 'every[foo === 1]')
assert.deepEqual(Object.keys(specs[0]), ['name', '1s'])
}
})

it('handles empty string', function () {
let specs = htmx.__parseTriggerSpecs('')
assert.equal(specs.length, 0)
Expand Down