Debian Codegraph @

tpl/internal/go_templates/htmltemplate/html_test.go

raw ◀ ▶
1 Debian patch touch this file
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 //go:build !windows
6
7 package template
8
9 import (
10 "html"
11 "strings"
12 "testing"
13 )
14
15 func TestHTMLNospaceEscaper(t *testing.T) {
16 input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
17 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
18 ` !"#$%&'()*+,-./` +
19 `0123456789:;<=>?` +
20 `@ABCDEFGHIJKLMNO` +
21 `PQRSTUVWXYZ[\]^_` +
22 "`abcdefghijklmno" +
23 "pqrstuvwxyz{|}~\x7f" +
24 "\u00A0\u0100\u2028\u2029\ufeff\ufdec\U0001D11E" +
25 "erroneous\x960") // keep at the end
26
27 want := ("&#xfffd;\x01\x02\x03\x04\x05\x06\x07" +
28 "\x08&#9;&#10;&#11;&#12;&#13;\x0E\x0F" +
29 "\x10\x11\x12\x13\x14\x15\x16\x17" +
30 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
31 `&#32;!&#34;#$%&amp;&#39;()*&#43;,-./` +
32 `0123456789:;&lt;&#61;&gt;?` +
33 `@ABCDEFGHIJKLMNO` +
34 `PQRSTUVWXYZ[\]^_` +
35 `&#96;abcdefghijklmno` +
36 `pqrstuvwxyz{|}~` + "\u007f" +
37 "\u00A0\u0100\u2028\u2029\ufeff&#xfdec;\U0001D11E" +
38 "erroneous&#xfffd;0") // keep at the end
39
40 got := htmlNospaceEscaper(input)
41 if got != want {
42 t.Errorf("encode: want\n\t%q\nbut got\n\t%q", want, got)
43 }
44
45 r := strings.NewReplacer("\x00", "\ufffd", "\x96", "\ufffd")
46 got, want = html.UnescapeString(got), r.Replace(input)
47 if want != got {
48 t.Errorf("decode: want\n\t%q\nbut got\n\t%q", want, got)
49 }
50 }
51
52 func TestStripTags(t *testing.T) {
53 tests := []struct {
54 input, want string
55 }{
56 {"", ""},
57 {"Hello, World!", "Hello, World!"},
58 {"foo&amp;bar", "foo&amp;bar"},
59 {`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
60 {"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
61 {"Foo <!-- Bar --> Baz", "Foo Baz"},
62 {"<", "<"},
63 {"foo < bar", "foo < bar"},
64 {`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
65 {`Foo<div title="1>2">Bar`, "FooBar"},
66 {`I <3 Ponies!`, `I <3 Ponies!`},
67 {`<script>foo()</script>`, ``},
68 }
69
70 for _, test := range tests {
71 if got := stripTags(test.input); got != test.want {
72 t.Errorf("%q: want %q, got %q", test.input, test.want, got)
73 }
74 }
75 }
76
77 func BenchmarkHTMLNospaceEscaper(b *testing.B) {
78 for i := 0; i < b.N; i++ {
79 htmlNospaceEscaper("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
80 }
81 }
82
83 func BenchmarkHTMLNospaceEscaperNoSpecials(b *testing.B) {
84 for i := 0; i < b.N; i++ {
85 htmlNospaceEscaper("The_quick,_brown_fox_jumps_over_the_lazy_dog.")
86 }
87 }
88
89 func BenchmarkStripTags(b *testing.B) {
90 for i := 0; i < b.N; i++ {
91 stripTags("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
92 }
93 }
94
95 func BenchmarkStripTagsNoSpecials(b *testing.B) {
96 for i := 0; i < b.N; i++ {
97 stripTags("The quick, brown fox jumps over the lazy dog.")
98 }
99 }