ReviewOS

also looking at this

stacks/stx

fix(client): preserve client-signal {⁠{ }} + bind SVG classes via setAttribute

#1774
Closed glennmichael123 wants to merge fix/client-signal-interpolation-and-svg-bindclass into main
4 files +354 -293
packages/stx/test/expressions/client-signal-preserve.test.tsadded+58-0
Changes to packages/stx/test/expressions/client-signal-preserve.test.ts
@@ -0,0 +1,58 @@
1import { describe, expect, it } from 'bun:test'
2import { processExpressions } from '../../src/expressions'
3import { generateSignalsRuntimeDev } from '../../src/signals'
4
5/**
6 * Regression tests for: a `{⁠{ }}` expression that references a client-only
7 * signal must be PRESERVED for client hydration, not baked to its
8 * server-evaluated value.
9 *
10 * The server SSG interpolator previously preserved a `{⁠{ }}` for the client
11 * ONLY when the whole expression server-evaluated to exactly `undefined`.
12 * A bare `{⁠{ likes }}` (signal absent from the server context) evaluates to
13 * `undefined` and was preserved. But a COMPOUND expression such as
14 * `{⁠{ liked ? … : likes + ' people' }}` evaluates to a *defined* string
15 * ("undefined people…") and was therefore baked into static HTML — freezing
16 * it at the undefined-substituted text with no way for the client to re-bind.
17 * Template literals were doubly affected: the `${…}` body was stripped before
18 * the "references a client var?" check, so its signal was never seen.
19 */
20
21// A `:if` directive marks the template client-reactive (usesSignalsInScript),
22// matching a real signal component. `likes`/`liked`/`rating` are client-only
23// signals (absent from the server context); `serverVar` IS server context.
24const reactive = (expr: string): string =>
25 processExpressions(`<div :if="x">${expr}</div>`, { serverVar: 'S' }, 't.stx')
26
27describe('client-signal {⁠{ }} preservation', () => {
28 it('preserves a COMPOUND expression referencing a client-only signal', () => {
29 const out = reactive(`{⁠{ likes + ' people find this helpful' }}`)
30 expect(out).toContain(`{⁠{ likes + ' people find this helpful' }}`)
31 })
32
33 it('preserves a nested ternary over client-only signals', () => {
34 const expr = `{⁠{ liked ? 'You find this helpful' : likes === 0 ? 'Be the first' : likes + ' people' }}`
35 expect(reactive(expr)).toContain(expr)
36 })
37
38 it('preserves a TEMPLATE LITERAL whose ${} references a client-only signal', () => {
39 expect(reactive('{⁠{ `${rating}/5` }}')).toContain('{⁠{ `${rating}/5` }}')
40 expect(reactive('{⁠{ `(${totalResults})` }}')).toContain('{⁠{ `(${totalResults})` }}')
41 })
42
43 it('still BAKES an expression that uses only server-context vars (no over-preserve)', () => {
44 expect(reactive('{⁠{ 1 + 2 }}')).toContain('3')
45 expect(reactive('{⁠{ serverVar }}')).toContain('S')
46 // template literal over ONLY context vars must bake, not preserve
47 const tpl = reactive('{⁠{ `Hello ${serverVar}` }}')
48 expect(tpl).toContain('Hello S')
49 expect(tpl).not.toContain('{⁠{')
50 })
51
52 it('runtime binds classes on SVG via setAttribute (SVGElement.className is read-only)', () => {
53 // Guards the companion client fix: `bindClass` must not do `el.className = …`
54 // on an SVG element (throws, aborting hydration for the rest of the page).
55 const rt = generateSignalsRuntimeDev()
56 expect(rt).toContain(`el.setAttribute('class'`)
57 })
58})