Repository navigation
Expand file tree
/
Copy pathxml_test.go
More file actions
163 lines (148 loc) · 4.6 KB
/
Copy pathxml_test.go
File metadata and controls
163 lines (148 loc) · 4.6 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
package xml
import (
"bytes"
"fmt"
"os"
"regexp"
"testing"
"github.com/lpha/minify/v2"
"github.com/tdewolff/test"
)
func TestXML(t *testing.T) {
xmlTests := []struct {
xml string
expected string
}{
{`<!-- comment -->`, ``},
{`<A>x</A>`, `<A>x</A>`},
{`<a><b>x</b></a>`, `<a><b>x</b></a>`},
{"<a><b>x\ny</b></a>", "<a><b>x\ny</b></a>"},
{`<a> <![CDATA[ a ]]> </a>`, `<a>a</a>`},
{`<a >a</a >`, `<a>a</a>`},
{`<?xml version="1.0" ?>`, `<?xml version="1.0"?>`},
{`<x></x>`, `<x/>`},
{`<x> </x>`, `<x/>`},
{`<x a="b"></x>`, `<x a="b"/>`},
{`<x a=""></x>`, `<x a=""/>`},
{`<x a=a></x>`, `<x a=a/>`},
{"<x a=\" a \n\r\t b \"/>", `<x a=" a b "/>`},
{`<x a="'b""></x>`, `<x a="'b""/>`},
{`<x a="""'"></x>`, `<x a='""''/>`},
{`<x a="&<>"></x>`, `<x a="&<>"/>`},
{`<x>&<></x>`, `<x>&<></x>`},
{`<x>&&<</x>`, `<x>&&<</x>`},
{`<!DOCTYPE foo SYSTEM "Foo.dtd">`, `<!DOCTYPE foo SYSTEM "Foo.dtd">`},
{`text <!--comment--> text`, `text text`},
{"text\n<!--comment-->\ntext", "text\ntext"},
{`<!doctype html>`, `<!doctype html=>`}, // bad formatted, doctype must be uppercase and html must have attribute value
{"<x>\n<!--y-->\n</x>", `<x></x>`},
{`<style>lala{color:red}</style>`, `<style>lala{color:red}</style>`},
{`cats and dogs `, `cats and dogs`},
// go fuzz
{`</0`, `</0`},
{`<!DOCTYPE`, `<!DOCTYPE`},
{`<![CDATA[`, ``},
}
m := minify.New()
for _, tt := range xmlTests {
t.Run(tt.xml, func(t *testing.T) {
r := bytes.NewBufferString(tt.xml)
w := &bytes.Buffer{}
err := Minify(m, w, r, nil)
test.Minify(t, tt.xml, err, w.String(), tt.expected)
})
}
}
func TestXMLKeepWhitespace(t *testing.T) {
xmlTests := []struct {
xml string
expected string
}{
{`cats and dogs `, `cats and dogs`},
{` <div> <i> test </i> <b> test </b> </div> `, `<div> <i> test </i> <b> test </b> </div>`},
{"text\n<!--comment-->\ntext", "text\ntext"},
{"text\n<!--comment-->text<!--comment--> text", "text\ntext text"},
{"<x>\n<!--y-->\n</x>", "<x>\n</x>"},
{`<style>lala{color:red}</style>`, `<style>lala{color:red}</style>`},
{`<x> <?xml?> </x>`, `<x><?xml?> </x>`},
{`<x> <![CDATA[ x ]]> </x>`, `<x> x </x>`},
{`<x> <![CDATA[ <<<<< ]]> </x>`, `<x><![CDATA[ <<<<< ]]></x>`},
}
m := minify.New()
xmlMinifier := &Minifier{KeepWhitespace: true}
for _, tt := range xmlTests {
t.Run(tt.xml, func(t *testing.T) {
r := bytes.NewBufferString(tt.xml)
w := &bytes.Buffer{}
err := xmlMinifier.Minify(m, w, r, nil)
test.Minify(t, tt.xml, err, w.String(), tt.expected)
})
}
}
func TestXMLKeepCDATA(t *testing.T) {
xmlTests := []struct {
xml string
expected string
}{
{`cats and dogs `, `cats and dogs`},
{` <div> <i> test </i> <b> test </b> </div> `, `<div> <i> test </i> <b> test </b> </div>`},
{"text\n<!--comment-->\ntext", "text\ntext"},
{"text\n<!--comment-->text<!--comment--> text", "text\ntext text"},
{"<x>\n<!--y-->\n</x>", "<x>\n</x>"},
{`<style>lala{color:red}</style>`, `<style>lala{color:red}</style>`},
{`<x> <?xml?> </x>`, `<x><?xml?> </x>`},
{`<x> <![CDATA[ x ]]> </x>`, `<x><![CDATA[ x ]]</x>`},
{`<x> <![CDATA[ <<<<< ]]> </x>`, `<x><![CDATA[ <<<<< ]]></x>`},
}
m := minify.New()
xmlMinifier := &Minifier{KeepWhitespace: true}
for _, tt := range xmlTests {
t.Run(tt.xml, func(t *testing.T) {
r := bytes.NewBufferString(tt.xml)
w := &bytes.Buffer{}
err := xmlMinifier.Minify(m, w, r, nil)
test.Minify(t, tt.xml, err, w.String(), tt.expected)
})
}
}
func TestReaderErrors(t *testing.T) {
r := test.NewErrorReader(0)
w := &bytes.Buffer{}
m := minify.New()
err := Minify(m, w, r, nil)
test.T(t, err, test.ErrPlain, "return error at first read")
}
func TestWriterErrors(t *testing.T) {
errorTests := []struct {
xml string
n []int
}{
{`<!DOCTYPE foo>`, []int{0}},
{`<?xml?>`, []int{0, 1}},
{`<a x=y z="val">`, []int{0, 1, 2, 3, 4, 8, 9}},
{`<foo/>`, []int{1}},
{`</foo>`, []int{0}},
{`<foo></foo>`, []int{1}},
{`<![CDATA[data<<<<<]]>`, []int{0}},
{`text`, []int{0}},
}
m := minify.New()
for _, tt := range errorTests {
for _, n := range tt.n {
t.Run(fmt.Sprint(tt.xml, " ", tt.n), func(t *testing.T) {
r := bytes.NewBufferString(tt.xml)
w := test.NewErrorWriter(n)
err := Minify(m, w, r, nil)
test.T(t, err, test.ErrPlain)
})
}
}
}
////////////////////////////////////////////////////////////////
func ExampleMinify() {
m := minify.New()
m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), Minify)
if err := m.Minify("text/xml", os.Stdout, os.Stdin); err != nil {
panic(err)
}
}