This updates the logic to use iteration instead of recursive calls, resulting in reduced memory usage and avoiding a possible stack overflow for large HTML documents.
The JS code is also tidied up with improved JSDoc and utilizing `let` instead of `var`.
Props jrchamp, sabernhardt, SirLouen, rollybueno, mukesh27, flixos90, westonruter.
Fixes#48054.
Built from https://develop.svn.wordpress.org/trunk@60907
git-svn-id: http://core.svn.wordpress.org/trunk@60243 1a063a9b-81f0-0310-95a4-ce76da25c4cd