All files / roosterjs-content-model-dom/lib/formatHandlers/list listItemThreadFormatHandler.ts

100% Statements 23/23
100% Branches 23/23
100% Functions 4/4
100% Lines 23/23

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 501x 1x             1x   331x 331x 331x   331x 4x 327x 160x 160x 160x     324x         250x 2x 248x 121x 121x 121x   121x 120x 120x             575x            
import { isElementOfType } from '../../domUtils/isElementOfType';
import { isNodeOfType } from '../../domUtils/isNodeOfType';
import type { FormatHandler } from '../FormatHandler';
import type { ListThreadFormat } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const listItemThreadFormatHandler: FormatHandler<ListThreadFormat> = {
    parse: (format, element, context, defaultStyles) => {
        const { listFormat } = context;
        const depth = listFormat.levels.length;
        const display = element.style.display || defaultStyles.display;
 
        if (display && display != 'list-item') {
            format.displayForDummyItem = display;
        } else if (isLiUnderOl(element) && depth > 0) {
            listFormat.threadItemCounts[depth - 1]++;
            listFormat.threadItemCounts.splice(depth);
            listFormat.levels.forEach(level => {
                // Delete restart number so next list item doesn't need to have this value.
                // Then it will be treated as a continuous list item to the previous one
                delete level.format.startNumberOverride;
            });
        }
    },
    apply: (format, element, context) => {
        if (format.displayForDummyItem) {
            element.style.display = format.displayForDummyItem;
        } else if (isLiUnderOl(element)) {
            const { listFormat } = context;
            const { threadItemCounts } = listFormat;
            const index = listFormat.nodeStack.length - 2; // The first one is always the parent of list, then minus another 1 to convert length to index
 
            if (index >= 0) {
                threadItemCounts.splice(index + 1);
                threadItemCounts[index] = (threadItemCounts[index] ?? 0) + 1;
            }
        }
    },
};
 
function isLiUnderOl(element: HTMLElement) {
    return (
        isElementOfType(element, 'li') &&
        isNodeOfType(element.parentNode, 'ELEMENT_NODE') &&
        isElementOfType(element.parentNode, 'ol')
    );
}