escape_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2013 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 html
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. type unescapeTest struct {
  10. // A short description of the test case.
  11. desc string
  12. // The HTML text.
  13. html string
  14. // The unescaped text.
  15. unescaped string
  16. }
  17. var unescapeTests = []unescapeTest{
  18. // Handle no entities.
  19. {
  20. "copy",
  21. "A\ttext\nstring",
  22. "A\ttext\nstring",
  23. },
  24. // Handle simple named entities.
  25. {
  26. "simple",
  27. "& > <",
  28. "& > <",
  29. },
  30. // Handle hitting the end of the string.
  31. {
  32. "stringEnd",
  33. "&amp &amp",
  34. "& &",
  35. },
  36. // Handle entities with two codepoints.
  37. {
  38. "multiCodepoint",
  39. "text &gesl; blah",
  40. "text \u22db\ufe00 blah",
  41. },
  42. // Handle decimal numeric entities.
  43. {
  44. "decimalEntity",
  45. "Delta = &#916; ",
  46. "Delta = Δ ",
  47. },
  48. // Handle hexadecimal numeric entities.
  49. {
  50. "hexadecimalEntity",
  51. "Lambda = &#x3bb; = &#X3Bb ",
  52. "Lambda = λ = λ ",
  53. },
  54. // Handle numeric early termination.
  55. {
  56. "numericEnds",
  57. "&# &#x &#128;43 &copy = &#169f = &#xa9",
  58. "&# &#x €43 © = ©f = ©",
  59. },
  60. // Handle numeric ISO-8859-1 entity replacements.
  61. {
  62. "numericReplacements",
  63. "Footnote&#x87;",
  64. "Footnote‡",
  65. },
  66. // Handle single ampersand.
  67. {
  68. "copySingleAmpersand",
  69. "&",
  70. "&",
  71. },
  72. // Handle ampersand followed by non-entity.
  73. {
  74. "copyAmpersandNonEntity",
  75. "text &test",
  76. "text &test",
  77. },
  78. // Handle "&#".
  79. {
  80. "copyAmpersandHash",
  81. "text &#",
  82. "text &#",
  83. },
  84. }
  85. func TestUnescape(t *testing.T) {
  86. for _, tt := range unescapeTests {
  87. unescaped := UnescapeString(tt.html)
  88. if unescaped != tt.unescaped {
  89. t.Errorf("TestUnescape %s: want %q, got %q", tt.desc, tt.unescaped, unescaped)
  90. }
  91. }
  92. }
  93. func TestUnescapeEscape(t *testing.T) {
  94. ss := []string{
  95. ``,
  96. `abc def`,
  97. `a & b`,
  98. `a&amp;b`,
  99. `a &amp b`,
  100. `&quot;`,
  101. `"`,
  102. `"<&>"`,
  103. `&quot;&lt;&amp;&gt;&quot;`,
  104. `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
  105. `The special characters are: <, >, &, ' and "`,
  106. }
  107. for _, s := range ss {
  108. if got := UnescapeString(EscapeString(s)); got != s {
  109. t.Errorf("got %q want %q", got, s)
  110. }
  111. }
  112. }
  113. var (
  114. benchEscapeData = strings.Repeat("AAAAA < BBBBB > CCCCC & DDDDD ' EEEEE \" ", 100)
  115. benchEscapeNone = strings.Repeat("AAAAA x BBBBB x CCCCC x DDDDD x EEEEE x ", 100)
  116. benchUnescapeSparse = strings.Repeat(strings.Repeat("AAAAA x BBBBB x CCCCC x DDDDD x EEEEE x ", 10)+"&amp;", 10)
  117. benchUnescapeDense = strings.Repeat("&amp;&lt; &amp; &lt;", 100)
  118. )
  119. func BenchmarkEscape(b *testing.B) {
  120. n := 0
  121. for i := 0; i < b.N; i++ {
  122. n += len(EscapeString(benchEscapeData))
  123. }
  124. }
  125. func BenchmarkEscapeNone(b *testing.B) {
  126. n := 0
  127. for i := 0; i < b.N; i++ {
  128. n += len(EscapeString(benchEscapeNone))
  129. }
  130. }
  131. func BenchmarkUnescape(b *testing.B) {
  132. s := EscapeString(benchEscapeData)
  133. n := 0
  134. for i := 0; i < b.N; i++ {
  135. n += len(UnescapeString(s))
  136. }
  137. }
  138. func BenchmarkUnescapeNone(b *testing.B) {
  139. s := EscapeString(benchEscapeNone)
  140. n := 0
  141. for i := 0; i < b.N; i++ {
  142. n += len(UnescapeString(s))
  143. }
  144. }
  145. func BenchmarkUnescapeSparse(b *testing.B) {
  146. n := 0
  147. for i := 0; i < b.N; i++ {
  148. n += len(UnescapeString(benchUnescapeSparse))
  149. }
  150. }
  151. func BenchmarkUnescapeDense(b *testing.B) {
  152. n := 0
  153. for i := 0; i < b.N; i++ {
  154. n += len(UnescapeString(benchUnescapeDense))
  155. }
  156. }