Skip to main content

Bug Tracker

Side navigation

#11187 closed bug (fixed)

Opened January 18, 2012 08:01AM UTC

Closed May 29, 2012 12:06AM UTC

Last modified July 23, 2012 02:21PM UTC

jQuery.find() returns unexpected result in Safari

Reported by: maspwr Owned by:
Priority: low Milestone: None
Component: selector Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

Using jQuery.find() with an element as the argument returns a jQuery object representing that element, even if the element has been removed from the DOM. The expected behavior is for find() to return an empty list after the element has been removed from the DOM.

The unexpected behavior has been observed in Safari 5.1.2 (7534.52.7).

The expected behavior has been observed in Chrome 16.0.912.75 and FireFox 8.0.1.

A short example is pasted below. You can try out two examples of this at the following links:

http://jsfiddle.net/jYbvU/

http://jsfiddle.net/UpWMV/

#!xml
<div id='foo'>
  <div id='bar'>Click Me</div>
</div>
#!js
$(document).on('click.weird', function(event) {
    console.log($(document).find(event.target));
});

$('#bar').on('click', function(event) {
    $('#foo').html('<h1>Replaced</h1>');
    console.log('Replaced "#foo"');
});
Attachments (0)
Change History (8)

Changed January 18, 2012 02:54PM UTC by rwaldron comment:1

component: unfiledselector
priority: undecidedhigh

This is interesting, take a look at a modified version of the test... http://jsfiddle.net/rwaldron/R8SMW/

It might not be something we can address, my first thought is that the event handler is firing before the node is actually removed.

Changed January 18, 2012 04:23PM UTC by dmethvin comment:2

Just to continue the debugging, the phenomenon doesn't occur with pure DOM:

http://jsfiddle.net/R8SMW/2/

Changed January 18, 2012 05:24PM UTC by maspwr comment:3

One more example. Searching from 'html' instead of document does not find the element.

http://jsfiddle.net/maspwr/pEKca/

This suggests to me that it has been removed from the DOM tree to some extent, but it is still represented in the document somehow.

Changed February 07, 2012 10:44PM UTC by dmethvin comment:4

priority: highlow
status: newopen

Okay, more stuff dumped:

http://jsfiddle.net/R8SMW/8/

Still fishy in Safari. When a DOM element is used in .find() we take the element and use .contains or .compareDocumentPosition to see if it is within the jQuery set. It seems like Safari implements .contains but we should be checking .compareDocumentPosition instead, perhaps we need to change the order in Sizzle?

Changed February 23, 2012 02:41PM UTC by shinigam8@yahoo.com.br comment:5

I cant't get acess content of iframe in the same domain, using Google Chrome 17.

It isn't works:

$('iframe').contents().find('body').height();

I want resize the iframe using the content of iframe height.

Changed February 23, 2012 03:20PM UTC by timmywil comment:6

@dmethvin: that is the plan actually.

Changed May 29, 2012 12:06AM UTC by timmywil comment:7

resolution: → fixed
status: openclosed

Changed July 23, 2012 02:21PM UTC by dmethvin comment:8

#12125 is a duplicate of this ticket.