Skip to content

Commit

Permalink
fix: handle is attribute on elements with spread (#12056)
Browse files Browse the repository at this point in the history
The `is` attribute is special and always needs to be part of the static template string, or else it wouldn't be taken into account on initialization
fixes #12052
  • Loading branch information
dummdidumm authored Jun 17, 2024
1 parent c3da6a4 commit a1b6cc6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-waves-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: handle `is` attribute on elements with spread
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ function serialize_element_spread_attributes(
// TODO: handle contains_call_expression
const [, value] = serialize_attribute_value(attribute.value, context);

if (
name === 'is' &&
value.type === 'Literal' &&
context.state.metadata.namespace === 'html'
) {
context.state.template.push(` is="${escape_html(value.value, true)}"`);
continue;
}

if (
is_event_attribute(attribute) &&
(attribute.value[0].expression.type === 'ArrowFunctionExpression' ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],
test({ assert, target, logs }) {
const [b1, b2] = target.querySelectorAll('button');

b1.click();
flushSync();
assert.deepEqual(logs, ['works']);

b2.click();
flushSync();
assert.deepEqual(logs, ['works', 'works']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts" context="module">
if (!customElements.get('x-button')) {
class XButton extends HTMLButtonElement {
connectedCallback() {
this.addEventListener('click', () => console.log('works'));
}
}
customElements.define('x-button', XButton, { extends: 'button' });
}
</script>

<script lang="ts">
let { ...props } = $props();
</script>

<button is="x-button">click me</button>
<button {...props} is="x-button">click me</button>

0 comments on commit a1b6cc6

Please sign in to comment.