Rewrite pretty-printing HTML elements to prevent throwing internal errors

when using non-native DOM implementations.

fixes #274