code: function (node, offset) {
        if (typeof offset === 'undefined') {
          offset = node.nodeType === 3 ? node.length : node.childNodes.length;
        }
  
        while (node && node.hasChildNodes()) {
          node = node.childNodes[offset];
          if (node) {
            offset = node.nodeType === 3 ? node.length : node.childNodes.length;
          }
        }
        return { node: node, offset: offset };
      }
docs: This function walks down the tree to find the leaf at the selection. The offset is also returned as if node initially a leaf, the offset may be in the middle of the text node.