escape.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2010 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 provides functions for escaping and unescaping HTML text.
  5. package html
  6. import (
  7. "bytes"
  8. "strings"
  9. "unicode/utf8"
  10. )
  11. type writer interface {
  12. WriteString(string) (int, error)
  13. }
  14. // These replacements permit compatibility with old numeric entities that
  15. // assumed Windows-1252 encoding.
  16. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference
  17. var replacementTable = [...]rune{
  18. '\u20AC', // First entry is what 0x80 should be replaced with.
  19. '\u0081',
  20. '\u201A',
  21. '\u0192',
  22. '\u201E',
  23. '\u2026',
  24. '\u2020',
  25. '\u2021',
  26. '\u02C6',
  27. '\u2030',
  28. '\u0160',
  29. '\u2039',
  30. '\u0152',
  31. '\u008D',
  32. '\u017D',
  33. '\u008F',
  34. '\u0090',
  35. '\u2018',
  36. '\u2019',
  37. '\u201C',
  38. '\u201D',
  39. '\u2022',
  40. '\u2013',
  41. '\u2014',
  42. '\u02DC',
  43. '\u2122',
  44. '\u0161',
  45. '\u203A',
  46. '\u0153',
  47. '\u009D',
  48. '\u017E',
  49. '\u0178', // Last entry is 0x9F.
  50. // 0x00->'\uFFFD' is handled programmatically.
  51. // 0x0D->'\u000D' is a no-op.
  52. }
  53. // unescapeEntity reads an entity like "<" from b[src:] and writes the
  54. // corresponding "<" to b[dst:], returning the incremented dst and src cursors.
  55. // Precondition: b[src] == '&' && dst <= src.
  56. // attribute should be true if parsing an attribute value.
  57. func unescapeEntity(b []byte, dst, src int, attribute bool) (dst1, src1 int) {
  58. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference
  59. // i starts at 1 because we already know that s[0] == '&'.
  60. i, s := 1, b[src:]
  61. if len(s) <= 1 {
  62. b[dst] = b[src]
  63. return dst + 1, src + 1
  64. }
  65. if s[i] == '#' {
  66. if len(s) <= 3 { // We need to have at least "&#.".
  67. b[dst] = b[src]
  68. return dst + 1, src + 1
  69. }
  70. i++
  71. c := s[i]
  72. hex := false
  73. if c == 'x' || c == 'X' {
  74. hex = true
  75. i++
  76. }
  77. x := '\x00'
  78. for i < len(s) {
  79. c = s[i]
  80. i++
  81. if hex {
  82. if '0' <= c && c <= '9' {
  83. x = 16*x + rune(c) - '0'
  84. continue
  85. } else if 'a' <= c && c <= 'f' {
  86. x = 16*x + rune(c) - 'a' + 10
  87. continue
  88. } else if 'A' <= c && c <= 'F' {
  89. x = 16*x + rune(c) - 'A' + 10
  90. continue
  91. }
  92. } else if '0' <= c && c <= '9' {
  93. x = 10*x + rune(c) - '0'
  94. continue
  95. }
  96. if c != ';' {
  97. i--
  98. }
  99. break
  100. }
  101. if i <= 3 { // No characters matched.
  102. b[dst] = b[src]
  103. return dst + 1, src + 1
  104. }
  105. if 0x80 <= x && x <= 0x9F {
  106. // Replace characters from Windows-1252 with UTF-8 equivalents.
  107. x = replacementTable[x-0x80]
  108. } else if x == 0 || (0xD800 <= x && x <= 0xDFFF) || x > 0x10FFFF {
  109. // Replace invalid characters with the replacement character.
  110. x = '\uFFFD'
  111. }
  112. return dst + utf8.EncodeRune(b[dst:], x), src + i
  113. }
  114. // Consume the maximum number of characters possible, with the
  115. // consumed characters matching one of the named references.
  116. for i < len(s) {
  117. c := s[i]
  118. i++
  119. // Lower-cased characters are more common in entities, so we check for them first.
  120. if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
  121. continue
  122. }
  123. if c != ';' {
  124. i--
  125. }
  126. break
  127. }
  128. entityName := string(s[1:i])
  129. if entityName == "" {
  130. // No-op.
  131. } else if attribute && entityName[len(entityName)-1] != ';' && len(s) > i && s[i] == '=' {
  132. // No-op.
  133. } else if x := entity[entityName]; x != 0 {
  134. return dst + utf8.EncodeRune(b[dst:], x), src + i
  135. } else if x := entity2[entityName]; x[0] != 0 {
  136. dst1 := dst + utf8.EncodeRune(b[dst:], x[0])
  137. return dst1 + utf8.EncodeRune(b[dst1:], x[1]), src + i
  138. } else if !attribute {
  139. maxLen := len(entityName) - 1
  140. if maxLen > longestEntityWithoutSemicolon {
  141. maxLen = longestEntityWithoutSemicolon
  142. }
  143. for j := maxLen; j > 1; j-- {
  144. if x := entity[entityName[:j]]; x != 0 {
  145. return dst + utf8.EncodeRune(b[dst:], x), src + j + 1
  146. }
  147. }
  148. }
  149. dst1, src1 = dst+i, src+i
  150. copy(b[dst:dst1], b[src:src1])
  151. return dst1, src1
  152. }
  153. // unescape unescapes b's entities in-place, so that "a&lt;b" becomes "a<b".
  154. func unescape(b []byte) []byte {
  155. for i, c := range b {
  156. if c == '&' {
  157. dst, src := unescapeEntity(b, i, i, false)
  158. for src < len(b) {
  159. c := b[src]
  160. if c == '&' {
  161. dst, src = unescapeEntity(b, dst, src, false)
  162. } else {
  163. b[dst] = c
  164. dst, src = dst+1, src+1
  165. }
  166. }
  167. return b[0:dst]
  168. }
  169. }
  170. return b
  171. }
  172. const escapedChars = `&'<>"`
  173. func escape(w writer, s string) error {
  174. i := strings.IndexAny(s, escapedChars)
  175. for i != -1 {
  176. if _, err := w.WriteString(s[:i]); err != nil {
  177. return err
  178. }
  179. var esc string
  180. switch s[i] {
  181. case '&':
  182. esc = "&amp;"
  183. case '\'':
  184. // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
  185. esc = "&#39;"
  186. case '<':
  187. esc = "&lt;"
  188. case '>':
  189. esc = "&gt;"
  190. case '"':
  191. // "&#34;" is shorter than "&quot;".
  192. esc = "&#34;"
  193. default:
  194. panic("unrecognized escape character")
  195. }
  196. s = s[i+1:]
  197. if _, err := w.WriteString(esc); err != nil {
  198. return err
  199. }
  200. i = strings.IndexAny(s, escapedChars)
  201. }
  202. _, err := w.WriteString(s)
  203. return err
  204. }
  205. // EscapeString escapes special characters like "<" to become "&lt;". It
  206. // escapes only five such characters: <, >, &, ' and ".
  207. // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
  208. // always true.
  209. func EscapeString(s string) string {
  210. if strings.IndexAny(s, escapedChars) == -1 {
  211. return s
  212. }
  213. var buf bytes.Buffer
  214. escape(&buf, s)
  215. return buf.String()
  216. }
  217. // UnescapeString unescapes entities like "&lt;" to become "<". It unescapes a
  218. // larger range of entities than EscapeString escapes. For example, "&aacute;"
  219. // unescapes to "á", as does "&#225;" and "&xE1;".
  220. // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
  221. // always true.
  222. func UnescapeString(s string) string {
  223. for _, c := range s {
  224. if c == '&' {
  225. return string(unescape([]byte(s)))
  226. }
  227. }
  228. return s
  229. }