1+ package filtering
2+
3+ import (
4+ "testing"
5+ )
6+
7+ func TestFilterContent (t * testing.T ) {
8+ tests := []struct {
9+ name string
10+ input string
11+ expected string
12+ cfg * Config
13+ }{
14+ {
15+ name : "Empty string" ,
16+ input : "" ,
17+ expected : "" ,
18+ cfg : DefaultConfig (),
19+ },
20+ {
21+ name : "Normal text without hidden content" ,
22+ input : "This is normal text without any hidden content." ,
23+ expected : "This is normal text without any hidden content." ,
24+ cfg : DefaultConfig (),
25+ },
26+ {
27+ name : "Text with invisible characters" ,
28+ input : "Hidden\u200B characters\u200B in\u200B this\u200B text" ,
29+ expected : "Hiddencharactersinthistext" ,
30+ cfg : DefaultConfig (),
31+ },
32+ {
33+ name : "Text with HTML comments" ,
34+ input : "This has a <!-- hidden comment --> in it." ,
35+ expected : "This has a [HTML_COMMENT] in it." ,
36+ cfg : DefaultConfig (),
37+ },
38+ {
39+ name : "Text with HTML elements" ,
40+ input : "This has <script>alert('hidden')</script> scripts." ,
41+ expected : "This has [HTML_ELEMENT] scripts." ,
42+ cfg : DefaultConfig (),
43+ },
44+ {
45+ name : "Text with details/summary" ,
46+ input : "Collapsed content: <details><summary>Click me</summary>Hidden content</details>" ,
47+ expected : "Collapsed content: \n \n **Click me:**\n Hidden content\n \n " ,
48+ cfg : DefaultConfig (),
49+ },
50+ {
51+ name : "Text with small font" ,
52+ input : "This has <span style=\" font-size:1px\" >hidden tiny text</span> in it." ,
53+ expected : "This has <span>hidden tiny text</span> in it." ,
54+ cfg : DefaultConfig (),
55+ },
56+ {
57+ name : "Text with excessive whitespace" ,
58+ input : "Line 1\n \n \n \n \n \n Line 2" ,
59+ expected : "Line 1\n \n \n Line 2" ,
60+ cfg : DefaultConfig (),
61+ },
62+ {
63+ name : "Text with HTML attributes" ,
64+ input : "<p data-hidden=\" true\" style=\" display:none\" >Hidden paragraph</p>" ,
65+ expected : "<p>Hidden paragraph</p>" ,
66+ cfg : DefaultConfig (),
67+ },
68+ {
69+ name : "Filtering disabled" ,
70+ input : "Hidden\u200B characters and <!-- comments -->" ,
71+ expected : "Hidden\u200B characters and <!-- comments -->" ,
72+ cfg : & Config {DisableContentFiltering : true },
73+ },
74+ {
75+ name : "Nil config uses default (filtering enabled)" ,
76+ input : "Hidden\u200B characters" ,
77+ expected : "Hiddencharacters" ,
78+ cfg : nil ,
79+ },
80+ {
81+ name : "Normal markdown with code blocks" ,
82+ input : "# Title\n \n ```go\n func main() {\n fmt.Println(\" Hello, world!\" )\n }\n ```" ,
83+ expected : "# Title\n \n ```go\n func main() {\n fmt.Println(\" Hello, world!\" )\n }\n ```" ,
84+ cfg : DefaultConfig (),
85+ },
86+ {
87+ name : "GitHub flavored markdown with tables" ,
88+ input : "| Header 1 | Header 2 |\n | -------- | -------- |\n | Cell 1 | Cell 2 |" ,
89+ expected : "| Header 1 | Header 2 |\n | -------- | -------- |\n | Cell 1 | Cell 2 |" ,
90+ cfg : DefaultConfig (),
91+ },
92+ }
93+
94+ for _ , tc := range tests {
95+ t .Run (tc .name , func (t * testing.T ) {
96+ result := FilterContent (tc .input , tc .cfg )
97+ if result != tc .expected {
98+ t .Errorf ("FilterContent() = %q, want %q" , result , tc .expected )
99+ }
100+ })
101+ }
102+ }
103+
104+ func TestMakeCollapsedSectionVisible (t * testing.T ) {
105+ tests := []struct {
106+ name string
107+ input string
108+ expected string
109+ }{
110+ {
111+ name : "Simple details/summary" ,
112+ input : "<details><summary>Click me</summary>Hidden content</details>" ,
113+ expected : "\n \n **Click me:**\n Hidden content\n \n " ,
114+ },
115+ {
116+ name : "Details without summary" ,
117+ input : "<details>Hidden content</details>" ,
118+ expected : "\n \n **Collapsed section:**\n Hidden content\n \n " ,
119+ },
120+ {
121+ name : "Nested content" ,
122+ input : "<details><summary>Outer</summary>Content<details><summary>Inner</summary>Nested</details></details>" ,
123+ expected : "\n \n **Outer:**\n Content<details><summary>Inner</summary>Nested</details>\n \n " ,
124+ },
125+ }
126+
127+ for _ , tc := range tests {
128+ t .Run (tc .name , func (t * testing.T ) {
129+ result := makeCollapsedSectionVisible (tc .input )
130+ if result != tc .expected {
131+ t .Errorf ("makeCollapsedSectionVisible() = %q, want %q" , result , tc .expected )
132+ }
133+ })
134+ }
135+ }
136+
137+ func TestCleanHTMLAttributes (t * testing.T ) {
138+ tests := []struct {
139+ name string
140+ input string
141+ expected string
142+ }{
143+ {
144+ name : "Tag with style attribute" ,
145+ input : "<p style=\" display:none\" >Hidden</p>" ,
146+ expected : "<p>Hidden</p>" ,
147+ },
148+ {
149+ name : "Tag with data attribute" ,
150+ input : "<p data-hidden=\" true\" >Hidden</p>" ,
151+ expected : "<p>Hidden</p>" ,
152+ },
153+ {
154+ name : "Tag with multiple attributes" ,
155+ input : "<p id=\" para\" style=\" display:none\" data-test=\" value\" >Hidden</p>" ,
156+ expected : "<p id=\" para\" >Hidden</p>" ,
157+ },
158+ {
159+ name : "Tag with allowed attributes" ,
160+ input : "<a href=\" https://example.com\" target=\" _blank\" >Link</a>" ,
161+ expected : "<a href=\" https://example.com\" target=\" _blank\" >Link</a>" ,
162+ },
163+ }
164+
165+ for _ , tc := range tests {
166+ t .Run (tc .name , func (t * testing.T ) {
167+ result := cleanHTMLAttributes (tc .input )
168+ if result != tc .expected {
169+ t .Errorf ("cleanHTMLAttributes() = %q, want %q" , result , tc .expected )
170+ }
171+ })
172+ }
173+ }
0 commit comments