-
Notifications
You must be signed in to change notification settings - Fork 5
/
writeattr_test.go
280 lines (267 loc) · 6.85 KB
/
writeattr_test.go
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
280
package fir
import (
"bytes"
"strings"
"testing"
"github.com/yosssi/gohtml"
"golang.org/x/net/html"
)
func Test_addAttributes(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "key is present but no @ or x-on is present",
input: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div>
<span>Inner element</span>
</div>
</div>
`,
want: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div>
<span>Inner element</span>
</div>
</div>
`,
},
{
name: "key is present and @ is present but child already has key",
input: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div>
<span fir-key="" @click="console.log()">Inner element</span>
</div>
</div>
`,
want: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div>
<span fir-key="" @click="console.log()">Inner element</span>
</div>
</div>
`,
},
{
name: "key is present and @ is present and child does not have key",
input: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div x-on:click="doSomething()">
<span>Inner element</span>
</div>
</div>
`,
want: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div x-on:click="doSomething()" fir-key="parent-key">
<span>Inner element</span>
</div>
</div>
`,
},
{
name: "two elements: key is present and @ is present and child does not have key",
input: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div x-on:click="doSomething()">
<span>Inner element</span>
</div>
</div>
<div fir-key="parent-key-2">
<p>Hello, World!</p>
<div x-on:click="doSomething()">
<span>Inner element</span>
</div>
</div>
`,
want: `
<div fir-key="parent-key">
<p>Hello, World!</p>
<div x-on:click="doSomething()" fir-key="parent-key">
<span >Inner element</span>
</div>
</div>
<div fir-key="parent-key-2">
<p>Hello, World!</p>
<div x-on:click="doSomething()" fir-key="parent-key-2">
<span >Inner element</span>
</div>
</div>
`,
},
{
name: "add key attribute to nested children",
input: `
<div fir-key="1">
<div @fir:event:ok="" > </div>
<div> <button @click=""> </button></div>
<div> <div> <div> <input @change="" > </div> </div> </div>
<form @submit=""> </form>
</div>
`,
want: `
<div fir-key="1">
<div fir-key="1" @fir:event:ok="" class="fir-event-ok--1"> </div>
<div> <button @click="" fir-key="1"> </button> </div>
<div> <div> <div> <input @change="" fir-key="1"> </div> </div> </div>
<form @submit="" fir-key="1"> </form>
</div>
`,
},
{
name: "add key attribute to nested children and ignore child node already with key",
input: `
<div fir-key="1">
<div @fir:event:ok="" > </div>
<div fir-key="2"> <button @click=""> </button></div>
<div> <div> <div> <input @change="" > </div> </div> </div>
<form @submit=""> </form>
</div>
`,
want: `
<div fir-key="1">
<div fir-key="1" @fir:event:ok="" class="fir-event-ok--1"> </div>
<div fir-key="2"> <button @click="" fir-key="2"> </button> </div>
<div> <div> <div> <input @change="" fir-key="1"> </div> </div> </div>
<form @submit="" fir-key="1"> </form>
</div>
`,
},
{
name: "no filters in event string and key is not present",
input: `
<div
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
`,
want: `
<div class="fir-event-ok--tmpl1 fir-event-ok"
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
`,
},
{
name: "same event multiple elements: no filters in event string and key is not present",
input: `
<div
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
<div
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
`,
want: `
<div class="fir-event-ok--tmpl1 fir-event-ok"
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
<div class="fir-event-ok--tmpl1 fir-event-ok"
@fir:event:ok::tmpl1=""
@fir:event:ok="">
</div>
`,
},
{
name: "multiple events, multiple templates, multiple elements: no filters in event string and key is not present",
input: `
<div
@fir:event1:ok::tmpl1=""
@fir:event:ok="">
</div>
<div
@fir:event2:ok::tmpl2=""
@fir:event:ok="">
</div>
`,
want: `
<div class="fir-event1-ok--tmpl1 fir-event-ok"
@fir:event1:ok::tmpl1=""
@fir:event:ok="">
</div>
<div class="fir-event2-ok--tmpl2 fir-event-ok"
@fir:event2:ok::tmpl2=""
@fir:event:ok="">
</div>
`,
},
{
name: "filters in event string and key is present",
input: `
<div fir-key="1"
@fir:event:ok::tmpl1=""
@fir:event:ok::tmpl2=""
@fir:event:ok::tmpl2=""
@fir:event:ok=""
@fir:[event1:ok,event2:ok]::tmpl3="console.log('hello')"
@fir:event2:ok="">
</div>
`,
want: `
<div fir-key="1"
class="fir-event-ok--tmpl1--1 fir-event-ok--tmpl2--1 fir-event-ok--1 fir-event1-ok--tmpl3--1 fir-event2-ok--tmpl3--1 fir-event2-ok--1"
@fir:event:ok::tmpl1=""
@fir:event:ok::tmpl2=""
@fir:event:ok::tmpl2=""
@fir:event:ok=""
@fir:event1:ok::tmpl3="console.log('hello')"
@fir:event2:ok::tmpl3="console.log('hello')"
@fir:event2:ok="">
</div>
`,
},
{
name: "filters in event string and no key is present",
input: `
<div
@fir:event:ok::tmpl1=""
@fir:event:ok::tmpl2=""
@fir:event:ok::tmpl2=""
@fir:[event1:ok,event2:ok]::tmpl3="console.log('hello')"
@fir:event2:ok="">
</div>
`,
want: `
<div
class="fir-event-ok--tmpl1 fir-event-ok--tmpl2 fir-event1-ok--tmpl3 fir-event2-ok--tmpl3 fir-event2-ok"
@fir:event:ok::tmpl1=""
@fir:event:ok::tmpl2=""
@fir:event:ok::tmpl2=""
@fir:event1:ok::tmpl3="console.log('hello')"
@fir:event2:ok::tmpl3="console.log('hello')"
@fir:event2:ok="">
</div>
`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
want, err := html.Parse(strings.NewReader(test.want))
if err != nil {
t.Fatalf("failed to parse HTML: %v", err)
}
input := addAttributes([]byte(test.input))
got, err := html.Parse(bytes.NewReader(input))
if err != nil {
t.Fatalf("failed to parse HTML: %v", err)
}
if err := areNodesDeepEqual(got, want); err != nil {
t.Fatalf("\nerr: %v \ngot \n %v \n want \n %v", err, gohtml.Format(string(htmlNodeToBytes(got))), gohtml.Format(string(htmlNodeToBytes(want))))
}
})
}
}