all repos — grayfriday @ f90a576a05b8a97dd6e2936e613eadb48c57112a

blackfriday fork with a few changes

Untangle some mess with attribute escaping

1. Remove unused preserveEntities parameters
2. Move attrEscape() implementation inside esc()
3. Delegate most of esc() work to escCode()
Vytautas Ĺ altenis vytas@rtfb.lt
Sat, 02 Jul 2016 10:45:06 +0300
commit

f90a576a05b8a97dd6e2936e613eadb48c57112a

parent

ab20da6e278cf0217d66c11bbcb2e79c5815d3fb

2 files changed, 14 insertions(+), 20 deletions(-)

jump to
M html.gohtml.go

@@ -145,13 +145,6 @@ w: writer,

} } -func attrEscape(src []byte) []byte { - unesc := []byte(html.UnescapeString(string(src))) - esc1 := []byte(html.EscapeString(string(unesc))) - esc2 := bytes.Replace(esc1, []byte("""), []byte("""), -1) - return bytes.Replace(esc2, []byte("'"), []byte{'\''}, -1) -} - func isHtmlTag(tag []byte, tagname string) bool { found, _ := findHtmlTagPos(tag, tagname) return found

@@ -384,11 +377,12 @@ return ""

} } -func esc(text []byte, preserveEntities bool) []byte { - return attrEscape(text) +func esc(text []byte) []byte { + unesc := []byte(html.UnescapeString(string(text))) + return escCode(unesc) } -func escCode(text []byte, preserveEntities bool) []byte { +func escCode(text []byte) []byte { e1 := []byte(html.EscapeString(string(text))) e2 := bytes.Replace(e1, []byte("""), []byte("""), -1) return bytes.Replace(e2, []byte("'"), []byte{'\''}, -1)

@@ -466,7 +460,7 @@ } else {

if entering { dest = r.addAbsPrefix(dest) //if (!(options.safe && potentiallyUnsafe(node.destination))) { - attrs = append(attrs, fmt.Sprintf("href=%q", esc(dest, true))) + attrs = append(attrs, fmt.Sprintf("href=%q", esc(dest))) //} if node.NoteID != 0 { r.out(w, footnoteRef(r.FootnoteAnchorPrefix, node))

@@ -474,7 +468,7 @@ break

} attrs = appendLinkAttrs(attrs, r.Flags, dest) if len(node.LinkData.Title) > 0 { - attrs = append(attrs, fmt.Sprintf("title=%q", esc(node.LinkData.Title, true))) + attrs = append(attrs, fmt.Sprintf("title=%q", esc(node.LinkData.Title))) } r.out(w, tag("a", attrs, false)) } else {

@@ -495,7 +489,7 @@ if r.disableTags == 0 {

//if options.safe && potentiallyUnsafe(dest) { //out(w, `<img src="" alt="`) //} else { - r.out(w, []byte(fmt.Sprintf(`<img src="%s" alt="`, esc(dest, true)))) + r.out(w, []byte(fmt.Sprintf(`<img src="%s" alt="`, esc(dest)))) //} } r.disableTags++

@@ -504,14 +498,14 @@ r.disableTags--

if r.disableTags == 0 { if node.LinkData.Title != nil { r.out(w, []byte(`" title="`)) - r.out(w, esc(node.LinkData.Title, true)) + r.out(w, esc(node.LinkData.Title)) } r.out(w, []byte(`" />`)) } } case Code: r.out(w, tag("code", nil, false)) - r.out(w, escCode(node.Literal, false)) + r.out(w, escCode(node.Literal)) r.out(w, tag("/code", nil, false)) case Document: break

@@ -650,7 +644,7 @@ attrs = appendLanguageAttr(attrs, node.Info)

r.cr(w) r.out(w, tag("pre", nil, false)) r.out(w, tag("code", attrs, false)) - r.out(w, escCode(node.Literal, false)) + r.out(w, escCode(node.Literal)) r.out(w, tag("/code", nil, false)) r.out(w, tag("/pre", nil, false)) if node.Parent.Type != Item {

@@ -735,7 +729,7 @@ w.WriteString(" <title>")

if r.Extensions&Smartypants != 0 { w.Write(sr.Process([]byte(r.Title))) } else { - w.Write(esc([]byte(r.Title), false)) + w.Write(esc([]byte(r.Title))) } w.WriteString("</title>\n") w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")

@@ -748,7 +742,7 @@ w.WriteString(ending)

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

@@ -774,7 +768,7 @@ if node.Type == Text {

if r.Extensions&Smartypants != 0 { node.Literal = sr.Process(node.Literal) } else { - node.Literal = esc(node.Literal, false) + node.Literal = esc(node.Literal) } } return GoToNext
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 = attrEscape(text) + text = esc(text) mark := 0 for i := 0; i < len(text); i++ { if action := sr.callbacks[text[i]]; action != nil {