| 1 |
describe('swap() unit tests', function() { |
| 2 |
|
| 3 |
|
| 4 |
it('prepend normalizes to afterbegin', function () { |
| 5 |
assert.equal(htmx.__parseSwapSpec('prepend').style, 'afterbegin') |
| 6 |
}) |
| 7 |
|
| 8 |
it('append normalizes to beforeend', function () { |
| 9 |
assert.equal(htmx.__parseSwapSpec('append').style, 'beforeend') |
| 10 |
}) |
| 11 |
|
| 12 |
it('before normalizes to beforebegin', function () { |
| 13 |
assert.equal(htmx.__parseSwapSpec('before').style, 'beforebegin') |
| 14 |
}) |
| 15 |
|
| 16 |
it('after normalizes to afterend', function () { |
| 17 |
assert.equal(htmx.__parseSwapSpec('after').style, 'afterend') |
| 18 |
}) |
| 19 |
|
| 20 |
it('new terminology works with modifiers', function () { |
| 21 |
assert.equal(htmx.__parseSwapSpec('prepend swap:10').style, 'afterbegin') |
| 22 |
assert.equal(htmx.__parseSwapSpec('prepend swap:10').swap, "10") |
| 23 |
assert.equal(htmx.__parseSwapSpec('append swap:20').style, 'beforeend') |
| 24 |
assert.equal(htmx.__parseSwapSpec('append swap:20').swap, "20") |
| 25 |
}) |
| 26 |
|
| 27 |
|
| 28 |
it('swaps in plain content properly', async function () { |
| 29 |
await htmx.swap({"target":"#test-playground", "text":"Hello Swap"}) |
| 30 |
playground().innerText.should.equal("Hello Swap") |
| 31 |
}) |
| 32 |
|
| 33 |
it('swaps in html content properly', async function () { |
| 34 |
await htmx.swap({"target":"#test-playground", "text":"<a>Hello Swap</a>"}) |
| 35 |
let child = playground().children[0]; |
| 36 |
child.tagName.should.equal("A"); |
| 37 |
child.innerText.should.equal("Hello Swap") |
| 38 |
}) |
| 39 |
|
| 40 |
it('initializes htmx content properly', async function () { |
| 41 |
await htmx.swap({"target":"#test-playground", "text":"<a hx-get='/foo'>Hello Swap</a>"}) |
| 42 |
let child = playground().children[0]; |
| 43 |
child.tagName.should.equal("A"); |
| 44 |
child.innerText.should.equal("Hello Swap") |
| 45 |
assert.isNotNull(child._htmx); |
| 46 |
}) |
| 47 |
|
| 48 |
it('swaps in plain content properly w/outerHTML', async function () { |
| 49 |
createProcessedHTML("<div id='d1'></div>") |
| 50 |
await htmx.swap({"target":"#d1", "text":"Hello Swap", "swap" : "outerHTML"}) |
| 51 |
playground().innerText.should.equal("Hello Swap") |
| 52 |
}) |
| 53 |
|
| 54 |
it('swaps in html content properly w/outerHTML', async function () { |
| 55 |
createProcessedHTML("<div id='d1'></div>") |
| 56 |
await htmx.swap({"target":"#d1", "text":"<span>Hello Swap</span>", "swap" : "outerHTML"}) |
| 57 |
let child = playground().children[0]; |
| 58 |
child.tagName.should.equal("SPAN"); |
| 59 |
child.innerText.should.equal("Hello Swap") |
| 60 |
}) |
| 61 |
|
| 62 |
it('replaces target element w/outerHTML', async function () { |
| 63 |
createProcessedHTML("<div id='d1'></div>") |
| 64 |
let original = find('#d1'); |
| 65 |
await htmx.swap({"target":"#d1", "text":"<span id='d1'>Replaced</span>", "swap" : "outerHTML"}) |
| 66 |
let replaced = find('#d1'); |
| 67 |
replaced.should.not.equal(original); |
| 68 |
replaced.tagName.should.equal("SPAN"); |
| 69 |
}) |
| 70 |
|
| 71 |
it('inserts before target w/beforebegin', async function () { |
| 72 |
createProcessedHTML("<div id='d1'>Target</div>") |
| 73 |
await htmx.swap({"target":"#d1", "text":"<span>Before</span>", "swap" : "beforebegin"}) |
| 74 |
let children = playground().children; |
| 75 |
children[0].tagName.should.equal("SPAN"); |
| 76 |
children[0].innerText.should.equal("Before"); |
| 77 |
children[1].innerText.should.equal("Target"); |
| 78 |
}) |
| 79 |
|
| 80 |
it('inserts plain text before target w/beforebegin', async function () { |
| 81 |
createProcessedHTML("<div id='d1'>Target</div>") |
| 82 |
await htmx.swap({"target":"#d1", "text":"Before", "swap" : "beforebegin"}) |
| 83 |
playground().childNodes[0].textContent.should.equal("Before"); |
| 84 |
find('#d1').innerText.should.equal("Target"); |
| 85 |
}) |
| 86 |
|
| 87 |
it('prepends content inside target w/afterbegin', async function () { |
| 88 |
createProcessedHTML("<div id='d1'><span>Existing</span></div>") |
| 89 |
await htmx.swap({"target":"#d1", "text":"<span>First</span>", "swap" : "afterbegin"}) |
| 90 |
let children = find('#d1').children; |
| 91 |
children[0].innerText.should.equal("First"); |
| 92 |
children[1].innerText.should.equal("Existing"); |
| 93 |
}) |
| 94 |
|
| 95 |
it('prepends plain text inside target w/afterbegin', async function () { |
| 96 |
createProcessedHTML("<div id='d1'>Existing</div>") |
| 97 |
await htmx.swap({"target":"#d1", "text":"First", "swap" : "afterbegin"}) |
| 98 |
find('#d1').childNodes[0].textContent.should.equal("First"); |
| 99 |
}) |
| 100 |
|
| 101 |
it('appends content inside target w/beforeend', async function () { |
| 102 |
createProcessedHTML("<div id='d1'><span>Existing</span></div>") |
| 103 |
await htmx.swap({"target":"#d1", "text":"<span>Last</span>", "swap" : "beforeend"}) |
| 104 |
let children = find('#d1').children; |
| 105 |
children[0].innerText.should.equal("Existing"); |
| 106 |
children[1].innerText.should.equal("Last"); |
| 107 |
}) |
| 108 |
|
| 109 |
it('appends plain text inside target w/beforeend', async function () { |
| 110 |
createProcessedHTML("<div id='d1'>Existing</div>") |
| 111 |
await htmx.swap({"target":"#d1", "text":"Last", "swap" : "beforeend"}) |
| 112 |
let target = find('#d1'); |
| 113 |
target.childNodes[target.childNodes.length - 1].textContent.should.equal("Last"); |
| 114 |
}) |
| 115 |
|
| 116 |
it('inserts after target w/afterend', async function () { |
| 117 |
createProcessedHTML("<div id='d1'>Target</div>") |
| 118 |
await htmx.swap({"target":"#d1", "text":"<span>After</span>", "swap" : "afterend"}) |
| 119 |
let children = playground().children; |
| 120 |
children[0].innerText.should.equal("Target"); |
| 121 |
children[1].tagName.should.equal("SPAN"); |
| 122 |
children[1].innerText.should.equal("After"); |
| 123 |
}) |
| 124 |
|
| 125 |
it('inserts plain text after target w/afterend', async function () { |
| 126 |
createProcessedHTML("<div id='d1'>Target</div>") |
| 127 |
await htmx.swap({"target":"#d1", "text":"After", "swap" : "afterend"}) |
| 128 |
find('#d1').innerText.should.equal("Target"); |
| 129 |
playground().childNodes[1].textContent.should.equal("After"); |
| 130 |
}) |
| 131 |
|
| 132 |
it('executes script w/innerHTML', async function () { |
| 133 |
window.testVar = 0; |
| 134 |
await htmx.swap({"target":"#test-playground", "text":"<script>window.testVar = 1</script>"}) |
| 135 |
window.testVar.should.equal(1); |
| 136 |
delete window.testVar; |
| 137 |
}) |
| 138 |
|
| 139 |
it('executes script w/outerHTML', async function () { |
| 140 |
window.testVar = 0; |
| 141 |
createProcessedHTML("<div id='d1'></div>") |
| 142 |
await htmx.swap({"target":"#d1", "text":"<div><script>window.testVar = 2</script></div>", "swap" : "outerHTML"}) |
| 143 |
window.testVar.should.equal(2); |
| 144 |
delete window.testVar; |
| 145 |
}) |
| 146 |
|
| 147 |
it('executes script w/beforebegin', async function () { |
| 148 |
window.testVar = 0; |
| 149 |
createProcessedHTML("<div id='d1'></div>") |
| 150 |
await htmx.swap({"target":"#d1", "text":"<script>window.testVar = 3</script>", "swap" : "beforebegin"}) |
| 151 |
window.testVar.should.equal(3); |
| 152 |
delete window.testVar; |
| 153 |
}) |
| 154 |
|
| 155 |
it('executes script w/afterbegin', async function () { |
| 156 |
window.testVar = 0; |
| 157 |
createProcessedHTML("<div id='d1'></div>") |
| 158 |
await htmx.swap({"target":"#d1", "text":"<script>window.testVar = 4</script>", "swap" : "afterbegin"}) |
| 159 |
window.testVar.should.equal(4); |
| 160 |
delete window.testVar; |
| 161 |
}) |
| 162 |
|
| 163 |
it('executes script w/beforeend', async function () { |
| 164 |
window.testVar = 0; |
| 165 |
createProcessedHTML("<div id='d1'></div>") |
| 166 |
await htmx.swap({"target":"#d1", "text":"<script>window.testVar = 5</script>", "swap" : "beforeend"}) |
| 167 |
window.testVar.should.equal(5); |
| 168 |
delete window.testVar; |
| 169 |
}) |
| 170 |
|
| 171 |
it('executes script w/afterend', async function () { |
| 172 |
window.testVar = 0; |
| 173 |
createProcessedHTML("<div id='d1'></div>") |
| 174 |
await htmx.swap({"target":"#d1", "text":"<script>window.testVar = 6</script>", "swap" : "afterend"}) |
| 175 |
window.testVar.should.equal(6); |
| 176 |
delete window.testVar; |
| 177 |
}) |
| 178 |
|
| 179 |
it('swaps oob content', async function () { |
| 180 |
createProcessedHTML("<div id='d1'></div><div id='d2'></div>") |
| 181 |
await htmx.swap({"target":"#d1", "text":"<div>Main</div><div id='d2' hx-swap-oob='true'>OOB</div>"}) |
| 182 |
find('#d1').innerText.trim().should.equal("Main"); |
| 183 |
find('#d2').innerText.should.equal("OOB"); |
| 184 |
}) |
| 185 |
|
| 186 |
it('swaps oob with outerHTML', async function () { |
| 187 |
createProcessedHTML("<div id='d1'></div><div id='d2'></div>") |
| 188 |
await htmx.swap({"target":"#d1", "text":"<div>Main</div><div id='d2' hx-swap-oob='outerHTML'>OOB</div>"}) |
| 189 |
find('#d2').innerText.should.equal("OOB"); |
| 190 |
}) |
| 191 |
|
| 192 |
it('swaps oob with innerHTML', async function () { |
| 193 |
createProcessedHTML("<div id='d1'></div><div id='d2'><span>Old</span></div>") |
| 194 |
await htmx.swap({"target":"#d1", "text":"<div>Main</div><div id='d2' hx-swap-oob='innerHTML'>OOB</div>"}) |
| 195 |
find('#d2').innerText.should.equal("OOB"); |
| 196 |
find('#d2').tagName.should.equal("DIV"); |
| 197 |
}) |
| 198 |
|
| 199 |
it('swaps partial with default target', async function () { |
| 200 |
await htmx.swap({"target":"#test-playground", "text":"<hx-partial hx-target='#test-playground'>Partial</hx-partial>"}) |
| 201 |
playground().innerText.should.equal("Partial"); |
| 202 |
}) |
| 203 |
|
| 204 |
it('swaps partial with custom target', async function () { |
| 205 |
createProcessedHTML("<div id='d1'></div><div id='d2'></div>") |
| 206 |
await htmx.swap({"target":"#d1", "text":"<hx-partial hx-target='#d2'>Partial</hx-partial>"}) |
| 207 |
find('#d2').innerText.should.equal("Partial"); |
| 208 |
}) |
| 209 |
|
| 210 |
it('swaps partial with custom swap style', async function () { |
| 211 |
createProcessedHTML("<div id='d1'>Existing</div>") |
| 212 |
await htmx.swap({"target":"#test-playground", "text":"<hx-partial hx-target='#d1' hx-swap='beforeend'>Partial</hx-partial>"}) |
| 213 |
find('#d1').innerText.should.equal("ExistingPartial"); |
| 214 |
}) |
| 215 |
|
| 216 |
it('executes script in oob swap', async function () { |
| 217 |
window.testVar = 0; |
| 218 |
createProcessedHTML("<div id='d1'></div><div id='d2'></div>") |
| 219 |
await htmx.swap({"target":"#d1", "text":"<div>Main</div><div id='d2' hx-swap-oob='true'><script>window.testVar = 7</script></div>"}) |
| 220 |
window.testVar.should.equal(7); |
| 221 |
delete window.testVar; |
| 222 |
}) |
| 223 |
|
| 224 |
it('executes script in partial', async function () { |
| 225 |
window.testVar = 0; |
| 226 |
await htmx.swap({"target":"#test-playground", "text":"<hx-partial hx-target='#test-playground'><script>window.testVar = 8</script></hx-partial>"}) |
| 227 |
window.testVar.should.equal(8); |
| 228 |
delete window.testVar; |
| 229 |
}) |
| 230 |
|
| 231 |
it('executes script when wrapped in html tag', async function () { |
| 232 |
window.testVar = 0; |
| 233 |
await htmx.swap({"target":"#test-playground", "text":"<html><body><script>window.testVar = 9</script><div>Content</div></body></html>"}) |
| 234 |
window.testVar.should.equal(9); |
| 235 |
delete window.testVar; |
| 236 |
}) |
| 237 |
|
| 238 |
it('executes script when wrapped in body tag', async function () { |
| 239 |
window.testVar = 0; |
| 240 |
await htmx.swap({"target":"#test-playground", "text":"<body><script>window.testVar = 10</script><div>Content</div></body>"}) |
| 241 |
window.testVar.should.equal(10); |
| 242 |
delete window.testVar; |
| 243 |
}) |
| 244 |
|
| 245 |
it('replaces attributes when swapping element with same id', async function () { |
| 246 |
createProcessedHTML("<div id='d1' class='old' data-value='1'></div>") |
| 247 |
await htmx.swap({"target":"#d1", "text":"<div id='d1' class='new' data-value='2'>Content</div>", "swap":"outerHTML"}) |
| 248 |
let replaced = find('#d1'); |
| 249 |
replaced.getAttribute('class').should.equal('new'); |
| 250 |
replaced.getAttribute('data-value').should.equal('2'); |
| 251 |
}) |
| 252 |
|
| 253 |
it('triggers CSS transitions during swap', async function () { |
| 254 |
this.skip(); |
| 255 |
createProcessedHTML("<style>#d1 { transition: opacity 100ms; }</style><div id='d1' style='opacity: 1;'>Old</div>") |
| 256 |
let transitioned = false; |
| 257 |
htmx.on('transitionstart', () => { |
| 258 |
transitioned = true; |
| 259 |
}); |
| 260 |
await htmx.swap({"target":"#d1", "text":"<div id='d1' style='opacity: 0.5;'>New</div>", "swap":"outerHTML"}) |
| 261 |
await htmx.timeout(50); |
| 262 |
transitioned.should.be.true; |
| 263 |
}) |
| 264 |
|
| 265 |
it('triggers htmx:before:swap event', async function () { |
| 266 |
let triggered = false; |
| 267 |
htmx.on('htmx:before:swap', () => { |
| 268 |
triggered = true; |
| 269 |
}); |
| 270 |
await htmx.swap({"target":"#test-playground", "text":"<div>Content</div>"}) |
| 271 |
triggered.should.be.true; |
| 272 |
}) |
| 273 |
|
| 274 |
it('triggers htmx:after:swap event', async function () { |
| 275 |
let triggered = false; |
| 276 |
htmx.on('htmx:after:swap', () => { |
| 277 |
triggered = true; |
| 278 |
}); |
| 279 |
await htmx.swap({"target":"#test-playground", "text":"<div>Content</div>"}) |
| 280 |
triggered.should.be.true; |
| 281 |
}) |
| 282 |
|
| 283 |
it('triggers htmx:after:settle event', async function () { |
| 284 |
let triggered = false; |
| 285 |
htmx.on('htmx:after:settle', () => { |
| 286 |
triggered = true; |
| 287 |
}); |
| 288 |
await htmx.swap({"target":"#test-playground", "text":"<div>Content</div>"}) |
| 289 |
triggered.should.be.true; |
| 290 |
}) |
| 291 |
|
| 292 |
it('bubbles settle events up from replacement element after outerHTML swap', async function () { |
| 293 |
createProcessedHTML("<div id='d1'>Original</div>") |
| 294 |
let beforeSettleTarget, afterSettleTarget; |
| 295 |
htmx.on('htmx:before:settle', (e) => { beforeSettleTarget = e.target; }); |
| 296 |
htmx.on('htmx:after:settle', (e) => { afterSettleTarget = e.target; }); |
| 297 |
await htmx.swap({"target":"#d1", "text":"<span id='replaced'>Replaced</span>", "swap":"outerHTML"}) |
| 298 |
let replacement = find('#replaced'); |
| 299 |
assert.isNotNull(replacement); |
| 300 |
beforeSettleTarget.should.equal(replacement); |
| 301 |
afterSettleTarget.should.equal(replacement); |
| 302 |
document.body.contains(beforeSettleTarget).should.be.true; |
| 303 |
document.body.contains(afterSettleTarget).should.be.true; |
| 304 |
}) |
| 305 |
|
| 306 |
it('bubbles settle events after outerHTML swap when target has no previous sibling', async function () { |
| 307 |
createProcessedHTML("<div id='d1'>First</div><div id='d2'>Second</div>") |
| 308 |
let settleTarget; |
| 309 |
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; }); |
| 310 |
await htmx.swap({"target":"#d1", "text":"<span id='new-first'>New</span>", "swap":"outerHTML"}) |
| 311 |
let replacement = find('#new-first'); |
| 312 |
assert.isNotNull(replacement); |
| 313 |
settleTarget.should.equal(replacement); |
| 314 |
document.body.contains(settleTarget).should.be.true; |
| 315 |
}) |
| 316 |
|
| 317 |
it('bubbles settle events when outerHTML replacement content starts with a text node', async function () { |
| 318 |
createProcessedHTML("<div id='d1'>Original</div>") |
| 319 |
let settleTarget; |
| 320 |
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; }); |
| 321 |
await htmx.swap({"target":"#d1", "text":"Hello <span id='replaced'>World</span>", "swap":"outerHTML"}) |
| 322 |
playground().innerText.should.contain("Hello") |
| 323 |
playground().innerText.should.contain("World") |
| 324 |
assert.isOk(settleTarget); |
| 325 |
document.body.contains(settleTarget).should.be.true; |
| 326 |
}) |
| 327 |
|
| 328 |
it('bubbles settle events when outerHTML target comes after a text node', async function () { |
| 329 |
playground().innerHTML = "Text before <div id='d1'>Original</div>"; |
| 330 |
htmx.process(playground()); |
| 331 |
let settleTarget; |
| 332 |
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; }); |
| 333 |
await htmx.swap({"target":"#d1", "text":"<span id='replaced'>Replaced</span>", "swap":"outerHTML"}) |
| 334 |
let replacement = find('#replaced'); |
| 335 |
assert.isNotNull(replacement); |
| 336 |
settleTarget.should.equal(replacement); |
| 337 |
document.body.contains(settleTarget).should.be.true; |
| 338 |
}) |
| 339 |
|
| 340 |
it('bubbles settle events when outerHTML swap empties the parent (empty fragment, sole child)', async function () { |
| 341 |
createProcessedHTML("<div id='wrapper'><div id='d1'>Original</div></div>") |
| 342 |
let wrapper = find('#wrapper'); |
| 343 |
let beforeSettleTarget, afterSettleTarget; |
| 344 |
wrapper.addEventListener('htmx:before:settle', (e) => { beforeSettleTarget = e.target; }); |
| 345 |
wrapper.addEventListener('htmx:after:settle', (e) => { afterSettleTarget = e.target; }); |
| 346 |
await htmx.swap({"target":"#d1", "text":"", "swap":"outerHTML"}) |
| 347 |
wrapper.children.length.should.equal(0); |
| 348 |
beforeSettleTarget.should.equal(wrapper); |
| 349 |
afterSettleTarget.should.equal(wrapper); |
| 350 |
}) |
| 351 |
|
| 352 |
it('bubbles settle events when target follows a text node AND replacement starts with a text node', async function () { |
| 353 |
playground().innerHTML = "Before text <div id='d1'>Original</div>"; |
| 354 |
htmx.process(playground()); |
| 355 |
let settleTarget; |
| 356 |
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; }); |
| 357 |
await htmx.swap({"target":"#d1", "text":"inserted text <span id='replaced'>After</span>", "swap":"outerHTML"}) |
| 358 |
assert.isOk(settleTarget); |
| 359 |
document.body.contains(settleTarget).should.be.true; |
| 360 |
playground().innerText.should.contain("Before text inserted text After"); |
| 361 |
}) |
| 362 |
|
| 363 |
it('triggers view transition events with transition:true', async function () { |
| 364 |
if (!document.startViewTransition) { |
| 365 |
this.skip(); |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
let beforeTriggered = false; |
| 370 |
let afterTriggered = false; |
| 371 |
htmx.on('htmx:before:viewTransition', () => { |
| 372 |
beforeTriggered = true; |
| 373 |
}); |
| 374 |
htmx.on('htmx:after:viewTransition', () => { |
| 375 |
afterTriggered = true; |
| 376 |
}); |
| 377 |
|
| 378 |
await htmx.swap({"target":"#test-playground", "text":"<div id='d1'>Content</div>", "swap":"innerHTML transition:true"}) |
| 379 |
beforeTriggered.should.be.true; |
| 380 |
afterTriggered.should.be.true; |
| 381 |
}) |
| 382 |
|
| 383 |
it('sets document title from response', async function () { |
| 384 |
let originalTitle = document.title; |
| 385 |
await htmx.swap({"target":"#test-playground", "text":"<html><head><title>New Title</title></head><body><div>Content</div></body></html>"}) |
| 386 |
document.title.should.equal('New Title'); |
| 387 |
document.title = originalTitle; |
| 388 |
}) |
| 389 |
|
| 390 |
it('ignores title when ignoreTitle:true modifier is set', async function () { |
| 391 |
let originalTitle = document.title; |
| 392 |
await htmx.swap({"target":"#test-playground", "text":"<html><head><title>Ignored Title</title></head><body><div>Content</div></body></html>", "swap":"innerHTML ignoreTitle:true"}) |
| 393 |
document.title.should.equal(originalTitle); |
| 394 |
}) |
| 395 |
|
| 396 |
it('sets title from fragment without html/body tags', async function () { |
| 397 |
let originalTitle = document.title; |
| 398 |
await htmx.swap({"target":"#test-playground", "text":"<title>Fragment Title</title><div>Content</div>"}) |
| 399 |
document.title.should.equal('Fragment Title'); |
| 400 |
document.title = originalTitle; |
| 401 |
}) |
| 402 |
|
| 403 |
it('does not set title when response has no title tag', async function () { |
| 404 |
let originalTitle = document.title; |
| 405 |
await htmx.swap({"target":"#test-playground", "text":"<div>Content without title</div>"}) |
| 406 |
document.title.should.equal(originalTitle); |
| 407 |
}) |
| 408 |
|
| 409 |
it('sets title with oob swap', async function () { |
| 410 |
let originalTitle = document.title; |
| 411 |
createProcessedHTML("<div id='d1'></div><div id='d2'></div>") |
| 412 |
await htmx.swap({"target":"#d1", "text":"<title>OOB Title</title><div>Main</div><div id='d2' hx-swap-oob='true'>OOB</div>"}) |
| 413 |
document.title.should.equal('OOB Title'); |
| 414 |
document.title = originalTitle; |
| 415 |
}) |
| 416 |
|
| 417 |
it('sets title with partial swap', async function () { |
| 418 |
let originalTitle = document.title; |
| 419 |
createProcessedHTML("<div id='d1'></div>") |
| 420 |
await htmx.swap({"target":"#test-playground", "text":"<title>Partial Title</title><hx-partial hx-target='#d1'>Partial Content</hx-partial>"}) |
| 421 |
document.title.should.equal('Partial Title'); |
| 422 |
document.title = originalTitle; |
| 423 |
}) |
| 424 |
|
| 425 |
it('sets title from body tag response', async function () { |
| 426 |
let originalTitle = document.title; |
| 427 |
await htmx.swap({"target":"#test-playground", "text":"<body><title>Body Title</title><div>Content</div></body>"}) |
| 428 |
document.title.should.equal('Body Title'); |
| 429 |
document.title = originalTitle; |
| 430 |
}) |
| 431 |
|
| 432 |
it('decodes HTML entities in title', async function () { |
| 433 |
let originalTitle = document.title; |
| 434 |
await htmx.swap({"target":"#test-playground", "text":"<title></> htmx & friends</title><div>Content</div>"}) |
| 435 |
document.title.should.equal('</> htmx & friends'); |
| 436 |
document.title = originalTitle; |
| 437 |
}) |
| 438 |
|
| 439 |
it('does not swap title tag into page content', async function () { |
| 440 |
await htmx.swap({"target":"#test-playground", "text":"<title>Test Title</title><div id='content'>Main Content</div>"}) |
| 441 |
assert.isNull(playground().querySelector('title')); |
| 442 |
find('#content').innerText.should.equal('Main Content'); |
| 443 |
}) |
| 444 |
|
| 445 |
it('supports autofocus', async function () { |
| 446 |
let originalTitle = document.title; |
| 447 |
await htmx.swap({"target":"#test-playground", "text":"<input id='i1' autofocus>"}) |
| 448 |
document.activeElement.id.should.equal("i1") |
| 449 |
}) |
| 450 |
|
| 451 |
it('swaps both main target and partial target when both are present', async function () { |
| 452 |
createProcessedHTML("<div id='target'>Hello</div><div id='target_oob'>OOB</div>") |
| 453 |
await htmx.swap({ |
| 454 |
"target":"#target", |
| 455 |
"text":"<div>Hello me!</div><hx-partial hx-target='#target_oob' hx-swap='innerHTML'><div>OOB swap!</div></hx-partial>" |
| 456 |
}) |
| 457 |
find('#target').textContent.should.equal("Hello me!"); |
| 458 |
find('#target_oob').textContent.should.equal("OOB swap!"); |
| 459 |
}) |
| 460 |
|
| 461 |
it('swaps only partial target when response contains only partial', async function () { |
| 462 |
createProcessedHTML("<div id='target'>Original</div><div id='target_oob'>OOB Original</div>") |
| 463 |
await htmx.swap({ |
| 464 |
"target":"#target", |
| 465 |
"text":"<hx-partial hx-target='#target_oob' hx-swap='innerHTML'><div>OOB Updated</div></hx-partial>" |
| 466 |
}) |
| 467 |
find('#target').textContent.should.equal("Original"); |
| 468 |
find('#target_oob').textContent.should.equal("OOB Updated"); |
| 469 |
}) |
| 470 |
|
| 471 |
it('does not swap main target when only whitespace and partial present', async function () { |
| 472 |
createProcessedHTML("<div id='target'>Original</div><div id='target_oob'>OOB</div>") |
| 473 |
await htmx.swap({ |
| 474 |
"target":"#target", |
| 475 |
"text":"\n <hx-partial hx-target='#target_oob' hx-swap='innerHTML'><div>OOB swap!</div></hx-partial> \n" |
| 476 |
}) |
| 477 |
find('#target').textContent.should.equal("Original"); |
| 478 |
find('#target_oob').textContent.should.equal("OOB swap!"); |
| 479 |
}) |
| 480 |
|
| 481 |
it('swaps both targets when empty element and partial present', async function () { |
| 482 |
createProcessedHTML("<div id='target'>Original</div><div id='target_oob'>OOB</div>") |
| 483 |
await htmx.swap({ |
| 484 |
"target":"#target", |
| 485 |
"text":"<p></p><hx-partial hx-target='#target_oob' hx-swap='innerHTML'><div>OOB swap!</div></hx-partial>" |
| 486 |
}) |
| 487 |
find('#target').querySelector('p').should.not.be.null; |
| 488 |
find('#target_oob').textContent.should.equal("OOB swap!"); |
| 489 |
}) |
| 490 |
|
| 491 |
it('swaps both targets when plain text and partial present', async function () { |
| 492 |
createProcessedHTML("<div id='target'>Original</div><div id='target_oob'>OOB</div>") |
| 493 |
await htmx.swap({ |
| 494 |
"target":"#target", |
| 495 |
"text":"Hello<hx-partial hx-target='#target_oob' hx-swap='innerHTML'><div>OOB swap!</div></hx-partial>" |
| 496 |
}) |
| 497 |
find('#target').textContent.should.equal("Hello"); |
| 498 |
find('#target_oob').innerText.should.equal("OOB swap!"); |
| 499 |
}) |
| 500 |
|
| 501 |
it('restores focus after innerHTML swap when element has same id', async function () { |
| 502 |
createProcessedHTML("<input id='focused-input' value='test'>") |
| 503 |
let input = find('#focused-input') |
| 504 |
input.focus() |
| 505 |
input.setSelectionRange(2, 2) |
| 506 |
|
| 507 |
await htmx.swap({"target":"#test-playground", "text":"<input id='focused-input' value='test'>"}) |
| 508 |
|
| 509 |
document.activeElement.id.should.equal('focused-input') |
| 510 |
document.activeElement.selectionStart.should.equal(2) |
| 511 |
document.activeElement.selectionEnd.should.equal(2) |
| 512 |
}) |
| 513 |
|
| 514 |
it('restores focus after outerHTML swap when element has same id', async function () { |
| 515 |
createProcessedHTML("<div id='container'><input id='focused-input' value='test'></div>") |
| 516 |
let input = find('#focused-input') |
| 517 |
input.focus() |
| 518 |
input.setSelectionRange(1, 3) |
| 519 |
|
| 520 |
await htmx.swap({"target":"#container", "text":"<div id='container'><input id='focused-input' value='test'></div>", "swap":"outerHTML"}) |
| 521 |
|
| 522 |
document.activeElement.id.should.equal('focused-input') |
| 523 |
document.activeElement.selectionStart.should.equal(1) |
| 524 |
document.activeElement.selectionEnd.should.equal(3) |
| 525 |
}) |
| 526 |
|
| 527 |
it('does not restore focus when focused element has no id', async function () { |
| 528 |
createProcessedHTML("<input value='test'>") |
| 529 |
let input = playground().querySelector('input') |
| 530 |
input.focus() |
| 531 |
|
| 532 |
await htmx.swap({"target":"#test-playground", "text":"<input value='test'>"}) |
| 533 |
|
| 534 |
document.activeElement.should.not.equal(input) |
| 535 |
}) |
| 536 |
|
| 537 |
it('does not restore focus when new content lacks matching id', async function () { |
| 538 |
createProcessedHTML("<input id='focused-input' value='test'>") |
| 539 |
let input = find('#focused-input') |
| 540 |
input.focus() |
| 541 |
|
| 542 |
await htmx.swap({"target":"#test-playground", "text":"<input id='different-input' value='test'>"}) |
| 543 |
|
| 544 |
document.activeElement.id.should.not.equal('focused-input') |
| 545 |
}) |
| 546 |
|
| 547 |
it('does not restore focus for morph swaps', async function () { |
| 548 |
createProcessedHTML("<input id='focused-input' value='test'>") |
| 549 |
let input = find('#focused-input') |
| 550 |
input.focus() |
| 551 |
input.setSelectionRange(2, 2) |
| 552 |
|
| 553 |
await htmx.swap({"target":"#test-playground", "text":"<input id='focused-input' value='test'>", "swap":"innerMorph"}) |
| 554 |
|
| 555 |
|
| 556 |
document.activeElement.should.equal(input) |
| 557 |
}) |
| 558 |
|
| 559 |
it('__insertContent accepts string swapSpec', async function () { |
| 560 |
createProcessedHTML("<div id='d1'>Old</div>") |
| 561 |
let fragment = document.createDocumentFragment() |
| 562 |
fragment.append(document.createTextNode('New')) |
| 563 |
await htmx.__insertContent({target: find('#d1'), swapSpec: 'innerHTML', fragment}) |
| 564 |
find('#d1').innerText.should.equal('New') |
| 565 |
}) |
| 566 |
|
| 567 |
it('outerSync cleans up powered children before replacing them', async function() { |
| 568 |
mockResponse('GET', '/endpoint', 'response') |
| 569 |
createProcessedHTML("<div id='target'><button id='btn' hx-get='/endpoint' hx-swap='innerHTML'>click</button></div>") |
| 570 |
const btn = find('#btn') |
| 571 |
assert.isNotNull(btn._htmx?.initialized, 'child should start initialized') |
| 572 |
|
| 573 |
await htmx.swap({ |
| 574 |
target: '#target', |
| 575 |
swap: 'outerSync', |
| 576 |
text: '<div id="target"><span>replaced</span></div>', |
| 577 |
sourceElement: find('#target') |
| 578 |
}) |
| 579 |
|
| 580 |
assert.isNull(btn.getAttribute('data-htmx-powered'), 'old child should be cleaned up') |
| 581 |
assert.isNotOk(find('#btn'), 'old button should be gone') |
| 582 |
assert.equal(find('#target').textContent, 'replaced') |
| 583 |
}) |
| 584 |
|
| 585 |
it('outerSync adds hx-get to root element and initializes it', async function () { |
| 586 |
createProcessedHTML("<div id='target'><p>static</p></div>") |
| 587 |
mockResponse('GET', '/dynamic', 'fetched') |
| 588 |
|
| 589 |
await htmx.swap({ |
| 590 |
target: '#target', |
| 591 |
swap: 'outerSync', |
| 592 |
text: '<div id="target" hx-get="/dynamic" hx-trigger="click" hx-swap="innerHTML"><p>now interactive</p></div>', |
| 593 |
sourceElement: find('#target') |
| 594 |
}) |
| 595 |
|
| 596 |
let target = find('#target') |
| 597 |
target.getAttribute('hx-get').should.equal('/dynamic') |
| 598 |
assert.isNotNull(target._htmx?.initialized, 'target root should be initialized with htmx') |
| 599 |
target.click() |
| 600 |
await forRequest() |
| 601 |
target.textContent.should.equal('fetched') |
| 602 |
}) |
| 603 |
|
| 604 |
it('outerSync removes hx-get from root element and old handler no longer fires', async function () { |
| 605 |
mockResponse('GET', '/should-not-fire', 'bad') |
| 606 |
createProcessedHTML("<div id='target' hx-get='/should-not-fire' hx-trigger='click' hx-swap='innerHTML'>interactive</div>") |
| 607 |
let target = find('#target') |
| 608 |
assert.isNotNull(target._htmx?.initialized, 'target should start initialized') |
| 609 |
|
| 610 |
|
| 611 |
await htmx.swap({ |
| 612 |
target: '#target', |
| 613 |
swap: 'outerSync', |
| 614 |
text: '<div id="target">no longer interactive</div>', |
| 615 |
sourceElement: target |
| 616 |
}) |
| 617 |
|
| 618 |
target = find('#target') |
| 619 |
target.textContent.should.equal('no longer interactive') |
| 620 |
assert.isNull(target.getAttribute('hx-get'), 'hx-get should be removed') |
| 621 |
|
| 622 |
nt |
| 623 |
let requestFired = false |
| 624 |
let handler = () => { requestFired = true } |
| 625 |
document.addEventListener('htmx:before:request', handler) |
| 626 |
target.click() |
| 627 |
await htmx.timeout(50) |
| 628 |
document.removeEventListener('htmx:before:request', handler) |
| 629 |
assert.isFalse(requestFired, 'no request should fire after hx-get removed') |
| 630 |
}) |
| 631 |
|
| 632 |
it('outerSync changes hx-trigger on root element and new trigger fires', async function () { |
| 633 |
mockResponse('GET', '/endpoint', 'response') |
| 634 |
createProcessedHTML("<div id='target' hx-get='/endpoint' hx-trigger='click' hx-swap='innerHTML'>original</div>") |
| 635 |
|
| 636 |
|
| 637 |
await htmx.swap({ |
| 638 |
target: '#target', |
| 639 |
swap: 'outerSync', |
| 640 |
text: '<div id="target" hx-get="/endpoint" hx-trigger="mousedown" hx-swap="innerHTML">updated</div>', |
| 641 |
sourceElement: find('#target') |
| 642 |
}) |
| 643 |
|
| 644 |
let target = find('#target') |
| 645 |
target.getAttribute('hx-trigger').should.equal('mousedown') |
| 646 |
|
| 647 |
|
| 648 |
let requestFired = false |
| 649 |
let handler = () => { requestFired = true } |
| 650 |
document.addEventListener('htmx:before:request', handler) |
| 651 |
target.click() |
| 652 |
await htmx.timeout(50) |
| 653 |
document.removeEventListener('htmx:before:request', handler) |
| 654 |
assert.isFalse(requestFired, 'click should not trigger after hx-trigger changed to mousedown') |
| 655 |
|
| 656 |
|
| 657 |
target.dispatchEvent(new MouseEvent('mousedown', {bubbles: true})) |
| 658 |
await forRequest() |
| 659 |
target.textContent.should.equal('response') |
| 660 |
}) |
| 661 |
|
| 662 |
it('swaps partial to all elements matching a class selector', async function () { |
| 663 |
createProcessedHTML("<div class='target'>A</div><div class='target'>B</div>") |
| 664 |
await htmx.swap({"target":"#test-playground", "text":"<hx-partial hx-target='.target' hx-swap='innerHTML'>Updated</hx-partial>"}) |
| 665 |
playground().querySelectorAll('.target').forEach(el => el.innerText.should.equal('Updated')) |
| 666 |
}) |
| 667 |
|
| 668 |
it('swapEmpty:false prevents main swap when response is only oob', async function () { |
| 669 |
createProcessedHTML("<div id='target'>Original</div><div id='oob'>OOB</div>") |
| 670 |
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:false", "text":"<div id='oob' hx-swap-oob='true'>Updated</div>"}) |
| 671 |
find('#target').innerText.should.equal('Original'); |
| 672 |
find('#oob').innerText.should.equal('Updated'); |
| 673 |
}) |
| 674 |
|
| 675 |
it('swapEmpty:false prevents main swap when response is only partials', async function () { |
| 676 |
createProcessedHTML("<div id='target'>Original</div><div id='partial'>Partial</div>") |
| 677 |
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:false", "text":"<hx-partial hx-target='#partial'>Updated</hx-partial>"}) |
| 678 |
find('#target').innerText.should.equal('Original'); |
| 679 |
find('#partial').innerText.should.equal('Updated'); |
| 680 |
}) |
| 681 |
|
| 682 |
it('swapEmpty:true forces main swap even on empty response with partials', async function () { |
| 683 |
createProcessedHTML("<div id='target'>Original</div><div id='partial'>Partial</div>") |
| 684 |
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:true", "text":"<hx-partial hx-target='#partial'>Updated</hx-partial>"}) |
| 685 |
find('#target').innerText.should.equal(''); |
| 686 |
find('#partial').innerText.should.equal('Updated'); |
| 687 |
}) |
| 688 |
|
| 689 |
it('swapEmpty:false still swaps main target when response has real content alongside oob', async function () { |
| 690 |
createProcessedHTML("<div id='target'>Original</div><div id='oob'>OOB</div>") |
| 691 |
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:false", "text":"<div>New Content</div><div id='oob' hx-swap-oob='true'>Updated</div>"}) |
| 692 |
find('#target').innerText.trim().should.equal('New Content'); |
| 693 |
find('#oob').innerText.should.equal('Updated'); |
| 694 |
}) |
| 695 |
|
| 696 |
it('restores focus to textarea after innerHTML swap', async function () { |
| 697 |
createProcessedHTML("<textarea id='focused-textarea'>hello world</textarea>") |
| 698 |
let textarea = find('#focused-textarea') |
| 699 |
textarea.focus() |
| 700 |
textarea.setSelectionRange(6, 11) |
| 701 |
|
| 702 |
await htmx.swap({"target":"#test-playground", "text":"<textarea id='focused-textarea'>hello world</textarea>"}) |
| 703 |
|
| 704 |
document.activeElement.id.should.equal('focused-textarea') |
| 705 |
document.activeElement.selectionStart.should.equal(6) |
| 706 |
document.activeElement.selectionEnd.should.equal(11) |
| 707 |
}) |
| 708 |
|
| 709 |
}) |