mirror of https://github.com/sveltejs/svelte
fix: properly set `value` property of custom elements (#15206)
Avoid going through the `element.value = element.__value = newValue` condition because `__value` is actually how Lit stores the current value on the element, and messing with that would break things: Lit would think the value hasn't changed (because `__value` is already set to the new value by us) and doesn't fire an update. fixes #15194pull/15216/head
parent
872ee51378
commit
6f392d679b
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: properly set `value` property of custom elements
|
@ -1,19 +1,24 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
mode: ['client', 'server'],
|
||||
mode: ['client'],
|
||||
async test({ assert, target }) {
|
||||
const my_element = /** @type HTMLElement & { object: { test: true }; } */ (
|
||||
target.querySelector('my-element')
|
||||
);
|
||||
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
|
||||
target.querySelector('a')
|
||||
);
|
||||
assert.equal(my_element.getAttribute('string'), 'test');
|
||||
assert.equal(my_element.hasAttribute('object'), false);
|
||||
assert.deepEqual(my_element.object, { test: true });
|
||||
|
||||
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
|
||||
target.querySelector('a')
|
||||
);
|
||||
assert.equal(my_link.getAttribute('string'), 'test');
|
||||
assert.equal(my_link.hasAttribute('object'), false);
|
||||
assert.deepEqual(my_link.object, { test: true });
|
||||
|
||||
const [value1, value2] = target.querySelectorAll('value-element');
|
||||
assert.equal(value1.shadowRoot?.innerHTML, '<span>test</span>');
|
||||
assert.equal(value2.shadowRoot?.innerHTML, '<span>test</span>');
|
||||
}
|
||||
});
|
||||
|
@ -1,2 +1,22 @@
|
||||
<script module>
|
||||
customElements.define('value-element', class extends HTMLElement {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
set value(v) {
|
||||
if (this.__value !== v) {
|
||||
this.__value = v;
|
||||
this.shadowRoot.innerHTML = `<span>${v}</span>`;
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<my-element string="test" object={{ test: true }}></my-element>
|
||||
<a is="my-link" string="test" object={{ test: true }}></a>
|
||||
|
||||
<value-element value="test"></value-element>
|
||||
<value-element {...{value: "test"}}></value-element>
|
||||
|
Loading…
Reference in new issue