xmlserializer serializes a DOM subtree or DOM document into XML/XHTML
MIT License
xmlserializer serializes a DOM subtree or DOM document into XML/XHTML.
It understands documents generated by parse5 and regular browser DOMs (and thus can act as a drop-in replacement for XMLSerializer which for some browsers only serializes true XML documents).
See the wiki for limitations in HTML to XML conversion.
Currently some cases are treated differently to the XMLSerializer implementation of the browsers:
xmlns
attribute has higher precedence than the type of the DOM object passed to the serializer.This behaviour might become optional in the future.
See https://runkit.com/embed/siinwcyjdcjw
$ npm install && npm test
For a browser based example run npm test
and see example.html
.
The same code for Node.js:
var xmlserializer = require('xmlserializer');
var html2xhtml = function (htmlString) {
var parser = require('parse5'),
dom = parser.parse(htmlString);
return xmlserializer.serializeToString(dom);
};
console.log(html2xhtml('<br>'));
// => <html xmlns="http://www.w3.org/1999/xhtml"><head/><body><br/></body></html>