XMLSerializer().serializeToString() method converts DOM tree to text.

(new XMLSerializer()).serializeToString(document.documentElement)

On Firefox, the output text is well-formed XML source code. The source DOM tree can be HTML. If XMLSerializer().serializeToString() is applied to a HTML document, it returns a XML-converted source code. For example, img elements ("<img ...>") are converted to the XML's empty-element tag form : "<img ... />"