1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-12-02 09:27:49 +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

26
esc.go
View File

@ -17,10 +17,16 @@ func escapeHTML(w io.Writer, s []byte) {
for end < len(s) { for end < len(s) {
escSeq := htmlEscaper[s[end]] escSeq := htmlEscaper[s[end]]
if escSeq != nil { if escSeq != nil {
isEntity, entityEnd := nodeIsEntity(s, end)
if isEntity {
w.Write(s[start : entityEnd+1])
start = entityEnd + 1
} else {
w.Write(s[start:end]) w.Write(s[start:end])
w.Write(escSeq) w.Write(escSeq)
start = end + 1 start = end + 1
} }
}
end++ end++
} }
if start < len(s) && end <= len(s) { if start < len(s) && end <= len(s) {
@ -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) { func escLink(w io.Writer, text []byte) {
unesc := html.UnescapeString(string(text)) unesc := html.UnescapeString(string(text))
escapeHTML(w, []byte(unesc)) escapeHTML(w, []byte(unesc))