[jQuery] Fix for IE XML nested each issues (Bug #164)

[jQuery] Fix for IE XML nested each issues (Bug #164)

The current version of jQuery has trouble when trying to access methods such as text() or parent() or attr() inside an each statement within the context of an XML document. For a test case, see <a href="http://jquery.com/dev/bugs/bug/164/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
Bug #164</a>. The general assumption with this bug has been that the problem lies in methods such as text(), and therefore it would be impossible to determine whether the object in question was an XML object or a HTML object and act on it accordingly. While this bug has been closed, it is crucial for what I'm using jQuery for, so I decided to debug it and have found a simple yet (as far as I can tell) effective solution to the bug which works in both IE and Firefox, and should work fine in other browsers as well.
The problem itself does not actually lie within the text() method, but instead is caused by this statement within the primary jQuery function body:
// Handle HTML strings
var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
As such, any time $(this) is called within the each function body the error is thrown, not just when specific methods such as text() or parent() are called. The error seems to be caused by 'a' being an object instead of a string and IE barfing when trying to execute the regular expression on 'a'.
The fix is quite simple, as far as I can tell... simply replace the line in question with this:
// Handle HTML strings
var m;
if (typeof a == "string") m = /^[^<]*(<.+>)[^>]*$/.exec(a);
Because the regular expression should only be effective when it's executed on a string anyway, this prevents the IE error from being thrown and still preserves the functionality otherwise. In my limited testing so far, I have yet to find any other side effects to this.
Replacing the statement above fixes the text() and parent() functions, however the attr() function still requires some prodding to make it work correctly in an XML context within IE. The troublesome lines within the attr: declaration are (~line 706):
} else if ( elem.getAttribute != undefined ) {
    if ( value != undefined ) elem.setAttribute( name, value );
    return elem.getAttribute( name, 2 );
For whatever reason, IE throws an error when checking whether
elem.getAttribute is defined, complaining about an invalid parameter list (despite the fact that it's just a check for existence). When typeof elem.getAttribute is called in IE, the type is reported as "unknown", which certainly doesn't help things. IE also doesn't like the function call to
elem.getAttribute with two parameters, so the return statement would have to be changed too. The fix once again is relatively simple. Before the block above, add another similar block that looks like this:
} else if (
jQuery.browser.msie && elem.getAttribute(name) != undefined ) {
    if ( value != undefined ) elem.setAttribute( name, value );
    return elem.getAttribute( name );
This makes attr() work in both IE and Firefox, although I haven't tested in other browsers as of yet.
One last XML related fix I've found within jQuery. When using Xpath expressions, jQuery has a custom expression which allows matching against the content of an element, such as $("p:contains('test')"), which will match all

tags which contain text in their body. Once again, IE has trouble with this when processing it in an XML context. The fix is simple, change this line (~line 520):
contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
to this:
contains: "((a.firstChild && a.firstChild.nodeValue)||a.innerText||a.innerHTML).indexOf(m[3])>=0",
And it works as expected in Firefox and IE (and, once again, presumably in other browsers too).
I hope these fixes are as much a lifesaver for others as they are for me--most of what I'm doing in jQuery is based on processing XML, and not having the ability to use jQuery functionality within each loops was proving a major pain. I'm not familiar enough yet with SVN or the jQuery checkin process to check these changes into SVN myself--if it's better for me to check them in than for someone more familiar with the process, let me know and I'll work on that tonight.
Any testing in browsers other than IE or Firefox would also be appreciated--the modifications shouldn't really affect any browser other than IE, but I'm not yet familiar enough with Javascript to say that with any degree of certainty.
Cheers,
~Peter Woods
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/