| 1 |
describe('Morph Swap Styles Tests', function() { |
| 2 |
|
| 3 |
beforeEach(function() { |
| 4 |
setupTest(); |
| 5 |
}); |
| 6 |
|
| 7 |
afterEach(function() { |
| 8 |
cleanupTest(); |
| 9 |
}); |
| 10 |
|
| 11 |
describe('innerMorph', function() { |
| 12 |
it('morphs children while preserving element with matching id', async function() { |
| 13 |
mockResponse('GET', '/test', '<div id="child1">updated</div><div id="child2">new</div>'); |
| 14 |
const div = createProcessedHTML('<div id="target"><div id="child1">original</div></div>'); |
| 15 |
const child1 = div.querySelector('#child1'); |
| 16 |
|
| 17 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 18 |
|
| 19 |
assert.equal(div.querySelector('#child1'), child1, 'Element with id should be preserved'); |
| 20 |
assert.equal(child1.textContent, 'updated'); |
| 21 |
assert.isNotNull(div.querySelector('#child2')); |
| 22 |
}); |
| 23 |
|
| 24 |
it('morphs text content', async function() { |
| 25 |
mockResponse('GET', '/test', '<p>new text</p>'); |
| 26 |
const div = createProcessedHTML('<div id="target"><p>old text</p></div>'); |
| 27 |
|
| 28 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 29 |
|
| 30 |
assert.include(div.innerHTML, 'new text'); |
| 31 |
assert.notInclude(div.innerHTML, 'old text'); |
| 32 |
}); |
| 33 |
|
| 34 |
it('morphs attributes', async function() { |
| 35 |
mockResponse('GET', '/test', '<div id="child" class="new-class" data-value="123">content</div>'); |
| 36 |
const div = createProcessedHTML('<div id="target"><div id="child" class="old-class">content</div></div>'); |
| 37 |
const child = div.querySelector('#child'); |
| 38 |
|
| 39 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 40 |
|
| 41 |
assert.equal(child.className, 'new-class'); |
| 42 |
assert.equal(child.getAttribute('data-value'), '123'); |
| 43 |
}); |
| 44 |
|
| 45 |
it('removes old elements not in new content', async function() { |
| 46 |
mockResponse('GET', '/test', '<div id="keep">kept</div>'); |
| 47 |
const div = createProcessedHTML('<div id="target"><div id="keep">kept</div><div id="remove">removed</div></div>'); |
| 48 |
|
| 49 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 50 |
|
| 51 |
assert.isNotNull(div.querySelector('#keep')); |
| 52 |
assert.isNull(div.querySelector('#remove')); |
| 53 |
}); |
| 54 |
|
| 55 |
it('adds new elements', async function() { |
| 56 |
mockResponse('GET', '/test', '<div id="old">old</div><div id="new">new</div>'); |
| 57 |
const div = createProcessedHTML('<div id="target"><div id="old">old</div></div>'); |
| 58 |
|
| 59 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 60 |
|
| 61 |
assert.isNotNull(div.querySelector('#old')); |
| 62 |
assert.isNotNull(div.querySelector('#new')); |
| 63 |
}); |
| 64 |
|
| 65 |
it('preserves element references with matching ids', async function() { |
| 66 |
mockResponse('GET', '/test', '<input id="input1" value="new"><input id="input2" value="added">'); |
| 67 |
const div = createProcessedHTML('<div id="target"><input id="input1" value="old"></div>'); |
| 68 |
const input1 = div.querySelector('#input1'); |
| 69 |
|
| 70 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 71 |
|
| 72 |
assert.equal(div.querySelector('#input1'), input1, 'Input element should be same reference'); |
| 73 |
assert.equal(input1.value, 'new'); |
| 74 |
assert.isNotNull(div.querySelector('#input2')); |
| 75 |
}); |
| 76 |
|
| 77 |
it('handles nested elements with ids', async function() { |
| 78 |
mockResponse('GET', '/test', '<div id="outer"><div id="inner">updated</div></div>'); |
| 79 |
const div = createProcessedHTML('<div id="target"><div id="outer"><div id="inner">original</div></div></div>'); |
| 80 |
const outer = div.querySelector('#outer'); |
| 81 |
const inner = div.querySelector('#inner'); |
| 82 |
|
| 83 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 84 |
|
| 85 |
assert.equal(div.querySelector('#outer'), outer); |
| 86 |
assert.equal(div.querySelector('#inner'), inner); |
| 87 |
assert.equal(inner.textContent, 'updated'); |
| 88 |
}); |
| 89 |
|
| 90 |
it('morphs without ids using tag matching', async function() { |
| 91 |
mockResponse('GET', '/test', '<p>new paragraph</p><span>new span</span>'); |
| 92 |
const div = createProcessedHTML('<div id="target"><p>old paragraph</p><span>old span</span></div>'); |
| 93 |
|
| 94 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 95 |
|
| 96 |
assert.include(div.innerHTML, 'new paragraph'); |
| 97 |
assert.include(div.innerHTML, 'new span'); |
| 98 |
}); |
| 99 |
}); |
| 100 |
|
| 101 |
describe('outerMorph', function() { |
| 102 |
it('morphs the target element itself', async function() { |
| 103 |
mockResponse('GET', '/test', '<div id="target" class="new-class">updated</div>'); |
| 104 |
const container = createProcessedHTML('<div><div id="target" class="old-class">original</div></div>'); |
| 105 |
const target = container.querySelector('#target'); |
| 106 |
|
| 107 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 108 |
|
| 109 |
const newTarget = container.querySelector('#target'); |
| 110 |
assert.equal(newTarget, target, 'Target element should be morphed, not replaced'); |
| 111 |
assert.equal(newTarget.className, 'new-class'); |
| 112 |
assert.equal(newTarget.textContent, 'updated'); |
| 113 |
}); |
| 114 |
|
| 115 |
it('morphs target attributes', async function() { |
| 116 |
mockResponse('GET', '/test', '<div id="target" data-value="123" class="morphed">content</div>'); |
| 117 |
const container = createProcessedHTML('<div><div id="target" class="original">content</div></div>'); |
| 118 |
const target = container.querySelector('#target'); |
| 119 |
|
| 120 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 121 |
|
| 122 |
assert.equal(container.querySelector('#target'), target); |
| 123 |
assert.equal(target.className, 'morphed'); |
| 124 |
assert.equal(target.getAttribute('data-value'), '123'); |
| 125 |
}); |
| 126 |
|
| 127 |
it('morphs target children', async function() { |
| 128 |
mockResponse('GET', '/test', '<div id="target"><span id="child">new child</span></div>'); |
| 129 |
const container = createProcessedHTML('<div><div id="target"><span id="child">old child</span></div></div>'); |
| 130 |
const target = container.querySelector('#target'); |
| 131 |
const child = target.querySelector('#child'); |
| 132 |
|
| 133 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 134 |
|
| 135 |
assert.equal(container.querySelector('#target'), target); |
| 136 |
assert.equal(target.querySelector('#child'), child); |
| 137 |
assert.equal(child.textContent, 'new child'); |
| 138 |
}); |
| 139 |
|
| 140 |
it('preserves element identity during morph', async function() { |
| 141 |
mockResponse('GET', '/test', '<button id="btn" class="updated">Click Me</button>'); |
| 142 |
const container = createProcessedHTML('<div><button id="btn" class="original">Click</button></div>'); |
| 143 |
const btn = container.querySelector('#btn'); |
| 144 |
let clicked = false; |
| 145 |
btn.addEventListener('click', () => clicked = true); |
| 146 |
|
| 147 |
await htmx.ajax('GET', '/test', {target: '#btn', swap: 'outerMorph'}); |
| 148 |
|
| 149 |
const newBtn = container.querySelector('#btn'); |
| 150 |
assert.equal(newBtn, btn, 'Button should be same element'); |
| 151 |
newBtn.click(); |
| 152 |
assert.isTrue(clicked, 'Event listener should still work'); |
| 153 |
}); |
| 154 |
}); |
| 155 |
|
| 156 |
describe('innerMorph vs innerHTML comparison', function() { |
| 157 |
it('innerMorph preserves elements, innerHTML replaces', async function() { |
| 158 |
mockResponse('GET', '/test', '<input id="field" value="new">'); |
| 159 |
|
| 160 |
|
| 161 |
const div1 = createProcessedHTML('<div id="target1"><input id="field" value="old"></div>'); |
| 162 |
const input1 = div1.querySelector('#field'); |
| 163 |
await htmx.ajax('GET', '/test', {target: '#target1', swap: 'innerHTML'}); |
| 164 |
const newInput1 = div1.querySelector('#field'); |
| 165 |
assert.notEqual(newInput1, input1, 'innerHTML should create new element'); |
| 166 |
|
| 167 |
|
| 168 |
mockResponse('GET', '/test', '<input id="field" value="new">'); |
| 169 |
const div2 = createProcessedHTML('<div id="target2"><input id="field" value="old"></div>'); |
| 170 |
const input2 = div2.querySelector('#field'); |
| 171 |
await htmx.ajax('GET', '/test', {target: '#target2', swap: 'innerMorph'}); |
| 172 |
const newInput2 = div2.querySelector('#field'); |
| 173 |
assert.equal(newInput2, input2, 'innerMorph should preserve element'); |
| 174 |
}); |
| 175 |
}); |
| 176 |
|
| 177 |
describe('outerMorph vs outerHTML comparison', function() { |
| 178 |
it('outerMorph preserves target, outerHTML replaces', async function() { |
| 179 |
mockResponse('GET', '/test', '<div id="target" class="new">updated</div>'); |
| 180 |
|
| 181 |
|
| 182 |
const container1 = createProcessedHTML('<div><div id="target" class="old">original</div></div>'); |
| 183 |
const target1 = container1.querySelector('#target'); |
| 184 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerHTML'}); |
| 185 |
const newTarget1 = container1.querySelector('#target'); |
| 186 |
assert.notEqual(newTarget1, target1, 'outerHTML should create new element'); |
| 187 |
|
| 188 |
|
| 189 |
mockResponse('GET', '/test', '<div id="target" class="new">updated</div>'); |
| 190 |
const container2 = createProcessedHTML('<div><div id="target" class="old">original</div></div>'); |
| 191 |
const target2 = container2.querySelector('#target'); |
| 192 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 193 |
const newTarget2 = container2.querySelector('#target'); |
| 194 |
assert.equal(newTarget2, target2, 'outerMorph should preserve element'); |
| 195 |
}); |
| 196 |
}); |
| 197 |
|
| 198 |
describe('edge cases', function() { |
| 199 |
it('handles empty content', async function() { |
| 200 |
mockResponse('GET', '/test', ''); |
| 201 |
const div = createProcessedHTML('<div id="target"><p>content</p></div>'); |
| 202 |
|
| 203 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 204 |
|
| 205 |
assert.equal(div.innerHTML, ''); |
| 206 |
}); |
| 207 |
|
| 208 |
it('handles complex nested structures', async function() { |
| 209 |
mockResponse('GET', '/test', |
| 210 |
'<div id="a"><div id="b"><div id="c">updated</div></div></div>'); |
| 211 |
const div = createProcessedHTML( |
| 212 |
'<div id="target"><div id="a"><div id="b"><div id="c">original</div></div></div></div>'); |
| 213 |
const a = div.querySelector('#a'); |
| 214 |
const b = div.querySelector('#b'); |
| 215 |
const c = div.querySelector('#c'); |
| 216 |
|
| 217 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 218 |
|
| 219 |
assert.equal(div.querySelector('#a'), a); |
| 220 |
assert.equal(div.querySelector('#b'), b); |
| 221 |
assert.equal(div.querySelector('#c'), c); |
| 222 |
assert.equal(c.textContent, 'updated'); |
| 223 |
}); |
| 224 |
|
| 225 |
it('handles mixed content with and without ids', async function() { |
| 226 |
mockResponse('GET', '/test', |
| 227 |
'<div id="with-id">has id</div><div>no id</div><span id="another">another</span>'); |
| 228 |
const div = createProcessedHTML( |
| 229 |
'<div id="target"><div id="with-id">old</div><p>paragraph</p></div>'); |
| 230 |
const withId = div.querySelector('#with-id'); |
| 231 |
|
| 232 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 233 |
|
| 234 |
assert.equal(div.querySelector('#with-id'), withId); |
| 235 |
assert.equal(withId.textContent, 'has id'); |
| 236 |
assert.isNotNull(div.querySelector('#another')); |
| 237 |
}); |
| 238 |
|
| 239 |
it('handles numeric ids', async function() { |
| 240 |
mockResponse('GET', '/test', '<div><hr id="1"></div>'); |
| 241 |
const div = createProcessedHTML('<div id="target"><hr id="1"></div>'); |
| 242 |
const hr = div.querySelector('#\\31'); |
| 243 |
|
| 244 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 245 |
|
| 246 |
assert.equal(div.querySelector('#\\31'), hr); |
| 247 |
}); |
| 248 |
|
| 249 |
it('does not re-execute identical script tags during morph', async function() { |
| 250 |
window._scriptMorphCount = 0; |
| 251 |
createProcessedHTML('<div id="target"></div>'); |
| 252 |
await htmx.swap({target: '#target', text: '<script>window._scriptMorphCount++<\/script><div id="content">original</div>', swap: 'innerHTML'}); |
| 253 |
assert.equal(window._scriptMorphCount, 1, 'Script should run once after initial swap'); |
| 254 |
|
| 255 |
await htmx.swap({target: '#target', text: '<script>window._scriptMorphCount++<\/script><div id="content">updated</div>', swap: 'innerMorph'}); |
| 256 |
|
| 257 |
assert.equal(window._scriptMorphCount, 1, 'Identical script should not re-execute after morph'); |
| 258 |
delete window._scriptMorphCount; |
| 259 |
}); |
| 260 |
|
| 261 |
it('executes changed script tags once when replaced during morph', async function() { |
| 262 |
window._scriptMorphCount = 0; |
| 263 |
createProcessedHTML('<div id="target"></div>'); |
| 264 |
await htmx.swap({target: '#target', text: '<script>window._scriptMorphCount++<\/script>', swap: 'innerHTML'}); |
| 265 |
assert.equal(window._scriptMorphCount, 1, 'Original script should run once after initial swap'); |
| 266 |
|
| 267 |
await htmx.swap({target: '#target', text: '<script>window._scriptMorphCount += 10<\/script>', swap: 'innerMorph'}); |
| 268 |
|
| 269 |
assert.equal(window._scriptMorphCount, 11, 'Changed script should execute exactly once after morph'); |
| 270 |
delete window._scriptMorphCount; |
| 271 |
}); |
| 272 |
|
| 273 |
it('does not treat empty id="" as a persistent id', async function() { |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
mockResponse('GET', '/test', '<h1 id="">B</h1><div>Y</div>'); |
| 279 |
const div = createProcessedHTML('<div id="target"><h1 id="">A</h1><section>X</section></div>'); |
| 280 |
|
| 281 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 282 |
|
| 283 |
assert.equal(div.querySelector('h1').textContent, 'B'); |
| 284 |
assert.isNotNull(div.querySelector('div')); |
| 285 |
assert.isNull(div.querySelector('section')); |
| 286 |
}); |
| 287 |
}); |
| 288 |
|
| 289 |
describe('input value preservation', function() { |
| 290 |
it('preserves input value when attribute unchanged', async function() { |
| 291 |
mockResponse('GET', '/test', '<input id="field" value="old" class="updated">'); |
| 292 |
const div = createProcessedHTML('<div id="target"><input id="field" value="old"></div>'); |
| 293 |
const input = div.querySelector('#field'); |
| 294 |
input.value = 'user-typed'; |
| 295 |
|
| 296 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 297 |
|
| 298 |
assert.equal(input.value, 'user-typed', 'Preserves user input when value attribute unchanged'); |
| 299 |
assert.equal(input.className, 'updated'); |
| 300 |
}); |
| 301 |
|
| 302 |
it('updates input value when attribute changes', async function() { |
| 303 |
mockResponse('GET', '/test', '<input id="field" value="new">'); |
| 304 |
const div = createProcessedHTML('<div id="target"><input id="field" value="old"></div>'); |
| 305 |
const input = div.querySelector('#field'); |
| 306 |
input.value = 'user-typed'; |
| 307 |
|
| 308 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 309 |
|
| 310 |
assert.equal(input.value, 'new', 'Updates value when attribute changes'); |
| 311 |
}); |
| 312 |
|
| 313 |
it('preserves textarea value when content unchanged', async function() { |
| 314 |
mockResponse('GET', '/test', '<textarea id="field" class="updated">old</textarea>'); |
| 315 |
const div = createProcessedHTML('<div id="target"><textarea id="field">old</textarea></div>'); |
| 316 |
const textarea = div.querySelector('#field'); |
| 317 |
textarea.value = 'user-typed'; |
| 318 |
|
| 319 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 320 |
|
| 321 |
assert.equal(textarea.value, 'user-typed', 'Preserves user input when content unchanged'); |
| 322 |
assert.equal(textarea.className, 'updated'); |
| 323 |
}); |
| 324 |
|
| 325 |
it('updates textarea value when content changes', async function() { |
| 326 |
mockResponse('GET', '/test', '<textarea id="field">new</textarea>'); |
| 327 |
const div = createProcessedHTML('<div id="target"><textarea id="field">old</textarea></div>'); |
| 328 |
const textarea = div.querySelector('#field'); |
| 329 |
textarea.value = 'user-typed'; |
| 330 |
|
| 331 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 332 |
|
| 333 |
assert.equal(textarea.value, 'new', 'Updates value when content changes'); |
| 334 |
}); |
| 335 |
|
| 336 |
it('preserves checkbox state when attribute unchanged', async function() { |
| 337 |
mockResponse('GET', '/test', '<input id="cb" type="checkbox" checked class="updated">'); |
| 338 |
const div = createProcessedHTML('<div id="target"><input id="cb" type="checkbox" checked></div>'); |
| 339 |
const checkbox = div.querySelector('#cb'); |
| 340 |
checkbox.checked = false; |
| 341 |
|
| 342 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 343 |
|
| 344 |
assert.equal(checkbox.checked, false, 'Preserves user state when checked attribute unchanged'); |
| 345 |
assert.equal(checkbox.className, 'updated'); |
| 346 |
}); |
| 347 |
|
| 348 |
it('preserves focused input node when validation error div is inserted before it', async function() { |
| 349 |
|
| 350 |
|
| 351 |
const div = createProcessedHTML( |
| 352 |
'<div id="target"><input name="q" placeholder="Search"><div class="info">hint</div></div>' |
| 353 |
); |
| 354 |
const input = div.querySelector('input[name=q]'); |
| 355 |
input.value = 'hello'; |
| 356 |
input.focus(); |
| 357 |
|
| 358 |
await htmx.swap({ |
| 359 |
target: '#target', |
| 360 |
text: '<div class="error">Required</div><input name="q" placeholder="Search"><div class="info">hint</div>', |
| 361 |
swap: 'innerMorph', |
| 362 |
sourceElement: div |
| 363 |
}); |
| 364 |
|
| 365 |
assert.isNotNull(div.querySelector('.error'), 'error div should be inserted'); |
| 366 |
assert.equal(div.querySelector('input[name=q]'), input, 'focused input node should be preserved'); |
| 367 |
assert.equal(input.value, 'hello', 'typed value should be preserved'); |
| 368 |
}); |
| 369 |
|
| 370 |
it('does not block morph when a clicked button holds focus but is removed in new content', async function() { |
| 371 |
|
| 372 |
|
| 373 |
const div = createProcessedHTML('<div id="target"><button class="action">Go</button><input name="name"><input name="email"></div>'); |
| 374 |
const btn = div.querySelector('button'); |
| 375 |
const nameInput = div.querySelector('input[name=name]'); |
| 376 |
const emailInput = div.querySelector('input[name=email]'); |
| 377 |
nameInput.value = 'Alice'; |
| 378 |
emailInput.value = '[email protected]'; |
| 379 |
btn.focus(); |
| 380 |
|
| 381 |
await htmx.swap({ |
| 382 |
target: '#target', |
| 383 |
text: '<input name="name"><input name="email">', |
| 384 |
swap: 'innerMorph', |
| 385 |
sourceElement: div |
| 386 |
}); |
| 387 |
|
| 388 |
assert.equal(div.querySelector('input[name=name]'), nameInput, 'name input node should be reused'); |
| 389 |
assert.equal(div.querySelector('input[name=email]'), emailInput, 'email input node should be reused'); |
| 390 |
assert.equal(nameInput.value, 'Alice', 'name value should be preserved'); |
| 391 |
assert.equal(emailInput.value, '[email protected]', 'email value should be preserved'); |
| 392 |
assert.isNull(div.querySelector('button'), 'button should be removed'); |
| 393 |
}); |
| 394 |
|
| 395 |
|
| 396 |
}); |
| 397 |
|
| 398 |
describe('element reordering with ids', function() { |
| 399 |
it('reorders elements with ids correctly', async function() { |
| 400 |
mockResponse('GET', '/test', '<div id="c">C</div><div id="b">B</div><div id="a">A</div>'); |
| 401 |
const div = createProcessedHTML('<div id="target"><div id="a">A</div><div id="b">B</div><div id="c">C</div></div>'); |
| 402 |
const a = div.querySelector('#a'); |
| 403 |
const b = div.querySelector('#b'); |
| 404 |
const c = div.querySelector('#c'); |
| 405 |
|
| 406 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 407 |
|
| 408 |
assert.equal(div.querySelector('#a'), a, 'Element A should be preserved'); |
| 409 |
assert.equal(div.querySelector('#b'), b, 'Element B should be preserved'); |
| 410 |
assert.equal(div.querySelector('#c'), c, 'Element C should be preserved'); |
| 411 |
assert.equal(div.children[0].id, 'c'); |
| 412 |
assert.equal(div.children[1].id, 'b'); |
| 413 |
assert.equal(div.children[2].id, 'a'); |
| 414 |
}); |
| 415 |
|
| 416 |
it('moves id node into nested div correctly', async function() { |
| 417 |
mockResponse('GET', '/test', '<div><input id="first"></div>'); |
| 418 |
const div = createProcessedHTML('<div id="target"><input id="first"></div>'); |
| 419 |
const input = div.querySelector('#first'); |
| 420 |
|
| 421 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 422 |
|
| 423 |
assert.equal(div.querySelector('#first'), input); |
| 424 |
assert.equal(input.parentElement.tagName, 'DIV'); |
| 425 |
}); |
| 426 |
|
| 427 |
it('handles complex id reordering with nesting', async function() { |
| 428 |
mockResponse('GET', '/test', '<br><a id="a"><b id="b"></b></a>'); |
| 429 |
const div = createProcessedHTML('<div id="target"><a id="a"></a><br><b id="b"></b></div>'); |
| 430 |
const a = div.querySelector('#a'); |
| 431 |
const b = div.querySelector('#b'); |
| 432 |
|
| 433 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 434 |
|
| 435 |
assert.equal(div.querySelector('#a'), a); |
| 436 |
assert.equal(div.querySelector('#b'), b); |
| 437 |
assert.equal(b.parentElement, a, 'b should be inside a'); |
| 438 |
}); |
| 439 |
}); |
| 440 |
|
| 441 |
describe('attribute morphing edge cases', function() { |
| 442 |
it('morphs multiple attributes correctly', async function() { |
| 443 |
mockResponse('GET', '/test', |
| 444 |
'<section id="s" class="thing" data-one="1" data-two="2" data-three="3" data-four="4" fizz="buzz" foo="bar">B</section>'); |
| 445 |
const container = createProcessedHTML('<div><section id="s" class="child">A</section></div>'); |
| 446 |
const section = container.querySelector('#s'); |
| 447 |
|
| 448 |
await htmx.ajax('GET', '/test', {target: '#s', swap: 'outerMorph'}); |
| 449 |
|
| 450 |
assert.equal(section.className, 'thing'); |
| 451 |
assert.equal(section.getAttribute('data-one'), '1'); |
| 452 |
assert.equal(section.getAttribute('data-four'), '4'); |
| 453 |
assert.equal(section.getAttribute('fizz'), 'buzz'); |
| 454 |
assert.equal(section.textContent, 'B'); |
| 455 |
}); |
| 456 |
|
| 457 |
it('removes attributes correctly', async function() { |
| 458 |
mockResponse('GET', '/test', '<section id="s" class="child">A</section>'); |
| 459 |
const container = createProcessedHTML( |
| 460 |
'<div><section id="s" class="thing" data-one="1" data-two="2" fizz="buzz">B</section></div>'); |
| 461 |
const section = container.querySelector('#s'); |
| 462 |
|
| 463 |
await htmx.ajax('GET', '/test', {target: '#s', swap: 'outerMorph'}); |
| 464 |
|
| 465 |
assert.equal(section.className, 'child'); |
| 466 |
assert.isNull(section.getAttribute('data-one')); |
| 467 |
assert.isNull(section.getAttribute('fizz')); |
| 468 |
assert.equal(section.textContent, 'A'); |
| 469 |
}); |
| 470 |
|
| 471 |
it('handles fieldset disabled property correctly', async function() { |
| 472 |
mockResponse('GET', '/test', '<fieldset id="fs">hello</fieldset>'); |
| 473 |
const container = createProcessedHTML('<div><fieldset id="fs" class="foo" disabled></fieldset></div>'); |
| 474 |
const fieldset = container.querySelector('#fs'); |
| 475 |
|
| 476 |
await htmx.ajax('GET', '/test', {target: '#fs', swap: 'outerMorph'}); |
| 477 |
|
| 478 |
assert.equal(fieldset.innerHTML, 'hello'); |
| 479 |
assert.equal(fieldset.classList.length, 0); |
| 480 |
assert.equal(fieldset.disabled, false); |
| 481 |
}); |
| 482 |
}); |
| 483 |
|
| 484 |
describe('htmx processing during morph', function() { |
| 485 |
it('processes new htmx attributes added during innerMorph', async function() { |
| 486 |
mockResponse('GET', '/test', '<button id="btn" hx-get="/click" hx-target="#result">Updated</button><div id="result"></div>'); |
| 487 |
const div = createProcessedHTML('<div id="target"><button id="btn">Original</button><div id="result"></div></div>'); |
| 488 |
const btn = div.querySelector('#btn'); |
| 489 |
|
| 490 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 491 |
|
| 492 |
assert.equal(btn.getAttribute('data-htmx-powered'), 'true', 'New htmx attributes should be processed'); |
| 493 |
|
| 494 |
mockResponse('GET', '/click', 'Clicked!'); |
| 495 |
btn.click(); |
| 496 |
await waitForEvent('htmx:after:swap', 100); |
| 497 |
|
| 498 |
assert.equal(div.querySelector('#result').textContent, 'Clicked!', 'New htmx functionality should work'); |
| 499 |
}); |
| 500 |
|
| 501 |
it('processes new htmx attributes added during outerMorph', async function() { |
| 502 |
mockResponse('GET', '/test', '<button id="btn" hx-get="/click" hx-target="#result">Updated</button>'); |
| 503 |
const container = createProcessedHTML('<div><button id="btn">Original</button><div id="result"></div></div>'); |
| 504 |
const btn = container.querySelector('#btn'); |
| 505 |
|
| 506 |
await htmx.ajax('GET', '/test', {target: '#btn', swap: 'outerMorph'}); |
| 507 |
|
| 508 |
assert.equal(btn.getAttribute('data-htmx-powered'), 'true', 'New htmx attributes should be processed'); |
| 509 |
|
| 510 |
mockResponse('GET', '/click', 'Clicked!'); |
| 511 |
btn.click(); |
| 512 |
await waitForEvent('htmx:after:swap', 100); |
| 513 |
|
| 514 |
assert.equal(container.querySelector('#result').textContent, 'Clicked!', 'New htmx functionality should work'); |
| 515 |
}); |
| 516 |
|
| 517 |
it('processes new htmx attributes on inserted elements during innerMorph', async function() { |
| 518 |
mockResponse('GET', '/test', '<button id="existing">Existing</button><button id="new" hx-get="/new">New Button</button>'); |
| 519 |
const div = createProcessedHTML('<div id="target"><button id="existing">Existing</button></div>'); |
| 520 |
|
| 521 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 522 |
|
| 523 |
const newBtn = div.querySelector('#new'); |
| 524 |
assert.isNotNull(newBtn); |
| 525 |
assert.equal(newBtn.getAttribute('data-htmx-powered'), 'true', 'New inserted element should be processed'); |
| 526 |
}); |
| 527 |
|
| 528 |
it('reinitializes element when hx-get is added during innerMorph', async function() { |
| 529 |
mockResponse('GET', '/dynamic', 'fetched'); |
| 530 |
const div = createProcessedHTML('<div id="target"><div id="child"><p>static</p></div></div>'); |
| 531 |
|
| 532 |
await htmx.swap({target: '#target', text: '<div id="child" hx-get="/dynamic" hx-trigger="click" hx-swap="innerHTML"><p>now interactive</p></div>', swap: 'innerMorph', sourceElement: div}); |
| 533 |
|
| 534 |
const child = div.querySelector('#child'); |
| 535 |
assert.isNotNull(child._htmx?.initialized, 'child should be initialized'); |
| 536 |
child.click(); |
| 537 |
await forRequest(); |
| 538 |
assert.equal(child.textContent, 'fetched'); |
| 539 |
}); |
| 540 |
|
| 541 |
it('reinitializes element when hx-get is removed during innerMorph', async function() { |
| 542 |
mockResponse('GET', '/should-not-fire', 'bad'); |
| 543 |
const div = createProcessedHTML('<div id="target"><div id="child" hx-get="/should-not-fire" hx-trigger="click" hx-swap="innerHTML">interactive</div></div>'); |
| 544 |
const child = div.querySelector('#child'); |
| 545 |
assert.isNotNull(child._htmx?.initialized, 'child should start initialized'); |
| 546 |
|
| 547 |
await htmx.swap({target: '#target', text: '<div id="child">no longer interactive</div>', swap: 'innerMorph', sourceElement: div}); |
| 548 |
|
| 549 |
assert.isNull(child.getAttribute('hx-get'), 'hx-get should be removed'); |
| 550 |
let requestFired = false; |
| 551 |
let handler = () => { requestFired = true; }; |
| 552 |
document.addEventListener('htmx:before:request', handler); |
| 553 |
child.click(); |
| 554 |
await htmx.timeout(50); |
| 555 |
document.removeEventListener('htmx:before:request', handler); |
| 556 |
assert.isFalse(requestFired, 'no request should fire after hx-get removed'); |
| 557 |
}); |
| 558 |
|
| 559 |
it('reinitializes element when hx-trigger changes during innerMorph', async function() { |
| 560 |
mockResponse('GET', '/endpoint', 'response'); |
| 561 |
const div = createProcessedHTML('<div id="target"><div id="child" hx-get="/endpoint" hx-trigger="click" hx-swap="innerHTML">original</div></div>'); |
| 562 |
|
| 563 |
await htmx.swap({target: '#target', text: '<div id="child" hx-get="/endpoint" hx-trigger="mousedown" hx-swap="innerHTML">updated</div>', swap: 'innerMorph', sourceElement: div}); |
| 564 |
|
| 565 |
const child = div.querySelector('#child'); |
| 566 |
assert.equal(child.getAttribute('hx-trigger'), 'mousedown'); |
| 567 |
|
| 568 |
let requestFired = false; |
| 569 |
let handler = () => { requestFired = true; }; |
| 570 |
document.addEventListener('htmx:before:request', handler); |
| 571 |
child.click(); |
| 572 |
await htmx.timeout(50); |
| 573 |
document.removeEventListener('htmx:before:request', handler); |
| 574 |
assert.isFalse(requestFired, 'click should not trigger after hx-trigger changed to mousedown'); |
| 575 |
|
| 576 |
child.dispatchEvent(new MouseEvent('mousedown', {bubbles: true})); |
| 577 |
await forRequest(); |
| 578 |
assert.equal(child.textContent, 'response'); |
| 579 |
}); |
| 580 |
|
| 581 |
it('processes new htmx attributes on inserted elements during outerMorph', async function() { |
| 582 |
mockResponse('GET', '/test', '<div id="target"><button id="existing">Existing</button><button id="new" hx-get="/new">New Button</button></div>'); |
| 583 |
const container = createProcessedHTML('<div><div id="target"><button id="existing">Existing</button></div></div>'); |
| 584 |
|
| 585 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 586 |
|
| 587 |
const newBtn = container.querySelector('#new'); |
| 588 |
assert.isNotNull(newBtn); |
| 589 |
assert.equal(newBtn.getAttribute('data-htmx-powered'), 'true', 'New inserted element should be processed'); |
| 590 |
}); |
| 591 |
|
| 592 |
it('does not process detached fragment children during outerMorph', async function() { |
| 593 |
mockResponse('GET', '/test', '<div id="target"><span class="child" hx-get="/x">new</span></div>'); |
| 594 |
const container = createProcessedHTML('<div><div id="target"><span class="child">old</span></div></div>'); |
| 595 |
|
| 596 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 597 |
|
| 598 |
assert.equal(container.querySelector('#target .child').getAttribute('data-htmx-powered'), 'true', |
| 599 |
'live nested child should still be initialized'); |
| 600 |
}); |
| 601 |
|
| 602 |
it('does not attach htmx state to detached fragment children during outerMorph', async function() { |
| 603 |
mockResponse('GET', '/test', '<div id="target"><span class="child" hx-get="/x">new</span></div>'); |
| 604 |
const container = createProcessedHTML('<div><div id="target"><span class="child">old</span></div></div>'); |
| 605 |
|
| 606 |
let fragmentChild; |
| 607 |
document.addEventListener('htmx:before:swap', (e) => { |
| 608 |
const mainTask = (e.detail.tasks || []).find(t => t.type === 'main'); |
| 609 |
fragmentChild = mainTask && mainTask.fragment && mainTask.fragment.querySelector('.child'); |
| 610 |
}, { once: true }); |
| 611 |
|
| 612 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 613 |
|
| 614 |
assert.isOk(fragmentChild, 'captured the fragment child before morph'); |
| 615 |
assert.isFalse(fragmentChild.isConnected, |
| 616 |
'same-tag outerMorph leaves fragment children detached as templates'); |
| 617 |
assert.equal(typeof fragmentChild._htmx, 'undefined', |
| 618 |
'process() should not have attached _htmx state to the detached fragment child'); |
| 619 |
assert.isNull(fragmentChild.getAttribute('data-htmx-powered'), |
| 620 |
'detached fragment child should not be marked data-htmx-powered'); |
| 621 |
}); |
| 622 |
|
| 623 |
it('processes new htmx attributes on tag-changing outerMorph (span to div)', async function() { |
| 624 |
mockResponse('GET', '/test', '<div id="target" hx-get="/click" hx-target="#result">Updated</div>'); |
| 625 |
const container = createProcessedHTML('<div><span id="target">Original</span><div id="result"></div></div>'); |
| 626 |
|
| 627 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'}); |
| 628 |
|
| 629 |
const newDiv = container.querySelector('#target'); |
| 630 |
assert.isNotNull(newDiv, 'New div should exist in DOM'); |
| 631 |
assert.equal(newDiv.tagName, 'DIV', 'Element should now be a div'); |
| 632 |
assert.equal(newDiv.getAttribute('data-htmx-powered'), 'true', 'New div should be processed'); |
| 633 |
|
| 634 |
mockResponse('GET', '/click', 'Clicked!'); |
| 635 |
newDiv.click(); |
| 636 |
await waitForEvent('htmx:after:swap', 100); |
| 637 |
|
| 638 |
assert.equal(container.querySelector('#result').textContent, 'Clicked!', 'htmx actions on new div should work'); |
| 639 |
}); |
| 640 |
|
| 641 |
it('does not stack hx-on:click handlers across innerMorph', async function() { |
| 642 |
window._calls = 0; |
| 643 |
mockResponse('GET', '/test', '<button id="btn" hx-on:click="window._calls++">v</button>'); |
| 644 |
createProcessedHTML('<div id="target"><button id="btn" hx-on:click="window._calls++">v</button></div>'); |
| 645 |
for (let i = 0; i < 3; i++) { |
| 646 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 647 |
} |
| 648 |
document.getElementById('btn').click(); |
| 649 |
assert.equal(window._calls, 1, 'click should fire handler exactly once'); |
| 650 |
delete window._calls; |
| 651 |
}); |
| 652 |
|
| 653 |
it('does not stack hx-on:click handlers across outerMorph', async function() { |
| 654 |
window._calls = 0; |
| 655 |
mockResponse('GET', '/test', '<button id="btn" hx-on:click="window._calls++">v</button>'); |
| 656 |
createProcessedHTML('<button id="btn" hx-on:click="window._calls++">v</button>'); |
| 657 |
for (let i = 0; i < 3; i++) { |
| 658 |
await htmx.ajax('GET', '/test', {target: '#btn', swap: 'outerMorph'}); |
| 659 |
} |
| 660 |
document.getElementById('btn').click(); |
| 661 |
assert.equal(window._calls, 1, 'click should fire handler exactly once'); |
| 662 |
delete window._calls; |
| 663 |
}); |
| 664 |
|
| 665 |
it('handles multiple hx-on attributes after morph', async function() { |
| 666 |
window._log = []; |
| 667 |
mockResponse('GET', '/test', '<button id="b" hx-on:click="window._log.push(\'c\')" hx-on:focus="window._log.push(\'f\')">b</button>'); |
| 668 |
createProcessedHTML('<div id="wrap"><button id="b" hx-on:click="window._log.push(\'c\')" hx-on:focus="window._log.push(\'f\')">b</button></div>'); |
| 669 |
await htmx.ajax('GET', '/test', {target: '#wrap', swap: 'innerMorph'}); |
| 670 |
const b = document.getElementById('b'); |
| 671 |
b.dispatchEvent(new Event('focus')); |
| 672 |
b.click(); |
| 673 |
assert.deepEqual(window._log, ['f', 'c'], 'both handlers should fire exactly once'); |
| 674 |
delete window._log; |
| 675 |
}); |
| 676 |
|
| 677 |
it('picks up hx-trigger value change across innerMorph', async function() { |
| 678 |
mockResponse('GET', '/x', '<span/>'); |
| 679 |
mockResponse('GET', '/test', '<button id="b" hx-get="/x" hx-trigger="keyup">b</button>'); |
| 680 |
createProcessedHTML('<div id="wrap"><button id="b" hx-get="/x" hx-trigger="click">b</button></div>'); |
| 681 |
|
| 682 |
await htmx.ajax('GET', '/test', {target: '#wrap', swap: 'innerMorph'}); |
| 683 |
|
| 684 |
const b = document.getElementById('b'); |
| 685 |
let fired = 0; |
| 686 |
b.addEventListener('htmx:before:request', () => fired++); |
| 687 |
|
| 688 |
b.click(); |
| 689 |
await new Promise(r => setTimeout(r, 20)); |
| 690 |
assert.equal(fired, 0, 'click should no longer fire after morph'); |
| 691 |
b.dispatchEvent(new KeyboardEvent('keyup')); |
| 692 |
await waitForEvent('htmx:after:request', 100); |
| 693 |
assert.equal(fired, 1, 'keyup should fire after morph'); |
| 694 |
}); |
| 695 |
|
| 696 |
}); |
| 697 |
|
| 698 |
describe('htmx integration', function() { |
| 699 |
it('preserves data-htmx-powered attribute during innerMorph', async function() { |
| 700 |
mockResponse('GET', '/test', '<button id="btn" hx-get="/click">Updated</button>'); |
| 701 |
const div = createProcessedHTML('<div id="target"><button id="btn" hx-get="/click">Original</button></div>'); |
| 702 |
const btn = div.querySelector('#btn'); |
| 703 |
htmx.process(btn); |
| 704 |
|
| 705 |
assert.equal(btn.getAttribute('data-htmx-powered'), 'true'); |
| 706 |
|
| 707 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 708 |
|
| 709 |
assert.equal(btn.getAttribute('data-htmx-powered'), 'true', 'data-htmx-powered should be preserved'); |
| 710 |
assert.equal(btn.textContent, 'Updated'); |
| 711 |
}); |
| 712 |
|
| 713 |
it('preserves data-htmx-powered attribute during outerMorph', async function() { |
| 714 |
mockResponse('GET', '/test', '<button id="btn" hx-get="/click" class="new">Updated</button>'); |
| 715 |
const container = createProcessedHTML('<div><button id="btn" hx-get="/click">Original</button></div>'); |
| 716 |
const btn = container.querySelector('#btn'); |
| 717 |
htmx.process(btn); |
| 718 |
|
| 719 |
assert.equal(btn.getAttribute('data-htmx-powered'), 'true'); |
| 720 |
|
| 721 |
await htmx.ajax('GET', '/test', {target: '#btn', swap: 'outerMorph'}); |
| 722 |
|
| 723 |
const newBtn = container.querySelector('#btn'); |
| 724 |
assert.equal(newBtn, btn, 'Button should be same element'); |
| 725 |
assert.equal(newBtn.getAttribute('data-htmx-powered'), 'true', 'data-htmx-powered should be preserved'); |
| 726 |
assert.equal(newBtn.className, 'new'); |
| 727 |
}); |
| 728 |
|
| 729 |
it('preserves htmx event listeners during morph', async function() { |
| 730 |
mockResponse('GET', '/click', 'Clicked!'); |
| 731 |
mockResponse('GET', '/test', '<button id="btn" hx-get="/click" hx-target="#result">Updated</button><div id="result"></div>'); |
| 732 |
const div = createProcessedHTML('<div id="target"><button id="btn" hx-get="/click" hx-target="#result">Original</button><div id="result"></div></div>'); |
| 733 |
const btn = div.querySelector('#btn'); |
| 734 |
const result = div.querySelector('#result'); |
| 735 |
htmx.process(btn); |
| 736 |
|
| 737 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 738 |
|
| 739 |
btn.click(); |
| 740 |
await waitForEvent('htmx:after:swap', 100); |
| 741 |
|
| 742 |
assert.equal(result.textContent, 'Clicked!', 'htmx functionality should still work'); |
| 743 |
}); |
| 744 |
}); |
| 745 |
|
| 746 |
describe('hx-morph-skip attribute (default morphSkip selector)', function() { |
| 747 |
it('skips element with hx-morph-skip attribute', async function() { |
| 748 |
mockResponse('GET', '/test', '<div hx-morph-skip data-v="new">new</div>'); |
| 749 |
const div = createProcessedHTML('<div id="target"><div hx-morph-skip data-v="old">old</div></div>'); |
| 750 |
|
| 751 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 752 |
|
| 753 |
assert.equal(div.querySelector('[hx-morph-skip]').getAttribute('data-v'), 'old'); |
| 754 |
assert.equal(div.querySelector('[hx-morph-skip]').textContent, 'old'); |
| 755 |
}); |
| 756 |
|
| 757 |
it('morphs other elements when only some have hx-morph-skip', async function() { |
| 758 |
mockResponse('GET', '/test', '<div hx-morph-skip data-v="new">skip</div><div class="morph" data-v="new">morph</div>'); |
| 759 |
const div = createProcessedHTML('<div id="target"><div hx-morph-skip data-v="old">skip</div><div class="morph" data-v="old">morph</div></div>'); |
| 760 |
|
| 761 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 762 |
|
| 763 |
assert.equal(div.querySelector('[hx-morph-skip]').getAttribute('data-v'), 'old'); |
| 764 |
assert.equal(div.querySelector('.morph').getAttribute('data-v'), 'new'); |
| 765 |
}); |
| 766 |
}); |
| 767 |
|
| 768 |
describe('hx-morph-skip-children attribute (default morphSkipChildren selector)', function() { |
| 769 |
it('updates attrs but preserves children with hx-morph-skip-children', async function() { |
| 770 |
mockResponse('GET', '/test', '<div hx-morph-skip-children data-v="new"><span>new</span></div>'); |
| 771 |
const div = createProcessedHTML('<div id="target"><div hx-morph-skip-children data-v="old"><span>old</span></div></div>'); |
| 772 |
|
| 773 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 774 |
|
| 775 |
assert.equal(div.querySelector('[hx-morph-skip-children]').getAttribute('data-v'), 'new', 'attrs update'); |
| 776 |
assert.equal(div.querySelector('[hx-morph-skip-children] span').textContent, 'old', 'children preserved'); |
| 777 |
}); |
| 778 |
|
| 779 |
it('morphs children of elements without hx-morph-skip-children', async function() { |
| 780 |
mockResponse('GET', '/test', '<div hx-morph-skip-children data-v="new"><span>new</span></div><div class="normal"><span>new</span></div>'); |
| 781 |
const div = createProcessedHTML('<div id="target"><div hx-morph-skip-children data-v="old"><span>old</span></div><div class="normal"><span>old</span></div></div>'); |
| 782 |
|
| 783 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 784 |
|
| 785 |
assert.equal(div.querySelector('[hx-morph-skip-children] span').textContent, 'old'); |
| 786 |
assert.equal(div.querySelector('.normal span').textContent, 'new'); |
| 787 |
}); |
| 788 |
}); |
| 789 |
|
| 790 |
describe('morphSkip config', function() { |
| 791 |
afterEach(function() { |
| 792 |
htmx.config.morphSkip = '[hx-morph-skip]'; |
| 793 |
}); |
| 794 |
|
| 795 |
it('skips morphing elements matching selector', async function() { |
| 796 |
htmx.config.morphSkip = '.no-morph'; |
| 797 |
mockResponse('GET', '/test', '<div class="no-morph" data-value="new">new content</div>'); |
| 798 |
const div = createProcessedHTML('<div id="target"><div class="no-morph" data-value="old">old content</div></div>'); |
| 799 |
const noMorph = div.querySelector('.no-morph'); |
| 800 |
|
| 801 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 802 |
|
| 803 |
assert.equal(noMorph.getAttribute('data-value'), 'old', 'Attributes should not be updated'); |
| 804 |
assert.equal(noMorph.textContent, 'old content', 'Content should not be updated'); |
| 805 |
}); |
| 806 |
|
| 807 |
it('skips morphing custom elements', async function() { |
| 808 |
htmx.config.morphSkip = 'custom-element'; |
| 809 |
mockResponse('GET', '/test', '<custom-element id="ce" data-value="new"><span>new</span></custom-element>'); |
| 810 |
const div = createProcessedHTML('<div id="target"><custom-element id="ce" data-value="old"><span>old</span></custom-element></div>'); |
| 811 |
const ce = div.querySelector('custom-element'); |
| 812 |
|
| 813 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 814 |
|
| 815 |
assert.equal(ce.getAttribute('data-value'), 'old'); |
| 816 |
assert.equal(ce.querySelector('span').textContent, 'old'); |
| 817 |
}); |
| 818 |
|
| 819 |
it('morphs other elements when some are skipped', async function() { |
| 820 |
htmx.config.morphSkip = '.skip'; |
| 821 |
mockResponse('GET', '/test', '<div class="skip" data-value="new">skip</div><div class="morph" data-value="new">morph</div>'); |
| 822 |
const div = createProcessedHTML('<div id="target"><div class="skip" data-value="old">skip</div><div class="morph" data-value="old">morph</div></div>'); |
| 823 |
const skip = div.querySelector('.skip'); |
| 824 |
const morph = div.querySelector('.morph'); |
| 825 |
|
| 826 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 827 |
|
| 828 |
assert.equal(skip.getAttribute('data-value'), 'old'); |
| 829 |
assert.equal(morph.getAttribute('data-value'), 'new'); |
| 830 |
}); |
| 831 |
}); |
| 832 |
|
| 833 |
describe('text node handling', function() { |
| 834 |
it('removes text nodes during morph without error', async function() { |
| 835 |
mockResponse('GET', '/test', '<div id="child">content</div>'); |
| 836 |
const div = createProcessedHTML('<div id="target">text node<div id="child">old</div></div>'); |
| 837 |
|
| 838 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 839 |
|
| 840 |
assert.isNotNull(div.querySelector('#child')); |
| 841 |
assert.equal(div.querySelector('#child').textContent, 'content'); |
| 842 |
}); |
| 843 |
|
| 844 |
it('handles mixed text nodes and elements', async function() { |
| 845 |
mockResponse('GET', '/test', '<span>new</span>'); |
| 846 |
const div = createProcessedHTML('<div id="target">text1<span>old</span>text2</div>'); |
| 847 |
|
| 848 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 849 |
|
| 850 |
assert.isNotNull(div.querySelector('span')); |
| 851 |
assert.equal(div.querySelector('span').textContent, 'new'); |
| 852 |
}); |
| 853 |
|
| 854 |
it('replaces text nodes with elements', async function() { |
| 855 |
mockResponse('GET', '/test', '<div id="new">element</div>'); |
| 856 |
const div = createProcessedHTML('<div id="target">just text</div>'); |
| 857 |
|
| 858 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 859 |
|
| 860 |
assert.isNotNull(div.querySelector('#new')); |
| 861 |
assert.equal(div.querySelector('#new').textContent, 'element'); |
| 862 |
}); |
| 863 |
}); |
| 864 |
|
| 865 |
describe('morphSkipChildren config', function() { |
| 866 |
afterEach(function() { |
| 867 |
htmx.config.morphSkipChildren = '[hx-morph-skip-children]'; |
| 868 |
}); |
| 869 |
|
| 870 |
it('updates attributes but skips children morphing', async function() { |
| 871 |
htmx.config.morphSkipChildren = '.skip-children'; |
| 872 |
mockResponse('GET', '/test', '<div class="skip-children" data-value="new"><span>new child</span></div>'); |
| 873 |
const div = createProcessedHTML('<div id="target"><div class="skip-children" data-value="old"><span>old child</span></div></div>'); |
| 874 |
const skipChildren = div.querySelector('.skip-children'); |
| 875 |
|
| 876 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 877 |
|
| 878 |
assert.equal(skipChildren.getAttribute('data-value'), 'new', 'Attributes should be updated'); |
| 879 |
assert.equal(skipChildren.querySelector('span').textContent, 'old child', 'Children should not be morphed'); |
| 880 |
}); |
| 881 |
|
| 882 |
it('preserves Light DOM children in custom elements', async function() { |
| 883 |
htmx.config.morphSkipChildren = 'lit-component'; |
| 884 |
mockResponse('GET', '/test', '<lit-component id="lc" value="new"><div class="internal">new</div></lit-component>'); |
| 885 |
const div = createProcessedHTML('<div id="target"><lit-component id="lc" value="old"><div class="internal">old</div></lit-component></div>'); |
| 886 |
const lc = div.querySelector('lit-component'); |
| 887 |
const internal = lc.querySelector('.internal'); |
| 888 |
|
| 889 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 890 |
|
| 891 |
assert.equal(lc.getAttribute('value'), 'new', 'Attributes should update'); |
| 892 |
assert.equal(internal.textContent, 'old', 'Light DOM children should be preserved'); |
| 893 |
}); |
| 894 |
|
| 895 |
it('works with multiple selectors', async function() { |
| 896 |
htmx.config.morphSkipChildren = '.skip1, .skip2'; |
| 897 |
mockResponse('GET', '/test', '<div class="skip1" data-value="new"><span>new1</span></div><div class="skip2" data-value="new"><span>new2</span></div>'); |
| 898 |
const div = createProcessedHTML('<div id="target"><div class="skip1" data-value="old"><span>old1</span></div><div class="skip2" data-value="old"><span>old2</span></div></div>'); |
| 899 |
|
| 900 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 901 |
|
| 902 |
assert.equal(div.querySelector('.skip1').getAttribute('data-value'), 'new'); |
| 903 |
assert.equal(div.querySelector('.skip1 span').textContent, 'old1'); |
| 904 |
assert.equal(div.querySelector('.skip2').getAttribute('data-value'), 'new'); |
| 905 |
assert.equal(div.querySelector('.skip2 span').textContent, 'old2'); |
| 906 |
}); |
| 907 |
|
| 908 |
it('allows normal morphing for non-matching elements', async function() { |
| 909 |
htmx.config.morphSkipChildren = '.skip-children'; |
| 910 |
mockResponse('GET', '/test', '<div class="normal" data-value="new"><span>new</span></div><div class="skip-children" data-value="new"><span>new</span></div>'); |
| 911 |
const div = createProcessedHTML('<div id="target"><div class="normal" data-value="old"><span>old</span></div><div class="skip-children" data-value="old"><span>old</span></div></div>'); |
| 912 |
|
| 913 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 914 |
|
| 915 |
assert.equal(div.querySelector('.normal span').textContent, 'new', 'Normal elements should morph children'); |
| 916 |
assert.equal(div.querySelector('.skip-children span').textContent, 'old', 'Skip elements should preserve children'); |
| 917 |
}); |
| 918 |
}); |
| 919 |
|
| 920 |
describe('morphIgnore config', function() { |
| 921 |
afterEach(function() { |
| 922 |
htmx.config.morphIgnore = ['data-htmx-powered']; |
| 923 |
}); |
| 924 |
|
| 925 |
it('preserves attributes whose name starts with an ignored prefix', async function() { |
| 926 |
htmx.config.morphIgnore = ['data-keep']; |
| 927 |
mockResponse('GET', '/test', '<div id="child" data-keep="new" data-keep-extra="new" data-other="new">content</div>'); |
| 928 |
const div = createProcessedHTML('<div id="target"><div id="child" data-keep="old" data-keep-extra="old" data-other="old">content</div></div>'); |
| 929 |
const child = div.querySelector('#child'); |
| 930 |
|
| 931 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 932 |
|
| 933 |
assert.equal(child.getAttribute('data-keep'), 'old', 'exact prefix match should be preserved'); |
| 934 |
assert.equal(child.getAttribute('data-keep-extra'), 'old', 'longer name sharing the prefix should be preserved'); |
| 935 |
assert.equal(child.getAttribute('data-other'), 'new', 'non-matching attribute should be morphed'); |
| 936 |
}); |
| 937 |
|
| 938 |
it('does not remove an ignored attribute that is absent from the new content', async function() { |
| 939 |
htmx.config.morphIgnore = ['data-keep']; |
| 940 |
mockResponse('GET', '/test', '<div id="child">content</div>'); |
| 941 |
const div = createProcessedHTML('<div id="target"><div id="child" data-keep="old">content</div></div>'); |
| 942 |
const child = div.querySelector('#child'); |
| 943 |
|
| 944 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 945 |
|
| 946 |
assert.equal(child.getAttribute('data-keep'), 'old', 'ignored attribute should not be removed'); |
| 947 |
}); |
| 948 |
|
| 949 |
it('matches an exact attribute name without affecting others', async function() { |
| 950 |
htmx.config.morphIgnore = ['data-keep']; |
| 951 |
mockResponse('GET', '/test', '<div id="child" data-keep="new" data-kee="new">content</div>'); |
| 952 |
const div = createProcessedHTML('<div id="target"><div id="child" data-keep="old" data-kee="old">content</div></div>'); |
| 953 |
const child = div.querySelector('#child'); |
| 954 |
|
| 955 |
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'}); |
| 956 |
|
| 957 |
assert.equal(child.getAttribute('data-keep'), 'old', 'exact name should be preserved'); |
| 958 |
assert.equal(child.getAttribute('data-kee'), 'new', 'shorter non-prefixed name should be morphed'); |
| 959 |
}); |
| 960 |
}); |
| 961 |
|
| 962 |
}); |