MCPcopy Create free account
hub / github.com/Alphanimble/htmlstream / sanitizePatchFragment

Function sanitizePatchFragment

src/patch-streaming-html.ts:23–37  ·  view source on GitHub ↗

DOMPurify strips bare tr/td — wrap in table context first

(html: string)

Source from the content-addressed store, hash-verified

21
22/** DOMPurify strips bare tr/td — wrap in table context first */
23function 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}
38const TR_RE = /<tr[^>]*>[\s\S]*?<\/tr>/gi;
39const CELL_RE = /<t[hd]\b/i;
40

Callers 3

insertSafeHtmlFunction · 0.85
promoteOrAppendRowsFunction · 0.85
patchPartialRowInSectionFunction · 0.85

Calls 1

sanitizeHtmlFunction · 0.90

Tested by

no test coverage detected