All files / roosterjs-content-model-plugins/lib/paste/WacComponents processPastedContentWacComponents.ts

94.95% Statements 94/99
93.33% Branches 84/90
100% Functions 14/14
94.85% Lines 92/97

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 2801x 1x 1x 1x                                       1x 1x 1x 1x 1x                                 1x       135x 135x     135x                         1x         404x   404x 23x 23x     404x 58x 58x     346x 2343x       24x 322x           322x           1x         98x 98x   98x   98x 98x 98x 98x   98x 77x 22x 22x     55x 55x       98x   98x 98x 92x 92x 92x 92x       98x 98x 138x         138x   98x 98x               1x       184x 2x             1x     181x 181x                                                   322x   305x         1x       135x       2x                   1x 50x 50x 50x 50x 50x 50x 50x   50x 50x     1x       87x 3x                 92x 69x     23x 23x 23x   23x 13x     23x 23x 3x   23x             23x   23x 23x   23x    
import { addParser } from '../utils/addParser';
import { createListLevel, parseFormat } from 'roosterjs-content-model-dom';
import { setProcessor } from '../utils/setProcessor';
import {
    COMMENT_HIGHLIGHT_CLASS,
    COMMENT_HIGHLIGHT_CLICKED_CLASS,
    LIST_CONTAINER_ELEMENT_CLASS_NAME,
    REMOVE_MARGIN_ELEMENTS,
    TEMP_ELEMENTS_CLASSES,
} from './constants';
import type {
    BeforePasteEvent,
    ContentModelBlockFormat,
    ContentModelBlockGroup,
    ContentModelListItemLevelFormat,
    ContentModelListLevel,
    ContentModelSegmentFormat,
    DomToModelContext,
    DomToModelListFormat,
    ElementProcessor,
    FormatParser,
} from 'roosterjs-content-model-types';
 
const LIST_ELEMENT_TAGS = ['UL', 'OL', 'LI'];
const LIST_ELEMENT_SELECTOR = LIST_ELEMENT_TAGS.join(',');
const END_OF_PARAGRAPH = 'EOP';
const SELECTED_CLASS = 'Selected';
const BASE_PADDING_WAC_LISTS = '1em';
 
interface WacContext extends DomToModelListFormat {
    /**
     * Current list levels
     */
    currentListLevels?: ContentModelListLevel[];
    /**
     * Array to keep the start of the lists and determine if the start override should be set.
     */
    listItemThread?: number[];
}
 
/**
 * Wac components do not use sub and super tags, instead only add vertical align to a span.
 * This parser normalize the content for content model
 */
const wacSubSuperParser: FormatParser<ContentModelSegmentFormat> = (
    format: ContentModelSegmentFormat,
    element: HTMLElement
): void => {
    const verticalAlign = element.style.verticalAlign;
    Iif (verticalAlign === 'super') {
        format.superOrSubScriptSequence = 'super';
    }
    Iif (verticalAlign === 'sub') {
        format.superOrSubScriptSequence = 'sub';
    }
};
 
/**
 * This processor does:
 * 1) Remove the display and margin of the element.
 * 2) When an element should be ignored but should handle the child elements call the default child processor.
 * 3) Removes the End of Paragraph element to avoid empty lines, we should only remove this if the previous element of the EOP is an EmptyTextRun
 * 4) Finally call the default processor.
 * @returns
 */
const wacElementProcessor: ElementProcessor<HTMLElement> = (
    group: ContentModelBlockGroup,
    element: HTMLElement,
    context: DomToModelContext
): void => {
    const elementTag = element.tagName;
 
    if (element.matches(REMOVE_MARGIN_ELEMENTS)) {
        element.style.removeProperty('display');
        element.style.removeProperty('margin');
    }
 
    if (element.classList.contains(LIST_CONTAINER_ELEMENT_CLASS_NAME)) {
        context.elementProcessors.child(group, element, context);
        return;
    }
 
    if (
        TEMP_ELEMENTS_CLASSES.some(className => element.classList.contains(className)) ||
        // This is needed to remove some temporary End of paragraph elements that WAC sometimes preserves
        (element.classList.contains(SELECTED_CLASS) && element.classList.contains(END_OF_PARAGRAPH))
    ) {
        return;
    } else Iif (shouldClearListContext(elementTag, element, context)) {
        const { listFormat } = context;
        listFormat.levels = [];
        listFormat.listParent = undefined;
    }
 
    context.defaultElementProcessors.element(group, element, context);
};
 
/**
 * This processor calls the default list processor and then sets the correct list level and list bullet.
 */
