Simplify node logic

The only effect of setting p to 0 here is to break the loop, using break is much simpler.
As for line 858, there is no point to set node to 0 and then break the loop right after.
If there's a mismatch, `break;` will break the loop, InsertEndChild won't be called.