escape.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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
  5. import (
  6. "bytes"
  7. "strings"
  8. "unicode/utf8"
  9. )
  10. // These replacements permit compatibility with old numeric entities that
  11. // assumed Windows-1252 encoding.
  12. // https://html.spec.whatwg.org/multipage/syntax.html#consume-a-character-reference
  13. var replacementTable = [...]rune{
  14. '\u20AC', // First entry is what 0x80 should be replaced with.
  15. '\u0081',
  16. '\u201A',
  17. '\u0192',
  18. '\u201E',
  19. '\u2026',
  20. '\u2020',
  21. '\u2021',
  22. '\u02C6',
  23. '\u2030',
  24. '\u0160',
  25. '\u2039',
  26. '\u0152',
  27. '\u008D',
  28. '\u017D',
  29. '\u008F',
  30. '\u0090',
  31. '\u2018',
  32. '\u2019',
  33. '\u201C',
  34. '\u201D',
  35. '\u2022',
  36. '\u2013',
  37. '\u2014',
  38. '\u02DC',
  39. '\u2122',
  40. '\u0161',
  41. '\u203A',
  42. '\u0153',
  43. '\u009D',
  44. '\u017E',
  45. '\u0178', // Last entry is 0x9F.
  46. // 0x00->'\uFFFD' is handled programmatically.
  47. // 0x0D->'\u000D' is a no-op.
  48. }
  49. // unescapeEntity reads an entity like "<" from b[src:] and writes the
  50. // corresponding "<" to b[dst:], returning the incremented dst and src cursors.
  51. // Precondition: b[src] == '&' && dst <= src.
  52. // attribute should be true if parsing an attribute value.
  53. func unescapeEntity(b []byte, dst, src int, attribute bool) (dst1, src1 int) {
  54. // https://html.spec.whatwg.org/multipage/syntax.html#consume-a-character-reference
  55. // i starts at 1 because we already know that s[0] == '&'.
  56. i, s := 1, b[src:]
  57. if len(s) <= 1 {
  58. b[dst] = b[src]
  59. return dst + 1, src + 1
  60. }
  61. if s[i] == '#' {
  62. if len(s) <= 3 { // We need to have at least "&#.".
  63. b[dst] = b[src]
  64. return dst + 1, src + 1
  65. }
  66. i++
  67. c := s[i]
  68. hex := false
  69. if c == 'x' || c == 'X' {
  70. hex = true
  71. i++
  72. }
  73. x := '\x00'
  74. for i < len(s) {
  75. c = s[i]
  76. i++
  77. if hex {
  78. if '0' <= c && c <= '9' {
  79. x = 16*x + rune(c) - '0'
  80. continue
  81. } else if 'a' <= c && c <= 'f' {
  82. x = 16*x + rune(c) - 'a' + 10
  83. continue
  84. } else if 'A' <= c && c <= 'F' {
  85. x = 16*x + rune(c) - 'A' + 10
  86. continue
  87. }
  88. } else if '0' <= c && c <= '9' {
  89. x = 10*x + rune(c) - '0'
  90. continue
  91. }
  92. if c != ';' {
  93. i--
  94. }
  95. break
  96. }
  97. if i <= 3 { // No characters matched.
  98. b[dst] = b[src]
  99. return dst + 1, src + 1
  100. }
  101. if 0x80 <= x && x <= 0x9F {
  102. // Replace characters from Windows-1252 with UTF-8 equivalents.
  103. x = replacementTable[x-0x80]
  104. } else if x == 0 || (0xD800 <= x && x <= 0xDFFF) || x > 0x10FFFF {
  105. // Replace invalid characters with the replacement character.
  106. x = '\uFFFD'
  107. }
  108. return dst + utf8.EncodeRune(b[dst:], x), src + i
  109. }
  110. // Consume the maximum number of characters possible, with the
  111. // consumed characters matching one of the named references.
  112. for i < len(s) {
  113. c := s[i]
  114. i++
  115. // Lower-cased characters are more common in entities, so we check for them first.
  116. if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
  117. continue
  118. }
  119. if c != ';' {
  120. i--
  121. }
  122. break
  123. }
  124. entityName := string(s[1:i])
  125. if entityName == "" {
  126. // No-op.
  127. } else if attribute && entityName[len(entityName)-1] != ';' && len(s) > i && s[i] == '=' {
  128. // No-op.
  129. } else if x := entity[entityName]; x != 0 {
  130. return dst + utf8.EncodeRune(b[dst:], x), src + i
  131. } else if x := entity2[entityName]; x[0] != 0 {
  132. dst1 := dst + utf8.EncodeRune(b[dst:], x[0])
  133. return dst1 + utf8.EncodeRune(b[dst1:], x[1]), src + i
  134. } else if !attribute {
  135. maxLen := len(entityName) - 1
  136. if maxLen > longestEntityWithoutSemicolon {
  137. maxLen = longestEntityWithoutSemicolon
  138. }
  139. for j := maxLen; j > 1; j-- {
  140. if x := entity[entityName[:j]]; x != 0 {
  141. return dst + utf8.EncodeRune(b[dst:], x), src + j + 1
  142. }
  143. }
  144. }
  145. dst1, src1 = dst+i, src+i
  146. copy(b[dst:dst1], b[src:src1])
  147. return dst1, src1
  148. }
  149. // unescape unescapes b's entities in-place, so that "a&lt;b" becomes "a<b".
  150. // attribute should be true if parsing an attribute value.
  151. func unescape(b []byte, attribute bool) []byte {
  152. for i, c := range b {
  153. if c == '&' {
  154. dst, src := unescapeEntity(b, i, i, attribute)
  155. for src < len(b) {
  156. c := b[src]
  157. if c == '&' {
  158. dst, src = unescapeEntity(b, dst, src, attribute)
  159. } else {
  160. b[dst] = c
  161. dst, src = dst+1, src+1
  162. }
  163. }
  164. return b[0:dst]
  165. }
  166. }
  167. return b
  168. }
  169. // lower lower-cases the A-Z bytes in b in-place, so that "aBc" becomes "abc".
  170. func lower(b []byte) []byte {
  171. for i, c := range b {
  172. if 'A' <= c && c <= 'Z' {
  173. b[i] = c + 'a' - 'A'
  174. }
  175. }
  176. return b
  177. }
  178. // escapeComment is like func escape but escapes its input bytes less often.
  179. // Per https://github.com/golang/go/issues/58246 some HTML comments are (1)
  180. // meaningful and (2) contain angle brackets that we'd like to avoid escaping
  181. // unless we have to.
  182. //
  183. // "We have to" includes the '&' byte, since that introduces other escapes.
  184. //
  185. // It also includes those bytes (not including EOF) that would otherwise end
  186. // the comment. Per the summary table at the bottom of comment_test.go, this is
  187. // the '>' byte that, per above, we'd like to avoid escaping unless we have to.
  188. //
  189. // Studying the summary table (and T actions in its '>' column) closely, we
  190. // only need to escape in states 43, 44, 49, 51 and 52. State 43 is at the
  191. // start of the comment data. State 52 is after a '!'. The other three states
  192. // are after a '-'.
  193. //
  194. // Our algorithm is thus to escape every '&' and to escape '>' if and only if:
  195. // - The '>' is after a '!' or '-' (in the unescaped data) or
  196. // - The '>' is at the start of the comment data (after the opening "<!--").
  197. func escapeComment(w writer, s string) error {
  198. // When modifying this function, consider manually increasing the
  199. // maxSuffixLen constant in func TestComments, from 6 to e.g. 9 or more.
  200. // That increase should only be temporary, not committed, as it
  201. // exponentially affects the test running time.
  202. if len(s) == 0 {
  203. return nil
  204. }
  205. // Loop:
  206. // - Grow j such that s[i:j] does not need escaping.
  207. // - If s[j] does need escaping, output s[i:j] and an escaped s[j],
  208. // resetting i and j to point past that s[j] byte.
  209. i := 0
  210. for j := 0; j < len(s); j++ {
  211. escaped := ""
  212. switch s[j] {
  213. case '&':
  214. escaped = "&amp;"
  215. case '>':
  216. if j > 0 {
  217. if prev := s[j-1]; (prev != '!') && (prev != '-') {
  218. continue
  219. }
  220. }
  221. escaped = "&gt;"
  222. default:
  223. continue
  224. }
  225. if i < j {
  226. if _, err := w.WriteString(s[i:j]); err != nil {
  227. return err
  228. }
  229. }
  230. if _, err := w.WriteString(escaped); err != nil {
  231. return err
  232. }
  233. i = j + 1
  234. }
  235. if i < len(s) {
  236. if _, err := w.WriteString(s[i:]); err != nil {
  237. return err
  238. }
  239. }
  240. return nil
  241. }
  242. // escapeCommentString is to EscapeString as escapeComment is to escape.
  243. func escapeCommentString(s string) string {
  244. if strings.IndexAny(s, "&>") == -1 {
  245. return s
  246. }
  247. var buf bytes.Buffer
  248. escapeComment(&buf, s)
  249. return buf.String()
  250. }
  251. const escapedChars = "&'<>\"\r"
  252. func escape(w writer, s string) error {
  253. i := strings.IndexAny(s, escapedChars)
  254. for i != -1 {
  255. if _, err := w.WriteString(s[:i]); err != nil {
  256. return err
  257. }
  258. var esc string
  259. switch s[i] {
  260. case '&':
  261. esc = "&amp;"
  262. case '\'':
  263. // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
  264. esc = "&#39;"
  265. case '<':
  266. esc = "&lt;"
  267. case '>':
  268. esc = "&gt;"
  269. case '"':
  270. // "&#34;" is shorter than "&quot;".
  271. esc = "&#34;"
  272. case '\r':
  273. esc = "&#13;"
  274. default:
  275. panic("unrecognized escape character")
  276. }
  277. s = s[i+1:]
  278. if _, err := w.WriteString(esc); err != nil {
  279. return err
  280. }
  281. i = strings.IndexAny(s, escapedChars)
  282. }
  283. _, err := w.WriteString(s)
  284. return err
  285. }
  286. // EscapeString escapes special characters like "<" to become "&lt;". It
  287. // escapes only five such characters: <, >, &, ' and ".
  288. // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
  289. // always true.
  290. func EscapeString(s string) string {
  291. if strings.IndexAny(s, escapedChars) == -1 {
  292. return s
  293. }
  294. var buf bytes.Buffer
  295. escape(&buf, s)
  296. return buf.String()
  297. }
  298. // UnescapeString unescapes entities like "&lt;" to become "<". It unescapes a
  299. // larger range of entities than EscapeString escapes. For example, "&aacute;"
  300. // unescapes to "á", as does "&#225;" and "&xE1;".
  301. // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
  302. // always true.
  303. func UnescapeString(s string) string {
  304. for _, c := range s {
  305. if c == '&' {
  306. return string(unescape([]byte(s), false))
  307. }
  308. }
  309. return s
  310. }