Fixes #13779. Remove nodes in document order (uses for loop matching empty()).