All files / roosterjs-content-model-core/lib/override pasteGeneralProcessor.ts

100% Statements 18/18
85.71% Branches 12/14
100% Functions 4/4
100% Lines 17/17

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 591x 1x                           1x 1x     1x               1x     48x 48x 48x 48x   48x 15x   15x   5x               5x 5x           15x      
import { moveChildNodes } from 'roosterjs-content-model-dom';
import {
    AllowedTags,
    createSanitizedElement,
    DisallowedTags,
} from '../command/createModelFromHtml/sanitizeElement';
import type {
    DomToModelOptionForSanitizing,
    ElementProcessor,
    ValueSanitizer,
} from 'roosterjs-content-model-types';
 
/**
 * @internal Export for test only
 */
export const removeDisplayFlex: ValueSanitizer = value => {
    return value == 'flex' ? null : value;
};
 
const DefaultStyleSanitizers: Readonly<Record<string, ValueSanitizer>> = {
    position: false,
    display: removeDisplayFlex,
};
 
/**
 * @internal
 */
export function createPasteGeneralProcessor(
    options: DomToModelOptionForSanitizing
): ElementProcessor<HTMLElement> {
    const allowedTags = AllowedTags.concat(options.additionalAllowedTags);
    const disallowedTags = DisallowedTags.concat(options.additionalDisallowedTags);
    const styleSanitizers = Object.assign({}, DefaultStyleSanitizers, options.styleSanitizers);
    const attrSanitizers = options.attributeSanitizers;
 
    return (group, element, context) => {
        const tag = element.tagName.toLowerCase();
        const processor: ElementProcessor<HTMLElement> | undefined =
            allowedTags.indexOf(tag) >= 0
                ? (group, element, context) => {
                      const sanitizedElement = createSanitizedElement(
                          element.ownerDocument,
                          element.tagName,
                          element.attributes,
                          styleSanitizers,
                          attrSanitizers
                      );
 
                      moveChildNodes(sanitizedElement, element);
                      context.defaultElementProcessors['*']?.(group, sanitizedElement, context);
                  }
                : disallowedTags.indexOf(tag) >= 0
                ? undefined // Ignore those disallowed tags
                : context.defaultElementProcessors.span; // For other unknown tags, treat them as SPAN
 
        processor?.(group, element, context);
    };
}