[whatwg] createContextualFragment in detached contexts

Erik Arvidsson arv at chromium.org
Thu Sep 29 15:37:04 PDT 2011

Currently there is no easy way to create a tbody (and others from
markup). To do this correctly people have to go through ugly hacks
like what jQuery does [1] where they wrap the HTML string with the
right context elements and then grab the nodes inside that.

I would like to propose a change to createContextualFragment [2].

If the context object is in a detached state, then relax the parsing
rules so that all elements are allowed at that level. The hand wavy
explanation is that for every tag at the top level create a new
element in the same way that ownerDocument.createElement would do it.
This would allow the following to work.

var df = document.createRange().createContextualFragment('<tbody><tr><td>Hi</tbody>
text <option></option>');
assertEquals(3, df.childNodes.length);
assertEquals('TBODY', df.childNodes[0].tagName);
assertEquals(' text ', df.childNodes[1].textContent);
assertEquals('OPTION', df.childNodes[2].tagName);

[1] https://github.com/jquery/jquery/blob/master/src/manipulation.js#L643
[2] http://html5.org/specs/dom-parsing.html#dom-range-createcontextualfragment


More information about the whatwg mailing list