From: danilsomsikov Date: Fri, 11 Jan 2013 15:04:50 +0000 (+0100) Subject: Fix #8335: Avoid memory leak by never setting data on non-element non-document nodes... X-Git-Tag: 1.9.1~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8f72967ee2090d4b0d38a59b2e4743e0ce7a1b5e;p=jquery.git Fix #8335: Avoid memory leak by never setting data on non-element non-document nodes. Ref gh-1127. (cherry picked from commit cc324abf739669bd2a4669742c994b86c4ad467b) --- diff --git a/src/data.js b/src/data.js index d5a25ff6c..eaa483813 100644 --- a/src/data.js +++ b/src/data.js @@ -223,6 +223,11 @@ jQuery.extend({ // A method for determining if a DOM node can handle the data expando acceptData: function( elem ) { + // Do not set data on non-element because it will not be cleared (#8335). + if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 ) { + return false; + } + var noData = elem.nodeName && jQuery.noData[ elem.nodeName.toLowerCase() ]; // nodes accept data unless otherwise specified; rejection can be conditional diff --git a/test/unit/data.js b/test/unit/data.js index c09149b65..8f86d00c4 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -133,8 +133,16 @@ test("Expando cleanup", 4, function() { jQuery(div).remove(); }); +test("Data is not being set on comment and text nodes", function() { + expect(2); + + ok( !jQuery.hasData( jQuery("").data("foo", 0) ) ); + ok( !jQuery.hasData( jQuery("text").contents().data("foo", 0) ) ); + +}); + test("jQuery.acceptData", function() { - expect(7); + expect(9); ok( jQuery.acceptData( document ), "document" ); ok( jQuery.acceptData( document.documentElement ), "documentElement" ); @@ -149,6 +157,9 @@ test("jQuery.acceptData", function() { var applet = document.createElement("object"); applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"); ok( !jQuery.acceptData( applet ), "applet" ); + + ok( !jQuery.acceptData( document.createComment("") ), "comment" ); + ok( !jQuery.acceptData( document.createTextNode("") ), "text" ); }); test(".data()", function() {