W3C home > Mailing lists > Public > whatwg@whatwg.org > October 2012

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

From: Ian Hickson <ian@hixie.ch>
Date: Tue, 30 Oct 2012 02:10:58 +0000 (UTC)
To: WHAT-WG list <whatwg@whatwg.org>
Message-ID: <Pine.LNX.4.64.1210300200370.5442@ps20323.dreamhostps.com>
On Mon, 29 Oct 2012, Johan Sundström wrote:
> 
> 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.

If you know the document is always going to be in the no-quirks mode, then 
you can just stick "<!DOCTYPE HTML>" at the start. If you need to be able 
to tell what the mode is but are ok with ignoring the "limited quirks" 
mode, then you can use document.compatMode to pick whether to use that 
string or none, as in:

   (document.compatMode == 'CSS1Compat' ? '<!DOCTYPE HTML>' : '') +
   document.documentElement.outerHTML

That will drop any comment nodes around the root element, in case that 
matters. If you want to get the actual DOCTYPE strings, you can make a 
simple serialisation function for doctype nodes that uses the three 
attributes on that object to string together the full thing (much as you 
do in the polyfill you mentioned).


> I believe only Firefox implements "internalSubset" today

Since the "internal subset" has no meaning in text/html, that's ok if your 
goal is just to be semantically equivalent.


> The most useful implementation would IMO be a native one that 
> reproducing the doctype, as it was formatted in the source document.

What's your use case, exactly?


On Mon, 29 Oct 2012, Boris Zbarsky wrote:
> 
> I thought there were plans to put innerHTML on Document.  Did that go 
> nowhere?

Lack of implementor interest killed it a while ago.


On Mon, 29 Oct 2012, Ojan Vafai wrote:
> On Mon, Oct 29, 2012 at 6:17 PM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
> >
> > I thought there were plans to put innerHTML on Document.  Did that go 
> > nowhere?
> 
> There were plans to put in on DocumentFragment.

That was a different plan, but yes, there have also been proposals to do 
that. This was in the context of templates; a better solution to which has 
since been worked on in public-webapps.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
Received on Tuesday, 30 October 2012 02:11:33 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:48:11 GMT