Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,25 @@ function sanitizeHtml(
element.setAttribute('target', '_blank');
}
} else {
element.insertAdjacentHTML('afterend', element.innerHTML);
// Disallowed tag: mark, and unwrap only after traversal
toRemove.push(element);
}
}

for (const element of toRemove) {
try {
try {
element.parentNode?.removeChild(element);
} catch {
element.outerHTML = '';
}
} catch {
try {
element.remove();
} catch {}
// Unwrap disallowed elements by moving their child nodes before them
// and then dropping the now-empty element.
// Iterate in reverse to start from the innermost elements
// and limit the size of the trees we move.
for (let i = toRemove.length - 1; i >= 0; i--) {
const element = toRemove[i];
const parent = element.parentNode;
if (!parent) continue; // already removed
// copy each of its children above it
while (element.firstChild) {
parent.insertBefore(element.firstChild, element);
}
// then remove it
parent.removeChild(element);
}

const styleList = doc.querySelectorAll('style');
Expand Down
Loading