-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_test.go
More file actions
187 lines (167 loc) · 4.9 KB
/
extract_test.go
File metadata and controls
187 lines (167 loc) · 4.9 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
180
181
182
183
184
185
186
187
package main
import (
"strings"
"testing"
)
func TestStripScriptAndStyle(t *testing.T) {
h := `<html><body>
<script>alert("evil")</script>
<style>body { color: red; }</style>
<p>Keep this paragraph.</p>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
body := findFirst(doc, "body")
bodyText := textContent(body)
if strings.Contains(bodyText, "alert") {
t.Error("script content should be removed")
}
if strings.Contains(bodyText, "color: red") {
t.Error("style content should be removed")
}
if !strings.Contains(bodyText, "Keep this paragraph.") {
t.Error("paragraph content should be kept")
}
}
func TestStripNavFooter(t *testing.T) {
h := `<html><body>
<nav>Navigation links here</nav>
<main><p>Main content here.</p></main>
<footer>Footer text here</footer>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
if findFirst(doc, "nav") != nil {
t.Error("nav should be removed")
}
if findFirst(doc, "footer") != nil {
t.Error("footer should be removed")
}
mainNode := findFirst(doc, "main")
if mainNode == nil {
t.Error("main content should be kept")
}
if !strings.Contains(textContent(mainNode), "Main content here.") {
t.Error("main content text should be kept")
}
}
func TestStripNoiseByClass(t *testing.T) {
h := `<html><body>
<div class="cookie-consent">Accept cookies</div>
<div class="sidebar">Sidebar links</div>
<div class="newsletter">Subscribe now!</div>
<article class="post">Real article content.</article>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
body := findFirst(doc, "body")
bodyText := textContent(body)
if strings.Contains(bodyText, "Accept cookies") {
t.Error("cookie-consent element should be removed")
}
if strings.Contains(bodyText, "Sidebar links") {
t.Error("sidebar element should be removed")
}
if strings.Contains(bodyText, "Subscribe now!") {
t.Error("newsletter element should be removed")
}
if !strings.Contains(bodyText, "Real article content.") {
t.Error("article content should be kept")
}
}
func TestStripHiddenElements(t *testing.T) {
h := `<html><body>
<div style="display:none">Hidden div</div>
<div style="display: none">Also hidden</div>
<div aria-hidden="true">Aria hidden</div>
<p>Visible paragraph.</p>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
body := findFirst(doc, "body")
bodyText := textContent(body)
if strings.Contains(bodyText, "Hidden div") {
t.Error("display:none element should be removed")
}
if strings.Contains(bodyText, "Also hidden") {
t.Error("display: none element should be removed")
}
if strings.Contains(bodyText, "Aria hidden") {
t.Error("aria-hidden element should be removed")
}
if !strings.Contains(bodyText, "Visible paragraph.") {
t.Error("visible paragraph should be kept")
}
}
func TestStripHeaderWithoutH1(t *testing.T) {
h := `<html><body>
<header><nav>Site nav</nav></header>
<main><p>Content here.</p></main>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
if findFirst(doc, "header") != nil {
t.Error("header without h1 should be removed")
}
mainNode := findFirst(doc, "main")
if !strings.Contains(textContent(mainNode), "Content here.") {
t.Error("main content should be kept")
}
}
func TestKeepHeaderWithH1(t *testing.T) {
h := `<html><body>
<header><h1>Page Title</h1></header>
<main><p>Content here.</p></main>
</body></html>`
doc := parseHTML(h)
stripNoise(doc)
header := findFirst(doc, "header")
if header == nil {
t.Error("header with h1 should be kept")
}
if !strings.Contains(textContent(header), "Page Title") {
t.Error("h1 in header should be kept")
}
}
func TestExtractMainContent(t *testing.T) {
h := `<html><body>
<aside>Sidebar content</aside>
<main><article><p>Main article content.</p></article></main>
</body></html>`
doc := parseHTML(h)
sel := extractContent(doc, "example.com")
text := strings.TrimSpace(textContent(sel))
if !strings.Contains(text, "Main article content.") {
t.Errorf("expected main article content, got: %s", text)
}
if strings.Contains(text, "Sidebar content") {
t.Error("sidebar content should not be selected")
}
}
func TestExtractArticleTag(t *testing.T) {
h := `<html><body>
<div class="wrapper">
<article><p>Article body text.</p></article>
</div>
</body></html>`
doc := parseHTML(h)
sel := extractContent(doc, "example.com")
text := textContent(sel)
if !strings.Contains(text, "Article body text.") {
t.Errorf("expected article text in selection, got: %s", text)
}
}
func TestExtractFallsBackToBody(t *testing.T) {
h := `<html><body>
<div><p>Some content without semantic tags.</p></div>
</body></html>`
doc := parseHTML(h)
// Use a domain with no site selector and HTML with no semantic tags
sel := extractContent(doc, "no-semantic-site.example.com")
if sel == nil {
t.Fatal("expected a node to be returned")
}
if !strings.Contains(textContent(sel), "Some content without semantic tags.") {
t.Errorf("expected body content, got: %s", textContent(sel))
}
}