-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathhtml.ts
More file actions
29 lines (27 loc) · 776 Bytes
/
html.ts
File metadata and controls
29 lines (27 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const htmlEntities: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
''': "'",
' ': '\u00A0',
}
export function decodeHtmlEntities(text: string): string {
return text.replace(/&(?:amp|lt|gt|quot|apos|nbsp|#39);/g, match => htmlEntities[match] || match)
}
/**
* Strip all HTML tags from a string, looping until stable to prevent
* incomplete sanitization from nested/interleaved tags
* (e.g. `<scr<script>ipt>` → `<script>` after one pass).
*/
export function stripHtmlTags(text: string): string {
const tagPattern = /<[^>]*>/g
let result = text
let previous: string
do {
previous = result
result = result.replace(tagPattern, '')
} while (result !== previous)
return result
}