All files / roosterjs-content-model-core/lib/corePlugin/selection normalizePos.ts

100% Statements 15/15
83.33% Branches 25/30
100% Functions 1/1
100% Lines 15/15

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 611x                 1x                                           1x 26x     26x   26x 8x 4x 4x       4x   4x       1x   3x 3x         26x    
import { isNodeOfType } from 'roosterjs-content-model-dom';
import type { DOMInsertPoint } from 'roosterjs-content-model-types';
 
/**
 * HTML void elements
 * Per https://www.w3.org/TR/html/syntax.html#syntax-elements, cannot have child nodes
 * This regex is used when we move focus to very begin of editor. We should avoid putting focus inside
 * void elements so users don't accidentally create child nodes in them
 */
const HTML_VOID_ELEMENTS = [
    'AREA',
    'BASE',
    'BR',
    'COL',
    'COMMAND',
    'EMBED',
    'HR',
    'IMG',
    'INPUT',
    'KEYGEN',
    'LINK',
    'META',
    'PARAM',
    'SOURCE',
    'TRACK',
    'WBR',
];
 
/**
 * @internal
 */
export function normalizePos(node: Node, offset: number): DOMInsertPoint {
    const len = isNodeOfType(node, 'TEXT_NODE')
        ? node.nodeValue?.length ?? 0
        : node.childNodes.length;
    offset = Math.max(Math.min(offset, len), 0);
 
    while (node?.lastChild) {
        if (offset >= node.childNodes.length) {
            node = node.lastChild;
            offset = isNodeOfType(node, 'TEXT_NODE')
                ? node.nodeValue?.length ?? 0
                : node.childNodes.length;
        } else {
            const nextNode = node.childNodes[offset];
 
            if (
                isNodeOfType(nextNode, 'ELEMENT_NODE') &&
                HTML_VOID_ELEMENTS.indexOf(nextNode.tagName) >= 0
            ) {
                break;
            } else {
                node = node.childNodes[offset];
                offset = 0;
            }
        }
    }
 
    return { node, offset };
}