Make sure that [name=foo] and #id selectors don't use the native methods on XML documents (since id and name attributes may not be defined by a DTD and will return nothing instead. Fixes jQuery bug #3945.

This commit is contained in:
John Resig 2009-01-21 23:19:30 +00:00
parent f9a5849723
commit 7d9d210540
2 changed files with 15 additions and 9 deletions

View File

@ -55,7 +55,7 @@ var Sizzle = function(selector, context, results, seed) {
} else { } else {
var ret = seed ? var ret = seed ?
{ expr: parts.pop(), set: makeArray(seed) } : { expr: parts.pop(), set: makeArray(seed) } :
Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context ); Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) );
set = Sizzle.filter( ret.expr, ret.set ); set = Sizzle.filter( ret.expr, ret.set );
if ( parts.length > 0 ) { if ( parts.length > 0 ) {
@ -120,7 +120,7 @@ Sizzle.matches = function(expr, set){
return Sizzle(expr, null, null, set); return Sizzle(expr, null, null, set);
}; };
Sizzle.find = function(expr, context){ Sizzle.find = function(expr, context, isXML){
var set, match; var set, match;
if ( !expr ) { if ( !expr ) {
@ -135,7 +135,7 @@ Sizzle.find = function(expr, context){
if ( left.substr( left.length - 1 ) !== "\\" ) { if ( left.substr( left.length - 1 ) !== "\\" ) {
match[1] = (match[1] || "").replace(/\\/g, ""); match[1] = (match[1] || "").replace(/\\/g, "");
set = Expr.find[ type ]( match, context ); set = Expr.find[ type ]( match, context, isXML );
if ( set != null ) { if ( set != null ) {
expr = expr.replace( Expr.match[ type ], "" ); expr = expr.replace( Expr.match[ type ], "" );
break; break;
@ -315,14 +315,16 @@ var Expr = Sizzle.selectors = {
} }
}, },
find: { find: {
ID: function(match, context){ ID: function(match, context, isXML){
if ( context.getElementById ) { if ( typeof context.getElementById !== "undefined" && !isXML ) {
var m = context.getElementById(match[1]); var m = context.getElementById(match[1]);
return m ? [m] : []; return m ? [m] : [];
} }
}, },
NAME: function(match, context){ NAME: function(match, context, isXML){
return context.getElementsByName ? context.getElementsByName(match[1]) : null; if ( typeof context.getElementsByName !== "undefined" && !isXML ) {
return context.getElementsByName(match[1]);
}
}, },
TAG: function(match, context){ TAG: function(match, context){
return context.getElementsByTagName(match[1]); return context.getElementsByTagName(match[1]);

View File

@ -21,11 +21,15 @@ test("element", function() {
}); });
if ( location.protocol != "file:" ) { if ( location.protocol != "file:" ) {
test("Element Selector with underscore", function() { test("XML Document Selectors", function() {
expect(1); expect(5);
stop(); stop();
jQuery.get("data/with_fries.xml", function(xml) { jQuery.get("data/with_fries.xml", function(xml) {
equals( jQuery("foo_bar", xml).length, 1, "Element Selector with underscore" ); equals( jQuery("foo_bar", xml).length, 1, "Element Selector with underscore" );
equals( jQuery("property[name=prop2]", xml).length, 1, "Attribute selector with name" );
equals( jQuery("[name=prop2]", xml).length, 1, "Attribute selector with name" );
equals( jQuery("#seite1", xml).length, 1, "Attribute selector with name" );
equals( jQuery("component#seite1", xml).length, 1, "Attribute selector with name" );
start(); start();
}); });
}); });