-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Roll our own implementation of HTML escaper
- Loading branch information
Showing
4 changed files
with
130 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package blackfriday | ||
|
||
import ( | ||
"html" | ||
"io" | ||
) | ||
|
||
type escMap struct { | ||
char byte | ||
seq []byte | ||
} | ||
|
||
var htmlEscaper = []escMap{ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
{'&', []byte("&")}, | ||
{'<', []byte("<")}, | ||
{'>', []byte(">")}, | ||
{'"', []byte(""")}, | ||
} | ||
|
||
func escapeHTML(w io.Writer, s []byte) { | ||
var start, end int | ||
var sEnd byte | ||
for end < len(s) { | ||
sEnd = s[end] | ||
if sEnd == '&' || sEnd == '<' || sEnd == '>' || sEnd == '"' { | ||
This comment has been minimized.
Sorry, something went wrong.
Ambrevar
|
||
for i := 0; i < len(htmlEscaper); i++ { | ||
if sEnd == htmlEscaper[i].char { | ||
w.Write(s[start:end]) | ||
w.Write(htmlEscaper[i].seq) | ||
start = end + 1 | ||
break | ||
} | ||
} | ||
} | ||
end++ | ||
} | ||
if start < len(s) && end <= len(s) { | ||
w.Write(s[start:end]) | ||
} | ||
} | ||
|
||
func escLink(w io.Writer, text []byte) { | ||
unesc := html.UnescapeString(string(text)) | ||
escapeHTML(w, []byte(unesc)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package blackfriday | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestEsc(t *testing.T) { | ||
tests := []string{ | ||
"abc", "abc", | ||
"a&c", "a&c", | ||
"<", "<", | ||
"[]:<", "[]:<", | ||
"Hello <!--", "Hello <!--", | ||
} | ||
for i := 0; i < len(tests); i += 2 { | ||
var b bytes.Buffer | ||
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()) | ||
} | ||
} | ||
} | ||
|
||
/* | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
func BenchmarkEscapeHTML(b *testing.B) { | ||
tests := [][]byte{ | ||
[]byte(""), | ||
[]byte("AT&T has an ampersand in their name."), | ||
[]byte("AT&T is another way to write it."), | ||
[]byte("This & that."), | ||
[]byte("4 < 5."), | ||
[]byte("6 > 5."), | ||
[]byte("Here's a [link] [1] with an ampersand in the URL."), | ||
[]byte("Here's a link with an amersand in the link text: [AT&T] [2]."), | ||
[]byte("Here's an inline [link](/script?foo=1&bar=2)."), | ||
[]byte("Here's an inline [link](</script?foo=1&bar=2>)."), | ||
[]byte("[1]: http://example.com/?foo=1&bar=2"), | ||
[]byte("[2]: http://att.com/ \"AT&T\""), | ||
} | ||
var buff bytes.Buffer | ||
for n := 0; n < b.N; n++ { | ||
for _, t := range tests { | ||
escapeHTML(&buff, t) | ||
buff.Reset() | ||
} | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Instead of looping over 4 characters, index by them: