forked from aymerick/raymond
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Borrow html.EscapeString() and changes its behavior to output " …
…and &aquot; in order to pass mustache tests
- Loading branch information
Showing
5 changed files
with
79 additions
and
16 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,64 @@ | ||
package raymond | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
// | ||
// That whole file is borrowed from https://github.com/golang/go/tree/master/src/html/escape.go | ||
// | ||
// With changes: | ||
// ' => ' | ||
// " => " | ||
// | ||
// To stay in sync with JS implementation, and make mustache tests pass. | ||
// | ||
|
||
type writer interface { | ||
WriteString(string) (int, error) | ||
} | ||
|
||
const escapedChars = `&'<>"` | ||
|
||
func escape(w writer, s string) error { | ||
i := strings.IndexAny(s, escapedChars) | ||
for i != -1 { | ||
if _, err := w.WriteString(s[:i]); err != nil { | ||
return err | ||
} | ||
var esc string | ||
switch s[i] { | ||
case '&': | ||
esc = "&" | ||
case '\'': | ||
esc = "'" | ||
case '<': | ||
esc = "<" | ||
case '>': | ||
esc = ">" | ||
case '"': | ||
esc = """ | ||
default: | ||
panic("unrecognized escape character") | ||
} | ||
s = s[i+1:] | ||
if _, err := w.WriteString(esc); err != nil { | ||
return err | ||
} | ||
i = strings.IndexAny(s, escapedChars) | ||
} | ||
_, err := w.WriteString(s) | ||
return err | ||
} | ||
|
||
// EscapeString escapes special characters like "<" to become "<". It | ||
// escapes only five such characters: <, >, &, ' and ". | ||
func EscapeString(s string) string { | ||
if strings.IndexAny(s, escapedChars) == -1 { | ||
return s | ||
} | ||
var buf bytes.Buffer | ||
escape(&buf, s) | ||
return buf.String() | ||
} |
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
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