All files / roosterjs-content-model-dom/lib/domUtils/style normalizeFontFamily.ts

100% Statements 19/19
100% Branches 4/4
100% Functions 3/3
100% Lines 19/19

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            1x 36x 36x 36x 36x   36x 22x   22x 22x   22x     36x   36x       58x   58x 113x   113x   68x 9x   59x          
/**
 * Normalize font family string to a standard format
 * Add quotes around font family names that contain non-alphanumeric/dash characters
 * @param fontFamily The font family string to normalize
 * @returns The normalized font family string
 */
export function normalizeFontFamily(fontFamily: string): string {
    const existingQuotedFontsRegex = /(".*?")|('.*?')/g;
    let match = existingQuotedFontsRegex.exec(fontFamily);
    let start = 0;
    const result: string[] = [];
 
    while (match) {
        process(fontFamily, result, start, match.index);
 
        start = match.index + match[0].length;
        result.push(match[0]);
 
        match = existingQuotedFontsRegex.exec(fontFamily);
    }
 
    process(fontFamily, result, start, fontFamily.length);
 
    return result.join(', ');
}
 
function process(fontFamily: string, result: string[], start: number, end: number) {
    const families = fontFamily.substring(start, end).split(',');
 
    families.forEach(family => {
        family = family.trim();
 
        if (family) {
            // Check if the family name contains non-alphanumeric characters
            if (/[^a-zA-Z0-9\-]/.test(family)) {
                result.push(`"${family}"`);
            } else {
                result.push(family);
            }
        }
    });
}