W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2011

[whatwg] createContextualFragment in detached contexts

From: Erik Arvidsson <arv@chromium.org>
Date: Thu, 29 Sep 2011 15:37:04 -0700
Message-ID: <CAJ8+GohfkEupi6OpOgFeXN=25gJCKDqnVq-A=fqFtHae6_bLEw@mail.gmail.com>
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

Received on Thursday, 29 September 2011 15:37:04 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:36 UTC