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" +
23 "erroneous\x960") // keep at the end
25 want
:= ("�\x01\x02\x03\x04\x05\x06\x07" +
26 "\x08	  \x0E\x0F" +
27 "\x10\x11\x12\x13\x14\x15\x16\x17" +
28 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
29 ` !"#$%&'()*+,-./` +
30 `0123456789:;<=>?` +
33 ``abcdefghijklmno` +
34 `pqrstuvwxyz{|}~` + "\u007f" +
35 "\u00A0\u0100\u2028\u2029\ufeff\U0001D11E" +
36 "erroneous�0") // keep at the end
38 got
:= htmlNospaceEscaper(input
)
40 t
.Errorf("encode: want\n\t%q\nbut got\n\t%q", want
, got
)
43 r
:= strings
.NewReplacer("\x00", "\ufffd", "\x96", "\ufffd")
44 got
, want
= html
.UnescapeString(got
), r
.Replace(input
)
46 t
.Errorf("decode: want\n\t%q\nbut got\n\t%q", want
, got
)
50 func TestStripTags(t
*testing
.T
) {
55 {"Hello, World!", "Hello, World!"},
56 {"foo&bar", "foo&bar"},
57 {`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
58 {"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
59 {"Foo <!-- Bar --> Baz", "Foo Baz"},
61 {"foo < bar", "foo < bar"},
62 {`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
63 {`Foo<div title="1>2">Bar`, "FooBar"},
64 {`I <3 Ponies!`, `I <3 Ponies!`},
65 {`<script>foo()</script>`, ``},
68 for _
, test
:= range tests
{
69 if got
:= stripTags(test
.input
); got
!= test
.want
{
70 t
.Errorf("%q: want %q, got %q", test
.input
, test
.want
, got
)
75 func BenchmarkHTMLNospaceEscaper(b
*testing
.B
) {
76 for i
:= 0; i
< b
.N
; i
++ {
77 htmlNospaceEscaper("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
81 func BenchmarkHTMLNospaceEscaperNoSpecials(b
*testing
.B
) {
82 for i
:= 0; i
< b
.N
; i
++ {
83 htmlNospaceEscaper("The_quick,_brown_fox_jumps_over_the_lazy_dog.")
87 func BenchmarkStripTags(b
*testing
.B
) {
88 for i
:= 0; i
< b
.N
; i
++ {
89 stripTags("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
93 func BenchmarkStripTagsNoSpecials(b
*testing
.B
) {
94 for i
:= 0; i
< b
.N
; i
++ {
95 stripTags("The quick, brown fox jumps over the lazy dog.")