Fix html renderer escaping valid entities This should fix #403
Faruq Rasid ruqqq@users.noreply.github.com
Thu, 26 Jul 2018 11:48:47 +0800
1 files changed,
29 insertions(+),
3 deletions(-)
jump to
M
esc.go
→
esc.go
@@ -17,15 +17,41 @@ var start, end int
for end < len(s) { escSeq := htmlEscaper[s[end]] if escSeq != nil { - w.Write(s[start:end]) - w.Write(escSeq) - start = end + 1 + isEntity, entityEnd := nodeIsEntity(s, end) + if isEntity { + w.Write(s[start : entityEnd+1]) + start = entityEnd + 1 + } else { + w.Write(s[start:end]) + w.Write(escSeq) + start = end + 1 + } } end++ } if start < len(s) && end <= len(s) { w.Write(s[start:end]) } +} + +func nodeIsEntity(s []byte, end int) (isEntity bool, endEntityPos int) { + isEntity = false + endEntityPos = end + 1 + + if s[end] == '&' { + for endEntityPos < len(s) { + if s[endEntityPos] == ';' { + isEntity = true + break + } + if !isalnum(s[endEntityPos]) && s[endEntityPos] != '&' && s[endEntityPos] != '#' { + break + } + endEntityPos++ + } + } + + return isEntity, endEntityPos } func escLink(w io.Writer, text []byte) {