Skip to content

Commit 292acd9

Browse files
committed
Make a new jQuery.support.noCloneChecked - splitting apart the previous feature detect relating to clone in IE, fixes the last remaining issue with IE 9 RC. Fixes #8365.
1 parent 3663836 commit 292acd9

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/manipulation.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,27 @@ jQuery.each({
489489
};
490490
});
491491

492+
function getAll( elem ) {
493+
if ( "getElementsByTagName" in elem ) {
494+
return elem.getElementsByTagName( "*" );
495+
496+
} else if ( "querySelectorAll" in elem ) {
497+
return elem.querySelectorAll( "*" );
498+
499+
} else {
500+
return [];
501+
}
502+
}
503+
492504
jQuery.extend({
493505
clone: function( elem, dataAndEvents, deepDataAndEvents ) {
494506
var clone = elem.cloneNode(true),
495507
srcElements,
496508
destElements,
497509
i;
498510

499-
if ( !jQuery.support.noCloneEvent && (elem.nodeType === 1 || elem.nodeType === 11) && !jQuery.isXMLDoc(elem) ) {
511+
if ( (!jQuery.support.noCloneEvent || !jQuery.support.noCloneChecked) &&
512+
(elem.nodeType === 1 || elem.nodeType === 11) && !jQuery.isXMLDoc(elem) ) {
500513
// IE copies events bound via attachEvent when using cloneNode.
501514
// Calling detachEvent on the clone will also remove the events
502515
// from the original. In order to get around this, we use some
@@ -507,8 +520,8 @@ jQuery.extend({
507520

508521
// Using Sizzle here is crazy slow, so we use getElementsByTagName
509522
// instead
510-
srcElements = elem.getElementsByTagName("*");
511-
destElements = clone.getElementsByTagName("*");
523+
srcElements = getAll( elem );
524+
destElements = getAll( clone );
512525

513526
// Weird iteration because IE will replace the length property
514527
// with an element if you are cloning the body and one of the
@@ -520,21 +533,18 @@ jQuery.extend({
520533

521534
// Copy the events from the original to the clone
522535
if ( dataAndEvents ) {
523-
524536
cloneCopyEvent( elem, clone );
525537

526-
if ( deepDataAndEvents && "getElementsByTagName" in elem ) {
527-
528-
srcElements = elem.getElementsByTagName("*");
529-
destElements = clone.getElementsByTagName("*");
538+
if ( deepDataAndEvents ) {
539+
srcElements = getAll( elem );
540+
destElements = getAll( clone );
530541

531-
if ( srcElements.length ) {
532-
for ( i = 0; srcElements[i]; ++i ) {
533-
cloneCopyEvent( srcElements[i], destElements[i] );
534-
}
542+
for ( i = 0; srcElements[i]; ++i ) {
543+
cloneCopyEvent( srcElements[i], destElements[i] );
535544
}
536545
}
537546
}
547+
538548
// Return the cloned set
539549
return clone;
540550
},

src/support.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
var all = div.getElementsByTagName("*"),
1313
a = div.getElementsByTagName("a")[0],
1414
select = document.createElement("select"),
15-
opt = select.appendChild( document.createElement("option") );
15+
opt = select.appendChild( document.createElement("option") ),
16+
input = div.getElementsByTagName("input")[0];
1617

1718
// Can't get basic test support
1819
if ( !all || !all.length || !a ) {
@@ -51,7 +52,7 @@
5152
// Make sure that if no value is specified for a checkbox
5253
// that it defaults to "on".
5354
// (WebKit defaults to "" instead)
54-
checkOn: div.getElementsByTagName("input")[0].value === "on",
55+
checkOn: input.value === "on",
5556

5657
// Make sure that a selected-by-default option has a working selected property.
5758
// (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
@@ -62,12 +63,16 @@
6263
optDisabled: false,
6364
checkClone: false,
6465
noCloneEvent: true,
66+
noCloneChecked: true,
6567
boxModel: null,
6668
inlineBlockNeedsLayout: false,
6769
shrinkWrapBlocks: false,
6870
reliableHiddenOffsets: true
6971
};
7072

73+
input.checked = true;
74+
jQuery.support.noCloneChecked = input.cloneNode( true ).checked;
75+
7176
// Make sure that the options inside disabled selects aren't marked as disabled
7277
// (WebKit marks them as diabled)
7378
select.disabled = true;

test/unit/manipulation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,16 @@ test("append(Function) with incoming value", function() {
402402
});
403403

404404
test("append the same fragment with events (Bug #6997, 5566)", function () {
405-
expect(2 + (document.fireEvent ? 1 : 0));
405+
var doExtra = !jQuery.support.noCloneEvent && document.fireEvent;
406+
expect(2 + (doExtra ? 1 : 0));
406407
stop(1000);
407408

408409
var element;
409410

410411
// This patch modified the way that cloning occurs in IE; we need to make sure that
411412
// native event handlers on the original object don't get disturbed when they are
412413
// modified on the clone
413-
if (!jQuery.support.noCloneEvent && document.fireEvent) {
414+
if ( doExtra ) {
414415
element = jQuery("div:first").click(function () {
415416
ok(true, "Event exists on original after being unbound on clone");
416417
jQuery(this).unbind('click');
@@ -1015,7 +1016,7 @@ test("clone(form element) (Bug #3879, #6655)", function() {
10151016

10161017
equals( clone.is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
10171018
equals( clone[0].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
1018-
equals( clone[0].defaultChecked, !jQuery.support.noCloneEvent, "Checked input defaultChecked cloned correctly" );
1019+
equals( clone[0].defaultChecked, !jQuery.support.noCloneChecked, "Checked input defaultChecked cloned correctly" );
10191020

10201021
element = jQuery("<input type='text' value='foo'>");
10211022
clone = element.clone();

0 commit comments

Comments
 (0)