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

Johan Sundström oyasumi at gmail.com
Tue Oct 30 23:00:49 PDT 2012

On Tue, Oct 30, 2012 at 3:20 AM, Stewart Brodie
<stewart.brodie at antplc.com> wrote:
>> Hi everybody!
>> Serializing a complete HTML document DOM to a string is surprisingly
>> hard in javascript.
> Does XMLSerializer().serializeToString(document) not meet your requirement?

Ah – good thinking. (new XMLSerializer).serializeToString(document)
does indeed do a pretty excellent job of it, including the crazy hacks
people do with conditional comments outside of the root node, which I
hadn't figured I would be able to piece back together from an already
parsed page.

While I hate to admit it, maybe on some level there is benefit to much
of the DOM APIs being javascript hostile to force you towards the
occasional really well-paved paths like the above, when you can find

My use case was taking as good a snapshot of an already live web
page's structure from a non-privileged bookmarklet, for archival
purposes (i e essentially what a curl of the page would do). For my
purposes, it is a bonus that I actually get the current state of the
page with whatever DOM mods have transpired since it loaded rather
than what curl would produce, so I think XMLSerializer is a good

That said, I would still much enjoy a future where
javascript:alert(document.doctype) would tell you something rich about
the page that we today need deep knowledge of document.compatMode
and/or combinations of XMLSerializer and parsers, or deep study of
DocumentType refdocs to tease out.

Is there a case against it in people using it where they ought to pick
other solutions?

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

> --
> Stewart Brodie
> Team Leader - ANT Galio Browser
> ANT Software Limited

More information about the whatwg mailing list