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

100% Statements 11/11
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10

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 301x     1x     1x                 1x       87x 5x 5x 5x   5x 2x        
import { parseValueWithUnit } from 'roosterjs-content-model-dom';
import type { FormatParser, MarginFormat } from 'roosterjs-content-model-types';
 
const MSO_LIST_PARAGRAPH_CLASS = 'MsoListParagraph';
 
// Default list padding from the HTML user-agent stylesheet (paddingInlineStart for <ul>/<ol>)
const DEFAULT_LIST_PADDING_INLINE_START = '40px';
 
/**
 * @internal
 * Parser that subtracts the default list format (paddingInlineStart: 40px) from
 * the marginLeft of list item elements that have the MsoListParagraph class,
 * since Word adds the full indentation as margin on the paragraph, which
 * duplicates the padding the list element already provides.
 */
export const adjustWordListMarginParser: FormatParser<MarginFormat> = (
    format: MarginFormat,
    element: HTMLElement
): void => {
    if (element.classList.contains(MSO_LIST_PARAGRAPH_CLASS) && format.marginLeft) {
        const currentPx = parseValueWithUnit(format.marginLeft, element);
        const defaultPx = parseValueWithUnit(DEFAULT_LIST_PADDING_INLINE_START);
        const result = currentPx - defaultPx;
 
        if (result > 0) {
            format.marginLeft = `${result}px`;
        }
    }
};