Repository navigation
Expand file tree
/
Copy pathcss_filter_test.go
More file actions
211 lines (181 loc) · 5.62 KB
/
Copy pathcss_filter_test.go
File metadata and controls
211 lines (181 loc) · 5.62 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"strings"
"testing"
)
func TestExtractHTMLTokens(t *testing.T) {
html := []byte(`<html>
<head><title>Test</title></head>
<body>
<div class="container main-content" id="page">
<aside class="augl fluid" id="slot-632">Ad</aside>
<p class="text">Hello</p>
<span class="augl-wide">Wide</span>
<img src="test.png">
</div>
</body>
</html>`)
tokens := extractHTMLTokens(html)
// Check classes
wantClasses := []string{"container", "main-content", "augl", "fluid", "text", "augl-wide"}
for _, c := range wantClasses {
if !tokens.classes[c] {
t.Errorf("missing class %q", c)
}
}
// Check IDs
wantIDs := []string{"page", "slot-632"}
for _, id := range wantIDs {
if !tokens.ids[id] {
t.Errorf("missing id %q", id)
}
}
// Check that non-existent tokens are absent
if tokens.classes["nonexistent"] {
t.Error("should not contain class 'nonexistent'")
}
if tokens.ids["nonexistent"] {
t.Error("should not contain id 'nonexistent'")
}
}
func TestExtractHTMLTokensEmpty(t *testing.T) {
tokens := extractHTMLTokens([]byte(`<html><body><p>No classes or ids</p></body></html>`))
if len(tokens.classes) != 0 {
t.Errorf("expected no classes, got %d", len(tokens.classes))
}
if len(tokens.ids) != 0 {
t.Errorf("expected no ids, got %d", len(tokens.ids))
}
}
func TestSelectorMatchesTokens(t *testing.T) {
tokens := htmlTokens{
classes: map[string]bool{
"ad-banner": true,
"container": true,
"augl": true,
"fluid": true,
"mt-5": true,
"mb-5": true,
"augl-wide": true,
"sponsored": true,
},
ids: map[string]bool{
"slot-668": true,
"page": true,
},
}
tests := []struct {
selector string
want bool
desc string
}{
// Simple class selectors
{".ad-banner", true, "class present"},
{".nonexistent", false, "class absent"},
{".augl", true, "class present"},
// Simple ID selectors
{"#slot-668", true, "id present"},
{"#missing-id", false, "id absent"},
{"#page", true, "id present"},
// Compound class selectors (.a.b)
{".mt-5.mb-5", true, "both classes present"},
{".mt-5.nonexistent", false, "one class missing"},
{".augl.fluid", true, "both classes present"},
// Tag + class
{"div.ad-banner", true, "class present (tag ignored for matching)"},
{"aside.augl", true, "class present"},
// Tag + ID
{"div#page", true, "id present"},
{"div#missing", false, "id absent"},
// Attribute selectors — always included (can't pre-match)
{`div[class^="col-"]`, true, "attribute selector always matches"},
{`[data-ad]`, true, "attribute selector always matches"},
{`a[href^="/ads/"]`, true, "attribute selector always matches"},
// Complex selectors with combinators — always included
{"div .ad-child", true, "descendant combinator always matches"},
{"div > .ad-child", true, "child combinator always matches"},
{"div + .sibling", true, "adjacent sibling always matches"},
{"div ~ .sibling", true, "general sibling always matches"},
// Pseudo-classes — always included
{".foo:has(.bar)", true, "pseudo-class always matches"},
{":not(.foo)", true, "pseudo-class always matches"},
// Tag-only selectors — always included (too broad to pre-filter)
{"aside", true, "tag-only always matches"},
{"div", true, "tag-only always matches"},
}
for _, tt := range tests {
got := selectorMatchesTokens(tt.selector, tokens)
if got != tt.want {
t.Errorf("selectorMatchesTokens(%q) = %v, want %v (%s)", tt.selector, got, tt.want, tt.desc)
}
}
}
func TestFilterSelectorsAgainstHTML(t *testing.T) {
html := []byte(`<html><head></head><body>
<aside class="augl" id="slot-668">Ad</aside>
<div class="mt-5 mb-5">Spacer</div>
<p>Content</p>
</body></html>`)
selectors := []string{
".augl", // matches class="augl"
".nonexistent", // no match
"#slot-668", // matches id="slot-668"
"#missing", // no match
".mt-5.mb-5", // matches both classes
".mt-5.missing", // one class missing
".ad-banner", // no match
`div[data-ad]`, // attribute selector — always included
"div .child", // combinator — always included
}
filtered := filterSelectors(selectors, html)
want := map[string]bool{
".augl": true,
"#slot-668": true,
".mt-5.mb-5": true,
`div[data-ad]`: true,
"div .child": true,
}
if len(filtered) != len(want) {
t.Errorf("filtered %d selectors, want %d: %v", len(filtered), len(want), filtered)
}
for _, sel := range filtered {
if !want[sel] {
t.Errorf("unexpected selector in filtered output: %q", sel)
}
}
}
func TestBuildElementHidingCSSChunking(t *testing.T) {
// Build a selector list larger than maxSelectorsPerRule
n := maxSelectorsPerRule + 500
selectors := make([]string, n)
for i := range selectors {
selectors[i] = fmt.Sprintf(".sel-%d", i)
}
css := buildElementHidingCSS(selectors)
// Should produce 2 rules
count := strings.Count(css, "display: none !important")
if count != 2 {
t.Errorf("expected 2 CSS rules, got %d", count)
}
// First selector and last selector should both be present
if !strings.Contains(css, ".sel-0") {
t.Error("first selector missing")
}
if !strings.Contains(css, fmt.Sprintf(".sel-%d", n-1)) {
t.Error("last selector missing")
}
}
func TestBuildElementHidingCSSSmallList(t *testing.T) {
css := buildElementHidingCSS([]string{".a", ".b", ".c"})
count := strings.Count(css, "display: none !important")
if count != 1 {
t.Errorf("expected 1 CSS rule for small list, got %d", count)
}
}
func TestBuildElementHidingCSSEmpty(t *testing.T) {
css := buildElementHidingCSS(nil)
if css != "" {
t.Errorf("expected empty string for nil selectors, got %q", css)
}
}