Tr.mapping.map value incorrect after replacing content at differing depths

If I slice content from depth 2 and place it at depth 1 using tr.replace(...) I’m finding that I can’t then rely on tr.mapping.map(from) to provide a proper result. It adds positions equal to the size of the replace content - even if the replace content is identical to the original content, but one level shallower in the document.

Is this the expected behavior?

You’re going to have to provide example code for this (but keep it minimal).

1 Like

I know this isn’t really a repro; I’m really not supposed to share internal code. But I can confirm this issue.

This replaceWith (possibly only in conjunction with operating on the document in reverse) yields incorrect results:

// Change nodes to paragraph before further operations, (eg heading to paragraph)
nodesToWrap.reverse().forEach(({ node, pos, normalizeToParagraph }) => {
  if (normalizeToParagraph) {
    // Normalize the node to a paragraph, preserving its content
    const content = node.content.size > 0 ? node.content : null;
    const paragraphNode = paragraph.create(node.attrs, content);
     tr.replaceWith(pos, pos + node.nodeSize, paragraphNode); // BUG: Seemingly caused by this replaceWith, possibly only in conjunction with operating on the document in reverse
   }
})

Of note to my previous finding, it only seems to affect empty selections. Expanded selections are calculated correctly. Not sure if this offers any clues @marijn.