All files / roosterjs-content-model-dom/lib/formatHandlers/segment superOrSubScriptFormatHandler.ts

100% Statements 13/13
89.47% Branches 17/19
100% Functions 4/4
100% Lines 13/13

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 461x             1x   2847x 2847x   2847x 10x               1984x 5x       6x   6x 6x                   1x       3409x    
import { wrapAllChildNodes } from '../../domUtils/moveChildNodes';
import type { FormatHandler } from '../FormatHandler';
import type { SuperOrSubScriptFormat } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const superOrSubScriptFormatHandler: FormatHandler<SuperOrSubScriptFormat> = {
    parse: (format, element, context, defaultStyle) => {
        const verticalAlign = element.style.verticalAlign || defaultStyle.verticalAlign;
        const fontSize = element.style.fontSize || defaultStyle.fontSize;
 
        if (isSuperOrSubScript(fontSize, verticalAlign)) {
            format.superOrSubScriptSequence = (format.superOrSubScriptSequence || '')
                .split(' ')
                .concat(verticalAlign)
                .join(' ')
                .trim();
        }
    },
    apply: (format, element) => {
        if (format.superOrSubScriptSequence) {
            format.superOrSubScriptSequence
                .split(' ')
                .reverse()
                .forEach(value => {
                    const tagName = value == 'super' ? 'sup' : value == 'sub' ? 'sub' : null;
 
                    Eif (tagName) {
                        wrapAllChildNodes(element, tagName);
                    }
                });
        }
    },
};
 
/**
 * @internal
 */
export function isSuperOrSubScript(
    fontSize: string | undefined,
    verticalAlign: string | undefined
): verticalAlign is 'sub' | 'super' {
    return fontSize == 'smaller' && (verticalAlign == 'sub' || verticalAlign == 'super');
}