Testcase for jQuery Ticket 7263
is( selector ) and filter( selector) behave wrong in some cases
HTML
<ul class="test" style="display:none">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
JavaScript
/*
.is( selector ) has the set of matched elements as context not e.g. document root thus
$( selector ).is( stringselector ) can to be thought of as
$( selector ).filter( stringselector ).length > 0
*/
var log = function (val, expected) {
var color = val === expected ? "green" : "red";
$("body").append("<p style='color:"+color+"'>"+val+"</p>");
};
//safety checks
log($( 'li:first' ).text() == "one", true);
log($( 'li:eq(1)' ).text() == "two", true);
log($( 'li:last' ).text() == "three", true);
//false -> ok because there is only one matched li in the jQuery object
//thus :eq(2) fails :eq(0) would succeed
log($( 'li:first' ).is( 'li:eq(2)' ), false);
//true -> ok because the matched li in the jQuery object
//is the first and last li in the set at the same time
//li:last, li:first both match
log($( 'li:eq(1)' ).is( 'li:last' ), true);
log($( 'li:eq(1)' ).is( 'li:first' ), true);
//true -> ok because the matched li is actually really the first-child of ul.test
log($( 'li:first' ).is( 'li:first-child' ), true);
//false -> ok because the matched li isn't the last-child of ul.test
log($( 'li:first' ).is( 'li:last-child' ), false);
//false -> ok the matched li doesn't have the class test
log($( 'li:first' ).is( '.test' ), false);
//false -> ok the matched li doesn't have li children
log($( 'li:first' ).is( 'li li' ), false);
//true !!! ERROR. The only element matched is a li
//which hasn't the class test and also hasn't any children
//so how can '.test li:last' evaluate to true??
log($( 'li:first' ).is( '.test li:last' ), false);
//true !!! ERROR. The only element matched is a li
//which hasn't the class test and also hasn't any children
//so how can '.test li:last' evaluate to true??
//Did the right thing in jQuery 1.4.2 -> REGRESSION
log($( 'li:first' ).filter( '.test li:last' ).length > 0, false);
//true -> ok if you remove the context the tests work.
log($( 'li:first').filter($('.test li:first')).length > 0, true);
//false ->...