[whatwg] Proposal for window.DocumentType.prototype.toString

Johan Sundström oyasumi at gmail.com
Mon Oct 29 17:58:45 PDT 2012

Hi everybody!

Serializing a complete HTML document DOM to a string is surprisingly
hard in javascript. As a fairly seasoned javascript hacker I figured
this might do it:

  document.doctype + document.documentElement.outerHTML

It doesn't. No browser has a useful window.DocumentType.prototype that
returns either the original document's <!DOCTYPE ...> before parsing –
or a semantically equivalent post-parsing one. Google Chrome shows one
in its devtools, but seems not to export some way of getting at it to

My proposal is we specify this more useful behaviour for
javascript-running browsers, so it does become as simple as above. A
rough sketch of how a polyfill might implement the latter


Even as a polyfill, the above is rather limited, though:  I believe
only Firefox implements "internalSubset" today, and probably only in
XML contexts. The most useful implementation would IMO be a native one
that reproducing the doctype, as it was formatted in the source


 / Johan Sundström, http://ecmanaut.blogspot.com/

More information about the whatwg mailing list