Skip to content

Commit 10901f7

Browse files
elijahmanordmethvin
authored andcommitted
Fix #12266. IE9/10 says document[0] is document.frames[0]? Close jquerygh-903.
1 parent ae1d2b3 commit 10901f7

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/manipulation.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,11 @@ jQuery.buildFragment = function( args, context, scripts ) {
489489
first = args[ 0 ];
490490

491491
// Set context from what may come in as undefined or a jQuery collection or a node
492+
// Updated to fix #12266 where accessing context[0] could throw an exception in IE9/10 &
493+
// also doubles as fix for #8950 where plain objects caused createDocumentFragment exception
492494
context = context || document;
493-
context = (context[0] || context).ownerDocument || context[0] || context;
494-
495-
// Ensure that an attr object doesn't incorrectly stand in as a document object
496-
// Chrome and Firefox seem to allow this to occur and will throw exception
497-
// Fixes #8950
498-
if ( typeof context.createDocumentFragment === "undefined" ) {
499-
context = document;
500-
}
495+
context = !context.nodeType && context[0] || context;
496+
context = context.ownerDocument || context;
501497

502498
// Only cache "small" (1/2 KB) HTML strings that are associated with the main document
503499
// Cloning options loses the selected state, so don't cache them
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8 />
5+
<title>body</title>
6+
</head>
7+
<body>
8+
<div id="qunit-fixture"></div>
9+
<script src="../../../dist/jquery.js"></script>
10+
<script>
11+
var script = document.getElementsByTagName( "script" )[ 0 ],
12+
div = document.createElement( "div" ),
13+
src = "http://google.com",
14+
success = true,
15+
error = "";
16+
17+
script.parentNode.appendChild( div );
18+
div.innerHTML = "<iframe name=\"test\" src=\"" + src + "\">";
19+
20+
jQuery(function() {
21+
try {
22+
jQuery( "<div>hello<div>world</div>!</div>" ).appendTo( "#qunit-fixture" );
23+
} catch( e ) {
24+
success = false;
25+
error = e;
26+
}
27+
28+
window.parent.iframeCallback({
29+
status: success,
30+
description: "buildFragment sets the context without throwing an exception" +
31+
( error ? ": " + error : "" )
32+
});
33+
});
34+
</script>
35+
</body>
36+
</html>

test/unit/manipulation.js

+6
Original file line numberDiff line numberDiff line change
@@ -1933,3 +1933,9 @@ test("checked state is cloned with clone()", function(){
19331933
elem.checked = true;
19341934
equal( jQuery(elem).clone().attr("id","clone")[0].checked, true, "Checked true state correctly cloned" );
19351935
});
1936+
1937+
testIframeWithCallback( "buildFragment works even if document[0] is iframe's window object in IE9/10 (#12266)", "manipulation/iframe-denied.html", function( test ) {
1938+
expect( 1 );
1939+
1940+
ok( test.status, test.description );
1941+
});

0 commit comments

Comments
 (0)