-
Notifications
You must be signed in to change notification settings - Fork 6
/
microformats_test.go
583 lines (496 loc) · 15.9 KB
/
microformats_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
// Copyright (c) The microformats project authors.
// SPDX-License-Identifier: MIT
package microformats
import (
"bytes"
"net/url"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var body = &html.Node{Type: html.ElementNode, Data: "body", DataAtom: atom.Body}
// parse the first HTML node found in s.
func parseNode(s string) (n *html.Node, err error) {
nodes, err := html.ParseFragment(strings.NewReader(s), body)
if len(nodes) > 0 {
n = nodes[0]
}
return n, err
}
func renderNode(n *html.Node) string {
b := new(bytes.Buffer)
_ = html.Render(b, n)
return b.String()
}
func ptr(s string) *string { return &s }
func Test_ExpandAttrURLs(t *testing.T) {
base, _ := url.Parse("/a/")
p := &parser{base: base}
tests := []struct {
html string
want string
}{
{`<form action="b"></form>`, `<form action="/a/b"></form>`},
{`<blockquote cite="b"></blockquote>`, `<blockquote cite="/a/b"></blockquote>`},
{`<del cite="b"></del>`, `<del cite="/a/b"></del>`},
{`<ins cite="b"></ins>`, `<ins cite="/a/b"></ins>`},
{`<q cite="b"></q>`, `<q cite="/a/b"></q>`},
{`<object data="b"></object>`, `<object data="/a/b"></object>`},
{`<button formaction="b"></button>`, `<button formaction="/a/b"></button>`},
{`<input formaction="b"/>`, `<input formaction="/a/b"/>`},
{`<a href="b"></a>`, `<a href="/a/b"></a>`},
{`<area href="b"/>`, `<area href="/a/b"/>`},
{`<base href="b"/>`, `<base href="/a/b"/>`},
{`<link href="b"/>`, `<link href="/a/b"/>`},
{`<a ping="b"></a>`, `<a ping="/a/b"></a>`},
{`<area ping="b"/>`, `<area ping="/a/b"/>`},
{`<audio src="b"></audio>`, `<audio src="/a/b"></audio>`},
{`<embed src="b"/>`, `<embed src="/a/b"/>`},
{`<iframe src="b"></iframe>`, `<iframe src="/a/b"></iframe>`},
{`<img src="b"/>`, `<img src="/a/b"/>`},
{`<input src="b"/>`, `<input src="/a/b"/>`},
{`<script src="b"></script>`, `<script src="/a/b"></script>`},
{`<source src="b"/>`, `<source src="/a/b"/>`},
{`<track src="b"/>`, `<track src="/a/b"/>`},
{`<video src="b"></video>`, `<video src="/a/b"></video>`},
{`<video poster="b"></video>`, `<video poster="/a/b"></video>`},
// multiple attributes
{`<input formaction="b" src="c"/>`, `<input formaction="/a/b" src="/a/c"/>`},
}
for _, tt := range tests {
n, _ := parseNode(tt.html)
p.expandAttrURLs(n)
got := renderNode(n)
if got != tt.want {
t.Errorf("expandAttrURL(%q) returned %q, want %q", tt.html, got, tt.want)
}
}
}
func Test_ExpandURL(t *testing.T) {
example, _ := url.Parse("http://example.com/base/")
tests := []struct {
relative string
base *url.URL
want string
}{
{"", nil, ""},
{"", example, "http://example.com/base/"},
{"/", nil, "/"},
{"/", example, "http://example.com/"},
{"foo", example, "http://example.com/base/foo"},
{"/foo", example, "http://example.com/foo"},
}
for _, tt := range tests {
got := expandURL(tt.relative, tt.base)
if want := tt.want; got != want {
t.Errorf("expandURL(%q, %q) returned %q, want %q", tt.relative, tt.base, got, want)
}
}
}
func Test_GetClasses(t *testing.T) {
tests := []struct {
html string
classes []string
}{
{``, nil},
{`<img>`, nil},
{`<img class_="a">`, nil},
{`<img class="a">`, []string{"a"}},
{`<img class="a b">`, []string{"a", "b"}},
{`<img class=" a
b c ">`, []string{"a", "b", "c"}},
{`<img class="http://example.com/ b">`, []string{"http://example.com/", "b"}},
{`<img CLASS="a">`, []string{"a"}},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getClasses(n), tt.classes; !cmp.Equal(got, want) {
t.Errorf("getClasses(%q) returned %v, want %v", tt.html, got, want)
}
}
}
func Test_HasMatchingClass(t *testing.T) {
root, prop := rootClassNames.String(), propertyClassNames.String()
tests := []struct {
html string
regex string
match bool
}{
{``, ``, false},
{`<img>`, ``, false},
{`<img>`, `.*`, false},
{`<img class="">`, `.*`, false},
{`<img class="a">`, `.+`, true},
// root class names
{`<img class="h-card">`, root, true},
{`<img class="h-card vcard">`, root, true},
{`<img class="vcard h-card">`, root, true},
{`<img class="hcard">`, root, false},
{`<img class="p-name">`, root, false},
// property class names
{`<img class="p-name">`, prop, true},
{`<img class="u-url">`, prop, true},
{`<img class="dt-updated">`, prop, true},
{`<img class="e-content">`, prop, true},
{`<img class="p-name name">`, prop, true},
{`<img class="h-card">`, prop, false},
{`<img class="pname">`, prop, false},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
r, err := regexp.Compile(tt.regex)
if err != nil {
t.Fatalf("Error compiling regex: %v", err)
}
if got, want := hasMatchingClass(n, r), tt.match; got != want {
t.Errorf("hasMatchingClass(%q, %q) returned %t, want %t", tt.html, tt.regex, got, want)
}
}
}
// test both getAttr and getAttrPtr
func Test_GetAttr(t *testing.T) {
tests := []struct {
html, attr string
value string // string valuer returned by getAttr
ptr *string // pointer value returned by getAttrPtr
}{
{``, "", "", nil},
{`<img>`, "", "", nil},
{`<img>`, "src", "", nil},
{`<img src>`, "src", "", ptr("")},
{`<img src="a">`, "src", "a", ptr("a")},
{`<img src="a">`, "SRC", "a", ptr("a")},
{`<img SRC="a">`, "src", "a", ptr("a")},
{`<img src="a" src="b">`, "src", "a", ptr("a")},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getAttr(n, tt.attr), tt.value; got != want {
t.Errorf("getAttr(%q, %q) returned %v, want %v", tt.html, tt.attr, got, want)
}
if got, want := getAttrPtr(n, tt.attr), tt.ptr; !cmp.Equal(got, want) {
t.Errorf("getAttrPtr(%q, %q) returned %v, want %v", tt.html, tt.attr, got, want)
}
}
}
func Test_IsAtom(t *testing.T) {
tests := []struct {
html string
atoms []atom.Atom
match bool
}{
{"", nil, false},
{"<img>", []atom.Atom{}, false},
{"<img>", []atom.Atom{atom.A}, false},
{"<img>", []atom.Atom{atom.Img}, true},
{"<img>", []atom.Atom{atom.A, atom.Img}, true},
{"<img>", []atom.Atom{atom.Img, atom.A}, true},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := isAtom(n, tt.atoms...), tt.match; got != want {
t.Errorf("isAtom(%q, %v) returned %t, want %t", tt.html, tt.atoms, got, want)
}
}
}
func Test_GetTextContent(t *testing.T) {
base, _ := url.Parse("http://example.com/")
p := &parser{base: base}
tests := []struct {
html string
imgFn func(*html.Node) string
content string
}{
{"", nil, ""},
{"foo", nil, "foo"},
{"<a>", nil, ""},
{"<a>foo</a>", nil, "foo"},
{"<a><b>foo</b>bar</a>", nil, "foobar"},
{"<a><b>foo</b><i>bar</i></a>", nil, "foobar"},
{"<a> <b>foo</b> <i>bar</i> </a>", nil, " foo bar "},
{"<a><b><i>foo</i></b>bar</a>", nil, "foobar"},
// test image functions
{"<a><img alt='foo'></a>", nil, ""},
{"<a><img alt='foo'></a>", imageAltValue, "foo"},
{"<a><img src='foo'></a>", imageAltValue, ""},
{"<a><img src='foo'></a>", p.imageAltSrcValue, " http://example.com/foo "},
{"<a><img alt='foo' src='bar'></a>", p.imageAltSrcValue, "foo"},
{"<a><img></a>", p.imageAltSrcValue, ""},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getTextContent(n, tt.imgFn), tt.content; got != want {
t.Errorf("getTextContent(%q) returned %q, want %q", tt.html, got, want)
}
}
}
func Test_GetOnlyChild(t *testing.T) {
tests := []struct {
html, child string
}{
{"", ""},
{"<img>", ""},
{"<a>foo</a>", ""},
{"<a><img></a>", "<img>"},
{"<a>foo<img>bar</a>", "<img>"},
{"<a><b><img></b></a>", "<b><img></b>"},
// too many children
{"<a><img><img></a>", ""},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
want, err := parseNode(tt.child)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
got := getOnlyChild(n)
if got != nil {
// for the purposes of comparison, adjacent nodes don't matter
got.Parent = nil
got.PrevSibling = nil
got.NextSibling = nil
}
if !cmp.Equal(got, want) {
t.Errorf("getOnlyChild(%q) returned %#v, want %#v", tt.html, got, want)
}
}
}
func Test_GetOnlyChildAtom(t *testing.T) {
tests := []struct {
html, atom, child string
}{
{"", "", ""},
{"<img>", "", ""},
{"<a>foo</a>", "", ""},
{"<a><img></a>", "img", "<img>"},
{"<a>foo<img>bar</a>", "img", "<img>"},
{"<a><b><img></b></a>", "b", "<b><img></b>"},
// wrong atom
{"<a><img></a>", "b", ""},
// too many children
{"<a><img><img></a>", "img", ""},
// child too deep
{"<a><b><img></b></a>", "img", ""},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
want, err := parseNode(tt.child)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
got := getOnlyChildAtom(n, atom.Lookup([]byte(tt.atom)))
if got != nil {
// for the purposes of comparison, adjacent nodes don't matter
got.Parent = nil
got.PrevSibling = nil
got.NextSibling = nil
}
if !cmp.Equal(got, want) {
t.Errorf("getOnlyChildAtom(%q, %q) returned %#v, want %#v", tt.html, tt.atom, got, want)
}
}
}
func Test_GetImpliedName(t *testing.T) {
tests := []struct {
html, name string
}{
{"", ""},
{`<img alt="name">`, "name"},
{`<area alt="name">`, "name"},
{`<abbr title="name">`, "name"},
{`<span><img alt="name"></span>`, "name"},
{`<span><img alt="name" class="h-card"></span>`, "name"},
{`<span><area alt="name"></span>`, "name"},
{`<span><area alt="name" class="h-card"></span>`, ""},
{`<span><abbr title="name"></span>`, "name"},
{`<p><span><img alt="name"></span></p>`, "name"},
{`<p><span><img alt="name" class="h-card"></span></p>`, "name"},
{`<p><span><area alt="name"></span></p>`, "name"},
{`<p><span><area alt="name" class="h-card"></span></p>`, ""},
{`<p><span><abbr title="name"></span></p>`, "name"},
{`<p><span>name</span></p>`, "name"},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getImpliedName(n), tt.name; got != want {
t.Errorf("getImpliedName(%q) returned %v, want %v", tt.html, got, want)
}
}
}
func Test_GetImpliedPhoto(t *testing.T) {
base, _ := url.Parse("http://example.com/")
tests := []struct {
html string
base *url.URL
url string
alt string
}{
{"", nil, "", ""},
{`<img src="p">`, nil, "p", ""},
{`<img src="p" alt="a">`, nil, "p", "a"},
{`<img src="p">`, base, "http://example.com/p", ""},
{`<img src="p" alt="a">`, base, "http://example.com/p", "a"},
{`<object data="p">`, nil, "p", ""},
{`<object data="p">`, base, "http://example.com/p", ""},
{`<p><img src="p"></p>`, nil, "p", ""},
{`<p><img src="p" alt="a"></p>`, nil, "p", "a"},
{`<p><img src="p"></p>`, base, "http://example.com/p", ""},
{`<p><img src="p" alt="a"></p>`, base, "http://example.com/p", "a"},
{`<p><img src="p"><img src="q"></p>`, nil, "", ""},
{`<p><img src="p" class="h-entry"></p>`, nil, "", ""},
{`<p><object data="p"></p>`, nil, "p", ""},
{`<p><object data="p"></p>`, base, "http://example.com/p", ""},
{`<p><object data="p"></object><object data="p"></object></p>`, nil, "", ""},
{`<p><object data="p" class="h-entry"></p>`, nil, "", ""},
{`<p><span><img src="p"></span></p>`, nil, "p", ""},
{`<p><span><img src="p" alt="a"></span></p>`, nil, "p", "a"},
{`<p><span><object data="p"></span></p>`, nil, "p", ""},
{`<p><span><object data="p"></span></p>`, base, "http://example.com/p", ""},
{`<p><span><object data="p" class="h-entry"></span></p>`, nil, "", ""},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
src, alt := getImpliedPhoto(n, tt.base)
if got, want := src, tt.url; got != want {
t.Errorf("getImpliedPhoto(%q, %s) returned src %v, want %v", tt.html, tt.base, got, want)
}
if got, want := alt, tt.alt; got != want {
t.Errorf("getImpliedPhoto(%q, %s) returned alt %v, want %v", tt.html, tt.base, got, want)
}
}
}
func Test_GetImpliedURL(t *testing.T) {
base, _ := url.Parse("http://example.com/")
tests := []struct {
html string
base *url.URL
url string
}{
{"", nil, ""},
{`<a href="p">`, nil, "p"},
{`<a href="p">`, base, "http://example.com/p"},
{`<area href="p">`, nil, "p"},
{`<area href="p">`, base, "http://example.com/p"},
{`<p><a href="p"></p>`, nil, "p"},
{`<p><a href="p"></p>`, base, "http://example.com/p"},
{`<p><a href="p" class="h-entry"></p>`, nil, ""},
{`<p><b><a href="p"></b></p>`, nil, "p"},
{`<p><area href="p"></p>`, nil, "p"},
{`<p><area href="p"></p>`, base, "http://example.com/p"},
{`<p><area href="p" class="h-entry"></p>`, nil, ""},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getImpliedURL(n, tt.base), tt.url; got != want {
t.Errorf("getImpliedURL(%q, %s) returned %v, want %v", tt.html, tt.base, got, want)
}
}
}
func Test_GetValueClassPattern(t *testing.T) {
tests := []struct {
html string
value *string
}{
{"", nil},
{`<p><img alt="v"></p>`, nil},
{`<p><img alt="v" class="value"></p>`, ptr("v")},
{`<p><area alt="v" class="value"></p>`, ptr("v")},
{`<p><data value="v"></data></p>`, nil},
{`<p><data value="v" class="value"></data></p>`, ptr("v")},
{`<p><data class="value">v</data></p>`, ptr("v")},
{`<p><abbr title="v"></abbr></p>`, nil},
{`<p><abbr title="v" class="value"></abbr></p>`, ptr("v")},
{`<p><abbr class="value">v</abbr></p>`, ptr("v")},
{`<p><span>v</span></p>`, nil},
{`<p><span class="value">v</span></p>`, ptr("v")},
// concatenation
{`<p><b class="value">a</b><b class="value">b</b></p>`, ptr("ab")},
{`<p><img class="value" alt="a"><b>b</b><b class="value">c</b></p>`, ptr("ac")},
// value-title
{`<p><img alt="v" class="value-title" title="t"></p>`, ptr("t")},
{`<p><img alt="v" class="value" title="t"><img alt="v" class="value-title" title="t"></p>`, ptr("vt")},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getValueClassPattern(n), tt.value; !cmp.Equal(got, want) {
t.Errorf("getValueClassPattern(%q) returned %v, want %v", tt.html, got, want)
}
}
}
func Test_GetFirstPropValue(t *testing.T) {
tests := []struct {
properties map[string][]any
prop string
value *string
}{
{nil, "", nil},
{nil, "name", nil},
{map[string][]any{"name": {"n"}}, "", nil},
{map[string][]any{"name": {"n"}}, "name", ptr("n")},
{map[string][]any{"name": {"a", "b"}}, "name", ptr("a")},
{map[string][]any{"name": {"a", "b"}}, "url", nil},
{map[string][]any{"name": {1, 2}}, "name", nil},
{map[string][]any{"name": {"n"}, "url": {"u"}}, "url", ptr("u")},
}
for _, tt := range tests {
mf := &Microformat{Properties: tt.properties}
if got, want := getFirstPropValue(mf, tt.prop), tt.value; !cmp.Equal(got, want) {
t.Errorf("getFirstPropValue(%v, %q) returned %v, want %v", tt.properties, tt.prop, got, want)
}
}
}
func Test_ParseNodeNil(t *testing.T) {
tests := []struct {
name string
html string
}{
{"absolute", "<html><head><base href=\"https://example.com\"></head></html>"},
{"relative", "<html><head><base href=\"./something\"></head></html>"},
{"none", "<html><head></head></html>"}, // parseNode(tt.html) == nil
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
ParseNode(n, nil) // this should not panic
})
}
}