All files / roosterjs-content-model-plugins/lib/imageEdit/utils canRegenerateImage.ts

85.71% Statements 12/14
75% Branches 3/4
100% Functions 1/1
85.71% Lines 12/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 24 25 26 27 28 29              1x 2x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x                
/**
 * @internal
 * Check if we can regenerate edited image from the source image.
 * An image can't regenerate result when there is CORS issue of the source content.
 * @param img The image element to test
 * @returns True when we can regenerate the edited image, otherwise false
 */
export function canRegenerateImage(img: HTMLImageElement | null): boolean {
    if (!img) {
        return false;
    }
 
    try {
        const canvas = img.ownerDocument.createElement('canvas');
        canvas.width = 10;
        canvas.height = 10;
        const context = canvas.getContext('2d');
        Eif (context) {
            context.drawImage(img, 0, 0);
            context.getImageData(0, 0, 1, 1);
            return true;
        }
 
        return false;
    } catch {
        return false;
    }
}