all repos — grayfriday @ 76d8c71d704b5109ae86cc2d8d2d5d4714a70917

blackfriday fork with a few changes

Unduplicate attrEscape funcs
Vytautas Ĺ altenis vytas@rtfb.lt
Mon, 11 Apr 2016 11:50:33 +0300
commit

76d8c71d704b5109ae86cc2d8d2d5d4714a70917

parent

e95d23065aaafd2c383c35124cbb983609606011

2 files changed, 4 insertions(+), 51 deletions(-)

jump to
M html.gohtml.go

@@ -145,58 +145,11 @@ w: writer,

} } -// Using if statements is a bit faster than a switch statement. As the compiler -// 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 "&quot;", true - } - if char == '&' { - return "&amp;", 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 { +func attrEscape(src []byte) []byte { unesc := []byte(html.UnescapeString(string(src))) esc1 := []byte(html.EscapeString(string(unesc))) esc2 := bytes.Replace(esc1, []byte("&#34;"), []byte("&quot;"), -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 {

@@ -448,7 +401,7 @@ }

} func esc(text []byte, preserveEntities bool) []byte { - return attrEscape2(text) + return attrEscape(text) } func escCode(text []byte, preserveEntities bool) []byte {

@@ -811,7 +764,7 @@ w.WriteString(ending)

w.WriteString(">\n") if r.CSS != "" { w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") - r.attrEscape([]byte(r.CSS)) + attrEscape([]byte(r.CSS)) w.WriteString("\"") w.WriteString(ending) w.WriteString(">\n")
M smartypants.gosmartypants.go

@@ -401,7 +401,7 @@

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