From 0b8b77db400c3a65c6f7f58bc06cdd8f9d857a2b Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Sat, 27 Jan 2018 00:52:23 -0800 Subject: [PATCH] export EscapeHTML --- esc.go | 16 +++++++++------- esc_test.go | 4 ++-- html_renderer.go | 18 +++++++++--------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/esc.go b/esc.go index 8accfc5..2607ccc 100644 --- a/esc.go +++ b/esc.go @@ -12,23 +12,25 @@ var htmlEscaper = [256][]byte{ '"': []byte("""), } -func escapeHTML(w io.Writer, s []byte) { +// EscapeHTML writes html-escaped d to w. It escapes &, <, > and " characters. +func EscapeHTML(w io.Writer, d []byte) { var start, end int - for end < len(s) { - escSeq := htmlEscaper[s[end]] + n := len(d) + for end < n { + escSeq := htmlEscaper[d[end]] if escSeq != nil { - w.Write(s[start:end]) + w.Write(d[start:end]) w.Write(escSeq) start = end + 1 } end++ } - if start < len(s) && end <= len(s) { - w.Write(s[start:end]) + if start < n && end <= n { + w.Write(d[start:end]) } } func escLink(w io.Writer, text []byte) { unesc := html.UnescapeString(string(text)) - escapeHTML(w, []byte(unesc)) + EscapeHTML(w, []byte(unesc)) } diff --git a/esc_test.go b/esc_test.go index b301519..8a91bd5 100644 --- a/esc_test.go +++ b/esc_test.go @@ -16,7 +16,7 @@ func TestEsc(t *testing.T) { } for i := 0; i < len(tests); i += 2 { var b bytes.Buffer - escapeHTML(&b, []byte(tests[i])) + EscapeHTML(&b, []byte(tests[i])) if !bytes.Equal(b.Bytes(), []byte(tests[i+1])) { t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", tests[i], tests[i+1], b.String()) @@ -42,7 +42,7 @@ func BenchmarkEscapeHTML(b *testing.B) { var buf bytes.Buffer for n := 0; n < b.N; n++ { for _, t := range tests { - escapeHTML(&buf, t) + EscapeHTML(&buf, t) buf.Reset() } } diff --git a/html_renderer.go b/html_renderer.go index 071811b..c9b5998 100644 --- a/html_renderer.go +++ b/html_renderer.go @@ -418,13 +418,13 @@ func (r *HTMLRenderer) outHRTag(w io.Writer) { func (r *HTMLRenderer) text(w io.Writer, node *Node, nodeData *TextData) { if r.params.Flags&Smartypants != 0 { var tmp bytes.Buffer - escapeHTML(&tmp, node.Literal) + EscapeHTML(&tmp, node.Literal) r.sr.Process(w, tmp.Bytes()) } else { if isLinkData(node.Parent.Data) { escLink(w, node.Literal) } else { - escapeHTML(w, node.Literal) + EscapeHTML(w, node.Literal) } } } @@ -476,7 +476,7 @@ func (r *HTMLRenderer) linkEnter(w io.Writer, node *Node, nodeData *LinkData) { if len(nodeData.Title) > 0 { var titleBuff bytes.Buffer titleBuff.WriteString("title=\"") - escapeHTML(&titleBuff, nodeData.Title) + EscapeHTML(&titleBuff, nodeData.Title) titleBuff.WriteByte('"') attrs = append(attrs, titleBuff.String()) } @@ -523,7 +523,7 @@ func (r *HTMLRenderer) imageExit(w io.Writer, node *Node, nodeData *ImageData) { if r.disableTags == 0 { if nodeData.Title != nil { r.outs(w, `" title="`) - escapeHTML(w, nodeData.Title) + EscapeHTML(w, nodeData.Title) } r.outs(w, `" />`) } @@ -571,7 +571,7 @@ func (r *HTMLRenderer) image(w io.Writer, node *Node, nodeData *ImageData, enter func (r *HTMLRenderer) code(w io.Writer, node *Node, nodeData *CodeData) { r.outs(w, "") - escapeHTML(w, node.Literal) + EscapeHTML(w, node.Literal) r.outs(w, "") } @@ -740,7 +740,7 @@ func (r *HTMLRenderer) codeBlock(w io.Writer, node *Node, nodeData *CodeBlockDat r.cr(w) r.outs(w, "
")
 	r.outTag(w, "")
 	r.outs(w, "
") if !isListItemData(node.Parent.Data) { @@ -898,7 +898,7 @@ func (r *HTMLRenderer) writeDocumentHeader(w io.Writer) { if r.params.Flags&Smartypants != 0 { r.sr.Process(w, []byte(r.params.Title)) } else { - escapeHTML(w, []byte(r.params.Title)) + EscapeHTML(w, []byte(r.params.Title)) } io.WriteString(w, "\n") io.WriteString(w, " \n") if r.params.CSS != "" { io.WriteString(w, " \n") } if r.params.Icon != "" { io.WriteString(w, " \n")