DOMPurify strips bare tr/td — wrap in table context first
(html: string)
| 21 | |
| 22 | /** DOMPurify strips bare tr/td — wrap in table context first */ |
| 23 | function sanitizePatchFragment(html: string): string { |
| 24 | if (!html || !/<\/?t[dhbr]/i.test(html)) { |
| 25 | return sanitizeHtml(html); |
| 26 | } |
| 27 | |
| 28 | const section = /<th\b/i.test(html) ? "thead" : "tbody"; |
| 29 | const wrapped = `<table><${section}>${html}</${section}></table>`; |
| 30 | const clean = sanitizeHtml(wrapped); |
| 31 | const re = new RegExp( |
| 32 | `<table><${section}>([\\s\\S]*)</${section}></table>`, |
| 33 | "i", |
| 34 | ); |
| 35 | const match = clean.match(re); |
| 36 | return match?.[1]?.trim() ? match[1]! : sanitizeHtml(html); |
| 37 | } |
| 38 | const TR_RE = /<tr[^>]*>[\s\S]*?<\/tr>/gi; |
| 39 | const CELL_RE = /<t[hd]\b/i; |
| 40 |
no test coverage detected