]> source.dussan.org Git - jquery.git/commitdiff
Fail silently if closest is somehow called on a document. Fixes #10726.
authorTimmy Willison <timmywillisn@gmail.com>
Wed, 25 Jul 2012 20:05:48 +0000 (16:05 -0400)
committerTimmy Willison <timmywillisn@gmail.com>
Wed, 25 Jul 2012 20:05:59 +0000 (16:05 -0400)
src/traversing.js
test/unit/traversing.js

index 321c25197273d95a0e12b677dd3a4e239d0fa7bf..baac0399e9e885b61890cb6a6dd98ae0edd33c95 100644 (file)
@@ -92,16 +92,13 @@ jQuery.fn.extend({
                for ( ; i < l; i++ ) {
                        cur = this[i];
 
-                       while ( cur ) {
+                       while ( cur && cur.ownerDocument && cur !== context && cur.nodeType !== 11 ) {
                                if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
                                        ret.push( cur );
                                        break;
 
                                } else {
                                        cur = cur.parentNode;
-                                       if ( !cur || !cur.ownerDocument || cur === context || cur.nodeType === 11 ) {
-                                               break;
-                                       }
                                }
                        }
                }
index 2712e7f2fe2dd29946ae71e2d98db3a379143c27..7f9678307c655d97b9be8212f96c4320ad0af415 100644 (file)
@@ -275,7 +275,8 @@ test("filter() with positional selectors", function() {
 });
 
 test("closest()", function() {
-       expect(13);
+       expect( 14 );
+
        deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
        deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
        deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
@@ -299,6 +300,8 @@ test("closest()", function() {
        // Bug #7369
        equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
        equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
+
+       ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
 });
 
 test("closest(jQuery)", function() {