@@ -10,7 +10,6 @@ import (
10
10
. "github.com/gost-dom/browser/testing/gomega-matchers"
11
11
"github.com/stretchr/testify/suite"
12
12
13
- . "github.com/onsi/ginkgo/v2"
14
13
"github.com/onsi/gomega"
15
14
)
16
15
@@ -163,220 +162,222 @@ func TestElement(t *testing.T) {
163
162
suite .Run (t , new (ElementTestSuite ))
164
163
}
165
164
166
- var _ = Describe ("Element" , func () {
167
- Describe ("InsertAdjacentHTML" , func () {
168
- It ("Should insert correctly 'beforeBegin'" , func () {
169
- doc := ParseHtmlString (`<body>
165
+ func TestElementInsertAdjacentHTMLBeforeBegin (t * testing.T ) {
166
+ gomega := gomega .NewWithT (t )
167
+ doc := ParseHtmlString (`<body>
170
168
<div id="1">El 1</div>
171
169
<div id="2">El 2
172
170
<div>El 2-a</div>
173
171
<div>El 2-b</div>
174
172
</div>
175
173
<div id="3">El 1</div>
176
174
</body>` )
177
- el := doc .GetElementById ("2" )
178
- gomega .Expect (el .InsertAdjacentHTML (
179
- "beforebegin" ,
180
- "<div>1st new child</div><div>2nd new child</div>" ,
181
- )).To (Succeed ())
182
- gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
175
+ el := doc .GetElementById ("2" )
176
+ gomega .Expect (el .InsertAdjacentHTML (
177
+ "beforebegin" ,
178
+ "<div>1st new child</div><div>2nd new child</div>" ,
179
+ )).To (Succeed ())
180
+ gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
183
181
<div id="1">El 1</div>
184
182
<div>1st new child</div><div>2nd new child</div><div id="2">El 2
185
183
<div>El 2-a</div>
186
184
<div>El 2-b</div>
187
185
</div>
188
186
<div id="3">El 1</div>
189
187
</body>` ))
188
+ }
190
189
191
- })
192
-
193
- It ("Should insert correctly 'afterBegin'" , func () {
194
- doc := ParseHtmlString (`<body>
190
+ func TestElementInsertAdjacentHTMLAfterBegin (t * testing.T ) {
191
+ gomega := gomega .NewWithT (t )
192
+ doc := ParseHtmlString (`<body>
195
193
<div id="1">El 1</div>
196
194
<div id="2">El 2
197
195
<div>El 2-a</div>
198
196
<div>El 2-b</div>
199
197
</div>
200
198
<div id="3">El 1</div>
201
199
</body>` )
202
- el , err := (doc .QuerySelector ("[id='2']" ))
203
- gomega .Expect (err ).ToNot (HaveOccurred ())
204
- gomega .Expect (
205
- el .InsertAdjacentHTML (
206
- "afterbegin" ,
207
- "<div>1st new child</div><div>2nd new child</div>" ,
208
- ),
209
- ).To (Succeed ())
210
- gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
200
+ el , err := (doc .QuerySelector ("[id='2']" ))
201
+ gomega .Expect (err ).ToNot (HaveOccurred ())
202
+ gomega .Expect (
203
+ el .InsertAdjacentHTML (
204
+ "afterbegin" ,
205
+ "<div>1st new child</div><div>2nd new child</div>" ,
206
+ ),
207
+ ).To (Succeed ())
208
+ gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
211
209
<div id="1">El 1</div>
212
210
<div id="2"><div>1st new child</div><div>2nd new child</div>El 2
213
211
<div>El 2-a</div>
214
212
<div>El 2-b</div>
215
213
</div>
216
214
<div id="3">El 1</div>
217
215
</body>` ))
216
+ }
218
217
219
- })
220
-
221
- It ("Should insert correctly 'beforeEnd'" , func () {
222
- doc := ParseHtmlString (`<body>
218
+ func TestElementInsertAdjacentHTMLBeforeEnd (t * testing.T ) {
219
+ gomega := gomega .NewWithT (t )
220
+ doc := ParseHtmlString (`<body>
223
221
<div id="1">El 1</div>
224
222
<div id="2">El 2
225
223
<div>El 2-a</div>
226
224
<div>El 2-b</div>
227
225
</div>
228
226
<div id="3">El 1</div>
229
227
</body>` )
230
- el , err := (doc .QuerySelector ("[id='2']" ))
231
- gomega .Expect (err ).ToNot (HaveOccurred ())
232
- gomega .Expect (
233
- el .InsertAdjacentHTML (
234
- "beforeend" ,
235
- "<div>1st new child</div><div>2nd new child</div>" ,
236
- ),
237
- ).To (Succeed ())
238
- gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
228
+ el , err := (doc .QuerySelector ("[id='2']" ))
229
+ gomega .Expect (err ).ToNot (HaveOccurred ())
230
+ gomega .Expect (
231
+ el .InsertAdjacentHTML (
232
+ "beforeend" ,
233
+ "<div>1st new child</div><div>2nd new child</div>" ,
234
+ ),
235
+ ).To (Succeed ())
236
+ gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
239
237
<div id="1">El 1</div>
240
238
<div id="2">El 2
241
239
<div>El 2-a</div>
242
240
<div>El 2-b</div>
243
241
<div>1st new child</div><div>2nd new child</div></div>
244
242
<div id="3">El 1</div>
245
243
</body>` ))
244
+ }
246
245
247
- })
248
-
249
- It ("Should insert correctly 'afterend'" , func () {
250
- doc := ParseHtmlString (`<body>
246
+ func TestElementInsertAdjacentHTMLAfterEnd (t * testing.T ) {
247
+ gomega := gomega .NewWithT (t )
248
+ doc := ParseHtmlString (`<body>
251
249
<div id="1">El 1</div>
252
250
<div id="2">El 2
253
251
<div>El 2-a</div>
254
252
<div>El 2-b</div>
255
253
</div>
256
254
<div id="3">El 1</div>
257
255
</body>` )
258
- el , err := (doc .QuerySelector ("[id='2']" ))
259
- gomega .Expect (err ).ToNot (HaveOccurred ())
260
- gomega .Expect (
261
- el .InsertAdjacentHTML (
262
- "afterend" ,
263
- "<div>1st new child</div><div>2nd new child</div>" ,
264
- ),
265
- ).To (Succeed ())
266
- gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
256
+ el , err := (doc .QuerySelector ("[id='2']" ))
257
+ gomega .Expect (err ).ToNot (HaveOccurred ())
258
+ gomega .Expect (
259
+ el .InsertAdjacentHTML (
260
+ "afterend" ,
261
+ "<div>1st new child</div><div>2nd new child</div>" ,
262
+ ),
263
+ ).To (Succeed ())
264
+ gomega .Expect (doc .Body ()).To (HaveOuterHTML (`<body>
267
265
<div id="1">El 1</div>
268
266
<div id="2">El 2
269
267
<div>El 2-a</div>
270
268
<div>El 2-b</div>
271
269
</div><div>1st new child</div><div>2nd new child</div>
272
270
<div id="3">El 1</div>
273
271
</body>` ))
274
- })
275
- })
272
+ }
276
273
277
- Describe ("HTML Rendering" , func () {
278
- It ("Should support OuterHTML" , func () {
279
- doc := ParseHtmlString (`<body><div id="2">El 2
274
+ func TestElementOuterHTML (t * testing.T ) {
275
+ gomega := gomega .NewWithT (t )
276
+ // NOTE: Whitespace is part of the parsed HTML as #text nodes
277
+ doc := ParseHtmlString (`<body><div id="2">El 2
280
278
<div>El 2-a</div>
281
279
<div>El 2-b</div>
282
280
</div></body>` )
283
- gomega .Expect (doc .Body ().OuterHTML ()).To (Equal (`<body><div id="2">El 2
281
+ gomega .Expect (doc .Body ().OuterHTML ()).To (Equal (`<body><div id="2">El 2
284
282
<div>El 2-a</div>
285
283
<div>El 2-b</div>
286
284
</div></body>` ))
287
- gomega .Expect (doc .Body ().InnerHTML ()).To (Equal (`<div id="2">El 2
285
+ gomega .Expect (doc .Body ().InnerHTML ()).To (Equal (`<div id="2">El 2
288
286
<div>El 2-a</div>
289
287
<div>El 2-b</div>
290
288
</div>` ))
291
- })
292
- })
293
-
294
- Describe ("Append/Prepend/ReplaceChildren/Children" , func () {
295
- // These should ideaaly be tested on both Element, Document, and
296
- // DocumentFragment. The functions are defined in the ParentNode
297
- // mixin interface that all 3 share.
298
-
299
- It ("Should add elements in 'Append'" , func () {
300
- doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
301
- b := doc .Body ()
302
- divE := doc .CreateElement ("div" )
303
- divE .SetTextContent ("e" )
304
- b .Append (
305
- doc .CreateText ("d" ),
306
- divE ,
307
- doc .CreateText ("f" ),
308
- )
309
- gomega .Expect (b ).To (HaveOuterHTML (`<body>a<div>b</div>cd<div>e</div>f</body>` ))
310
- })
311
-
312
- It ("Should add elements first in 'Prepend'" , func () {
313
- doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
314
- b := doc .Body ()
315
- divE := doc .CreateElement ("div" )
316
- divE .SetTextContent ("e" )
317
- b .Prepend (
318
- doc .CreateText ("d" ),
319
- divE ,
320
- doc .CreateText ("f" ),
321
- )
322
- gomega .Expect (b ).To (HaveOuterHTML (`<body>d<div>e</div>fa<div>b</div>c</body>` ))
323
- })
324
-
325
- It ("Should replace elements first in 'ReplaceChildren'" , func () {
326
- doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
327
- b := doc .Body ()
328
- divE := doc .CreateElement ("div" )
329
- divE .SetTextContent ("e" )
330
- b .ReplaceChildren (
331
- doc .CreateText ("d" ),
332
- divE ,
333
- doc .CreateText ("f" ),
334
- )
335
- gomega .Expect (b ).To (HaveOuterHTML (`<body>d<div>e</div>f</body>` ))
336
- })
337
-
338
- It ("Should iterate elements in 'Children'" , func () {
339
- doc := ParseHtmlString (
340
- `<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>` ,
341
- )
342
- b := doc .Body ()
343
- c := b .Children ()
344
-
345
- gomega .Expect (c .Length ()).To (Equal (3 ))
346
- gomega .Expect (c .Item (0 )).To (HaveOuterHTML (`<div id="el-1">b</div>` ))
347
- gomega .Expect (c .Item (1 )).To (HaveAttribute ("id" , "el-2" ))
348
- gomega .Expect (c .Item (2 )).To (HaveAttribute ("name" , "el-3" ))
349
-
350
- gomega .Expect (c .Item (- 1 )).To (BeNil ())
351
- gomega .Expect (c .Item (3 )).To (BeNil ())
352
-
353
- gomega .Expect (c .NamedItem ("el-1" )).To (HaveAttribute ("id" , "el-1" ))
354
- gomega .Expect (c .NamedItem ("el-2" )).To (HaveAttribute ("id" , "el-2" ))
355
- gomega .Expect (c .NamedItem ("el-3" )).To (HaveAttribute ("name" , "el-3" ))
356
- })
357
-
358
- Describe ("First/Last element" , func () {
359
- It ("Should return elements when they exist" , func () {
360
- doc := ParseHtmlString (
361
- `<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>` ,
362
- )
363
- b := doc .Body ()
364
- gomega .Expect (b .FirstElementChild ()).To (HaveAttribute ("id" , "el-1" ))
365
- gomega .Expect (b .LastElementChild ()).To (HaveOuterHTML (`<div name="el-3">f</div>` ))
366
- })
367
-
368
- It ("Should nil when there are only non-element children" , func () {
369
- doc := ParseHtmlString (
370
- `<body>body text</body>` ,
371
- )
372
- b := doc .Body ()
373
- gomega .Expect (b .FirstElementChild ()).To (BeNil ())
374
- gomega .Expect (b .LastElementChild ()).To (BeNil ())
375
- })
376
- })
377
-
378
- It ("Should handle empty string correctly in 'NamedItem'" , func () {
379
- Skip ("Need to research" )
380
- })
381
- })
382
- })
289
+ }
290
+
291
+ // ParentElementTestSuite describes functionality in the ParentNode IDL
292
+ // interface mixin, which is used by both Element, Document, and
293
+ // DocumentFragment.
294
+ //
295
+ // Ideally, these tests should be executed on all 3 types
296
+ type ParentElementTestSuite struct {
297
+ gosttest.GomegaSuite
298
+ }
299
+
300
+ func TestParentElement (t * testing.T ) {
301
+ suite .Run (t , new (ParentElementTestSuite ))
302
+ }
303
+
304
+ func (s * ParentElementTestSuite ) TestAppend () {
305
+ doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
306
+ b := doc .Body ()
307
+ divE := doc .CreateElement ("div" )
308
+ divE .SetTextContent ("e" )
309
+ b .Append (
310
+ doc .CreateText ("d" ),
311
+ divE ,
312
+ doc .CreateText ("f" ),
313
+ )
314
+ s .Expect (b ).To (HaveOuterHTML (`<body>a<div>b</div>cd<div>e</div>f</body>` ))
315
+ }
316
+
317
+ func (s * ParentElementTestSuite ) TestPrepend () {
318
+ doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
319
+ b := doc .Body ()
320
+ divE := doc .CreateElement ("div" )
321
+ divE .SetTextContent ("e" )
322
+ b .Prepend (
323
+ doc .CreateText ("d" ),
324
+ divE ,
325
+ doc .CreateText ("f" ),
326
+ )
327
+ s .Expect (b ).To (HaveOuterHTML (`<body>d<div>e</div>fa<div>b</div>c</body>` ))
328
+ }
329
+
330
+ func (s * ParentElementTestSuite ) TestReplaceChildren () {
331
+ doc := ParseHtmlString (`<body>a<div>b</div>c</body>` )
332
+ b := doc .Body ()
333
+ divE := doc .CreateElement ("div" )
334
+ divE .SetTextContent ("e" )
335
+ b .ReplaceChildren (
336
+ doc .CreateText ("d" ),
337
+ divE ,
338
+ doc .CreateText ("f" ),
339
+ )
340
+ s .Expect (b ).To (HaveOuterHTML (`<body>d<div>e</div>f</body>` ))
341
+ }
342
+
343
+ func (s * ParentElementTestSuite ) TestIterateChildren () {
344
+ doc := ParseHtmlString (
345
+ `<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>` ,
346
+ )
347
+ b := doc .Body ()
348
+ c := b .Children ()
349
+
350
+ s .Expect (c .Length ()).To (Equal (3 ))
351
+ s .Expect (c .Item (0 )).To (HaveOuterHTML (`<div id="el-1">b</div>` ))
352
+ s .Expect (c .Item (1 )).To (HaveAttribute ("id" , "el-2" ))
353
+ s .Expect (c .Item (2 )).To (HaveAttribute ("name" , "el-3" ))
354
+
355
+ s .Expect (c .Item (- 1 )).To (BeNil ())
356
+ s .Expect (c .Item (3 )).To (BeNil ())
357
+
358
+ s .Expect (c .NamedItem ("el-1" )).To (HaveAttribute ("id" , "el-1" ))
359
+ s .Expect (c .NamedItem ("el-2" )).To (HaveAttribute ("id" , "el-2" ))
360
+ s .Expect (c .NamedItem ("el-3" )).To (HaveAttribute ("name" , "el-3" ))
361
+ }
362
+
363
+ func (s * ParentElementTestSuite ) TestFirstLastOnExistingElement () {
364
+ doc := ParseHtmlString (
365
+ `<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>` ,
366
+ )
367
+ b := doc .Body ()
368
+ s .Expect (b .FirstElementChild ()).To (HaveAttribute ("id" , "el-1" ))
369
+ s .Expect (b .LastElementChild ()).To (HaveOuterHTML (`<div name="el-3">f</div>` ))
370
+ }
371
+
372
+ func (s * ParentElementTestSuite ) TestFirstLastWithNonElement () {
373
+ doc := ParseHtmlString (
374
+ `<body>body text</body>` ,
375
+ )
376
+ b := doc .Body ()
377
+ s .Expect (b .FirstElementChild ()).To (BeNil ())
378
+ s .Expect (b .LastElementChild ()).To (BeNil ())
379
+ }
380
+
381
+ func (s * ParentElementTestSuite ) TestEmptyString () {
382
+ s .T ().Skip ("How to handle empty string in NamedItem" )
383
+ }
0 commit comments