function commonParentNode(oNode1, oNode2) {
    if(oNode1.contains(oNode2)) return oNode1;
    else return commonParentNode(oNode1.parentNode, oNode2);
}

The contains() method returns true if a node is a descendant of a node.

The contains() method returns false if not.

The parentNode property returns the parent node of an element or node.

The parentNode property is read-only.

https://www.w3schools.com/jsref/met_node_contains.asp

https://www.w3schools.com/jsref/prop_node_parentnode.asp