-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer_test.go
181 lines (162 loc) · 4.88 KB
/
transformer_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
package gsoup
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func Test_ShouldNotAllowTransformingDocNode(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.DocumentNode {
x.SetType(html.TextNode)
x.SetData("wot")
}
return x
})
actual, err := c.CleanString(`<i>some text</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>some text</i>`, actual)
}
func Test_ShouldDoSimpleTagReplace(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B))
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.I {
x.SetAtom(atom.B)
}
return x
})
actual, err := c.CleanString(`<i>some text</i>`)
assert.Nil(t, err)
assert.Equal(t, `<b>some text</b>`, actual)
}
func Test_ShouldStillValidateTransformedElements(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B)).PreserveChildren()
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.I {
x.SetAtom(atom.Em)
}
return x
})
actual, err := c.CleanString(`<i>some text</i>`)
assert.Nil(t, err)
assert.Equal(t, `some text`, actual)
}
func Test_ShouldReplaceText(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B))
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.TextNode {
x.SetData("xxx")
}
return x
})
actual, err := c.CleanString(`<b>some text</b>`)
assert.Nil(t, err)
assert.Equal(t, `<b>xxx</b>`, actual)
}
func Test_ShouldDeleteNodeAndChildren(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B), T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.B {
return nil
}
return x
})
actual, err := c.CleanString(`<i>keep</i><b><i>destroy</i></b><i>keep2</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>keep</i><i>keep2</i>`, actual)
}
func Test_ShouldAllowReplacementWithFirstChild(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B), T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.B {
return x.FirstChild()
}
return x
})
actual, err := c.CleanString(`<i>keep1</i><b><i>keep2</i><i>forget about me</i></b><i>keep3</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>keep1</i><i>keep2</i><i>keep3</i>`, actual)
// test that it works for a nil child
actual, err = c.CleanString(`<i>keep1</i><b></b><i>keep3</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>keep1</i><i>keep3</i>`, actual)
}
func Test_ShouldAllowReplacementWithLastChild(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B), T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.B {
return x.LastChild()
}
return x
})
actual, err := c.CleanString(`<i>keep1</i><b><i>forget about me</i>me too<i>keep2</i></b><i>keep3</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>keep1</i><i>keep2</i><i>keep3</i>`, actual)
// test that it works for a nil child
actual, err = c.CleanString(`<i>keep1</i><b></b><i>keep3</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>keep1</i><i>keep3</i>`, actual)
}
func Test_ShouldInspectDataAndConditionallyReplace(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.TextNode {
x.SetData(strings.ToUpper(x.Data()))
}
return x
})
actual, err := c.CleanString(`<i>one</i>two<i>three</i>`)
assert.Nil(t, err)
assert.Equal(t, `<i>ONE</i>TWO<i>THREE</i>`, actual)
}
func Test_ShouldBeAbleToModifyElementAttributes(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.I, "foo", "bar"), T(atom.B, "foo", "bar"))
c.AddTransformer(func(x XNode) XNode {
if x.Atom() == atom.I {
attrs := x.Attr()
var newAttrs []html.Attribute
for _, attr := range attrs {
if attr.Key == "foo" {
newAttrs = append(newAttrs, html.Attribute{Key: "foo", Val: "manchu"})
}
}
x.SetAttrs(newAttrs)
}
return x
})
actual, err := c.CleanString(`<i foo="fighter" bar="brawl">hi</i><b foo="fighter" bar="brawl">there</b>`)
assert.Nil(t, err)
assert.Equal(t, `<i foo="manchu">hi</i><b foo="fighter" bar="brawl">there</b>`, actual)
}
func Test_ShouldBeAbleToSetType(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B), T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.ElementNode && x.Atom() == atom.B {
x.SetType(html.TextNode)
}
return x
})
actual, err := c.CleanString(`<i>a</i><b>c</b>d`)
assert.Nil(t, err)
assert.Equal(t, `<i>a</i>bd`, actual)
}
func Test_ShouldApplyMultipleTransformers(t *testing.T) {
c := NewEmptyCleaner().AddTags(T(atom.B), T(atom.I))
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.ElementNode && x.Atom() == atom.Em {
x.SetAtom(atom.I)
}
return x
})
c.AddTransformer(func(x XNode) XNode {
if x.Type() == html.ElementNode && x.Atom() == atom.Strong {
x.SetAtom(atom.B)
}
return x
})
actual, err := c.CleanString(`<em>a<strong>b</strong></em><strong>c</strong>`)
assert.Nil(t, err)
assert.Equal(t, `<i>a<b>b</b></i><b>c</b>`, actual)
}