1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestHTMLNospaceEscaper(t *testing.T) {
14 input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
15 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
21 "pqrstuvwxyz{|}~\x7f" +
22 "\u00A0\u0100\u2028\u2029\ufeff\ufdec\U0001D11E")
24 want := ("�\x01\x02\x03\x04\x05\x06\x07" +
25 "\x08	  \x0E\x0F" +
26 "\x10\x11\x12\x13\x14\x15\x16\x17" +
27 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
28 ` !"#$%&'()*+,-./` +
29 `0123456789:;<=>?` +
32 ``abcdefghijklmno` +
33 `pqrstuvwxyz{|}~` + "\u007f" +
34 "\u00A0\u0100\u2028\u2029\ufeff\U0001D11E")
36 got := htmlNospaceEscaper(input)
38 t.Errorf("encode: want\n\t%q\nbut got\n\t%q", want, got)
41 got, want = html.UnescapeString(got), strings.Replace(input, "\x00", "\ufffd", 1)
43 t.Errorf("decode: want\n\t%q\nbut got\n\t%q", want, got)
47 func TestStripTags(t *testing.T) {
52 {"Hello, World!", "Hello, World!"},
53 {"foo&bar", "foo&bar"},
54 {`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
55 {"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
56 {"Foo <!-- Bar --> Baz", "Foo Baz"},
58 {"foo < bar", "foo < bar"},
59 {`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
60 {`Foo<div title="1>2">Bar`, "FooBar"},
61 {`I <3 Ponies!`, `I <3 Ponies!`},
62 {`<script>foo()</script>`, ``},
65 for _, test := range tests {
66 if got := stripTags(test.input); got != test.want {
67 t.Errorf("%q: want %q, got %q", test.input, test.want, got)
72 func BenchmarkHTMLNospaceEscaper(b *testing.B) {
73 for i := 0; i < b.N; i++ {
74 htmlNospaceEscaper("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
78 func BenchmarkHTMLNospaceEscaperNoSpecials(b *testing.B) {
79 for i := 0; i < b.N; i++ {
80 htmlNospaceEscaper("The_quick,_brown_fox_jumps_over_the_lazy_dog.")
84 func BenchmarkStripTags(b *testing.B) {
85 for i := 0; i < b.N; i++ {
86 stripTags("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
90 func BenchmarkStripTagsNoSpecials(b *testing.B) {
91 for i := 0; i < b.N; i++ {
92 stripTags("The quick, brown fox jumps over the lazy dog.")