summaryrefslogtreecommitdiffstatshomepage
path: root/test/attributes/hx-swap-oob.js
blob: e7f54a45782248fbe6d23d805d7b32d8f1f22560 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
describe('hx-swap-oob attribute', function() {
  const savedConfig = htmx.config
  beforeEach(function() {
    this.server = makeServer()
    htmx.config = Object.assign({}, savedConfig)
    clearWorkArea()
  })
  afterEach(function() {
    this.server.restore()
    htmx.config = savedConfig
    clearWorkArea()
  })

  // Repeat the same test to make sure it works with different configurations
  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('handles basic response properly with config ' + JSON.stringify(config), function() {
      Object.assign(htmx.config, config)
      this.server.respondWith('GET', '/test', "Clicked<div id='d1' hx-swap-oob='true'>Swapped0</div>")
      var div = make('<div hx-get="/test">click me</div>')
      make('<div id="d1"></div>')
      div.click()
      this.server.respond()
      div.innerHTML.should.equal('Clicked')
      byId('d1').innerHTML.should.equal('Swapped0')
    })
  }

  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('oob swap works when the response has a body tag with config ' + JSON.stringify(config), function() {
      Object.assign(htmx.config, config)
      this.server.respondWith('GET', '/test', "<body>Clicked<div id='d2' hx-swap-oob='true'>Swapped0</div></body>")
      var div = make('<div hx-get="/test">click me</div>')
      make('<div id="d2"></div>')
      div.click()
      this.server.respond()
      div.innerHTML.should.equal('Clicked')
      byId('d2').innerHTML.should.equal('Swapped0')
    })
  }

  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('oob swap works when the response has html and body tags with config ' + JSON.stringify(config), function() {
      Object.assign(htmx.config, config)
      this.server.respondWith('GET', '/test', "<html><body>Clicked<div id='d3' hx-swap-oob='true'>Swapped0</div></body></html>")
      var div = make('<div hx-get="/test">click me</div>')
      make('<div id="d3"></div>')
      div.click()
      this.server.respond()
      div.innerHTML.should.equal('Clicked')
      byId('d3').innerHTML.should.equal('Swapped0')
    })
  }

  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('handles more than one oob swap properly with config ' + JSON.stringify(config), function() {
      Object.assign(htmx.config, config)
      this.server.respondWith('GET', '/test', "Clicked<div id='d1' hx-swap-oob='true'>Swapped1</div><div id='d2' hx-swap-oob='true'>Swapped2</div>")
      var div = make('<div hx-get="/test">click me</div>')
      make('<div id="d1"></div>')
      make('<div id="d2"></div>')
      div.click()
      this.server.respond()
      div.innerHTML.should.equal('Clicked')
      byId('d1').innerHTML.should.equal('Swapped1')
      byId('d2').innerHTML.should.equal('Swapped2')
    })
  }

  it('handles remvoing hx-swap-oob tag', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' data-hx-swap-oob='true'>Swapped3</div>")
    var div = make('<div data-hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    div.innerHTML.should.equal('Clicked')
    byId('d1').innerHTML.should.equal('Swapped3')
    byId('d1').hasAttribute('hx-swap-oob').should.equal(false)
  })

  it('handles remvoing data-hx-swap-oob tag', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' data-hx-swap-oob='true'>Swapped3</div>")
    var div = make('<div data-hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    div.innerHTML.should.equal('Clicked')
    byId('d1').innerHTML.should.equal('Swapped3')
    byId('d1').hasAttribute('data-hx-swap-oob').should.equal(false)
  })

  it('handles no id match properly', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' hx-swap-oob='true'>Swapped2</div>")
    var div = make('<div hx-get="/test">click me</div>')
    div.click()
    this.server.respond()
    div.innerText.should.equal('Clicked')
  })

  it('handles basic response properly w/ data-* prefix', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' data-hx-swap-oob='true'>Swapped3</div>")
    var div = make('<div data-hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    div.innerHTML.should.equal('Clicked')
    byId('d1').innerHTML.should.equal('Swapped3')
  })

  it('handles outerHTML response properly', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' foo='bar' hx-swap-oob='outerHTML'>Swapped4</div>")
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    byId('d1').getAttribute('foo').should.equal('bar')
    div.innerHTML.should.equal('Clicked')
    byId('d1').innerHTML.should.equal('Swapped4')
  })

  it('handles innerHTML response properly', function() {
    this.server.respondWith('GET', '/test', "Clicked<div id='d1' foo='bar' hx-swap-oob='innerHTML'>Swapped5</div>")
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    should.equal(byId('d1').getAttribute('foo'), null)
    div.innerHTML.should.equal('Clicked')
    byId('d1').innerHTML.should.equal('Swapped5')
  })

  it('oob swaps can be nested in content with config {"allowNestedOobSwaps": true}', function() {
    htmx.config.allowNestedOobSwaps = true
    this.server.respondWith('GET', '/test', "<div>Clicked<div id='d1' foo='bar' hx-swap-oob='innerHTML'>Swapped6</div></div>")
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    should.equal(byId('d1').getAttribute('foo'), null)
    div.innerHTML.should.equal('<div>Clicked</div>')
    byId('d1').innerHTML.should.equal('Swapped6')
  })

  it('oob swaps in nested content are ignored and stripped with config {"allowNestedOobSwaps": false}', function() {
    htmx.config.allowNestedOobSwaps = false
    this.server.respondWith('GET', '/test', '<div>Clicked<div hx-swap-oob="innerHTML:#d1">Swapped6.1</div></div>')
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1"></div>')
    div.click()
    this.server.respond()
    byId('d1').innerHTML.should.equal('')
    div.innerHTML.should.equal('<div>Clicked<div>Swapped6.1</div></div>')
  })

  it('oob swaps can use selectors to match up', function() {
    this.server.respondWith('GET', '/test', "<div>Clicked<div hx-swap-oob='innerHTML:[oob-foo]'>Swapped7</div></div>")
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1" oob-foo="bar"></div>')
    div.click()
    this.server.respond()
    should.equal(byId('d1').getAttribute('oob-foo'), 'bar')
    div.innerHTML.should.equal('<div>Clicked</div>')
    byId('d1').innerHTML.should.equal('Swapped7')
  })

  it('swaps into all targets that match the selector (innerHTML)', function() {
    this.server.respondWith('GET', '/test', "<div>Clicked</div><div class='target' hx-swap-oob='innerHTML:.target'>Swapped8</div>")
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1">No swap</div>')
    make('<div id="d2" class="target">Not swapped</div>')
    make('<div id="d3" class="target">Not swapped</div>')
    div.click()
    this.server.respond()
    byId('d1').innerHTML.should.equal('No swap')
    byId('d2').innerHTML.should.equal('Swapped8')
    byId('d3').innerHTML.should.equal('Swapped8')
  })

  it('swaps into all targets that match the selector (outerHTML)', function() {
    var oobSwapContent = '<div class="new-target" hx-swap-oob="outerHTML:.target">Swapped9</div>'
    var finalContent = '<div class="new-target">Swapped9</div>'
    this.server.respondWith('GET', '/test', '<div>Clicked</div>' + oobSwapContent)
    var div = make('<div hx-get="/test">click me</div>')
    make('<div id="d1"><div>No swap</div></div>')
    make('<div id="d2"><div class="target">Not swapped</div></div>')
    make('<div id="d3"><div class="target">Not swapped</div></div>')
    div.click()
    this.server.respond()
    byId('d1').innerHTML.should.equal('<div>No swap</div>')
    byId('d2').innerHTML.should.equal(finalContent)
    byId('d3').innerHTML.should.equal(finalContent)
  })

  it('oob swap delete works properly', function() {
    this.server.respondWith('GET', '/test', '<div hx-swap-oob="delete" id="d1"></div>')

    var div = make('<div id="d1" hx-get="/test">Foo</div>')
    div.click()
    this.server.respond()
    should.equal(byId('d1'), null)
  })

  it('oob swap removes templates used for oob encapsulation only properly', function() {
    this.server.respondWith('GET', '/test', '' +
        'Clicked<template><div hx-swap-oob="outerHTML" id="d1">Foo</div></template>')
    var div = make('<button hx-get="/test" id="b1">Click Me</button>' +
        '<div id="d1" ></div>')
    var btn = byId('b1')
    btn.click()
    this.server.respond()
    should.equal(byId('b1').innerHTML, 'Clicked')
    should.equal(byId('d1').innerHTML, 'Foo')
  })

  it('oob swap keeps templates not used for oob swap encapsulation', function() {
    this.server.respondWith('GET', '/test', '' +
        'Clicked<template></template>')
    var div = make('<button hx-get="/test" id="b1">Click Me</button>' +
        '<div id="d1" ></div>')
    var btn = byId('b1')
    btn.click()
    this.server.respond()
    should.equal(byId('b1').innerHTML, 'Clicked<template></template>')
    should.equal(byId('d1').innerHTML, '')
  })

  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('oob swap supports table row in fragment along other oob swap elements with config ' + JSON.stringify(config), function() {
      Object.assign(htmx.config, config)
      this.server.respondWith('GET', '/test',
        `Clicked
      <div hx-swap-oob="innerHTML" id="d1">Test</div>
      <button type="button" hx-swap-oob="true" id="b2">Another button</button>
      <template>
        <tr hx-swap-oob="true" id="r1"><td>bar</td></tr>
      </template>
      <template>
        <td hx-swap-oob="true" id="td1">hey</td>
      </template>`)

      make(`<div id="d1">Bar</div>
      <button id="b2">Foo</button>
      <table id="table">
        <tbody id="tbody">
          <tr id="r1">
           <td>foo</td>
          </tr>
          <tr>
            <td id="td1">Bar</td>
          </tr>
        </tbody>
      </table>`)

      var btn = make('<button id="b1" type="button" hx-get="/test">Click me</button>')
      btn.click()
      this.server.respond()
      btn.innerText.should.equal('Clicked')
      byId('r1').innerHTML.should.equal('<td>bar</td>')
      byId('b2').innerHTML.should.equal('Another button')
      byId('d1').innerHTML.should.equal('Test')
      byId('td1').innerHTML.should.equal('hey')
    })
  }
  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('handles oob target in web components with both inside shadow root and config ' + JSON.stringify(config), function() {
      this.server.respondWith('GET', '/test', '<div hx-swap-oob="innerHTML:#oob-swap-target">new contents</div>Clicked')
      class TestElement extends HTMLElement {
        connectedCallback() {
          const root = this.attachShadow({ mode: 'open' })
          root.innerHTML = `
            <button hx-get="/test" hx-target="next div">Click me!</button>
            <div id="main-target"></div>
            <div id="oob-swap-target">this should get swapped</div>
          `
          htmx.process(root) // Tell HTMX about this component's shadow DOM
        }
      }
      var elementName = 'test-oobswap-inside-' + config.allowNestedOobSwaps
      customElements.define(elementName, TestElement)
      var div = make(`<div><div id="oob-swap-target">this should not get swapped</div><${elementName}/></div>`)
      var badTarget = div.querySelector('#oob-swap-target')
      var webComponent = div.querySelector(elementName)
      var btn = webComponent.shadowRoot.querySelector('button')
      var goodTarget = webComponent.shadowRoot.querySelector('#oob-swap-target')
      var mainTarget = webComponent.shadowRoot.querySelector('#main-target')
      btn.click()
      this.server.respond()
      should.equal(mainTarget.textContent, 'Clicked')
      should.equal(goodTarget.textContent, 'new contents')
      should.equal(badTarget.textContent, 'this should not get swapped')
    })
  }
  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('handles oob target in web components with main target outside web component config ' + JSON.stringify(config), function() {
      this.server.respondWith('GET', '/test', '<div hx-swap-oob="innerHTML:#oob-swap-target">new contents</div>Clicked')
      class TestElement extends HTMLElement {
        connectedCallback() {
          const root = this.attachShadow({ mode: 'open' })
          root.innerHTML = `
            <button hx-get="/test" hx-target="global #main-target">Click me!</button>
            <div id="main-target"></div>
            <div id="oob-swap-target">this should get swapped</div>
          `
          htmx.process(root) // Tell HTMX about this component's shadow DOM
        }
      }
      var elementName = 'test-oobswap-global-main-' + config.allowNestedOobSwaps
      customElements.define(elementName, TestElement)
      var div = make(`<div><div id="main-target"></div><div id="oob-swap-target">this should not get swapped</div><${elementName}/></div>`)
      var badTarget = div.querySelector('#oob-swap-target')
      var webComponent = div.querySelector(elementName)
      var btn = webComponent.shadowRoot.querySelector('button')
      var goodTarget = webComponent.shadowRoot.querySelector('#oob-swap-target')
      var mainTarget = div.querySelector('#main-target')
      btn.click()
      this.server.respond()
      should.equal(mainTarget.textContent, 'Clicked')
      should.equal(goodTarget.textContent, 'new contents')
      should.equal(badTarget.textContent, 'this should not get swapped')
    })
  }
  for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
    it('handles global oob target in web components with main target inside web component config ' + JSON.stringify(config), function() {
      this.server.respondWith('GET', '/test', '<div hx-swap-oob="innerHTML:global #oob-swap-target">new contents</div>Clicked')
      class TestElement extends HTMLElement {
        connectedCallback() {
          const root = this.attachShadow({ mode: 'open' })
          root.innerHTML = `
            <button hx-get="/test" hx-target="next div">Click me!</button>
            <div id="main-target"></div>
            <div id="oob-swap-target">this should not get swapped</div>
          `
          htmx.process(root) // Tell HTMX about this component's shadow DOM
        }
      }
      var elementName = 'test-oobswap-global-oob-' + config.allowNestedOobSwaps
      customElements.define(elementName, TestElement)
      var div = make(`<div><div id="main-target"></div><div id="oob-swap-target">this should get swapped</div><${elementName}/></div>`)
      var webComponent = div.querySelector(elementName)
      var badTarget = webComponent.shadowRoot.querySelector('#oob-swap-target')
      var btn = webComponent.shadowRoot.querySelector('button')
      var goodTarget = div.querySelector('#oob-swap-target')
      var mainTarget = webComponent.shadowRoot.querySelector('#main-target')
      btn.click()
      this.server.respond()
      should.equal(mainTarget.textContent, 'Clicked')
      should.equal(goodTarget.textContent, 'new contents')
      should.equal(badTarget.textContent, 'this should not get swapped')
    })
  }
})