All files / roosterjs-content-model-plugins/lib/paste/WordDesktop processWordLists.ts

96% Statements 96/100
94.37% Branches 134/142
100% Functions 7/7
95.83% Lines 92/96

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 2791x 1x 1x                                   1x 1x 1x 1x 1x                                       1x             402x 402x 27x   402x       402x 75x     327x     327x 327x   327x 61x       327x 172x   86x   86x         86x     86x     86x 86x             86x             86x 67x         86x       84x   86x     241x       86x     86x 19x 19x         4x 4x         5x 5x   10x 10x   19x         19x   67x   9x 9x   10x 10x   2x 2x   46x 46x       86x                   67x 67x 67x 67x 67x 67x   67x 67x             55x     55x   55x 26x 29x             5x             67x 93x 93x 58x       9x             86x 33x   86x 6x                                                 86x 86x   86x 82x 99x 99x 78x 78x   78x 64x 64x           86x    
import { getStyles } from '../utils/getStyles';
import { processAsListItem, setupListFormat } from '../utils/customListUtils';
import {
    getListStyleTypeFromString,
    isElementOfType,
    isEmpty,
    isNodeOfType,
} from 'roosterjs-content-model-dom';
import type { WordMetadata } from './WordMetadata';
import type {
    ContentModelBlockGroup,
    ContentModelListItem,
    ContentModelListItemFormat,
    ContentModelListItemLevelFormat,
    ContentModelListLevel,
    DomToModelContext,
    DomToModelListFormat,
} from 'roosterjs-content-model-types';
 
/** Word list metadata style name */
const MSO_LIST = 'mso-list';
const MSO_LIST_IGNORE = 'ignore';
const WORD_FIRST_LIST = 'l0';
const TEMPLATE_VALUE_REGEX = /%[0-9a-zA-Z]+/g;
const BULLET_METADATA = 'bullet';
 
interface WordDesktopListFormat extends DomToModelListFormat {
    wordLevel?: number | '';
    wordList?: string;
    wordKnownLevels?: Map<string, ContentModelListLevel[]>;
}
 
interface WordListFormat extends ContentModelListItemFormat {
    wordList?: string;
}
 
/**
 * @internal
 * @param styles
 * @param group
 * @param element
 * @param context
 * @returns
 */
export function processWordList(
    styles: Record<string, string>,
    group: ContentModelBlockGroup,
    element: HTMLElement,
    context: DomToModelContext,
    metadata: Map<string, WordMetadata>
) {
    const listFormat = context.listFormat as WordDesktopListFormat;
    if (!listFormat.wordKnownLevels) {
        listFormat.wordKnownLevels = new Map<string, ContentModelListLevel[]>();
    }
    const wordListStyle = styles[MSO_LIST] || '';
 
    // If the element contains Ignore style, do not process it,
    // Usually this element contains the fake bullet used in Word Desktop.
    if (wordListStyle.toLowerCase() === MSO_LIST_IGNORE) {
        return true;
    }
 
    const [lNumber, level] = wordListStyle.split(' ');
    // Try get the list metadata from word, which follows this format: l1 level1 lfo2
    // If we are able to get the level property means we can process this element to be a list
    listFormat.wordLevel = level && parseInt(level.substr('level'.length));
    listFormat.wordList = lNumber || WORD_FIRST_LIST;
 
    if (listFormat.levels.length == 0) {
        listFormat.levels =
            (listFormat.wordList && listFormat.wordKnownLevels.get(listFormat.wordList)) || [];
    }
 
    if (wordListStyle && group && typeof listFormat.wordLevel === 'number') {
        const { wordLevel, wordList } = listFormat;
        // Retrieve the Fake bullet on the element and also the list type
        const listMetadata = metadata.get(`${lNumber}:${level}`);
        const listType =
            listMetadata?.['mso-level-number-format']?.toLowerCase() != BULLET_METADATA
                ? 'OL'
                : 'UL';
 
        // Create the new level of the list item and parse the format
        setupListFormat(listType, element, context, wordLevel, listFormat, group, [
            wordListPaddingParser,
        ]);
        (listFormat.levels[listFormat.levels.length - 1]
            .format as WordListFormat).wordList = wordList;
 
        const bullet = getBulletFromMetadata(listMetadata, listType);
        const listFormatMetadata = bullet
            ? {
                  unorderedStyleType: listType == 'UL' ? bullet : undefined,
                  orderedStyleType: listType == 'OL' ? bullet : undefined,
              }
            : undefined;
 
        processAsListItem(
            context,
            element,
            group,
            listFormatMetadata,
            getBulletElement(element),
            listItem => {
                if (listType == 'OL') {
                    setStartNumber(listItem, context, listMetadata, element);
                }
            }
        );
 
        if (
            listFormat.levels.length > 0 &&
            listFormat.wordKnownLevels.get(wordList) != listFormat.levels
        ) {
            listFormat.wordKnownLevels.set(wordList, [...listFormat.levels]);
        }
        return true;
    }
 
    return false;
}
 
