Bug Tracker

Opened 5 years ago

Closed 4 years ago

#11566 closed bug (fixed)

node.append et al. does not work when node is a DocumentFragment

Reported by: joeyadams Owned by: joeyadams
Priority: low Milestone: 1.8
Component: manipulation Version: git
Keywords: Cc:
Blocked by: Blocking:


The following does not work as expected:

jQuery(document).ready(function($) {
    var frag = $(document.createDocumentFragment());
    var h3   = $('<h3>It works</h3>');
    frag.append(h3); // Should append h3 to the DocumentFragment, but doesn't

Namely, when a jQuery object selects a DocumentFragment, .append doesn't append to it. This is because of how the nodeType is tested:

	append: function() {
		return this.domManip(arguments, true, function( elem ) {
			if ( this.nodeType === 1 ) {
				this.appendChild( elem );

A DocumentFragment has a different nodeType than an HTML element, which the implementation of append omits.

Unfortunately, this isn't an isolated issue. The nodeType === 1 test appears all over the jQuery source. The general fix would involve adding || nodeType === 11 in those places, but only when applicable.

DocumentFragment can be used to add many DOM nodes to a page more efficiently. See John Resig's blog post about DocumentFragment for more info.

Change History (5)

comment:1 Changed 5 years ago by dmethvin

As you've pointed out, there is a lot of work involved in trying to make this work. It's not documented to work, somewhat similar to the queasy relationship a jQuery collection has with text or comment nodes. What are the benefits of trying to expand documentFragment to first-class citizenship? What problems become easy to solve that aren't currently easy? If it's an important issue, why has it taken five years for someone to report this?

comment:2 Changed 5 years ago by dmethvin

  • Owner set to joeyadams
  • Status changed from new to pending

comment:3 Changed 5 years ago by trac-o-bot

  • Resolution set to invalid
  • Status changed from pending to closed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

comment:4 Changed 4 years ago by dmethvin

  • Component changed from unfiled to manipulation
  • Milestone changed from None to 1.8
  • Priority changed from undecided to low
  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:5 Changed 4 years ago by Vladimir Zhuravlev

  • Resolution set to fixed
  • Status changed from reopened to closed

Fix #11566, allow appending to DocumentFragment, closes gh-814.

Changeset: 9c28a320c3fa6dcc06de4919d24da41451843570

Note: See TracTickets for help on using tickets.