Consider following https://w3c.github.io/DOM-Parsing/#xml-serialization
I'm looking for something that follows the spec browsers use so that I can use it in jsdom. Any thoughts on adding a mode that follows that algorithm more exactly?
Hey, as somewhat hinted at in the README, I'm happy to make this thing more standards compliant. We might only need to retain some specifics, for rasterizeHTML.js to force HTML to XML conversion, but hopefully this will get down to being a xmlns override only + the removal of characters invalid in XML. Both could be made optional behaviour.
After a quick glimpse into the linked document, it looks like e.g. the serialisation for empty nodes needs an overhaul. And most likely there's going to be more.
Let me know what you need. Happy to give access to the repo.
Btw, tests/BrowserCompatibilityRunner.html shows how the test cases align with what a browser would currently serialise. Gives a bit of an overview on what's different right now.
@cburgmer @domenic I'm happy to help out also if I can, as we could need this in https://github.com/paperjs/paper.js
Indeed xmlserializer seems to behave differently from browsers.
In a browser you can do:
const doctypeStr = new XMLSerializer().serializeToString(document.doctype);
while xmlserializer.serializeToString(document.doctype) results in:
TypeError: Cannot read property 'replace' of undefined
at removeInvalidCharacters (node_modules/xmlserializer/xmlserializer.js:13:24)
at Object.serializeToString (node_modules/xmlserializer/xmlserializer.js:126:20)
at XMLSerializer.serializeToString (test/helper.js:57:29)