fix: detect style shorthands as stateful variables in legacy mode (#11421)

Fixes #11417
pull/11432/head
Paolo Ricciuti 9 months ago committed by GitHub
parent 0f4f3d7df0
commit f64d16931d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: detect style shorthands as stateful variables in legacy mode

@ -870,6 +870,16 @@ const legacy_scope_tweaker = {
}
}
}
},
StyleDirective(node, { state }) {
// the case for node.value different from true is already covered by the Identifier visitor
if (node.value === true) {
// get the binding for node.name and change the binding to state
let binding = state.scope.get(node.name);
if (binding?.mutated && binding.kind === 'normal') {
binding.kind = 'state';
}
}
}
};

@ -0,0 +1,33 @@
import { test } from '../../test';
export default test({
html: `
<p style="color: red;"></p>
<p style="color: red;"></p>
<button></button>
`,
async test({ assert, target, window }) {
const [p1, p2] = target.querySelectorAll('p');
assert.equal(window.getComputedStyle(p1).color, 'red');
assert.equal(window.getComputedStyle(p2).color, 'red');
const btn = target.querySelector('button');
console.log(btn);
btn?.click();
await Promise.resolve();
assert.htmlEqual(
target.innerHTML,
`
<p style="color: green;"></p>
<p style="color: green;"></p>
<button></button>
`
);
assert.equal(window.getComputedStyle(p1).color, 'green');
assert.equal(window.getComputedStyle(p2).color, 'green');
}
});

@ -0,0 +1,15 @@
<script>
let color = "red";
function change(){
color = "green";
}
</script>
<p style:color></p>
{#each [1] as _}
<p style:color></p>
{/each}
<button on:click={change}></button>
Loading…
Cancel
Save