fix: ensure StyleDirective and ClassDirective are marked as dynamic (#13205)

If we don't, the corresponding runtime code would never be created if in a static sub tree. Fixes #13193
pull/13217/head
Dominic Gannaway 1 year ago committed by GitHub
parent 0b25e2be0e
commit a7a477804e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure StyleDirective and ClassDirective are marked as dynamic

@ -1,10 +1,13 @@
/** @import { AST } from '#compiler' */ /** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
import { mark_subtree_dynamic } from './shared/fragment.js';
/** /**
* @param {AST.ClassDirective} node * @param {AST.ClassDirective} node
* @param {Context} context * @param {Context} context
*/ */
export function ClassDirective(node, context) { export function ClassDirective(node, context) {
mark_subtree_dynamic(context.path);
context.next({ ...context.state, expression: node.metadata.expression }); context.next({ ...context.state, expression: node.metadata.expression });
} }

@ -13,6 +13,8 @@ export function StyleDirective(node, context) {
e.style_directive_invalid_modifier(node); e.style_directive_invalid_modifier(node);
} }
mark_subtree_dynamic(context.path);
if (node.value === true) { if (node.value === true) {
// get the binding for node.name and change the binding to state // get the binding for node.name and change the binding to state
let binding = context.state.scope.get(node.name); let binding = context.state.scope.get(node.name);
@ -22,8 +24,6 @@ export function StyleDirective(node, context) {
node.metadata.expression.has_state = true; node.metadata.expression.has_state = true;
} }
} }
mark_subtree_dynamic(context.path);
} else { } else {
context.next(); context.next();

@ -0,0 +1,12 @@
import { test } from '../../test';
export default test({
html: `<div><p class="foo" style="color: red;">This text should be red with a class of foo</p></div>`,
async test({ assert, target }) {
const p = target.querySelector('p');
assert.equal(p?.className, `foo`);
assert.equal(p?.style.color, `red`);
}
});

@ -0,0 +1,3 @@
<div>
<p style:color="red" class:foo={true}>This text should be red with a class of foo</p>
</div>
Loading…
Cancel
Save