escape_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  64. func TestUnescape(t *testing.T) {
  65. for _, tt := range unescapeTests {
  66. unescaped := UnescapeString(tt.html)
  67. if unescaped != tt.unescaped {
  68. t.Errorf("TestUnescape %s: want %q, got %q", tt.desc, tt.unescaped, unescaped)
  69. }
  70. }
  71. }
  72. func TestUnescapeEscape(t *testing.T) {
  73. ss := []string{
  74. ``,
  75. `abc def`,
  76. `a & b`,
  77. `a&amp;b`,
  78. `a &amp b`,
  79. `&quot;`,
  80. `"`,
  81. `"<&>"`,
  82. `&quot;&lt;&amp;&gt;&quot;`,
  83. `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
  84. `The special characters are: <, >, &, ' and "`,
  85. }
  86. for _, s := range ss {
  87. if got := UnescapeString(EscapeString(s)); got != s {
  88. t.Errorf("got %q want %q", got, s)
  89. }
  90. }
  91. }