Skip to content

Commit 9746b87

Browse files
authored
fix(synthetic-shadow): dont add key for slotted text vnode (salesforce#1946)
1 parent e4800e0 commit 9746b87

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

packages/@lwc/engine-core/src/framework/vm.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ export function allocateInSlot(vm: VM, children: VNodes) {
616616
// which might have similar keys. Each vnode will always have a key that
617617
// starts with a numeric character from compiler. In this case, we add a unique
618618
// notation for slotted vnodes keys, e.g.: `@foo:1:1`
619-
vnode.key = `@${slotName}:${vnode.key}`;
619+
if (!isUndefined(vnode.key)) {
620+
vnode.key = `@${slotName}:${vnode.key}`;
621+
}
620622
ArrayPush.call(vnodes, vnode);
621623
}
622624
if (isFalse(vm.isDirty)) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createElement } from 'lwc';
2+
import Container from 'x/container';
3+
4+
describe('Dynamic diffing algo for slotted text', () => {
5+
it('should not confuse text vnodes when moving elements', function () {
6+
const elm = createElement('x-container', { is: Container });
7+
document.body.appendChild(elm);
8+
9+
expect(elm.shadowRoot.textContent).toBe('first textDisplay Boolean Value: truesecond text');
10+
elm.toggleVisibility();
11+
12+
return Promise.resolve()
13+
.then(() => {
14+
const text = elm.shadowRoot.textContent;
15+
expect(text).toBe('Display Boolean Value: false');
16+
17+
elm.toggleVisibility();
18+
})
19+
.then(() => {
20+
const text = elm.shadowRoot.textContent;
21+
expect(text).toBe('first textDisplay Boolean Value: truesecond text');
22+
});
23+
});
24+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<slot></slot>
3+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class Child extends LightningElement {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<x-child>
3+
<template if:true={visible}>
4+
<span>first text</span>
5+
</template>
6+
Display Boolean Value: {visible}
7+
<template if:true={visible}>
8+
<span>second text</span>
9+
</template>
10+
</x-child>
11+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LightningElement, api } from 'lwc';
2+
3+
export default class Container extends LightningElement {
4+
visible = true;
5+
6+
@api toggleVisibility() {
7+
this.visible = !this.visible;
8+
}
9+
}

0 commit comments

Comments
 (0)