Skip to content

Commit 590bcab

Browse files
gibson042dmethvin
authored andcommitted
Fix #11543: .has should work on detached elements.
1 parent c04bfce commit 590bcab

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/traversing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jQuery.fn.extend({
5454
},
5555

5656
has: function( target ) {
57-
var targets = jQuery( target );
57+
var targets = jQuery( target, this );
5858
return this.filter(function() {
5959
for ( var i = 0, l = targets.length; i < l; i++ ) {
6060
if ( jQuery.contains( this, targets[i] ) ) {

test/unit/traversing.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,27 @@ test("not(jQuery)", function() {
370370
});
371371

372372
test("has(Element)", function() {
373-
expect(2);
373+
expect(3);
374374

375375
var obj = jQuery("#qunit-fixture").has(jQuery("#sndp")[0]);
376376
deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have the element as a descendant" );
377377

378+
var detached = jQuery("<a><b><i/></b></a>");
379+
deepEqual( detached.has( detached.find("i")[0] ).get(), detached.get(), "...Even when detached" );
380+
378381
var multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp")[0]);
379382
deepEqual( obj.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
380383
});
381384

382385
test("has(Selector)", function() {
383-
expect(3);
386+
expect(4);
384387

385388
var obj = jQuery("#qunit-fixture").has("#sndp");
386389
deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have any element matching the selector as a descendant" );
387390

391+
var detached = jQuery("<a><b><i/></b></a>");
392+
deepEqual( detached.has("i").get(), detached.get(), "...Even when detached" );
393+
388394
var multipleParent = jQuery("#qunit-fixture, #header").has("#sndp");
389395
deepEqual( obj.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
390396

@@ -393,11 +399,14 @@ test("has(Selector)", function() {
393399
});
394400

395401
test("has(Arrayish)", function() {
396-
expect(3);
402+
expect(4);
397403

398404
var simple = jQuery("#qunit-fixture").has(jQuery("#sndp"));
399405
deepEqual( simple.get(), q("qunit-fixture"), "Keeps elements that have any element in the jQuery list as a descendant" );
400406

407+
var detached = jQuery("<a><b><i/></b></a>");
408+
deepEqual( detached.has( detached.find("i") ).get(), detached.get(), "...Even when detached" );
409+
401410
var multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp"));
402411
deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
403412

0 commit comments

Comments
 (0)