Creating a node selection from resolved position is selecting a child node

I’ll try to be more descriptive using code and commenting: rp.node() correctly refers to a rowNode(the rowNode initially has a two child colNodes). However when I create the nodeSelection from rp, it’s node property points to the child colNode. When the replace occurs the colNode is replaced rather than the rowNode as intended. Am I misunderstanding something simple about the NodeSelection?

  public setRowColCount(update:{position:number,count:number}){
    let tr = this.view?.state.tr;
    const rp = this.view?.state.doc.resolve(update.position);
    if(rp && tr){
      let schema=wcSchema;
      const oldNode=rp.node(rp.depth);
      const nodeSel = new NodeSelection(rp);
      const children:Node[]=[];
      for(let i = 0;i<update.count;i++){
        children.push(schema.nodes['colNode'].create());
      }
      const oldattrs = oldNode.attrs;
      const newRowNode =schema.nodes['rowNode'].create(oldattrs,children);

      
      tr=tr.setSelection(nodeSel).replaceSelectionWith(newRowNode,true);
      const newpos = tr.mapping.map(update.position);
      this.view?.dispatch(tr)    
      this.docvalue=this.toHTML();
      this.createNodeSetFrom(newpos);
    }
  }

rp.node(rp.depth) will point to the parent of the node that rp is pointing in front of. (rp.nodeAfter will give you the node directly after the position).

@marijn Thanks for the reply, I am struggling a bit to understand what the various positions are and when they are used. There seems to be two different positions. A position from the DOM and a position form the nodes. Unfortunately the documentation doesn’t seem to be clear on when to use which position, or there is a standard that I am unaware(perhaps parameter naming?). I aplogize for the wall of text and code, my only excuse is to provide you (and everyone else) with as complete a picture as I can.

In my code I;m not overly concerned with DOM selections, the use will work with nodes. I just checked the outputted HTML, and the style and attribute events are also being applied to the child node.

Is there a ‘correct’ way to get the position of a clicked on node and then walk back up the path getting each node, position(a position that can be used to replace the node) along the way?