Skip to content
Permalink
Browse files
Fix #13797: .is with single-node context
(cherry picked from commit 4f786ba)
  • Loading branch information
gibson042 committed Apr 20, 2013
1 parent d754b50 commit fb1731a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
@@ -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;

This comment has been minimized.

Copy link
@rwaldron

rwaldron Apr 20, 2013

Member

I suspect we don't have any tests for the iframe use cases? How will this affect seamless iframe use cases?

http://jsfiddle.net/rwaldron/8nHRk/

This comment has been minimized.

Copy link
@gibson042

gibson042 Apr 20, 2013

Author Member

Those cases will fail to match, as they have since at least 1.7.2. And honestly, I wouldn't expect otherwise; passing positional selectors to .is is screwy business, both in terms of ambiguity and of performance. But if we want to support that, it'll look something like jQuery( selector )this.map(function() { return this.ownerDocument; }).add().find( selector ). 😒

},

closest: function( selectors, context ) {
@@ -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() {

0 comments on commit fb1731a

Please sign in to comment.