1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-29 20:19:11 +01:00

Fix html renderer escaping valid entities

This should fix #403
This commit is contained in:
Faruq Rasid 2018-07-26 11:48:47 +08:00 committed by GitHub
parent 670777b536
commit c5c549b063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

32
esc.go
View File

@ -17,9 +17,15 @@ func escapeHTML(w io.Writer, s []byte) {
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++
}
@ -28,6 +34,26 @@ func escapeHTML(w io.Writer, s []byte) {
}
}
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) {
unesc := html.UnescapeString(string(text))
escapeHTML(w, []byte(unesc))