2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// Package html provides functions for escaping and unescaping HTML text.
|
2010-12-03 05:34:57 +01:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2011-12-07 02:11:29 +01:00
|
|
|
"unicode/utf8"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// These replacements permit compatibility with old numeric entities that
|
2011-01-21 19:19:03 +01:00
|
|
|
// assumed Windows-1252 encoding.
|
|
|
|
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference
|
2011-12-02 20:34:41 +01:00
|
|
|
var replacementTable = [...]rune{
|
2011-01-21 19:19:03 +01:00
|
|
|
'\u20AC', // First entry is what 0x80 should be replaced with.
|
|
|
|
'\u0081',
|
|
|
|
'\u201A',
|
|
|
|
'\u0192',
|
|
|
|
'\u201E',
|
|
|
|
'\u2026',
|
|
|
|
'\u2020',
|
|
|
|
'\u2021',
|
|
|
|
'\u02C6',
|
|
|
|
'\u2030',
|
|
|
|
'\u0160',
|
|
|
|
'\u2039',
|
|
|
|
'\u0152',
|
|
|
|
'\u008D',
|
|
|
|
'\u017D',
|
|
|
|
'\u008F',
|
|
|
|
'\u0090',
|
|
|
|
'\u2018',
|
|
|
|
'\u2019',
|
|
|
|
'\u201C',
|
|
|
|
'\u201D',
|
|
|
|
'\u2022',
|
|
|
|
'\u2013',
|
|
|
|
'\u2014',
|
|
|
|
'\u02DC',
|
|
|
|
'\u2122',
|
|
|
|
'\u0161',
|
|
|
|
'\u203A',
|
|
|
|
'\u0153',
|
|
|
|
'\u009D',
|
|
|
|
'\u017E',
|
|
|
|
'\u0178', // Last entry is 0x9F.
|
2012-11-21 08:03:38 +01:00
|
|
|
// 0x00->'\uFFFD' is handled programmatically.
|
2011-01-21 19:19:03 +01:00
|
|
|
// 0x0D->'\u000D' is a no-op.
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// unescapeEntity reads an entity like "<" from b[src:] and writes the
|
|
|
|
// corresponding "<" to b[dst:], returning the incremented dst and src cursors.
|
2011-01-21 19:19:03 +01:00
|
|
|
// Precondition: b[src] == '&' && dst <= src.
|
2016-02-03 22:58:02 +01:00
|
|
|
func unescapeEntity(b []byte, dst, src int) (dst1, src1 int) {
|
|
|
|
const attribute = false
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference
|
|
|
|
|
|
|
|
// i starts at 1 because we already know that s[0] == '&'.
|
|
|
|
i, s := 1, b[src:]
|
2011-01-21 19:19:03 +01:00
|
|
|
|
|
|
|
if len(s) <= 1 {
|
|
|
|
b[dst] = b[src]
|
|
|
|
return dst + 1, src + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[i] == '#' {
|
|
|
|
if len(s) <= 3 { // We need to have at least "&#.".
|
|
|
|
b[dst] = b[src]
|
|
|
|
return dst + 1, src + 1
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
c := s[i]
|
|
|
|
hex := false
|
|
|
|
if c == 'x' || c == 'X' {
|
|
|
|
hex = true
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
x := '\x00'
|
2011-01-21 19:19:03 +01:00
|
|
|
for i < len(s) {
|
|
|
|
c = s[i]
|
|
|
|
i++
|
|
|
|
if hex {
|
|
|
|
if '0' <= c && c <= '9' {
|
2011-12-02 20:34:41 +01:00
|
|
|
x = 16*x + rune(c) - '0'
|
2011-01-21 19:19:03 +01:00
|
|
|
continue
|
|
|
|
} else if 'a' <= c && c <= 'f' {
|
2011-12-02 20:34:41 +01:00
|
|
|
x = 16*x + rune(c) - 'a' + 10
|
2011-01-21 19:19:03 +01:00
|
|
|
continue
|
|
|
|
} else if 'A' <= c && c <= 'F' {
|
2011-12-02 20:34:41 +01:00
|
|
|
x = 16*x + rune(c) - 'A' + 10
|
2011-01-21 19:19:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if '0' <= c && c <= '9' {
|
2011-12-02 20:34:41 +01:00
|
|
|
x = 10*x + rune(c) - '0'
|
2011-01-21 19:19:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c != ';' {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if i <= 3 { // No characters matched.
|
|
|
|
b[dst] = b[src]
|
|
|
|
return dst + 1, src + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if 0x80 <= x && x <= 0x9F {
|
|
|
|
// Replace characters from Windows-1252 with UTF-8 equivalents.
|
|
|
|
x = replacementTable[x-0x80]
|
|
|
|
} else if x == 0 || (0xD800 <= x && x <= 0xDFFF) || x > 0x10FFFF {
|
|
|
|
// Replace invalid characters with the replacement character.
|
|
|
|
x = '\uFFFD'
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst + utf8.EncodeRune(b[dst:], x), src + i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the maximum number of characters possible, with the
|
|
|
|
// consumed characters matching one of the named references.
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
for i < len(s) {
|
|
|
|
c := s[i]
|
|
|
|
i++
|
|
|
|
// Lower-cased characters are more common in entities, so we check for them first.
|
2011-09-16 17:47:21 +02:00
|
|
|
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
|
2010-12-03 05:34:57 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c != ';' {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
entityName := s[1:i]
|
|
|
|
if len(entityName) == 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
// No-op.
|
|
|
|
} else if attribute && entityName[len(entityName)-1] != ';' && len(s) > i && s[i] == '=' {
|
|
|
|
// No-op.
|
2016-02-03 22:58:02 +01:00
|
|
|
} else if x := entity[string(entityName)]; x != 0 {
|
2011-01-21 19:19:03 +01:00
|
|
|
return dst + utf8.EncodeRune(b[dst:], x), src + i
|
2016-02-03 22:58:02 +01:00
|
|
|
} else if x := entity2[string(entityName)]; x[0] != 0 {
|
2011-01-21 19:19:03 +01:00
|
|
|
dst1 := dst + utf8.EncodeRune(b[dst:], x[0])
|
|
|
|
return dst1 + utf8.EncodeRune(b[dst1:], x[1]), src + i
|
2011-09-16 17:47:21 +02:00
|
|
|
} else if !attribute {
|
|
|
|
maxLen := len(entityName) - 1
|
|
|
|
if maxLen > longestEntityWithoutSemicolon {
|
|
|
|
maxLen = longestEntityWithoutSemicolon
|
|
|
|
}
|
|
|
|
for j := maxLen; j > 1; j-- {
|
2016-02-03 22:58:02 +01:00
|
|
|
if x := entity[string(entityName[:j])]; x != 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return dst + utf8.EncodeRune(b[dst:], x), src + j + 1
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
dst1, src1 = dst+i, src+i
|
|
|
|
copy(b[dst:dst1], b[src:src1])
|
|
|
|
return dst1, src1
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
var htmlEscaper = strings.NewReplacer(
|
|
|
|
`&`, "&",
|
|
|
|
`'`, "'", // "'" is shorter than "'" and apos was not in HTML until HTML5.
|
|
|
|
`<`, "<",
|
|
|
|
`>`, ">",
|
|
|
|
`"`, """, // """ is shorter than """.
|
|
|
|
)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// EscapeString escapes special characters like "<" to become "<". It
|
2012-05-04 17:01:11 +02:00
|
|
|
// escapes only five such characters: <, >, &, ' and ".
|
2010-12-03 05:34:57 +01:00
|
|
|
// UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
|
|
|
|
// always true.
|
|
|
|
func EscapeString(s string) string {
|
2015-10-31 01:59:47 +01:00
|
|
|
return htmlEscaper.Replace(s)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnescapeString unescapes entities like "<" to become "<". It unescapes a
|
|
|
|
// larger range of entities than EscapeString escapes. For example, "á"
|
2016-07-22 20:15:38 +02:00
|
|
|
// unescapes to "á", as does "á" and "á".
|
2010-12-03 05:34:57 +01:00
|
|
|
// UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
|
|
|
|
// always true.
|
|
|
|
func UnescapeString(s string) string {
|
2016-02-03 22:58:02 +01:00
|
|
|
i := strings.IndexByte(s, '&')
|
|
|
|
|
|
|
|
if i < 0 {
|
2015-10-31 01:59:47 +01:00
|
|
|
return s
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
|
|
|
|
b := []byte(s)
|
|
|
|
dst, src := unescapeEntity(b, i, i)
|
|
|
|
for len(s[src:]) > 0 {
|
|
|
|
if s[src] == '&' {
|
|
|
|
i = 0
|
|
|
|
} else {
|
|
|
|
i = strings.IndexByte(s[src:], '&')
|
|
|
|
}
|
|
|
|
if i < 0 {
|
|
|
|
dst += copy(b[dst:], s[src:])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
copy(b[dst:], s[src:src+i])
|
|
|
|
}
|
|
|
|
dst, src = unescapeEntity(b, dst+i, src+i)
|
|
|
|
}
|
|
|
|
return string(b[:dst])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|