| 1 | | /// `text` with the characters HTML gives meaning to replaced by entities, safe inside |
| 2 | | /// element content and double-quoted attributes. |
| 3 | 1990 | func escapingHTML(_ text: some StringProtocol) -> String { |
| 4 | 1990 | var escaped = "" |
| 5 | 1990 | escaped.reserveCapacity(text.utf8.count) |
| 6 | 23.2k | for character in text { |
| 7 | 23.2k | switch character { |
| 8 | 23.2k | case "&": escaped += "&" |
| 9 | 23.2k | case "<": escaped += "<" |
| 10 | 23.2k | case ">": escaped += ">" |
| 11 | 23.2k | case "\"": escaped += """ |
| 12 | 23.2k | default: escaped.append(character) |
| 13 | 23.2k | } |
| 14 | 23.2k | } |
| 15 | 2058 | return escaped |
| 16 | 1990 | } |