|
| 1 | +/** |
| 2 | + * Reproducer for the slot-projection cycle bug: |
| 3 | + * |
| 4 | + * HierarchyRequestError: Failed to execute 'appendChild' on 'Node': |
| 5 | + * The new child element contains the parent. |
| 6 | + * |
| 7 | + * Two scenarios are known to trigger it (both surfaced when I tried to |
| 8 | + * land integration tests for signals + slot interaction): |
| 9 | + * |
| 10 | + * 1. A parent custom element's render conditionally outputs a slot |
| 11 | + * host with a nested child. Flipping the condition re-renders |
| 12 | + * the parent, which swaps in a NEW slot-host element containing |
| 13 | + * a child as authored content. Projection on the new slot host |
| 14 | + * then throws. |
| 15 | + * |
| 16 | + * 2. A slot host's own render swaps between two different shapes |
| 17 | + * around the slot (e.g. compact `<section><slot></slot></section>` |
| 18 | + * vs expanded `<section><header>...</header><slot></slot></section>`). |
| 19 | + * Authored children stay alive on the host; projection should |
| 20 | + * re-target the new slot. It throws instead. |
| 21 | + * |
| 22 | + * Both cases are valid webjs use, both currently fail. This file is |
| 23 | + * the failing reference; flip the wrapper test back to `test(...)` |
| 24 | + * once the bug is fixed. |
| 25 | + */ |
| 26 | +import { html } from '../../packages/core/src/html.js'; |
| 27 | +import { WebComponent } from '../../packages/core/src/component.js'; |
| 28 | +import { signal } from '../../packages/core/src/signal.js'; |
| 29 | + |
| 30 | +const assert = { |
| 31 | + ok: (v, msg) => { if (!v) throw new Error(msg || `Expected truthy, got ${v}`); }, |
| 32 | + equal: (a, b, msg) => { if (a !== b) throw new Error(msg || `Expected ${JSON.stringify(b)}, got ${JSON.stringify(a)}`); }, |
| 33 | +}; |
| 34 | + |
| 35 | +suite('slot projection cycle (regression)', () => { |
| 36 | + let nextTag = 0; |
| 37 | + const newTag = (base) => `${base}-cycle-${++nextTag}`; |
| 38 | + |
| 39 | + test('parent re-render swaps in a new slot host with nested authored child', async () => { |
| 40 | + const showChild = signal(false); |
| 41 | + const Shell = newTag('cycle-shell'); |
| 42 | + const Child = newTag('cycle-child'); |
| 43 | + const Parent = newTag('cycle-parent'); |
| 44 | + |
| 45 | + class ShellEl extends WebComponent { |
| 46 | + render() { return html`<section><slot></slot></section>`; } |
| 47 | + } |
| 48 | + customElements.define(Shell, ShellEl); |
| 49 | + |
| 50 | + class ChildEl extends WebComponent { |
| 51 | + render() { return html`<i>child</i>`; } |
| 52 | + } |
| 53 | + customElements.define(Child, ChildEl); |
| 54 | + |
| 55 | + // Static tag literals at the JS-template level. The Parent class |
| 56 | + // is generated once, after the inner classes are defined, so we |
| 57 | + // can splice the tag names into a Function constructor without |
| 58 | + // dynamic interpolation inside the `html` template. |
| 59 | + const ParentFactory = new Function( |
| 60 | + 'html', 'WebComponent', 'showChild', |
| 61 | + `class ParentEl extends WebComponent { |
| 62 | + render() { |
| 63 | + return showChild.get() |
| 64 | + ? html\`<${Shell}><${Child}></${Child}></${Shell}>\` |
| 65 | + : html\`<${Shell}></${Shell}>\`; |
| 66 | + } |
| 67 | + } |
| 68 | + return ParentEl;` |
| 69 | + ); |
| 70 | + const ParentEl = ParentFactory(html, WebComponent, showChild); |
| 71 | + customElements.define(Parent, ParentEl); |
| 72 | + |
| 73 | + const root = document.createElement(Parent); |
| 74 | + document.body.appendChild(root); |
| 75 | + await root.updateComplete; |
| 76 | + assert.equal(root.querySelectorAll('i').length, 0); |
| 77 | + |
| 78 | + showChild.set(true); |
| 79 | + await Promise.resolve(); await Promise.resolve(); |
| 80 | + await root.updateComplete; |
| 81 | + await Promise.resolve(); await Promise.resolve(); |
| 82 | + assert.equal(root.querySelectorAll('i').length, 1, 'child renders through projection'); |
| 83 | + |
| 84 | + showChild.set(false); |
| 85 | + await Promise.resolve(); await Promise.resolve(); |
| 86 | + await root.updateComplete; |
| 87 | + await Promise.resolve(); await Promise.resolve(); |
| 88 | + assert.equal(root.querySelectorAll('i').length, 0, 'child gone when signal flips back'); |
| 89 | + |
| 90 | + root.remove(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('slot host re-renders with a different wrapper shape around its slot', async () => { |
| 94 | + const expanded = signal(false); |
| 95 | + const T = newTag('cycle-wrap'); |
| 96 | + class ShellEl extends WebComponent { |
| 97 | + render() { |
| 98 | + return expanded.get() |
| 99 | + ? html`<section class="expanded"><header>BIG</header><slot></slot></section>` |
| 100 | + : html`<section class="compact"><slot></slot></section>`; |
| 101 | + } |
| 102 | + } |
| 103 | + customElements.define(T, ShellEl); |
| 104 | + |
| 105 | + const shell = document.createElement(T); |
| 106 | + const child = document.createElement('p'); |
| 107 | + child.id = 'projected-child'; |
| 108 | + child.textContent = 'authored'; |
| 109 | + shell.appendChild(child); |
| 110 | + document.body.appendChild(shell); |
| 111 | + await shell.updateComplete; |
| 112 | + const childRef = shell.querySelector('#projected-child'); |
| 113 | + assert.ok(childRef, 'child projected on first render'); |
| 114 | + assert.ok(shell.querySelector('section.compact')); |
| 115 | + |
| 116 | + expanded.set(true); |
| 117 | + await Promise.resolve(); await Promise.resolve(); |
| 118 | + await shell.updateComplete; |
| 119 | + await Promise.resolve(); await Promise.resolve(); |
| 120 | + assert.ok(shell.querySelector('section.expanded')); |
| 121 | + assert.ok(shell.querySelector('header')); |
| 122 | + const childRef2 = shell.querySelector('#projected-child'); |
| 123 | + assert.ok(childRef2, 'child still present after shape change'); |
| 124 | + assert.ok(childRef2 === childRef, 'DOM identity preserved across the host re-render'); |
| 125 | + |
| 126 | + expanded.set(false); |
| 127 | + await Promise.resolve(); await Promise.resolve(); |
| 128 | + await shell.updateComplete; |
| 129 | + await Promise.resolve(); await Promise.resolve(); |
| 130 | + assert.ok(shell.querySelector('section.compact')); |
| 131 | + assert.equal(shell.querySelector('header'), null); |
| 132 | + assert.ok(shell.querySelector('#projected-child') === childRef, 'identity survives round-trip'); |
| 133 | + |
| 134 | + shell.remove(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments