All files / roosterjs-content-model-api/lib/modelApi/image applyImageBorderFormat.ts

100% Statements 35/35
93.75% Branches 15/16
100% Functions 1/1
100% Lines 35/35

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 47 48 49 50 51 52 53 54 55 56 57 581x           1x         20x 18x 54x 18x 18x 18x 18x 18x 18x   18x 10x 8x 4x   4x     18x 8x 10x 3x   7x     18x 7x 11x 4x   18x 18x 18x 18x   2x 2x 2x 2x     20x 20x      
import { extractBorderValues, parseValueWithUnit } from 'roosterjs-content-model-dom';
import type { Border, ContentModelImage } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function applyImageBorderFormat(
    image: ContentModelImage,
    border: Border | null,
    borderRadius?: string
) {
    if (border) {
        const format = image.format;
        const { width, style, color } = border;
        const borderKey = 'borderTop';
        const extractedBorder = extractBorderValues(format[borderKey]);
        const borderColor = extractedBorder.color;
        const borderWidth = extractedBorder.width;
        const borderStyle = extractedBorder.style;
        let borderFormat = '';
 
        if (width) {
            borderFormat = parseValueWithUnit(width) + 'px';
        } else if (borderWidth) {
            borderFormat = borderWidth;
        } else {
            borderFormat = '1px';
        }
 
        if (style) {
            borderFormat = `${borderFormat} ${style}`;
        } else if (borderStyle) {
            borderFormat = `${borderFormat} ${borderStyle}`;
        } else {
            borderFormat = `${borderFormat} solid`;
        }
 
        if (color) {
            borderFormat = `${borderFormat} ${color}`;
        } else if (borderColor) {
            borderFormat = `${borderFormat} ${borderColor}`;
        }
        image.format.borderLeft = borderFormat;
        image.format.borderTop = borderFormat;
        image.format.borderBottom = borderFormat;
        image.format.borderRight = borderFormat;
    } else {
        delete image.format.borderLeft;
        delete image.format.borderTop;
        delete image.format.borderBottom;
        delete image.format.borderRight;
    }
 
    Eif (borderRadius) {
        image.format.borderRadius = borderRadius;
    }
}