]> source.dussan.org Git - jquery.git/commitdiff
Fix #13797: .is with single-node context
authorRichard Gibson <richard.gibson@gmail.com>
Sat, 20 Apr 2013 15:40:27 +0000 (11:40 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Sat, 20 Apr 2013 15:51:51 +0000 (11:51 -0400)
(cherry picked from commit 4f786ba4d2a5544cb48f589d2659d6cab84efc34)

src/traversing.js
test/unit/traversing.js

index e6b5913e2dd2739b8fb801e50df93709be19a6ab..5e9fe19438b0e4a6ab55ab740dba1b1d35f75bcd 100644 (file)
@@ -58,14 +58,16 @@ jQuery.fn.extend({
        },
 
        is: function( selector ) {
-               return !!selector && (
-                       typeof selector === "string" ?
-                               // If this is a positional/relative selector, check membership in the returned set
-                               // so $("p:first").is("p:last") won't return true for a doc with two "p".
-                               rneedsContext.test( selector ) ?
-                                       jQuery( selector, this.context ).index( this[ 0 ] ) >= 0 :
-                                       jQuery.filter( selector, this ).length > 0 :
-                               this.filter( selector ).length > 0 );
+               return !!winnow(
+                       this,
+
+                       // If this is a positional/relative selector, check membership in the returned set
+                       // so $("p:first").is("p:last") won't return true for a doc with two "p".
+                       typeof selector === "string" && rneedsContext.test( selector ) ?
+                               jQuery( selector ) :
+                               selector || [],
+                       false
+               ).length;
        },
 
        closest: function( selectors, context ) {
index d99de436a204d5e2801221a57429ba8d54236e4e..e45ab75a554bdf4d932cd27b96b973b74b3d9d17 100644 (file)
@@ -141,16 +141,22 @@ test("is() with :has() selectors", function() {
 });
 
 test("is() with positional selectors", function() {
-       expect(24);
-
-       var isit = function(sel, match, expect) {
-               equal( jQuery( sel ).is( match ), expect, "jQuery('" + sel + "').is('" + match + "')" );
-       };
-
-       jQuery(
-               "<p id='posp'><a class='firsta' href='#'><em>first</em></a><a class='seconda' href='#'><b>test</b></a><em></em></p>"
-       ).appendTo( "#qunit-fixture" );
+       expect(27);
+
+       var
+               posp = jQuery(
+                       "<p id='posp'><a class='firsta' href='#'><em>first</em></a>" +
+                       "<a class='seconda' href='#'><b>test</b></a><em></em></p>"
+               ).appendTo( "#qunit-fixture" ),
+               isit = function( sel, match, expect ) {
+                       equal(
+                               jQuery( sel ).is( match ),
+                               expect,
+                               "jQuery('" + sel + "').is('" + match + "')"
+                       );
+               };
 
+       isit( "#posp", "p:last", true );
        isit( "#posp", "#posp:first", true );
        isit( "#posp", "#posp:eq(2)", false );
        isit( "#posp", "#posp a:first", false );
@@ -179,6 +185,9 @@ test("is() with positional selectors", function() {
        isit( "#posp em", "#posp a em:eq(2)", false );
 
        ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" );
+
+       ok( jQuery( posp[0] ).is("p:last"), "context constructed from a single node (#13797)" );
+       ok( !jQuery( posp[0] ).find("#firsta").is("a:first"), "context derived from a single node (#13797)" );
 });
 
 test("index()", function() {