]> source.dussan.org Git - jquery.git/commitdiff
Fix #12266. IE9/10 says document[0] is document.frames[0]? Close gh-903.
authorElijah Manor <elijah.manor@gmail.com>
Thu, 23 Aug 2012 01:23:50 +0000 (21:23 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Thu, 23 Aug 2012 01:48:02 +0000 (21:48 -0400)
src/manipulation.js
test/data/manipulation/iframe-denied.html [new file with mode: 0644]
test/unit/manipulation.js

index 3cfc97c3b9c63e2da29293361e169ba38f84b0c7..f87e3ca94b309cbdc4310b21a6e3ee1b72df9295 100644 (file)
@@ -489,15 +489,11 @@ jQuery.buildFragment = function( args, context, scripts ) {
                first = args[ 0 ];
 
        // Set context from what may come in as undefined or a jQuery collection or a node
+       // Updated to fix #12266 where accessing context[0] could throw an exception in IE9/10 &
+       // also doubles as fix for #8950 where plain objects caused createDocumentFragment exception
        context = context || document;
-       context = (context[0] || context).ownerDocument || context[0] || context;
-
-       // Ensure that an attr object doesn't incorrectly stand in as a document object
-       // Chrome and Firefox seem to allow this to occur and will throw exception
-       // Fixes #8950
-       if ( typeof context.createDocumentFragment === "undefined" ) {
-               context = document;
-       }
+       context = !context.nodeType && context[0] || context;
+       context = context.ownerDocument || context;
 
        // Only cache "small" (1/2 KB) HTML strings that are associated with the main document
        // Cloning options loses the selected state, so don't cache them
diff --git a/test/data/manipulation/iframe-denied.html b/test/data/manipulation/iframe-denied.html
new file mode 100644 (file)
index 0000000..f2e3e83
--- /dev/null
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html>
+       <head>
+               <meta charset=utf-8 />
+               <title>body</title>
+       </head>
+       <body>
+               <div id="qunit-fixture"></div>
+               <script src="../../../dist/jquery.js"></script>
+               <script>
+                       var script = document.getElementsByTagName( "script" )[ 0 ],
+                               div = document.createElement( "div" ),
+                               src = "http://google.com",
+                               success = true,
+                               error = "";
+
+                       script.parentNode.appendChild( div );
+                       div.innerHTML = "<iframe name=\"test\" src=\"" + src + "\">";
+
+                       jQuery(function() {
+                               try {
+                                       jQuery( "<div>hello<div>world</div>!</div>" ).appendTo( "#qunit-fixture" );
+                               } catch( e ) {
+                                       success = false;
+                                       error = e;
+                               }
+
+                               window.parent.iframeCallback({
+                                       status: success,
+                                       description: "buildFragment sets the context without throwing an exception" +
+                                               ( error ? ": " + error : "" )
+                               });
+                       });
+                       </script>
+       </body>
+</html>
index a3385628205cea2997f3d8f0e767394bab58a020..d63b3ff86b7a9b12088bb07baaf987585f97ecde 100644 (file)
@@ -1933,3 +1933,9 @@ test("checked state is cloned with clone()", function(){
        elem.checked = true;
        equal( jQuery(elem).clone().attr("id","clone")[0].checked, true, "Checked true state correctly cloned" );
 });
+
+testIframeWithCallback( "buildFragment works even if document[0] is iframe's window object in IE9/10 (#12266)", "manipulation/iframe-denied.html", function( test ) {
+       expect( 1 );
+
+       ok( test.status, test.description );
+});