refactor into a `while`

I looked at your comment from the previous PR and realized that since you're already doing the `element = element.parentNode` assignment and null check, a `while` is more succinct than a `do...while`.

It doesn't change any functionality, and the tests still pass.