Fixed generation of unique id values

Fixes #6371. Fixes wet-boew/GCWeb#872.

This resolves an odd bug where this function on occasion generates unique id values that start with a number, which can trip up querySelector and querySelectorAll.

/cc @LaurentGoderre