| | |
| | |
| | |
| |
|
| | package template |
| |
|
| | import ( |
| | "bytes" |
| | "fmt" |
| | "strings" |
| | "unicode/utf8" |
| | ) |
| |
|
| | |
| | func htmlNospaceEscaper(args ...any) string { |
| | s, t := stringify(args...) |
| | if s == "" { |
| | return filterFailsafe |
| | } |
| | if t == contentTypeHTML { |
| | return htmlReplacer(stripTags(s), htmlNospaceNormReplacementTable, false) |
| | } |
| | return htmlReplacer(s, htmlNospaceReplacementTable, false) |
| | } |
| |
|
| | |
| | func attrEscaper(args ...any) string { |
| | s, t := stringify(args...) |
| | if t == contentTypeHTML { |
| | return htmlReplacer(stripTags(s), htmlNormReplacementTable, true) |
| | } |
| | return htmlReplacer(s, htmlReplacementTable, true) |
| | } |
| |
|
| | |
| | func rcdataEscaper(args ...any) string { |
| | s, t := stringify(args...) |
| | if t == contentTypeHTML { |
| | return htmlReplacer(s, htmlNormReplacementTable, true) |
| | } |
| | return htmlReplacer(s, htmlReplacementTable, true) |
| | } |
| |
|
| | |
| | func htmlEscaper(args ...any) string { |
| | s, t := stringify(args...) |
| | if t == contentTypeHTML { |
| | return s |
| | } |
| | return htmlReplacer(s, htmlReplacementTable, true) |
| | } |
| |
|
| | |
| | |
| | var htmlReplacementTable = []string{ |
| | |
| | |
| | |
| | |
| | |
| | |
| | 0: "\uFFFD", |
| | '"': """, |
| | '&': "&", |
| | '\'': "'", |
| | '+': "+", |
| | '<': "<", |
| | '>': ">", |
| | } |
| |
|
| | |
| | |
| | var htmlNormReplacementTable = []string{ |
| | 0: "\uFFFD", |
| | '"': """, |
| | '\'': "'", |
| | '+': "+", |
| | '<': "<", |
| | '>': ">", |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | var htmlNospaceReplacementTable = []string{ |
| | 0: "�", |
| | '\t': "	", |
| | '\n': " ", |
| | '\v': "", |
| | '\f': "", |
| | '\r': " ", |
| | ' ': " ", |
| | '"': """, |
| | '&': "&", |
| | '\'': "'", |
| | '+': "+", |
| | '<': "<", |
| | '=': "=", |
| | '>': ">", |
| | |
| | |
| | |
| | '`': "`", |
| | } |
| |
|
| | |
| | |
| | var htmlNospaceNormReplacementTable = []string{ |
| | 0: "�", |
| | '\t': "	", |
| | '\n': " ", |
| | '\v': "", |
| | '\f': "", |
| | '\r': " ", |
| | ' ': " ", |
| | '"': """, |
| | '\'': "'", |
| | '+': "+", |
| | '<': "<", |
| | '=': "=", |
| | '>': ">", |
| | |
| | |
| | |
| | '`': "`", |
| | } |
| |
|
| | |
| | |
| | func htmlReplacer(s string, replacementTable []string, badRunes bool) string { |
| | written, b := 0, new(strings.Builder) |
| | r, w := rune(0), 0 |
| | for i := 0; i < len(s); i += w { |
| | |
| | |
| | |
| | r, w = utf8.DecodeRuneInString(s[i:]) |
| | if int(r) < len(replacementTable) { |
| | if repl := replacementTable[r]; len(repl) != 0 { |
| | if written == 0 { |
| | b.Grow(len(s)) |
| | } |
| | b.WriteString(s[written:i]) |
| | b.WriteString(repl) |
| | written = i + w |
| | } |
| | } else if badRunes { |
| | |
| | |
| | } else if 0xfdd0 <= r && r <= 0xfdef || 0xfff0 <= r && r <= 0xffff { |
| | if written == 0 { |
| | b.Grow(len(s)) |
| | } |
| | fmt.Fprintf(b, "%s&#x%x;", s[written:i], r) |
| | written = i + w |
| | } |
| | } |
| | if written == 0 { |
| | return s |
| | } |
| | b.WriteString(s[written:]) |
| | return b.String() |
| | } |
| |
|
| | |
| | |
| | func stripTags(html string) string { |
| | var b strings.Builder |
| | s, c, i, allText := []byte(html), context{}, 0, true |
| | |
| | |
| | for i != len(s) { |
| | if c.delim == delimNone { |
| | st := c.state |
| | |
| | if c.element != elementNone && !isInTag(st) { |
| | st = stateRCDATA |
| | } |
| | d, nread := transitionFunc[st](c, s[i:]) |
| | i1 := i + nread |
| | if c.state == stateText || c.state == stateRCDATA { |
| | |
| | j := i1 |
| | if d.state != c.state { |
| | for j1 := j - 1; j1 >= i; j1-- { |
| | if s[j1] == '<' { |
| | j = j1 |
| | break |
| | } |
| | } |
| | } |
| | b.Write(s[i:j]) |
| | } else { |
| | allText = false |
| | } |
| | c, i = d, i1 |
| | continue |
| | } |
| | i1 := i + bytes.IndexAny(s[i:], delimEnds[c.delim]) |
| | if i1 < i { |
| | break |
| | } |
| | if c.delim != delimSpaceOrTagEnd { |
| | |
| | i1++ |
| | } |
| | c, i = context{state: stateTag, element: c.element}, i1 |
| | } |
| | if allText { |
| | return html |
| | } else if c.state == stateText || c.state == stateRCDATA { |
| | b.Write(s[i:]) |
| | } |
| | return b.String() |
| | } |
| |
|
| | |
| | |
| | func htmlNameFilter(args ...any) string { |
| | s, t := stringify(args...) |
| | if t == contentTypeHTMLAttr { |
| | return s |
| | } |
| | if len(s) == 0 { |
| | |
| | |
| | |
| | |
| | |
| | return filterFailsafe |
| | } |
| | s = strings.ToLower(s) |
| | if t := attrType(s); t != contentTypePlain { |
| | |
| | return filterFailsafe |
| | } |
| | for _, r := range s { |
| | switch { |
| | case '0' <= r && r <= '9': |
| | case 'a' <= r && r <= 'z': |
| | default: |
| | return filterFailsafe |
| | } |
| | } |
| | return s |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func commentEscaper(args ...any) string { |
| | return "" |
| | } |
| |
|