[whatwg] documentFragment and innerHTML

Aryeh Gregor Simetrical+w3c at gmail.com
Fri Feb 25 13:56:06 PST 2011


On Fri, Feb 25, 2011 at 3:56 PM,  <raf at ratherodd.com> wrote:
> Enter documentFragment. It would be very useful in this scenario to just
> create a documentFragment, chuck the new HTML into it via innerHTML and
> then use insertBefore to put the fragment into the right place in the node
> we want to augment.
>
> I realise this is a niche scenario that is not often encountered, but I'd
> be interested in the rationale behind not implementing it, or whether
> others think it would be a good idea to add this feature.

Isn't this exactly what insertAdjacentHTML() does?
http://www.whatwg.org/specs/web-apps/current-work/multipage/apis-in-html-documents.html#insertadjacenthtml()

On Fri, Feb 25, 2011 at 4:18 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
> If you want to parse this string:
>
>  <form><tr><td>Some text</td></tr></form>
>
> then the DOM it produces depends on whether it's being parsed in the context
> of a <table> parent element or not.  Try setting the innerHTML of a <table>
> and a <div> to the above string, and examine the resulting DOM trees.
>
> That's what I meant when I said that to properly parse a string you need to
> know the context it's being parsed in.

Well, you could create the DocumentFragment using
createContextualFragment() if you liked.  But insertAdjacentHTML() is
a better fit.


More information about the whatwg mailing list