Added sanity check to prevent infinite loop

Added sanity check to prevent infinite looping of the while loop if the parentWidth is 0, e.g. parent is "display: none".