all repos — grayfriday @ c5c549b0639be8deed666fb4ffd3ee8f9d2c2225

blackfriday fork with a few changes

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
commit

c5c549b0639be8deed666fb4ffd3ee8f9d2c2225

parent

670777b536d38a1aef1d517f6330a77d52ceb02e

1 files changed, 29 insertions(+), 3 deletions(-)

jump to
M esc.goesc.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) {