All files / roosterjs-content-model-core/lib/utils domCreator.ts

92.86% Statements 13/14
100% Branches 6/6
85.71% Functions 6/7
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                  1x 103x           1x 110x               1x     6x           1x 52x       109x 109x 54x        
import type {
    DOMCreator,
    LegacyTrustedHTMLHandler,
    TrustedHTMLHandler,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const createTrustedHTMLHandler = (domCreator: DOMCreator): LegacyTrustedHTMLHandler => {
    return (html: string) => domCreator.htmlToDOM(html).body.innerHTML;
};
 
/**
 * @internal
 */
export function createDOMCreator(trustedHTMLHandler?: TrustedHTMLHandler): DOMCreator {
    return trustedHTMLHandler && isDOMCreator(trustedHTMLHandler)
        ? trustedHTMLHandler
        : trustedHTMLHandlerToDOMCreator(trustedHTMLHandler as LegacyTrustedHTMLHandler);
}
 
/**
 * @internal
 */
export function isDOMCreator(
    trustedHTMLHandler: TrustedHTMLHandler
): trustedHTMLHandler is DOMCreator {
    return typeof (trustedHTMLHandler as DOMCreator).htmlToDOM === 'function';
}
 
/**
 * @internal
 */
export const defaultTrustHtmlHandler: LegacyTrustedHTMLHandler = (html: string) => {
    return html;
};
 
function trustedHTMLHandlerToDOMCreator(trustedHTMLHandler?: LegacyTrustedHTMLHandler): DOMCreator {
    const handler = trustedHTMLHandler || defaultTrustHtmlHandler;
    return {
        htmlToDOM: (html: string) => new DOMParser().parseFromString(handler(html), 'text/html'),
        isBypassed: !trustedHTMLHandler,
    };
}