-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_v_html_test.go
More file actions
179 lines (167 loc) · 5.08 KB
/
Copy patheval_v_html_test.go
File metadata and controls
179 lines (167 loc) · 5.08 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
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestVue_EvalVHtml(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "simple v-html",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "<span>inner</span>"},
expected: "<div><span>inner</span></div>",
},
{
name: "v-html with empty string",
template: "<div v-html=\"html\">original</div>",
data: map[string]any{"html": ""},
expected: "<div></div>",
},
{
name: "v-html missing variable",
template: "<div v-html=\"missing\">fallback</div>",
data: map[string]any{},
expected: "<div>fallback</div>",
},
{
name: "v-html replaces children",
template: "<div v-html=\"content\"><p>original</p></div>",
data: map[string]any{"content": "<b>replaced</b>"},
expected: "<div><b>replaced</b></div>",
},
{
name: "v-html with multiple elements",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "<p>one</p><p>two</p>"},
expected: "<div><p>one</p><p>two</p></div>",
},
{
name: "v-html with nested html",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "<div><span><b>nested</b></span></div>"},
expected: "<div><div><span><b>nested</b></span></div></div>",
},
{
name: "v-html with text content",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "plain text"},
expected: "<div>plain text</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_EvalVHtml_NoReinterpolation(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "v-html preserves interpolation syntax in variable value",
template: "<code v-html=\"code\"></code>",
data: map[string]any{"code": `echo "${{ version }}"`},
expected: `<code>echo "${{ version }}"</code>`,
},
{
name: "v-html does not re-interpolate variable contents",
template: "<div v-html=\"content\"></div>",
data: map[string]any{"content": "{{ name }}", "name": "Alice"},
expected: "<div>{{ name }}</div>",
},
{
name: "v-html with HTML containing interpolation-like syntax",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "<p>Use {{ variable }} in templates</p>"},
expected: "<div><p>Use {{ variable }} in templates</p></div>",
},
{
name: "v-html with pipe-like content not treated as filter",
template: "<div v-html=\"content\"></div>",
data: map[string]any{"content": "value | filter"},
expected: "<div>value | filter</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_EvalVHtml_PreservesWhitespace(t *testing.T) {
// Test that v-html 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-html preserves newlines in text",
template: "<div v-html=\"html\"></div>",
data: map[string]any{"html": "line1\nline2"},
expected: "<div>line1\nline2</div>\n",
},
{
name: "v-html preserves indentation in HTML without re-indenting",
template: "<div v-html=\"html\"></div>",
data: map[string]any{
"html": "<span>\n indented\n</span>",
},
// v-html content is output as-is without any system indentation
expected: "<div><span>\n indented\n</span></div>\n",
},
{
name: "v-html does not re-indent multi-element content",
template: "<div v-html=\"html\"></div>",
data: map[string]any{
"html": "<p>one</p>\n<p>two</p>",
},
// v-html content preserves its own whitespace exactly
expected: "<div><p>one</p>\n<p>two</p></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())
})
}
}