mirror of
https://github.com/danog/blackfriday.git
synced 2024-11-26 20:14:43 +01:00
rewrite of attrEscape: cleaner and faster
This commit is contained in:
parent
a74678bf51
commit
44db721708
38
html.go
38
html.go
@ -121,31 +121,39 @@ func HtmlTocRenderer(flags int) *Renderer {
|
||||
}
|
||||
|
||||
func attrEscape(out *bytes.Buffer, src []byte) {
|
||||
for i := 0; i < len(src); i++ {
|
||||
// directly copy normal characters
|
||||
org := i
|
||||
for i < len(src) && src[i] != '<' && src[i] != '>' && src[i] != '&' && src[i] != '"' {
|
||||
i++
|
||||
}
|
||||
if i > org {
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
|
||||
// escape a character
|
||||
if i >= len(src) {
|
||||
break
|
||||
}
|
||||
switch src[i] {
|
||||
org := 0
|
||||
for i, ch := range src {
|
||||
switch ch {
|
||||
case '<':
|
||||
if i > org {
|
||||
// copy all the normal characters since the last escape
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
org = i + 1
|
||||
out.WriteString("<")
|
||||
case '>':
|
||||
if i > org {
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
org = i + 1
|
||||
out.WriteString(">")
|
||||
case '&':
|
||||
if i > org {
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
org = i + 1
|
||||
out.WriteString("&")
|
||||
case '"':
|
||||
if i > org {
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
org = i + 1
|
||||
out.WriteString(""")
|
||||
}
|
||||
}
|
||||
if org < len(src) {
|
||||
out.Write(src[org:])
|
||||
}
|
||||
}
|
||||
|
||||
func htmlHeader(out *bytes.Buffer, text []byte, level int, opaque interface{}) {
|
||||
|
Loading…
Reference in New Issue
Block a user