All files / roosterjs-content-model-plugins/lib/paste/utils customListUtils.ts

100% Statements 39/39
90.48% Branches 19/21
100% Functions 6/6
100% Lines 38/38

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1161x 1x                                         1x 190x           1x             190x   190x 190x 190x       190x 56x 92x     134x 134x     190x           1x               190x 190x 190x 170x     190x   190x 190x 190x           190x 162x 162x 162x     190x   190x 190x     1x           190x 174x 297x 297x 297x         95x          
import { removeNegativeTextIndentParser } from '../parsers/removeNegativeTextIndentParser';
import {
    createListLevel,
    parseFormat,
    updateListMetadata,
    createListItem,
    addBlock,
    isElementOfType,
} from 'roosterjs-content-model-dom';
import type {
    DomToModelContext,
    DomToModelListFormat,
    ContentModelBlockGroup,
    ContentModelListLevel,
    ListMetadataFormat,
    ContentModelListItem,
    ContentModelListItemLevelFormat,
    ContentModelListItemFormat,
    FormatParser,
    ContentModelSegmentFormat,
} from 'roosterjs-content-model-types';
 
const removeMargin = (format: ContentModelListItemFormat): void => {
    delete format.marginLeft;
};
 
/**
 * @internal
 */
export function setupListFormat(
    listType: 'OL' | 'UL',
    element: HTMLElement,
    context: DomToModelContext,
    listDepth: number,
    listFormat: DomToModelListFormat,
    group: ContentModelBlockGroup,
    IadditionalParsers: FormatParser<ContentModelListItemLevelFormat>[] = []
) {
    const newLevel: ContentModelListLevel = createListLevel(listType);
    parseFormat(element, context.formatParsers.listLevel, newLevel.format, context);
    parseFormat(element, additionalParsers.concat(removeMargin), newLevel.format, context);
 
    // If the list format is in a different level, update the array so we get the new item
    // To be in the same level as the provided level metadata.
    if (listDepth > listFormat.levels.length) {
        while (listDepth != listFormat.levels.length) {
            listFormat.levels.push(newLevel);
        }
    } else {
        listFormat.levels.splice(listDepth, listFormat.levels.length - 1);
        listFormat.levels[listDepth - 1] = newLevel;
    }
 
    listFormat.listParent = group;
}
 
/**
 * @internal
 */
export function processAsListItem(
    context: DomToModelContext,
    element: HTMLElement,
    group: ContentModelBlockGroup,
    listFormatMetadata: ListMetadataFormat | undefined,
    bulletElement: HTMLElement | undefined,
    beforeProcessingChildren?: (listItem: ContentModelListItem) => void
) {
    const listFormat = context.listFormat;
    const lastLevel = listFormat.levels[listFormat.levels.length - 1];
    if (listFormatMetadata && lastLevel) {
        updateListMetadata(lastLevel, metadata => Object.assign({}, metadata, listFormatMetadata));
    }
 
    const listItem = createListItem(listFormat.levels, context.segmentFormat);
 
    parseFormat(element, context.formatParsers.segmentOnBlock, context.segmentFormat, context);
    parseFormat(element, context.formatParsers.listItemElement, listItem.format, context);
    parseFormat(
        element,
        [removeNegativeTextIndentParser, nonListElementParser],
        listItem.format,
        context
    );
    if (bulletElement) {
        const format: ContentModelSegmentFormat = { ...context.segmentFormat };
        parseFormat(bulletElement, context.formatParsers.segmentOnBlock, format, context);
        listItem.formatHolder.format = format;
    }
 
    beforeProcessingChildren?.(listItem);
 
    context.elementProcessors.child(listItem, element, context);
    addBlock(group, listItem);
}
 
const nonListElementParser: FormatParser<ContentModelListItemFormat> = (
    format,
    element,
    _context,
    defaultStyle
): void => {
    if (!isElementOfType(element, 'li')) {
        Object.keys(defaultStyle).forEach(keyInput => {
            const key = keyInput as keyof CSSStyleDeclaration;
            const formatKey = keyInput as keyof ContentModelListItemFormat;
            if (
                key != 'display' &&
                format[formatKey] != undefined &&
                format[formatKey] == defaultStyle[key]
            ) {
                delete format[formatKey];
            }
        });
    }
};