summaryrefslogtreecommitdiffstatshomepage
path: root/www/static/test/core/regressions.js
blob: 015b58152cf5bb5372cfa5645c4e95117fa3c1ec (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
describe('Core htmx Regression Tests', function() {
  beforeEach(function() {
    this.server = makeServer()
    clearWorkArea()
  })
  afterEach(function() {
    this.server.restore()
    clearWorkArea()
  })

  it('SVGs process properly in IE11', function() {
    var btn = make('<svg onclick="document.getElementById(\'contents\').classList.toggle(\'show\')" class="hamburger" viewBox="0 0 100 80" width="25" height="25" style="margin-bottom:-5px">\n' +
            '<rect width="100" height="20" style="fill:rgb(52, 101, 164)" rx="10"></rect>\n' +
            '<rect y="30" width="100" height="20" style="fill:rgb(52, 101, 164)" rx="10"></rect>\n' +
            '<rect y="60" width="100" height="20" style="fill:rgb(52, 101, 164)" rx="10"></rect>\n' +
            '</svg>')
  })

  it('Handles https://github.com/bigskysoftware/htmx/issues/4 properly', function() {
    this.server.respondWith('GET', '/index2a.php',
      "<div id='message' hx-swap-oob='true'>I came from message oob swap I should be second</div>" +
            "<div id='message2' hx-swap-oob='true'>I came from a message2 oob swap I should be third  but I am in the wrong spot</div>" +
            "I'm page2 content (non-swap) I should be first")

    var h1 = make('' +
            "<div id='page2' ></div>" +
            "<div id='message'></div>" +
            "<div id='message2'></div>" +
        "<h1 hx-get='/index2a.php' hx-target='#page2' hx-trigger='click'>Kutty CLICK ME</h1>")
    h1.click()
    this.server.respond()
    htmx.find('#page2').innerHTML.should.equal("I'm page2 content (non-swap) I should be first")
    htmx.find('#message').innerHTML.should.equal('I came from message oob swap I should be second')
    htmx.find('#message2').innerHTML.should.equal('I came from a message2 oob swap I should be third  but I am in the wrong spot')
  })

  it('Handles https://github.com/bigskysoftware/htmx/issues/33 "empty values" properly', function() {
    this.server.respondWith('POST', '/htmx.php', function(xhr) {
      xhr.respond(200, {}, xhr.requestBody)
    })

    var form = make('<form hx-trigger="click" hx-post="/htmx.php">\n' +
            '<input type="text" name="variable" value="">\n' +
            '<button type="submit">Submit</button>\n' +
            '</form>')
    form.click()
    this.server.respond()
    form.innerHTML.should.equal('variable=')
  })

  it('name=id doesnt cause an error', function() {
    this.server.respondWith('GET', '/test', 'Foo<form><input name="id"/></form>')
    var div = make('<div hx-get="/test">Get It</div>')
    div.click()
    this.server.respond()
    div.innerText.should.contain('Foo')
  })

  it('empty id doesnt cause an error', function() {
    this.server.respondWith('GET', '/test', "Foo\n<div id=''></div>")
    var div = make('<div hx-get="/test">Get It</div>')
    div.click()
    this.server.respond()
    div.innerText.should.contain('Foo')
  })

  it('id with dot in value doesnt cause an error', function() {
    this.server.respondWith('GET', '/test', "Foo <div id='ViewModel.Test'></div>")
    var div = make('<div hx-get="/test">Get It</div>')
    div.click()
    this.server.respond()
    div.innerText.should.contain('Foo')
  })

  it('@ symbol in attributes does not break requests', function() {
    this.server.respondWith('GET', '/test', "<div id='d1' @foo='bar'>Foo</div>")
    var div = make('<div hx-get="/test">Get It</div>')
    div.click()
    this.server.respond()
    byId('d1').getAttribute('@foo').should.equal('bar')
  })

  it('@ symbol in attributes does not break attribute settling requests', function() {
    this.server.respondWith('GET', '/test', "<div id='d1' @foo='bar'>Foo</div>")
    var div = make('<div hx-get="/test"><div id="d1">Foo</div></div>')
    div.click()
    this.server.respond()
    byId('d1').getAttribute('@foo').should.equal('bar')
  })

  it('selected element with ID does not cause NPE when it disappears', function() {
    this.server.respondWith('GET', '/test', "<div id='d1'>Replaced</div>")
    var input = make('<input hx-trigger="click" hx-get="/test" id="i1" hx-swap="outerHTML">')
    input.focus()
    input.click()
    this.server.respond()
    byId('d1').innerText.should.equal('Replaced')
  })

  it('does not submit with a false condition on a form', function() {
    this.server.respondWith('POST', '/test', 'Submitted')
    var defaultPrevented = false
    htmx.on('click', function(evt) {
      defaultPrevented = evt.defaultPrevented
    })
    var form = make('<form hx-post="/test" hx-trigger="click[false]"></form>')
    form.click()
    this.server.respond()
    defaultPrevented.should.equal(true)
  })

  it('two elements can listen for the same event on another element', function() {
    this.server.respondWith('GET', '/test', 'triggered')

    make('<div id="d1" hx-trigger="click from:body" hx-get="/test"></div>' +
            '        <div id="d2" hx-trigger="click from:body" hx-get="/test"></div>')

    var div1 = byId('d1')
    var div2 = byId('d2')

    document.body.click()
    this.server.respond()

    div2.innerHTML.should.equal('triggered')
    div1.innerHTML.should.equal('triggered')
  })

  it('a form can reset based on the htmx:afterRequest event', function() {
    this.server.respondWith('POST', '/test', 'posted')

    var form = make('<div id="d1"></div><form _="on htmx:afterRequest reset() me" hx-post="/test" hx-target="#d1">' +
            '  <input type="text" name="input" id="i1"/>' +
            '  <input type="submit" id="s1"/>' +
            '</form>')
    htmx.trigger(form, 'htmx:load') // have to manually trigger the load event for non-AJAX dynamic content

    var div1 = byId('d1')
    var input = byId('i1')
    input.value = 'foo'
    var submit = byId('s1')

    input.value.should.equal('foo')
    submit.click()
    this.server.respond()

    div1.innerHTML.should.equal('posted')
    input.value.should.equal('') // form should be reset
  })

  it('supports image maps', function() {
    this.server.respondWith('GET', '/test', 'triggered')

    make('<div>' +
            '    <div id="d1"></div>' +
            '    <img src="img/bars.svg" usemap="#workmap" width="400" height="379">' +
            '' +
            '    <map name="workmap">' +
            '        <area shape="rect" coords="34,44,270,350" alt="Computer" hx-get="/test" hx-target="#d1">' +
            '    </map>' +
            '</div>')

    var div1 = byId('d1')
    var area = document.getElementsByTagName('area')[0]

    area.click()
    this.server.respond()

    div1.innerHTML.should.equal('triggered')
  })

  it('supports unset on hx-select', function() {
    this.server.respondWith('GET', '/test', "Foo<span id='example'>Bar</span>")
    make('<form hx-select="#example">\n' +
            '      <button id="b1" hx-select="unset" hx-get="/test">Initial</button>\n' +
            '</form>')
    var btn = byId('b1')
    btn.click()
    this.server.respond()

    btn.innerText.should.equal('FooBar')
  })

  it("can trigger swaps from fields that don't support setSelectionRange", function() {
    const template = '<form id="formtest"> \n' +
              '<input hx-get="/test" hx-target="#formtest" hx-trigger="click" type="text" id="id_email" value="test@test.com" />\n' +
              '</form>'

    const response = '<form id="formtest">\n' +
              '<input hx-get="/test" hx-target="#formtest" hx-trigger="click" type="email" id="id_email" value="supertest@test.com" />\n' +
              '</form>'
    this.server.respondWith('GET', '/test', response)
    make(template)
    var input = byId('id_email')
    // HTMX only attempts to restore the selection on inputs that have a current selection and are active.
    // additionally we can't set the selection on email inputs (that's the whole bug) so start as a text input where you can set selection
    // and replace with an email
    input.focus()
    input.selectionStart = 3
    input.selectionEnd = 3
    input.click()
    this.server.respond()
    var input = byId('id_email')
    input.value.should.equal('supertest@test.com')
  })

  it('script tags only execute once', function(done) {
    window.i = 0 // set count to 0
    this.server.respondWith('GET', '/test', '<script>console.trace(); window.i++</script>') // increment the count by 1

    // make a div w/ a short settle delay to make the problem more obvious
    var div = make('<div hx-get="/test" hx-swap="innerHTML settle:5ms"/>')
    div.click()
    this.server.respond()

    setTimeout(function() {
      window.i.should.equal(1)
      delete window.i
      done()
    }, 50)
  })

  it('script tags only execute once when nested', function(done) {
    window.i = 0 // set count to 0
    this.server.respondWith('GET', '/test', '<p>foo</p><div><script>console.trace(); window.i++</script></div>') // increment the count by 1

    // make a div w/ a short settle delay to make the problem more obvious
    var div = make('<div hx-get="/test" hx-swap="innerHTML settle:5ms"/>')
    div.click()
    this.server.respond()

    setTimeout(function() {
      window.i.should.equal(1)
      delete window.i
      done()
    }, 50)
  })

  it('htmx.config.allowScriptTags properly disables script tags', function(done) {
    htmx.config.allowScriptTags = false
    window.i = 0 // set count to 0
    this.server.respondWith('GET', '/test', '<script>console.trace(); window.i++</script>') // increment the count by 1

    // make a div w/ a short settle delay to make the problem more obvious
    var div = make('<div hx-get="/test" hx-swap="innerHTML settle:5ms"/>')
    div.click()
    this.server.respond()

    setTimeout(function() {
      window.i.should.equal(0)
      htmx.config.allowScriptTags = true
      delete window.i
      done()
    }, 50)
  })

  it('htmx.config.allowScriptTags properly disables script tags when nested', function(done) {
    htmx.config.allowScriptTags = false
    window.i = 0 // set count to 0
    this.server.respondWith('GET', '/test', '<div><script>console.trace(); window.i++</script></div>') // increment the count by 1

    // make a div w/ a short settle delay to make the problem more obvious
    var div = make('<div hx-get="/test" hx-swap="innerHTML settle:5ms"/>')
    div.click()
    this.server.respond()

    setTimeout(function() {
      window.i.should.equal(0)
      htmx.config.allowScriptTags = true
      delete window.i
      done()
    }, 50)
  })

  it('a modified click trigger on a form does not prevent the default behaviour of other elements - https://github.com/bigskysoftware/htmx/issues/2755', function(done) {
    var defaultPrevented = 'unset'
    make('<input type="date" id="datefield">')
    make('<form hx-trigger="click from:body"></form>')

    htmx.on('#datefield', 'click', function(evt) {
      // we need to wait so the state of the evt is finalized
      setTimeout(() => {
        defaultPrevented = evt.defaultPrevented
        try {
          defaultPrevented.should.equal(false)
          done()
        } catch (err) {
          done(err)
        }
      }, 0)
    })

    byId('datefield').click()
  })
})