]> source.dussan.org Git - jquery.git/commitdiff
Fix #12048. Set attributes for XML fragments. Close gh-965.
authorSai Wong <sai.wong@huffingtonpost.com>
Mon, 15 Oct 2012 18:20:36 +0000 (14:20 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Sat, 20 Oct 2012 19:27:43 +0000 (15:27 -0400)
src/attributes.js
test/.jshintrc
test/data/testinit.js
test/unit/attributes.js

index 5a674998287e22db25c18d8d079a8167849cdf46..68e763e09beb93b74b367cea7de5a1d60374e417 100644 (file)
@@ -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 + "" );
index 11e1c0361435195bfc5c5e8cca5a5e00d0415875..ec1b4fa1e70f5f45dc448332c288b5d211ca8c57 100644 (file)
@@ -41,6 +41,7 @@
     "testIframe": true,
     "testIframeWithCallback": true,
     "createDashboardXML": true,
+    "createXMLFragment": true,
     "moduleTeardown": true,
     "testFoo": true,
     "foobar": true,
index 99b0f11e163e93b9915723b145822a0d8e1f878a..6f9fd46e651c0dde4caa96f3d083ea0bc0f454da 100644 (file)
@@ -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 ) {
index 7795b88b59e54df31aee3784ccfd62a059872ff5..9ead9a9b9457e7e49d57321d5e97a08fe322cab3 100644 (file)
@@ -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 );