Merge pull request #92 from erikdubbelboer/noescapehtml

Add NoEscapeHTML to jwriter.Writer
This commit is contained in:
Victor Starodub
2017-01-16 17:37:52 +04:00
committed by GitHub
+10 -5
View File
@@ -23,8 +23,9 @@ const (
type Writer struct {
Flags Flags
Error error
Buffer buffer.Buffer
Error error
Buffer buffer.Buffer
NoEscapeHTML bool
}
// Size returns the size of the data that was written out.
@@ -225,10 +226,14 @@ func (w *Writer) Bool(v bool) {
const chars = "0123456789abcdef"
func isNotEscapedSingleChar(c byte) bool {
func isNotEscapedSingleChar(c byte, escapeHTML bool) bool {
// Note: might make sense to use a table if there are more chars to escape. With 4 chars
// it benchmarks the same.
return c != '<' && c != '\\' && c != '"' && c != '>' && c >= 0x20 && c < utf8.RuneSelf
if escapeHTML {
return c != '<' && c != '>' && c != '&' && c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
} else {
return c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
}
}
func (w *Writer) String(s string) {
@@ -242,7 +247,7 @@ func (w *Writer) String(s string) {
for i := 0; i < len(s); {
c := s[i]
if isNotEscapedSingleChar(c) {
if isNotEscapedSingleChar(c, !w.NoEscapeHTML) {
// single-width character, no escaping is required
i++
continue