const wacLiElementProcessor: ElementProcessor<HTMLLIElement> = (
    group: ContentModelBlockGroup,
    element: HTMLLIElement,
    context: DomToModelContext
): void => {
    const level = parseInt(element.getAttribute('data-aria-level') ?? '');
    const listFormat = context.listFormat as WacContext;
    const listType =
        listFormat.levels[context.listFormat.levels.length - 1]?.listType ||
        (element.closest('ol,ul')?.tagName.toUpperCase() as 'UL' | 'OL');
    const newLevel: ContentModelListLevel = createListLevel(listType, context.blockFormat);
    parseFormat(element, context.formatParsers.listLevelThread, newLevel.format, context);
    parseFormat(element, context.formatParsers.listLevel, newLevel.format, context);
    context.listFormat.levels = listFormat.currentListLevels || context.listFormat.levels;
 
    if (level > 0) {
        if (level > context.listFormat.levels.length) {
            while (level != context.listFormat.levels.length) {
                context.listFormat.levels.push(newLevel);
            }
        } else {
            context.listFormat.levels.splice(level, context.listFormat.levels.length - 1);
            context.listFormat.levels[level - 1] = newLevel;
        }
    }
 
    context.defaultElementProcessors.li?.(group, element, context);
 
    const listParent = listFormat.listParent;
    if (listParent) {
        const lastblock = listParent.blocks[listParent.blocks.length - 1];
        Eif (lastblock.blockType == 'BlockGroup' && lastblock.blockGroupType == 'ListItem') {
            const currentLevel = lastblock.levels[lastblock.levels.length - 1];
            updateStartOverride(currentLevel, element, context);
        }
    }
 
    const newLevels: ContentModelListLevel[] = [];
    listFormat.levels.forEach(v => {
        const newValue: ContentModelListLevel = {
            dataset: { ...v.dataset },
            format: { ...v.format },
            listType: v.listType,
        };
        newLevels.push(newValue);
    });
    listFormat.currentListLevels = newLevels;
    listFormat.levels = [];
};
 
/**
 * This parsers does:
 * 1) Sets the display for dummy item to undefined when the current style is block.
 * 2) Removes the Margin Left
 */
const wacListItemParser: FormatParser<ContentModelListItemLevelFormat> = (
    format: ContentModelListItemLevelFormat,
    element: HTMLElement
): void => {
    if (element.style.display === 'block') {
        format.displayForDummyItem = undefined;
    }
};
 
/**
 * Wac usually adds padding to lists which is unwanted so remove it.
 */
const wacListLevelParser: FormatParser<ContentModelListItemLevelFormat> = (
    format: ContentModelListItemLevelFormat
): void => {
    format.marginLeft = undefined;
    format.paddingLeft = BASE_PADDING_WAC_LISTS;
};
 
/**
 * This function returns whether we need to clear the list format.
 * Word Online wraps lists inside divs to have this structure:
 *
 *  <div class='ListContainerWrapper'>
 *      <ol>...</ol>
 *  </div>
 *  <div>
 *      <p>...</p>
 *  <div>
 *  <div class='ListContainerWrapper'>
 *      <ol>...</ol>
 *  </div>
 *
 *  So if a elements is not contained inside of a list we should clear the list context to prevent normal text to be
 *  transformed into list
 *  For the above scenario, if we do not clear the format, the content inside of the second div would be transformed to a list too.
 */
function shouldClearListContext(
    elementTag: string,
    element: HTMLElement,
    context: DomToModelContext
) {
    return (
        context.listFormat.levels.length > 0 &&
        LIST_ELEMENT_TAGS.every(tag => tag != elementTag) &&
        !element.closest(LIST_ELEMENT_SELECTOR)
    );
}
 
const wacCommentParser: FormatParser<ContentModelSegmentFormat> = (
    format: ContentModelSegmentFormat,
    element: HTMLElement
): void => {
    if (
        element.className.includes(COMMENT_HIGHLIGHT_CLASS) ||
        element.className.includes(COMMENT_HIGHLIGHT_CLICKED_CLASS)
    ) {
        delete format.backgroundColor;
    }
};
/**
 * @internal
 * Convert pasted content from Office Online
 * Once it is known that the document is from WAC
 * We need to remove the display property and margin from all the list item
 * @param ev BeforePasteEvent
 */
export function processPastedContentWacComponents(ev: BeforePasteEvent) {
    addParser(ev.domToModelOption, 'segment', wacSubSuperParser);
    addParser(ev.domToModelOption, 'listItemThread', wacListItemParser);
    addParser(ev.domToModelOption, 'listItemElement', wacListItemParser);
    addParser(ev.domToModelOption, 'listLevel', wacListLevelParser);
    addParser(ev.domToModelOption, 'container', wacContainerParser);
    addParser(ev.domToModelOption, 'table', wacContainerParser);
    addParser(ev.domToModelOption, 'segment', wacCommentParser);
 
    setProcessor(ev.domToModelOption, 'element', wacElementProcessor);
    setProcessor(ev.domToModelOption, 'li', wacLiElementProcessor);
}
 
const wacContainerParser: FormatParser<ContentModelBlockFormat> = (
    format: ContentModelBlockFormat,
    element: HTMLElement
) => {
    if (element.style.marginLeft.startsWith('-')) {
        delete format.marginLeft;
    }
};
 
function updateStartOverride(
    currentLevel: ContentModelListLevel | undefined,
    element: HTMLLIElement,
    ctx: DomToModelContext
) {
    if (!currentLevel || currentLevel.listType == 'UL') {
        return;
    }
 
    const list = element.closest('ol');
    const listFormat = ctx.listFormat as WacContext;
    const [start, listLevel] = extractWordListMetadata(list, element);
 
    if (!listFormat.listItemThread) {
        listFormat.listItemThread = [];
    }
 
    const thread: number | undefined = listFormat.listItemThread[listLevel];
    if (thread && start - thread != 1) {
        currentLevel.format.startNumberOverride = start;
    }
    listFormat.listItemThread[listLevel] = start;
}
function extractWordListMetadata(
    list: HTMLElement | null | undefined,
    item: HTMLElement | null | undefined
) {
    const itemIndex =
        item && Array.from(list?.querySelectorAll('li') || []).indexOf(item as HTMLLIElement);
    const start =
        parseInt(list?.getAttribute('start') || '1') + (itemIndex && itemIndex > 0 ? itemIndex : 0);
    const listLevel = parseInt(item?.getAttribute('data-aria-level') || '');
 
    return [start, listLevel];
}