func Example_escape() { const s = `"Fran & Freddie's Diner" <*****@*****.**>` v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<*****@*****.**>`} fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.HTMLEscaper(v...)) fmt.Println(template.JSEscapeString(s)) template.JSEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.JSEscaper(v...)) fmt.Println(template.URLQueryEscaper(v...)) // Output: // "Fran & Freddie's Diner" <[email protected]> // "Fran & Freddie's Diner" <[email protected]> // "Fran & Freddie's Diner"32<[email protected]> // \"Fran & Freddie\'s Diner\" \[email protected]\x3E // \"Fran & Freddie\'s Diner\" \[email protected]\x3E // \"Fran & Freddie\'s Diner\"32\[email protected]\x3E // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E }
func TestHTMLEscape(t *testing.T) { const s = `"Fran & Freddie's Diner" <*****@*****.**>` v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<*****@*****.**>`} fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprint(os.Stdout, "") fmt.Println(template.JSEscapeString(s)) fmt.Println(template.JSEscaper(v...)) fmt.Println(template.URLQueryEscaper(v...)) }
/* <script> s='$text' </script> */ func JS(text interface{}) string { return template.JSEscaper(fmt.Sprint(text)) }