UISanitize sanitizes a string for display in the UI. The following transformations are applied, in order: - HTML tags are removed using bluemonday's strict policy. - ANSI escape codes are stripped using stripansi. - Consecutive backslashes are replaced with a single backslash. - Non-printable charac
(in string)
| 106 | // - Multiple spaces are collapsed into a single space. |
| 107 | // - Leading and trailing whitespace is trimmed. |
| 108 | func UISanitize(in string) string { |
| 109 | if unq, err := strconv.Unquote(`"` + in + `"`); err == nil { |
| 110 | in = unq |
| 111 | } |
| 112 | in = bmPolicy.Sanitize(in) |
| 113 | in = stripansi.Strip(in) |
| 114 | var b strings.Builder |
| 115 | var spaceSeen bool |
| 116 | for _, r := range in { |
| 117 | if unicode.IsSpace(r) { |
| 118 | if !spaceSeen { |
| 119 | _, _ = b.WriteRune(' ') |
| 120 | spaceSeen = true |
| 121 | } |
| 122 | continue |
| 123 | } |
| 124 | spaceSeen = false |
| 125 | if unicode.IsPrint(r) { |
| 126 | _, _ = b.WriteRune(r) |
| 127 | } |
| 128 | } |
| 129 | return strings.TrimSpace(b.String()) |
| 130 | } |
| 131 | |
| 132 | // Capitalize returns s with its first rune upper-cased. It is safe for |
| 133 | // multi-byte UTF-8 characters, unlike naive byte-slicing approaches. |