Skip to content

Commit fb1731a

Browse files
committed
Fix #13797: .is with single-node context
(cherry picked from commit 4f786ba)
1 parent d754b50 commit fb1731a

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/traversing.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ jQuery.fn.extend({
5858
},
5959

6060
is: function( selector ) {
61-
return !!selector && (
62-
typeof selector === "string" ?
63-
// If this is a positional/relative selector, check membership in the returned set
64-
// so $("p:first").is("p:last") won't return true for a doc with two "p".
65-
rneedsContext.test( selector ) ?
66-
jQuery( selector, this.context ).index( this[ 0 ] ) >= 0 :
67-
jQuery.filter( selector, this ).length > 0 :
68-
this.filter( selector ).length > 0 );
61+
return !!winnow(
62+
this,
63+
64+
// If this is a positional/relative selector, check membership in the returned set
65+
// so $("p:first").is("p:last") won't return true for a doc with two "p".
66+
typeof selector === "string" && rneedsContext.test( selector ) ?
67+
jQuery( selector ) :
68+
selector || [],
69+
false
70+
).length;
6971
},
7072

7173
closest: function( selectors, context ) {

test/unit/traversing.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,22 @@ test("is() with :has() selectors", function() {
141141
});
142142

143143
test("is() with positional selectors", function() {
144-
expect(24);
145-
146-
var isit = function(sel, match, expect) {
147-
equal( jQuery( sel ).is( match ), expect, "jQuery('" + sel + "').is('" + match + "')" );
148-
};
149-
150-
jQuery(
151-
"<p id='posp'><a class='firsta' href='#'><em>first</em></a><a class='seconda' href='#'><b>test</b></a><em></em></p>"
152-
).appendTo( "#qunit-fixture" );
144+
expect(27);
145+
146+
var
147+
posp = jQuery(
148+
"<p id='posp'><a class='firsta' href='#'><em>first</em></a>" +
149+
"<a class='seconda' href='#'><b>test</b></a><em></em></p>"
150+
).appendTo( "#qunit-fixture" ),
151+
isit = function( sel, match, expect ) {
152+
equal(
153+
jQuery( sel ).is( match ),
154+
expect,
155+
"jQuery('" + sel + "').is('" + match + "')"
156+
);
157+
};
153158

159+
isit( "#posp", "p:last", true );
154160
isit( "#posp", "#posp:first", true );
155161
isit( "#posp", "#posp:eq(2)", false );
156162
isit( "#posp", "#posp a:first", false );
@@ -179,6 +185,9 @@ test("is() with positional selectors", function() {
179185
isit( "#posp em", "#posp a em:eq(2)", false );
180186

181187
ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" );
188+
189+
ok( jQuery( posp[0] ).is("p:last"), "context constructed from a single node (#13797)" );
190+
ok( !jQuery( posp[0] ).find("#firsta").is("a:first"), "context derived from a single node (#13797)" );
182191
});
183192

184193
test("index()", function() {

0 commit comments

Comments
 (0)