-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_v_text_test.go
More file actions
279 lines (261 loc) · 7.62 KB
/
Copy patheval_v_text_test.go
File metadata and controls
279 lines (261 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestVue_EvalVText(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "simple v-text",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": "plain text"},
expected: "<div>plain text</div>",
},
{
name: "v-text escapes HTML",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": "<span>inner</span>"},
expected: "<div><span>inner</span></div>",
},
{
name: "v-text with empty string",
template: "<div v-text=\"text\">original</div>",
data: map[string]any{"text": ""},
expected: "<div></div>",
},
{
name: "v-text missing variable",
template: "<div v-text=\"missing\">fallback</div>",
data: map[string]any{},
expected: "<div>fallback</div>",
},
{
name: "v-text replaces children",
template: "<div v-text=\"text\"><p>original</p></div>",
data: map[string]any{"text": "replaced"},
expected: "<div>replaced</div>",
},
{
name: "v-text with special characters",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": "a & b < c > d"},
expected: "<div>a & b < c > d</div>",
},
{
name: "v-text with quotes",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": `"quoted" value`},
expected: "<div>"quoted" value</div>",
},
{
name: "v-text same as interpolation",
template: "<span v-text=\"msg\"></span>",
data: map[string]any{"msg": "hello"},
expected: "<span>hello</span>",
},
{
name: "v-text preserves interpolation syntax in variable value",
template: "<code v-text=\"code\"></code>",
data: map[string]any{"code": `echo "${{ version }}"`},
expected: `<code>echo "${{ version }}"</code>`,
},
{
name: "v-text does not re-interpolate variable contents",
template: "<div v-text=\"content\"></div>",
data: map[string]any{"content": "{{ name }}", "name": "Alice"},
expected: "<div>{{ name }}</div>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalVText_PreservesWhitespace(t *testing.T) {
// Test that v-text content preserves its original whitespace without re-indenting
tests := []struct {
name string
template string
data map[string]any
expected string // exact expected output (not normalized)
}{
{
name: "v-text preserves newlines in text",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": "line1\nline2"},
expected: "<div>line1\nline2</div>\n",
},
{
name: "v-text escapes HTML with newlines",
template: "<div v-text=\"text\"></div>",
data: map[string]any{
"text": "<span>\n content\n</span>",
},
// v-text content is escaped and output as-is without re-indenting
expected: "<div><span>\n content\n</span></div>\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
// Compare exact output to verify whitespace is preserved
require.Equal(t, tc.expected, buf.String())
})
}
}
func TestVue_EvalVText_WithFiltersAndFunctions(t *testing.T) {
// Test v-text with pipe filters and complex expressions
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "v-text with upper filter",
template: "<span v-text=\"name | upper\"></span>",
data: map[string]any{"name": "hello world"},
expected: "<span>HELLO WORLD</span>",
},
{
name: "v-text with multiple filters",
template: "<span v-text=\"text | trim | upper\"></span>",
data: map[string]any{"text": " hello world "},
expected: "<span>HELLO WORLD</span>",
},
{
name: "v-text with default filter",
template: "<span v-text=\"missing | default('placeholder')\"></span>",
data: map[string]any{},
expected: "<span>placeholder</span>",
},
{
name: "v-text with function call",
template: "<span v-text=\"len(items)\"></span>",
data: map[string]any{"items": []int{1, 2, 3}},
expected: "<span>3</span>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalVText_WithComplexVariables(t *testing.T) {
// Test v-text with nested property access and variables
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "v-text with nested property access",
template: "<span v-text=\"user.profile.name\"></span>",
data: map[string]any{
"user": map[string]any{
"profile": map[string]any{
"name": "Alice",
},
},
},
expected: "<span>Alice</span>",
},
{
name: "v-text with array index",
template: "<span v-text=\"items.0\"></span>",
data: map[string]any{
"items": []string{"first", "second", "third"},
},
expected: "<span>first</span>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalVText_HTMLEscaping(t *testing.T) {
// Test various HTML content that needs escaping
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "v-text with mixed entities",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": "a < b && c > d"},
expected: "<div>a < b && c > d</div>",
},
{
name: "v-text with single quotes",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": `it's a test`},
expected: "<div>it's a test</div>",
},
{
name: "v-text with script injection attempt",
template: "<div v-text=\"text\"></div>",
data: map[string]any{"text": `"><script>alert('xss')</script>`},
expected: "<div>"><script>alert('xss')</script></div>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}