From 2b0e720406c42a4065010ba94e7adb7170ba74c3 Mon Sep 17 00:00:00 2001 From: Sai Wong Date: Mon, 15 Oct 2012 14:20:36 -0400 Subject: [PATCH] Fix #12048. Set attributes for XML fragments. Close gh-965. --- src/attributes.js | 2 +- test/.jshintrc | 1 + test/data/testinit.js | 15 +++++++++++++++ test/unit/attributes.js | 11 +++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/attributes.js b/src/attributes.js index 5a6749982..68e763e09 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -523,7 +523,7 @@ if ( !getSetAttribute ) { // Set the existing or create a new attribute node var ret = elem.getAttributeNode( name ); if ( !ret ) { - ret = document.createAttribute( name ); + ret = elem.ownerDocument.createAttribute( name ); elem.setAttributeNode( ret ); } return ( ret.value = value + "" ); diff --git a/test/.jshintrc b/test/.jshintrc index 11e1c0361..ec1b4fa1e 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -41,6 +41,7 @@ "testIframe": true, "testIframeWithCallback": true, "createDashboardXML": true, + "createXMLFragment": true, "moduleTeardown": true, "testFoo": true, "foobar": true, diff --git a/test/data/testinit.js b/test/data/testinit.js index 99b0f11e1..6f9fd46e6 100644 --- a/test/data/testinit.js +++ b/test/data/testinit.js @@ -102,6 +102,21 @@ var createWithFriesXML = function() { return jQuery.parseXML(string); }; +var createXMLFragment = function() { + var xml, frag; + if ( window.ActiveXObject ) { + xml = new ActiveXObject("msxml2.domdocument"); + } else { + xml = document.implementation.createDocument( "", "", null ); + } + + if ( xml ) { + frag = xml.createElement("data"); + } + + return frag; +}; + var fireNative; if ( document.createEvent ) { fireNative = function( node, type ) { diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 7795b88b5..9ead9a9b9 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -522,6 +522,17 @@ test( "attr(String, Object) - Loaded via XML document", function() { equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" ); }); +test( "attr(String, Object) - Loaded via XML fragment", function() { + expect( 2 ); + var frag = createXMLFragment(), + $frag = jQuery( frag ); + + $frag.attr( "test", "some value" ); + equal( $frag.attr("test"), "some value", "set attribute" ); + $frag.attr( "test", null ); + equal( $frag.attr("test"), undefined, "remove attribute" ); +}); + test( "attr('tabindex')", function() { expect( 8 ); -- 2.39.5