All files / roosterjs-content-model-plugins/lib/paste/parsers linkParser.ts

84.62% Statements 11/13
85.71% Branches 6/7
100% Functions 1/1
83.33% Lines 10/12

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 301x     1x 1x         1x 15x         15x 15x         15x       2x 2x      
import { isElementOfType } from 'roosterjs-content-model-dom';
import type { ContentModelHyperLinkFormat, FormatParser } from 'roosterjs-content-model-types';
 
const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'notes:', 'mailto:', 'onenote:'];
const INVALID_LINKS_REGEX = /^file:\/\/\/[a-zA-Z\/]/i;
 
/**
 * @internal
 */
export const parseLink: FormatParser<ContentModelHyperLinkFormat> = (format, element) => {
    Iif (!isElementOfType(element, 'a')) {
        return;
    }
 
    let url: URL | undefined;
    try {
        url = new URL(element.href);
    } catch {
        url = undefined;
    }
 
    if (
        (url && SUPPORTED_PROTOCOLS.indexOf(url.protocol) === -1) ||
        INVALID_LINKS_REGEX.test(element.href)
    ) {
        element.removeAttribute('href');
        format.href = '';
    }
};