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

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 UTC