jQuery: The Write Less, Do More JavaScript Library

Ticket #2847 (new bug)

Opened 3 months ago

Last modified 6 days ago

jQuery :first-child selector does not work in IE if comment precedes element

Reported by: westonruter Assigned to: anonymous
Type: bug Priority: major
Milestone: 1.2.4 Component: core
Version: 1.2.3 Keywords:
Cc: Needs: Review

Description

In MEIE 6/7/8b1, the :first-child selector does not work if a comment is the first child; it does not work using either pure CSS nor using the jQuery selector engine. Both, however, work in Firefox and Safari.

Test case: http://weston.ruter.net/projects/test-cases/jquery-first-child-bug/

Attachments

2847.html (6.4 kB) - added by nathanhammond 6 days ago.
In depth test case.
2847.diff (0.8 kB) - added by nathanhammond 6 days ago.
Patch

Change History

Changed 2 months ago by pr

The problem occurs because the first node returned for IE is the comment node. Here is my attempt at fixing it. (checked in IE6/7 and FF2/3).

"first-child": function(a){
  var isFirst = false;
  $.each(a.parentNode.getElementsByTagName("*"), function() {
    // check if the first non-comment node is same as the node.
    return (this.tagName === '!') || ((isFirst = (this == a)) && false);
  });
  return isFirst;
}

Changed 6 days ago by nathanhammond

In depth test case.

Changed 6 days ago by nathanhammond

I've done a much more in-depth test case and determined that this is the only one of the similar selectors that exhibits this bug. It appears that .getElementsByTagName("*") returns comment nodes in IE. I'm working on a patch.

Changed 6 days ago by nathanhammond

Patch

Changed 6 days ago by nathanhammond

To fix this we're going to have to go slower and longer. A more clever patch might be better than the one I've just posted for review.

Note: See TracTickets for help on using tickets.