All files / roosterjs-content-model-dom/lib/formatHandlers/common borderFormatHandler.ts

96.15% Statements 25/26
90% Branches 18/20
100% Functions 5/5
96.15% Lines 25/26

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78            1x               1x             1x             1x         1x   876x 3504x 3504x 3504x   3504x       3504x 272x       876x   876x 2x   874x 3496x   3496x 4x           1129x 9032x   9032x 144x       1129x 1x        
import type { BorderFormat } from 'roosterjs-content-model-types';
import type { FormatHandler } from '../FormatHandler';
 
/**
 * Keys of border items
 */
export const BorderKeys: (keyof BorderFormat & keyof CSSStyleDeclaration)[] = [
    'borderTop',
    'borderRight',
    'borderBottom',
    'borderLeft',
];
 
// This array needs to match BorderKeys array
const BorderWidthKeys: (keyof CSSStyleDeclaration)[] = [
    'borderTopWidth',
    'borderRightWidth',
    'borderBottomWidth',
    'borderLeftWidth',
];
 
const BorderRadiusKeys: (keyof BorderFormat & keyof CSSStyleDeclaration)[] = [
    'borderTopLeftRadius',
    'borderTopRightRadius',
    'borderBottomLeftRadius',
    'borderBottomRightRadius',
];
 
const AllKeys = BorderKeys.concat(BorderRadiusKeys);
 
/**
 * @internal
 */
export const borderFormatHandler: FormatHandler<BorderFormat> = {
    parse: (format, element, _, defaultStyle) => {
        BorderKeys.forEach((key, i) => {
            const value = element.style[key];
            const defaultWidth = defaultStyle[BorderWidthKeys[i]] ?? '0px';
            let width = element.style[BorderWidthKeys[i]];
 
            Iif (width == '0') {
                width = '0px';
            }
 
            if (value && width != defaultWidth) {
                format[key] = value == 'none' ? '' : value;
            }
        });
 
        const borderRadius = element.style.borderRadius;
 
        if (borderRadius) {
            format.borderRadius = borderRadius;
        } else {
            BorderRadiusKeys.forEach(key => {
                const value = element.style[key];
 
                if (value) {
                    format[key] = value;
                }
            });
        }
    },
    apply: (format, element) => {
        AllKeys.forEach(key => {
            const value = format[key];
 
            if (value) {
                element.style[key] = value;
            }
        });
 
        if (format.borderRadius) {
            element.style.borderRadius = format.borderRadius;
        }
    },
};