in packages/roosterjs-editor-dom/lib/edit/adjustInsertPosition.ts [101:171]
function adjustInsertPositionForStructuredNode(
root: HTMLElement,
nodeToInsert: Node,
position: NodePosition,
range: Range
): NodePosition {
let rootNodeToInsert = nodeToInsert;
if (rootNodeToInsert.nodeType == NodeType.DocumentFragment) {
let rootNodes = toArray(rootNodeToInsert.childNodes).filter(
(n: ChildNode) => getTagOfNode(n) != 'BR'
);
rootNodeToInsert = rootNodes.length == 1 ? rootNodes[0] : null;
}
let tag = getTagOfNode(rootNodeToInsert);
let hasBrNextToRoot = tag && getTagOfNode(rootNodeToInsert.nextSibling) == 'BR';
let listItem = findClosestElementAncestor(position.node, root, 'LI');
let listNode = listItem && findClosestElementAncestor(listItem, root, 'OL,UL');
let tdNode = findClosestElementAncestor(position.node, root, 'TD,TH');
let trNode = tdNode && findClosestElementAncestor(tdNode, root, 'TR');
if (tag == 'LI') {
tag = listNode ? getTagOfNode(listNode) : 'UL';
rootNodeToInsert = wrap(rootNodeToInsert, tag);
}
if ((tag == 'OL' || tag == 'UL') && getTagOfNode(rootNodeToInsert.firstChild) == 'LI') {
let shouldInsertListAsText = !rootNodeToInsert.firstChild.nextSibling && !hasBrNextToRoot;
if (hasBrNextToRoot && rootNodeToInsert.parentNode) {
safeRemove(rootNodeToInsert.nextSibling);
}
if (shouldInsertListAsText) {
unwrap(rootNodeToInsert.firstChild);
unwrap(rootNodeToInsert);
} else if (getTagOfNode(listNode) == tag) {
unwrap(rootNodeToInsert);
position = new Position(
listItem,
isPositionAtBeginningOf(position, listItem)
? PositionType.Before
: PositionType.After
);
}
} else if (tag == 'TABLE' && trNode) {
// When inserting a table into a table, if these tables have the same column count, and
// current position is at beginning of a row, then merge these two tables
let newTable = new VTable(<HTMLTableElement>rootNodeToInsert);
let currentTable = new VTable(<HTMLTableCellElement>tdNode);
if (
currentTable.col == 0 &&
tdNode == currentTable.getCell(currentTable.row, 0).td &&
newTable.cells[0] &&
newTable.cells[0].length == currentTable.cells[0].length &&
isPositionAtBeginningOf(position, tdNode)
) {
if (
getTagOfNode(rootNodeToInsert.firstChild) == 'TBODY' &&
!rootNodeToInsert.firstChild.nextSibling
) {
unwrap(rootNodeToInsert.firstChild);
}
unwrap(rootNodeToInsert);
position = new Position(trNode, PositionType.After);
}
}
return position;
}