Bug when a DOM element is passed to find() ?
Hi,
I am jquery newbee and I *think* I ran into a bug with the find()
method
Details:
jquery version : 1.2.1 (but looking at 1.2.2 I think it has the same
problem)
Browser : IE 6 & 7
Steps to reproduce:
<html>
<head>
<script src="lib/jquery/jquery.js" type="text/javascript"
charset="utf-8">
</script>
<script type="text/javascript">
$(document).ready(function(){
var y=$('p')[0];
$("#load").find(y); //Should bomb on this line
})
</script>
</head>
<body>
ABC
This is some data here
<div id="load">
XYZ
</div>
</body>
</html>
In IE I get the infamous "Object doesn't support this property or
method."
I could debug it down to the following block:
find: function(t) {
var data = jQuery.map(this, function(a){ return
jQuery.find(t,a); });
return this.pushStack( /[^+>] [^+>]/.test( t ) || t.indexOf("..") >
-1 ?
jQuery.unique( data ) : data );
},
The regex /[^+>] [^+>]/.test( t ) returns true in FF2 but false in IE
From what I understand the regex and the indexOf look for strings and
not DOM elements.
I am not sure what the documented behavior is, for a RegExp.test on an
non string object esp. a DOM element.
Interestingly this is what I found:
Running this on the given HTML page gave mixed results in FF2 and IE
6/7
var p = $('p')[0]
/.*/.exec(p) [0]
evaluates to
"[object HTMLParagraphElement]" in FF2 and
"[object]" in IE.
Since the regexp is looking for a non +|> followed by a space followed
by a non +|>
FF2 evaluates the regex.test( t ) to true (it matches "t H") and short
circuits the || condition
In IE it evaluates to false and bombs on indexOf as the || is not
short circuited and indexOf is not present on t.
FYI, I ran into this bug while using idrag.js of interface 1.2,
inspite of the warning that it works only with jquery 1.1.4 :)
I eventually changed it from $(xx).find(el) to $(el,xx) and got things
to work.
From what I understand from the jQuery docs, the find expects only a
string as an argument but not DOM elements.
Technically this is a bug because it is not supposed to work!
Suggestion:
Check the input to see if it is a string always so that it fails
consistently in all browsers if we pass in a DOM element. If not , its
easy to hit other kinds of x-broswer bugs
Typically its easier to fix it with FF than go through the pain of
finding it in IE, like I did :)
p.s: flipping the tests in the || condition - i.e check for indexOf
first - will definitely bomb in all browsers - but its a bad idea -
its not maintainable code ;)
If I understood the documentation wrongly, and DOM elements are indeed
allowed then its a bug in IE and needs a fix anyway!
Thanks,
-Ajay