All files / roosterjs-content-model-dom/lib/domToModel/processors fontProcessor.ts

96.3% Statements 26/27
87.5% Branches 14/16
100% Functions 3/3
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 551x 1x 1x     1x     7x   7x 3x 4x   4x 1x   3x             1x 7x           7x 7x 7x 7x   7x 3x     7x 4x     7x 3x     7x   7x        
import { isBlockElement } from '../utils/isBlockElement';
import { parseFormat } from '../utils/parseFormat';
import { stackFormat } from '../utils/stackFormat';
import type { ElementProcessor } from 'roosterjs-content-model-types';
 
const FontSizes = ['10px', '13px', '16px', '18px', '24px', '32px', '48px'];
 
function getFontSize(size: string | null) {
    const intSize = parseInt(size || '');
 
    if (Number.isNaN(intSize)) {
        return undefined;
    } else Iif (intSize < 1) {
        return FontSizes[0];
    } else if (intSize > FontSizes.length) {
        return FontSizes[FontSizes.length - 1];
    } else {
        return FontSizes[intSize - 1];
    }
}
 
/**
 * @internal
 */
export const fontProcessor: ElementProcessor<HTMLFontElement> = (group, element, context) => {
    stackFormat(
        context,
        {
            segment: isBlockElement(element) ? 'shallowCloneForBlock' : 'shallowClone',
        },
        () => {
            const fontFamily = element.getAttribute('face');
            const fontSize = getFontSize(element.getAttribute('size'));
            const textColor = element.getAttribute('color');
            const format = context.segmentFormat;
 
            if (fontFamily) {
                format.fontFamily = fontFamily;
            }
 
            if (fontSize) {
                format.fontSize = fontSize;
            }
 
            if (textColor) {
                format.textColor = textColor;
            }
 
            parseFormat(element, context.formatParsers.segment, context.segmentFormat, context);
 
            context.elementProcessors.child(group, element, context);
        }
    );
};