closing tags and using valid XML escape sequences to avoid JS errors on XHTML pages

Note that sometimes people may want to use this library in pages they did not author and cannot control (e.g. in third-party libraries or in browser extension content scripts).