Home | History | Annotate | Download | only in template
      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.
      4 
      5 package template
      6 
      7 import (
      8 	"html"
      9 	"strings"
     10 	"testing"
     11 )
     12 
     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" +
     16 		` !"#$%&'()*+,-./` +
     17 		`0123456789:;<=>?` +
     18 		`@ABCDEFGHIJKLMNO` +
     19 		`PQRSTUVWXYZ[\]^_` +
     20 		"`abcdefghijklmno" +
     21 		"pqrstuvwxyz{|}~\x7f" +
     22 		"\u00A0\u0100\u2028\u2029\ufeff\ufdec\U0001D11E" +
     23 		"erroneous\x960") // keep at the end
     24 
     25 	want := ("&#xfffd;\x01\x02\x03\x04\x05\x06\x07" +
     26 		"\x08&#9;&#10;&#11;&#12;&#13;\x0E\x0F" +
     27 		"\x10\x11\x12\x13\x14\x15\x16\x17" +
     28 		"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
     29 		`&#32;!&#34;#$%&amp;&#39;()*&#43;,-./` +
     30 		`0123456789:;&lt;&#61;&gt;?` +
     31 		`@ABCDEFGHIJKLMNO` +
     32 		`PQRSTUVWXYZ[\]^_` +
     33 		`&#96;abcdefghijklmno` +
     34 		`pqrstuvwxyz{|}~` + "\u007f" +
     35 		"\u00A0\u0100\u2028\u2029\ufeff&#xfdec;\U0001D11E" +
     36 		"erroneous&#xfffd;0") // keep at the end
     37 
     38 	got := htmlNospaceEscaper(input)
     39 	if got != want {
     40 		t.Errorf("encode: want\n\t%q\nbut got\n\t%q", want, got)
     41 	}
     42 
     43 	r := strings.NewReplacer("\x00", "\ufffd", "\x96", "\ufffd")
     44 	got, want = html.UnescapeString(got), r.Replace(input)
     45 	if want != got {
     46 		t.Errorf("decode: want\n\t%q\nbut got\n\t%q", want, got)
     47 	}
     48 }
     49 
     50 func TestStripTags(t *testing.T) {
     51 	tests := []struct {
     52 		input, want string
     53 	}{
     54 		{"", ""},
     55 		{"Hello, World!", "Hello, World!"},
     56 		{"foo&amp;bar", "foo&amp;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"},
     60 		{"<", "<"},
     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>`, ``},
     66 	}
     67 
     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)
     71 		}
     72 	}
     73 }
     74 
     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>")
     78 	}
     79 }
     80 
     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.")
     84 	}
     85 }
     86 
     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>")
     90 	}
     91 }
     92 
     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.")
     96 	}
     97 }
     98