File: html_test.go

package info (click to toggle)
gcc-arm-none-eabi 15%3A12.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 959,712 kB
  • sloc: cpp: 3,275,382; ansic: 2,061,766; ada: 840,956; f90: 208,513; makefile: 76,132; asm: 73,433; xml: 50,448; exp: 34,146; sh: 32,436; objc: 15,637; fortran: 14,012; python: 11,991; pascal: 6,787; awk: 4,779; perl: 3,054; yacc: 338; ml: 285; lex: 201; haskell: 122
file content (97 lines) | stat: -rw-r--r-- 2,873 bytes parent folder | download | duplicates (56)
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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package template

import (
	"html"
	"strings"
	"testing"
)

func TestHTMLNospaceEscaper(t *testing.T) {
	input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
		"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
		` !"#$%&'()*+,-./` +
		`0123456789:;<=>?` +
		`@ABCDEFGHIJKLMNO` +
		`PQRSTUVWXYZ[\]^_` +
		"`abcdefghijklmno" +
		"pqrstuvwxyz{|}~\x7f" +
		"\u00A0\u0100\u2028\u2029\ufeff\ufdec\U0001D11E" +
		"erroneous\x960") // keep at the end

	want := ("&#xfffd;\x01\x02\x03\x04\x05\x06\x07" +
		"\x08&#9;&#10;&#11;&#12;&#13;\x0E\x0F" +
		"\x10\x11\x12\x13\x14\x15\x16\x17" +
		"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
		`&#32;!&#34;#$%&amp;&#39;()*&#43;,-./` +
		`0123456789:;&lt;&#61;&gt;?` +
		`@ABCDEFGHIJKLMNO` +
		`PQRSTUVWXYZ[\]^_` +
		`&#96;abcdefghijklmno` +
		`pqrstuvwxyz{|}~` + "\u007f" +
		"\u00A0\u0100\u2028\u2029\ufeff&#xfdec;\U0001D11E" +
		"erroneous&#xfffd;0") // keep at the end

	got := htmlNospaceEscaper(input)
	if got != want {
		t.Errorf("encode: want\n\t%q\nbut got\n\t%q", want, got)
	}

	r := strings.NewReplacer("\x00", "\ufffd", "\x96", "\ufffd")
	got, want = html.UnescapeString(got), r.Replace(input)
	if want != got {
		t.Errorf("decode: want\n\t%q\nbut got\n\t%q", want, got)
	}
}

func TestStripTags(t *testing.T) {
	tests := []struct {
		input, want string
	}{
		{"", ""},
		{"Hello, World!", "Hello, World!"},
		{"foo&amp;bar", "foo&amp;bar"},
		{`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
		{"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
		{"Foo <!-- Bar --> Baz", "Foo  Baz"},
		{"<", "<"},
		{"foo < bar", "foo < bar"},
		{`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
		{`Foo<div title="1>2">Bar`, "FooBar"},
		{`I <3 Ponies!`, `I <3 Ponies!`},
		{`<script>foo()</script>`, ``},
	}

	for _, test := range tests {
		if got := stripTags(test.input); got != test.want {
			t.Errorf("%q: want %q, got %q", test.input, test.want, got)
		}
	}
}

func BenchmarkHTMLNospaceEscaper(b *testing.B) {
	for i := 0; i < b.N; i++ {
		htmlNospaceEscaper("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
	}
}

func BenchmarkHTMLNospaceEscaperNoSpecials(b *testing.B) {
	for i := 0; i < b.N; i++ {
		htmlNospaceEscaper("The_quick,_brown_fox_jumps_over_the_lazy_dog.")
	}
}

func BenchmarkStripTags(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stripTags("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
	}
}

func BenchmarkStripTagsNoSpecials(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stripTags("The quick, brown fox jumps over the lazy dog.")
	}
}