mirror of https://github.com/sveltejs/svelte
fix: remove memory leak from retaining old DOM elements (#11197)
* fix: remove memory leak from retaining old DOM elements * missing logic * fix dynamic html bugpull/11201/head
parent
63456f1df9
commit
777527b5a3
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
fix: remove memory leak from retaining old DOM elements
|
@ -0,0 +1,39 @@
|
||||
import { flushSync } from '../../../../src/index-client';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>add item</button><button>make span</button><button>reverse</button>`,
|
||||
|
||||
async test({ assert, target }) {
|
||||
const [btn1, btn2, btn3] = target.querySelectorAll('button');
|
||||
|
||||
flushSync(() => {
|
||||
btn1?.click();
|
||||
btn1?.click();
|
||||
btn1?.click();
|
||||
});
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button>add item</button><button>make span</button><button>reverse</button><div>Item 1</div><div>Item 2</div><div>Item 3</div>`
|
||||
);
|
||||
|
||||
flushSync(() => {
|
||||
btn2?.click();
|
||||
});
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button>add item</button><button>make span</button><button>reverse</button><span>Item 1</span><span>Item 2</span><span>Item 3</span>`
|
||||
);
|
||||
|
||||
flushSync(() => {
|
||||
btn3?.click();
|
||||
});
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button>add item</button><button>make span</button><button>reverse</button><span>Item 3</span><span>Item 2</span><span>Item 1</span>`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,30 @@
|
||||
<script>
|
||||
let items = $state([]);
|
||||
|
||||
function add_item() {
|
||||
items.push({
|
||||
id: items.length,
|
||||
text: 'Item ' + (items.length + 1),
|
||||
html: '<div>Item ' + (items.length + 1) + '</div>',
|
||||
dom: null,
|
||||
})
|
||||
}
|
||||
|
||||
function make_span() {
|
||||
items.forEach(item => {
|
||||
item.html = item.html.replace(/div/g, 'span')
|
||||
})
|
||||
}
|
||||
|
||||
function reverse() {
|
||||
items.reverse();
|
||||
}
|
||||
</script>
|
||||
|
||||
<button on:click={add_item}>add item</button>
|
||||
<button on:click={make_span}>make span</button>
|
||||
<button on:click={reverse}>reverse</button>
|
||||
|
||||
{#each items as item (item.id)}
|
||||
{@html item.html}
|
||||
{/each}
|
Loading…
Reference in new issue