-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathutils-node_test.go
652 lines (560 loc) · 15.8 KB
/
utils-node_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package readability
import (
"fmt"
"os"
"strings"
"testing"
"golang.org/x/net/html"
)
func openTestFile(path string) (*html.Node, error) {
testFile, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open test file: %v", err)
}
defer testFile.Close()
doc, err := html.Parse(testFile)
if err != nil {
return nil, fmt.Errorf("failed to parse test file: %v", err)
}
return doc, nil
}
func Test_getElementsByTagName(t *testing.T) {
doc, err := openTestFile("test-pages/nodes.html")
if err != nil {
t.Error(err)
}
html := doc.FirstChild
body := html.FirstChild.NextSibling
scenarios := map[string]int{
"h1": 1,
"h2": 2,
"h3": 3,
"p": 6,
"div": 7,
"img": 12,
"*": 31,
}
for tag, expected := range scenarios {
if count := len(getElementsByTagName(body, tag)); count != expected {
t.Errorf("\n"+
"tag : \"%s\"\n"+
"want : %d\n"+
"got : %d", tag, expected, count)
}
}
}
func Test_createElement(t *testing.T) {
scenarios := map[string]string{
"h1": "<h1></h1>",
"h2": "<h2></h2>",
"h3": "<h3></h3>",
"p": "<p></p>",
"div": "<div></div>",
"img": "<img/>",
}
for tag, expected := range scenarios {
node := createElement(tag)
if html := outerHTML(node); html != expected {
t.Errorf("\n"+
"tag : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", tag, expected, html)
}
}
}
func Test_createTextNode(t *testing.T) {
scenarios := []string{
"hello world",
"this is awesome",
"all cat is good boy",
"all dog is good boy as well",
}
for _, text := range scenarios {
textNode := createTextNode(text)
if html := outerHTML(textNode); html != text {
t.Errorf("\n"+
"want : \"%s\"\n"+
"got : \"%s\"", text, html)
}
}
}
func Test_tagName(t *testing.T) {
scenarios := map[string]string{
"this is only ordinary text": "",
"<h1>Hello</h1>": "h1",
"<p>This is paragraph</p>": "p",
"<div><p>Some nested element</p></div>": "div",
"<ul><li>Another nested element</li></ul>": "ul",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if nodeTag := tagName(node); nodeTag != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, nodeTag)
}
}
}
func Test_getAttribute(t *testing.T) {
scenarios := map[string]string{
`<p data-test="trying to"></p>`: "trying to",
`<ul data-test="make a dream"></p>`: "make a dream",
`<div data-test="becomes reality"></div>`: "becomes reality",
`<div data-not-test="before it ends"></div>`: "",
`<div data-test=""></div>`: "",
`<ul></ul>`: "",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if attrValue := getAttribute(node, "data-test"); attrValue != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, attrValue)
}
}
}
func Test_setAttribute(t *testing.T) {
type setAttributeTest struct {
name string
value string
expected string
}
scenarios := []setAttributeTest{{
name: "id",
value: "item-1",
expected: `<div id="item-1"></div>`,
}, {
name: "class",
value: "container",
expected: `<div class="container"></div>`,
}, {
name: "data-custom",
value: "hello",
expected: `<div data-custom="hello"></div>`,
}}
for _, test := range scenarios {
div := createElement("div")
setAttribute(div, test.name, test.value)
if html := outerHTML(div); html != test.expected {
t.Errorf("\n"+
"attribute : \"%s\" = \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"",
test.name, test.value, test.expected, html)
}
}
}
func Test_removeAttribute(t *testing.T) {
type rmAttributeTest struct {
origin string
removedAttr string
expected string
}
scenarios := []rmAttributeTest{{
origin: `<p class="quote"></p>`,
removedAttr: "class",
expected: `<p></p>`,
}, {
origin: `<div id="list-story"></div>`,
removedAttr: "id",
expected: `<div></div>`,
}, {
origin: `<img src="file://sample/image.jpg" alt="Sample image"/>`,
removedAttr: "src",
expected: `<img alt="Sample image"/>`,
}}
for _, test := range scenarios {
doc, err := html.Parse(strings.NewReader(test.origin))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", test.origin, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
removeAttribute(node, test.removedAttr)
if html := outerHTML(node); html != test.expected {
t.Errorf("\n"+
"origin : \"%s\"\n"+
"removed : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"",
test.origin, test.removedAttr, test.expected, html)
}
}
}
func Test_hasAttribute(t *testing.T) {
origin := `<img
id="main"
class="img-thumbnail"
src="file://sample/image.jpg"
alt="Sample image" data-loaded="false"/>`
scenarios := map[string]bool{
"id": true,
"class": true,
"src": true,
"alt": true,
"href": false,
"data-custom": false,
}
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
for attrName, expected := range scenarios {
if result := hasAttribute(node, attrName); result != expected {
t.Errorf("\n"+
"origin : \"%s\"\n"+
"name : \"%s\"\n"+
"want : %t\n"+
"got : %t",
outerHTML(node), attrName, expected, result)
}
}
}
func Test_textContent(t *testing.T) {
scenarios := map[string]string{
"this is only ordinary text": "this is only ordinary text",
"<h1>Hello</h1>": "Hello",
"<p>This is paragraph</p>": "This is paragraph",
"<div><p>Some nested element</p></div>": "Some nested element",
"<ul><li>Another nested element</li></ul>": "Another nested element",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if text := textContent(node); text != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, text)
}
}
}
func Test_outerHTML(t *testing.T) {
scenarios := []string{
"this is only ordinary text",
"<h1>Hello</h1>",
"<p>This is paragraph</p>",
"<div><p>Some nested element</p></div>",
"<ul><li>Another nested element</li></ul>",
}
for _, strHTML := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if outer := outerHTML(node); outer != strHTML {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"got : \"%s\"", strHTML, outer)
}
}
}
func Test_innerHTML(t *testing.T) {
scenarios := map[string]string{
"this is only ordinary text": "",
"<h1>Hello</h1>": "Hello",
"<p>This is paragraph</p>": "This is paragraph",
"<div><p>Some nested element</p></div>": "<p>Some nested element</p>",
"<ul><li>Another nested element</li></ul>": "<li>Another nested element</li>",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if inner := innerHTML(node); inner != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, inner)
}
}
}
func Test_documentElement(t *testing.T) {
doc, _ := html.Parse(strings.NewReader("<html></html>"))
docElement := documentElement(doc)
if docElement != doc.FirstChild {
t.Errorf("unable to find <HTML> tag")
}
}
func Test_id(t *testing.T) {
scenarios := map[string]string{
`<p id="txt-excerpt"></p>`: "txt-excerpt",
`<img src="" alt="" id="avatar"/>`: "avatar",
`<div id="list-container"></div>`: "list-container",
`<ul></ul>`: "",
`<li></li>`: "",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if nodeID := id(node); nodeID != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, nodeID)
}
}
}
func Test_class(t *testing.T) {
scenarios := map[string]string{
`<p class="dark"></p>`: "dark",
`<img src="" alt="" class="round-image"/>`: "round-image",
`<div class="story-box"></div>`: "story-box",
`<ul></ul>`: "",
`<li></li>`: "",
}
for strHTML, expected := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
if nodeClass := className(node); nodeClass != expected {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", strHTML, expected, nodeClass)
}
}
}
func Test_children(t *testing.T) {
doc, err := openTestFile("test-pages/nodes.html")
if err != nil {
t.Error(err)
}
body := getElementsByTagName(doc, "body")[0]
nChildElements := len(children(body))
expected := 30
if nChildElements != expected {
t.Errorf("\n"+
"failed to get all children element\n"+
"want : %d\n"+
"got : %d", expected, nChildElements)
}
}
func Test_childNodes(t *testing.T) {
doc, err := openTestFile("test-pages/nodes.html")
if err != nil {
t.Error(err)
}
body := getElementsByTagName(doc, "body")[0]
nChildNodes := len(childNodes(body))
expected := 61
if nChildNodes != expected {
t.Errorf("\n"+
"failed to get all child nodes\n"+
"want : %d\n"+
"got : %d", expected, nChildNodes)
}
}
func Test_firstElementChild(t *testing.T) {
origin := `<div>
Hi, this is a TextNode.
<p>This is HTML element p.</p>
Somehow there are another TextNode here.
<ul><li>Which followed by list here.</li></ul>
</div>`
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
}
div := getElementsByTagName(doc, "div")[0]
firstElement := firstElementChild(div)
expected := "<p>This is HTML element p.</p>"
if outer := outerHTML(firstElement); outer != expected {
t.Errorf("\n"+
"failed to get first element child\n"+
"want : \"%s\"\n"+
"got : \"%s\"", expected, outer)
}
}
func Test_nextElementSibling(t *testing.T) {
origin := `<div>
Hi, this is a TextNode.
<p>This is HTML element p.</p>
Somehow there are another TextNode here.
<ul><li>Which followed by list here.</li></ul>
</div>`
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
}
div := getElementsByTagName(doc, "div")[0]
firstElement := firstElementChild(div)
sibling := nextElementSibling(firstElement)
expected := "<ul><li>Which followed by list here.</li></ul>"
if outer := outerHTML(sibling); outer != expected {
t.Errorf("\n"+
"failed to get next element sibling\n"+
"want : \"%s\"\n"+
"got : \"%s\"", expected, outer)
}
}
func Test_appendChild_fromNew(t *testing.T) {
origin := `<div><p>This is a div with one lonely p.</p></div>`
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
}
div := getElementsByTagName(doc, "div")[0]
newImg := createElement("img")
appendChild(div, newImg)
expected := `<div><p>This is a div with one lonely p.</p><img/></div>`
if outer := outerHTML(div); outer != expected {
t.Errorf("\n"+
"failed to append child from new element\n"+
"want : \"%s\"\n"+
"got : \"%s\"", expected, outer)
}
}
func Test_appendChild_fromExisting(t *testing.T) {
origin := `<div><div></div><p>One div-less p</p></div>`
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
}
outerDiv := getElementsByTagName(doc, "div")[0]
div := getElementsByTagName(doc, "div")[1]
p := getElementsByTagName(doc, "p")[0]
appendChild(div, p)
expected := `<div><div><p>One div-less p</p></div></div>`
if outer := outerHTML(outerDiv); outer != expected {
t.Errorf("\n"+
"failed to append child from existing element\n"+
"want : \"%s\"\n"+
"got : \"%s\"", expected, outer)
}
}
func Test_replaceNode(t *testing.T) {
origin := `<p>Paragraph with one thousand words</p>`
doc, err := html.Parse(strings.NewReader(origin))
if err != nil {
if err != nil {
t.Errorf("\nfailed to parse HTML: %v", err)
}
}
body := getElementsByTagName(doc, "body")[0]
p := getElementsByTagName(doc, "p")[0]
img := createElement("img")
replaceNode(p, img)
expected := `<img/>`
if html := innerHTML(body); html != expected {
t.Errorf("\n"+
"failed to replace node\n"+
"want : \"%s\"\n"+
"got : \"%s\"", expected, html)
}
}
func Test_includeNode(t *testing.T) {
doc, err := openTestFile("test-pages/nodes.html")
if err != nil {
t.Error(err)
}
body := getElementsByTagName(doc, "body")[0]
allElements := getElementsByTagName(body, "*")
scenarios := map[*html.Node]bool{
getElementsByTagName(body, "h1")[0]: true,
getElementsByTagName(body, "h2")[0]: true,
getElementsByTagName(body, "h3")[0]: true,
getElementsByTagName(body, "p")[0]: true,
getElementsByTagName(body, "div")[0]: true,
getElementsByTagName(body, "img")[0]: true,
createElement("h4"): false,
createElement("h5"): false,
createElement("ul"): false,
createElement("pre"): false,
createElement("table"): false,
}
for node, expected := range scenarios {
if isIncluded := includeNode(allElements, node); isIncluded != expected {
t.Errorf("\n"+
"element : \"%s\"\n"+
"want : %t\n"+
"got : %t", tagName(node), expected, isIncluded)
}
}
}
func Test_cloneNode(t *testing.T) {
scenarios := []string{
`<p>Hello world</p>`,
`<p>This has <b>bold</b> content</p>`,
`<img src="cute-cat.jpg"/>`,
`<div><div><p></p></div><div></div></div>`,
}
for _, strHTML := range scenarios {
doc, err := html.Parse(strings.NewReader(strHTML))
if err != nil {
t.Errorf("\n"+
"HTML : \"%s\"\n"+
"failed to parse: %v", strHTML, err)
}
body := getElementsByTagName(doc, "body")[0]
node := body.FirstChild
clone := cloneNode(node)
nodeHTML := outerHTML(node)
cloneHTML := outerHTML(clone)
// Make sure two node is same, but has different pointer
if nodeHTML != cloneHTML || node == clone {
t.Errorf("\n"+
"failed to be cloned properly\n"+
"origin : \"%s\"\n"+
"node HTML : \"%s\"\n"+
"clone HTML : \"%s\"\n"+
"node addr : %p\n"+
"clone addr : %p",
strHTML, nodeHTML, cloneHTML, node, clone)
}
}
}