escape_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "testing"
  6. type unescapeTest struct {
  7. // A short description of the test case.
  8. desc string
  9. // The HTML text.
  10. html string
  11. // The unescaped text.
  12. unescaped string
  13. }
  14. var unescapeTests = []unescapeTest{
  15. // Handle no entities.
  16. {
  17. "copy",
  18. "A\ttext\nstring",
  19. "A\ttext\nstring",
  20. },
  21. // Handle simple named entities.
  22. {
  23. "simple",
  24. "& > <",
  25. "& > <",
  26. },
  27. // Handle hitting the end of the string.
  28. {
  29. "stringEnd",
  30. "&amp &amp",
  31. "& &",
  32. },
  33. // Handle entities with two codepoints.
  34. {
  35. "multiCodepoint",
  36. "text &gesl; blah",
  37. "text \u22db\ufe00 blah",
  38. },
  39. // Handle decimal numeric entities.
  40. {
  41. "decimalEntity",
  42. "Delta = &#916; ",
  43. "Delta = Δ ",
  44. },
  45. // Handle hexadecimal numeric entities.
  46. {
  47. "hexadecimalEntity",
  48. "Lambda = &#x3bb; = &#X3Bb ",
  49. "Lambda = λ = λ ",
  50. },
  51. // Handle numeric early termination.
  52. {
  53. "numericEnds",
  54. "&# &#x &#128;43 &copy = &#169f = &#xa9",
  55. "&# &#x €43 © = ©f = ©",
  56. },
  57. // Handle numeric ISO-8859-1 entity replacements.
  58. {
  59. "numericReplacements",
  60. "Footnote&#x87;",
  61. "Footnote‡",
  62. },
  63. // Handle single ampersand.
  64. {
  65. "copySingleAmpersand",
  66. "&",
  67. "&",
  68. },
  69. // Handle ampersand followed by non-entity.
  70. {
  71. "copyAmpersandNonEntity",
  72. "text &test",
  73. "text &test",
  74. },
  75. // Handle "&#".
  76. {
  77. "copyAmpersandHash",
  78. "text &#",
  79. "text &#",
  80. },
  81. }
  82. func TestUnescape(t *testing.T) {
  83. for _, tt := range unescapeTests {
  84. unescaped := UnescapeString(tt.html)
  85. if unescaped != tt.unescaped {
  86. t.Errorf("TestUnescape %s: want %q, got %q", tt.desc, tt.unescaped, unescaped)
  87. }
  88. }
  89. }
  90. func TestUnescapeEscape(t *testing.T) {
  91. ss := []string{
  92. ``,
  93. `abc def`,
  94. `a & b`,
  95. `a&amp;b`,
  96. `a &amp b`,
  97. `&quot;`,
  98. `"`,
  99. `"<&>"`,
  100. `&quot;&lt;&amp;&gt;&quot;`,
  101. `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
  102. `The special characters are: <, >, &, ' and "`,
  103. }
  104. for _, s := range ss {
  105. if got := UnescapeString(EscapeString(s)); got != s {
  106. t.Errorf("got %q want %q", got, s)
  107. }
  108. }
  109. }