All files / roosterjs-content-model-plugins/lib/paste/parsers adjustPercentileLineHeightParser.ts

100% Statements 7/7
100% Branches 6/6
100% Functions 1/1
100% Lines 7/7

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    1x   1x               1x           63x       6x       57x 6x      
import type { ContentModelBlockFormat } from 'roosterjs-content-model-types';
 
const PERCENTAGE_REGEX = /%/;
// Default line height in browsers according to https://developer.mozilla.org/en-US/docs/Web/CSS/line-height#normal
const DEFAULT_BROWSER_LINE_HEIGHT_PERCENTAGE = 1.2;
 
/**
 * @internal
 * Parser for adjusting percentage-based line heights and converting 'normal' to a specific percentage
 * @param format The block format to modify
 * @param element The HTML element being processed
 */
export function adjustPercentileLineHeight(
    format: ContentModelBlockFormat,
    element: HTMLElement
): void {
    // If the line height is less than the browser default line height, line between the text is going to be too narrow
    let parsedLineHeight: number;
    if (
        PERCENTAGE_REGEX.test(element.style.lineHeight) &&
        !isNaN((parsedLineHeight = parseInt(element.style.lineHeight)))
    ) {
        format.lineHeight = (
            DEFAULT_BROWSER_LINE_HEIGHT_PERCENTAGE *
            (parsedLineHeight / 100)
        ).toString();
    } else if (element.style.lineHeight.toLowerCase() === 'normal') {
        format.lineHeight = '120%';
    }
}