Skip to main content

Bug Tracker

Side navigation

#11566 closed bug (fixed)

Opened April 07, 2012 02:22AM UTC

Closed June 16, 2012 01:42AM UTC

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:
Description

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
    $(document.body).append(frag);
});

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.

Attachments (0)
Change History (5)

Changed April 07, 2012 02:25AM UTC by dmethvin comment:1

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?

Changed April 07, 2012 02:26AM UTC by dmethvin comment:2

owner: → joeyadams
status: newpending

Changed April 21, 2012 08:20AM UTC by trac-o-bot comment:3

resolution: → invalid
status: pendingclosed

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!

Changed June 16, 2012 01:40AM UTC by dmethvin comment:4

component: unfiledmanipulation
milestone: None1.8
priority: undecidedlow
resolution: invalid
status: closedreopened

Changed June 16, 2012 01:42AM UTC by Vladimir Zhuravlev comment:5

resolution: → fixed
status: reopenedclosed

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

Changeset: 9c28a320c3fa6dcc06de4919d24da41451843570