| 1 |
describe('Strip Modifier', function() { |
| 2 |
afterEach(function() { |
| 3 |
cleanupTest() |
| 4 |
}) |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
it('Main swap with strip:true extracts children', async function() { |
| 11 |
mockResponse('GET', '/api', '<wrapper><p>A</p><p>B</p></wrapper>'); |
| 12 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-swap="innerHTML strip:true">Get</button><div id="target"></div>'); |
| 13 |
find('#btn').click() |
| 14 |
await forRequest(); |
| 15 |
|
| 16 |
|
| 17 |
const target = find('#target'); |
| 18 |
assert.equal(target.children.length, 2); |
| 19 |
assert.equal(target.children[0].tagName, 'P'); |
| 20 |
assert.equal(target.children[0].textContent, 'A'); |
| 21 |
assert.equal(target.children[1].tagName, 'P'); |
| 22 |
assert.equal(target.children[1].textContent, 'B'); |
| 23 |
assert.isUndefined(find('#target wrapper')); |
| 24 |
}) |
| 25 |
|
| 26 |
it('Main swap with strip:false keeps wrapper', async function() { |
| 27 |
mockResponse('GET', '/api', '<wrapper><p>A</p><p>B</p></wrapper>'); |
| 28 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-swap="innerHTML strip:false">Get</button><div id="target"></div>'); |
| 29 |
find('#btn').click() |
| 30 |
await forRequest(); |
| 31 |
|
| 32 |
|
| 33 |
const wrapper = find('#target wrapper'); |
| 34 |
assert.exists(wrapper); |
| 35 |
assert.equal(wrapper.children.length, 2); |
| 36 |
}) |
| 37 |
|
| 38 |
it('Main swap without strip modifier keeps wrapper (default)', async function() { |
| 39 |
mockResponse('GET', '/api', '<wrapper><p>A</p><p>B</p></wrapper>'); |
| 40 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-swap="innerHTML">Get</button><div id="target"></div>'); |
| 41 |
find('#btn').click() |
| 42 |
await forRequest(); |
| 43 |
|
| 44 |
|
| 45 |
const wrapper = find('#target wrapper'); |
| 46 |
assert.exists(wrapper); |
| 47 |
}) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
it('hx-select with strip:true extracts children', async function() { |
| 54 |
mockResponse('GET', '/api', '<div class="content"><span>X</span><span>Y</span></div>'); |
| 55 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-select=".content" hx-swap="innerHTML strip:true">Get</button><div id="target"></div>'); |
| 56 |
find('#btn').click() |
| 57 |
await forRequest(); |
| 58 |
|
| 59 |
|
| 60 |
const target = find('#target'); |
| 61 |
assert.equal(target.children.length, 2); |
| 62 |
assert.equal(target.children[0].tagName, 'SPAN'); |
| 63 |
assert.isUndefined(find('#target .content')); |
| 64 |
}) |
| 65 |
|
| 66 |
it('hx-select with default keeps selected element', async function() { |
| 67 |
mockResponse('GET', '/api', '<div class="content"><span>X</span><span>Y</span></div>'); |
| 68 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-select=".content">Get</button><div id="target"></div>'); |
| 69 |
find('#btn').click() |
| 70 |
await forRequest(); |
| 71 |
|
| 72 |
|
| 73 |
const content = find('#target .content'); |
| 74 |
assert.exists(content); |
| 75 |
assert.equal(content.children.length, 2); |
| 76 |
}) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
it('OOB innerHTML with default strip extracts children', async function() { |
| 83 |
mockResponse('GET', '/api', '<div id="main">Main</div><div hx-swap-oob="innerHTML:#oob"><p>OOB A</p><p>OOB B</p></div>'); |
| 84 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="oob">Original</div>'); |
| 85 |
find('#btn').click() |
| 86 |
await forRequest(); |
| 87 |
|
| 88 |
|
| 89 |
const oob = find('#oob'); |
| 90 |
assert.equal(oob.children.length, 2); |
| 91 |
assert.equal(oob.children[0].tagName, 'P'); |
| 92 |
assert.equal(oob.children[0].textContent, 'OOB A'); |
| 93 |
}) |
| 94 |
|
| 95 |
it('OOB innerHTML with strip:false keeps wrapper', async function() { |
| 96 |
mockResponse('GET', '/api', '<div id="main">Main</div><article hx-swap-oob="innerHTML target:#oob strip:false"><h1>Title</h1><p>Text</p></article>'); |
| 97 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="oob">Original</div>'); |
| 98 |
find('#btn').click() |
| 99 |
await forRequest(); |
| 100 |
|
| 101 |
|
| 102 |
const article = find('#oob article'); |
| 103 |
assert.exists(article); |
| 104 |
assert.equal(article.children.length, 2); |
| 105 |
}) |
| 106 |
|
| 107 |
it('OOB beforeend with default strip extracts children', async function() { |
| 108 |
mockResponse('GET', '/api', '<div id="main">Main</div><div hx-swap-oob="beforeend:#list"><li>Item A</li><li>Item B</li></div>'); |
| 109 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><ul id="list"><li>Existing</li></ul>'); |
| 110 |
find('#btn').click() |
| 111 |
await forRequest(); |
| 112 |
|
| 113 |
|
| 114 |
const list = find('#list'); |
| 115 |
assert.equal(list.children.length, 3); |
| 116 |
assert.equal(list.children[1].tagName, 'LI'); |
| 117 |
assert.equal(list.children[1].textContent, 'Item A'); |
| 118 |
}) |
| 119 |
|
| 120 |
it('OOB beforeend with strip:false keeps wrapper element', async function() { |
| 121 |
mockResponse('GET', '/api', '<div id="main">Main</div><li hx-swap-oob="beforeend target:#list strip:false"><strong>Item</strong></li>'); |
| 122 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><ul id="list"><li>Existing</li></ul>'); |
| 123 |
find('#btn').click() |
| 124 |
await forRequest(); |
| 125 |
|
| 126 |
|
| 127 |
const list = find('#list'); |
| 128 |
assert.equal(list.children.length, 2); |
| 129 |
assert.equal(list.children[1].tagName, 'LI'); |
| 130 |
assert.equal(list.children[1].children[0].tagName, 'STRONG'); |
| 131 |
}) |
| 132 |
|
| 133 |
it('OOB outerHTML with default (no strip) keeps wrapper', async function() { |
| 134 |
mockResponse('GET', '/api', '<div id="main">Main</div><wrapper hx-swap-oob="outerHTML:#target"><section>A</section><section>B</section></wrapper>'); |
| 135 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="target">Old</div>'); |
| 136 |
find('#btn').click() |
| 137 |
await forRequest(); |
| 138 |
|
| 139 |
|
| 140 |
const wrapper = find('wrapper'); |
| 141 |
assert.exists(wrapper); |
| 142 |
assert.equal(wrapper.children.length, 2); |
| 143 |
}) |
| 144 |
|
| 145 |
it('OOB outerHTML with strip:true replaces with multiple children', async function() { |
| 146 |
mockResponse('GET', '/api', '<div id="main">Main</div><wrapper hx-swap-oob="outerHTML strip:true target:#target"><section>A</section><section>B</section><aside>C</aside></wrapper>'); |
| 147 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="target">Old</div>'); |
| 148 |
find('#btn').click() |
| 149 |
await forRequest(); |
| 150 |
|
| 151 |
|
| 152 |
assert.isUndefined(find('#target')); |
| 153 |
assert.isUndefined(find('wrapper')); |
| 154 |
const sections = playground().querySelectorAll('section'); |
| 155 |
assert.equal(sections.length, 2); |
| 156 |
const aside = find('aside'); |
| 157 |
assert.exists(aside); |
| 158 |
}) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
it('OOB with SVG and default strip preserves namespace', async function() { |
| 165 |
mockResponse('GET', '/api', '<div id="main">Main</div><svg hx-swap-oob="beforeend:#canvas"><circle cx="50" cy="50" r="20"></circle><circle cx="100" cy="100" r="30"><circle></svg>'); |
| 166 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><svg id="canvas" xmlns="http://www.w3.org/2000/svg"></svg>'); |
| 167 |
find('#btn').click() |
| 168 |
await forRequest(); |
| 169 |
|
| 170 |
|
| 171 |
const canvas = find('#canvas'); |
| 172 |
assert.equal(canvas.children.length, 2); |
| 173 |
assert.equal(canvas.children[0].tagName, 'circle'); |
| 174 |
assert.equal(canvas.children[0].namespaceURI, 'http://www.w3.org/2000/svg'); |
| 175 |
}) |
| 176 |
|
| 177 |
it('OOB with SVG and strip:false creates nested SVG', async function() { |
| 178 |
mockResponse('GET', '/api', '<div id="main">Main</div><svg hx-swap-oob="beforeend target:#canvas strip:false" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="20"></circle></svg>'); |
| 179 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><svg id="canvas" xmlns="http://www.w3.org/2000/svg"></svg>'); |
| 180 |
find('#btn').click() |
| 181 |
await forRequest(); |
| 182 |
|
| 183 |
|
| 184 |
const canvas = find('#canvas'); |
| 185 |
const nestedSvg = canvas.querySelector('svg'); |
| 186 |
assert.exists(nestedSvg); |
| 187 |
assert.equal(nestedSvg.children.length, 1); |
| 188 |
assert.equal(nestedSvg.children[0].tagName, 'circle'); |
| 189 |
}) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
it('Partial with strip:true extracts children', async function() { |
| 196 |
mockResponse('GET', '/api', '<div id="main">Main</div><hx-partial hx-target="#target" hx-swap="innerHTML strip:true"><wrapper><span>A</span><span>B</span></wrapper></hx-partial>'); |
| 197 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="target">Original</div>'); |
| 198 |
find('#btn').click() |
| 199 |
await forRequest(); |
| 200 |
|
| 201 |
|
| 202 |
const target = find('#target'); |
| 203 |
assert.equal(target.children.length, 2); |
| 204 |
assert.equal(target.children[0].tagName, 'SPAN'); |
| 205 |
assert.isUndefined(find('#target wrapper')); |
| 206 |
}) |
| 207 |
|
| 208 |
it('Partial with strip:false keeps wrapper', async function() { |
| 209 |
mockResponse('GET', '/api', '<div id="main">Main</div><hx-partial hx-target="#target" hx-swap="innerHTML strip:false"><wrapper><span>A</span><span>B</span></wrapper></hx-partial>'); |
| 210 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="target">Original</div>'); |
| 211 |
find('#btn').click() |
| 212 |
await forRequest(); |
| 213 |
|
| 214 |
|
| 215 |
const wrapper = find('#target wrapper'); |
| 216 |
assert.exists(wrapper); |
| 217 |
assert.equal(wrapper.children.length, 2); |
| 218 |
}) |
| 219 |
|
| 220 |
it('Partial without strip modifier keeps wrapper (default)', async function() { |
| 221 |
mockResponse('GET', '/api', '<div id="main">Main</div><hx-partial hx-target="#target" hx-swap="innerHTML"><wrapper><span>A</span><span>B</span></wrapper></hx-partial>'); |
| 222 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><div id="target">Original</div>'); |
| 223 |
find('#btn').click() |
| 224 |
await forRequest(); |
| 225 |
|
| 226 |
|
| 227 |
const wrapper = find('#target wrapper'); |
| 228 |
assert.exists(wrapper); |
| 229 |
}) |
| 230 |
|
| 231 |
it('Partial with SVG and strip:true extracts circles', async function() { |
| 232 |
mockResponse('GET', '/api', '<div id="main">Main</div><hx-partial hx-target="#canvas" hx-swap="innerHTML strip:true"><svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="20"/><circle cx="100" cy="100" r="30"/></svg></hx-partial>'); |
| 233 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#main">Get</button><svg id="canvas" xmlns="http://www.w3.org/2000/svg"></svg>'); |
| 234 |
find('#btn').click() |
| 235 |
await forRequest(); |
| 236 |
|
| 237 |
|
| 238 |
const canvas = find('#canvas'); |
| 239 |
assert.equal(canvas.children.length, 2); |
| 240 |
assert.equal(canvas.children[0].tagName, 'circle'); |
| 241 |
assert.equal(canvas.children[0].namespaceURI, 'http://www.w3.org/2000/svg'); |
| 242 |
}) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
it('Strip with single text node extracts text', async function() { |
| 249 |
mockResponse('GET', '/api', '<wrapper>Just text</wrapper>'); |
| 250 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-swap="innerHTML strip:true">Get</button><div id="target"></div>'); |
| 251 |
find('#btn').click() |
| 252 |
await forRequest(); |
| 253 |
|
| 254 |
|
| 255 |
const target = find('#target'); |
| 256 |
assert.equal(target.textContent, 'Just text'); |
| 257 |
assert.equal(target.children.length, 0); |
| 258 |
}) |
| 259 |
|
| 260 |
it('Strip only removes one level', async function() { |
| 261 |
mockResponse('GET', '/api', '<outer><inner><p>Content</p></inner></outer>'); |
| 262 |
createProcessedHTML('<button id="btn" hx-get="/api" hx-target="#target" hx-swap="innerHTML strip:true">Get</button><div id="target"></div>'); |
| 263 |
find('#btn').click() |
| 264 |
await forRequest(); |
| 265 |
|
| 266 |
|
| 267 |
const target = find('#target'); |
| 268 |
const inner = find('#target inner'); |
| 269 |
assert.exists(inner); |
| 270 |
assert.isUndefined(find('#target outer')); |
| 271 |
assert.equal(inner.children[0].tagName, 'P'); |
| 272 |
}) |
| 273 |
}) |