-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpipeline_test.go
146 lines (124 loc) · 4.99 KB
/
pipeline_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
package pipeline
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
htmlSpaceRe = regexp.MustCompile(`>[\s]+<`)
)
func assertHTMLEqual(t *testing.T, exptected, actual string) {
exptected = strings.TrimSpace(exptected)
actual = strings.TrimSpace(actual)
if htmlSpaceRe.ReplaceAllString(exptected, "><") != htmlSpaceRe.ReplaceAllString(actual, "><") {
t.Errorf("\nexptected:\n%s\nactual :\n%s", exptected, actual)
}
}
func assertCall(t *testing.T, pipeline Pipeline, exptected, raw string) {
out, err := pipeline.Call(raw)
assert.NoError(t, err)
assert.Equal(t, exptected, out)
}
func TestNewPipeline(t *testing.T) {
pipeline := NewPipeline([]Filter{
&SanitizationFilter{},
})
assert.Equal(t, 1, len(pipeline.Filters))
out, err := pipeline.Call("<p>Hello world<script>alert</script></p>")
assert.NoError(t, err)
assert.Equal(t, "<p>Hello world</p>", out)
}
func BenchmarkMultiplePiplelines(b *testing.B) {
raw := `#huacnlee This is a #test_huacn-lee of some cool #中文名称 features that #mi_asd be
#use-ful but #don't. look at this email#address.com. #bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the comment blocks.
But #msft is cool. #huacnlee This is a #test_huacn-lee of some cool #中文名称 features that #mi_asd be
#use-ful but #don't. look at this email#address.com. #bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the comment blocks.
But #msft is cool. #huacnlee This is a #test_huacn-lee of some cool #中文名称 features that #mi_asd be
#use-ful but #don't. look at this email#address.com. #bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the comment blocks.
But #msft is cool.`
pipe := NewPipeline([]Filter{
HTMLEscapeFilter{},
SimpleFormatFilter{},
MentionFilter{},
})
for i := 0; i < b.N; i++ {
// 41251 ns/op
pipe.Call(raw)
}
}
func ExamplePipeline() {
pipe := NewPipeline([]Filter{
MarkdownFilter{},
SanitizationFilter{},
MentionFilter{
Prefix: "#",
Format: func(name string) string {
return fmt.Sprintf(`<a href="https://github.com/topic/%s">#%s</a>`, name, name)
},
},
MentionFilter{
Prefix: "@",
Format: func(name string) string {
return fmt.Sprintf(`<a href="https://github.com/%s">@%s</a>`, name, name)
},
},
})
markdown := `# Hello world
 [Click me](javascript:alert)
This is #html-pipeline example, @huacnlee created.`
out, _ := pipe.Call(markdown)
fmt.Printf(out)
// Output:
// <h1>Hello world</h1>
//
// <p><img alt=""/> Click me</p>
//
// <p>This is <a href="https://github.com/topic/html-pipeline">#html-pipeline</a> example, <a href="https://github.com/huacnlee">@huacnlee</a> created.</p>
}
func TestHTMLUnescape(t *testing.T) {
raw := "We don't like 'escape' and 'unescape'."
pipe := NewPipeline([]Filter{})
out, _ := pipe.Call(raw)
assert.Equal(t, raw, out)
raw = "<object props=\"{"url": "https://example.com/a.jpg"}\">We don't like 'escape'</object>"
out, _ = pipe.Call(raw)
assert.Equal(t, `<object props="{"url": "https://example.com/a.jpg"}">We don't like 'escape'</object>`, out)
}
func TestRenderPlainText(t *testing.T) {
raw := `
[tag value="qcc"]Foo#QuantumScape & Corporation Class A[/tag] @huacnlee with 'code',
<script>alert()</script> <style>style tag</style> <mark value="Bar">HTML mark will not remove</mark> recenty pressed "News".
https://www.google.com/search?newwindow=1&sxsrf=ALeKk01IaJz5BXWn2_C3_AFY3m_NL0c0pQ%3A1609989927324&ei=J3_2X76rE66zmAWLxIfoCg&q=Complex+url+%E4%B8%AD%E6%96%87&oq=Complex+url+%E4%B8%AD%E6%96%87&gs_lcp=CgZwc3ktYWIQAzoFCAAQywE6BAgAEB46BggAEAUQHjoGCAAQCBAeOgUIIRCgAVDJO1jORGDyRWgAcAB4AoAB_AGIAeQNkgEFMC4zLjaYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab&ved=0ahUKEwj-2tbt74juAhWuGaYKHQviAa0Q4dUDCA0&uact=5
`
expected := `
[tag value="qcc"]Foo#QuantumScape & Corporation Class A[/tag] [mt value="huacnlee"]@huacnlee[/mt] with 'code',
alert() style tag HTML mark will not remove recenty pressed "News".
https://www.google.com/search?newwindow=1&sxsrf=ALeKk01IaJz5BXWn2_C3_AFY3m_NL0c0pQ%3A1609989927324&ei=J3_2X76rE66zmAWLxIfoCg&q=Complex+url+%E4%B8%AD%E6%96%87&oq=Complex+url+%E4%B8%AD%E6%96%87&gs_lcp=CgZwc3ktYWIQAzoFCAAQywE6BAgAEB46BggAEAUQHjoGCAAQCBAeOgUIIRCgAVDJO1jORGDyRWgAcAB4AoAB_AGIAeQNkgEFMC4zLjaYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab&ved=0ahUKEwj-2tbt74juAhWuGaYKHQviAa0Q4dUDCA0&uact=5
`
// With HTMLEscapeFilter (will ignore)
pipe := NewPlainPipeline([]Filter{
HTMLEscapeFilter{},
MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`[mt value="%s"]@%s[/mt]`, name, name)
},
},
})
out, _ := pipe.Call(raw)
assert.Equal(t, strings.TrimSpace(expected), out)
// Without HTMLEscapeFilter
pipe = NewPlainPipeline([]Filter{
MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`[mt value="%s"]@%s[/mt]`, name, name)
},
},
})
out, _ = pipe.Call(raw)
assert.Equal(t, strings.TrimSpace(expected), out)
}