From f90a576a05b8a97dd6e2936e613eadb48c57112a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Sat, 2 Jul 2016 10:45:06 +0300 Subject: [PATCH] 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() --- html.go | 32 +++++++++++++------------------- smartypants.go | 2 +- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/html.go b/html.go index 0117c35..0c1362d 100644 --- a/html.go +++ b/html.go @@ -145,13 +145,6 @@ func NewHTMLRenderer(params HTMLRendererParameters) Renderer { } } -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 @@ func cellAlignment(align CellAlignFlags) string { } } -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 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt 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 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt } 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 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt //if options.safe && potentiallyUnsafe(dest) { //out(w, ``)
 				//} else {
-				r.out(w, []byte(fmt.Sprintf(`<img src=`)) } } 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 @@ func (r *HTMLRenderer) RenderNode(w io.Writer, node *Node, entering bool) WalkSt 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 @@ func (r *HTMLRenderer) writeDocumentHeader(w *bytes.Buffer, sr *SPRenderer) { 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("\n") w.WriteString(" \n") if r.CSS != "" { w.WriteString(" \n") @@ -774,7 +768,7 @@ func (r *HTMLRenderer) Render(ast *Node) []byte { 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 diff --git a/smartypants.go b/smartypants.go index 9f1dcd2..8f50fb5 100644 --- a/smartypants.go +++ b/smartypants.go @@ -401,7 +401,7 @@ func NewSmartypantsRenderer(flags Extensions) *SPRenderer { 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 {