All files / components/src figure.ts

100% Statements 16/16
100% Branches 7/7
100% Functions 3/3
100% Lines 14/14

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        1x 9x 9x 9x 9x 114x   110x 110x 109x 109x 109x 109x 109x 109x        
/**
 * @module kung-fu/components
 */
 
export const wrapImageWithFigure = (selector?: string, exclusion?: string): void => {
    selector = selector || 'img';
    exclusion = exclusion || 'data-no-caption';
    const imgs: NodeListOf<HTMLImageElement> = document.querySelectorAll(selector);
    const arr: HTMLImageElement[] = Array.from(imgs);
    arr.filter((e: HTMLImageElement) => e.getAttribute(exclusion as string) == null)
        .forEach((img: HTMLImageElement): void => {
            const caption = img.getAttribute('title') || img.getAttribute('alt') as string;
            if(caption != null) {
                const figure: HTMLElement = document.createElement('figure');
                const figCaption: HTMLElement = document.createElement('figcaption');
                figCaption.innerText = caption;
                figure.appendChild(img.cloneNode(true));
                figure.appendChild(figCaption);
                img.replaceWith(figure);
            }
        });
};