-
Notifications
You must be signed in to change notification settings - Fork 69
fix: removeBetween leaves the end marker it was asked to remove #1292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5012aac
fix: removeBetween leaves the end marker it was asked to remove
vivek7405 e1fa267
test: make the child-hole marker cases observe the leak they claim
vivek7405 6b74ace
test: cover the marker teardown in a real browser and inside a slot
vivek7405 f20386c
fix: state the terminator argument as the claim the walk actually needs
vivek7405 b329899
docs: stop the docblock arguing a choice this change never made
vivek7405 9f1c068
docs: drop the last overclaim from the browser suite header
vivek7405 08d0134
test: correct what the browser cases claim over the unit suite
vivek7405 d0708ae
docs: cut the last three claims that outrun what I verified
vivek7405 949aa9a
test: make every leak case fail when it stops counting anything
vivek7405 21a6021
test: correct what protects the slot counter from a vacuous pass
vivek7405 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
packages/core/test/rendering/browser/marker-leak-on-teardown.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| /** | ||
| * Real-browser assertions for the teardown that takes its own end marker. | ||
| * | ||
| * The unit suite proves the accounting under linkedom, driving the renderer | ||
| * with bare `render()` calls. Two things are left for a real engine. A | ||
| * component reaches the same teardown through the update pipeline instead, so | ||
| * the leak is shown on the path an app actually takes. And `removeChild` runs | ||
| * a custom element's `disconnectedCallback` synchronously, which means author | ||
| * code lands part-way through the removal walk; that is worth pinning on the | ||
| * engines people run rather than on a shim, and no unit case covers it. | ||
| * | ||
| * (linkedom does fire `disconnectedCallback`, so the second one is a question | ||
| * of fidelity rather than of capability. Do not write the stronger claim.) | ||
| */ | ||
| import { html } from '../../../src/html.js'; | ||
| import { MARKER } from '../../../src/html.js'; | ||
| import { repeat } from '../../../src/repeat.js'; | ||
| import { WebComponent } from '../../../src/component.js'; | ||
|
|
||
| import { assert } from '../../../../../test/browser-assert.js'; | ||
|
|
||
| let uid = 0; | ||
| const tagName = (p) => `${p}-${uid++}`; | ||
|
|
||
| const tick = () => new Promise((r) => queueMicrotask(() => queueMicrotask(r))); | ||
|
|
||
| /** Count renderer bookends under `root`, at any depth. */ | ||
| function countBookends(root) { | ||
| let s = 0; | ||
| let e = 0; | ||
| const walk = (n) => { | ||
| for (const c of n.childNodes) { | ||
| if (c.nodeType === 8) { | ||
| if (c.data === `${MARKER}s`) s++; | ||
| else if (c.data === `${MARKER}e`) e++; | ||
| } else walk(c); | ||
| } | ||
| }; | ||
| walk(root); | ||
| return { s, e }; | ||
| } | ||
|
|
||
| const rows = (n) => Array.from({ length: n }, (_, i) => ({ id: i, t: `row ${i}` })); | ||
|
|
||
| suite('teardown takes its own end marker', () => { | ||
| test('a component churning a repeat() list stays flat on marker count', async () => { | ||
| const tag = tagName('marker-churn'); | ||
| class C extends WebComponent({ items: Array }) { | ||
| constructor() { | ||
| super(); | ||
| this.items = rows(4); | ||
| } | ||
| render() { | ||
| return html`<ul>${repeat(this.items, (it) => it.id, (it) => html`<li>${it.t}</li>`)}</ul>`; | ||
| } | ||
| } | ||
| C.register(tag); | ||
|
|
||
| const host = document.createElement(tag); | ||
| document.body.appendChild(host); | ||
| await host.updateComplete; | ||
| await tick(); | ||
|
|
||
| try { | ||
| const baseline = countBookends(host); | ||
| assert.equal(baseline.s, baseline.e, 'baseline is paired'); | ||
| assert.ok(baseline.e > 0, 'the list really rendered bookends'); | ||
| const survivor = host.querySelector('li'); | ||
|
|
||
| for (let i = 0; i < 5; i++) { | ||
| host.items = rows(2); | ||
| await host.updateComplete; | ||
| host.items = rows(4); | ||
| await host.updateComplete; | ||
| } | ||
| await tick(); | ||
|
|
||
| const got = countBookends(host); | ||
| assert.equal(got.s, got.e, `bookends unpaired: ${got.s} start, ${got.e} end`); | ||
| assert.equal(got.e, baseline.e, 'end markers drifted from baseline'); | ||
| assert.equal(host.querySelectorAll('li').length, 4, 'all four rows rendered'); | ||
| assert.equal(host.querySelector('li'), survivor, 'a row that never left kept its identity'); | ||
| } finally { | ||
| host.remove(); | ||
| } | ||
| }); | ||
|
|
||
| test('a disconnectedCallback running mid-walk does not derail the removal', async () => { | ||
| // This reds on the reverted fix like the case above (it counts bookends | ||
| // too), but that is not what it is for. It exists to prove the removal | ||
| // finishes, and the region stays renderable, while a `disconnectedCallback` | ||
| // runs synchronously in the middle of the walk, on the engines people | ||
| // actually run. | ||
| // | ||
| // The callback here writes OUTSIDE the range being torn down, which is what | ||
| // ordinary author code does. It deliberately does not move a node the walk | ||
| // is about to step onto: a range mutated underneath the walk overruns, that | ||
| // is true before and after this fix, and this case is not the place to | ||
| // claim otherwise. | ||
| const seen = []; | ||
| // A fixed tag, because a tag NAME is not a hole position in an `html` | ||
| // template; only attribute and child positions are. | ||
| const cellTag = 'marker-leak-cell'; | ||
| class Cell extends WebComponent({ label: String }) { | ||
| disconnectedCallback() { | ||
| super.disconnectedCallback?.(); | ||
| // Author code, running synchronously from inside `removeChild`, that | ||
| // writes to a container OUTSIDE the range being torn down. | ||
| seen.push(this.label); | ||
| const note = document.createElement('i'); | ||
| note.className = 'gone'; | ||
| note.textContent = this.label; | ||
| document.querySelector('#marker-sink')?.appendChild(note); | ||
| } | ||
| render() { | ||
| return html`<span>${this.label}</span>`; | ||
| } | ||
| } | ||
| Cell.register(cellTag); | ||
|
|
||
| const tag = tagName('marker-host'); | ||
| class C extends WebComponent({ items: Array }) { | ||
| constructor() { | ||
| super(); | ||
| this.items = rows(4); | ||
| } | ||
| render() { | ||
| return html`<ul>${repeat( | ||
| this.items, | ||
| (it) => it.id, | ||
| (it) => html`<li><marker-leak-cell label=${it.t}></marker-leak-cell></li>`, | ||
| )}</ul>`; | ||
| } | ||
| } | ||
| C.register(tag); | ||
|
|
||
| const sink = document.createElement('div'); | ||
| sink.id = 'marker-sink'; | ||
| document.body.appendChild(sink); | ||
| const host = document.createElement(tag); | ||
| document.body.appendChild(host); | ||
| await host.updateComplete; | ||
| await tick(); | ||
|
|
||
| try { | ||
| const baseline = countBookends(host); | ||
| assert.equal(baseline.s, baseline.e, 'baseline is paired'); | ||
| assert.ok(baseline.e > 0, 'the list really rendered bookends to count'); | ||
|
|
||
| host.items = rows(1); | ||
| await host.updateComplete; | ||
| await tick(); | ||
|
|
||
| assert.ok(seen.length > 0, 'a disconnectedCallback really ran during the removal'); | ||
| assert.equal(host.querySelectorAll('li').length, 1, 'the shrink completed'); | ||
| assert.equal(host.querySelector('li span').textContent, 'row 0', 'the survivor is intact'); | ||
|
|
||
| // The region is still usable: the part marker survived the walk, so a | ||
| // later grow renders back into the same place. | ||
| host.items = rows(4); | ||
| await host.updateComplete; | ||
| await tick(); | ||
|
|
||
| assert.equal(host.querySelectorAll('li').length, 4, 'the region still renders after the walk'); | ||
| const got = countBookends(host); | ||
| assert.equal(got.s, got.e, `bookends unpaired: ${got.s} start, ${got.e} end`); | ||
| assert.equal(got.e, baseline.e, 'end markers drifted from baseline'); | ||
| } finally { | ||
| host.remove(); | ||
| sink.remove(); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.