diff options
Diffstat (limited to 'misc.go')
-rw-r--r-- | misc.go | 21 |
1 files changed, 16 insertions, 5 deletions
@@ -67,7 +67,13 @@ const ( BackslashEscapeUnicode ) -func EscapeJSSafe(c rune, _ BackslashEscapeMode) BackslashEscapeMode { +type BackslashEscaper = func(rune, BackslashEscapeMode) BackslashEscapeMode + +func EscapePreserve(_ rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { + return wasEscaped +} + +func EscapeJSSafe(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { // JSON is notionally a JS subset, but that's not actually // true. // @@ -76,7 +82,7 @@ func EscapeJSSafe(c rune, _ BackslashEscapeMode) BackslashEscapeMode { case '\u2028', '\u2029': return BackslashEscapeUnicode default: - return BackslashEscapeNone + return wasEscaped } } @@ -98,8 +104,13 @@ func EscapeDefault(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { } } -func EscapePreserve(_ rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { - return wasEscaped +func EscapeDefaultNonHTMLSafe(c rune, wasEscaped BackslashEscapeMode) BackslashEscapeMode { + switch c { + case '\b', '\f', utf8.RuneError: + return BackslashEscapeUnicode + default: + return EscapeJSSafe(c, wasEscaped) + } } func writeStringUnicodeEscape(w io.Writer, c rune) (int, error) { @@ -134,7 +145,7 @@ func writeStringShortEscape(w io.Writer, c rune) (int, error) { buf := [2]byte{'\\', b} return w.Write(buf[:]) } -func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escaper func(rune, BackslashEscapeMode) BackslashEscapeMode) (int, error) { +func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escaper BackslashEscaper) (int, error) { if escaper == nil { escaper = EscapeDefault } |