html_test.go 2.7 KB

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