function getBulletFromMetadata(listMetadata: WordMetadata | undefined, listType: 'OL' | 'UL') {
    const templateType = listMetadata?.['mso-level-number-format'] || 'decimal';
    let templateFinal: string;
 
    if (listMetadata?.['mso-level-text']) {
        let templateValue: string = '';
        switch (templateType) {
            case 'alpha-upper':
                templateValue = 'UpperAlpha';
                break;
            case 'alpha-lower':
                templateValue = 'LowerAlpha';
                break;
            case 'roman-lower':
                templateValue = 'LowerRoman';
                break;
            case 'roman-upper':
                templateValue = 'UpperRoman';
                break;
            default:
                templateValue = 'Number';
                break;
        }
        const template = (listMetadata['mso-level-text'] || '')
            .replace('\\', '')
            .replace('"', '')
            .replace(TEMPLATE_VALUE_REGEX, '${' + templateValue + '}');
 
        templateFinal = '"' + template + ' "';
    } else {
        switch (templateType) {
            case 'alpha-lower':
                templateFinal = 'lower-alpha';
                break;
            case 'roman-lower':
                templateFinal = 'lower-roman';
                break;
            case 'roman-upper':
                templateFinal = 'upper-roman';
                break;
            default:
                templateFinal = 'decimal';
                break;
        }
    }
 
    return getListStyleTypeFromString(listType, templateFinal);
}
 
function setStartNumber(
    listItem: ContentModelListItem,
    context: DomToModelContext,
    listMetadata: WordMetadata | undefined,
    element: HTMLElement
) {
    const {
        listParent,
        wordList,
        wordKnownLevels,
        wordLevel,
        levels,
    } = context.listFormat as WordDesktopListFormat;
 
    const block = getLastNotEmptyBlock(listParent);
    if (
        (block?.blockType != 'BlockGroup' ||
            block.blockGroupType != 'ListItem' ||
            (wordLevel &&
                (block.levels[wordLevel]?.format as WordListFormat)?.wordList != wordList)) &&
        wordList
    ) {
        const start = listMetadata?.['mso-level-start-at']
            ? parseInt(listMetadata['mso-level-start-at'])
            : NaN;
        const knownLevel = wordKnownLevels?.get(wordList) || [];
 
        if (start != undefined && !isNaN(start) && knownLevel.length != levels.length) {
            listItem.levels[listItem.levels.length - 1].format.startNumberOverride = start;
        } else if (
            isElementOfType(element, 'li') &&
            isNodeOfType(element.parentElement, 'ELEMENT_NODE') &&
            isElementOfType(element.parentElement, 'ol') &&
            element.parentElement.firstElementChild == element &&
            knownLevel.length != element.parentElement.start
        ) {
            listItem.levels[listItem.levels.length - 1].format.startNumberOverride =
                element.parentElement.start;
        }
    }
}
 
function getLastNotEmptyBlock(listParent: ContentModelBlockGroup | undefined) {
    for (let index = (listParent?.blocks.length || 0) - 1; index > 0; index--) {
        const result = listParent?.blocks[index];
        if (result && !isEmpty(result)) {
            return result;
        }
    }
 
    return undefined;
}
 
function wordListPaddingParser(
    format: ContentModelListItemLevelFormat,
    element: HTMLElement
): void {
    if (element.style.marginLeft && element.style.marginLeft != '0in') {
        format.paddingLeft = '0px';
    }
    if (element.style.marginRight && element.style.marginRight != '0in') {
        format.paddingRight = '0px';
    }
}
/**
 * Get the bullet element from word list item.
 * The first element of the list contains the bullet element, which contains the mso-list:ignore style.
 * @example
 *  <p class=MsoListParagraphCxSpFirst style='text-indent:-18.0pt;mso-list:l0 level1 lfo1'>
 *      <![if !supportLists]>
 *          <span lang=EN-US style='mso-fareast-font-family:Aptos;mso-fareast-theme-font:minor-latin; mso-bidi-font-family:Aptos;mso-bidi-theme-font:minor-latin;color:#C00000; mso-ansi-language:EN-US'>
 *              <span style='mso-list:Ignore'> <-- This is the bullet element
 *                  1.
 *                  <span style='font:7.0pt "Times New Roman"'>
 *                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 *                  </span>
 *              </span>
 *          </span>
 *      <![endif]>
 *      <span lang=EN-US style='color:#C00000; mso-ansi-language:EN-US'>
 *          Content in list<o:p></o:p>
 *      </span>
 *  </p>
 * @returns
 */
function getBulletElement(element: HTMLElement): HTMLElement | undefined {
    const firstChild = element.firstElementChild;
    let isBulletElement = false;
 
    if (firstChild) {
        for (let i = 0; i < firstChild.childNodes.length; i++) {
            const child = firstChild.childNodes[i];
            if (isNodeOfType(child, 'ELEMENT_NODE')) {
                const styles = getStyles(child);
                const wordListStyle = styles[MSO_LIST] || '';
 
                if (wordListStyle.toLowerCase() === MSO_LIST_IGNORE) {
                    isBulletElement = true;
                    break;
                }
            }
        }
    }
 
    return firstChild && isBulletElement ? (firstChild as HTMLElement) : undefined;
}