W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: innerHTML in DocumentFragment

From: Henri Sivonen <hsivonen@iki.fi>
Date: Thu, 10 Nov 2011 13:43:29 +0200
Message-ID: <CAJQvAud3QaLcd6GdeqE7Be+_kpayoi0cY4vcPnOX-QYN4mGzQQ@mail.gmail.com>
To: public-webapps WG <public-webapps@w3.org>
On Fri, Nov 4, 2011 at 1:03 AM, Yehuda Katz <wycats@gmail.com> wrote:
> It would be useful if there was a way to take a String of HTML and parse it
> into a document fragment. This should work even if the HTML string contains
> elements that are invalid in the "in body" insertion mode.
> Something like this code should work:
>   var frag = document.createDocumentFragment();
>   frag.innerHTML = "<tr><td>hello</td></tr>"
>   someTable.appendChild(frag)

It's easy for me to believe that there are valid use cases where the
first tag encountered is <tr>.

> This would probably require a new, laxer insertion mode, which would behave
> similarly to the body insertion mode, but with different semantics in the "A
> start tag whose tag name is one of: "caption", "col", "colgroup", "frame",
> "head", "tbody", "td", "tfoot", "th", "thead", "tr"" case.

What are the use cases for having this work with <head> and <frame> as
first-level tags in the string? Do you also want it work with <html>,
<body> and <frameset>?

What about SVG and MathML elements?

I totally sympathize that this is a problem with <tr>, but developing
a complete solution that works sensibly even when you do stuff like
frag.innerHTML = "<head></head>"
frag.innerHTML = "<head><div></div></head>"
frag.innerHTML = "<frameset></frameset>a<!-- b -->"
frag.innerHTML = "<html><body>foo</html>bar<tr></tr>"
frag.innerHTML = "<html><body>foo</html><tr></tr>"
frag.innerHTML = "<div></div><tr></tr>"
frag.innerHTML = "<tr></tr><div></div>"
frag.innerHTML = "<g><path/></g>"
is a much trickier problem than you <tr> example makes it first seem.

Do you have use cases for tags other than <tr> appearing as the outermost tag?

What would you expect the my examples above to do and why?

-- 
Henri Sivonen
hsivonen@iki.fi
http://hsivonen.iki.fi/
Received on Thursday, 10 November 2011 11:44:06 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:48 GMT