Skip to content

Commit 31d939f

Browse files
authored
fix: port over props that were set prior to initialization (#9701)
...and then delete the property descriptor that shadows the prototype descriptor fixes #9487
1 parent d15bc82 commit 31d939f

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

.changeset/wicked-chefs-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: port over props that were set prior to initialization

packages/svelte/src/runtime/internal/Component.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ if (typeof HTMLElement === 'function') {
270270
this.$$d[name] = get_custom_element_value(name, attribute.value, this.$$p_d, 'toProp');
271271
}
272272
}
273+
// Port over props that were set programmatically before ce was initialized
274+
for (const key in this.$$p_d) {
275+
if (!(key in this.$$d) && this[key] !== undefined) {
276+
this.$$d[key] = this[key]; // don't transform, these were set through JavaScript
277+
delete this[key]; // remove the property that shadows the getter/setter
278+
}
279+
}
273280
this.$$c = new this.$$ctor({
274281
target: this.shadowRoot || this,
275282
props: {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<svelte:options
2+
customElement={null}
3+
/>
4+
5+
<script>
6+
export let prop;
7+
</script>
8+
9+
<p>{prop}</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as assert from 'assert.js';
2+
import { tick } from 'svelte';
3+
import Main from './main.svelte';
4+
5+
export default async function (target) {
6+
target.innerHTML = '<custom-element red white></custom-element>';
7+
const ce = target.querySelector('custom-element');
8+
ce.prop = 1;
9+
customElements.define('custom-element', Main.element);
10+
await tick();
11+
await tick();
12+
13+
const ce_root = target.querySelector('custom-element').shadowRoot;
14+
const p = ce_root.querySelector('p');
15+
16+
assert.equal(p.textContent, '1');
17+
18+
ce.prop = 2;
19+
await tick();
20+
await tick();
21+
22+
assert.equal(p.textContent, '2');
23+
}

0 commit comments

Comments
 (0)