All files / roosterjs-content-model-dom/lib/domToModel/processors linkProcessor.ts

100% Statements 22/22
100% Branches 14/14
100% Functions 2/2
100% Lines 21/21

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 491x 1x 1x 1x 1x             1x 47x 47x   47x 44x 44x           44x 44x 44x   44x   5x         5x 1x     5x   39x         3x      
import { addSegment } from '../../modelApi/common/addSegment';
import { createText } from '../../modelApi/creators/createText';
import { knownElementProcessor } from './knownElementProcessor';
import { parseFormat } from '../utils/parseFormat';
import { stackFormat } from '../utils/stackFormat';
import type { StackFormatOptions } from '../utils/stackFormat';
import type { ElementProcessor } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const linkProcessor: ElementProcessor<HTMLElement> = (group, element, context) => {
    const name = element.getAttribute('name');
    const href = element.getAttribute('href');
 
    if (name || href) {
        const isAnchor = !!name && !href;
        const option: StackFormatOptions = {
            // For anchor (name without ref), no need to add other styles
            // For link (href exists), add default link styles
            link: isAnchor ? 'empty' : 'linkDefault',
        };
 
        stackFormat(context, option, () => {
            parseFormat(element, context.formatParsers.link, context.link.format, context);
            parseFormat(element, context.formatParsers.dataset, context.link.dataset, context);
 
            if (isAnchor && !element.firstChild) {
                // Empty anchor, need to make sure it has some child in model
                const emptyText = createText('', context.segmentFormat, {
                    dataset: context.link.dataset,
                    format: context.link.format,
                });
 
                if (context.isInSelection) {
                    emptyText.isSelected = true;
                }
 
                addSegment(group, emptyText);
            } else {
                knownElementProcessor(group, element, context);
            }
        });
    } else {
        // A tag without name or href, can be treated as normal SPAN tag
        knownElementProcessor(group, element, context);
    }
};