This is the expected behavior.

Line 83 adds the current term's URL in the first iteration of the while loop. Then, as long as the current term has children, it may add additional parent URLs in additional iterations of the loop.