github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/internal/go_templates/htmltemplate/html_test.go (about)

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