All files / roosterjs-content-model-plugins/lib/paste/processors pasteButtonProcessor.ts

100% Statements 15/15
66.67% Branches 4/6
100% Functions 2/2
100% Lines 13/13

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 471x                         1x         5x 5x   5x                       22x   8x 8x 14x 14x 14x   14x 17x        
import { addSegment, createText, isNodeOfType, parseFormat } from 'roosterjs-content-model-dom';
import type {
    ElementProcessor,
    ContentModelBlockGroup,
    DomToModelContext,
    ContentModelSegmentFormat,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 * This processor is used to process <button> elements in the DOM and convert them into Content Model.
 * It handles the button element by creating a text segment for each text node within the button.
 */
export const pasteButtonProcessor: ElementProcessor<HTMLButtonElement> = (
    group: ContentModelBlockGroup,
    element: HTMLButtonElement,
    context: DomToModelContext
): void => {
    const format: ContentModelSegmentFormat = {};
    parseFormat(element, context.formatParsers.segment, format, context);
    // Recursively process text nodes in the button
    processTextNodesRecursively(group, element, context, format);
};
 
/**
 * Helper function to recursively iterate through nodes and process text nodes
 */
function processTextNodesRecursively(
    group: ContentModelBlockGroup,
    node: Node,
    context: DomToModelContext,
    format: ContentModelSegmentFormat
): void {
    if (node.nodeType === Node.TEXT_NODE) {
        // Process text node directly
        const text = createText(node.nodeValue || '', format);
        addSegment(group, text);
    } else Eif (isNodeOfType(node, 'ELEMENT_NODE')) {
        const newFormat: ContentModelSegmentFormat = { ...format };
        parseFormat(node, context.formatParsers.segment, newFormat, context);
        // Recursively process all child nodes
        for (let i = 0; i < node.childNodes.length; i++) {
            processTextNodesRecursively(group, node.childNodes[i], context, newFormat);
        }
    }
}