Bug Tracker

Modify

Ticket #11187 (closed bug: fixed)

Opened 16 months ago

Last modified 10 months ago

jQuery.find() returns unexpected result in Safari

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

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/

<div id='foo'>
  <div id='bar'>Click Me</div>
</div>
$(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"');
});

Change History

comment:1 Changed 16 months ago by rwaldron

  • Priority changed from undecided to high
  • Component changed from unfiled to selector

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.

comment:2 Changed 16 months ago by dmethvin

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

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

comment:3 Changed 16 months ago by maspwr

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.

comment:4 Changed 16 months ago by dmethvin

  • Priority changed from high to low
  • Status changed from new to open

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?

comment:5 Changed 15 months ago by shinigam8@…

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.

comment:6 Changed 15 months ago by timmywil

@dmethvin: that is the plan actually.

comment:7 Changed 12 months ago by timmywil

  • Status changed from open to closed
  • Resolution set to fixed

comment:8 Changed 10 months ago by dmethvin

#12125 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.