Skip to main content

Bug Tracker

Side navigation

#2997 closed bug (fixed)

Opened June 06, 2008 09:25AM UTC

Closed February 18, 2009 01:28AM UTC

Last modified March 14, 2012 04:43PM UTC

$.clone() bugs in IE when $.length>1 and elements don't belong to document

Reported by: shazam Owned by: flesler
Priority: minor Milestone: 1.3
Component: core Version: 1.2.6
Keywords: clone Cc:
Blocked by: Blocking:
Description

If you are manipulating elements in a child iframe, the clone method tries to do some manips inserting nodes belonging to child iframe document into main frame document.

The clone method should be replace by this one which uses this.ownerDocument when possible

	clone: function( events ) {
		// Do the clone
		var ret = this.map(function(){
			if ( jQuery.browser.msie && !jQuery.isXMLDoc(this) ) {
				// IE copies events bound via attachEvent when
				// using cloneNode. Calling detachEvent on the
				// clone will also remove the events from the orignal
				// In order to get around this, we use innerHTML.
				// Unfortunately, this means some modifications to
				// attributes in IE that are actually only stored
				// as properties will not be copied (such as the
				// the name attribute on an input).
				var clone = this.cloneNode(true),
					container = (this.ownerDocument||document).createElement("div");
				container.appendChild(clone);
				return jQuery.clean([container.innerHTML], this.ownerDocument||document)[0];
			} else
				return this.cloneNode(true);
		});

Attachments (2)
  • cloneBug.rar (1.2 KB) - added by shazam June 11, 2008 10:41AM UTC.
  • clone[5790].diff (1.1 KB) - added by flesler July 22, 2008 05:01PM UTC.

    This is the patch, but it breaks many many tests :(

Change History (9)

Changed June 06, 2008 09:29AM UTC by shazam comment:1

Title is wrong and should be $.clone() bugs in IE

The clone function is not completely false and only the begining has been written in the post

Changed June 06, 2008 09:36AM UTC by shazam comment:2

Title is wrong and should be '$.clone() bugs in IE'

The clone function is not completely false and only the begining has been written in the post

Changed June 10, 2008 08:14PM UTC by flesler comment:3

need: PatchTest Case
priority: majorminor

So, does that cause an error or what ?

Could you provide a test case ? a minimalistic html file with the requires html and js to reproduce the problem ?

And please detail the error or odd behavior.

Thanks.

Changed June 11, 2008 10:40AM UTC by shazam comment:4

Yes this causes an 'invalid argument' error when you manipulate elements in a child iframe.

I took the time to make a complete sample.

I also made a better fix to the problem which is well explained in the sample.

So, this part of the $.clone() function :

var clone = this.cloneNode(true),
	container = (this.ownerDocument||document).createElement("div");
container.appendChild(clone);
return jQuery.clean([container.innerHTML], this.ownerDocument||document)[0];

Should be replaced by :

// As this line is executed only in IE we can use the outerHTML property 
// and we use this.ownerDocument in the $.clean() function to avoid cross document bug
return jQuery.clean([this.outerHTML], this.ownerDocument||document)[0];

Changed June 12, 2008 10:58PM UTC by flesler comment:5

owner: → flesler
status: newassigned

Right, clever proposition, could be reduced to:

return jQuery.clean([this.outerHTML], this)[0];

jQuery.clean will handle the rest. I'll assign this to myself and will check this asap.

This obviously needs a lot of testing. Will surely be in for the next release if all goes well.

Thanks

Changed June 12, 2008 10:59PM UTC by flesler comment:6

need: Test CaseCommit

Changed July 19, 2008 03:57AM UTC by Xiao Xu comment:7

Replying to [comment:2 shazam]:

Title is wrong and should be '$.clone() bugs in IE' The clone function is not completely false and only the begining has been written in the post

So do you know the situation about clone a node from child frame to parent window?

Regards

Changed October 17, 2008 10:54AM UTC by antix comment:8

This works..

var clone = this.cloneNode(true),

container = this.ownerDocument.createElement("div");

container.appendChild(clone);

return jQuery.clean([container.innerHTML], this)[0];

Changed February 18, 2009 01:28AM UTC by dmethvin comment:9

resolution: → fixed
status: assignedclosed

Fixed at [6186].