import { JSDOM } from 'jsdom'; import DOMPurify, { WindowLike } from 'dompurify'; import { globalEnv } from "../../../common/globalEnv.js"; import { descendentIterator } from "../../../common/html/Html.js"; const defaultUrl = `https://${globalEnv.host}`; export class TransformContext { public node: Node; get element() { return this.node as HTMLElement; } get isTextNode() { return this.node.nodeType === 3; } get isElement() { return this.node.nodeType === 1; } get nodeType() { switch(this.node.nodeType) { case 1: return "TextNode"; case 3: return "Element"; } } private queueActions = [] as (() => void | Promise)[]; constructor(protected readonly document) { } remove = () => { const node = this.node as HTMLElement; this.queueActions.push(() => node.remove()); }; queue = (x: () => void | Promise) => { this.queueActions.push(x); }; *iterateAttributes(attributes: string[]) { const element = this.node as HTMLElement; for (const a of attributes) { const av = element.getAttribute(a); if (av === null) { continue; } yield { name: a, value: av}; } } /** * This method will replace fragment of text with nodes represented by given html. * This isn't simple as replacing innerHTML, as parent might contain multiple text nodes. * And only one of the text node is replaced with multiple html nodes. * For example, * ```html *
* This is a notification regarding * Email * That your account has been #Activated; *
* ``` * In this example, only last text node is replaced with * ```html * That your account has been #Activated * ``` * @param html outer html to be replaced */ replaceNodeWithHtml = (html) => { const node = this.node; this.queue(() => { const div = this.document.createElement("div"); div.innerHTML = html; const parent = node.parentNode; let child = div.firstChild; while(child) { const next = child.nextSibling; div.removeChild(child); parent.insertBefore(child, node); child = next; } parent.removeChild(node); }); }; replaceBackground(prefix: any) { const e = this.node as HTMLElement; if (!/body|td|th|table/i.test(e.tagName)) { return; } const attributeName = "background"; let src = e.getAttribute(attributeName); if (!src || src.startsWith("cid:")) { return; } src = src.replace(/https?\:\/\//, prefix); e.setAttribute(attributeName, src); } replaceSrc(prefix: any, attributeName = "src") { const e = this.node as HTMLElement; if (!/img|picture|video|source|svg|image\-slices/i.test(e.tagName)) { return; } let src = e.getAttribute(attributeName); if (!src || src.startsWith("cid:")) { return; } src = src.replace(/https?\:\/\//g, prefix); e.setAttribute(attributeName, src); } async commit() { try { for (const a of this.queueActions) { const r = a(); if((r as any)?.then) { await r; } } this.queueActions.length = 0; } catch (error) { throw error; } } } export default class HtmlTransform { public readonly root: HTMLElement; document: Document; public get html() { return this.root.outerHTML; } private context: TransformContext; constructor(html: string, public readonly url = defaultUrl) { const window = new JSDOM('', { url }).window as any as WindowLike; const purify = DOMPurify(window); this.root = purify.sanitize(html, { ADD_ATTR: ["hash-tag"], RETURN_DOM: true }) as HTMLElement; this.document = window.document; this.context = new TransformContext(this.document); } *all() { for(const node of descendentIterator(this.root)) { this.context.node = node; yield this.context; } } async commit() { await this.context.commit(); } }