1
0
mirror of https://github.com/danog/blackfriday.git synced 2025-01-23 05:41:27 +01:00

Unduplicate attrEscape funcs

This commit is contained in:
Vytautas Šaltenis 2016-04-11 11:50:33 +03:00
parent e95d23065a
commit 76d8c71d70
2 changed files with 4 additions and 51 deletions

53
html.go
View File

@ -145,60 +145,13 @@ func NewHTMLRenderer(params HTMLRendererParameters) Renderer {
} }
} }
// Using if statements is a bit faster than a switch statement. As the compiler func attrEscape(src []byte) []byte {
// improves, this should be unnecessary this is only worthwhile because
// attrEscape is the single largest CPU user in normal use.
// Also tried using map, but that gave a ~3x slowdown.
func escapeSingleChar(char byte) (string, bool) {
if char == '"' {
return """, true
}
if char == '&' {
return "&", true
}
if char == '<' {
return "&lt;", true
}
if char == '>' {
return "&gt;", true
}
return "", false
}
func (r *HTMLRenderer) attrEscape(src []byte) {
org := 0
for i, ch := range src {
if entity, ok := escapeSingleChar(ch); ok {
if i > org {
// copy all the normal characters since the last escape
r.w.Write(src[org:i])
}
org = i + 1
r.w.WriteString(entity)
}
}
if org < len(src) {
r.w.Write(src[org:])
}
}
func attrEscape2(src []byte) []byte {
unesc := []byte(html.UnescapeString(string(src))) unesc := []byte(html.UnescapeString(string(src)))
esc1 := []byte(html.EscapeString(string(unesc))) esc1 := []byte(html.EscapeString(string(unesc)))
esc2 := bytes.Replace(esc1, []byte("&#34;"), []byte("&quot;"), -1) esc2 := bytes.Replace(esc1, []byte("&#34;"), []byte("&quot;"), -1)
return bytes.Replace(esc2, []byte("&#39;"), []byte{'\''}, -1) return bytes.Replace(esc2, []byte("&#39;"), []byte{'\''}, -1)
} }
func (r *HTMLRenderer) entityEscapeWithSkip(src []byte, skipRanges [][]int) {
end := 0
for _, rang := range skipRanges {
r.attrEscape(src[end:rang[0]])
r.w.Write(src[rang[0]:rang[1]])
end = rang[1]
}
r.attrEscape(src[end:])
}
func isHtmlTag(tag []byte, tagname string) bool { func isHtmlTag(tag []byte, tagname string) bool {
found, _ := findHtmlTagPos(tag, tagname) found, _ := findHtmlTagPos(tag, tagname)
return found return found
@ -448,7 +401,7 @@ func cellAlignment(align CellAlignFlags) string {
} }
func esc(text []byte, preserveEntities bool) []byte { func esc(text []byte, preserveEntities bool) []byte {
return attrEscape2(text) return attrEscape(text)
} }
func escCode(text []byte, preserveEntities bool) []byte { func escCode(text []byte, preserveEntities bool) []byte {
@ -811,7 +764,7 @@ func (r *HTMLRenderer) writeDocumentHeader(w *bytes.Buffer, sr *SPRenderer) {
w.WriteString(">\n") w.WriteString(">\n")
if r.CSS != "" { if r.CSS != "" {
w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
r.attrEscape([]byte(r.CSS)) attrEscape([]byte(r.CSS))
w.WriteString("\"") w.WriteString("\"")
w.WriteString(ending) w.WriteString(ending)
w.WriteString(">\n") w.WriteString(">\n")

View File

@ -401,7 +401,7 @@ func NewSmartypantsRenderer(flags Extensions) *SPRenderer {
func (sr *SPRenderer) Process(text []byte) []byte { func (sr *SPRenderer) Process(text []byte) []byte {
var buff bytes.Buffer var buff bytes.Buffer
// first do normal entity escaping // first do normal entity escaping
text = attrEscape2(text) text = attrEscape(text)
mark := 0 mark := 0
for i := 0; i < len(text); i++ { for i := 0; i < len(text); i++ {
if action := sr.callbacks[text[i]]; action != nil { if action := sr.callbacks[text[i]]; action != nil {