Generating relative XPath given an element or

I am looking for a library that can generate a relative XPath given a DOM element. The relative XPath would begin at the nearest element with an id or class, and end up at the element.

This library is the closest thing I could find:

This code has to work in front-end JavaScript. Looks like with the xpath-dom package, it can generate a relative xpath, but you have to provide the library with a root element to start from.

xpath.getUniqueXPath(el, elToStartFrom)

does anyone know if there is native functionality to get a relative XPath to an element, starting from the nearest element with an id/class?